com.lmax.disruptor.EventTranslator Java Examples

The following examples show how to use com.lmax.disruptor.EventTranslator. 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: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/** 
 *                                            Consumer B1 -> Consumer C1
 *                                           /                          \
 * Publisher -> Ring buffer ---> Consumer A -                            -> Consumer D 
 *                                           \                          /
 *                                            Consumer B2 -> Consumer C2
 * 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_complicated_diamond_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerB2 consumerB2 = new ConsumerB2();
	ConsumerC1 consumerC1 = new ConsumerC1();
	ConsumerC2 consumerC2 = new ConsumerC2();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1, consumerB2});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1}, new EventHandler[]{consumerC1});
	EventHandlerChain<String> eventHandlerChain3 = new EventHandlerChain<String>(new EventHandler[]{consumerB2}, new EventHandler[]{consumerC2});
	EventHandlerChain<String> eventHandlerChain4 = new EventHandlerChain<String>(new EventHandler[]{consumerC1, consumerC2}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2, eventHandlerChain3, eventHandlerChain4});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #2
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/** 
 *                                            Consumer B1  
 *                                           /           \
 * Publisher -> Ring buffer ---> Consumer A -             -> Consumer D 
 *                                           \           /
 *                                            Consumer B2
 * 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_diamond_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerB2 consumerB2 = new ConsumerB2();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1, consumerB2});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1, consumerB2}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #3
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/**
 * Publisher -> Ring buffer ---> Consumer A -> Consumer B1 -> Consumer D 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_simple_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #4
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/**
 * Publisher -> Ring buffer ---> Consumer A 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_single_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #5
Source File: BaseDisruptorConfiguratorTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
@Before
public void setup(){
	disruptorConfigurator = new BaseDisruptorConfig() {

		@Override
		public void disruptorExceptionHandler() {}

		@Override
		public void disruptorEventHandler() {}

		@Override
		public void publish(EventTranslator eventTranslator) {}
	};
	
	disruptorConfigurator.setThreadName(THREAD_NAME);
	disruptorConfigurator.setProducerType(ProducerType.SINGLE);
	disruptorConfigurator.setRingBufferSize(ringBufferSize);
	disruptorConfigurator.setWaitStrategyType(WaitStrategyType.BLOCKING);
	disruptorConfigurator.setEventFactory(new SampleEventFactory());
	
	disruptorConfigurator.init();
}
 
Example #6
Source File: RingBuffer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
private void translateAndPublishBatch(final EventTranslator<E>[] translators, int batchStartsAt, final int batchSize, final long finalSequence) {
    final long initialSequence = finalSequence - (batchSize - 1);
    try {
        long sequence = initialSequence;
        final int batchEndsAt = batchStartsAt + batchSize;
        for (int i = batchStartsAt; i < batchEndsAt; i++) {
            final EventTranslator<E> translator = translators[i];
            translator.translateTo(get(sequence), sequence++);
        }
    } finally {
        sequencer.publish(initialSequence, finalSequence);
    }
}
 
Example #7
Source File: LogManagerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean tryOfferEvent(final StableClosure done, final EventTranslator<StableClosureEvent> translator) {
    if (this.stopped) {
        Utils.runClosureInThread(done, new Status(RaftError.ESTOP, "Log manager is stopped."));
        return true;
    }
    return this.diskQueue.tryPublishEvent(translator);
}
 
Example #8
Source File: RingBuffer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
private void translateAndPublish(EventTranslator<E> translator, long sequence) {
    try {
        translator.translateTo(get(sequence), sequence);
    } finally {
        sequencer.publish(sequence);
    }
}
 
Example #9
Source File: RingBuffer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to publish multiple events to the ring buffer. It handles claiming the next sequence, getting the current (uninitialised) event from the ring
 * buffer and publishing the claimed sequence after translation. Will return false if specified capacity was not available.
 * 
 * @param translators The user specified translation for the event
 * @param batchStartsAt The first element of the array which is within the batch.
 * @param batchSize The actual size of the batch
 * @return true if all the values were published, false if there was insufficient capacity.
 */
public boolean tryPublishEvents(EventTranslator<E>[] translators, int batchStartsAt, int batchSize) {
    checkBounds(translators, batchStartsAt, batchSize);
    try {
        final long finalSequence = sequencer.tryNext(batchSize);
        translateAndPublishBatch(translators, batchStartsAt, batchSize, finalSequence);
        return true;
    } catch (InsufficientCapacityException e) {
        return false;
    }
}
 
Example #10
Source File: RingBuffer.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to publish an event to the ring buffer. It handles claiming the next sequence, getting the current (uninitialised) event from the ring buffer
 * and publishing the claimed sequence after translation. Will return false if specified capacity was not available.
 * 
 * @param translator The user specified translation for the event
 * @return true if the value was published, false if there was insufficient capacity.
 */
