org.jctools.queues.SpscArrayQueue Java Examples

The following examples show how to use org.jctools.queues.SpscArrayQueue. 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: DynamicConnectionSet.java    From mantis with Apache License 2.0 6 votes vote down vote up
public static <K, V> DynamicConnectionSet<MantisGroup<K, V>> createMGO(
        final ConnectToGroupedObservable.Builder<K, V> config, int maxTimeBeforeDisconnectSec, final SpscArrayQueue<MantisGroup<?, ?>> inputQueue) {
    Func3<Endpoint, Action0, PublishSubject<Integer>, RemoteRxConnection<MantisGroup<K, V>>> toObservableFunc
            = new Func3<Endpoint, Action0, PublishSubject<Integer>, RemoteRxConnection<MantisGroup<K, V>>>() {
        @Override
        public RemoteRxConnection<MantisGroup<K, V>> call(Endpoint endpoint, Action0 disconnectCallback,
                                                          PublishSubject<Integer> closeConnectionTrigger) {
            // copy config, change host, port and id
            ConnectToGroupedObservable.Builder<K, V> configCopy = new ConnectToGroupedObservable.Builder<K, V>(config);
            configCopy
                    .host(endpoint.getHost())
                    .port(endpoint.getPort())
                    .closeTrigger(closeConnectionTrigger)
                    .connectionDisconnectCallback(disconnectCallback)
                    .slotId(endpoint.getSlotId());
            return RemoteObservable.connectToMGO(configCopy.build(), inputQueue);
        }
    };
    return new DynamicConnectionSet<MantisGroup<K, V>>(toObservableFunc, MIN_TIME_SEC_DEFAULT, maxTimeBeforeDisconnectSec);
}
 
Example #2
Source File: JCToolsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenMultipleProducers_whenSpscQueueUsed_thenNoWarningOccurs() throws InterruptedException {
    SpscArrayQueue<Integer> queue = new SpscArrayQueue<Integer>(2);

    Thread producer1 = new Thread(() -> {
        queue.offer(1);
    });
    producer1.start();
    producer1.join();

    Thread producer2 = new Thread(() -> {
        queue.offer(2);
    });
    producer2.start();
    producer2.join();

    Set<Integer> fromQueue = new HashSet<>();
    Thread consumer = new Thread(() -> queue.drain(fromQueue::add));
    consumer.start();
    consumer.join();

    assertThat(fromQueue).containsOnly(1, 2);
}
 
Example #3
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object remove()
{
  final SpscArrayQueue<Object> queue = getQueue();
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    Object o = queue.remove();
    if (o != null) {
      notFull.signal();
    }
    return o;
  } finally {
    lock.unlock();
  }
}
 
Example #4
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Override
public void put(Object o) throws InterruptedException
{
  final SpscArrayQueue<Object> queue = getQueue();
  if (!queue.offer(o)) {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      while (!queue.offer(o)) {
        notFull.await();
      }
    } finally {
      lock.unlock();
    }
  }
}
 
Example #5
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@Override
public Tuple sweep()
{
  Object o;
  final ReentrantLock lock = this.lock;
  final SpscArrayQueue<Object> queue = getQueue();
  final Sink<Object> sink = getSink();
  lock.lock();
  try {
    while ((o = queue.peek()) != null) {
      if (o instanceof Tuple) {
        return (Tuple)o;
      }
      count++;
      sink.put(queue.poll());
      notFull.signal();
      if (lock.hasQueuedThreads()) {
        return null;
      }
    }
    return null;
  } finally {
    lock.unlock();
  }
}
 
