public class Disruptor<T> {
//消息队列
private final RingBuffer<T> ringBuffer;
//线程池
private final Executor executor;
//消费者集合
private final ConsumerRepository<T> consumerRepository;
private final AtomicBoolean started;
private ExceptionHandler<? super T> exceptionHandler;
abstract class RingBufferFields<E> extends RingBufferPad {
private static final int BUFFER_PAD;
private static final long REF_ARRAY_BASE;
private static final int REF_ELEMENT_SHIFT;
private static final Unsafe UNSAFE = Util.getUnsafe();
private final long indexMask;
private final Object[] entries;
protected final int bufferSize;
//MultiProducerSequencer 管理生产者
protected final Sequencer sequencer;
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(-1L);
//做为生产者,也不能覆盖消费者尚未消费的数据
protected volatile Sequence[] gatingSequences = new Sequence[0];
public final class MultiProducerSequencer extends AbstractSequencer {
private static final Unsafe UNSAFE = Util.getUnsafe();
private static final long BASE;
private static final long SCALE;
private final Sequence gatingSequenceCache = new Sequence(-1L);
private final int[] availableBuffer;
private final int indexMask;
private final int indexShift;
//消费者相关
final class ProcessingSequenceBarrier implements SequenceBarrier {
//指向生产者等待策略
private final WaitStrategy waitStrategy;
//消费者之间的依赖关系
private final Sequence dependentSequence;
private volatile boolean alerted = false;
//指向生产者cursor
private final Sequence cursorSequence;
//指向生产者sequencer
private final Sequencer sequencer;
public final class BatchEventProcessor<T> implements EventProcessor {
private static final int IDLE = 0;
private static final int HALTED = 1;
private static final int RUNNING = 2;
private final AtomicInteger running = new AtomicInteger(0);
private ExceptionHandler<? super T> exceptionHandler = new FatalExceptionHandler();
//实际上是个RingBuffer
private final DataProvider<T> dataProvider;
//ProcessingSequenceBarrier类型,跟生产者挂钩,控制是否可以消费
private final SequenceBarrier sequenceBarrier;
//实际的处理逻辑
private final EventHandler<? super T> eventHandler;
//控制消费进度,指向当前已消费的最新位置
private final Sequence sequence = new Sequence(-1L);
private final TimeoutHandler timeoutHandler;
private final BatchStartAware batchStartAware;
/**
*
*/
private void processEvents() {
T event = null;
//获取下一个可消费的位置
long nextSequence = this.sequence.get() + 1L;
while(true) {
while(true) {
try {
long availableSequence = this.sequenceBarrier.waitFor(nextSequence);
if (this.batchStartAware != null) {
this.batchStartAware.onBatchStart(availableSequence - nextSequence + 1L);
}
while(nextSequence <= availableSequence) {
event = this.dataProvider.get(nextSequence);
this.eventHandler.onEvent(event, nextSequence, nextSequence == availableSequence);
++nextSequence;
}
this.sequence.set(availableSequence);
} catch (TimeoutException var6) {
this.notifyTimeout(this.sequence.get());
} catch (AlertException var7) {
if (this.running.get() != 2) {
return;
}
} catch (Throwable var8) {
this.exceptionHandler.handleEventException(var8, nextSequence, event);
this.sequence.set(nextSequence);
++nextSequence;
}
}
}
}
//BlockingWaitStrategy
//
public long waitFor(long sequence, Sequence cursorSequence, Sequence dependentSequence, SequenceBarrier barrier) throws AlertException, InterruptedException {
//生产者序号指向下一个可供生产的序号
if (cursorSequence.get() < sequence) {
this.lock.lock();
try {
while(cursorSequence.get() < sequence) {
barrier.checkAlert();
this.processorNotifyCondition.await();
}
} finally {
this.lock.unlock();
}
}
long availableSequence;
while((availableSequence = dependentSequence.get()) < sequence) {
barrier.checkAlert();
ThreadHints.onSpinWait();
}
return availableSequence;
}