use of org.apache.gobblin.runtime.Task in project incubator-gobblin by apache.
the class Fork method consumeRecordStream.
@SuppressWarnings(value = "RV_RETURN_VALUE_IGNORED", justification = "We actually don't care about the return value of subscribe.")
public void consumeRecordStream(RecordStreamWithMetadata<D, S> stream) throws RecordStreamProcessor.StreamProcessingException {
if (this.converter instanceof MultiConverter) {
// if multiconverter, unpack it
for (Converter cverter : ((MultiConverter) this.converter).getConverters()) {
stream = cverter.processStream(stream, this.taskState);
}
} else {
stream = this.converter.processStream(stream, this.taskState);
}
stream = this.rowLevelPolicyChecker.processStream(stream, this.taskState);
stream = stream.mapStream(s -> s.map(r -> {
onEachRecord();
return r;
}));
stream = stream.mapStream(s -> s.doOnSubscribe(subscription -> onStart()));
stream = stream.mapStream(s -> s.doOnComplete(() -> verifyAndSetForkState(ForkState.RUNNING, ForkState.SUCCEEDED)));
stream = stream.mapStream(s -> s.doOnCancel(() -> verifyAndSetForkState(ForkState.RUNNING, ForkState.SUCCEEDED)));
stream = stream.mapStream(s -> s.doOnError(exc -> {
verifyAndSetForkState(ForkState.RUNNING, ForkState.FAILED);
this.logger.error(String.format("Fork %d of task %s failed to process data records", this.index, this.taskId), exc);
}));
stream = stream.mapStream(s -> s.doFinally(this::cleanup));
stream.getRecordStream().subscribe(r -> {
if (r instanceof RecordEnvelope) {
this.writer.get().writeEnvelope((RecordEnvelope) r);
} else if (r instanceof ControlMessage) {
this.writer.get().getMessageHandler().handleMessage((ControlMessage) r);
r.ack();
}
}, e -> logger.error("Failed to process record.", e), () -> {
if (this.writer.isPresent()) {
this.writer.get().close();
}
});
}
Aggregations