Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Trisha gee concurrentprogrammingusingthedisruptor

  • Login to see the comments

Trisha gee concurrentprogrammingusingthedisruptor

  1. 1. Concurrent Programming Using The Disruptor Trisha Gee, Senior Developer at LMAX
  2. 2. The Disruptor?• Open Source• Message Passing
  3. 3. The Magic RingBuffer
  4. 4. Example 1:One Publisher, One Processor
  5. 5. First, create your Eventpublic class SimpleEvent { public static final EventFactory<SimpleEvent> EVENT_FACTORY = new SimpleEventFactory(); private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "SimpleEvent{" + "value=" + value + + }; } private static class SimpleEventFactory implements EventFactory<SimpleEvent> { public SimpleEvent newInstance() { return new SimpleEvent(); } }}
  6. 6. Then create your EventHandlerpublic class SimpleEventHandler implements EventHandler<SimpleEvent>{ private List<String> valuesSeen = new ArrayList<String>(); @Override public void onEvent(final SimpleEvent event, final long sequence, final boolean endOfBatch) throws Exception { valuesSeen.add(event.getValue()); } public int getNumberOfEventsProcessed() { return valuesSeen.size(); } public List<String> getEventValuesSeen() { return valuesSeen; }}
  7. 7. Then wire it all togetherfinal RingBuffer<SimpleEvent> ringBuffer = new RingBuffer<SimpleEvent>(SimpleEvent.EVENT_FACTORY, new SingleThreadedClaimStrategy(RING_BUFFER_SIZE), new YieldingWaitStrategy());final SimpleEventHandler eventHandler = new SimpleEventHandler();final BatchEventProcessor<SimpleEvent> eventProcessor = new BatchEventProcessor<SimpleEvent>(ringBuffer, ringBuffer.newBarrier(), eventHandler);eventHandler.setSequence(eventProcessor.getSequence());ringBuffer.setGatingSequences(eventProcessor.getSequence());
  8. 8. Great. But Boring.
  9. 9. Example 2:One Publisher, Three Processors
  10. 10. ...and in the Disruptor?
  11. 11. A more complicated Eventpublic class WriteTrackingEvent { public static final EventFactory<> EVENT_FACTORY = new MyEventFactory(); private Values redValues = new Values(); private Values blueValues = new Values(); private Result result; public void setResult(Result result) { this.result = result; } private static final class MyEventFactory implements EventFactory<WriteTrackingEvent> { public WriteTrackingEvent newInstance() { return new WriteTrackingEvent(); } }}
  12. 12. ...a couple of EventHandlerspublic class BlueEventHandler implements EventHandler<WriteTrackingEvent> { @Override public void onEvent(WriteTrackingEvent event, long sequence, final boolean endOfBatch) throws Exception { event.getBlueValues().setTime(System.currentTimeMillis()); System.out.println("blue: " + event); }}public class RedEventHandler implements EventHandler<WriteTrackingEvent> { @Override public void onEvent(WriteTrackingEvent event, long sequence, boolean endOfBatch) throws Exception { event.getRedValues().setTime(System.currentTimeMillis()); System.out.println("red: " + event); }}
  13. 13. ...a final EventHandlerpublic class SummaryEventHandler implements EventHandler<WriteTrackingEvent> { private int numberOfEventsProcessed = 0; public void onEvent(WriteTrackingEvent event, long sequence, boolean endOfBatch) throws Exception { numberOfEventsProcessed++; final long blueTimestamp = event.getBlueValues().getTimestamp(); final long redTimestamp = event.getRedValues().getTimestamp(); WriteTrackingEvent.Result result; if (blueTimestamp < redTimestamp) { result = WriteTrackingEvent.Result.BLUE_FIRST; } else if (redTimestamp < blueTimestamp) { result = WriteTrackingEvent.Result.RED_FIRST; } else { result = WriteTrackingEvent.Result.SAME_TIME; } event.setResult(result); System.out.println("final: " + event); } public int getNumberOfEventsProcessed() { return numberOfEventsProcessed; }}
  14. 14. Put it all together, and what have you got?RingBuffer<WriteTrackingEvent> ringBuffer = new RingBuffer<WriteTrackingEvent>(WriteTrackingEvent.EVENT_FACTORY, new SingleThreadedClaimStrategy(RING_BUFFER_SIZE), new YieldingWaitStrategy());SequenceBarrier colourSequenceBarrier = ringBuffer.newBarrier();BatchEventProcessor<WriteTrackingEvent> redEventProcessor = new BatchEventProcessor<WriteTrackingEvent>(ringBuffer, colourSequenceBarrier, new RedEventHandler());BatchEventProcessor<WriteTrackingEvent> blueEventProcessor = new BatchEventProcessor<WriteTrackingEvent>(ringBuffer, colourSequenceBarrier, new BlueEventHandler());SequenceBarrier summarySequenceBarrier = ringBuffer.newBarrier(redEventProcessor.getSequence(), blueEventProcessor.getSequence());BatchEventProcessor<WriteTrackingEvent> finalEventProcessor = new BatchEventProcessor<WriteTrackingEvent>(ringBuffer, summarySequenceBarrier, new SummaryEventHandler());ringBuffer.setGatingSequences(finalEventProcessor.getSequence());
  15. 15. What’s all that gubbins on the RingBuffer? RingBuffer<WriteTrackingEvent> ringBuffer = newBuffer<WriteTrackingEvent>(WriteTrackingEvent.EVENT_FA new SingleThreadedClaimStrategy(RING_B new YieldingWaitStrategy());
  16. 16. Is that it?• Multiple publishers• Different EventHandlers• The Wizard
  17. 17. More Information• Blog• Wiki• Presentations
  18. 18. Q&A• We are hiring!• careers@lmax.com

×