SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
Concurrent Programming
  Using The Disruptor
  Trisha Gee, Senior Developer at LMAX
The Disruptor?



• Open Source
• Message Passing
The Magic RingBuffer
Example 1:
One Publisher, One Processor
First, create your Event
public 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();
        }
    }
}
Then create your
                  EventHandler
public 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;
    }
}
Then wire it all together
final 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());
Great. But Boring.
Example 2:
One Publisher, Three Processors
...and in the Disruptor?
A more complicated
              Event
public 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();
        }
    }
}
...a couple of
                 EventHandlers
public 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);
    }
}
...a final EventHandler
public 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;
    }
}
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());
What’s all that gubbins
        on the RingBuffer?
      RingBuffer<WriteTrackingEvent> ringBuffer = new
Buffer<WriteTrackingEvent>(WriteTrackingEvent.EVENT_FA
          new SingleThreadedClaimStrategy(RING_B
                        new YieldingWaitStrategy());
Is that it?

• Multiple publishers
• Different EventHandlers
• The Wizard
More Information

• Blog
• Wiki
• Presentations
Q&A


• We are hiring!
• careers@lmax.com

Más contenido relacionado

La actualidad más candente

Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
Fwdays
 

La actualidad más candente (20)

Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Smart Contract programming 101 with Solidity #PizzaHackathon
Smart Contract programming 101 with Solidity #PizzaHackathonSmart Contract programming 101 with Solidity #PizzaHackathon
Smart Contract programming 101 with Solidity #PizzaHackathon
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
 
Improved alerting with Prometheus and Alertmanager
Improved alerting with Prometheus and AlertmanagerImproved alerting with Prometheus and Alertmanager
Improved alerting with Prometheus and Alertmanager
 
$q and Promises in AngularJS
$q and Promises in AngularJS $q and Promises in AngularJS
$q and Promises in AngularJS
 
Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"Anton Moldovan "Load testing which you always wanted"
Anton Moldovan "Load testing which you always wanted"
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context Library#Gophercon Talk by Smita Vijayakumar - Go's Context Library
#Gophercon Talk by Smita Vijayakumar - Go's Context Library
 
Android dev 3
Android dev 3Android dev 3
Android dev 3
 
Asynchronní programování
Asynchronní programováníAsynchronní programování
Asynchronní programování
 
Understanding reactive programming with microsoft reactive extensions
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensions
 
Gwt RPC
Gwt RPCGwt RPC
Gwt RPC
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joined
 
How to send gzipped requests with boto3
How to send gzipped requests with boto3How to send gzipped requests with boto3
How to send gzipped requests with boto3
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
 
Deep Dumpster Diving
Deep Dumpster DivingDeep Dumpster Diving
Deep Dumpster Diving
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 

Similar a Trisha gee concurrentprogrammingusingthedisruptor

C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
Jussi Pohjolainen
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
roxlu
 

Similar a Trisha gee concurrentprogrammingusingthedisruptor (20)

A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
A Series of Fortunate Events - Drupalcon Europe, Amsterdam 2014
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196
 
A Series of Fortunate Events - Symfony Camp Sweden 2014
A Series of Fortunate Events - Symfony Camp Sweden 2014A Series of Fortunate Events - Symfony Camp Sweden 2014
A Series of Fortunate Events - Symfony Camp Sweden 2014
 
Speed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSocketsSpeed up your Web applications with HTML5 WebSockets
Speed up your Web applications with HTML5 WebSockets
 
The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189
 
#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
 
Decoupling with Design Patterns and Symfony2 DIC
Decoupling with Design Patterns and Symfony2 DICDecoupling with Design Patterns and Symfony2 DIC
Decoupling with Design Patterns and Symfony2 DIC
 
Android TDD & CI
Android TDD & CIAndroid TDD & CI
Android TDD & CI
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
An intro to cqrs
An intro to cqrsAn intro to cqrs
An intro to cqrs
 
Single server queue (Simulation Project)
Single server queue (Simulation Project)Single server queue (Simulation Project)
Single server queue (Simulation Project)
 
Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Trisha gee concurrentprogrammingusingthedisruptor

  • 1. Concurrent Programming Using The Disruptor Trisha Gee, Senior Developer at LMAX
  • 2. The Disruptor? • Open Source • Message Passing
  • 5. First, create your Event public 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. Then create your EventHandler public 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. Then wire it all together final 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());
  • 9. Example 2: One Publisher, Three Processors
  • 10. ...and in the Disruptor?
  • 11. A more complicated Event public 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. ...a couple of EventHandlers public 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. ...a final EventHandler public 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. 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. What’s all that gubbins on the RingBuffer? RingBuffer<WriteTrackingEvent> ringBuffer = new Buffer<WriteTrackingEvent>(WriteTrackingEvent.EVENT_FA new SingleThreadedClaimStrategy(RING_B new YieldingWaitStrategy());
  • 16. Is that it? • Multiple publishers • Different EventHandlers • The Wizard
  • 17. More Information • Blog • Wiki • Presentations
  • 18. Q&A • We are hiring! • careers@lmax.com