Search in sources :

Example 16 with MockSource

use of org.apache.flink.api.connector.source.mocks.MockSource in project flink by apache.

the class MultipleInputStreamTaskChainedSourcesCheckpointingTest method testOnlyOneSource.

@Test
public void testOnlyOneSource() throws Exception {
    try (StreamTaskMailboxTestHarness<String> testHarness = new StreamTaskMailboxTestHarnessBuilder<>(MultipleInputStreamTask::new, BasicTypeInfo.STRING_TYPE_INFO).modifyExecutionConfig(applyObjectReuse(objectReuse)).addSourceInput(new SourceOperatorFactory<>(new MockSource(Boundedness.BOUNDED, 1), WatermarkStrategy.noWatermarks()), BasicTypeInfo.INT_TYPE_INFO).setupOutputForSingletonOperatorChain(new MapToStringMultipleInputOperatorFactory(1)).build()) {
        testHarness.setAutoProcess(false);
        ArrayDeque<Object> expectedOutput = new ArrayDeque<>();
        addSourceRecords(testHarness, 0, 42, 43, 44);
        processSingleStepUntil(testHarness, () -> !testHarness.getOutput().isEmpty());
        expectedOutput.add(new StreamRecord<>("42", TimestampAssigner.NO_TIMESTAMP));
        CheckpointBarrier barrier = createBarrier(testHarness);
        Future<Boolean> checkpointFuture = testHarness.getStreamTask().triggerCheckpointAsync(metaData, barrier.getCheckpointOptions());
        processSingleStepUntil(testHarness, checkpointFuture::isDone);
        ArrayList<Object> actualOutput = new ArrayList<>(testHarness.getOutput());
        assertThat(actualOutput.subList(0, expectedOutput.size()), containsInAnyOrder(expectedOutput.toArray()));
        assertThat(actualOutput.get(expectedOutput.size()), equalTo(barrier));
    }
}
Also used : MockSource(org.apache.flink.api.connector.source.mocks.MockSource) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) MapToStringMultipleInputOperatorFactory(org.apache.flink.streaming.runtime.tasks.MultipleInputStreamTaskTest.MapToStringMultipleInputOperatorFactory) Test(org.junit.Test)

Example 17 with MockSource

use of org.apache.flink.api.connector.source.mocks.MockSource in project flink by apache.

the class MultipleInputStreamTaskChainedSourcesCheckpointingTest method testStopWithSavepointDrainWaitsForSourcesFinish.

/**
 * In this scenario:
 *
 * <ul>
 *   <li>Network inputs are processed until CheckpointBarriers for synchronous savepoint are
 *       processed.
 *   <li>RPC for stop-with-savepoint comes for sources
 *   <li>Sources keep being invoked until they return END_OF_DATA
 *   <li>Synchronous savepoint is triggered
 * </ul>
 */
