com.lmax.disruptor.Sequence Java Examples

The following examples show how to use com.lmax.disruptor.Sequence. 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: BusySpinWaitStrategyFactory.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence,
                    final SequenceBarrier barrier) throws AlertException, InterruptedException {

    long availableSequence;
    int spinTries = 0;

    // dependentSequence 该项目组织架构中,其实只是生产者的sequence,也就是cursor
    while ((availableSequence = dependentSequence.get()) < sequence) {
        barrier.checkAlert();
        Thread.onSpinWait();

        if (++spinTries == loopOnceSpinTries) {
            spinTries = 0;
            eventLoop.safeLoopOnce();
        }
    }

    return availableSequence;
}
 
Example #2
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example #3
Source File: RingBufferWorkProcessor.java    From camunda-bpm-reactor with Apache License 2.0 6 votes vote down vote up
private boolean replay(final boolean unbounded) {
  Sequence replayedSequence;
  MutableSignal<T> signal;
  while ((replayedSequence = processor.cancelledSequences.poll()) != null) {
    signal = processor.ringBuffer.get(replayedSequence.get() + 1L);
    try {
      if (signal.value == null) {
        barrier.waitFor(replayedSequence.get() + 1L);
      }
      readNextEvent(signal, unbounded);
      RingBufferSubscriberUtils.routeOnce(signal, subscriber);
      processor.ringBuffer.removeGatingSequence(replayedSequence);
    } catch (TimeoutException | InterruptedException | AlertException | CancelException ce) {
      processor.ringBuffer.removeGatingSequence(sequence);
      processor.cancelledSequences.add(replayedSequence);
      return true;
    }
  }
  return false;
}
 
Example #4
Source File: RingBufferProcessor.java    From camunda-bpm-reactor with Apache License 2.0 6 votes vote down vote up
private RingBufferProcessor(String name,
                            ExecutorService executor,
                            int bufferSize,
                            WaitStrategy waitStrategy,
                            boolean shared,
                            boolean autoCancel) {
  super(name, executor, autoCancel);

  this.ringBuffer = RingBuffer.create(
    shared ? ProducerType.MULTI : ProducerType.SINGLE,
    new EventFactory<MutableSignal<E>>() {
      @Override
      public MutableSignal<E> newInstance() {
        return new MutableSignal<E>();
      }
    },
    bufferSize,
    waitStrategy
  );

  this.recentSequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE);
  this.barrier = ringBuffer.newBarrier();
  //ringBuffer.addGatingSequences(recentSequence);
}
 
Example #5
Source File: SleepingBlockingWaitStrategy.java    From ballerina-message-broker with Apache License 2.0 6 votes vote down vote up
public long waitFor(long sequence, Sequence cursorSequence, Sequence dependentSequence, SequenceBarrier barrier)
        throws AlertException, InterruptedException {
    if (cursorSequence.get() < sequence) {
        this.lock.lock();

        try {
            while (cursorSequence.get() < sequence) {
                barrier.checkAlert();
                this.processorNotifyCondition.await();
            }
        } finally {
            this.lock.unlock();
        }
    }

    long availableSequence;
    while ((availableSequence = dependentSequence.get()) < sequence) {
        barrier.checkAlert();
        LockSupport.parkNanos(1L);
    }

    return availableSequence;
}
 
Example #6
Source File: SleepWaitStrategyFactory.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence,
                    final SequenceBarrier barrier) throws AlertException {
    long availableSequence;
    int counter = retries;

    // dependentSequence 该项目组织架构中,其实只是生产者的sequence,也就是cursor
    while ((availableSequence = dependentSequence.get()) < sequence) {
        counter = applyWaitMethod(barrier, counter);

        // 每睡眠一次,执行一次循环
        if (counter <= 0) {
            eventLoop.safeLoopOnce();
        }
    }
    return availableSequence;
}
 
Example #7
Source File: LoggerServiceImpl.java    From gflogger with Apache License 2.0 5 votes vote down vote up
public long waitFor(
	long sequence,
	Sequence cursor,
	Sequence dependentSequence,
	SequenceBarrier barrier
) throws AlertException, InterruptedException, TimeoutException {
	long availableSequence;
	if ((availableSequence = cursor.get()) < sequence) {
		flush();
		synchronized (lock) {
			++numWaiters;
			while ((availableSequence = cursor.get()) < sequence) {
				if (state == State.STOPPED) {
					disruptor.halt();
					throw AlertException.INSTANCE;
				}
				barrier.checkAlert();
				//*/
				lock.wait();
				/*/
				Thread.sleep(1);
				//*/
			}
			--numWaiters;
		}
	}
	while ((availableSequence = dependentSequence.get()) < sequence) {
		barrier.checkAlert();
	}

	return availableSequence;
}
 
