com.lmax.disruptor.BlockingWaitStrategy Java Examples

The following examples show how to use com.lmax.disruptor.BlockingWaitStrategy. 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: DisruptorUtil.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
static WaitStrategy createWaitStrategy(final String propertyName, final long timeoutMillis) {
    final String strategy = PropertiesUtil.getProperties().getStringProperty(propertyName, "TIMEOUT");
    LOGGER.trace("property {}={}", propertyName, strategy);
    final String strategyUp = strategy.toUpperCase(Locale.ROOT); // TODO Refactor into Strings.toRootUpperCase(String)
    switch (strategyUp) { // TODO Define a DisruptorWaitStrategy enum?
    case "SLEEP":
        return new SleepingWaitStrategy();
    case "YIELD":
        return new YieldingWaitStrategy();
    case "BLOCK":
        return new BlockingWaitStrategy();
    case "BUSYSPIN":
        return new BusySpinWaitStrategy();
    case "TIMEOUT":
        return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
    default:
        return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
    }
}
 
Example #2
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private PersistenceProcessorHandler[] configureHandlers(TSOServerConfig tsoConfig,
                                                        LeaseManager leaseManager,
                                                        ObjectPool<Batch> batchPool)
        throws Exception {
    PersistenceProcessorHandler[] handlers = new PersistenceProcessorHandler[tsoConfig.getNumConcurrentCTWriters()];
    for (int i = 0; i < tsoConfig.getNumConcurrentCTWriters(); i++) {
        handlers[i] = new PersistenceProcessorHandler(metrics,
                                                      "localhost:1234",
                                                      leaseManager,
                                                      commitTable,
                                                      new ReplyProcessorImpl(new BlockingWaitStrategy(), metrics, panicker, batchPool, lowWatermarkWriter),
                                                      retryProcessor,
                                                      new RuntimeExceptionPanicker());
    }
    return handlers;
}
 
Example #3
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private void testPersistenceWithHALeaseManagerFailingToPreserveLease3(TSOServerConfig tsoConfig) throws Exception {

        // Init a HA lease manager
        LeaseManager simulatedHALeaseManager = mock(LeaseManager.class);

        ObjectPool<Batch> batchPool = spy(new BatchPoolModule(tsoConfig).getBatchPool());

        PersistenceProcessorHandler[] handlers = configureHandlers (tsoConfig, simulatedHALeaseManager, batchPool);

        // Component under test
        PersistenceProcessorImpl proc = new PersistenceProcessorImpl(tsoConfig, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                     panicker, handlers, metrics);

        // Test: Configure the lease manager to return true first and false later for stillInLeasePeriod and raise
        // an exception when flush
        // Configure mock writer to flush unsuccessfully
        doThrow(new IOException("Unable to write")).when(mockWriter).flush();
        doReturn(true).doReturn(false).when(simulatedHALeaseManager).stillInLeasePeriod();
        proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), mock(MonitoringContextImpl.class), Optional.<Long>absent());
        proc.triggerCurrentBatchFlush();
        verify(simulatedHALeaseManager, timeout(1000).times(1)).stillInLeasePeriod();
        verify(batchPool, times(2)).borrowObject();

    }
 
Example #4
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private void testPersistenceWithHALeaseManagerFailingToPreserveLease2(TSOServerConfig tsoConfig) throws Exception {

        // Init a HA lease manager
        LeaseManager simulatedHALeaseManager = mock(LeaseManager.class);

        ObjectPool<Batch> batchPool = spy(new BatchPoolModule(tsoConfig).getBatchPool());

        PersistenceProcessorHandler[] handlers = configureHandlers (tsoConfig, simulatedHALeaseManager, batchPool);

        // Component under test
        PersistenceProcessorImpl proc = new PersistenceProcessorImpl(tsoConfig, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                     panicker, handlers, metrics);

        // Test: Configure the lease manager to return false for stillInLeasePeriod
        doReturn(false).when(simulatedHALeaseManager).stillInLeasePeriod();
        proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), mock(MonitoringContextImpl.class), Optional.<Long>absent());
        proc.triggerCurrentBatchFlush();
        verify(simulatedHALeaseManager, timeout(1000).times(1)).stillInLeasePeriod();
        verify(batchPool, times(2)).borrowObject();
    }
 