public boolean tryPublishEvent(EventTranslator<E> translator) {
    try {
        final long sequence = sequencer.tryNext();
        translateAndPublish(translator, sequence);
        return true;
    } catch (InsufficientCapacityException e) {
        return false;
    }
}
 
Example #11
Source File: QueryLoggerDisruptor.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #12
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes ErrorEvent
 */
@Override
public void error(final SchedulerDriver driver, final String message) {
  disruptorManager.getErrorEventDisruptor().publishEvent(new EventTranslator<ErrorEvent>() {
    @Override
    public void translateTo(ErrorEvent event, long sequence) {
      event.setDriver(driver);
      event.setMessage(message);
    }
  });
}
 
Example #13
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes ExecutorLostEvent
 */
@Override
public void executorLost(final SchedulerDriver driver, final Protos.ExecutorID executorId, final Protos.SlaveID slaveId,
                         final int exitStatus) {
  disruptorManager.getExecutorLostEventDisruptor().publishEvent(new EventTranslator<ExecutorLostEvent>() {
    @Override
    public void translateTo(ExecutorLostEvent event, long sequence) {
      event.setDriver(driver);
      event.setExecutorId(executorId);
      event.setSlaveId(slaveId);
      event.setExitStatus(exitStatus);
    }
  });
}
 
Example #14
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes DisconnectedEvent
 */
@Override
public void disconnected(final SchedulerDriver driver) {
  disruptorManager.getDisconnectedEventDisruptor().publishEvent(new EventTranslator<DisconnectedEvent>() {
    @Override
    public void translateTo(DisconnectedEvent event, long sequence) {
      event.setDriver(driver);
    }
  });
}
 
Example #15
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean enqueueTask(final EventTranslator<ApplyTask> tpl) {
    if (this.shutdownLatch != null) {
        // Shutting down
        LOG.warn("FSMCaller is stopped, can not apply new task.");
        return false;
    }
    if (!this.taskQueue.tryPublishEvent(tpl)) {
        setError(new RaftException(ErrorType.ERROR_TYPE_STATE_MACHINE, new Status(RaftError.EBUSY,
            "FSMCaller is overload.")));
        return false;
    }
    return true;
}
 
Example #16
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Task task) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down."));
        throw new IllegalStateException("Node is shutting down");
    }
    Requires.requireNonNull(task, "Null task");

    final LogEntry entry = new LogEntry();
    entry.setData(task.getData());
    int retryTimes = 0;
    try {
        final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> {
            event.reset();
            event.done = task.getDone();
            event.entry = entry;
            event.expectedTerm = task.getExpectedTerm();
        };
        while (true) {
            if (this.applyQueue.tryPublishEvent(translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > MAX_APPLY_RETRY_TIMES) {
                    Utils.runClosureInThread(task.getDone(),
                        new Status(RaftError.EBUSY, "Node is busy, has too many tasks."));
                    LOG.warn("Node {} applyQueue is overload.", getNodeId());
                    this.metrics.recordTimes("apply-task-overload-times", 1);
                    return;
                }
                ThreadHelper.onSpinWait();
            }
        }

    } catch (final Exception e) {
        LOG.error("Fail to apply task.", e);
        Utils.runClosureInThread(task.getDone(), new Status(RaftError.EPERM, "Node is down."));
    }
}
 
Example #17
Source File: ReadOnlyServiceImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
        throw new IllegalStateException("Service already shutdown.");
    }
    try {
        EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
            event.done = closure;
            event.requestContext = new Bytes(reqCtx);
            event.startTime = Utils.monotonicMs();
        };
        int retryTimes = 0;
        while (true) {
            if (this.readIndexQueue.tryPublishEvent(translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > MAX_ADD_REQUEST_RETRY_TIMES) {
                    Utils.runClosureInThread(closure,
                        new Status(RaftError.EBUSY, "Node is busy, has too many read-only requests."));
                    this.nodeMetrics.recordTimes("read-index-overload-times", 1);
                    LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
                    return;
                }
                ThreadHelper.onSpinWait();
            }
        }
    } catch (final Exception e) {
        Utils.runClosureInThread(closure, new Status(RaftError.EPERM, "Node is down."));
    }
}
 
Example #18
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a RegisteredEvent
 */
@Override
public void registered(final SchedulerDriver driver, final Protos.FrameworkID frameworkId, final Protos.MasterInfo masterInfo) {
  disruptorManager.getRegisteredEventDisruptor().publishEvent(new EventTranslator<RegisteredEvent>() {
    @Override
    public void translateTo(RegisteredEvent event, long sequence) {
      event.setDriver(driver);
      event.setFrameworkId(frameworkId);
      event.setMasterInfo(masterInfo);
    }
  });
}
 
