Disruptor

来源:互联网 发布:西安知行软件公司 编辑:程序博客网 时间:2024/05/18 03:03

在最近的项目中看到同事使用到了Disruptor,以前在ifeve上看到过关于Disruptor的文章,但是没有深入研究,现在项目中用到了,就借这个机会对这个并发编程框架进行深入学习。项目中使用到的是disruptor-2.10.4,所以下面分析到的Disruptor的代码是这个版本的。

并发编程网介绍Disruptor的文章是disruptor1.0版本,所以有一些术语在2.0版本上已经没有了或者被替代了。

Disruptor术语

github上Disruptor的wiki对Disruptor中的术语进行了解释,在看Disruptor的过程中,对于几个其他的类,觉得有必要与这些术语放到一起,就加进来了。

  • RingBuffer 经常被看作Disruptor最主要的组件,然而从3.0开始RingBuffer仅仅负责存储和更新在Disruptor中流通的数据。对一些特殊的使用场景能够被用户(使用其他数据结构)完全替代。
  • Sequence Disruptor使用Sequence来表示一个特殊组件处理的序号。和Disruptor一样,每个消费者(EventProcessor)都维持着一个Sequence。大部分的并发代码依赖这些Sequence值的运转,因此Sequence支持多种当前AtomicLong类的特性。事实上,这两者之间唯一的区别是Sequence包含额外的功能来阻止Sequence和其他值之间的共享。
  • Sequencer 这是Disruptor真正的核心。实现了这个接口的两种生产者(单生产者和多生产者)均实现了所有的并发算法,为了在生产者和消费者之间进行准确快速的数据传递。
  • SequenceBarrier 由Sequencer生成,并且包含了已经发布的Sequence的引用,这些的Sequence源于Sequencer和一些独立的消费者的Sequence。它包含了决定是否有供消费者来消费的Event的逻辑。
  • WaitStrategy:它决定了一个消费者将如何等待生产者将Event置入Disruptor。
  • Event 从生产者到消费者过程中所处理的数据单元。Disruptor中没有代码表示Event,因为它完全是由用户定义的。
  • EventProcessor 主要的事件循环,用于处理Disruptor中的Event,并且拥有消费者的Sequence。它有一个实现类是BatchEventProcessor,包含了event loop有效的实现,并且将回调到一个EventHandler接口的实现对象。
  • EventHandler 由用户实现并且代表了Disruptor中的一个消费者的接口。
  • Producer 由用户实现,它调用RingBuffer来插入事件(Event),在Disruptor中没有相应的实现代码,由用户实现。
  • WorkProcessor 确保每个sequence只被一个processor消费,在同一个WorkPool中的处理多个WorkProcessor不会消费同样的sequence。
  • WorkerPool 一个WorkProcessor池,其中WorkProcessor将消费Sequence,所以任务可以在实现WorkHandler接口的worker吃间移交
  • LifecycleAware 当BatchEventProcessor启动和停止时,于实现这个接口用于接收通知。

Disruptor印象

初看Disruptor,给人的印象就是RingBuffer是其核心,生产者向RingBuffer中写入元素,消费者从RingBuffer中消费元素,如下图: 

这就是Disruptor最简单的模型。其中的RingBuffer被组织成要给环形队列,但它与我们在常常使用的队列又不一样,这个队列大小固定,且每个元素槽都以一个整数进行编号,RingBuffer中只有一个游标维护着一个指向下一个可用位置的序号,生产者每次向RingBuffer中写入一个元素时都需要向RingBuffer申请一个可写入的序列号,如果此时RingBuffer中有可用节点,RingBuffer就向生产者返回这个可用节点的序号,如果没有,那么就等待。同样消费者消费的元素序号也必须是生产者已经写入了的元素序号。

那么Disruptor是如何实现这些逻辑的呢?先来看一个Disruptor的使用示例

Disruptor使用示例

不适用Disruptor的dsl,直接使用Disruptor中的类来完成。

