Refine search
private synchronized List<Object> getConsumeBatch() throws AlertException, InterruptedException, TimeoutException { long endCursor = getAvailableConsumeCursor(); long currCursor = _consumer.get(); long eventNumber = endCursor - currCursor; List<Object> batch = new ArrayList<>((int) eventNumber); for (long curr = currCursor + 1; curr <= endCursor; curr++) { try { MutableObject mo = _buffer.get(curr); Object o = mo.o; mo.setObject(null); batch.add(o); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } } _consumer.set(endCursor); return batch; }
@Test public void shouldReturnMinimumOf2Sequences() throws Exception { Sequence sequence1 = new Sequence(34); Sequence sequnece2 = new Sequence(47); Sequence group = new FixedSequenceGroup(new Sequence[]{sequence1, sequnece2}); assertThat(group.get(), is(34L)); sequence1.set(35); assertThat(group.get(), is(35L)); sequence1.set(48); assertThat(group.get(), is(47L)); } }
/** * Atomically increment the sequence by one. * * @return The value after the increment */ public long incrementAndGet() { return addAndGet(1L); }
/** * Atomically add the supplied value. * * @param increment The value to add to the sequence. * @return The value after the increment. */ public long addAndGet(final long increment) { long currentValue; long newValue; do { currentValue = get(); newValue = currentValue + increment; } while (!compareAndSet(currentValue, newValue)); return newValue; }
private boolean hasAvailableCapacity(Sequence[] gatingSequences, final int requiredCapacity, long cursorValue) { long wrapPoint = (cursorValue + requiredCapacity) - bufferSize; long cachedGatingSequence = gatingSequenceCache.get(); if (wrapPoint > cachedGatingSequence || cachedGatingSequence > cursorValue) { long minSequence = Util.getMinimumSequence(gatingSequences, cursorValue); gatingSequenceCache.set(minSequence); if (wrapPoint > minSequence) { return false; } } return true; }
current = cursor.get(); next = current + n; long cachedGatingSequence = gatingSequenceCache.get(); gatingSequenceCache.set(gatingSequence); else if (cursor.compareAndSet(current, next))
conf.getInt("hbase.regionserver.wal.disruptor.event.count", 1024 * 16); waitingConsumePayloads = RingBuffer.createMultiProducer(RingBufferTruck::new, preallocatedEventCount); waitingConsumePayloadsGatingSequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE); waitingConsumePayloads.addGatingSequences(waitingConsumePayloadsGatingSequence); waitingConsumePayloads.publish(waitingConsumePayloads.next()); waitingConsumePayloadsGatingSequence.set(waitingConsumePayloads.getCursor());
long nextSequence = sequence.get(); T event = null; while (true) do nextSequence = workSequence.get() + 1L; sequence.set(nextSequence - 1L); while (!workSequence.compareAndSet(nextSequence - 1L, nextSequence)); event = ringBuffer.get(nextSequence); workHandler.onEvent(event); processedSequence = true; notifyTimeout(sequence.get());
@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 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)); }
@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(); }
@Test public void shouldAddWhileRunning() throws Exception { RingBuffer<TestEvent> ringBuffer = RingBuffer.createSingleProducer(TestEvent.EVENT_FACTORY, 32); final Sequence sequenceThree = new Sequence(3L); final Sequence sequenceSeven = new Sequence(7L); final SequenceGroup sequenceGroup = new SequenceGroup(); sequenceGroup.add(sequenceSeven); for (int i = 0; i < 11; i++) { ringBuffer.publish(ringBuffer.next()); } sequenceGroup.addWhileRunning(ringBuffer, sequenceThree); assertThat(sequenceThree.get(), is(10L)); } }
if (block || _buffer.getCursor() > _consumer.get()) { final long nextSequence = _consumer.get() + 1; try { final long availableSequence = _barrier.waitFor(nextSequence); if (availableSequence >= nextSequence) { MutableObject mo = _buffer.get(nextSequence); _consumer.set(nextSequence); ret = mo.o; mo.setObject(null);
@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 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) { } }
@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)); }
@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")); }
@Override public T poll() { long l = consumedSeq.get() + 1; if (l > knownPublishedSeq) { updatePublishedSequence(); } if (l <= knownPublishedSeq) { Event<T> eventHolder = ringBuffer.get(l); T t = eventHolder.removeValue(); consumedSeq.incrementAndGet(); return t; } return null; }
nextSequence = sequence.get() + 1L; final boolean unbounded = pendingRequest.get() == Long.MAX_VALUE; while (true) { try { final long availableSequence = processor.barrier.waitFor(nextSequence); while (nextSequence <= availableSequence) { event = processor.ringBuffer.get(nextSequence); if (!unbounded && pendingRequest.addAndGet(-1l) < 0l) { pendingRequest.incrementAndGet(); while (pendingRequest.addAndGet(-1l) < 0l) { pendingRequest.incrementAndGet(); sequence.set(availableSequence); } catch (final TimeoutException e) { } else { long cursor = processor.barrier.getCursor(); if (processor.ringBuffer.get(cursor).type == MutableSignal.Type.ERROR) { sequence.set(cursor); nextSequence = cursor; } else { sequence.set(cursor - 1l); sequence.set(nextSequence); nextSequence++;
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; } }