Example #5
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private void testPersistenceWithHALeaseManagerFailingToPreserveLease1(TSOServerConfig tsoConfig) throws Exception {

        // Init a HA lease manager
        LeaseManager simulatedHALeaseManager = mock(LeaseManager.class);

        ObjectPool<Batch> batchPool = spy(new BatchPoolModule(tsoConfig).getBatchPool());

        PersistenceProcessorHandler[] handlers = configureHandlers (tsoConfig, simulatedHALeaseManager, batchPool);

        // Component under test
        PersistenceProcessorImpl proc = new PersistenceProcessorImpl(tsoConfig, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                     panicker, handlers, metrics);

        // Test: Configure the lease manager to return true first and false later for stillInLeasePeriod
        doReturn(true).doReturn(false).when(simulatedHALeaseManager).stillInLeasePeriod();
        proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), mock(MonitoringContextImpl.class), Optional.<Long>absent());
        proc.triggerCurrentBatchFlush();
        verify(simulatedHALeaseManager, timeout(1000).times(2)).stillInLeasePeriod();
        verify(batchPool, times(2)).borrowObject();
    }
 
Example #6
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 6 votes vote down vote up
private void testPersistenceWithHALeaseManagerPreservingLease(TSOServerConfig tsoConfig) throws Exception {

        // Init a HA lease manager
        LeaseManager simulatedHALeaseManager = mock(LeaseManager.class);

        ObjectPool<Batch> batchPool = spy(new BatchPoolModule(tsoConfig).getBatchPool());

        PersistenceProcessorHandler[] handlers = configureHandlers (tsoConfig, simulatedHALeaseManager, batchPool);

        // Component under test
        PersistenceProcessorImpl proc = new PersistenceProcessorImpl(tsoConfig, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                     panicker, handlers, metrics);

        // Test: Configure the lease manager to return true always
        doReturn(true).when(simulatedHALeaseManager).stillInLeasePeriod();
        proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), mock(MonitoringContextImpl.class), Optional.<Long>absent());
        proc.triggerCurrentBatchFlush();
        verify(simulatedHALeaseManager, timeout(1000).times(2)).stillInLeasePeriod();
        verify(batchPool, times(2)).borrowObject();
    }
 
Example #7
Source File: MythTransactionEventPublisher.java    From myth with Apache License 2.0 6 votes vote down vote up
/**
 * start disruptor.
 *
 * @param bufferSize bufferSize
 */
private void start(final int bufferSize, final int threadSize) {
    disruptor = new Disruptor<>(new MythTransactionEventFactory(), bufferSize, runnable -> {
        return new Thread(new ThreadGroup("hmily-disruptor"), runnable,
                "disruptor-thread-" + INDEX.getAndIncrement());
    }, ProducerType.MULTI, new BlockingWaitStrategy());
    final Executor executor = new ThreadPoolExecutor(MAX_THREAD, MAX_THREAD, 0, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            MythTransactionThreadFactory.create("myth-log-disruptor", false),
            new ThreadPoolExecutor.AbortPolicy());

    MythTransactionEventHandler[] consumers = new MythTransactionEventHandler[MAX_THREAD];
    for (int i = 0; i < threadSize; i++) {
        consumers[i] = new MythTransactionEventHandler(coordinatorService, executor);
    }
    disruptor.handleEventsWithWorkerPool(consumers);
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
}
 
Example #8
Source File: DisruptorProviderManage.java    From hmily with Apache License 2.0 6 votes vote down vote up
/**
 * start disruptor.
 */
@SuppressWarnings("unchecked")
public void startup() {
    Disruptor<DataEvent<T>> disruptor = new Disruptor<>(new DisruptorEventFactory<>(),
            size,
            HmilyThreadFactory.create("disruptor_consumer_" + consumerFactory.fixName(), false),
            ProducerType.MULTI,
            new BlockingWaitStrategy());
    DisruptorConsumer<T>[] consumers = new DisruptorConsumer[consumerSize];
    for (int i = 0; i < consumerSize; i++) {
        consumers[i] = new DisruptorConsumer<>(consumerFactory);
    }
    disruptor.handleEventsWithWorkerPool(consumers);
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
    RingBuffer<DataEvent<T>> ringBuffer = disruptor.getRingBuffer();
    provider = new DisruptorProvider<>(ringBuffer);
}
 
