Refine search
Disruptor<LongEvent> disruptor = new Disruptor<>(factory, bufferSize, executor, ProducerType.SINGLE, new YieldingWaitStrategy()); disruptor.handleEventsWith(new LongEventHandler()); disruptor.start(); RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
public static void main(String[] args) throws TimeoutException, InterruptedException { Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>( LongEvent.FACTORY, 16, DaemonThreadFactory.INSTANCE ); CountDownLatch shutdownLatch = new CountDownLatch(2); disruptor.handleEventsWith(new Handler(shutdownLatch)).then(new Handler(shutdownLatch)); disruptor.start(); long next = disruptor.getRingBuffer().next(); disruptor.getRingBuffer().get(next).set(next); disruptor.getRingBuffer().publish(next); disruptor.shutdown(10, TimeUnit.SECONDS); shutdownLatch.await(); System.out.println(value); } }
@Override protected void doShutdown() throws IOException { // Shutdown the disruptor. Will stop after all entries have been processed. Make sure we // have stopped incoming appends before calling this else it will not shutdown. We are // conservative below waiting a long time and if not elapsed, then halting. if (this.disruptor != null) { long timeoutms = conf.getLong("hbase.wal.disruptor.shutdown.timeout.ms", 60000); try { this.disruptor.shutdown(timeoutms, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { LOG.warn("Timed out bringing down disruptor after " + timeoutms + "ms; forcing halt " + "(It is a problem if this is NOT an ABORT! -- DATALOSS!!!!)"); this.disruptor.halt(); this.disruptor.shutdown(); } } if (LOG.isDebugEnabled()) { LOG.debug("Closing WAL writer in " + FSUtils.getPath(walDir)); } if (this.writer != null) { this.writer.close(); this.writer = null; } }
/** * <p>Waits until all events currently in the disruptor have been processed by all event processors * and then halts the processors.</p> * * <p>This method will not shutdown the executor, nor will it await the final termination of the * processor threads.</p> * * @param timeout the amount of time to wait for all events to be processed. <code>-1</code> will give an infinite timeout * @param timeUnit the unit the timeOut is specified in * @throws TimeoutException if a timeout occurs before shutdown completes. */ public void shutdown(final long timeout, final TimeUnit timeUnit) throws TimeoutException { final long timeOutAt = System.currentTimeMillis() + timeUnit.toMillis(timeout); while (hasBacklog()) { if (timeout >= 0 && System.currentTimeMillis() > timeOutAt) { throw TimeoutException.INSTANCE; } // Busy spin } halt(); }
/** * Shuts down the command bus. It no longer accepts new commands, and finishes processing commands that have * already been published. This method <b>will not</b> shut down any executor that has been provided as part of the * Builder process. */ public void stop() { if (!started) { return; } started = false; long lastChangeDetected = System.currentTimeMillis(); long lastKnownCursor = disruptor.getRingBuffer().getCursor(); while (System.currentTimeMillis() - lastChangeDetected < coolingDownPeriod && !Thread.interrupted()) { if (disruptor.getRingBuffer().getCursor() != lastKnownCursor) { lastChangeDetected = System.currentTimeMillis(); lastKnownCursor = disruptor.getRingBuffer().getCursor(); } } disruptorShutDown = true; disruptor.shutdown(); if (executorService != null) { executorService.shutdown(); } }
public static void main(String[] args) { Disruptor<PipelinerEvent> disruptor = new Disruptor<PipelinerEvent>( PipelinerEvent.FACTORY, 1024, DaemonThreadFactory.INSTANCE); disruptor.handleEventsWith( new ParallelHandler(0, 3), new ParallelHandler(1, 3), new ParallelHandler(2, 3) ).then(new JoiningHandler()); RingBuffer<PipelinerEvent> ringBuffer = disruptor.start(); for (int i = 0; i < 1000; i++) { long next = ringBuffer.next(); try { PipelinerEvent pipelinerEvent = ringBuffer.get(next); pipelinerEvent.input = i; } finally { ringBuffer.publish(next); } } }
@Test public void shouldHandleLotsOfThreads() throws Exception Disruptor<TestEvent> disruptor = new Disruptor<TestEvent>( TestEvent.FACTORY, 1 << 16, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new SleepingWaitStrategy()); RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); disruptor.setDefaultExceptionHandler(new FatalExceptionHandler()); Publisher[] publishers = initialise(new Publisher[publisherCount], ringBuffer, iterations, barrier, latch); disruptor.handleEventsWithWorkerPool(handlers); disruptor.start(); while (ringBuffer.getCursor() < (iterations - 1)) disruptor.shutdown();
public static void main(String[] args) { Disruptor<MyEvent> disruptor = new Disruptor<MyEvent>(factory, 1024, DaemonThreadFactory.INSTANCE); disruptor.handleEventsWith(handler1).then(handler2).then(handler3); disruptor.start(); } }
public static void main(String[] args) throws InterruptedException Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(LongEvent.FACTORY, 1024, DaemonThreadFactory.INSTANCE); disruptor.handleEventsWith(new MyHandler()); disruptor.start(); disruptor.publishEvent(t);
@Test public void shouldSetSequenceForHandlerIfAddedAfterPublish() throws Exception { RingBuffer<TestEvent> rb = disruptor.getRingBuffer(); EventHandler<TestEvent> b1 = new SleepingEventHandler(); EventHandler<TestEvent> b2 = new SleepingEventHandler(); EventHandler<TestEvent> b3 = new SleepingEventHandler(); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); disruptor.handleEventsWith(b1, b2, b3); assertThat(disruptor.getSequenceValueFor(b1), is(5L)); assertThat(disruptor.getSequenceValueFor(b2), is(5L)); assertThat(disruptor.getSequenceValueFor(b3), is(5L)); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionIfStartIsCalledTwice() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.start(); }
@Test public void shouldAddEventProcessorsAfterPublishing() throws Exception { RingBuffer<TestEvent> rb = disruptor.getRingBuffer(); BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>( rb, rb.newBarrier(), new SleepingEventHandler()); BatchEventProcessor<TestEvent> b2 = new BatchEventProcessor<TestEvent>( rb, rb.newBarrier(b1.getSequence()), new SleepingEventHandler()); BatchEventProcessor<TestEvent> b3 = new BatchEventProcessor<TestEvent>( rb, rb.newBarrier(b2.getSequence()), new SleepingEventHandler()); assertThat(b1.getSequence().get(), is(-1L)); assertThat(b2.getSequence().get(), is(-1L)); assertThat(b3.getSequence().get(), is(-1L)); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); disruptor.handleEventsWith(b1, b2, b3); assertThat(b1.getSequence().get(), is(5L)); assertThat(b2.getSequence().get(), is(5L)); assertThat(b3.getSequence().get(), is(5L)); }
@Test public void should() throws Exception { RingBuffer<TestEvent> rb = disruptor.getRingBuffer(); BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>( rb, rb.newBarrier(), new SleepingEventHandler()); EventProcessorFactory<TestEvent> b2 = new EventProcessorFactory<TestEvent>() { @Override public EventProcessor createEventProcessor( RingBuffer<TestEvent> ringBuffer, Sequence[] barrierSequences) { return new BatchEventProcessor<TestEvent>( ringBuffer, ringBuffer.newBarrier(barrierSequences), new SleepingEventHandler()); } }; disruptor.handleEventsWith(b1).then(b2); disruptor.start(); }
Disruptor<StubEvent> disruptor = new Disruptor<StubEvent>( StubEvent.EVENT_FACTORY, 1024, DaemonThreadFactory.INSTANCE); RingBuffer<StubEvent> ringBuffer = disruptor.start(); new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(), handler1); new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(processor1.getSequence()), handler2); ringBuffer.addGatingSequences(processor1.getSequence(), processor2.getSequence());
@VisibleForTesting protected SyncFuture publishSyncOnRingBuffer(long sequence, boolean forceSync) { // here we use ring buffer sequence as transaction id SyncFuture syncFuture = getSyncFuture(sequence).setForceSync(forceSync); try { RingBufferTruck truck = this.disruptor.getRingBuffer().get(sequence); truck.load(syncFuture); } finally { this.disruptor.getRingBuffer().publish(sequence); } return syncFuture; }
@Override public void destroy() { disruptor.shutdown(); }
@Test public void shouldSetSequenceForWorkProcessorIfAddedAfterPublish() throws Exception { RingBuffer<TestEvent> rb = disruptor.getRingBuffer(); TestWorkHandler wh1 = createTestWorkHandler(); TestWorkHandler wh2 = createTestWorkHandler(); TestWorkHandler wh3 = createTestWorkHandler(); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); rb.publish(rb.next()); disruptor.handleEventsWithWorkerPool(wh1, wh2, wh3); assertThat(disruptor.getRingBuffer().getMinimumGatingSequence(), is(5L)); }
@SuppressWarnings("unchecked") @Before public void setUp() { disruptor = new Disruptor<byte[]>( new ByteArrayFactory(256), 1024, DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new BlockingWaitStrategy()); disruptor.handleEventsWith(eventHandler); disruptor.setDefaultExceptionHandler(new FatalExceptionHandler()); }
@Test(timeout = 1000) public void shouldTrackRemainingCapacity() throws Exception { final long[] remainingCapacity = {-1}; //Given final EventHandler<TestEvent> eventHandler = new EventHandler<TestEvent>() { @Override public void onEvent(final TestEvent event, final long sequence, final boolean endOfBatch) throws Exception { remainingCapacity[0] = disruptor.getRingBuffer().remainingCapacity(); } }; disruptor.handleEventsWith(eventHandler); //When publishEvent(); //Then while (remainingCapacity[0] == -1) { Thread.sleep(100); } assertThat(remainingCapacity[0], is(ringBuffer.getBufferSize() - 1L)); assertThat(disruptor.getRingBuffer().remainingCapacity(), is(ringBuffer.getBufferSize() - 0L)); }
@Test public void shouldSupportHandlersAsDependenciesToCustomProcessors() throws Exception { final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch); final SequenceBarrier sequenceBarrier = disruptor.after(delayedEventHandler).asSequenceBarrier(); final BatchEventProcessor<TestEvent> processor = new BatchEventProcessor<TestEvent>(ringBuffer, sequenceBarrier, handlerWithBarrier); disruptor.handleEventsWith(processor); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler); }