80
An Introduction to the LMAX Disruptor Trisha Gee, Developer at LMAX Exchange @trisha_gee mechanitis.blogspot.com Tuesday, 25 September 12

Workshop: Introduction to the Disruptor

Embed Size (px)

DESCRIPTION

Trisha Gee of LMAX ran a workshop at Strange Loop 2012 giving a hands on set of exercises demonstrating the syntax of the Disruptor

Citation preview

Page 1: Workshop: Introduction to the Disruptor

An Introduction to the LMAX DisruptorTrisha Gee, Developer at LMAX Exchange

@trisha_geemechanitis.blogspot.com

Tuesday, 25 September 12

Page 2: Workshop: Introduction to the Disruptor

Laptop Setup... Go!

Tuesday, 25 September 12

Page 3: Workshop: Introduction to the Disruptor

The Disruptor?

Tuesday, 25 September 12

Page 4: Workshop: Introduction to the Disruptor

The Workshop

• Introduction to the Disruptor

• Simplest Case

• Performance Results

• Going Parallel

• The Real World

• Questions

Tuesday, 25 September 12

Page 5: Workshop: Introduction to the Disruptor

What is The Disruptor?

• Very fast message passing

• Data structure and work flow without contention

• Allows you to go truly parallel

Tuesday, 25 September 12

Page 6: Workshop: Introduction to the Disruptor

So...?

Tuesday, 25 September 12

Page 7: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 8: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 9: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 10: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 11: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 12: Workshop: Introduction to the Disruptor

The Magic RingBuffer

Tuesday, 25 September 12

Page 13: Workshop: Introduction to the Disruptor

The Events are Buckets

Tuesday, 25 September 12

Page 14: Workshop: Introduction to the Disruptor

I’ve got a RingBuffer!

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

Tuesday, 25 September 12

Page 15: Workshop: Introduction to the Disruptor

The Publisher

Tuesday, 25 September 12

Page 16: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 17: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 18: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 19: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 20: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 21: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 22: Workshop: Introduction to the Disruptor

...so now I want to read

• The Disruptor provides nice batching behaviour for free

Tuesday, 25 September 12

Page 23: Workshop: Introduction to the Disruptor

BatchEventProcessor

Tuesday, 25 September 12

Page 24: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 25: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 26: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 27: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 28: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 29: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 30: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 31: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 32: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 33: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 34: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 35: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 36: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 37: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 38: Workshop: Introduction to the Disruptor

The Problem

Tuesday, 25 September 12

Page 39: Workshop: Introduction to the Disruptor

Tower Defence?

Tuesday, 25 September 12

Page 40: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 41: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 42: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 43: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 44: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 45: Workshop: Introduction to the Disruptor

Exercise OneOne publisher, one event handler

Tuesday, 25 September 12

Page 46: Workshop: Introduction to the Disruptor

Our Problem

Tuesday, 25 September 12

Page 47: Workshop: Introduction to the Disruptor

Simplest Disruptor

Tuesday, 25 September 12

Page 48: Workshop: Introduction to the Disruptor

• Print status of the enemy when the tower sees it

• Print status of enemy after the tower shoots it

• Shoot all enemies

TODO

Tuesday, 25 September 12

Page 49: Workshop: Introduction to the Disruptor

Task Breakdown

• Wire up the EventTranslator & EventHandler, using the Disruptor class

• Implement shootAt

• Implement respawn & isDead

• Get unit tests to pass

• (check out mechanitis.examples.disruptor)

Tuesday, 25 September 12

Page 50: Workshop: Introduction to the Disruptor

Exercise One Goals

1. Understand the roles of Translators and Event Handlers

2. Understand how to wire up a simple Disruptor configuration

3. Understand how to get events flowing through the Disruptor

Tuesday, 25 September 12

Page 51: Workshop: Introduction to the Disruptor

Performance vs a Queue

Mac Air, 1.7GHz Core i5:run 0: BlockingQueue=3,390,060 Disruptor=69,108,500 ops/secrun 1: BlockingQueue=5,229,851 Disruptor=71,123,755 ops/secrun 2: BlockingQueue=4,303,481 Disruptor=65,530,799 ops/sec

Disruptor - Time Taken: 3,242 millisDisruptor - Time Taken: 3,319 millisDisruptor - Time Taken: 4,045 millis

Queue - Time Taken: 30,022 millisQueue - Time Taken: 31,823 millisQueue - Time Taken: 29,280 millis

Tuesday, 25 September 12

Page 52: Workshop: Introduction to the Disruptor

Exercise TwoOne publisher, two parallel event handlers

Tuesday, 25 September 12

Page 53: Workshop: Introduction to the Disruptor

Let’s log all the enemies

Tuesday, 25 September 12

Page 54: Workshop: Introduction to the Disruptor

Parallel Processors

Tuesday, 25 September 12

Page 55: Workshop: Introduction to the Disruptor

• Make each enemy unique

• Write the details of each enemy generated into a text file

Tuesday, 25 September 12

Page 56: Workshop: Introduction to the Disruptor

Task Breakdown

• Add randomness to enemy (e.g. give them a speed to travel at)

• Wire in the EnemyLogger

• Implement describeTo in enemy

Tuesday, 25 September 12

Page 57: Workshop: Introduction to the Disruptor

Exercise Two Goals

1. Understand that EventHandlers can process in parallel

2. Understand how to wire up EventHandlers in parallel

3. Understand the caveats to going parallel - single writer principal

Tuesday, 25 September 12

Page 58: Workshop: Introduction to the Disruptor

Exercise ThreeDiamond Dependencies

Tuesday, 25 September 12

Page 59: Workshop: Introduction to the Disruptor

Recording the Results

Tuesday, 25 September 12

Page 60: Workshop: Introduction to the Disruptor

Sequence Barriers

Tuesday, 25 September 12

Page 61: Workshop: Introduction to the Disruptor

Managing Dependencies

Tuesday, 25 September 12

Page 62: Workshop: Introduction to the Disruptor

• Don’t use the Disruptor Wizard for configuration

• Record results of the game to a different text file

• Optional: add variability to Turret

Tuesday, 25 September 12

Page 63: Workshop: Introduction to the Disruptor

Task Breakdown

• Remove use of Disruptor.java and wire up manually (mechanitis.examples.disruptor)

• Wire in ResultHandler

• Add missing things from Enemy class

Tuesday, 25 September 12

Page 64: Workshop: Introduction to the Disruptor

Exercise Three Goals

1. Understand sequence barriers

2. Understand different dependency configurations

3. Understand wiring up without the wizard

Tuesday, 25 September 12

Page 65: Workshop: Introduction to the Disruptor

Exercise FourPlaytime!!

Tuesday, 25 September 12

Page 66: Workshop: Introduction to the Disruptor

Slow Consumers?

Tuesday, 25 September 12

Page 67: Workshop: Introduction to the Disruptor

The Real World

Tuesday, 25 September 12

Page 68: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 69: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 70: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 71: Workshop: Introduction to the Disruptor

Ticket Purchasing

Tuesday, 25 September 12

Page 72: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 73: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 74: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 75: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 76: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 77: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 78: Workshop: Introduction to the Disruptor

Tuesday, 25 September 12

Page 79: Workshop: Introduction to the Disruptor

Other Uses

• Queue replacement

• Single-threading your code

• Filtering

• Coalescing

• Dependency graphs

Tuesday, 25 September 12

Page 80: Workshop: Introduction to the Disruptor

Questions

Tuesday, 25 September 12