Example #9
Source File: TxTransactionEventPublisher.java    From Raincat with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * disruptor start.
 *
 * @param bufferSize this is disruptor buffer size.
 * @param threads this is disruptor consumer thread size.
 */
private void start(final int bufferSize, final int threads) {
    disruptor = new Disruptor<>(new TxTransactionEventFactory(), bufferSize, r -> {
        return new Thread(null, r, "disruptor-thread-" + INDEX.getAndIncrement());
    }, ProducerType.MULTI, new BlockingWaitStrategy());

    final Executor executor = new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            TxTransactionThreadFactory.create("raincat-log-disruptor", false),
            new ThreadPoolExecutor.AbortPolicy());

    TxTransactionEventHandler[] consumers = new TxTransactionEventHandler[threads];
    for (int i = 0; i < threads; i++) {
        consumers[i] = new TxTransactionEventHandler(executor, txCompensationService);
    }
    disruptor.handleEventsWithWorkerPool(consumers);
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
}
 
Example #10
Source File: FastEventServiceImpl.java    From redtorch with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	if ("BusySpinWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new BusySpinWaitStrategy());
	} else if ("SleepingWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new SleepingWaitStrategy());
	} else if ("BlockingWaitStrategy".equals(waitStrategy)) {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new BlockingWaitStrategy());
	} else {
		disruptor = new Disruptor<FastEvent>(new FastEventFactory(), 65536, DaemonThreadFactory.INSTANCE,
				ProducerType.MULTI, new YieldingWaitStrategy());
	}
	ringBuffer = disruptor.start();
}
 
Example #11
Source File: NettyUnitTest.java    From jstorm with Apache License 2.0 6 votes vote down vote up
private IConnection initNettyServer(int port) {
    ConcurrentHashMap<Integer, DisruptorQueue> deserializeQueues = new ConcurrentHashMap<Integer, DisruptorQueue>();
    //ConcurrentHashMap<Integer, DisruptorQueue> deserializeCtrlQueues = new ConcurrentHashMap<Integer, DisruptorQueue>();

    WaitStrategy wait = (WaitStrategy)Utils.newInstance("com.lmax.disruptor.TimeoutBlockingWaitStrategy", 5, TimeUnit.MILLISECONDS);
    DisruptorQueue recvControlQueue = DisruptorQueue.mkInstance("Dispatch-control", ProducerType.MULTI,
            256, wait, false, 0, 0);
    Set<Integer> taskSet = new HashSet<Integer>();
    taskSet.add(1);
    IConnection server = context.bind(null, port, deserializeQueues, recvControlQueue, true, taskSet);

    WaitStrategy waitStrategy = new BlockingWaitStrategy();
    DisruptorQueue recvQueue = DisruptorQueue.mkInstance("NettyUnitTest", ProducerType.SINGLE, 1024, waitStrategy, false, 0, 0);
    server.registerQueue(task, recvQueue);

    return server;
}
 