[java] view plaincopy
  1. //RingBuffer中存储的单元  
  2. public class IntEvent {  
  3.     private int value = -1;  
  4.   
  5.     public int getValue() {  
  6.         return value;  
  7.     }  
  8.   
  9.     public void setValue(int value) {  
  10.         this.value = value;  
  11.     }  
  12.   
  13.     public String toString() {  
  14.         return String.valueOf(value);  
  15.     }  
  16.   
  17.     public static EventFactory<IntEvent> INT_ENEVT_FACTORY = new EventFactory<IntEvent>() {  
  18.         public IntEvent newInstance() {  
  19.             return new IntEvent();  
  20.         }  
  21.     };  
  22. }  
  23. //生产者  
  24. public class IntEventProducer implements WorkHandler<IntEvent> {  
  25.   
  26.     private int seq = 0;  
  27.     public void onEvent(IntEvent event) throws Exception {  
  28.         System.out.println("produced " + seq);  
  29.         event.setValue(++seq);  
  30.     }  
  31.   
  32. }  
  33. //消费者  
  34. public class IntEventProcessor implements WorkHandler<IntEvent> {  
  35.   
  36.     public void onEvent(IntEvent event) throws Exception {  
  37.         System.out.println(event.getValue());  
  38.         event.setValue(1);  
  39.     }  
  40.   
  41. }  
  42.   
  43. public class DisruptorTest {  
  44.   
  45.     public static void main(String[] args) throws InterruptedException {  
  46.         //创建一个RingBuffer对象  
  47.         RingBuffer<IntEvent> ringBuffer = new RingBuffer<IntEvent>(IntEvent.INT_ENEVT_FACTORY,  
  48.             new SingleThreadedClaimStrategy(16),  
  49.             new SleepingWaitStrategy());  
  50.   
  51.         SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  52.         IntEventProducer[] producers = new IntEventProducer[1];  
  53.         for (int i = 0; i < producers.length; i++) {  
  54.             producers[i] = new IntEventProducer();  
  55.         }  
  56.         WorkerPool<IntEvent> crawler = new WorkerPool<IntEvent>(ringBuffer,  
  57.             sequenceBarrier,  
  58.             new IntEventExceptionHandler(),  
  59.             producers);  
  60.         SequenceBarrier sb = ringBuffer.newBarrier(crawler.getWorkerSequences());  
  61.         IntEventProcessor[] processors = new IntEventProcessor[1];  
  62.         for (int i = 0; i < processors.length; i++) {  
  63.             processors[i] = new IntEventProcessor();  
  64.         }  
  65.   
  66.         WorkerPool<IntEvent> applier = new WorkerPool<IntEvent>(ringBuffer,sb,  
  67.             new IntEventExceptionHandler(),  
  68.             processors);  
  69.         List<Sequence> gatingSequences = new ArrayList<Sequence>();  
  70.         for(Sequence s : crawler.getWorkerSequences()) {  
  71.             gatingSequences.add(s);  
  72.         }  
  73.         for(Sequence s : applier.getWorkerSequences()) {  
  74.             gatingSequences.add(s);  
  75.         }  
  76. ringBuffer.setGatingSequences(gatingSequences.toArray(new Sequence[gatingSequences.size()]));  
  77.         ThreadPoolExecutor executor = new ThreadPoolExecutor(7,7,10,TimeUnit.MINUTES,new LinkedBlockingQueue<Runnable>(5));  
  78.         crawler.start(executor);  
  79.         applier.start(executor);  
  80.   
  81.         while (true) {  
  82.             Thread.sleep(1000);  
  83.             long lastSeq = ringBuffer.next();  
  84.             ringBuffer.publish(lastSeq);  
  85.         }  
  86.     }  
  87. }  
  88.   
  89. class IntEventExceptionHandler implements ExceptionHandler {  
  90.     public void handleEventException(Throwable ex, long sequence, Object event) {}  
  91.     public void handleOnStartException(Throwable ex) {}  
  92.     public void handleOnShutdownException(Throwable ex) {}  
  93. }  


在上面的代码中IntEvent类就是术语中的Event,IntEventProducer对应Producer,IntEventProcessor对应着EventProcessor,也就是消费者,但是IntEventProcessor类并不是实现的IntEventProcessor接口,这个下面会分析到。

下面从main方法开始分析。

首先是RingBuffer的创建,RingBuffer<IntEvent> ringBuffer = new RingBuffer<IntEvent>(IntEvent.INT_ENEVT_FACTORY,new SingleThreadedClaimStrategy(16), new SleepingWaitStrategy());RingBuffer的构造方法的第一个参数是实现了EventFactory接口的类,主要作用是创建IntEvent对象,在创建RingBuffer对象时,第二个参数是ClaimStrategy,生产者通过ClaimStrategy 来申请下一个可用节点,第三个参数是WaitStrategy的实现类,它定义了消费者的等待策略。

下面一段代码是初始化生产者的过程

