Welcome to my GitHub

Github.com/zq2599/blog…

Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

Disruptor Notes series of links

  1. Quick start
  2. Disruptor class analysis
  3. Basic operations for ring queues (not using Disruptor class)
  4. Event consumption knowledge summary
  5. Event consumption Actual combat
  6. Common scenarios
  7. Waiting for the strategy
  8. Knowledge supplement (Final)

About independent consumption and co-consumption

  • This is the fourth edition of Disruptor Notes, and you’ll need to relax and familiarize yourself with one important concept: how disruptor events are consumed, both individually and collectively;

  • For example, suppose that in an e-mart scenario, buyers are notified by email and text message every time an order is placed. If ten orders are placed, there are two things to consider:

First: to send ten e-mails and text messages, at this point, the mail system and SMS system is independent, their consumption this ten order of events, that is to say ten events are spending twenty times, so mail system and SMS system independently consumption, has nothing to do with each other, the following figure, an origin represents an event:

The second: suppose mail system processing ability is poor, in order to improve processing capacity, deployed two mail server, so is the two mail server processing ten order event together, together sent a total of ten e-mails, as the chart, a mail server and mail server 2 is common consumption, an order event only in a mail server is consumption:

The core knowledge of independent consumption

  1. The API used is handleEventsWith
  2. The business processing logic goes into the EventHandler implementation class
  3. The internal implementation uses the BatchEventProcessor class. Each consumer corresponds to an instance of BatchEventProcessor. The task is to fetch the event and then call EventHandler’s onEvent method to process it
  4. Each consumer corresponds to a SequenceBarrier instance, which is used to wait for consumable events
  5. Each consumer corresponds to a Sequence instance (a member variable of the BatchEventProcessor) that records the consumption progress
  6. Each BatchEventProcessor instance will be into the collection (consumerRepository consumerInfos)
  7. Disruptor’s start method executes the BatchEventProcessor in a thread pool, meaning that each consumer executes in a separate thread

The core knowledge of common consumption

  1. Use the API is handleEventsWithWorkerPool
  2. The business processing logic goes into the WorkHandler implementation class
  3. The internal implementation is completed by the WorkerPool and WorkProcessor classes. There is only one Instance of WorkerPool, one instance of WorkProcessor for each consumer
  4. There is only one instance of SequenceBarrier waiting for consumable events
  5. Each consumer has its own Sequence instance, and a common Sequence instance (a member variable of the WorkerPool) is used to record consumption progress
  6. WorkerPool instance will be wrapped as WorkerPoolInfo add collection (consumerRepository. ConsumerInfos)
  7. Disruptor’s start method calls the workerPool.start method, which executes each WorkProcessor in a thread pool, meaning that each consumer executes in a separate thread

A concise summary

  • The above core knowledge points are still a little too much, let’s use comparison to simplify, the following is the essence of the essence, really can not save, please focus on:
  1. Each consumer of independent consumption has its own unique instance of SequenceBarrier, while the co-consumer means that everyone shares the same instance of SequenceBarrier
  2. Each consumer of independent consumption has its own unique Sequence instance. For co-consumers, although they also have their own Sequence instance, But the value of this Sequence instance is derived from a common Sequence instance (workSequence, the member variable of WorkerPool)
  3. In the case of joint consumption, the Sequence value of each consumer actually comes from the common Sequence instance, and multiple threads compete with each other to preempt events for consumption:

Speak in a figure

  • Finally, put a self-made picture, I hope a picture is better than a thousand words:

  • At this point, the theoretical analysis is over, the following article will take hot iron, based on the above knowledge points for actual combat, coding to achieve three scenarios:
  1. 100 orders, SMS and email system independent consumption
  2. 100 orders, shared between the two mail servers of the mail system;
  3. 100 orders, SMS system consumption independently, at the same time, the two mail server consumption;

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…