Example #12
Source File: FSHLog.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Create an edit log at the given <code>dir</code> location. You should never have to load an
 * existing log. If there is a log at startup, it should have already been processed and deleted
 * by the time the WAL object is started up.
 * @param fs filesystem handle
 * @param rootDir path to where logs and oldlogs
 * @param logDir dir where wals are stored
 * @param archiveDir dir where wals are archived
 * @param conf configuration to use
 * @param listeners Listeners on WAL events. Listeners passed here will be registered before we do
 *          anything else; e.g. the Constructor {@link #rollWriter()}.
 * @param failIfWALExists If true IOException will be thrown if files related to this wal already
 *          exist.
 * @param prefix should always be hostname and port in distributed env and it will be URL encoded
 *          before being used. If prefix is null, "wal" will be used
 * @param suffix will be url encoded. null is treated as empty. non-empty must start with
 *          {@link org.apache.hadoop.hbase.wal.AbstractFSWALProvider#WAL_FILE_NAME_DELIMITER}
 */
public FSHLog(final FileSystem fs, final Path rootDir, final String logDir,
    final String archiveDir, final Configuration conf, final List<WALActionsListener> listeners,
    final boolean failIfWALExists, final String prefix, final String suffix) throws IOException {
  super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix);
  this.minTolerableReplication = conf.getInt("hbase.regionserver.hlog.tolerable.lowreplication",
    CommonFSUtils.getDefaultReplication(fs, this.walDir));
  this.lowReplicationRollLimit = conf.getInt("hbase.regionserver.hlog.lowreplication.rolllimit",
    5);
  this.closeErrorsTolerated = conf.getInt("hbase.regionserver.logroll.errors.tolerated", 2);

  // This is the 'writer' -- a single threaded executor. This single thread 'consumes' what is
  // put on the ring buffer.
  String hostingThreadName = Thread.currentThread().getName();
  // Using BlockingWaitStrategy. Stuff that is going on here takes so long it makes no sense
  // spinning as other strategies do.
  this.disruptor = new Disruptor<>(RingBufferTruck::new,
      getPreallocatedEventCount(),
      Threads.newDaemonThreadFactory(hostingThreadName + ".append"),
      ProducerType.MULTI, new BlockingWaitStrategy());
  // Advance the ring buffer sequence so that it starts from 1 instead of 0,
  // because SyncFuture.NOT_DONE = 0.
  this.disruptor.getRingBuffer().next();
  int maxHandlersCount = conf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT, 200);
  this.ringBufferEventHandler = new RingBufferEventHandler(
      conf.getInt("hbase.regionserver.hlog.syncer.count", 5), maxHandlersCount);
  this.disruptor.setDefaultExceptionHandler(new RingBufferExceptionHandler());
  this.disruptor.handleEventsWith(new RingBufferEventHandler[] { this.ringBufferEventHandler });
  // Starting up threads in constructor is a no no; Interface should have an init call.
  this.disruptor.start();
}
 
Example #13
Source File: WaitStrategyTypeTest.java    From disruptor-spring-manager with MIT License 5 votes vote down vote up
@Test
public void test_All_WaitStrategies() {
	assertTrue(WaitStrategyType.BLOCKING.instance() instanceof BlockingWaitStrategy);
	assertTrue(WaitStrategyType.BUSY_SPIN.instance() instanceof BusySpinWaitStrategy);
	assertTrue(WaitStrategyType.LITE_BLOCKING.instance() instanceof LiteBlockingWaitStrategy);
	assertTrue(WaitStrategyType.SLEEPING_WAIT.instance() instanceof SleepingWaitStrategy);
	assertTrue(WaitStrategyType.YIELDING.instance() instanceof YieldingWaitStrategy);
}
 
Example #14
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 #15
Source File: WorkQueueDispatcher.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public WorkQueueDispatcher(String name,
                           int poolSize,
                           int backlog,
                           final Consumer<Throwable> uncaughtExceptionHandler) {
  this(name, poolSize, backlog, uncaughtExceptionHandler, ProducerType.MULTI, new BlockingWaitStrategy());
}
 
Example #16
Source File: TestReplyProcessor.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 10_000)
public void testBadFormedPackageThrowsException() throws Exception {

    // We need an instance throwing exceptions for this test
    replyProcessor = spy(new ReplyProcessorImpl(new BlockingWaitStrategy(), metrics, new RuntimeExceptionPanicker(), batchPool, lowWatermarkWriter));

    // Prepare test batch
    Batch batch = batchPool.borrowObject();
    batch.addCommitRetry(FIRST_ST, null, monCtx);
    ReplyBatchEvent e = ReplyBatchEvent.EVENT_FACTORY.newInstance();
    ReplyBatchEvent.makeReplyBatch(e, batch, 0);

    assertEquals(replyProcessor.nextIDToHandle.get(), 0);
    assertEquals(replyProcessor.futureEvents.size(), 0);
    assertEquals(batchPool.getNumActive(), 1);
    assertEquals(batchPool.getNumIdle(), BATCH_POOL_SIZE - 1);

    try {
        replyProcessor.onEvent(e, ANY_DISRUPTOR_SEQUENCE, false);
        fail();
    } catch (RuntimeException re) {
        // Expected
    }

    assertEquals(replyProcessor.nextIDToHandle.get(), 0);
    assertEquals(replyProcessor.futureEvents.size(), 0);
    assertEquals(batchPool.getNumActive(), 1);
    assertEquals(batchPool.getNumIdle(), BATCH_POOL_SIZE - 1);

}
 