[java] view plaincopy
  1. SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  2. IntEventProducer[] producers = new IntEventProducer[1];  
  3. for (int i = 0; i < producers.length; i++) {  
  4.     producers[i] = new IntEventProducer();  
  5. }  
  6. WorkerPool<IntEvent> crawler = new WorkerPool<IntEvent>(ringBuffer,  
  7. sequenceBarrier,new IntEventExceptionHandler(), producers);  

ringBuffer.newBarrier()返回的是一个SequenceBarrier对象,Barrier顾名思义就是障碍的意思,这个障碍阻止了生产者越过其所能到达的ringbuffer中的位置。RingBuffer在执行过程中会维持一个游标cursor,所以生产者从RingBuffer中获取到的的游标必须小于等于这个cursor。

然后就是消费者的初始化:

[java] view plaincopy
  1. SequenceBarrier sb = ringBuffer.newBarrier(crawler.getWorkerSequences());  
  2. IntEventProcessor[] processors = new IntEventProcessor[1];  
  3. for (int i = 0; i < processors.length; i++) {  
  4.     processors[i] = new IntEventProcessor();  
  5. }  
  6.   
  7. WorkerPool<IntEvent> applier = new WorkerPool<IntEvent>(ringBuffer,sb,new IntEventExceptionHandler(),processors);  

消费者初始化也需要设置一个SequenceBarrier对象,这个SequenceBarrier对象指明了消费者可以消费的元素序号,如果消费者的游标大于这个序号,那么消费者必须以WaitStrategy定义的策略等待。

生产者和消费者创建完成后下一步是设置RingBuffer的一个变量gatingSequencesgatingSequences的作用是防止生产者覆盖还未被消费者消费的元素,假设一个RingBuffer的大小为8,消费者消费速度较慢,那么RingBuffer可能是满的,当生产者向RingBuffer申请下一个可用序号时,还未被消费者消费的序号就不能被覆盖,所以RingBuffer就不能给生产者返回可用序号,此时消费者线程就进入等待,在这种情况下RingBuffer会检查当前申请的序号是否大于gatingSequences中的最小序号,如果当前申请的序号大于最小序号,那么生产者就等待。代码如下:

[java] view plaincopy
  1. List<Sequence> gatingSequences = new ArrayList<Sequence>();  
  2. for(Sequence s : crawler.getWorkerSequences()) {  
  3.     gatingSequences.add(s);  
  4. }  
  5. for(Sequence s : applier.getWorkerSequences()) {  
  6.     gatingSequences.add(s);  
  7. }     ringBuffer.setGatingSequences(gatingSequences.toArray(new Sequence[gatingSequences.size()]));  


<span style="font-family: Arial, Helvetica, sans-serif;">这里之所以加入了生产者的序号,是因为在多生产者的情况下,一个生产者不能覆盖另一个生产者已经申请的序号。</span>

然后下面一段的代码就是启动Disruptor线程来执行生产者和消费者线程。

[java] view plaincopy
  1. ThreadPoolExecutor executor = new ThreadPoolExecutor(77,10,  
  2. TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(2));  
  3. crawler.start(executor);  
  4.  applier.start(executor);  

最后要做的是生产者不断的申请可用序号,提交准备好的元素序号,即:

[java] view plaincopy
  1. while (true) {  
  2.     Thread.sleep(1000);  
  3.     long lastSeq = ringBuffer.next();  
  4.     ringBuffer.publish(lastSeq);  
  5. }  


<span style="font-family: Arial, Helvetica, sans-serif;">这个过程一般成为两阶段提交,第一个阶段调用</span><code>RingBuffer.next()</code><span style="font-family: Arial, Helvetica, sans-serif;">方法得到下一个可用节点,此时会调用生产者类的OnEvent方法,即</span><code>IntEventProducer.onEvent()</code><span style="font-family: Arial, Helvetica, sans-serif;">,第二个阶段是发布已经生产完成的元素序号</span><code>lastSeq</code><span style="font-family: Arial, Helvetica, sans-serif;">,提醒消费者,可以消费这个序号了。</span>

小结

本文简要介绍了Disruptor的一些术语,解释了一个Disruptor示例程序执行过程的各个要点,从宏观上了解了Disruptor的执行流程,在下一篇文章中接着分析Disruptor的内部执行原理。

Reference

并发框架Disruptor译文 
JAVA并发编程学习笔记之Unsafe类 
从 Java 代码到 Java 堆 
Disruptor的GitHub 
Disruptor使用指南

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0
原创粉丝点击