@Test
public void testStopWithSavepointDrainWaitsForSourcesFinish() throws Exception {
    try (StreamTaskMailboxTestHarness<String> testHarness = new StreamTaskMailboxTestHarnessBuilder<>(MultipleInputStreamTask::new, BasicTypeInfo.STRING_TYPE_INFO).setCollectNetworkEvents().modifyExecutionConfig(applyObjectReuse(objectReuse)).modifyStreamConfig(config -> config.setCheckpointingEnabled(true)).addInput(BasicTypeInfo.STRING_TYPE_INFO).addSourceInput(new SourceOperatorFactory<>(new MockSource(Boundedness.CONTINUOUS_UNBOUNDED, 1), WatermarkStrategy.noWatermarks()), BasicTypeInfo.INT_TYPE_INFO).addSourceInput(new SourceOperatorFactory<>(new MockSource(Boundedness.CONTINUOUS_UNBOUNDED, 1), WatermarkStrategy.noWatermarks()), BasicTypeInfo.INT_TYPE_INFO).addInput(BasicTypeInfo.DOUBLE_TYPE_INFO).setupOutputForSingletonOperatorChain(new MapToStringMultipleInputOperatorFactory(4, true)).build()) {
        testHarness.setAutoProcess(false);
        ArrayDeque<Object> expectedOutput = new ArrayDeque<>();
        CheckpointBarrier barrier = createStopWithSavepointDrainBarrier();
        testHarness.processElement(new StreamRecord<>("44", TimestampAssigner.NO_TIMESTAMP), 0);
        testHarness.processEvent(new EndOfData(StopMode.DRAIN), 0);
        testHarness.processEvent(barrier, 0);
        testHarness.processElement(new StreamRecord<>(47d, TimestampAssigner.NO_TIMESTAMP), 1);
        testHarness.processEvent(new EndOfData(StopMode.DRAIN), 1);
        testHarness.processEvent(barrier, 1);
        addSourceRecords(testHarness, 1, Boundedness.CONTINUOUS_UNBOUNDED, 1, 2);
        addSourceRecords(testHarness, 2, Boundedness.CONTINUOUS_UNBOUNDED, 3, 4);
        testHarness.processAll();
        Future<Boolean> checkpointFuture = testHarness.getStreamTask().triggerCheckpointAsync(metaData, barrier.getCheckpointOptions());
        processSingleStepUntil(testHarness, checkpointFuture::isDone);
        expectedOutput.add(new StreamRecord<>("3", TimestampAssigner.NO_TIMESTAMP));
        expectedOutput.add(new StreamRecord<>("47.0", TimestampAssigner.NO_TIMESTAMP));
        expectedOutput.add(new StreamRecord<>("44", TimestampAssigner.NO_TIMESTAMP));
        expectedOutput.add(new StreamRecord<>("1", TimestampAssigner.NO_TIMESTAMP));
        expectedOutput.add(new StreamRecord<>("4", TimestampAssigner.NO_TIMESTAMP));
        expectedOutput.add(new StreamRecord<>("2", TimestampAssigner.NO_TIMESTAMP));
        ArrayList<Object> actualOutput = new ArrayList<>(testHarness.getOutput());
        assertThat(actualOutput.subList(0, expectedOutput.size()), containsInAnyOrder(expectedOutput.toArray()));
        assertThat(actualOutput.subList(actualOutput.size() - 3, actualOutput.size()), contains(new StreamRecord<>("FINISH"), new EndOfData(StopMode.DRAIN), barrier));
    }
}
Also used : MockSource(org.apache.flink.api.connector.source.mocks.MockSource) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) SourceOperatorFactory(org.apache.flink.streaming.api.operators.SourceOperatorFactory) ArrayDeque(java.util.ArrayDeque) CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) EndOfData(org.apache.flink.runtime.io.network.api.EndOfData) MapToStringMultipleInputOperatorFactory(org.apache.flink.streaming.runtime.tasks.MultipleInputStreamTaskTest.MapToStringMultipleInputOperatorFactory) Test(org.junit.Test)

Example 18 with MockSource

use of org.apache.flink.api.connector.source.mocks.MockSource in project flink by apache.

the class HybridSourceSplitSerializerTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Map<Integer, Source> switchedSources = new HashMap<>();
    switchedSources.put(0, new MockSource(null, 0));
    byte[] splitBytes = { 1, 2, 3 };
    HybridSourceSplitSerializer serializer = new HybridSourceSplitSerializer();
    HybridSourceSplit split = new HybridSourceSplit(0, splitBytes, 0, "splitId");
    byte[] serialized = serializer.serialize(split);
    HybridSourceSplit clonedSplit = serializer.deserialize(0, serialized);
    Assert.assertEquals(split, clonedSplit);
    try {
        serializer.deserialize(1, serialized);
        Assert.fail();
    } catch (IOException e) {
    // expected invalid version
    }
}
Also used : MockSource(org.apache.flink.api.connector.source.mocks.MockSource) HashMap(java.util.HashMap) IOException(java.io.IOException) MockSource(org.apache.flink.api.connector.source.mocks.MockSource) Source(org.apache.flink.api.connector.source.Source) Test(org.junit.Test)

Aggregations

MockSource (org.apache.flink.api.connector.source.mocks.MockSource)18 Test (org.junit.Test)16 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)7 SourceOperatorFactory (org.apache.flink.streaming.api.operators.SourceOperatorFactory)6 ArrayList (java.util.ArrayList)5 ArrayDeque (java.util.ArrayDeque)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 JobID (org.apache.flink.api.common.JobID)2 SourceReaderContext (org.apache.flink.api.connector.source.SourceReaderContext)2 MockSourceSplit (org.apache.flink.api.connector.source.mocks.MockSourceSplit)2 Gauge (org.apache.flink.metrics.Gauge)2 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)2 CheckpointMetrics (org.apache.flink.runtime.checkpoint.CheckpointMetrics)2 TaskStateSnapshot (org.apache.flink.runtime.checkpoint.TaskStateSnapshot)2 ExecutionAttemptID (org.apache.flink.runtime.executiongraph.ExecutionAttemptID)2 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)2