Example #17
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 30_000)
public void testRuntimeExceptionOnCommitPersistenceTakesDownDaemon() throws Exception {

    TSOServerConfig config = new TSOServerConfig();

    ObjectPool<Batch> batchPool = new BatchPoolModule(config).getBatchPool();

    ReplyProcessor replyProcessor = new ReplyProcessorImpl(new BlockingWaitStrategy(), metrics, panicker, batchPool, lowWatermarkWriter);

    PersistenceProcessorHandler[] handlers = new PersistenceProcessorHandler[config.getNumConcurrentCTWriters()];
    for (int i = 0; i < config.getNumConcurrentCTWriters(); i++) {
        handlers[i] = new PersistenceProcessorHandler(metrics,
                                                      "localhost:1234",
                                                      mock(LeaseManager.class),
                                                      commitTable,
                                                      replyProcessor,
                                                      retryProcessor,
                                                      panicker);
    }

    PersistenceProcessorImpl proc = new PersistenceProcessorImpl(config, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                 panicker, handlers, metrics);

    // Configure writer to explode with a runtime exception
    doThrow(new RuntimeException("Kaboom!")).when(mockWriter).addCommittedTransaction(anyLong(), anyLong());
    MonitoringContextImpl monCtx = new MonitoringContextImpl(metrics);

    // Check the panic is extended!
    proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), monCtx, Optional.<Long>absent());
    proc.triggerCurrentBatchFlush();
    verify(panicker, timeout(1000).atLeastOnce()).panic(anyString(), any(Throwable.class));

}
 
Example #18
Source File: SlowLogRecorder.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize disruptor with configurable ringbuffer size
 */
public SlowLogRecorder(Configuration conf) {
  isOnlineLogProviderEnabled = conf.getBoolean(HConstants.SLOW_LOG_BUFFER_ENABLED_KEY,
    HConstants.DEFAULT_ONLINE_LOG_PROVIDER_ENABLED);

  if (!isOnlineLogProviderEnabled) {
    this.disruptor = null;
    this.logEventHandler = null;
    this.eventCount = 0;
    return;
  }

  this.eventCount = conf.getInt(SLOW_LOG_RING_BUFFER_SIZE,
    HConstants.DEFAULT_SLOW_LOG_RING_BUFFER_SIZE);

  // This is the 'writer' -- a single threaded executor. This single thread consumes what is
  // put on the ringbuffer.
  final String hostingThreadName = Thread.currentThread().getName();

  // disruptor initialization with BlockingWaitStrategy
  this.disruptor = new Disruptor<>(RingBufferEnvelope::new,
    getEventCount(),
    Threads.newDaemonThreadFactory(hostingThreadName + ".slowlog.append"),
    ProducerType.MULTI,
    new BlockingWaitStrategy());
  this.disruptor.setDefaultExceptionHandler(new DisruptorExceptionHandler());

  // initialize ringbuffer event handler
  final boolean isSlowLogTableEnabled = conf.getBoolean(HConstants.SLOW_LOG_SYS_TABLE_ENABLED_KEY,
    HConstants.DEFAULT_SLOW_LOG_SYS_TABLE_ENABLED_KEY);
  this.logEventHandler = new LogEventHandler(this.eventCount, isSlowLogTableEnabled, conf);
  this.disruptor.handleEventsWith(new LogEventHandler[]{this.logEventHandler});
  this.disruptor.start();
}
 
Example #19
Source File: StreamJunction.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Create and start disruptor based on annotations given in the streamDefinition.
 */
