Search in sources :

Example 11 with RecordEnvelope

use of org.apache.gobblin.stream.RecordEnvelope in project incubator-gobblin by apache.

the class Task method runSynchronousModel.

@Deprecated
private void runSynchronousModel() throws Exception {
    // Get the fork operator. By default IdentityForkOperator is used with a single branch.
    ForkOperator forkOperator = closer.register(this.taskContext.getForkOperator());
    forkOperator.init(this.taskState);
    int branches = forkOperator.getBranches(this.taskState);
    // Set fork.branches explicitly here so the rest task flow can pick it up
    this.taskState.setProp(ConfigurationKeys.FORK_BRANCHES_KEY, branches);
    // Extract, convert, and fork the source schema.
    Object schema = converter.convertSchema(extractor.getSchema(), this.taskState);
    List<Boolean> forkedSchemas = forkOperator.forkSchema(this.taskState, schema);
    if (forkedSchemas.size() != branches) {
        throw new ForkBranchMismatchException(String.format("Number of forked schemas [%d] is not equal to number of branches [%d]", forkedSchemas.size(), branches));
    }
    if (inMultipleBranches(forkedSchemas) && !(CopyHelper.isCopyable(schema))) {
        throw new CopyNotSupportedException(schema + " is not copyable");
    }
    RowLevelPolicyCheckResults rowResults = new RowLevelPolicyCheckResults();
    if (!areSingleBranchTasksSynchronous(this.taskContext) || branches > 1) {
        // Create one fork for each forked branch
        for (int i = 0; i < branches; i++) {
            if (forkedSchemas.get(i)) {
                AsynchronousFork fork = closer.register(new AsynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, i, this.taskMode));
                configureStreamingFork(fork, watermarkingStrategy);
                // Run the Fork
                this.forks.put(Optional.<Fork>of(fork), Optional.<Future<?>>of(this.taskExecutor.submit(fork)));
            } else {
                this.forks.put(Optional.<Fork>absent(), Optional.<Future<?>>absent());
            }
        }
    } else {
        SynchronousFork fork = closer.register(new SynchronousFork(this.taskContext, schema instanceof Copyable ? ((Copyable) schema).copy() : schema, branches, 0, this.taskMode));
        configureStreamingFork(fork, watermarkingStrategy);
        this.forks.put(Optional.<Fork>of(fork), Optional.<Future<?>>of(this.taskExecutor.submit(fork)));
    }
    if (isStreamingTask()) {
        // Start watermark manager and tracker
        if (this.watermarkTracker.isPresent()) {
            this.watermarkTracker.get().start();
        }
        this.watermarkManager.get().start();
        ((StreamingExtractor) this.taskContext.getRawSourceExtractor()).start(this.watermarkStorage.get());
        RecordEnvelope recordEnvelope;
        // Extract, convert, and fork one source record at a time.
        while (!shutdownRequested() && (recordEnvelope = extractor.readRecordEnvelope()) != null) {
            onRecordExtract();
            AcknowledgableWatermark ackableWatermark = new AcknowledgableWatermark(recordEnvelope.getWatermark());
            if (watermarkTracker.isPresent()) {
                watermarkTracker.get().track(ackableWatermark);
            }
            for (Object convertedRecord : converter.convertRecord(schema, recordEnvelope, this.taskState)) {
                processRecord(convertedRecord, forkOperator, rowChecker, rowResults, branches, ackableWatermark.incrementAck());
            }
            ackableWatermark.ack();
        }
    } else {
        RecordEnvelope record;
        // Extract, convert, and fork one source record at a time.
        long errRecords = 0;
        while ((record = extractor.readRecordEnvelope()) != null) {
            onRecordExtract();
            try {
                for (Object convertedRecord : converter.convertRecord(schema, record.getRecord(), this.taskState)) {
                    processRecord(convertedRecord, forkOperator, rowChecker, rowResults, branches, null);
                }
            } catch (Exception e) {
                if (!(e instanceof DataConversionException) && !(e.getCause() instanceof DataConversionException)) {
                    LOG.error("Processing record incurs an unexpected exception: ", e);
                    throw new RuntimeException(e.getCause());
                }
                errRecords++;
                if (errRecords > this.taskState.getPropAsLong(TaskConfigurationKeys.TASK_SKIP_ERROR_RECORDS, TaskConfigurationKeys.DEFAULT_TASK_SKIP_ERROR_RECORDS)) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    LOG.info("Extracted " + this.recordsPulled + " data records");
    LOG.info("Row quality checker finished with results: " + rowResults.getResults());
    this.taskState.setProp(ConfigurationKeys.EXTRACTOR_ROWS_EXTRACTED, this.recordsPulled);
    this.taskState.setProp(ConfigurationKeys.EXTRACTOR_ROWS_EXPECTED, extractor.getExpectedRecordCount());
    for (Optional<Fork> fork : this.forks.keySet()) {
        if (fork.isPresent()) {
            // Tell the fork that the main branch is completed and no new incoming data records should be expected
            fork.get().markParentTaskDone();
        }
    }
    for (Optional<Future<?>> forkFuture : this.forks.values()) {
        if (forkFuture.isPresent()) {
            try {
                long forkFutureStartTime = System.nanoTime();
                forkFuture.get().get();
                long forkDuration = System.nanoTime() - forkFutureStartTime;
                LOG.info("Task shutdown: Fork future reaped in {} millis", forkDuration / 1000000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
Also used : AsynchronousFork(org.apache.gobblin.runtime.fork.AsynchronousFork) AsynchronousFork(org.apache.gobblin.runtime.fork.AsynchronousFork) Fork(org.apache.gobblin.runtime.fork.Fork) SynchronousFork(org.apache.gobblin.runtime.fork.SynchronousFork) RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) StreamingExtractor(org.apache.gobblin.source.extractor.StreamingExtractor) DataConversionException(org.apache.gobblin.converter.DataConversionException) IOException(java.io.IOException) CopyNotSupportedException(org.apache.gobblin.fork.CopyNotSupportedException) ForkOperator(org.apache.gobblin.fork.ForkOperator) Copyable(org.apache.gobblin.fork.Copyable) SynchronousFork(org.apache.gobblin.runtime.fork.SynchronousFork) Future(java.util.concurrent.Future) RowLevelPolicyCheckResults(org.apache.gobblin.qualitychecker.row.RowLevelPolicyCheckResults) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataConversionException(org.apache.gobblin.converter.DataConversionException) CopyNotSupportedException(org.apache.gobblin.fork.CopyNotSupportedException)

Example 12 with RecordEnvelope

use of org.apache.gobblin.stream.RecordEnvelope in project incubator-gobblin by apache.

the class CloseOnFlushWriterWrapperTest method testCloseOnFlushDisabled.

@Test
public void testCloseOnFlushDisabled() throws IOException {
    WorkUnitState state = new WorkUnitState();
    List<DummyWriter> dummyWriters = new ArrayList<>();
    CloseOnFlushWriterWrapper<byte[]> writer = getCloseOnFlushWriter(dummyWriters, state);
    byte[] record = new byte[] { 'a', 'b', 'c', 'd' };
    writer.writeEnvelope(new RecordEnvelope(record));
    writer.getMessageHandler().handleMessage(FlushControlMessage.builder().build());
    Assert.assertEquals(dummyWriters.get(0).recordsWritten(), 1);
    Assert.assertEquals(dummyWriters.get(0).flushCount, 1);
    Assert.assertEquals(dummyWriters.get(0).closeCount, 0);
    Assert.assertFalse(dummyWriters.get(0).committed);
    Assert.assertEquals(dummyWriters.get(0).handlerCalled, 1);
}
Also used : RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 13 with RecordEnvelope

use of org.apache.gobblin.stream.RecordEnvelope in project incubator-gobblin by apache.

the class CloseOnFlushWriterWrapperTest method testCloseAfterFlush.

@Test
public void testCloseAfterFlush() throws IOException {
    WorkUnitState state = new WorkUnitState();
    state.getJobState().setProp(CloseOnFlushWriterWrapper.WRITER_CLOSE_ON_FLUSH_KEY, "true");
    List<DummyWriter> dummyWriters = new ArrayList<>();
    CloseOnFlushWriterWrapper<byte[]> writer = getCloseOnFlushWriter(dummyWriters, state);
    byte[] record = new byte[] { 'a', 'b', 'c', 'd' };
    writer.writeEnvelope(new RecordEnvelope(record));
    writer.getMessageHandler().handleMessage(FlushControlMessage.builder().build());
    Assert.assertEquals(dummyWriters.get(0).recordsWritten(), 1);
    Assert.assertEquals(dummyWriters.get(0).flushCount, 1);
    Assert.assertEquals(dummyWriters.get(0).closeCount, 1);
    Assert.assertTrue(dummyWriters.get(0).committed);
    Assert.assertEquals(dummyWriters.get(0).handlerCalled, 1);
    writer.close();
    // writer should not be closed multiple times
    Assert.assertEquals(dummyWriters.get(0).closeCount, 1);
}
Also used : RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 14 with RecordEnvelope

use of org.apache.gobblin.stream.RecordEnvelope in project incubator-gobblin by apache.

the class CloseOnFlushWriterWrapperTest method testCloseOnFlushEnabled.

@Test
public void testCloseOnFlushEnabled() throws IOException {
    WorkUnitState state = new WorkUnitState();
    state.getJobState().setProp(CloseOnFlushWriterWrapper.WRITER_CLOSE_ON_FLUSH_KEY, "true");
    List<DummyWriter> dummyWriters = new ArrayList<>();
    CloseOnFlushWriterWrapper<byte[]> writer = getCloseOnFlushWriter(dummyWriters, state);
    byte[] record = new byte[] { 'a', 'b', 'c', 'd' };
    writer.writeEnvelope(new RecordEnvelope(record));
    writer.getMessageHandler().handleMessage(FlushControlMessage.builder().build());
    Assert.assertEquals(dummyWriters.get(0).recordsWritten(), 1);
    Assert.assertEquals(dummyWriters.get(0).flushCount, 1);
    Assert.assertEquals(dummyWriters.get(0).closeCount, 1);
    Assert.assertTrue(dummyWriters.get(0).committed);
    Assert.assertEquals(dummyWriters.get(0).handlerCalled, 1);
}
Also used : RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 15 with RecordEnvelope

use of org.apache.gobblin.stream.RecordEnvelope in project incubator-gobblin by apache.

the class ConsoleWriterTest method writeEnvelope.

private void writeEnvelope(ConsoleWriter consoleWriter, String content, String source, long value) throws IOException {
    CheckpointableWatermark watermark = new DefaultCheckpointableWatermark(source, new LongWatermark(value));
    AcknowledgableWatermark ackable = new AcknowledgableWatermark(watermark);
    RecordEnvelope<String> mockEnvelope = (RecordEnvelope<String>) new RecordEnvelope<>(content).addCallBack(ackable);
    consoleWriter.writeEnvelope(mockEnvelope);
    Assert.assertTrue(ackable.isAcked());
}
Also used : RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) DefaultCheckpointableWatermark(org.apache.gobblin.source.extractor.DefaultCheckpointableWatermark) CheckpointableWatermark(org.apache.gobblin.source.extractor.CheckpointableWatermark) DefaultCheckpointableWatermark(org.apache.gobblin.source.extractor.DefaultCheckpointableWatermark) LongWatermark(org.apache.gobblin.source.extractor.extract.LongWatermark)

Aggregations

RecordEnvelope (org.apache.gobblin.stream.RecordEnvelope)23 Test (org.testng.annotations.Test)13 State (org.apache.gobblin.configuration.State)7 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 RecordStreamWithMetadata (org.apache.gobblin.records.RecordStreamWithMetadata)4 LongWatermark (org.apache.gobblin.source.extractor.extract.LongWatermark)4 FinalState (org.apache.gobblin.util.FinalState)4 CheckpointableWatermark (org.apache.gobblin.source.extractor.CheckpointableWatermark)3 List (java.util.List)2 Properties (java.util.Properties)2 BasicAckableForTesting (org.apache.gobblin.ack.BasicAckableForTesting)2 DataConversionException (org.apache.gobblin.converter.DataConversionException)2 NonTransientException (org.apache.gobblin.exception.NonTransientException)2 TaskPublisher (org.apache.gobblin.publisher.TaskPublisher)2 RowLevelPolicyCheckResults (org.apache.gobblin.qualitychecker.row.RowLevelPolicyCheckResults)2 RowLevelPolicyChecker (org.apache.gobblin.qualitychecker.row.RowLevelPolicyChecker)2 TaskLevelPolicyCheckResults (org.apache.gobblin.qualitychecker.task.TaskLevelPolicyCheckResults)2