use of loghub.processors.FuturProcessor in project LogHub by fbacchella.
the class EventsProcessor method process.
ProcessingStatus process(Event e, Processor p) {
ProcessingStatus status = null;
if (p instanceof Forker) {
((Forker) p).fork(e);
status = ProcessingStatus.SUCCESS;
} else if (p instanceof Forwarder) {
((Forwarder) p).forward(e);
status = ProcessingStatus.SUCCESS;
} else if (p instanceof Drop) {
status = ProcessingStatus.DROPED;
} else if (e.stepsCount() > maxSteps) {
logger.error("Too much steps for event {}, dropping", e);
status = ProcessingStatus.FAILED;
} else {
try {
boolean success = false;
if (p.isprocessNeeded(e)) {
success = e.process(p);
}
// After processing, check the failures and success processors
Processor failureProcessor = p.getFailure();
Processor successProcessor = p.getSuccess();
if (success && successProcessor != null) {
e.insertProcessor(successProcessor);
} else if (!success && failureProcessor != null) {
e.insertProcessor(failureProcessor);
}
status = ProcessingStatus.SUCCESS;
} catch (ProcessorException.PausedEventException ex) {
// The event to pause might be a transformation of the original event.
Event topause = ex.getEvent();
// First check if the process will be able to manage the call back
if (p instanceof AsyncProcessor) {
AsyncProcessor<?> ap = (AsyncProcessor<?>) p;
// A paused event was catch, create a custom FuturProcess for it that will be awaken when event come back
Future<?> future = ex.getFuture();
PausedEvent<Future<?>> paused = new PausedEvent<>(topause, future);
paused = paused.onSuccess(p.getSuccess()).onFailure(p.getFailure()).onException(p.getException()).setTimeout(ap.getTimeout(), TimeUnit.SECONDS);
// Create the processor that will process the call back processor
@SuppressWarnings({ "rawtypes", "unchecked" }) FuturProcessor<?> pauser = new FuturProcessor(future, paused, ap);
ex.getEvent().insertProcessor(pauser);
// Store the callback informations
future.addListener((i) -> {
inQueue.put(topause);
evrepo.cancel(future);
});
evrepo.pause(paused);
status = ProcessingStatus.PAUSED;
} else {
Properties.metrics.counter("Pipeline." + e.getCurrentPipeline() + ".exception").inc();
Exception cce = new ClassCastException("A not AsyncProcessor throws a asynchronous operation");
Stats.newException(cce);
logger.error("A not AsyncProcessor {} throws a asynchronous operation", p);
logger.throwing(Level.DEBUG, cce);
status = ProcessingStatus.FAILED;
}
} catch (ProcessorException.DroppedEventException ex) {
status = ProcessingStatus.DROPED;
} catch (IgnoredEventException ex) {
// A do nothing event
status = ProcessingStatus.SUCCESS;
} catch (ProcessorException | UncheckedProcessingException ex) {
logger.debug("got a processing exception");
logger.catching(Level.DEBUG, ex);
e.doMetric(() -> {
Stats.newError(ex);
});
Processor exceptionProcessor = p.getException();
if (exceptionProcessor != null) {
e.insertProcessor(exceptionProcessor);
status = ProcessingStatus.SUCCESS;
} else {
status = ProcessingStatus.FAILED;
}
} catch (Exception ex) {
e.doMetric(() -> {
Properties.metrics.counter("Pipeline." + e.getCurrentPipeline() + ".exception").inc();
Stats.newException(ex);
});
String message = ex.getMessage();
if (message == null) {
message = ex.getClass().getCanonicalName();
}
logger.error("failed to transform event {} with unmanaged error: {}", e, message);
logger.throwing(Level.DEBUG, ex);
status = ProcessingStatus.FAILED;
}
}
return status;
}
Aggregations