public void startProcessing() {
    this.exceptionListener = siddhiAppContext.getRuntimeExceptionListener();
    if (!receivers.isEmpty() && async) {
        for (Constructor constructor : Disruptor.class.getConstructors()) {
            if (constructor.getParameterTypes().length == 5) {      // If new disruptor classes available
                ProducerType producerType = ProducerType.MULTI;
                disruptor = new Disruptor<EventExchangeHolder>(
                        new EventExchangeHolderFactory(streamDefinition.getAttributeList().size()),
                        bufferSize, executorService, producerType,
                        new BlockingWaitStrategy());
                disruptor.handleExceptionsWith(siddhiAppContext.getDisruptorExceptionHandler());
                break;
            }
        }
        if (disruptor == null) {
            disruptor = new Disruptor<EventExchangeHolder>(
                    new EventExchangeHolderFactory(streamDefinition.getAttributeList().size()),
                    bufferSize, executorService);
            disruptor.handleExceptionsWith(siddhiAppContext.getDisruptorExceptionHandler());
        }
        if (workers > 0) {
            for (int i = 0; i < workers; i++) {
                disruptor.handleEventsWith(new StreamHandler(receivers, batchSize, streamDefinition.getId(),
                        siddhiAppContext.getName(), faultStreamJunction, onErrorAction, exceptionListener));
            }
        } else {
            disruptor.handleEventsWith(new StreamHandler(receivers, batchSize, streamDefinition.getId(),
                    siddhiAppContext.getName(), faultStreamJunction, onErrorAction, exceptionListener));
        }
        ringBuffer = disruptor.start();
    } else {
        for (Receiver receiver : receivers) {
            if (receiver instanceof StreamCallback) {
                ((StreamCallback) receiver).startProcessing();
            }
        }
    }
}
 
Example #20
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
EventQueue(int queueSize) {
    eventQueuePool = Executors.newSingleThreadExecutor(
            new DataBridgeThreadFactory("EventQueue"));
    eventQueueDisruptor = new Disruptor<>(new WrappedEventFactory(), queueSize, eventQueuePool,
            ProducerType.MULTI, new BlockingWaitStrategy());
    eventQueueDisruptor.handleEventsWith(new EventQueueWorker());
    this.ringBuffer = eventQueueDisruptor.start();
}
 
Example #21
Source File: ReadOnlyServiceImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init(final ReadOnlyServiceOptions opts) {
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.fsmCaller = opts.getFsmCaller();
    this.raftOptions = opts.getRaftOptions();

    this.scheduledExecutorService = Executors
        .newSingleThreadScheduledExecutor(new NamedThreadFactory("ReadOnlyService-PendingNotify-Scanner", true));
    this.readIndexDisruptor = DisruptorBuilder.<ReadIndexEvent> newInstance() //
        .setEventFactory(new ReadIndexEventFactory()) //
        .setRingBufferSize(this.raftOptions.getDisruptorBufferSize()) //
        .setThreadFactory(new NamedThreadFactory("JRaft-ReadOnlyService-Disruptor-", true)) //
        .setWaitStrategy(new BlockingWaitStrategy()) //
        .setProducerType(ProducerType.MULTI) //
        .build();
    this.readIndexDisruptor.handleEventsWith(new ReadIndexEventHandler());
    this.readIndexDisruptor
        .setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.readIndexQueue = this.readIndexDisruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry() //
            .register("jraft-read-only-service-disruptor", new DisruptorMetricSet(this.readIndexQueue));
    }
    // listen on lastAppliedLogIndex change events.
    this.fsmCaller.addLastAppliedLogIndexListener(this);

    // start scanner
    this.scheduledExecutorService.scheduleAtFixedRate(() -> onApplied(this.fsmCaller.getLastAppliedIndex()),
        this.raftOptions.getMaxElectionDelayMs(), this.raftOptions.getMaxElectionDelayMs(), TimeUnit.MILLISECONDS);
    return true;
}
 
Example #22
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init(final FSMCallerOptions opts) {
    this.logManager = opts.getLogManager();
    this.fsm = opts.getFsm();
    this.closureQueue = opts.getClosureQueue();
    this.afterShutdown = opts.getAfterShutdown();
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.lastAppliedIndex.set(opts.getBootstrapId().getIndex());
    notifyLastAppliedIndexUpdated(this.lastAppliedIndex.get());
    this.lastAppliedTerm = opts.getBootstrapId().getTerm();
    this.disruptor = DisruptorBuilder.<ApplyTask> newInstance() //
        .setEventFactory(new ApplyTaskFactory()) //
        .setRingBufferSize(opts.getDisruptorBufferSize()) //
        .setThreadFactory(new NamedThreadFactory("JRaft-FSMCaller-Disruptor-", true)) //
        .setProducerType(ProducerType.MULTI) //
        .setWaitStrategy(new BlockingWaitStrategy()) //
        .build();
    this.disruptor.handleEventsWith(new ApplyTaskHandler());
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.taskQueue = this.disruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry().register("jraft-fsm-caller-disruptor",
            new DisruptorMetricSet(this.taskQueue));
    }
    this.error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_NONE);
    LOG.info("Starts FSMCaller successfully.");
    return true;
}
 
