Search in sources :

Example 1 with WrapEvent

use of loghub.processors.WrapEvent in project LogHub by fbacchella.

the class EventsProcessor method run.

@Override
public void run() {
    final AtomicReference<Counter> gaugecounter = new AtomicReference<>();
    while (true) {
        Event event = null;
        try {
            event = inQueue.take();
        } catch (InterruptedException e) {
            break;
        }
        {
            // Needed because eventtemp must be final
            final Event eventtemp = event;
            event.doMetric(() -> {
                gaugecounter.set(Properties.metrics.counter("Pipeline." + eventtemp.getCurrentPipeline() + ".inflight"));
                gaugecounter.get().inc();
            });
        }
        try (Context cw = event.isTest() ? null : Properties.metrics.timer("Pipeline." + event.getCurrentPipeline() + ".timer").time()) {
            {
                // Needed because eventtemp must be final
                final Event eventtemp = event;
                logger.trace("received {} in {}", () -> eventtemp, () -> eventtemp.getCurrentPipeline());
            }
            Processor processor = event.next();
            while (processor != null) {
                logger.trace("processing with {}", processor);
                if (processor instanceof WrapEvent) {
                    event = new EventWrapper(event, processor.getPathArray());
                } else if (processor instanceof UnwrapEvent) {
                    event = event.unwrap();
                } else {
                    ProcessingStatus processingstatus = process(event, processor);
                    if (processingstatus != ProcessingStatus.SUCCESS) {
                        event.doMetric(() -> {
                            gaugecounter.get().dec();
                            gaugecounter.set(null);
                        });
                        // Processing status was non null, so the event will not be processed any more
                        // But it's needed to check why.
                        String currentPipeline = event.getCurrentPipeline();
                        switch(processingstatus) {
                            case DROPED:
                                {
                                    // It was a drop action
                                    logger.debug("dropped event {}", event);
                                    event.doMetric(() -> {
                                        Properties.metrics.meter("Allevents.dropped");
                                        Properties.metrics.meter("Pipeline." + currentPipeline + ".dropped").mark();
                                    });
                                    event.drop();
                                    break;
                                }
                            case FAILED:
                                {
                                    // Processing failed critically (with an exception) and no recovery was attempted
                                    logger.debug("Failed event {}", event);
                                    event.doMetric(() -> {
                                        Properties.metrics.meter("Allevents.failed");
                                        Properties.metrics.meter("Pipeline." + currentPipeline + ".failed").mark();
                                    });
                                    event.end();
                                    break;
                                }
                            case PAUSED:
                                // It's simply a paused event, nothing to worry
                                break;
                            case SUCCESS:
                                // Unreachable code
                                break;
                        }
                        // It was not a success, end the processing.
                        event = null;
                        break;
                    }
                }
                processor = event.next();
                // If next processor is null, refill the event
                while (processor == null && event.getNextPipeline() != null) {
                    // Send to another pipeline, loop in the main processing queue
                    Pipeline next = namedPipelines.get(event.getNextPipeline());
                    event.refill(next);
                    processor = event.next();
                }
            }
            // Detect if will send to another pipeline, or just wait for a sender to take it
            if (event != null) {
                event.doMetric(() -> {
                    gaugecounter.get().dec();
                    gaugecounter.set(null);
                });
                if (event.isTest()) {
                    // A test event, it will not be send an output queue
                    // Checked after pipeline forwarding, but before output sending
                    TestEventProcessing.log(event);
                    event.end();
                } else if (event.getCurrentPipeline() != null && outQueues.containsKey(event.getCurrentPipeline())) {
                    // Put in the output queue, where the wanting output will come to take it
                    try {
                        outQueues.get(event.getCurrentPipeline()).put(event);
                    } catch (InterruptedException e) {
                        Stats.dropped.incrementAndGet();
                        event.end();
                        Thread.currentThread().interrupt();
                    }
                } else {
                    logger.error("Miss-configured event droped: {}", event);
                    Properties.metrics.meter("Allevents.failed");
                    event.end();
                }
                event = null;
            }
        }
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) FuturProcessor(loghub.processors.FuturProcessor) UnwrapEvent(loghub.processors.UnwrapEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Counter(com.codahale.metrics.Counter) WrapEvent(loghub.processors.WrapEvent) UnwrapEvent(loghub.processors.UnwrapEvent) WrapEvent(loghub.processors.WrapEvent)

Aggregations

Counter (com.codahale.metrics.Counter)1 Context (com.codahale.metrics.Timer.Context)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FuturProcessor (loghub.processors.FuturProcessor)1 UnwrapEvent (loghub.processors.UnwrapEvent)1 WrapEvent (loghub.processors.WrapEvent)1