我的项目中有一个环形缓冲区,很多发布者会发布事件(例如500个发布者),我有3个EventProcessors,它们应该按顺序处理事件。所有事件都应该通过这种方式:
{很多发布商} - > {UpStreamProcessor} - > {DownStreamProcessor} - > {的LogProcessor}
问题是我在UpStreamProcessor的发布和启动之间传递事件时失去了很多时间,并且UpStreamProcessor的结束是为了启动DownStreamProcessor。
例如,当我有500个发布者时,它在UpStreamProcessor和DownStreamProcessor中的处理平均持续1ms,而在UpStreamProcessor完成时间到DownStreamProcessor启动时间之间持续400ms。
这是用于构造环形缓冲区和处理器的代码:
SequenceBarrier sequenceBarrier;
receiveBuffer = new RingBuffer<>(
MessageContext.FACTORY,
new MultiThreadedLowContentionClaimStrategy(inputBufferSize),
new YieldingWaitStrategy()
);
upStreamAgentProcessor = new BatchEventProcessor<>(
receiveBuffer,
receiveBuffer.newBarrier(),
new UpStreamAgent()
);
sequenceBarrier = receiveBuffer.newBarrier(
upStreamAgentProcessor.getSequence()
);
downStreamAgentProcessor = new BatchEventProcessor<MessageContext>(
receiveBuffer,
sequenceBarrier,
new DownStreamAgent()
);
sequenceBarrier = receiveBuffer.newBarrier(
downStreamAgentProcessor.getSequence()
);
logMapAgentProcessor = new BatchEventProcessor<MessageContext>(
receiveBuffer,
sequenceBarrier,
LogMap.getInstance()
);
receiveBuffer.setGatingSequences(logMapAgentProcessor.getSequence());
operationalExecutor.submit(upStreamAgentProcessor);
operationalExecutor.submit(downStreamAgentProcessor);
operationalExecutor.submit(logMapAgentProcessor);
答案 0 :(得分:2)
Disruptor设计用于处理耗时0.0001 ms的消息如果1 ms甚至0.1 ms的延迟不打扰你,我会使用普通的ExecutorService。如果你看到延迟或超过0.001毫秒,它不太可能是破坏者,你正在执行的任务太长。
这是关于协调疏忽的一个很好的介绍。 http://www.infoq.com/presentations/latency-pitfalls坏消息是如果你有一个瓶颈会让你的生产者看起来变慢,那么延迟可能比你测量的要差很多。