Example #23
Source File: DisruptorConfigure.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "deprecation" })
@Bean(name="multiupdate")   
   public Disruptor<UserDataEvent> multiupdate() {   
   	Executor executor = Executors.newCachedThreadPool();
   	 MultiUpdateEventFactory factory = new MultiUpdateEventFactory();
   	 Disruptor<UserDataEvent> disruptor = new Disruptor<UserDataEvent>(factory, 1024, executor, ProducerType.MULTI , new BlockingWaitStrategy());
   	 disruptor.handleEventsWith(new MultiUpdateEventHandler());
   	 disruptor.setDefaultExceptionHandler(new UKeFuExceptionHandler());
   	 disruptor.start();
        return disruptor;   
   }
 
Example #24
Source File: LogEventPublisher.java    From fast-family-master with Apache License 2.0 5 votes vote down vote up
public void start() {
    disruptor = new Disruptor<LogEvent>(new LogEventFactory(), 1024, r -> {
        AtomicInteger integer = new AtomicInteger(1);
        return new Thread(null, r, "disruptor-thread-" + integer.getAndIncrement());
    }, ProducerType.MULTI, new BlockingWaitStrategy());
    disruptor.handleEventsWithWorkerPool(new LogEventHandler(logRepository));
    disruptor.setDefaultExceptionHandler(new IgnoreExceptionHandler());
    disruptor.start();
}
 
Example #25
Source File: Main.java    From java-concurrent-programming with MIT License 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    Executor executor = Executors.newCachedThreadPool();
    PCDataFactory factory = new PCDataFactory();

    int bufferSize = 1024;
    Disruptor<PCData> disruptor = new Disruptor<PCData>(factory,
            bufferSize,
            executor,
            ProducerType.MULTI,
            new BlockingWaitStrategy()
            );
    disruptor.handleEventsWithWorkerPool(
            new Consumer(),
            new Consumer(),
            new Consumer(),
            new Consumer()
    );
    disruptor.start();

    // 1个生产者 4个消费者
    RingBuffer<PCData> ringBuffer = disruptor.getRingBuffer();
    Producer producer = new Producer(ringBuffer);
    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++) {
        bb.putLong(0, l);
        producer.pushData(bb);
        Thread.sleep(100);
        System.out.println("add data "+l);
    }
}
 
Example #26
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
EventQueue(int queueSize) {
    eventQueuePool = Executors.newCachedThreadPool(
            new DataBridgeThreadFactory("EventQueue"));
    eventQueueDisruptor = new Disruptor<>(new WrappedEventFactory(), queueSize, eventQueuePool,
            ProducerType.MULTI, new BlockingWaitStrategy());
    eventQueueDisruptor.handleEventsWith(new EventQueueWorker());
    this.ringBuffer = eventQueueDisruptor.start();
}
 
Example #27
Source File: DisruptorModule.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    switch (config.getWaitStrategyEnum()) {
    // A low-cpu usage Disruptor configuration for using in local/test environments
    case LOW_CPU:
         bind(WaitStrategy.class).annotatedWith(Names.named("PersistenceStrategy")).to(BlockingWaitStrategy.class);
         bind(WaitStrategy.class).annotatedWith(Names.named("ReplyStrategy")).to(BlockingWaitStrategy.class);
         bind(WaitStrategy.class).annotatedWith(Names.named("RetryStrategy")).to(BlockingWaitStrategy.class);
         break;
    // The default high-cpu usage Disruptor configuration for getting high throughput on production environments
    case HIGH_THROUGHPUT:
    default:
         bind(WaitStrategy.class).annotatedWith(Names.named("PersistenceStrategy")).to(BusySpinWaitStrategy.class);
         bind(WaitStrategy.class).annotatedWith(Names.named("ReplyStrategy")).to(BusySpinWaitStrategy.class);
         bind(WaitStrategy.class).annotatedWith(Names.named("RetryStrategy")).to(YieldingWaitStrategy.class);
         break;
    }

    if (config.getLowLatency()) {
        bind(RequestProcessor.class).to(RequestProcessorSkipCT.class).in(Singleton.class);
        bind(PersistenceProcessor.class).to(PersitenceProcessorNullImpl.class).in(Singleton.class);
    } else {
        bind(PersistenceProcessor.class).to(PersistenceProcessorImpl.class).in(Singleton.class);
        bind(RequestProcessor.class).to(RequestProcessorPersistCT.class).in(Singleton.class);
    }

    bind(ReplyProcessor.class).to(ReplyProcessorImpl.class).in(Singleton.class);
    bind(RetryProcessor.class).to(RetryProcessorImpl.class).in(Singleton.class);

}
 
