Presented to the London Java Community at Skillsmatter on 1st March 2012.
Full presentation can be viewed here: http://skillsmatter.com/podcast/home/the-disruptor/js-3798
15. Great! I want one!
public class SimpleEvent {
public static final EventFactory<SimpleEvent> EVENT_FACTORY =
new SimpleEventFactory();
private volatile String value;
private static class SimpleEventFactory implements EventFactory<SimpleEvent> {
@Override
public SimpleEvent newInstance() {
return new SimpleEvent();
}
}
}
16. I’ve got a RingBuffer!
• Erm.... how do I poke things into it?
24. What do I do?
public class SimpleEventTranslator implements
EventTranslator<SimpleEvent>
SimpleEventTranslator translator = new SimpleEventTranslator();
EventPublisher<SimpleEvent> publisher =
new EventPublisher<SimpleEvent>(ringBuffer);
// poke your translator here
// ...and when you’re done...
publisher.publishEvent(translator);
25. ...so now I want to read
• The Disruptor provides nice batching
behaviour for free
41. ...and all you need is...
public class SimpleEventHandler implements EventHandler<SimpleEvent>
{
@Override
public void onEvent(final SimpleEvent event,
final long sequence,
final boolean endOfBatch) throws Exception {
// do stuff
}
}
94. Is that it?
• Wait and claim strategies
• Multiple publishers
• Different EventHandlers
• The Wizard
95. You get...
• A framework the encourages you to model
your domain
• The ability to run in parallel but single-
threaded
• Reliable ordering
• ...and it can be very fast
96. More Information
• Google Code Site, including Wiki
http://code.google.com/p/disruptor/
• Blogs, e.g. mine: mechanitis.blogspot.com
• Presentations
• Google Group
Who has heard of it?\nWho has seen other presentations or read blogs?\nWho has played with it?\n Open Source\n Message Passing\n
What it does, and how it works\nThe really cool stuff\n
Uncontended data structure * except multiple producers\nWay to pass data between threads very fast\n\n
That&#x2019;s just words\n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
It is not, in fact, magic\nArray-backed ring buffer\nSequence numbers\nNothing magic\n not going to touch on: how the ring buffer doesn&#x2019;t wrap; \n
Generics to type it\n
Garbage friendly\nReuse the events\n
Garbage friendly - event factory\n\n
\n
Producer, writer...\nBlack & White classes are provided by the Disruptor\nColoured ones are your own\n
\n
\n
\n
\n
\n
\n
\n
\n
Disruptor provides a BatchEventProcessor \nfor heavy lifting\n
Again, coloured classes for you\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
Fast, uncontended replacement for a queue\nvery garbage friendly (or can be)\n
Uncontended means things can really work in parallel\n\nHas anyone seen this before?\n
\n
Imagine the world&#x2019;s most rubbish car\n
\n
\n
\n
\n
\n
\n
Tada!\n
\n
\n
\n
\n
\n
Sequence barriers\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
Maybe putting engine in takes a while\nMaybe it needs to go to a database to get the &#x201C;engine&#x201D;\nPassenger seat might be retrieved from another webservice\n\n
\n
Dude can put engines in more than one car\nDude will write engines into different cars in the ring buffer\nThis is the only thing that can write this field\n
Maybe the seats are retrieved from another service\n
\n
3 cars\n
2 cars\n
4 cars\n
\n
Bonnet is dead fast. Maybe it&#x2019;s calculating the result based on what&#x2019;s already in the ringbuffer\n\n
Bonnet handler writes results into 33\n
Front door handler is sloooow. Maybe it got caught waiting for something else, or it&#x2019;s sub-optimal\nAll door handlers ask their sequence barrier for the next available sequence\nthis is the lowest of the sequence numbers that barrier is tracking\nin that case, it&#x2019;s 35 - rear seat handler\n
In this case its 35\n
front door handler is waaaaay behind\nhe&#x2019;s going to have to put his door into a bunch of cars\n
\n
\n
\n
\n
Not going to show writing to the buffer\n
Wheels blocked because they&#x2019;re already at 27\nRingBuffer is blocked cos it can&#x2019;t write any more\nBack-pressure\n
\n
\n
\n
\n
Can&#x2019;t run the car demo at full speed on a machine with two cores\nFields have to be volatile\nStill low contention, but you are contending upon CPU\nYour ring buffer should be bigger than 12\n
\n
Martin Fowler talks about DDD this morning. Aggregates.\nTake care of separate concerns in separate processes\nSharding\nJoe Armstrong spoke about handling errors outside of your process. Can have processes outside of your business logic\n\n\n
\n
Feedback - any interest in a workshop?\nWhat else do you want to know about?\n.net version\n