Java Code Examples for com.lmax.disruptor.dsl.ProducerType#SINGLE

The following examples show how to use com.lmax.disruptor.dsl.ProducerType#SINGLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RingBufferSupplier.java    From conga with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a background thread to process events
 */
public void start() {
  if (disruptor == null) {
    disruptor = new Disruptor<BufferEvent>(BufferEvent::new, queueDepth, threadFactory,
        ProducerType.SINGLE, new BusySpinWaitStrategy());

    // Connect the handler
    disruptor.handleEventsWith(eventHandler);

    // Start the Disruptor, starts all threads running
    disruptor.start();

    ringBuffer = disruptor.getRingBuffer();
  }
}
 
Example 2
Source File: DisruptorBroker.java    From swarm with MIT License 5 votes vote down vote up
public void initialize() throws Exception {
  logger.info("Initializing...");
  logger.info("> parallelism={}", builder.parallelism);
  logger.info("> lowLatency={}", builder.lowLatency);
  logger.info("> bufferSize={}", builder.bufferSize);

  WaitStrategy waitStrategy =
      builder.isLowLatency() ? new BusySpinWaitStrategy() : new BlockingWaitStrategy();
  ProducerType producerType =
      builder.getProducerMode() == ProducerMode.SINGLE ? ProducerType.SINGLE : ProducerType.MULTI;
  EventFactory eventFactory = () -> new Event();

  disruptor =
      new Disruptor(
          eventFactory, builder.bufferSize, getThreadFactory(), producerType, waitStrategy);
  initializeRingBuffer();

  disruptor.handleEventsWithWorkerPool(getWorkersPool());
  // Start the Disruptor, starts all threads running
  disruptor.start();

  logger.info("Initialized");
}
 
Example 3
Source File: DisruptorAdapterHandler.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
        protected Disruptor<ConcurrentEvent> initialValue() {
            Disruptor<ConcurrentEvent> disruptor = new Disruptor<>(
                    ConcurrentEventFactory.DEFAULT, DEFAULT_RING_BUFFER_SIZE, CACHED_THREAD_POOL, ProducerType.SINGLE, new BlockingWaitStrategy());
            disruptor.handleEventsWith(new ConcurrentHandler());
//            disruptor.handleExceptionsWith();
            disruptor.start();
            return disruptor;
        }
 
Example 4
Source File: DisruptorAdapterBy41xHandler.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
        protected Disruptor<ConcurrentEvent> initialValue() {
            Disruptor<ConcurrentEvent> disruptor = new Disruptor<>(
                    ConcurrentEventFactory.DEFAULT, DEFAULT_RING_BUFFER_SIZE, CACHED_THREAD_POOL, ProducerType.SINGLE, new LiteBlockingWaitStrategy());
            disruptor.handleEventsWith(new ConcurrentHandler());
//            disruptor.handleExceptionsWith();
            disruptor.start();
            return disruptor;
        }
 
Example 5
Source File: BenchmarkHandler.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
        protected Disruptor<ConcurrentEvent> initialValue() {
            Disruptor<ConcurrentEvent> disruptor = new Disruptor<>(
                    ConcurrentEventFactory.DEFAULT, DEFAULT_RING_BUFFER_SIZE, CACHED_THREAD_POOL, ProducerType.SINGLE, new LiteBlockingWaitStrategy());
            disruptor.handleEventsWith(new ConcurrentHandler());
//            disruptor.handleExceptionsWith();
            disruptor.start();
            return disruptor;
        }
 
Example 6
Source File: RingBufferTrainingExecutor.java    From sgdtk with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the Disruptor.  The buffer size must be a power of 2 or the RingBuffer will complain
 *
 * @param learner The learner
 * @param model The initialized but untrained model
 * @param numEpochs The number of epochs
 * @param cacheFile The cache file to use
 * @param bufferSize The size of the internal buffer to train from
 */
@Override
public void initialize(Learner learner, Model model, int numEpochs, File cacheFile, int bufferSize, List<TrainingEventListener> listeners)
{

    this.numEpochs = numEpochs;
    executor = Executors.newSingleThreadExecutor();
    MessageEventFactory factory = new MessageEventFactory();
    WaitStrategy waitStrategy = (strategy == Strategy.YIELD) ? new YieldingWaitStrategy(): new BusySpinWaitStrategy();
    disruptor = new Disruptor<MessageEvent>(factory, ExecUtils.nextPowerOf2(bufferSize), executor, ProducerType.SINGLE, waitStrategy);
    handler = new MessageEventHandler(learner, model, listeners);
    disruptor.handleEventsWith(handler);
    this.cacheFile = cacheFile;

}
 
