Top Banner
Concurrent Programming Using The Disruptor Trisha Gee LMAX Wednesday, 23 May 12
102

Concurrent Programming Using The Disruptor - Copenhagen

May 17, 2015

Download

Technology

Trisha Gee

The version of my Disruptor talk given at GOTO Copenhagen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrent Programming Using The Disruptor - Copenhagen

Concurrent Programming Using The Disruptor

Trisha GeeLMAX

Wednesday, 23 May 12

Page 2: Concurrent Programming Using The Disruptor - Copenhagen

Concurrent Programming Using The Disruptor

Trisha Gee, Developer at LMAX@trisha_gee

mechanitis.blogspot.com

Wednesday, 23 May 12

Page 3: Concurrent Programming Using The Disruptor - Copenhagen

The Disruptor?

Wednesday, 23 May 12

Page 4: Concurrent Programming Using The Disruptor - Copenhagen

What I’m covering

• Overview of the Disruptor

• Create your own!

• Turn it up to Eleven

• Q&A

Wednesday, 23 May 12

Page 5: Concurrent Programming Using The Disruptor - Copenhagen

What is it?

• Data structure and work flow with no contention.

• Very fast message passing.

• Allows you to go truly parallel.

Wednesday, 23 May 12

Page 6: Concurrent Programming Using The Disruptor - Copenhagen

So...?

Wednesday, 23 May 12

Page 7: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 8: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 9: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 10: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 11: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 12: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 13: Concurrent Programming Using The Disruptor - Copenhagen

The Magic RingBuffer

Wednesday, 23 May 12

Page 14: Concurrent Programming Using The Disruptor - Copenhagen

Creating a RingBuffer

final RingBuffer<SimpleEvent> ringBuffer = new RingBuffer<SimpleEvent>(SimpleEvent.EVENT_FACTORY, RING_BUFFER_SIZE);

Wednesday, 23 May 12

Page 15: Concurrent Programming Using The Disruptor - Copenhagen

The Events are Buckets

Wednesday, 23 May 12

Page 16: Concurrent Programming Using The Disruptor - Copenhagen

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(); } } }

Wednesday, 23 May 12

Page 17: Concurrent Programming Using The Disruptor - Copenhagen

I’ve got a RingBuffer!

• Erm.... how do I poke things into it?

Wednesday, 23 May 12

Page 18: Concurrent Programming Using The Disruptor - Copenhagen

The Publisher

Wednesday, 23 May 12

Page 19: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 20: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 21: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 22: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 23: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 24: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 25: Concurrent Programming Using The Disruptor - Copenhagen

What do I do?

SimpleEventTranslator translator = new SimpleEventTranslator();

EventPublisher<SimpleEvent> publisher = new EventPublisher<SimpleEvent>(ringBuffer);

// poke your translator here// ...and when you’re done...publisher.publishEvent(translator);

public class SimpleEventTranslator implements EventTranslator<SimpleEvent>

Wednesday, 23 May 12

Page 26: Concurrent Programming Using The Disruptor - Copenhagen

...so now I want to read

• The Disruptor provides nice batching behaviour for free

Wednesday, 23 May 12

Page 27: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 28: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 29: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 30: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 31: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 32: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 33: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 34: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 35: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 36: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 37: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 38: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 39: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 40: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 41: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 42: Concurrent Programming Using The Disruptor - Copenhagen

...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 } }

Wednesday, 23 May 12

Page 43: Concurrent Programming Using The Disruptor - Copenhagen

Shiny. So what?

Wednesday, 23 May 12

Page 44: Concurrent Programming Using The Disruptor - Copenhagen

Let’s go parallel

Wednesday, 23 May 12

Page 45: Concurrent Programming Using The Disruptor - Copenhagen

And now for something different...

Wednesday, 23 May 12

Page 46: Concurrent Programming Using The Disruptor - Copenhagen

Remember Henry Ford?

Wednesday, 23 May 12

Page 47: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 48: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 49: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 50: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 51: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 52: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 53: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 54: Concurrent Programming Using The Disruptor - Copenhagen

Complex workflow...

Wednesday, 23 May 12

Page 55: Concurrent Programming Using The Disruptor - Copenhagen

What on Earth has this got to do with RingBuffers?!

Wednesday, 23 May 12

Page 56: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 57: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 58: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 59: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 60: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 61: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 62: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 63: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 64: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 65: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 66: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 67: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 68: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 69: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 70: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 71: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 72: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 73: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 74: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 75: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 76: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 77: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 78: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 79: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 80: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 81: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 82: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 83: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 84: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 85: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 86: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 87: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 88: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 89: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 90: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 91: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 92: Concurrent Programming Using The Disruptor - Copenhagen

Wednesday, 23 May 12

Page 93: Concurrent Programming Using The Disruptor - Copenhagen

Don’t wrap the buffer!

ringBuffer.setGatingSequences(finalEventProcessor.getSequence());

Wednesday, 23 May 12

Page 94: Concurrent Programming Using The Disruptor - Copenhagen

Caveats

Wednesday, 23 May 12

Page 95: Concurrent Programming Using The Disruptor - Copenhagen

Is that it?

• Wait and claim strategies

• Batch publishing

• Multiple publishers

• Different EventHandlers

• The Wizard

• You don’t even need a RingBuffer...

Wednesday, 23 May 12

Page 96: Concurrent Programming Using The Disruptor - Copenhagen

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

Wednesday, 23 May 12

Page 97: Concurrent Programming Using The Disruptor - Copenhagen

More Information

• Google Code Site, including Wikihttp://code.google.com/p/disruptor/

• Blogs, e.g. mine: mechanitis.blogspot.com

• Presentations

• Google Group

Wednesday, 23 May 12

Page 98: Concurrent Programming Using The Disruptor - Copenhagen

Q&A

Wednesday, 23 May 12

Page 99: Concurrent Programming Using The Disruptor - Copenhagen

WorkerPool

Wednesday, 23 May 12

Page 100: Concurrent Programming Using The Disruptor - Copenhagen

AggregateEventHandler

Wednesday, 23 May 12

Page 101: Concurrent Programming Using The Disruptor - Copenhagen

WaitStrategies

• BlockingWaitStrategy

• BusySpinWaitStrategy

• SleepingWaitStrategy

• YieldingWaitStrategy

Wednesday, 23 May 12

Page 102: Concurrent Programming Using The Disruptor - Copenhagen

ClaimStrategies

• SingleThreadedClaimStrategy

• MultiThreadedClaimStrategy

• MultiThreadedLowContentionClaimStrategy

Wednesday, 23 May 12