当前位置: 首页 > news >正文

Disruptor—核心源码实现分析(三)

4.Disruptor的高性能原因

一.使用了环形结构 + 数组 + 内存预加载

二.使用了单线程写的方式并配合内存屏障

三.消除伪共享(填充缓存行)

四.序号栅栏和序号配合使用来消除锁

五.提供了多种不同性能的等待策略

5.Disruptor高性能之数据结构(内存预加载机制)

(1)RingBuffer使用环形数组来存储元素

环形数组可以避免数组扩容和缩容带来的性能损耗。

(2)RingBuffer采用了内存预加载机制

初始化RingBuffer时,会将entries数组里的每一个元素都先new出来。比如RingBuffer的大小设置为8,那么初始化RingBuffer时,就会先将entries数组的8个元素分别指向新new出来的空的Event对象。往RingBuffer填充元素时,只是将对应的Event对象进行赋值。所以RingBuffer中的Event对象是一直存活着的,也就是说它能最小程度减少系统GC频率,从而提升性能。

public class Main {public static void main(String[] args) {//参数准备OrderEventFactory orderEventFactory = new OrderEventFactory();int ringBufferSize = 4;ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());//参数一:eventFactory,消息(Event)工厂对象//参数二:ringBufferSize,容器的长度//参数三:executor,线程池(建议使用自定义线程池),RejectedExecutionHandler//参数四:ProducerType,单生产者还是多生产者//参数五:waitStrategy,等待策略//1.实例化Disruptor对象Disruptor<OrderEvent> disruptor = new Disruptor<OrderEvent>(orderEventFactory,ringBufferSize,executor,ProducerType.SINGLE,new BlockingWaitStrategy());//2.添加Event处理器,用于处理事件//也就是构建Disruptor与消费者的一个关联关系disruptor.handleEventsWith(new OrderEventHandler());//3.启动disruptordisruptor.start();//4.获取实际存储数据的容器: RingBufferRingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();OrderEventProducer producer = new OrderEventProducer(ringBuffer);ByteBuffer bb = ByteBuffer.allocate(8);for (long i = 0; i < 5; i++) {bb.putLong(0, i);//向容器中投递数据producer.sendData(bb);}disruptor.shutdown();executor.shutdown();}
}public class Disruptor<T> {private final RingBuffer<T> ringBuffer;private final Executor executor;...//Create a new Disruptor.//@param eventFactory   the factory to create events in the ring buffer.//@param ringBufferSize the size of the ring buffer, must be power of 2.//@param executor       an Executor to execute event processors.//@param producerType   the claim strategy to use for the ring buffer.//@param waitStrategy   the wait strategy to use for the ring buffer.public Disruptor(final EventFactory<T> eventFactory, final int ringBufferSize, final Executor executor, final ProducerType producerType, final WaitStrategy waitStrategy) {this(RingBuffer.create(producerType, eventFactory, ringBufferSize, waitStrategy), executor);}//Private constructor helperprivate Disruptor(final RingBuffer<T> ringBuffer, final Executor executor) {this.ringBuffer = ringBuffer;this.executor = executor;}...
}//Ring based store of reusable entries containing the data representing an event being exchanged between event producer and EventProcessors.
//@param <E> implementation storing the data for sharing during exchange or parallel coordination of an event.
public final class RingBuffer<E> extends RingBufferFields<E> implements Cursored, EventSequencer<E>, EventSink<E> {//值为-1public static final long INITIAL_CURSOR_VALUE = Sequence.INITIAL_VALUE;protected long p1, p2, p3, p4, p5, p6, p7;...//Create a new Ring Buffer with the specified producer type (SINGLE or MULTI)public static <E> RingBuffer<E> create(ProducerType producerType, EventFactory<E> factory, int bufferSize, WaitStrategy waitStrategy) {switch (producerType) {case SINGLE:return createSingleProducer(factory, bufferSize, waitStrategy);case MULTI:return createMultiProducer(factory, bufferSize, waitStrategy);default:throw new IllegalStateException(producerType.toString());}}//Create a new single producer RingBuffer with the specified wait strategy.public static <E> RingBuffer<E> createSingleProducer(EventFactory<E> factory, int bufferSize, WaitStrategy waitStrategy) {SingleProducerSequencer sequencer = new SingleProducerSequencer(bufferSize, waitStrategy);return new RingBuffer<E>(factory, sequencer);}//Construct a RingBuffer with the full option set.//@param eventFactory to newInstance entries for filling the RingBuffer//@param sequencer sequencer to handle the ordering of events moving through the RingBuffer.RingBuffer(EventFactory<E> eventFactory, Sequencer sequencer) {super(eventFactory, sequencer);}...
}abstract class RingBufferFields<E> extends RingBufferPad {private final long indexMask;//环形数组存储事件消息private final Object[] entries;protected final int bufferSize;//RingBuffer的sequencer属性代表了当前线程对应的生产者protected final Sequencer sequencer;...RingBufferFields(EventFactory<E> eventFactory, Sequencer sequencer) {this.sequencer = sequencer;this.bufferSize = sequencer.getBufferSize();if (bufferSize < 1) {throw new IllegalArgumentException("bufferSize must not be less than 1");}if (Integer.bitCount(bufferSize) != 1) {throw new IllegalArgumentException("bufferSize must be a power of 2");}this.indexMask = bufferSize - 1;//初始化数组this.entries = new Object[sequencer.getBufferSize() + 2 * BUFFER_PAD];//内存预加载fill(eventFactory);}private void fill(EventFactory<E> eventFactory) {for (int i = 0; i < bufferSize; i++) {//设置一个空的数据对象entries[BUFFER_PAD + i] = eventFactory.newInstance();}}...
}abstract class RingBufferPad {protected long p1, p2, p3, p4, p5, p6, p7;
}

6.Disruptor高性能之内核(使用单线程写)

Disruptor的RingBuffer之所以可以做到完全无锁是因为单线程写。离开单线程写,没有任何技术可以做到完全无锁。Redis和Netty等高性能技术框架也是利用单线程写来实现的。

具体就是:单生产者时,固然只有一个生产者线程在写。多生产者时,每个生产者线程都只会写各自获取到的Sequence序号对应的环形数组的元素,从而使得多个生产者线程相互之间不会产生写冲突。

7.Disruptor高性能之系统内存优化(内存屏障)

要正确实现无锁,还需要另外一个关键技术——内存屏障。对应到Java语言,就是valotile变量与Happens Before语义。

内存屏障:Linux的smp_wmb()/smp_rmb()。

8.Disruptor高性能之系统缓存优化(消除伪共享)

CPU缓存是以缓存行(Cache Line)为单位进行存储的。缓存行是2的整数幂个连续字节,一般为32-256个字节,最常见的缓存行大小是64个字节。

当多线程修改互相独立的变量时,如果这些变量共享同一个缓存行,就会对这个缓存行形成竞争,从而无意中影响彼此性能,这就是伪共享。

消除伪共享:利用了空间换时间的思想。

由于代表着一个序号的Sequence其核心字段value是一个long型变量(占8个字节),所以有可能会出现多个Sequence对象的value变量共享同一个缓存行。因此,需要对Sequence对象的value变量消除伪共享。具体做法就是:对Sequence对象的value变量前后增加7个long型变量。

注意:伪共享与Sequence的静态变量无关,因为静态变量本身就是多个线程共享的,而不是多个线程隔离独立的。

class LhsPadding {protected long p1, p2, p3, p4, p5, p6, p7;
}class Value extends LhsPadding {protected volatile long value;
}class RhsPadding extends Value {protected long p9, p10, p11, p12, p13, p14, p15;
}public class Sequence extends RhsPadding {static final long INITIAL_VALUE = -1L;private static final Unsafe UNSAFE;private static final long VALUE_OFFSET;static {UNSAFE = Util.getUnsafe();try {VALUE_OFFSET = UNSAFE.objectFieldOffset(Value.class.getDeclaredField("value"));} catch (final Exception e) {throw new RuntimeException(e);}}//Create a sequence initialised to -1.public Sequence() {this(INITIAL_VALUE);}//Create a sequence with a specified initial value.public Sequence(final long initialValue) {UNSAFE.putOrderedLong(this, VALUE_OFFSET, initialValue);}//Perform a volatile read of this sequence's value.public long get() {return value;}//Perform an ordered write of this sequence.  //The intent is a Store/Store barrier between this write and any previous store.public void set(final long value) {UNSAFE.putOrderedLong(this, VALUE_OFFSET, value);}...
}

9.Disruptor高性能之序号获取优化(自旋 + CAS)

生产者投递Event时会使用"long sequence = ringBuffer.next()"获取序号,而序号栅栏SequenceBarrier和会序号Sequence搭配起来一起使用,用来协调和管理消费者和生产者的工作节奏,避免锁的使用。

各个消费者和生产者都持有自己的序号,这些序号需满足如下条件以避免生产者速度过快,将还没来得及消费的消息覆盖。

一.消费者序号数值必须小于生产者序号数值
二.消费者序号数值必须小于其前置消费者的序号数值
三.生产者序号数值不能大于消费者中最小的序号数值

高性能的序号获取优化:为避免生产者每次执行next()获取序号时,都要查询消费者的最小序号,Disruptor采取了自旋 + LockSupport挂起线程 + 缓存最小序号 + CAS来优化。既避免了锁,也尽量在不耗费CPU的情况下提升了性能。

单生产者的情况下,只有一个线程添加元素,此时没必要使用锁。多生产者的情况下,会有多个线程并发获取Sequence序号添加元素,此时会通过自旋 + CAS避免锁。

public class OrderEventProducer {private RingBuffer<OrderEvent> ringBuffer;public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {this.ringBuffer = ringBuffer;}public void sendData(ByteBuffer data) {//1.在生产者发送消息时, 首先需要从ringBuffer里获取一个可用的序号long sequence = ringBuffer.next();try {//2.根据这个序号, 找到具体的"OrderEvent"元素//注意:此时获取的OrderEvent对象是一个没有被赋值的"空对象"OrderEvent event = ringBuffer.get(sequence);//3.进行实际的赋值处理event.setValue(data.getLong(0));} finally {//4.提交发布操作ringBuffer.publish(sequence);}}
}//Ring based store of reusable entries containing the data representing an event being exchanged between event producer and EventProcessors.
//@param <E> implementation storing the data for sharing during exchange or parallel coordination of an event.
public final class RingBuffer<E> extends RingBufferFields<E> implements Cursored, EventSequencer<E>, EventSink<E> {//值为-1public static final long INITIAL_CURSOR_VALUE = Sequence.INITIAL_VALUE;protected long p1, p2, p3, p4, p5, p6, p7;...//Increment and return the next sequence for the ring buffer.//Calls of this method should ensure that they always publish the sequence afterward.//E.g.//  long sequence = ringBuffer.next();//  try {//      Event e = ringBuffer.get(sequence);//      ...//  } finally {//      ringBuffer.publish(sequence);//  }//@return The next sequence to publish to.@Overridepublic long next() {return sequencer.next();}//Publish the specified sequence.//This action marks this particular message as being available to be read.//@param sequence the sequence to publish.@Overridepublic void publish(long sequence) {sequencer.publish(sequence);}//Get the event for a given sequence in the RingBuffer.//This call has 2 uses.  //Firstly use this call when publishing to a ring buffer.//After calling RingBuffer#next() use this call to get hold of the preallocated event to fill with data before calling RingBuffer#publish(long).//Secondly use this call when consuming data from the ring buffer.  //After calling SequenceBarrier#waitFor(long) call this method with any value greater than that //your current consumer sequence and less than or equal to the value returned from the SequenceBarrier#waitFor(long) method.//@param sequence for the event//@return the event for the given sequence@Overridepublic E get(long sequence) {//调用父类RingBufferFields的elementAt()方法return elementAt(sequence);}...
}abstract class RingBufferPad {protected long p1, p2, p3, p4, p5, p6, p7;
}abstract class RingBufferFields<E> extends RingBufferPad {...private static final Unsafe UNSAFE = Util.getUnsafe();private final long indexMask;//环形数组存储事件消息private final Object[] entries;protected final int bufferSize;//RingBuffer的sequencer属性代表了当前线程对应的生产者protected final Sequencer sequencer;RingBufferFields(EventFactory<E> eventFactory, Sequencer sequencer) {this.sequencer = sequencer;this.bufferSize = sequencer.getBufferSize();if (bufferSize < 1) {throw new IllegalArgumentException("bufferSize must not be less than 1");}if (Integer.bitCount(bufferSize) != 1) {throw new IllegalArgumentException("bufferSize must be a power of 2");}this.indexMask = bufferSize - 1;//初始化数组this.entries = new Object[sequencer.getBufferSize() + 2 * BUFFER_PAD];//内存预加载fill(eventFactory);}private void fill(EventFactory<E> eventFactory) {for (int i = 0; i < bufferSize; i++) {entries[BUFFER_PAD + i] = eventFactory.newInstance();}}protected final E elementAt(long sequence) {return (E) UNSAFE.getObject(entries, REF_ARRAY_BASE + ((sequence & indexMask) << REF_ELEMENT_SHIFT));}...
}

public abstract class AbstractSequencer implements Sequencer {private static final AtomicReferenceFieldUpdater<AbstractSequencer, Sequence[]> SEQUENCE_UPDATER =AtomicReferenceFieldUpdater.newUpdater(AbstractSequencer.class, Sequence[].class, "gatingSequences");//环形数组的大小protected final int bufferSize;//等待策略protected final WaitStrategy waitStrategy;//当前生产者的进度protected final Sequence cursor = new Sequence(Sequencer.INITIAL_CURSOR_VALUE);//每一个Sequence都对应着一个消费者(一个EventHandler或者一个WorkHandler)//这些Sequence会通过SEQUENCE_UPDATER在执行Disruptor的handleEventsWith()等方法时,//由RingBuffer的addGatingSequences()方法进行添加protected volatile Sequence[] gatingSequences = new Sequence[0];...//Create with the specified buffer size and wait strategy.//@param bufferSize The total number of entries, must be a positive power of 2.//@param waitStrategypublic AbstractSequencer(int bufferSize, WaitStrategy waitStrategy) {if (bufferSize < 1) {throw new IllegalArgumentException("bufferSize must not be less than 1");}if (Integer.bitCount(bufferSize) != 1) {throw new IllegalArgumentException("bufferSize must be a power of 2");}this.bufferSize = bufferSize;this.waitStrategy = waitStrategy;}...
}abstract class SingleProducerSequencerPad extends AbstractSequencer {protected long p1, p2, p3, p4, p5, p6, p7;public SingleProducerSequencerPad(int bufferSize, WaitStrategy waitStrategy) {super(bufferSize, waitStrategy);}
}abstract class SingleProducerSequencerFields extends SingleProducerSequencerPad {public SingleProducerSequencerFields(int bufferSize, WaitStrategy waitStrategy) {super(bufferSize, waitStrategy);}//表示生产者的当前序号,值为-1protected long nextValue = Sequence.INITIAL_VALUE;//表示消费者的最小序号,值为-1protected long cachedValue = Sequence.INITIAL_VALUE;
}public final class SingleProducerSequencer extends SingleProducerSequencerFields {protected long p1, p2, p3, p4, p5, p6, p7;//Construct a Sequencer with the selected wait strategy and buffer size.//@param bufferSize   the size of the buffer that this will sequence over.//@param waitStrategy for those waiting on sequences.public SingleProducerSequencer(int bufferSize, WaitStrategy waitStrategy) {super(bufferSize, waitStrategy);}...@Overridepublic long next() {return next(1);}@Overridepublic long next(int n) {//Sequence的初始化值为-1if (n < 1) {throw new IllegalArgumentException("n must be > 0");}//nextValue指的是当前Sequence//this.nextValue为SingleProducerSequencerFields的变量//第一次调用next()方法时,nextValue = -1//第二次调用next()方法时,nextValue = 0//第三次调用next()方法时,nextValue = 1//第四次调用next()方法时,nextValue = 2//第五次调用next()方法时,nextValue = 3long nextValue = this.nextValue;//第一次调用next()方法时,nextSequence = -1 + 1 = 0//第二次调用next()方法时,nextSequence = 0 + 1 = 1//第三次调用next()方法时,nextSequence = 1 + 1 = 2//第四次调用next()方法时,nextSequence = 2 + 1 = 3//第五次调用next()方法时,nextSequence = 3 + 1 = 4long nextSequence = nextValue + n;//wrapPoint会用来判断生产者序号是否绕过RingBuffer的环//如果wrapPoint是负数,则表示还没绕过RingBuffer的环//如果wrapPoint是非负数,则表示已经绕过RingBuffer的环//假设bufferSize = 3,那么://第一次调用next()方法时,wrapPoint = 0 - 3 = -3,还没绕过RingBuffer的环//第二次调用next()方法时,wrapPoint = 1 - 3 = -2,还没绕过RingBuffer的环//第三次调用next()方法时,wrapPoint = 2 - 3 = -1,还没绕过RingBuffer的环//第四次调用next()方法时,wrapPoint = 3 - 3 = 0,已经绕过RingBuffer的环//第五次调用next()方法时,wrapPoint = 4 - 3 = 1,已经绕过RingBuffer的环long wrapPoint = nextSequence - bufferSize;//cachedGatingSequence是用来将消费者的最小消费序号缓存起来//这样就不用每次执行next()方法都要去获取消费者的最小消费序号//第一次调用next()方法时,cachedGatingSequence = -1//第二次调用next()方法时,cachedGatingSequence = -1//第三次调用next()方法时,cachedGatingSequence = -1//第四次调用next()方法时,cachedGatingSequence = -1//第五次调用next()方法时,cachedGatingSequence = 1long cachedGatingSequence = this.cachedValue;//第四次调用next()方法时,wrapPoint大于cachedGatingSequence,执行条件中的逻辑if (wrapPoint > cachedGatingSequence || cachedGatingSequence > nextValue) {//最小的消费者序号long minSequence;//自旋操作://Util.getMinimumSequence(gatingSequences, nextValue)的含义就是找到消费者中最小的序号值//如果wrapPoint > 消费者中最小的序号,那么生产者线程就需要进行阻塞//即如果生产者序号 > 消费者中最小的序号,那么就挂起并进行自旋操作//第四次调用next()方法时,nextValue = 2,wrapPoint = 0,gatingSequences里面的消费者序号如果还没消费(即-1),则要挂起while (wrapPoint > (minSequence = Util.getMinimumSequence(gatingSequences, nextValue))) {//TODO: Use waitStrategy to spin?  LockSupport.parkNanos(1L); }//cachedValue接收了消费者的最小序号//第四次调用next()方法时,假设消费者的最小序号minSequence为1,则cachedValue = 1this.cachedValue = minSequence;}//第一次调用完next()方法时,nextValue会变为0//第二次调用完next()方法时,nextValue会变为1//第三次调用完next()方法时,nextValue会变为2//第四次调用完next()方法时,nextValue会变为3//第五次调用完next()方法时,nextValue会变为4this.nextValue = nextSequence;//第一次调用next()方法时,返回的nextSequence = 0//第二次调用next()方法时,返回的nextSequence = 1//第三次调用next()方法时,返回的nextSequence = 2//第四次调用next()方法时,返回的nextSequence = 3//第五次调用next()方法时,返回的nextSequence = 4return nextSequence;}@Overridepublic void publish(long sequence) {//设置当前生产者的sequencecursor.set(sequence);//通过等待策略通知阻塞的消费者waitStrategy.signalAllWhenBlocking();}...
}public final class Util {...//Get the minimum sequence from an array of {@link com.lmax.disruptor.Sequence}s.//@param sequences to compare.//@param minimum   an initial default minimum. If the array is empty this value will be returned.//@return the smaller of minimum sequence value found in sequences and minimum; minimum if sequences is emptypublic static long getMinimumSequence(final Sequence[] sequences, long minimum) {for (int i = 0, n = sequences.length; i < n; i++) {long value = sequences[i].get();minimum = Math.min(minimum, value);}return minimum;}...
}public final class MultiProducerSequencer extends AbstractSequencer {...@Overridepublic long next() {return next(1);}@Overridepublic long next(int n) {if (n < 1) {throw new IllegalArgumentException("n must be > 0");}long current;long next;do {//获取当前生产者的序号current = cursor.get();next = current + n;//wrapPoint会用来判断生产者序号是否绕过RingBuffer的环//如果wrapPoint是负数,则表示还没绕过RingBuffer的环//如果wrapPoint是非负数,则表示已经绕过RingBuffer的环long wrapPoint = next - bufferSize;//cachedGatingSequence是用来将消费者的最小消费序号缓存起来//这样就不用每次执行next()方法都要去获取消费者的最小消费序号long cachedGatingSequence = gatingSequenceCache.get();if (wrapPoint > cachedGatingSequence || cachedGatingSequence > current) {//gatingSequence表示的是消费者的最小序号long gatingSequence = Util.getMinimumSequence(gatingSequences, current);if (wrapPoint > gatingSequence) {//TODO, should we spin based on the wait strategy?LockSupport.parkNanos(1); continue;}gatingSequenceCache.set(gatingSequence);} else if (cursor.compareAndSet(current, next)) {break;}} while (true);return next;}...
}

文章转载自:东阳马生架构

原文链接:Disruptor—3.核心源码实现分析 - 东阳马生架构 - 博客园

体验地址:JNPF快速开发平台

相关文章:

  • 全球地表水与地下水盐度数据(1980-2019)含45,103个地表水站点与208,550个地下水站点
  • RAG综述:大模型检索增强生成全解析
  • C++数据结构 : 二叉搜索树
  • 【Elasticsearch】使用脚本删除索引中的某个字段
  • SLOT:测试时样本专属语言模型优化,让大模型推理更精准!
  • 中车靶场,网络安全暑期实训营
  • FOFA网络空间测绘初学者指南:像探险家一样探索数字世界
  • 什么是数据驱动?以及我们应如何理解数据驱动?
  • ICMP与TCP端口:网络层与传输层解析
  • Flutter 实现6个验收码输入框
  • 实现单例模式的6种方法(Python)
  • 防爆手机VS普通手机,区别在哪里?
  • 获取oracle的HQL日志,采取参数日志,拼装SQL语句
  • Oracle初识
  • Java大师成长计划之第32天:使用Kubernetes进行Java应用编排与管理
  • C++学习-入门到精通【9】面向对象编程:继承
  • 低空经济数据湖架构设计方案
  • 贝壳后端golang面经
  • 浅浅学:XCP协议原理及应用
  • ctf.show pwn入门 堆利用-前置基础 pwn142
  • 南京网站开发哪家好/售卖链接
  • 网站建设相关知识/脱发严重是什么原因引起的
  • 煤矿网站建设/搜索引擎seo关键词优化方法
  • 湛江制作网站企业/本地广告推广平台哪个好
  • 网站做影集安全吗/班级优化大师官方网站
  • 社会组织网站建设/seo排名工具提升流量