Example 7
Source File: SingleProducerDisruptorExecutionStrategy.java    From gridgo with MIT License 4 votes vote down vote up
public SingleProducerDisruptorExecutionStrategy(final int bufferSize, final WaitStrategy waitStrategy,
        final ThreadFactory threadFactory) {
    super(ProducerType.SINGLE, bufferSize, waitStrategy, threadFactory);
}
 
Example 8
Source File: ExecutionStrategyUnitTest.java    From gridgo with MIT License 4 votes vote down vote up
@Test
public void testDisruptorStrategy() throws InterruptedException {
    var latch2 = new CountDownLatch(10);
    var s3 = new MultiProducerDisruptorExecutionStrategy<>();
    s3.start();
    for (int i = 0; i < 10; i++) {
        s3.execute(() -> {
            latch2.countDown();
        });
    }
    latch2.await();
    s3.stop();

    var latch3 = new CountDownLatch(10);
    var s4 = new SingleProducerDisruptorExecutionStrategy<>();
    s4.start();
    for (int i = 0; i < 10; i++) {
        s4.execute(() -> {
            latch3.countDown();
        });
    }
    latch3.await();
    s4.stop();

    var latch4 = new CountDownLatch(10);
    var s5 = new SingleConsumerDisruptorExecutionStrategy<>(ProducerType.SINGLE);
    s5.start();
    for (int i = 0; i < 10; i++) {
        s5.execute(() -> {
            latch4.countDown();
        });
    }
    latch4.await();
    s5.stop();

    var latch5 = new CountDownLatch(10);
    var s6 = new DisruptorWorkerPoolExecutionStrategy<>();
    s6.start();
    for (int i = 0; i < 10; i++) {
        s6.execute(() -> {
            latch5.countDown();
        });
    }
    latch5.await();
    s6.stop();
}
 
Example 9
Source File: AppMain.java    From perf-workshop with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    final CommandLineArgs commandLineArgs = new CommandLineArgs();
    new JCommander(commandLineArgs).parse(args);

    final Disruptor<Packet> packetDisruptor =
            new Disruptor<>(new Packet.Factory(commandLineArgs.getRecordLength()), commandLineArgs.getBufferSize(),
                    newCachedThreadPool(DAEMON_THREAD_FACTORY), ProducerType.SINGLE, new SpinLoopHintBusySpinWaitStrategy());

    final Overrides overrides = new Overrides(commandLineArgs);
    overrides.init();

    final Journaller journaller = new Journaller(SYSTEM_NANO_TIMER, commandLineArgs, overrides.enableJournaller());
    journaller.init();

    final Histogram[] messageTransitTimeHistograms = new Histogram[commandLineArgs.getNumberOfIterations()];
    setAll(messageTransitTimeHistograms, HISTOGRAMS::createHistogramForArray);
    final Histogram[] interMessageTimeHistograms = new Histogram[commandLineArgs.getNumberOfIterations()];
    setAll(interMessageTimeHistograms, HISTOGRAMS::createHistogramForArray);

    packetDisruptor.handleEventsWith(
            runOnCpus(wrap(new Accumulator(messageTransitTimeHistograms, interMessageTimeHistograms, SYSTEM_NANO_TIMER, commandLineArgs)::process),
                    "Accumulator", overrides.getAccumulatorThreadAffinity()),
            runOnCpus(wrap(journaller::process), "Journaller", overrides.getJournallerThreadAffinity()));

    packetDisruptor.start();

    final InputReader inputReader = new InputReader(packetDisruptor.getRingBuffer(), SYSTEM_NANO_TIMER, commandLineArgs);

    if(commandLineArgs.runSpinners())
    {
        System.out.println("Starting spinner threads to perturb the system");
        Spinners.SPINNERS.start();
    }

    System.out.println("Starting replay at " + new Date());

    final Thread thread = DAEMON_THREAD_FACTORY.newThread(THREADS.runOnCpu(inputReader::processFiles,
            overrides.getProducerThreadAffinity()));
    thread.start();

    try
    {
        thread.join();
        System.out.println("Finished replay at " + new Date());
        packetDisruptor.shutdown(1, TimeUnit.MINUTES);
    }
    catch (TimeoutException e)
    {
        throw new RuntimeException("Consumers did not process remaining events within timeout", e);
    }
    finally
    {
        Spinners.SPINNERS.stop();
        packetDisruptor.halt();
    }

    System.out.println("Pausing for 10 seconds...");
    THREADS.sleep(10L, TimeUnit.SECONDS);
}