Example #19
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a ReRegisteredEvent
 */
@Override
public void reregistered(final SchedulerDriver driver, final Protos.MasterInfo masterInfo) {
  disruptorManager.getReRegisteredEventDisruptor().publishEvent(new EventTranslator<ReRegisteredEvent>() {
    @Override
    public void translateTo(ReRegisteredEvent event, long sequence) {
      event.setDriver(driver);
      event.setMasterInfo(masterInfo);
    }
  });
}
 
Example #20
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a ResourceOffersEvent
 */
@Override
public void resourceOffers(final SchedulerDriver driver, final List<Protos.Offer> offers) {
  disruptorManager.getResourceOffersEventDisruptor().publishEvent(new EventTranslator<ResourceOffersEvent>() {
    @Override
    public void translateTo(ResourceOffersEvent event, long sequence) {
      event.setDriver(driver);
      event.setOffers(offers);
    }
  });
}
 
Example #21
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a StatusUpdateEvent
 */
@Override
public void statusUpdate(final SchedulerDriver driver, final Protos.TaskStatus status) {
  disruptorManager.getStatusUpdateEventDisruptor().publishEvent(new EventTranslator<StatusUpdateEvent>() {
    @Override
    public void translateTo(StatusUpdateEvent event, long sequence) {
      event.setDriver(driver);
      event.setStatus(status);
    }
  });
}
 
Example #22
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes SlaveLostEvent
 */
@Override
public void slaveLost(final SchedulerDriver driver, final Protos.SlaveID slaveId) {
  disruptorManager.getSlaveLostEventDisruptor().publishEvent(new EventTranslator<SlaveLostEvent>() {
    @Override
    public void translateTo(SlaveLostEvent event, long sequence) {
      event.setDriver(driver);
      event.setSlaveId(slaveId);
    }
  });
}
 
Example #23
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes FrameworkMessageEvent
 */
@Override
public void frameworkMessage(final SchedulerDriver driver, final Protos.ExecutorID executorId, final Protos.SlaveID slaveId,
                             final byte[] bytes) {
  disruptorManager.getFrameworkMessageEventDisruptor().publishEvent(new EventTranslator<FrameworkMessageEvent>() {
    @Override
    public void translateTo(FrameworkMessageEvent event, long sequence) {
      event.setDriver(driver);
      event.setBytes(bytes);
      event.setExecutorId(executorId);
      event.setSlaveId(slaveId);
    }
  });
}
 
Example #24
Source File: MyriadScheduler.java    From incubator-myriad with Apache License 2.0 5 votes vote down vote up
/**
 * Publishes a OfferRescindedEvent
 */
@Override
public void offerRescinded(final SchedulerDriver driver, final Protos.OfferID offerId) {
  disruptorManager.getOfferRescindedEventDisruptor().publishEvent(new EventTranslator<OfferRescindedEvent>() {
    @Override
    public void translateTo(OfferRescindedEvent event, long sequence) {
      event.setDriver(driver);
      event.setOfferId(offerId);
    }
  });
}
 
Example #25
Source File: CollectingResponseEventSink.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
@Override
public void publishEvent(EventTranslator<ResponseEvent> translator) {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: BaseDisruptorConfig.java    From disruptor-spring-manager with MIT License 4 votes vote down vote up
@Override
public abstract void publish(EventTranslator<T> eventTranslator);
 
Example #27
Source File: RingBuffer.java    From jstorm with Apache License 2.0 4 votes vote down vote up
private void checkBounds(final EventTranslator<E>[] translators, final int batchStartsAt, final int batchSize) {
    checkBatchSizing(batchStartsAt, batchSize);
    batchOverRuns(translators, batchStartsAt, batchSize);
}
 
Example #28
Source File: DefaultDisruptorConfig.java    From disruptor-spring-manager with MIT License 4 votes vote down vote up
@Override
public void publish(EventTranslator<T> eventTranslator){
	getRingBuffer().publishEvent(eventTranslator);
	LOG.debug("Published " + eventTranslator.getClass().getSimpleName()  +" event to sequence: " + getCurrentLocation());
}
 
Example #29
Source File: DisruptorAsyncPublisher.java    From james with Apache License 2.0 4 votes vote down vote up
private EventTranslator<JobEvent> translateEvent(Event evt) {
    return (
        JobEvent event,
        long sequence) -> event.setJob(publishToDelegate(evt));
}
 
Example #30
Source File: CollectingResponseEventSink.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
@Override
public boolean tryPublishEvents(EventTranslator<ResponseEvent>[] translators, int batchStartsAt, int batchSize) {
    throw new UnsupportedOperationException();
}