Example #6
Source File: RemoteObservable.java    From mantis with Apache License 2.0 6 votes vote down vote up
public static <K, V> RemoteRxConnection<MantisGroup<K, V>> connectToMGO(
        final ConnectToGroupedObservable<K, V> config, final SpscArrayQueue<MantisGroup<?, ?>> inputQueue) {
    final RxMetrics metrics = new RxMetrics();
    return new RemoteRxConnection<MantisGroup<K, V>>(Observable.create(
            new OnSubscribe<MantisGroup<K, V>>() {
                @Override
                public void call(Subscriber<? super MantisGroup<K, V>> subscriber) {
                    RemoteUnsubscribe remoteUnsubscribe = new RemoteUnsubscribe(config.getName());
                    // wrapped in Observable.create() to inject unsubscribe callback
                    subscriber.add(remoteUnsubscribe); // unsubscribed callback
                    // create connection
                    createTcpConnectionToGOServer(config, remoteUnsubscribe, metrics,
                            config.getConnectionDisconnectCallback(),
                            config.getCloseTrigger(),
                            inputQueue)
                            .retryWhen(retryLogic(config))

                            .subscribe(subscriber);
                }
            }), metrics, config.getCloseTrigger());
}
 
Example #7
Source File: MonitoredQueue.java    From mantis with Apache License 2.0 6 votes vote down vote up
public MonitoredQueue(String name, int capacity, boolean useSpsc) {
    this.isSpsc = useSpsc;
    if (!useSpsc) {
        queue = new LinkedBlockingQueue<>(capacity);
    } else {
        queue = new SpscArrayQueue<>(capacity);
    }

    final String qId = Optional.ofNullable(name).orElse("none");
    final BasicTag idTag = new BasicTag(MantisMetricStringConstants.GROUP_ID_TAG, qId);
    final MetricGroupId metricGroup = new MetricGroupId("MonitoredQueue", idTag);

    Gauge queueDepth = new GaugeCallback(metricGroup, "queueDepth", () -> (double) queue.size());

    metrics = new Metrics.Builder()
            .id(metricGroup)
            .addCounter("numFailedToQueue")
            .addCounter("numSuccessQueued")
            .addGauge(queueDepth)
            .build();

    numSuccessEnqueu = metrics.getCounter("numSuccessQueued");
    numFailedEnqueu = metrics.getCounter("numFailedToQueue");
}
 
Example #8
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Object remove()
{
  final SpscArrayQueue<Object> queue = getQueue();
  final ReentrantLock lock = this.lock;
  lock.lock();
  try {
    Object o = queue.remove();
    if (o != null) {
      notFull.signal();
    }
    return o;
  } finally {
    lock.unlock();
  }
}
 
Example #9
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void put(Object o) throws InterruptedException
{
  final SpscArrayQueue<Object> queue = getQueue();
  if (!queue.offer(o)) {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      while (!queue.offer(o)) {
        notFull.await();
      }
    } finally {
      lock.unlock();
    }
  }
}
 
Example #10
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Tuple sweep()
{
  Object o;
  final ReentrantLock lock = this.lock;
  final SpscArrayQueue<Object> queue = getQueue();
  final Sink<Object> sink = getSink();
  lock.lock();
  try {
    while ((o = queue.peek()) != null) {
      if (o instanceof Tuple) {
        return (Tuple)o;
      }
      count++;
      sink.put(queue.poll());
      notFull.signal();
      if (lock.hasQueuedThreads()) {
        return null;
      }
    }
    return null;
  } finally {
    lock.unlock();
  }
}
 
Example #11
Source File: SpscArrayQueueSizeTest.java    From fastjgame with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    MessagePassingQueue<String> messageQueue = new SpscArrayQueue<>(8);

    new Producer(messageQueue).start();
    new Consumer(messageQueue).start();

    try {
        Thread.sleep(5 * 1000);
    } finally {
        stop = true;
    }
}
 
