Search in sources :

Example 1 with FlinkMetricContainer

use of org.apache.beam.runners.flink.metrics.FlinkMetricContainer in project beam by apache.

the class FlinkStatefulDoFnFunction method open.

@Override
public void open(Configuration parameters) {
    // Note that the SerializablePipelineOptions already initialize FileSystems in the readObject()
    // deserialization method. However, this is a hack, and we want to properly initialize the
    // options where they are needed.
    PipelineOptions options = serializedOptions.get();
    FileSystems.setDefaultPipelineOptions(options);
    metricContainer = new FlinkMetricContainer(getRuntimeContext());
    doFnInvoker = DoFnInvokers.tryInvokeSetupFor(dofn, options);
}
Also used : SerializablePipelineOptions(org.apache.beam.runners.core.construction.SerializablePipelineOptions) FlinkPipelineOptions(org.apache.beam.runners.flink.FlinkPipelineOptions) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) FlinkMetricContainer(org.apache.beam.runners.flink.metrics.FlinkMetricContainer)

Example 2 with FlinkMetricContainer

use of org.apache.beam.runners.flink.metrics.FlinkMetricContainer in project beam by apache.

the class FlinkExecutableStageFunctionTest method testAccumulatorRegistrationOnOperatorClose.

@Test
public void testAccumulatorRegistrationOnOperatorClose() throws Exception {
    FlinkExecutableStageFunction<Integer> function = getFunction(Collections.emptyMap());
    function.open(new Configuration());
    String metricContainerFieldName = "metricContainer";
    FlinkMetricContainer monitoredContainer = Mockito.spy((FlinkMetricContainer) Whitebox.getInternalState(function, metricContainerFieldName));
    Whitebox.setInternalState(function, metricContainerFieldName, monitoredContainer);
    function.close();
    Mockito.verify(monitoredContainer).registerMetricsForPipelineResult();
}
Also used : Configuration(org.apache.flink.configuration.Configuration) FlinkMetricContainer(org.apache.beam.runners.flink.metrics.FlinkMetricContainer) Test(org.junit.Test)

Example 3 with FlinkMetricContainer

use of org.apache.beam.runners.flink.metrics.FlinkMetricContainer in project beam by apache.

the class BoundedSourceWrapper method run.

@Override
public void run(SourceContext<WindowedValue<OutputT>> ctx) throws Exception {
    // figure out which split sources we're responsible for
    int subtaskIndex = getRuntimeContext().getIndexOfThisSubtask();
    int numSubtasks = getRuntimeContext().getNumberOfParallelSubtasks();
    List<BoundedSource<OutputT>> localSources = new ArrayList<>();
    for (int i = 0; i < splitSources.size(); i++) {
        if (i % numSubtasks == subtaskIndex) {
            localSources.add(splitSources.get(i));
        }
    }
    LOG.info("Bounded Flink Source {}/{} is reading from sources: {}", subtaskIndex, numSubtasks, localSources);
    FlinkMetricContainer metricContainer = new FlinkMetricContainer(getRuntimeContext());
    ReaderInvocationUtil<OutputT, BoundedSource.BoundedReader<OutputT>> readerInvoker = new ReaderInvocationUtil<>(stepName, serializedOptions.getPipelineOptions(), metricContainer);
    readers = new ArrayList<>();
    // initialize readers from scratch
    for (BoundedSource<OutputT> source : localSources) {
        readers.add(source.createReader(serializedOptions.getPipelineOptions()));
    }
    if (readers.size() == 1) {
        // the easy case, we just read from one reader
        BoundedSource.BoundedReader<OutputT> reader = readers.get(0);
        boolean dataAvailable = readerInvoker.invokeStart(reader);
        if (dataAvailable) {
            emitElement(ctx, reader);
        }
        while (isRunning) {
            dataAvailable = readerInvoker.invokeAdvance(reader);
            if (dataAvailable) {
                emitElement(ctx, reader);
            } else {
                break;
            }
        }
    } else {
        // a bit more complicated, we are responsible for several readers
        // loop through them and sleep if none of them had any data
        int currentReader = 0;
        // start each reader and emit data if immediately available
        for (BoundedSource.BoundedReader<OutputT> reader : readers) {
            boolean dataAvailable = readerInvoker.invokeStart(reader);
            if (dataAvailable) {
                emitElement(ctx, reader);
            }
        }
        // a flag telling us whether any of the readers had data
        // if no reader had data, sleep for bit
        boolean hadData = false;
        while (isRunning && !readers.isEmpty()) {
            BoundedSource.BoundedReader<OutputT> reader = readers.get(currentReader);
            boolean dataAvailable = readerInvoker.invokeAdvance(reader);
            if (dataAvailable) {
                emitElement(ctx, reader);
                hadData = true;
            } else {
                readers.remove(currentReader);
                currentReader--;
                if (readers.isEmpty()) {
                    break;
                }
            }
            currentReader = (currentReader + 1) % readers.size();
            if (currentReader == 0 && !hadData) {
                Thread.sleep(50);
            } else if (currentReader == 0) {
                hadData = false;
            }
        }
    }
    // emit final Long.MAX_VALUE watermark, just to be sure
    ctx.emitWatermark(new Watermark(Long.MAX_VALUE));
}
Also used : ReaderInvocationUtil(org.apache.beam.runners.flink.metrics.ReaderInvocationUtil) BoundedSource(org.apache.beam.sdk.io.BoundedSource) ArrayList(java.util.ArrayList) Watermark(org.apache.flink.streaming.api.watermark.Watermark) FlinkMetricContainer(org.apache.beam.runners.flink.metrics.FlinkMetricContainer)