Example #28
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 30_000)
public void testLowWatermarkIsPersisted() throws Exception {

    TSOServerConfig tsoConfig = new TSOServerConfig();
    lowWatermarkWriter = new LowWatermarkWriterImpl(tsoConfig, commitTable, metrics);

    PersistenceProcessorHandler[] handlers = new PersistenceProcessorHandler[tsoConfig.getNumConcurrentCTWriters()];
    for (int i = 0; i < tsoConfig.getNumConcurrentCTWriters(); i++) {
        handlers[i] = new PersistenceProcessorHandler(metrics,
                                                      "localhost:1234",
                                                      mock(LeaseManager.class),
                                                      commitTable,
                                                      mock(ReplyProcessor.class),
                                                      retryProcessor,
                                                      panicker);
    }

    // Component under test
    PersistenceProcessorImpl persistenceProcessor =
            new PersistenceProcessorImpl(tsoConfig,
                                         new BlockingWaitStrategy(),
                                         commitTable,
                                         mock(ObjectPool.class),
                                         panicker,
                                         handlers,
                                         metrics);

    lowWatermarkWriter.persistLowWatermark(ANY_LWM).get();

    ArgumentCaptor<Long> lwmCapture = ArgumentCaptor.forClass(Long.class);
    CommitTable.Writer lwmWriter = commitTable.getWriter();
    verify(lwmWriter, timeout(100).times(1)).updateLowWatermark(lwmCapture.capture());
    assertEquals(lwmCapture.getValue().longValue(), ANY_LWM);

}
 
Example #29
Source File: TestPersistenceProcessor.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 30_000)
public void testCommitTableExceptionOnCommitPersistenceTakesDownDaemon() throws Exception {

    // Init lease management (doesn't matter if HA or not)
    LeaseManagement leaseManager = mock(LeaseManagement.class);

    TSOServerConfig config = new TSOServerConfig();

    ObjectPool<Batch> batchPool = spy(new BatchPoolModule(config).getBatchPool());

    ReplyProcessor replyProcessor = new ReplyProcessorImpl(new BlockingWaitStrategy(), metrics, panicker, batchPool, lowWatermarkWriter);

    PersistenceProcessorHandler[] handlers = new PersistenceProcessorHandler[config.getNumConcurrentCTWriters()];
    for (int i = 0; i < config.getNumConcurrentCTWriters(); i++) {
        handlers[i] = new PersistenceProcessorHandler(metrics,
                                                      "localhost:1234",
                                                      leaseManager,
                                                      commitTable,
                                                      replyProcessor,
                                                      mock(RetryProcessor.class),
                                                      panicker);
    }

    PersistenceProcessorImpl proc = new PersistenceProcessorImpl(config, new BlockingWaitStrategy(), commitTable, batchPool,
                                                                 panicker, handlers, metrics);

    MonitoringContextImpl monCtx = new MonitoringContextImpl(metrics);

    // Configure lease manager to work normally
    doReturn(true).when(leaseManager).stillInLeasePeriod();

    // Configure commit table writer to explode when flushing changes to DB
    doThrow(new IOException("Unable to write@TestPersistenceProcessor2")).when(mockWriter).flush();

    // Check the panic is extended!
    proc.addCommitToBatch(ANY_ST, ANY_CT, mock(Channel.class), monCtx, Optional.<Long>absent());
    proc.triggerCurrentBatchFlush();
    verify(panicker, timeout(1000).atLeastOnce()).panic(anyString(), any(Throwable.class));

}
 
Example #30
Source File: LogicProcessor.java    From Okra with Apache License 2.0 4 votes vote down vote up
public LogicProcessor() {
    this(DEFAULT, new ConcurrentHandler(), DEFAULT_SIZE, DEFAULT_POOL, SINGLE, new BlockingWaitStrategy());
}