Example #12
Source File: PulsarRecordCursor.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void initialize(List<PulsarColumnHandle> columnHandles, PulsarSplit pulsarSplit, PulsarConnectorConfig
    pulsarConnectorConfig, ManagedLedgerFactory managedLedgerFactory, ManagedLedgerConfig managedLedgerConfig,
    PulsarConnectorMetricsTracker pulsarConnectorMetricsTracker) {
    this.columnHandles = columnHandles;
    this.pulsarSplit = pulsarSplit;
    this.partition = TopicName.getPartitionIndex(pulsarSplit.getTableName());
    this.pulsarConnectorConfig = pulsarConnectorConfig;
    this.maxBatchSize = pulsarConnectorConfig.getMaxEntryReadBatchSize();
    this.messageQueue = new SpscArrayQueue<>(pulsarConnectorConfig.getMaxSplitMessageQueueSize());
    this.entryQueue = new SpscArrayQueue<>(pulsarConnectorConfig.getMaxSplitEntryQueueSize());
    this.topicName = TopicName.get("persistent",
            NamespaceName.get(pulsarSplit.getSchemaName()),
            pulsarSplit.getTableName());
    this.metricsTracker = pulsarConnectorMetricsTracker;
    this.readOffloaded = pulsarConnectorConfig.getManagedLedgerOffloadDriver() != null;
    this.pulsarConnectorConfig = pulsarConnectorConfig;

    this.schemaHandler = PulsarSchemaHandlers
            .newPulsarSchemaHandler(this.topicName,
                    this.pulsarConnectorConfig, pulsarSplit.getSchemaInfo(), columnHandles);

    log.info("Initializing split with parameters: %s", pulsarSplit);

    try {
        this.cursor = getCursor(TopicName.get("persistent", NamespaceName.get(pulsarSplit.getSchemaName()),
            pulsarSplit.getTableName()), pulsarSplit.getStartPosition(), managedLedgerFactory, managedLedgerConfig);
    } catch (ManagedLedgerException | InterruptedException e) {
        log.error(e, "Failed to get read only cursor");
        close();
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple sweep()
{
  Object o;
  final SpscArrayQueue<Object> queue = this.queue;
  final Sink<Object> sink = getSink();
  while ((o = queue.peek()) != null) {
    if (o instanceof Tuple) {
      return (Tuple)o;
    }
    count++;
    sink.put(queue.poll());
  }
  return null;
}
 
Example #14
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void put(Object o) throws InterruptedException
{
  long spinMillis = 0;
  final SpscArrayQueue<Object> queue = this.queue;
  while (!queue.offer(o)) {
    sleep(spinMillis);
    spinMillis = Math.min(maxSpinMillis, spinMillis + 1);
  }
}
 
Example #15
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void put(Object o) throws InterruptedException
{
  long spinMillis = 0;
  final SpscArrayQueue<Object> queue = this.queue;
  while (!queue.offer(o)) {
    sleep(spinMillis);
    spinMillis = Math.min(maxSpinMillis, spinMillis + 1);
  }
}
 
Example #16
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple sweep()
{
  Object o;
  final SpscArrayQueue<Object> queue = this.queue;
  final Sink<Object> sink = getSink();
  while ((o = queue.peek()) != null) {
    if (o instanceof Tuple) {
      return (Tuple)o;
    }
    count++;
    sink.put(queue.poll());
  }
  return null;
}
 
Example #17
Source File: WriteOnlyClient.java    From Bats with Apache License 2.0 4 votes vote down vote up
public WriteOnlyClient(final ByteBuffer writeBuffer, final int sendQueueCapacity)
{
  this.writeBuffer = writeBuffer;
  sendQueue = new SpscArrayQueue<Slice>(sendQueueCapacity);
  freeQueue = new SpscArrayQueue<Slice>(sendQueueCapacity);
}
 
Example #18
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 4 votes vote down vote up
private SpscArrayQueueReservoir(final String id, final int capacity)
{
  super(id);
  queue = new SpscArrayQueue<>(capacity);
}
 
Example #19
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
private SpscArrayQueueReservoir(final String id, final int capacity)
{
  super(id);
  queue = new SpscArrayQueue<>(capacity);
}
 
Example #20
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
@Override
public int remainingCapacity()
{
  final SpscArrayQueue<Object> queue = this.queue;
  return queue.capacity() - queue.size();
}
 
Example #21
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
protected SpscArrayQueue<Object> getQueue()
{
  return queue;
}
 
Example #22
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected SpscArrayQueue<Object> getQueue()
{
  return queue;
}
 
Example #23
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public int remainingCapacity()
{
  final SpscArrayQueue<Object> queue = this.queue;
  return queue.capacity() - queue.size();
}