Example 4 with FlinkMetricContainer

use of org.apache.beam.runners.flink.metrics.FlinkMetricContainer in project beam by apache.

the class DoFnOperatorTest method testAccumulatorRegistrationOnOperatorClose.

@Test
public void testAccumulatorRegistrationOnOperatorClose() throws Exception {
    DoFnOperator<String, String> doFnOperator = getOperatorForCleanupInspection();
    OneInputStreamOperatorTestHarness<WindowedValue<String>, WindowedValue<String>> testHarness = new OneInputStreamOperatorTestHarness<>(doFnOperator);
    testHarness.open();
    String metricContainerFieldName = "flinkMetricContainer";
    FlinkMetricContainer monitoredContainer = Mockito.spy((FlinkMetricContainer) Whitebox.getInternalState(doFnOperator, metricContainerFieldName));
    Whitebox.setInternalState(doFnOperator, metricContainerFieldName, monitoredContainer);
    // Closes and disposes the operator
    testHarness.close();
    // Ensure that dispose has the metrics code
    doFnOperator.cleanUp();
    Mockito.verify(monitoredContainer, Mockito.times(2)).registerMetricsForPipelineResult();
}
Also used : WindowedValue(org.apache.beam.sdk.util.WindowedValue) StreamRecordStripper.stripStreamRecordFromWindowedValue(org.apache.beam.runners.flink.translation.wrappers.streaming.StreamRecordStripper.stripStreamRecordFromWindowedValue) KeyedOneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness) OneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness) FlinkMetricContainer(org.apache.beam.runners.flink.metrics.FlinkMetricContainer) Test(org.junit.Test)

Example 5 with FlinkMetricContainer

use of org.apache.beam.runners.flink.metrics.FlinkMetricContainer in project beam by apache.

the class SourceInputFormatTest method testAccumulatorRegistrationOnOperatorClose.

@Test
public void testAccumulatorRegistrationOnOperatorClose() throws Exception {
    SourceInputFormat<Long> sourceInputFormat = new TestSourceInputFormat<>("step", CountingSource.upTo(10), PipelineOptionsFactory.create());
    sourceInputFormat.open(sourceInputFormat.createInputSplits(1)[0]);
    String metricContainerFieldName = "metricContainer";
    FlinkMetricContainer monitoredContainer = Mockito.spy((FlinkMetricContainer) Whitebox.getInternalState(sourceInputFormat, metricContainerFieldName));
    Whitebox.setInternalState(sourceInputFormat, metricContainerFieldName, monitoredContainer);
    sourceInputFormat.close();
    Mockito.verify(monitoredContainer).registerMetricsForPipelineResult();
}
Also used : FlinkMetricContainer(org.apache.beam.runners.flink.metrics.FlinkMetricContainer) Test(org.junit.Test)

Aggregations

FlinkMetricContainer (org.apache.beam.runners.flink.metrics.FlinkMetricContainer)12 Test (org.junit.Test)5 FlinkPipelineOptions (org.apache.beam.runners.flink.FlinkPipelineOptions)4 SerializablePipelineOptions (org.apache.beam.runners.core.construction.SerializablePipelineOptions)3 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)3 Configuration (org.apache.flink.configuration.Configuration)3 ArrayList (java.util.ArrayList)2 DoFnRunners (org.apache.beam.runners.core.DoFnRunners)2 WindowedValue (org.apache.beam.sdk.util.WindowedValue)2 TupleTag (org.apache.beam.sdk.values.TupleTag)2 Watermark (org.apache.flink.streaming.api.watermark.Watermark)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Serializable (java.io.Serializable)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1