private static void smarterPublish(Disruptor<Event> disruptor, AtomicBoolean running) { final RingBuffer<Event> ringBuffer = disruptor.getRingBuffer(); boolean publishOk; do { publishOk = ringBuffer.tryPublishEvent(new EventTranslator<Event>() { @Override public void translateTo(Event event, long sequence) { event.value = sequence; } }); } while (publishOk && running.get()); } }
@Test public void shouldTryPublishEvent() throws Exception { ringBuffer.addGatingSequences(new Sequence()); for (int i = 0; i < BUFFER_SIZE; i++) { assertThat(ringBuffer.tryPublishEvent(this), is(true)); } for (int i = 0; i < BUFFER_SIZE; i++) { assertThat(Long.valueOf(ringBuffer.get(i).get()), is(Long.valueOf(i + 29L))); } assertThat(ringBuffer.tryPublishEvent(this), is(false)); }
@Test public void shouldPublishEventOneArg() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 4); EventTranslatorOneArg<Object[], String> translator = new OneArgEventTranslator(); ringBuffer.publishEvent(translator, "Foo"); ringBuffer.tryPublishEvent(translator, "Foo"); assertThat(ringBuffer, ringBufferWithEvents("Foo-0", "Foo-1")); }
@Test public void shouldPublishEventTwoArg() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 4); EventTranslatorTwoArg<Object[], String, String> translator = new TwoArgEventTranslator(); ringBuffer.publishEvent(translator, "Foo", "Bar"); ringBuffer.tryPublishEvent(translator, "Foo", "Bar"); assertThat(ringBuffer, ringBufferWithEvents("FooBar-0", "FooBar-1")); }
@Test public void shouldPublishEvent() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 4); final EventTranslator<Object[]> translator = new NoArgEventTranslator(); ringBuffer.publishEvent(translator); ringBuffer.tryPublishEvent(translator); assertThat(ringBuffer, ringBufferWithEvents(0L, 1L)); }
@Test public void shouldPublishEventThreeArg() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 4); EventTranslatorThreeArg<Object[], String, String, String> translator = new ThreeArgEventTranslator(); ringBuffer.publishEvent(translator, "Foo", "Bar", "Baz"); ringBuffer.tryPublishEvent(translator, "Foo", "Bar", "Baz"); assertThat(ringBuffer, ringBufferWithEvents("FooBarBaz-0", "FooBarBaz-1")); }
@Test public void shouldPublishEventVarArg() throws Exception { RingBuffer<Object[]> ringBuffer = RingBuffer.createSingleProducer(new ArrayFactory(1), 4); EventTranslatorVararg<Object[]> translator = new VarArgEventTranslator(); ringBuffer.publishEvent(translator, "Foo", "Bar", "Baz", "Bam"); ringBuffer.tryPublishEvent(translator, "Foo", "Bar", "Baz", "Bam"); assertThat(ringBuffer, ringBufferWithEvents("FooBarBazBam-0", "FooBarBazBam-1")); }
@Test public void shouldPreventWrapping() throws Exception { Sequence sequence = new Sequence(Sequencer.INITIAL_CURSOR_VALUE); final RingBuffer<StubEvent> ringBuffer = createMultiProducer(StubEvent.EVENT_FACTORY, 4); ringBuffer.addGatingSequences(sequence); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 0, "0"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 1, "1"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 2, "2"); ringBuffer.publishEvent(StubEvent.TRANSLATOR, 3, "3"); assertFalse(ringBuffer.tryPublishEvent(StubEvent.TRANSLATOR, 3, "3")); }
/** * Attempts to publish an event by translating (write) data representations into events claimed from the RingBuffer. * @param translator * @return */ public boolean tryPublish(final EventTranslator<RingBufferEvent> translator) { if(isClosed()){ return false; } return disruptor.getRingBuffer().tryPublishEvent(translator); }
protected void dispatchInvoke(final Message message) { boolean success = ringBuffer.tryPublishEvent(new EventTranslator<Message.Invocation>() { @Override public void translateTo(Message.Invocation invocation, long sequence) { invocation.setMessage(message); } }); if (!success) { // looks like we are overloaded, let's cancel this request (connection) and drop warn in the log logger.warn(this + " ring buffer is full, dropping client message."); cancelMessage(message); } }
private void pushToRingBuffer(List<Sample> samples, EventTranslatorOneArg<SampleBatchEvent, List<Sample>> translator) { // Add the samples to the ring buffer if (!m_ringBuffer.tryPublishEvent(translator, samples)) { RATE_LIMITED_LOGGER.error("The ring buffer is full. {} samples associated with resource ids {} will be dropped.", samples.size(), new Object() { @Override public String toString() { // We wrap this in a toString() method to avoid build the string // unless the log message is actually printed return samples.stream() .map(s -> s.getResource().getId()) .distinct() .collect(Collectors.joining(", ")); } }); m_droppedSamples.mark(samples.size()); return; } // Increase our entry counter m_numEntriesOnRingBuffer.incrementAndGet(); }
private void pushToRingBuffer(List<Sample> samples, EventTranslatorOneArg<SampleBatchEvent, List<Sample>> translator) { // Add the samples to the ring buffer if (!m_ringBuffer.tryPublishEvent(translator, samples)) { RATE_LIMITED_LOGGER.error("The ring buffer is full. {} samples associated with resource ids {} will be dropped.", samples.size(), new Object() { @Override public String toString() { // We wrap this in a toString() method to avoid build the string // unless the log message is actually printed return samples.stream() .map(s -> s.getResource().getId()) .distinct() .collect(Collectors.joining(", ")); } }); m_droppedSamples.mark(samples.size()); return; } // Increase our entry counter m_numEntriesOnRingBuffer.incrementAndGet(); }
@Override public void tryPublish(LogEntry record) { if (ringBuffer.tryPublishEvent(TRANSLATOR, record)) { this.eventStat.incrementProducerSuccessCount(); } else { this.eventStat.incrementProducerFailCount(); } } }
@Override public boolean tryEnqueue(final LogEvent event, final AsyncLoggerConfig asyncLoggerConfig) { final LogEvent logEvent = prepareEvent(event); return disruptor.getRingBuffer().tryPublishEvent(translator, logEvent, asyncLoggerConfig); }
/** * Attempts to publish an event by translating (write) data representations into events claimed from the RingBuffer. * @param translator * @return */ public boolean tryPublish(final EventTranslator<RingBufferEvent> translator) { if(isClosed()){ return false; } return disruptor.getRingBuffer().tryPublishEvent(translator); }
/** * Attempts to publish an event by translating (write) data representations into events claimed from the RingBuffer. * @param translator * @return */ public boolean tryPublish(final EventTranslator<RingBufferEvent> translator) { if(isClosed()){ return false; } return disruptor.getRingBuffer().tryPublishEvent(translator); }
@Override public void tryPublish(HttpRequestRecord record) { if (ringBuffer.tryPublishEvent(TRANSLATOR, record)) { this.eventStat.incrementProducerSuccessCount(); } else { this.eventStat.incrementProducerFailCount(); } } }
public boolean tryPublish(final RingBufferLogEventTranslator translator) { // LOG4J2-639: catch NPE if disruptor field was set to null in stop() try { return disruptor.getRingBuffer().tryPublishEvent(translator); } catch (final NullPointerException npe) { LOGGER.warn("[{}] Ignoring log event after log4j was shut down: {} [{}] {}", contextName, translator.level, translator.loggerName, translator.message.getFormattedMessage() + (translator.thrown == null ? "" : Throwables.toStringList(translator.thrown))); return false; } }
private <E> boolean tryAddEventToRingBuffer(E event, EventTranslatorOneArg<ReportingEvent, E> eventTranslator) { if (dropTransactionIfQueueFull) { boolean queueFull = !disruptor.getRingBuffer().tryPublishEvent(eventTranslator, event); if (queueFull) { dropped.incrementAndGet(); return false; } } else { disruptor.getRingBuffer().publishEvent(eventTranslator, event); } return true; }
@Override public void close() throws Exception { isAlive = false; ringBuffer.tryPublishEvent((container, sequence) -> container.clear()); try { disruptor.shutdown(5, TimeUnit.SECONDS); } catch (TimeoutException e) { logger.warn("Disruptor shutdown timeout....", e); disruptor.halt(); } } }