Example #8
Source File: MultiProducerSequencer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: SpinLoopHintBusySpinWaitStrategy.java    From perf-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier)
        throws AlertException, InterruptedException
{
    long availableSequence;

    while ((availableSequence = dependentSequence.get()) < sequence)
    {
        SpinHint.spinLoopHint();
        barrier.checkAlert();
    }

    return availableSequence;
}
 
Example #10
Source File: ParkWaitStrategy.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(long sequence,
                    Sequence cursor,
                    Sequence dependentSequence,
                    SequenceBarrier barrier) throws AlertException,
  InterruptedException,
  TimeoutException {
  long availableSequence;
  while ((availableSequence = dependentSequence.get()) < sequence) {
    barrier.checkAlert();
    LockSupport.parkNanos(parkFor);
  }
  return availableSequence;
}
 
Example #11
Source File: ExponentionallyIncreasingSleepingWaitStrategy.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public long waitFor(final long sequence, Sequence cursor, final Sequence dependentSequence, final SequenceBarrier barrier) throws AlertException {
    long availableSequence;
    int currentSleep = sleepTimeNsStart;

    while ((availableSequence = dependentSequence.get()) < sequence) {
        currentSleep = applyWaitMethod(barrier, currentSleep);
    }

    return availableSequence;
}
 
Example #12
Source File: RingBufferProcessor.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link com.lmax.disruptor.EventProcessor} that will automatically track the progress by updating
 * its
 * sequence
 */
public BatchSignalProcessor(RingBufferProcessor<T> processor,
                            Sequence pendingRequest,
                            Subscriber<? super T> subscriber) {
  this.processor = processor;
  this.pendingRequest = pendingRequest;
  this.subscriber = subscriber;
}
 
Example #13
Source File: RingBufferProcessor.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
public RingBufferSubscription(Sequence pendingRequest,
                              Subscriber<? super E> subscriber,
                              BatchSignalProcessor<E> eventProcessor) {
  this.subscriber = subscriber;
  this.eventProcessor = eventProcessor;
  this.pendingRequest = pendingRequest;
}
 
Example #14
Source File: SamplingProfiler.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
public SamplingProfiler(final ElasticApmTracer tracer, ScheduledExecutorService scheduler, NanoClock nanoClock, File activationEventsFile, File jfrFile) throws IOException {
    this.tracer = tracer;
    this.config = tracer.getConfig(ProfilingConfiguration.class);
    this.coreConfig = tracer.getConfig(CoreConfiguration.class);
    this.scheduler = scheduler;
    this.nanoClock = nanoClock;
    this.eventBuffer = createRingBuffer();
    this.sequence = new Sequence();
    // tells the ring buffer to not override slots which have not been read yet
    this.eventBuffer.addGatingSequences(sequence);
    this.poller = eventBuffer.newPoller();
    contextForLogging = TraceContext.with64BitId(tracer);
    this.callTreePool = ListBasedObjectPool.<CallTree>ofRecyclable(2 * 1024, new Allocator<CallTree>() {
        @Override
        public CallTree createInstance() {
            return new CallTree();
        }
    });
    // call tree roots are pooled so that fast activations/deactivations with no associated stack traces don't cause allocations
    this.rootPool = ListBasedObjectPool.<CallTree.Root>ofRecyclable(512, new Allocator<CallTree.Root>() {
        @Override
        public CallTree.Root createInstance() {
            return new CallTree.Root(tracer);
        }
    });
    this.jfrFile = jfrFile;
    activationEventsBuffer = ByteBuffer.allocateDirect(ACTIVATION_EVENTS_BUFFER_SIZE);
    activationEventsFileChannel = FileChannel.open(activationEventsFile.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
    if (activationEventsFileChannel.size() == 0) {
        preAllocate(activationEventsFileChannel, PRE_ALLOCATE_ACTIVATION_EVENTS_FILE_MB);
    }
}
 
Example #15
Source File: QueryLogDetailsEventHandler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public void setSequenceCallback(final Sequence sequenceCallback) {
    this.sequenceCallback = sequenceCallback;
}
 
Example #16
Source File: RingBufferLogEventHandler.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void setSequenceCallback(final Sequence sequenceCallback) {
    this.sequenceCallback = sequenceCallback;
}
 
Example #17
Source File: AsyncLoggerConfigDisruptor.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void setSequenceCallback(final Sequence sequenceCallback) {
    this.sequenceCallback = sequenceCallback;
}
 
Example #18
Source File: SamplingProfiler.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
public long waitFor(long sequence, Sequence cursor, Sequence dependentSequence, SequenceBarrier barrier) {
    return dependentSequence.get();
}
 
Example #19
Source File: MysqlMultiStageCoprocessor.java    From canal with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
    super.start();
    this.exception = null;
    this.disruptorMsgBuffer = RingBuffer.createSingleProducer(new MessageEventFactory(),
        ringBufferSize,
        new BlockingWaitStrategy());
    int tc = parserThreadCount > 0 ? parserThreadCount : 1;
    this.parserExecutor = Executors.newFixedThreadPool(tc, new NamedThreadFactory("MultiStageCoprocessor-Parser-"
                                                                                  + destination));

    this.stageExecutor = Executors.newFixedThreadPool(2, new NamedThreadFactory("MultiStageCoprocessor-other-"
                                                                                + destination));
    SequenceBarrier sequenceBarrier = disruptorMsgBuffer.newBarrier();
    ExceptionHandler exceptionHandler = new SimpleFatalExceptionHandler();
    // stage 2
    this.logContext = new LogContext();
    simpleParserStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
        sequenceBarrier,
        new SimpleParserStage(logContext));
    simpleParserStage.setExceptionHandler(exceptionHandler);
    disruptorMsgBuffer.addGatingSequences(simpleParserStage.getSequence());

    // stage 3
    SequenceBarrier dmlParserSequenceBarrier = disruptorMsgBuffer.newBarrier(simpleParserStage.getSequence());
    WorkHandler<MessageEvent>[] workHandlers = new DmlParserStage[tc];
    for (int i = 0; i < tc; i++) {
        workHandlers[i] = new DmlParserStage();
    }
    workerPool = new WorkerPool<MessageEvent>(disruptorMsgBuffer,
        dmlParserSequenceBarrier,
        exceptionHandler,
        workHandlers);
    Sequence[] sequence = workerPool.getWorkerSequences();
    disruptorMsgBuffer.addGatingSequences(sequence);

    // stage 4
    SequenceBarrier sinkSequenceBarrier = disruptorMsgBuffer.newBarrier(sequence);
    sinkStoreStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
        sinkSequenceBarrier,
        new SinkStoreStage());
    sinkStoreStage.setExceptionHandler(exceptionHandler);
    disruptorMsgBuffer.addGatingSequences(sinkStoreStage.getSequence());

    // start work
    stageExecutor.submit(simpleParserStage);
    stageExecutor.submit(sinkStoreStage);
    workerPool.start(parserExecutor);
}
 
Example #20
Source File: RingBufferProcessor.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
@Override
public Sequence getSequence() {
  return sequence;
}
 
Example #21
Source File: AsyncFSWAL.java    From hbase with Apache License 2.0 4 votes vote down vote up
public AsyncFSWAL(FileSystem fs, Path rootDir, String logDir, String archiveDir,
    Configuration conf, List<WALActionsListener> listeners, boolean failIfWALExists,
    String prefix, String suffix, EventLoopGroup eventLoopGroup,
    Class<? extends Channel> channelClass) throws FailedLogCloseException, IOException {
  super(fs, rootDir, logDir, archiveDir, conf, listeners, failIfWALExists, prefix, suffix);
  this.eventLoopGroup = eventLoopGroup;
  this.channelClass = channelClass;
  Supplier<Boolean> hasConsumerTask;
  if (conf.getBoolean(ASYNC_WAL_USE_SHARED_EVENT_LOOP, DEFAULT_ASYNC_WAL_USE_SHARED_EVENT_LOOP)) {
    this.consumeExecutor = eventLoopGroup.next();
    if (consumeExecutor instanceof SingleThreadEventExecutor) {
      try {
        Field field = SingleThreadEventExecutor.class.getDeclaredField("taskQueue");
        field.setAccessible(true);
        Queue<?> queue = (Queue<?>) field.get(consumeExecutor);
        hasConsumerTask = () -> queue.peek() == consumer;
      } catch (Exception e) {
        LOG.warn("Can not get task queue of " + consumeExecutor +
          ", this is not necessary, just give up", e);
        hasConsumerTask = () -> false;
      }
    } else {
      hasConsumerTask = () -> false;
    }
  } else {
    ThreadPoolExecutor threadPool =
      new ThreadPoolExecutor(1, 1, 0L,
        TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
          new ThreadFactoryBuilder().setNameFormat("AsyncFSWAL-%d-" + rootDir.toString()).
            setDaemon(true).build());
    hasConsumerTask = () -> threadPool.getQueue().peek() == consumer;
    this.consumeExecutor = threadPool;
  }

  this.hasConsumerTask = hasConsumerTask;
  int preallocatedEventCount =
    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);

  // inrease the ringbuffer sequence so our txid is start from 1
  waitingConsumePayloads.publish(waitingConsumePayloads.next());
  waitingConsumePayloadsGatingSequence.set(waitingConsumePayloads.getCursor());

  batchSize = conf.getLong(WAL_BATCH_SIZE, DEFAULT_WAL_BATCH_SIZE);
  waitOnShutdownInSeconds = conf.getInt(ASYNC_WAL_WAIT_ON_SHUTDOWN_IN_SECONDS,
    DEFAULT_ASYNC_WAL_WAIT_ON_SHUTDOWN_IN_SECONDS);
}
 
