Dissecting the Disruptor: Writing to the ring buffer
Join the DZone community and get the full member experience.Join For Free
the important areas are: not wrapping the ring; informing the consumers; batching for producers; and how multiple producers work.
the disruptor code has interfaces and helper classes for the consumers, but there's no interface for your producer, the thing that writes to the ring buffer. that's because nothing else needs to access your producer, only you need to know about it. however, like the consuming side, a producerbarrier is created by the ring buffer and your producer will use this to write to it.
writing to the ring buffer involves a two-phase commit. first, your producer has to claim the next slot on the buffer. then, when the producer has finished writing to the slot, it will call commit on the producerbarrier.
so let's look at the first bit. it sounds easy - "get me the next slot on the ring buffer". well, from your producer's point of view it is easy. you simply call nextentry() on the producerbarrier. this will return you an entry object which is basically the next slot in the ring buffer.
the producerbarrier makes sure the ring buffer doesn't wrap
under the covers, the producerbarrier is doing all the negotiation to figure out what the next slot is, and if you're allowed to write to it yet.
(i'm not convinced the shiny new graphics tablet is helping the clarity of my pictures, but it's fun to use).
for this illustration, we're going to assume there's only one producer writing to the ring buffer. we will deal with the intricacies of multiple producers later.
the consumertrackingproducerbarrier has a list of all the consumers that are accessing the ring buffer. now to me this seemed a bit odd - i wouldn't expect the producerbarrier to know anything about the consuming side. but wait, there is a reason. because we don't want the "conflation of concerns" a queue has (it has to track the head and tail which are sometimes the same point), our consumers are responsible for knowing which sequence number they're up to, not the ring buffer. so, if we want to make sure we don't wrap the buffer, we need to check where the consumers have got to.
in the diagram above, one consumer is happily at the same point as the highest sequence number (12, highlighted in red/pink). the second consumer is a bit behind - maybe it's doing i/o operations or something - and it's at sequence number 3. therefore consumer 2 has the whole length of the buffer to go before it catches up with consumer 1.
the producer wants to write to the slot on the ring buffer currently occupied by sequence 3, because this slot is the one after the current ring buffer cursor. but the producerbarrier knows it can't write here because a consumer is using it. so the producerbarrier sits and spins, waiting, until the consumers move on.
claiming the next slot
now imagine consumer 2 has finished that batch of entries, and moves its sequence number on. maybe it got as far as sequence 9 (in real life i expect it will make it as far as 12 because of the way consumer batching works, but that doesn't make the example as interesting).
the diagram above shows what happens when consumer 2 updates to sequence number 9. i've slimmed down the consumerbarrier in this picture because it takes no active part in this scene.
the producerbarrier sees that the next slot, the one that had sequence number 3, is now available. it grabs the entry that sits in this slot (i've not talked specifically about the entry class, but it's basically a bucket for stuff you want to put into the ring buffer slot which has a sequence number), sets the sequence number on the entry to the next sequence number (13) and returns this entry to your producer. the producer can then write whatever value it wants into this entry.
committing the new value
the second phase of the two-stage commit is, well, the commit.
the green represents our newly updated entry with sequence 13 - yeah, i'm sorry, i'm red-green colour-blind too. but other colours were even more rubbish.
when the producer has finished writing stuff into the entry it tells the producerbarrier to commit it.
the producerbarrier waits for the ring buffer cursor to catch up to where we are (for a single producer this will always be a bit pointless - e.g. we know the cursor is already at 12, nothing else is writing to the ring buffer). then the producerbarrier updates the ring buffer cursor to the sequence number on the updated entry - 13 in our case. next, the producerbarrier lets the consumers know there's something new in the buffer. it does this by poking the waitstrategy on the consumerbarrier - "oi, wake up! something happened!" (note - different waitstrategy implementations deal with this in different ways, depending upon whether it's blocking or not).
now consumer 1 can get entry 13, consumer 2 can get everything up to and including 13, and they all live happily ever after.
interestingly the disruptor can batch on the producer side as well as on the consumer side . remember when consumer 2 finally got with the programme and found itself at sequence 9? there is a very cunning thing the producerbarrier can do here - it knows the size of the buffer, and it knows where the slowest consumer is. so it can figure out which slots are now available.
if the producerbarrier knows the ring buffer cursor is at 12, and the slowest consumer is at 9, it can let producers write to slots 3, 4, 5, 6, 7 and 8 before it needs to check where the consumers are.
you thought i was done, but there's more.
i slightly lied in some of the above drawings. i implied that the sequence number the producerbarrier deals with comes directly from the ring buffer's cursor. however, if you look at the code you'll see that it uses the claimstrategy to get this. i skipped this to simplify the diagrams, it's not so important in the single-producer case.
with multiple producers, you need yet another thing tracking a sequence number. this is the sequence that is available for writing to. note that this is not the same as ring-buffer-cursor-plus-one - if you have more than one producer writing to the buffer, it's possible there are entries in the process of being written that haven't been committed yet.
let's revisit claiming a slot. each producer asks the claimstrategy for the next available slot. producer 1 gets sequence 13, like in the single producer case above. producer 2 gets sequence 14, even though the ring buffer cursor is still only pointing to 12, because the claimsequence is dishing out the numbers and has been keeping track of what's been allocated.
so each producer has its own slot with a shiny new sequence number.
i'm going colour producer 1 and its slot in green, and producer 2 and its slot in a suspiciously pink-looking purple.
now producer 1 wakes up from its coma and asks to commit entry 13 (green arrows are sparked by the request from producer 1). the producerbarrier tells the claimstrategy to wait for the ring buffer cursor to get to 12, which it already had of course. so the ring buffer cursor is incremented to 13, and the producerbarrier pokes the waitstrategy to let everything know the ring buffer was updated. now the producerbarrier can finish the request from producer 2, increment the ring buffer cursor to 14, and let everyone know that we're done.
you'll see that the ring buffer retains the ordering implied by the order of the initial nextentry() calls, even if the producers finish writing at different times. it also means that if a producer is causing a pause in writing to the ring buffer, when it unblocks any other pending commits can happen immediately.
phew. and i managed to describe all that without mentioning a memory barrier once.
Opinions expressed by DZone contributors are their own.