private void updateGatingSequencesForNextInChain(final Sequence[] barrierSequences, final Sequence[] processorSequences) { if (processorSequences.length > 0) { ringBuffer.addGatingSequences(processorSequences); for (final Sequence barrierSequence : barrierSequences) { ringBuffer.removeGatingSequence(barrierSequence); } consumerRepository.unMarkEventProcessorsAsEndOfChain(barrierSequences); } }
public SequenceBarrierTest() { ringBuffer.addGatingSequences(new NoOpEventProcessor(ringBuffer).getSequence()); }
/** * Construct a work pool with an internal {@link RingBuffer} for convenience. * <p> * This option does not require {@link RingBuffer#addGatingSequences(Sequence...)} to be called before the work pool is started. * * @param eventFactory for filling the {@link RingBuffer} * @param exceptionHandler to callback when an error occurs which is not handled by the {@link WorkHandler}s. * @param workHandlers to distribute the work load across. */ @SafeVarargs public WorkerPool( final EventFactory<T> eventFactory, final ExceptionHandler<? super T> exceptionHandler, final WorkHandler<? super T>... workHandlers) { ringBuffer = RingBuffer.createMultiProducer(eventFactory, 1024, new BlockingWaitStrategy()); final SequenceBarrier barrier = ringBuffer.newBarrier(); final int numWorkers = workHandlers.length; workProcessors = new WorkProcessor[numWorkers]; for (int i = 0; i < numWorkers; i++) { workProcessors[i] = new WorkProcessor<>( ringBuffer, barrier, workHandlers[i], exceptionHandler, workSequence); } ringBuffer.addGatingSequences(getWorkerSequences()); }
/** * <p>Set up custom event processors to handle events from the ring buffer. The Disruptor will * automatically start this processors when {@link #start()} is called.</p> * * <p>This method can be used as the start of a chain. For example if the processor <code>A</code> must * process events before handler <code>B</code>:</p> * <pre><code>dw.handleEventsWith(A).then(B);</code></pre> * * @param processors the event processors that will process events. * @return a {@link EventHandlerGroup} that can be used to chain dependencies. */ public EventHandlerGroup<T> handleEventsWith(final EventProcessor... processors) { for (final EventProcessor processor : processors) { consumerRepository.add(processor); } final Sequence[] sequences = new Sequence[processors.length]; for (int i = 0; i < processors.length; i++) { sequences[i] = processors[i].getSequence(); } ringBuffer.addGatingSequences(sequences); return new EventHandlerGroup<>(this, consumerRepository, Util.getSequencesFor(processors)); }
public DisruptorQueueImpl(String queueName, ProducerType producerType, int bufferSize, WaitStrategy wait, boolean isBatch, int batchSize, long flushMs) { _queueName = PREFIX + queueName; _buffer = RingBuffer.create(producerType, new ObjectEventFactory(), bufferSize, wait); _consumer = new Sequence(); _barrier = _buffer.newBarrier(); _buffer.addGatingSequences(_consumer); _isBatch = isBatch; _cache = new ArrayList<>(); _inputBatchSize = batchSize; if (_isBatch) { _batcher = new ThreadLocalBatch(); _flusher = new DisruptorFlusher(Math.max(flushMs, 1)); _flusher.start(); } else { _batcher = null; } }
RingBuffer.createMultiProducer(RingBufferTruck::new, preallocatedEventCount); waitingConsumePayloadsGatingSequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE); waitingConsumePayloads.addGatingSequences(waitingConsumePayloadsGatingSequence);
BatchedPoller(RingBuffer<BatchedPoller.DataEvent<T>> ringBuffer, int batchSize) { this.poller = ringBuffer.newPoller(); ringBuffer.addGatingSequences(poller.getSequence()); if (batchSize < 1) { batchSize = 20; } this.maxBatchSize = batchSize; this.polledData = new BatchedData<T>(this.maxBatchSize); }
@Test public void shouldPublishEvent() { ringBuffer.addGatingSequences(new NoOpEventProcessor(ringBuffer).getSequence()); ringBuffer.publishEvent(this); ringBuffer.publishEvent(this); assertThat(Long.valueOf(ringBuffer.get(0).get()), is(Long.valueOf(0 + 29L))); assertThat(Long.valueOf(ringBuffer.get(1).get()), is(Long.valueOf(1 + 29L))); }
@Test public void shouldTryPublishEvent() throws Exception { ringBuffer.addGatingSequences(new Sequence()); for (int i = 0; i < BUFFER_SIZE; i++) { assertThat(ringBuffer.tryPublishEvent(this), is(true)); } for (int i = 0; i < BUFFER_SIZE; i++) { assertThat(Long.valueOf(ringBuffer.get(i).get()), is(Long.valueOf(i + 29L))); } assertThat(ringBuffer.tryPublishEvent(this), is(false)); }
final RingBuffer<StubEvent> buffer2 = createMultiProducer(StubEvent.EVENT_FACTORY, ringBufferSize); final TestEventProcessor processor = new TestEventProcessor(buffer2.newBarrier()); buffer2.addGatingSequences(processor.getSequence());
@Test public void shouldCallExceptionHandlerOnUncaughtException() throws Exception { CountDownLatch exceptionLatch = new CountDownLatch(1); LatchExceptionHandler latchExceptionHandler = new LatchExceptionHandler(exceptionLatch); final BatchEventProcessor<StubEvent> batchEventProcessor = new BatchEventProcessor<StubEvent>( ringBuffer, sequenceBarrier, new ExceptionEventHandler()); ringBuffer.addGatingSequences(batchEventProcessor.getSequence()); batchEventProcessor.setExceptionHandler(latchExceptionHandler); Thread thread = new Thread(batchEventProcessor); thread.start(); ringBuffer.publish(ringBuffer.next()); assertTrue(exceptionLatch.await(2, TimeUnit.SECONDS)); batchEventProcessor.halt(); thread.join(); }
new SimpleParserStage(logContext)); simpleParserStage.setExceptionHandler(exceptionHandler); disruptorMsgBuffer.addGatingSequences(simpleParserStage.getSequence()); workHandlers); Sequence[] sequence = workerPool.getWorkerSequences(); disruptorMsgBuffer.addGatingSequences(sequence); new SinkStoreStage()); sinkStoreStage.setExceptionHandler(exceptionHandler); disruptorMsgBuffer.addGatingSequences(sinkStoreStage.getSequence());
@Test public void shouldCallMethodsInLifecycleOrderForBatch() throws Exception { CountDownLatch eventLatch = new CountDownLatch(3); LatchEventHandler eventHandler = new LatchEventHandler(eventLatch); final BatchEventProcessor<StubEvent> batchEventProcessor = new BatchEventProcessor<StubEvent>( ringBuffer, sequenceBarrier, eventHandler); ringBuffer.addGatingSequences(batchEventProcessor.getSequence()); ringBuffer.publish(ringBuffer.next()); ringBuffer.publish(ringBuffer.next()); ringBuffer.publish(ringBuffer.next()); Thread thread = new Thread(batchEventProcessor); thread.start(); assertTrue(eventLatch.await(2, TimeUnit.SECONDS)); batchEventProcessor.halt(); thread.join(); }
ringBuffer.addGatingSequences(poller.getSequence());
@Test public void shouldReportProgressByUpdatingSequenceViaCallback() throws Exception { final RingBuffer<StubEvent> ringBuffer = createMultiProducer(StubEvent.EVENT_FACTORY, 16); final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(); final SequenceReportingEventHandler<StubEvent> handler = new TestSequenceReportingEventHandler(); final BatchEventProcessor<StubEvent> batchEventProcessor = new BatchEventProcessor<StubEvent>( ringBuffer, sequenceBarrier, handler); ringBuffer.addGatingSequences(batchEventProcessor.getSequence()); Thread thread = new Thread(batchEventProcessor); thread.setDaemon(true); thread.start(); assertEquals(-1L, batchEventProcessor.getSequence().get()); ringBuffer.publish(ringBuffer.next()); callbackLatch.await(); assertEquals(0L, batchEventProcessor.getSequence().get()); onEndOfBatchLatch.countDown(); assertEquals(0L, batchEventProcessor.getSequence().get()); batchEventProcessor.halt(); thread.join(); }
@SuppressWarnings("deprecation") private void assertHandleResetAndNotWrap(RingBuffer<StubEvent> rb) { Sequence sequence = new Sequence(); rb.addGatingSequences(sequence); for (int i = 0; i < 128; i++) { rb.publish(rb.next()); sequence.incrementAndGet(); } assertThat(rb.getCursor(), is(127L)); rb.resetTo(31); sequence.set(31); for (int i = 0; i < 4; i++) { rb.publish(rb.next()); } assertThat(rb.hasAvailableCapacity(1), is(false)); }
@Test public void shouldPreventWrapping() throws Exception { Sequence sequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE); final RingBuffer<StubEvent> ringBuffer = createMultiProducer(StubEvent.EVENT_FACTORY, 4); ringBuffer.addGatingSequences(sequence); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 0, "0"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 1, "1"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 2, "2"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 3, "3"); assertFalse(ringBuffer.tryPublishEvent(StubEvent.TRANSLATOR, 3, "3")); }
@Test public void shouldAddAndRemoveSequences() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 16); Sequence sequenceThree = new Sequence(-1); Sequence sequenceSeven = new Sequence(-1); ringBuffer.addGatingSequences(sequenceThree, sequenceSeven); for (int i = 0; i < 10; i++) { ringBuffer.publish(ringBuffer.next()); } sequenceThree.set(3); sequenceSeven.set(7); assertThat(ringBuffer.getMinimumGatingSequence(), is(3L)); assertTrue(ringBuffer.removeGatingSequence(sequenceThree)); assertThat(ringBuffer.getMinimumGatingSequence(), is(7L)); }
ringBuffer.addGatingSequences(processor1.getSequence(), processor2.getSequence());
@Test public void shouldThrowExceptionIfBufferIsFull() throws Exception { ringBuffer.addGatingSequences(new Sequence(ringBuffer.getBufferSize())); try { for (int i = 0; i < ringBuffer.getBufferSize(); i++) { ringBuffer.publish(ringBuffer.tryNext()); } } catch (Exception e) { fail("Should not of thrown exception"); } try { ringBuffer.tryNext(); fail("Exception should have been thrown"); } catch (InsufficientCapacityException e) { } }