Example #22
Source File: RingBufferProcessor.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(final Subscriber<? super E> subscriber) {
  if (null == subscriber) {
    throw new NullPointerException("Cannot subscribe NULL subscriber");
  }

  try {
    //create a unique eventProcessor for this subscriber
    final Sequence pendingRequest = new Sequence(0);
    final BatchSignalProcessor<E> signalProcessor = new BatchSignalProcessor<E>(
      this,
      pendingRequest,
      subscriber
    );

    //bind eventProcessor sequence to observe the ringBuffer

    //if only active subscriber, replay missed data
    if (incrementSubscribers()) {
      ringBuffer.addGatingSequences(signalProcessor.getSequence());

      //set eventProcessor sequence to minimum index (replay)
      signalProcessor.getSequence().set(recentSequence.get());
    } else {
      //otherwise only listen to new data
      //set eventProcessor sequence to ringbuffer index
      signalProcessor.getSequence().set(ringBuffer.getCursor());
      signalProcessor.nextSequence = signalProcessor.getSequence().get();

      ringBuffer.addGatingSequences(signalProcessor.getSequence());
    }

    //prepare the subscriber subscription to this processor
    signalProcessor.setSubscription(new RingBufferSubscription(pendingRequest, subscriber, signalProcessor));

    //start the subscriber thread
    executor.execute(signalProcessor);

  } catch (Throwable t) {
    subscriber.onError(t);
  }
}
 
Example #23
Source File: AgileWaitingStrategy.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
@Override
public long waitFor(long sequence, Sequence cursor, Sequence dependentSequence, SequenceBarrier barrier)
  throws AlertException, InterruptedException, TimeoutException {
  return currentStrategy.waitFor(sequence, cursor, dependentSequence, barrier);
}
 
Example #24
Source File: RingBufferWorkProcessor.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
@Override
public Sequence getSequence() {
  return sequence;
}
 
Example #25
Source File: RingBufferSubscriberUtils.java    From camunda-bpm-reactor with Apache License 2.0 4 votes vote down vote up
public static <T> boolean waitRequestOrTerminalEvent(
  Sequence pendingRequest,
  RingBuffer<MutableSignal<T>> ringBuffer,
  SequenceBarrier barrier,
  Subscriber<? super T> subscriber,
  AtomicBoolean isRunning
) {
  final long waitedSequence = ringBuffer.getCursor() + 1L;
  try {
    MutableSignal<T> event = null;
    while (pendingRequest.get() < 0l) {
      //pause until first request
      if (event == null) {
        barrier.waitFor(waitedSequence);
        event = ringBuffer.get(waitedSequence);

        if (event.type == MutableSignal.Type.COMPLETE) {
          try {
            subscriber.onComplete();
            return false;
          } catch (Throwable t) {
            Exceptions.throwIfFatal(t);
            subscriber.onError(t);
            return false;
          }
        } else if (event.type == MutableSignal.Type.ERROR) {
          subscriber.onError(event.error);
          return false;
        }
      } else {
        barrier.checkAlert();
      }
      LockSupport.parkNanos(1l);
    }
  } catch (TimeoutException te) {
    //ignore
  } catch (AlertException ae) {
    if (!isRunning.get()) {
      return false;
    }
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
  }

  return true;
}
 
Example #26
Source File: RingBuffer.java    From jstorm with Apache License 2.0 2 votes vote down vote up
/**
 * Add the specified gating sequences to this instance of the Disruptor. They will safely and atomically added to the list of gating sequences.
 * 
 * @param gatingSequences The sequences to add.
 */
public void addGatingSequences(Sequence... gatingSequences) {
    sequencer.addGatingSequences(gatingSequences);
}
 
Example #27
Source File: RingBuffer.java    From jstorm with Apache License 2.0 2 votes vote down vote up
/**
 * Remove the specified sequence from this ringBuffer.
 * 
 * @param sequence to be removed.
 * @return <tt>true</tt> if this sequence was found, <tt>false</tt> otherwise.
 */
public boolean removeGatingSequence(Sequence sequence) {
    return sequencer.removeGatingSequence(sequence);
}
 
Example #28
Source File: RingBuffer.java    From jstorm with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new SequenceBarrier to be used by an EventProcessor to track which messages are available to be read from the ring buffer given a list of
 * sequences to track.
 * 
 * @see SequenceBarrier
 * @param sequencesToTrack the additional sequences to track
 * @return A sequence barrier that will track the specified sequences.
 */
public SequenceBarrier newBarrier(Sequence... sequencesToTrack) {
    return sequencer.newBarrier(sequencesToTrack);
}