@Override
  protected long runDisruptorPass() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    fizzBuzzHandler.reset(latch, batchProcessorFizzBuzz.getSequence().get() + ITERATIONS);

    executor.submit(batchProcessorFizz);
    executor.submit(batchProcessorBuzz);
    executor.submit(batchProcessorFizzBuzz);

    long start = System.currentTimeMillis();

    for (long i = 0; i < ITERATIONS; i++) {
      long sequence = ringBuffer.next();
      ringBuffer.get(sequence).setValue(i);
      ringBuffer.publish(sequence);
    }

    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);

    batchProcessorFizz.halt();
    batchProcessorBuzz.halt();
    batchProcessorFizzBuzz.halt();

    Assert.assertEquals(expectedResult, fizzBuzzHandler.getFizzBuzzCounter());

    return opsPerSecond;
  }
예제 #2
0
 private void consumeBatchToCursor(long cursor, EventHandler<Object> handler) {
   for (long curr = _consumer.get() + 1; curr <= cursor; curr++) {
     try {
       MutableObject mo = _buffer.get(curr);
       Object o = mo.o;
       mo.setObject(null);
       if (o == FLUSH_CACHE) {
         Object c = null;
         while (true) {
           c = _cache.poll();
           if (c == null) break;
           else handler.onEvent(c, curr, true);
         }
       } else if (o == INTERRUPT) {
         throw new InterruptedException("Disruptor processing interrupted");
       } else {
         handler.onEvent(o, curr, curr == cursor);
       }
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
   // TODO: only set this if the consumer cursor has changed?
   _consumer.set(cursor);
 }
 public void onData(ByteBuffer bb) {
   long sequence = ringBuffer.next(); // Grab the next sequence
   try {
     LongEvent event = ringBuffer.get(sequence); // Get the entry in the Disruptor for the sequence
     event.set(bb.getLong(0)); // Fill with data
   } finally {
     ringBuffer.publish(sequence);
   }
 }
예제 #4
0
 public void pushData(ByteBuffer bb) {
   long sequence = ringBuffer.next();
   try {
     PCData event = ringBuffer.get(sequence);
     event.setData(bb.getLong(0));
   } finally {
     ringBuffer.publish(sequence);
   }
 }
예제 #5
0
파일: Producer.java 프로젝트: brucelau/git
 public void run() {
   // Publishers claim events in sequence
   long sequence = ringBuffer.next();
   ValueEvent event = ringBuffer.get(sequence);
   for (int i = 0; i < 3000; i++) {
     event.setValue(i); // this could be more complex with multiple fields
     // make the event available to EventProcessors
     ringBuffer.publish(sequence);
   }
 }
 public void put(Object member, List<Record> records) throws AnalyticsException {
   RingBuffer<RecordsHolder> buffer = this.getRingBuffer(member);
   long sequence = buffer.next();
   try {
     RecordsHolder event = buffer.get(sequence);
     event.setRecords(records);
   } finally {
     buffer.publish(sequence);
   }
 }
 private void prefillArgumentHolderObjects(
     final RingBuffer<ProxyMethodInvocation> ringBuffer,
     final ArgumentHolderGenerator argumentHolderGenerator) {
   final int bufferSize = ringBuffer.getBufferSize();
   for (int i = 0; i < bufferSize; i++) {
     ringBuffer
         .get(i)
         .setArgumentHolder(
             (Resetable) instantiate(argumentHolderGenerator.getGeneratedClass(), new Class[] {}));
   }
 }
예제 #8
0
 private void publishDirect(Object obj, boolean block) throws InsufficientCapacityException {
   final long id;
   if (block) {
     id = _buffer.next();
   } else {
     id = _buffer.tryNext(1);
   }
   final MutableObject m = _buffer.get(id);
   m.setObject(obj);
   _buffer.publish(id);
 }
예제 #9
0
    private boolean send(RingBuffer<byte[]> ringBuffer) {

      if (ringBuffer.hasAvailableCapacity(1)) {
        long next = ringBuffer.next();
        byte[] event = ringBuffer.get(next);
        System.arraycopy(input, 0, event, 0, event.length);
        ringBuffer.publish(next);
        return true;
      } else {
        errorCount++;
        return false;
      }
    }
  @Override
  public void run() {
    try {
      cyclicBarrier.await();

      for (long i = 0; i < iterations; i++) {
        long sequence = ringBuffer.next();
        ValueEvent event = ringBuffer.get(sequence);
        event.setValue(i);
        ringBuffer.publish(sequence);
      }
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
예제 #11
0
 public void publish(Object obj, boolean block) throws InsufficientCapacityException {
   if (consumerStartedFlag) {
     final long id;
     if (block) {
       id = _buffer.next();
     } else {
       id = _buffer.tryNext(1);
     }
     final MutableObject m = _buffer.get(id);
     m.setObject(obj);
     _buffer.publish(id);
   } else {
     _cache.add(obj);
     if (consumerStartedFlag) flushCache();
   }
 }
예제 #12
0
  public void engage() {
    // starts WorkerPool workers in separate thread(s)
    RingBuffer<DemoEvent> ringBuf = workerPool.start(execService);

    // publish lots of events
    for (int i = 0; i < 5 * NUM_WORKERS; i++) {
      long seq = ringBuf.next();
      ringBuf.get(seq).setProcessId(i);
      ringBuf.publish(seq);
      // try it with and without the sleep
      // try { Thread.sleep(33); } catch (Exception e) { }
    }

    // wait until all published events are processed, then stop the workers
    workerPool.drainAndHalt();
  }
    @SuppressWarnings("unchecked")
    public void run() {
      try {
        if (cyclicBarrier != null) cyclicBarrier.await();

        for (int i = 0; i < howmany; i++) {
          long seq = ringBuf.next();
          DemoEvent ev = ringBuf.get(seq);
          // each event will have the pubcount in processId
          // and the name of the publisher it came from in the Name field
          ev.setProcessId(i);
          ev.setName("Published by DemoPublisher #" + id);
          System.out.printf("Published by DemoPublisher #%d; procId: %d; slot: %d\n", id, i, seq);
          ringBuf.publish(seq);
          if (should_pause_b)
            try {
              Thread.sleep(8);
            } catch (Exception e) {
            }
        }
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }