Search in sources :

Example 11 with StreamSource

use of org.apache.flink.streaming.api.operators.StreamSource in project flink by apache.

the class SourceStreamTaskTest method testCheckpointing.

/**
	 * This test ensures that the SourceStreamTask properly serializes checkpointing
	 * and element emission. This also verifies that there are no concurrent invocations
	 * of the checkpoint method on the source operator.
	 *
	 * The source emits elements and performs checkpoints. We have several checkpointer threads
	 * that fire checkpoint requests at the source task.
	 *
	 * If element emission and checkpointing are not in series the count of elements at the
	 * beginning of a checkpoint and at the end of a checkpoint are not the same because the
	 * source kept emitting elements while the checkpoint was ongoing.
	 */
@Test
@SuppressWarnings("unchecked")
public void testCheckpointing() throws Exception {
    final int NUM_ELEMENTS = 100;
    final int NUM_CHECKPOINTS = 100;
    final int NUM_CHECKPOINTERS = 1;
    // in ms
    final int CHECKPOINT_INTERVAL = 5;
    // how many random values we sum up in storeCheckpoint
    final int SOURCE_CHECKPOINT_DELAY = 1000;
    // in ms
    final int SOURCE_READ_DELAY = 1;
    ExecutorService executor = Executors.newFixedThreadPool(10);
    try {
        final TupleTypeInfo<Tuple2<Long, Integer>> typeInfo = new TupleTypeInfo<Tuple2<Long, Integer>>(BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO);
        final SourceStreamTask<Tuple2<Long, Integer>, SourceFunction<Tuple2<Long, Integer>>, StreamSource<Tuple2<Long, Integer>, SourceFunction<Tuple2<Long, Integer>>>> sourceTask = new SourceStreamTask<>();
        final StreamTaskTestHarness<Tuple2<Long, Integer>> testHarness = new StreamTaskTestHarness<Tuple2<Long, Integer>>(sourceTask, typeInfo);
        testHarness.setupOutputForSingletonOperatorChain();
        StreamConfig streamConfig = testHarness.getStreamConfig();
        StreamSource<Tuple2<Long, Integer>, ?> sourceOperator = new StreamSource<>(new MockSource(NUM_ELEMENTS, SOURCE_CHECKPOINT_DELAY, SOURCE_READ_DELAY));
        streamConfig.setStreamOperator(sourceOperator);
        // prepare the
        Future<Boolean>[] checkpointerResults = new Future[NUM_CHECKPOINTERS];
        // invoke this first, so the tasks are actually running when the checkpoints are scheduled
        testHarness.invoke();
        for (int i = 0; i < NUM_CHECKPOINTERS; i++) {
            checkpointerResults[i] = executor.submit(new Checkpointer(NUM_CHECKPOINTS, CHECKPOINT_INTERVAL, sourceTask));
        }
        testHarness.waitForTaskCompletion();
        // will be rethrown here
        for (int i = 0; i < NUM_CHECKPOINTERS; i++) {
            if (!checkpointerResults[i].isDone()) {
                checkpointerResults[i].cancel(true);
            }
            if (!checkpointerResults[i].isCancelled()) {
                checkpointerResults[i].get();
            }
        }
        List<Tuple2<Long, Integer>> resultElements = TestHarnessUtil.getRawElementsFromOutput(testHarness.getOutput());
        Assert.assertEquals(NUM_ELEMENTS, resultElements.size());
    } finally {
        executor.shutdown();
    }
}
Also used : SourceFunction(org.apache.flink.streaming.api.functions.source.SourceFunction) RichSourceFunction(org.apache.flink.streaming.api.functions.source.RichSourceFunction) StreamSource(org.apache.flink.streaming.api.operators.StreamSource) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) Tuple2(org.apache.flink.api.java.tuple.Tuple2) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) Future(java.util.concurrent.Future) Test(org.junit.Test)

Aggregations

StreamSource (org.apache.flink.streaming.api.operators.StreamSource)11 Test (org.junit.Test)10 AbstractStreamOperatorTestHarness (org.apache.flink.streaming.util.AbstractStreamOperatorTestHarness)7 ArrayList (java.util.ArrayList)5 OperatorStateHandles (org.apache.flink.streaming.runtime.tasks.OperatorStateHandles)4 SourceFunction (org.apache.flink.streaming.api.functions.source.SourceFunction)3 HashMap (java.util.HashMap)2 TextInputFormat (org.apache.flink.api.java.io.TextInputFormat)2 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)2 Path (org.apache.flink.core.fs.Path)2 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)2 ContinuousFileMonitoringFunction (org.apache.flink.streaming.api.functions.source.ContinuousFileMonitoringFunction)2 RichSourceFunction (org.apache.flink.streaming.api.functions.source.RichSourceFunction)2 TimestampedFileInputSplit (org.apache.flink.streaming.api.functions.source.TimestampedFileInputSplit)2 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)2 StoppableStreamSource (org.apache.flink.streaming.api.operators.StoppableStreamSource)2 KafkaTopicPartition (org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicPartition)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 List (java.util.List)1