/** * Create a dependency barrier for the processors in this group. * This allows custom event processors to have dependencies on * {@link com.lmax.disruptor.BatchEventProcessor}s created by the disruptor. * * @return a {@link SequenceBarrier} including all the processors in this group. */ public SequenceBarrier asSequenceBarrier() { return disruptor.getRingBuffer().newBarrier(sequences); } }
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_ON_SOME_PATH_EXCEPTION", justification = "Will never be null") @Override public long append(final RegionInfo hri, final WALKeyImpl key, final WALEdit edits, final boolean inMemstore) throws IOException { return stampSequenceIdAndPublishToRingBuffer(hri, key, edits, inMemstore, disruptor.getRingBuffer()); }
RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
/** * publish disruptor event. * * @param hmilyTransaction {@linkplain HmilyTransaction } * @param type {@linkplain EventTypeEnum} */ public void publishEvent(final HmilyTransaction hmilyTransaction, final int type) { final RingBuffer<HmilyTransactionEvent> ringBuffer = disruptor.getRingBuffer(); ringBuffer.publishEvent(new HmilyTransactionEventTranslator(type), hmilyTransaction); }
@Override public void onEvent(final TestEvent event, final long sequence, final boolean endOfBatch) throws Exception { remainingCapacity[0] = disruptor.getRingBuffer().remainingCapacity(); } };
@VisibleForTesting protected long getSequenceOnRingBuffer() { return this.disruptor.getRingBuffer().next(); }
/** * publish disruptor event. * * @param mythTransaction {@linkplain MythTransaction } * @param type {@linkplain EventTypeEnum} */ public void publishEvent(final MythTransaction mythTransaction, final int type) { final RingBuffer<MythTransactionEvent> ringBuffer = disruptor.getRingBuffer(); ringBuffer.publishEvent(new MythTransactionEventTranslator(type), mythTransaction); }
private static void smarterPublish(Disruptor<Event> disruptor, AtomicBoolean running) { final RingBuffer<Event> ringBuffer = disruptor.getRingBuffer(); boolean publishOk; do { publishOk = ringBuffer.tryPublishEvent(new EventTranslator<Event>() { @Override public void translateTo(Event event, long sequence) { event.value = sequence; } }); } while (publishOk && running.get()); } }
@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 EventProcessor createEventProcessor( final RingBuffer<TestEvent> ringBuffer, final Sequence[] barrierSequences) { assertEquals("Should not have had any barrier sequences", 0, barrierSequences.length); return new BatchEventProcessor<TestEvent>( disruptor.getRingBuffer(), ringBuffer.newBarrier( barrierSequences), eventHandler); } });
@Override public EventProcessor createEventProcessor( final RingBuffer<TestEvent> ringBuffer, final Sequence[] barrierSequences) { assertSame("Should have had a barrier sequence", 1, barrierSequences.length); return new BatchEventProcessor<TestEvent>( disruptor.getRingBuffer(), ringBuffer.newBarrier( barrierSequences), eventHandler); } });
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); } }
@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 shouldSupportCustomProcessorsAsDependencies() throws Exception { RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch); final BatchEventProcessor<TestEvent> processor = new BatchEventProcessor<TestEvent>(ringBuffer, ringBuffer.newBarrier(), delayedEventHandler); disruptor.handleEventsWith(processor).then(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler); }
@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); }
@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)); }
@Test public void shouldSupportCustomProcessorsAndHandlersAsDependencies() throws Exception { final DelayedEventHandler delayedEventHandler1 = createDelayedEventHandler(); final DelayedEventHandler delayedEventHandler2 = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler1); RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch); final SequenceBarrier sequenceBarrier = disruptor.after(delayedEventHandler1).asSequenceBarrier(); final BatchEventProcessor<TestEvent> processor = new BatchEventProcessor<TestEvent>(ringBuffer, sequenceBarrier, delayedEventHandler2); disruptor.after(delayedEventHandler1).and(processor).handleEventsWith(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler1, delayedEventHandler2); }
@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 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(); }
@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)); }