Hello,
I have noticed CPU spikes when I have dependent event handlers in a Disruptor that use the BlockingWaitStrategy and the first event handler is blocked. Whilst the first event handler is blocked all subsequent handlers will busy spin.
Is this behaviour known and/or desired?
In our code we have modified the BlockingWaitStrategy so that it doesn't busy spin when waiting for dependent sequences. We use a phased spin/yield/sleep approach. Would this approach be appropriate in the Disruptor's BlockingWaitStrategy implementation?
Code to reproduce the behaviour:
@Test
public void should_spin() throws Exception
{
Disruptor<String> disruptor = new Disruptor<String>(new EventFactory<String>() {
@Override
public String newInstance() {
return "Whatever";
}
}, 1024, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new BlockingWaitStrategy());
disruptor.handleEventsWith(new BlockingHandler())
.then(new NoOpHandler());
disruptor.start();
for (int i = 0; i < 20; i++)
{
if (i == 9) // Force a batch
Thread.sleep(10);
publish(disruptor);
}
//Debug.WriteLine("Sleeping main thread");
Thread.sleep(Long.MAX_VALUE);
disruptor.shutdown();
}
private static void publish(Disruptor<String> disruptor)
{
long next = disruptor.getRingBuffer().next();
disruptor.getRingBuffer().publish(next);
}
public class NoOpHandler implements EventHandler<String>
{
@Override
public void onEvent(String event, long sequence, boolean endOfBatch) throws Exception {
System.out.println("Got sequence " + sequence);
}
}
public class BlockingHandler implements EventHandler<String>
{
@Override
public void onEvent(String event, long sequence, boolean endOfBatch) throws Exception {
if (sequence >= 10)
Thread.sleep(Long.MAX_VALUE);
}
}
----------------------------------------------------------------------------------------------------
ABC arbitrage, partenaire officiel du skipper Jean-Pierre Dick // ABC arbitrage, official partner of skipper Jean-Pierre Dick // www.jpdick.com Please consider your environmental responsibility before printing this email
*********************************************************************************
Ce message peut contenir des informations confidentielles. Les idées et opinions presentées dans ce message sont celles de son auteur, et ne représentent pas nécessairement celles du groupe ABC arbitrage.
Au cas où il ne vous serait pas destiné,merci d'en aviser l'expéditeur immédiatement et de le supprimer.
This message may contain confidential information. Any views or opinions presented are solely those of its author and do not necessarily represent those of ABC arbitrage.
If you are not the intended recipient, please notify the sender immediately and delete it.
*********************************************************************************