/** * Create a new Disruptor. Will default to {@link com.lmax.disruptor.BlockingWaitStrategy} and * {@link ProducerType}.MULTI * * @deprecated Use a {@link ThreadFactory} instead of an {@link Executor} as a the ThreadFactory * is able to report errors when it is unable to construct a thread to run a producer. * * @param eventFactory the factory to create events in the ring buffer. * @param ringBufferSize the size of the ring buffer. * @param executor an {@link Executor} to execute event processors. */ @Deprecated public Disruptor(final EventFactory<T> eventFactory, final int ringBufferSize, final Executor executor) { this(RingBuffer.createMultiProducer(eventFactory, ringBufferSize), executor); }
/** * Create a new Disruptor. Will default to {@link com.lmax.disruptor.BlockingWaitStrategy} and * {@link ProducerType}.MULTI * * @param eventFactory the factory to create events in the ring buffer. * @param ringBufferSize the size of the ring buffer. * @param threadFactory a {@link ThreadFactory} to create threads to for processors. */ public Disruptor(final EventFactory<T> eventFactory, final int ringBufferSize, final ThreadFactory threadFactory) { this(RingBuffer.createMultiProducer(eventFactory, ringBufferSize), new BasicExecutor(threadFactory)); }
/** * Create a new multiple producer RingBuffer using the default wait strategy {@link BlockingWaitStrategy}. * * @param <E> Class of the event stored in the ring buffer. * @param factory used to create the events within the ring buffer. * @param bufferSize number of elements to create within the ring buffer. * @return a constructed ring buffer. * @throws IllegalArgumentException if <code>bufferSize</code> is less than 1 or not a power of 2 * @see MultiProducerSequencer */ public static <E> RingBuffer<E> createMultiProducer(EventFactory<E> factory, int bufferSize) { return createMultiProducer(factory, bufferSize, new BlockingWaitStrategy()); }
/** * Create a new Ring Buffer with the specified producer type (SINGLE or MULTI) * * @param <E> Class of the event stored in the ring buffer. * @param producerType producer type to use {@link ProducerType}. * @param factory used to create events within the ring buffer. * @param bufferSize number of elements to create within the ring buffer. * @param waitStrategy used to determine how to wait for new elements to become available. * @return a constructed ring buffer. * @throws IllegalArgumentException if bufferSize is less than 1 or not a power of 2 */ public static <E> RingBuffer<E> create( ProducerType producerType, EventFactory<E> factory, int bufferSize, WaitStrategy waitStrategy) { switch (producerType) { case SINGLE: return createSingleProducer(factory, bufferSize, waitStrategy); case MULTI: return createMultiProducer(factory, bufferSize, waitStrategy); default: throw new IllegalStateException(producerType.toString()); } }
@Test public void shouldHandleResetToAndNotWrapUnnecessarilyMultiProducer() throws Exception { assertHandleResetAndNotWrap(RingBuffer.createMultiProducer(StubEvent.EVENT_FACTORY, 4)); }
/** * 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()); }
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);
public static void main(String[] args) throws Exception { RingBuffer<DataEvent<Object>> ringBuffer = RingBuffer.createMultiProducer(DataEvent.factory(), 1024); final EventPoller<DataEvent<Object>> poller = ringBuffer.newPoller(); Object value = getNextValue(poller); // Value could be null if no events are available. if (null != value) { // Process value. } }
public static void main(String[] args) throws Exception { int batchSize = 40; RingBuffer<BatchedPoller.DataEvent<Object>> ringBuffer = RingBuffer.createMultiProducer(BatchedPoller.DataEvent.factory(), 1024); BatchedPoller<Object> poller = new BatchedPoller<Object>(ringBuffer, batchSize); Object value = poller.poll(); // Value could be null if no events are available. if (null != value) { // Process value. } } }
final CountDownLatch latch = new CountDownLatch(ringBufferSize); final AtomicBoolean publisherComplete = new AtomicBoolean(false); final RingBuffer<StubEvent> buffer2 = createMultiProducer(StubEvent.EVENT_FACTORY, ringBufferSize); final TestEventProcessor processor = new TestEventProcessor(buffer2.newBarrier()); buffer2.addGatingSequences(processor.getSequence());
final RingBuffer<byte[]> ringBuffer = RingBuffer.createMultiProducer(factory, 4, new SleepingWaitStrategy());
@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 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")); }
/** * Create a new Disruptor. Will default to {@link com.lmax.disruptor.BlockingWaitStrategy} and * {@link ProducerType}.MULTI * * @deprecated Use a {@link ThreadFactory} instead of an {@link Executor} as a the ThreadFactory * is able to report errors when it is unable to construct a thread to run a producer. * * @param eventFactory the factory to create events in the ring buffer. * @param ringBufferSize the size of the ring buffer. * @param executor an {@link Executor} to execute event processors. */ @Deprecated public Disruptor(final EventFactory<T> eventFactory, final int ringBufferSize, final Executor executor) { this(RingBuffer.createMultiProducer(eventFactory, ringBufferSize), executor); }
/** * Create a new Disruptor. Will default to {@link com.lmax.disruptor.BlockingWaitStrategy} and * {@link ProducerType}.MULTI * * @param eventFactory the factory to create events in the ring buffer. * @param ringBufferSize the size of the ring buffer. * @param threadFactory a {@link ThreadFactory} to create threads to for processors. */ public Disruptor(final EventFactory<T> eventFactory, final int ringBufferSize, final ThreadFactory threadFactory) { this(RingBuffer.createMultiProducer(eventFactory, ringBufferSize), new BasicExecutor(threadFactory)); }
/** * Create a new multiple producer RingBuffer using the default wait strategy {@link BlockingWaitStrategy}. * * @param <E> Class of the event stored in the ring buffer. * @param factory used to create the events within the ring buffer. * @param bufferSize number of elements to create within the ring buffer. * @return a constructed ring buffer. * @throws IllegalArgumentException if <tt>bufferSize</tt> is less than 1 or not a power of 2 * @see MultiProducerSequencer */ public static <E> RingBuffer<E> createMultiProducer(EventFactory<E> factory, int bufferSize) { return createMultiProducer(factory, bufferSize, new BlockingWaitStrategy()); }
/** * Create a new Ring Buffer with the specified producer type (SINGLE or MULTI) * * @param <E> Class of the event stored in the ring buffer. * @param producerType producer type to use {@link ProducerType}. * @param factory used to create events within the ring buffer. * @param bufferSize number of elements to create within the ring buffer. * @param waitStrategy used to determine how to wait for new elements to become available. * @return a constructed ring buffer. * @throws IllegalArgumentException if bufferSize is less than 1 or not a power of 2 */ public static <E> RingBuffer<E> create( ProducerType producerType, EventFactory<E> factory, int bufferSize, WaitStrategy waitStrategy) { switch (producerType) { case SINGLE: return createSingleProducer(factory, bufferSize, waitStrategy); case MULTI: return createMultiProducer(factory, bufferSize, waitStrategy); default: throw new IllegalStateException(producerType.toString()); } }
/** * 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()); }
/** * Create a new multiple producer RingBuffer using the default wait strategy {@link BlockingWaitStrategy}. * * @param factory used to create the events within the ring buffer. * @param bufferSize number of elements to create within the ring buffer. * @throws IllegalArgumentException if <tt>bufferSize</tt> is less than 1 or not a power of 2 * @see MultiProducerSequencer */ public static <E> RingBuffer<E> createMultiProducer(EventFactory<E> factory, int bufferSize) { return createMultiProducer(factory, bufferSize, new BlockingWaitStrategy()); }
@Bean public WorkerPool<ServerEvent> workerPool(Gson gson, WebSocketKafkaProxyConfig proxyConfig, HandlersMapper requestHandlersMapper) { final ProxyServerEventHandler[] workHandlers = new ProxyServerEventHandler[proxyConfig.getWorkerThreads()]; IntStream.range(0, proxyConfig.getWorkerThreads()).forEach( nbr -> workHandlers[nbr] = new ProxyServerEventHandler(gson, proxyConfig, requestHandlersMapper) ); final RingBuffer<ServerEvent> ringBuffer = RingBuffer.createMultiProducer(ServerEvent::new, proxyConfig.getBufferSize(), getWaitStrategy()); final SequenceBarrier barrier = ringBuffer.newBarrier(); WorkerPool<ServerEvent> workerPool = new WorkerPool<>(ringBuffer, barrier, new FatalExceptionHandler(), workHandlers); ringBuffer.addGatingSequences(workerPool.getWorkerSequences()); return workerPool; }