SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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)
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 

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