How LMAX's Disruptor work with Multiple producer with a shared variable?

4.2k Views Asked by At

I am new to Disruptor. I have the following two queries:

Q1. I got a sample code of one producer to one consumer and one producer to multiple dependent consumers,

I like to get a sample code for multiple producers to multiple consumers or a Sequencer: 3P – 1C. Can you please refer any blogs or code sample to me?

Q2. This is a generic question regarding in a multiple producer environment, how one producer result will inter related on previous producer's result in a ring buffer.

for example: Is Disruptor can use in a environment where a single file/variable is getting updated by multiple producers. i.e there are two producers (P1,P2), Which are updating a single shared variable (named as "count").(Here count is a instance variable in ValueEvent class)

Initially the “count” value is 0.

Producer P1 will add 1 with the "count" current value. So after producer P1 processed, the value of count will be (0+1) = 1.

Producer P2 will add 2 with the "count" current value, So after producer P2 processed, the value of count will be (1+2) = 3.

Basically, P2 needs to read the updated "count" value (done by P1) from the ring buffer and add the incremented value(2).

How we can maintain the order of execution of producers?(P2 will execute always after P1 execution.)

At the consumer side, consumers (C1,C2) will read the "count" value as sequentially(1,3,.,.,.). This is ok, as in ring buffer, each consumer will read the ring buffer value in sequential order only.

Thanks, Prasenjit.

2

There are 2 best solutions below

0
On

q1: No examples - but assuming you are using the Java implementation you need to use a ProducerBarrier if you want multiple genuinely independent producers.

q2: But your Q2 tells me you have not quite got independent producers in your disruptor. Since there is a dependency P1 then P2 then {C1,C2}. Think about P2 being a "consumer" that updates a different variable (not the same variable that P1 updates), and C1&C2 follow P2.

For example: P1 does count=0+1. P2 does count2=1+2=3. C1&C2 read "count2" variable. Then set up your disruptor to have P1->P2->{C1,C2}. P2 won't start processing until P1 is finished. C1 & C2 won't start processing until P2 is finished.

If you are after peak performance you'll have to take care about cache line sharing between the two counters.

0
On
  1. I have written a sample project to illustrate multiple producer - single consumer. Here is the link: http://krishnansrinivasan.wordpress.com/2012/07/29/using-disruptor-net-with-wcf-2/

  2. Producers never update the state of a variable that is abstracted by the RingBuffer. The Ringbuffer only helps in strict sequencing of calls to the consumer, based on the order in which it receives the message from the producer.