Search in sources :

Example 86 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class MultiInputSortingDataInputsTest method twoInputOrderTest.

@SuppressWarnings("unchecked")
public void twoInputOrderTest(int preferredIndex, int sortedIndex) throws Exception {
    CollectingDataOutput<Object> collectingDataOutput = new CollectingDataOutput<>();
    List<StreamElement> sortedInputElements = Arrays.asList(new StreamRecord<>(1, 3), new StreamRecord<>(1, 1), new StreamRecord<>(2, 1), new StreamRecord<>(2, 3), new StreamRecord<>(1, 2), new StreamRecord<>(2, 2), Watermark.MAX_WATERMARK);
    CollectionDataInput<Integer> sortedInput = new CollectionDataInput<>(sortedInputElements, sortedIndex);
    List<StreamElement> preferredInputElements = Arrays.asList(new StreamRecord<>(99, 3), new StreamRecord<>(99, 1), new Watermark(99L));
    CollectionDataInput<Integer> preferredInput = new CollectionDataInput<>(preferredInputElements, preferredIndex);
    KeySelector<Integer, Integer> keySelector = value -> value;
    try (MockEnvironment environment = MockEnvironment.builder().build()) {
        SelectableSortingInputs selectableSortingInputs = MultiInputSortingDataInput.wrapInputs(new DummyInvokable(), new StreamTaskInput[] { sortedInput }, new KeySelector[] { keySelector }, new TypeSerializer[] { new IntSerializer() }, new IntSerializer(), new StreamTaskInput[] { preferredInput }, environment.getMemoryManager(), environment.getIOManager(), true, 1.0, new Configuration(), new ExecutionConfig());
        StreamTaskInput<?>[] sortingDataInputs = selectableSortingInputs.getSortedInputs();
        StreamTaskInput<?>[] preferredDataInputs = selectableSortingInputs.getPassThroughInputs();
        try (StreamTaskInput<Object> preferredTaskInput = (StreamTaskInput<Object>) preferredDataInputs[0];
            StreamTaskInput<Object> sortedTaskInput = (StreamTaskInput<Object>) sortingDataInputs[0]) {
            MultipleInputSelectionHandler selectionHandler = new MultipleInputSelectionHandler(selectableSortingInputs.getInputSelectable(), 2);
            @SuppressWarnings("rawtypes") StreamOneInputProcessor[] inputProcessors = new StreamOneInputProcessor[2];
            inputProcessors[preferredIndex] = new StreamOneInputProcessor<>(preferredTaskInput, collectingDataOutput, new DummyOperatorChain());
            inputProcessors[sortedIndex] = new StreamOneInputProcessor<>(sortedTaskInput, collectingDataOutput, new DummyOperatorChain());
            StreamMultipleInputProcessor processor = new StreamMultipleInputProcessor(selectionHandler, inputProcessors);
            DataInputStatus inputStatus;
            do {
                inputStatus = processor.processInput();
            } while (inputStatus != DataInputStatus.END_OF_INPUT);
        }
    }
    assertThat(collectingDataOutput.events, equalTo(Arrays.asList(new StreamRecord<>(99, 3), new StreamRecord<>(99, 1), // max watermark from the preferred input
    new Watermark(99L), new StreamRecord<>(1, 1), new StreamRecord<>(1, 2), new StreamRecord<>(1, 3), new StreamRecord<>(2, 1), new StreamRecord<>(2, 2), new StreamRecord<>(2, 3), // max watermark from the sorted input
    Watermark.MAX_WATERMARK)));
}
Also used : StreamTaskInput(org.apache.flink.streaming.runtime.io.StreamTaskInput) Arrays(java.util.Arrays) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) KeySelector(org.apache.flink.api.java.functions.KeySelector) BoundedMultiInput(org.apache.flink.streaming.api.operators.BoundedMultiInput) StreamElement(org.apache.flink.streaming.runtime.streamrecord.StreamElement) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Configuration(org.apache.flink.configuration.Configuration) SelectableSortingInputs(org.apache.flink.streaming.api.operators.sort.MultiInputSortingDataInput.SelectableSortingInputs) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test) StreamMultipleInputProcessor(org.apache.flink.streaming.runtime.io.StreamMultipleInputProcessor) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) List(java.util.List) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) DataInputStatus(org.apache.flink.streaming.runtime.io.DataInputStatus) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) MultipleInputSelectionHandler(org.apache.flink.streaming.runtime.io.MultipleInputSelectionHandler) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) StreamOneInputProcessor(org.apache.flink.streaming.runtime.io.StreamOneInputProcessor) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) Configuration(org.apache.flink.configuration.Configuration) StreamTaskInput(org.apache.flink.streaming.runtime.io.StreamTaskInput) StreamElement(org.apache.flink.streaming.runtime.streamrecord.StreamElement) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) SelectableSortingInputs(org.apache.flink.streaming.api.operators.sort.MultiInputSortingDataInput.SelectableSortingInputs) StreamOneInputProcessor(org.apache.flink.streaming.runtime.io.StreamOneInputProcessor) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) DataInputStatus(org.apache.flink.streaming.runtime.io.DataInputStatus) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) StreamMultipleInputProcessor(org.apache.flink.streaming.runtime.io.StreamMultipleInputProcessor) Watermark(org.apache.flink.streaming.api.watermark.Watermark) MultipleInputSelectionHandler(org.apache.flink.streaming.runtime.io.MultipleInputSelectionHandler)

Example 87 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class SortingDataInputTest method simpleVariableLengthKeySorting.

@Test
public void simpleVariableLengthKeySorting() throws Exception {
    CollectingDataOutput<Integer> collectingDataOutput = new CollectingDataOutput<>();
    CollectionDataInput<Integer> input = new CollectionDataInput<>(Arrays.asList(new StreamRecord<>(1, 3), new StreamRecord<>(1, 1), new StreamRecord<>(2, 1), new StreamRecord<>(2, 3), new StreamRecord<>(1, 2), new StreamRecord<>(2, 2)));
    MockEnvironment environment = MockEnvironment.builder().build();
    SortingDataInput<Integer, String> sortingDataInput = new SortingDataInput<>(input, new IntSerializer(), new StringSerializer(), (KeySelector<Integer, String>) value -> "" + value, environment.getMemoryManager(), environment.getIOManager(), true, 1.0, new Configuration(), new DummyInvokable(), new ExecutionConfig());
    DataInputStatus inputStatus;
    do {
        inputStatus = sortingDataInput.emitNext(collectingDataOutput);
    } while (inputStatus != DataInputStatus.END_OF_INPUT);
    assertThat(collectingDataOutput.events, equalTo(Arrays.asList(new StreamRecord<>(1, 1), new StreamRecord<>(1, 2), new StreamRecord<>(1, 3), new StreamRecord<>(2, 1), new StreamRecord<>(2, 2), new StreamRecord<>(2, 3))));
}
Also used : Arrays(java.util.Arrays) KeySelector(org.apache.flink.api.java.functions.KeySelector) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Configuration(org.apache.flink.configuration.Configuration) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) DataInputStatus(org.apache.flink.streaming.runtime.io.DataInputStatus) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Configuration(org.apache.flink.configuration.Configuration) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) DataInputStatus(org.apache.flink.streaming.runtime.io.DataInputStatus) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Test(org.junit.Test)

Example 88 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class SerializerComparatorTestData method getOrderedIntTestData.

@SuppressWarnings("unchecked")
static Tuple2<byte[], StreamRecord<Integer>>[] getOrderedIntTestData() {
    IntSerializer intSerializer = new IntSerializer();
    DataOutputSerializer outputSerializer = new DataOutputSerializer(intSerializer.getLength());
    return IntStream.range(-10, 10).mapToObj(idx -> {
        try {
            intSerializer.serialize(idx, outputSerializer);
            byte[] copyOfBuffer = outputSerializer.getCopyOfBuffer();
            outputSerializer.clear();
            return Tuple2.of(copyOfBuffer, new StreamRecord<>(idx, idx));
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }).toArray(Tuple2[]::new);
}
Also used : IntStream(java.util.stream.IntStream) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) Stream(java.util.stream.Stream) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Tuple2(org.apache.flink.api.java.tuple.Tuple2) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) IOException(java.io.IOException) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) IOException(java.io.IOException)

Example 89 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class AsyncWaitOperatorTest method testRestartWithFullQueue.

/**
 * Tests that the AsyncWaitOperator can restart if checkpointed queue was full.
 *
 * <p>See FLINK-7949
 */
@Test(timeout = 10000)
public void testRestartWithFullQueue() throws Exception {
    final int capacity = 10;
    // 1. create the snapshot which contains capacity + 1 elements
    final CompletableFuture<Void> trigger = new CompletableFuture<>();
    final OneInputStreamOperatorTestHarness<Integer, Integer> snapshotHarness = createTestHarness(new ControllableAsyncFunction<>(// the NoOpAsyncFunction is like a blocking function
    trigger), 1000L, capacity, AsyncDataStream.OutputMode.ORDERED);
    snapshotHarness.open();
    final OperatorSubtaskState snapshot;
    final ArrayList<Integer> expectedOutput = new ArrayList<>(capacity);
    try {
        synchronized (snapshotHarness.getCheckpointLock()) {
            for (int i = 0; i < capacity; i++) {
                snapshotHarness.processElement(i, 0L);
                expectedOutput.add(i);
            }
        }
        synchronized (snapshotHarness.getCheckpointLock()) {
            // execute the snapshot within the checkpoint lock, because then it is guaranteed
            // that the lastElementWriter has written the exceeding element
            snapshot = snapshotHarness.snapshot(0L, 0L);
        }
        // trigger the computation to make the close call finish
        trigger.complete(null);
    } finally {
        synchronized (snapshotHarness.getCheckpointLock()) {
            snapshotHarness.close();
        }
    }
    // 2. restore the snapshot and check that we complete
    final OneInputStreamOperatorTestHarness<Integer, Integer> recoverHarness = createTestHarness(new ControllableAsyncFunction<>(CompletableFuture.completedFuture(null)), 1000L, capacity, AsyncDataStream.OutputMode.ORDERED);
    recoverHarness.initializeState(snapshot);
    synchronized (recoverHarness.getCheckpointLock()) {
        recoverHarness.open();
    }
    synchronized (recoverHarness.getCheckpointLock()) {
        recoverHarness.endInput();
        recoverHarness.close();
    }
    final ConcurrentLinkedQueue<Object> output = recoverHarness.getOutput();
    final List<Integer> outputElements = output.stream().map(r -> ((StreamRecord<Integer>) r).getValue()).collect(Collectors.toList());
    assertThat(outputElements, Matchers.equalTo(expectedOutput));
}
Also used : Tuple1(org.apache.flink.api.java.tuple.Tuple1) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) SharedObjects(org.apache.flink.testutils.junit.SharedObjects) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) TimeoutException(java.util.concurrent.TimeoutException) ExceptionUtils(org.apache.flink.util.ExceptionUtils) RichAsyncFunction(org.apache.flink.streaming.api.functions.async.RichAsyncFunction) OneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness) MapFunction(org.apache.flink.api.common.functions.MapFunction) Lists(org.apache.flink.shaded.guava30.com.google.common.collect.Lists) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) TestTaskStateManager(org.apache.flink.runtime.state.TestTaskStateManager) AsyncFunction(org.apache.flink.streaming.api.functions.async.AsyncFunction) ChainingStrategy(org.apache.flink.streaming.api.operators.ChainingStrategy) TestLogger(org.apache.flink.util.TestLogger) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) StreamElementQueue(org.apache.flink.streaming.api.operators.async.queue.StreamElementQueue) StreamTaskMailboxTestHarness(org.apache.flink.streaming.runtime.tasks.StreamTaskMailboxTestHarness) TaskStateSnapshot(org.apache.flink.runtime.checkpoint.TaskStateSnapshot) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) ResultFuture(org.apache.flink.streaming.api.functions.async.ResultFuture) StreamElement(org.apache.flink.streaming.runtime.streamrecord.StreamElement) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) AsyncDataStream(org.apache.flink.streaming.api.datastream.AsyncDataStream) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) OneInputStreamTaskTestHarness(org.apache.flink.streaming.runtime.tasks.OneInputStreamTaskTestHarness) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) TypeExtractor(org.apache.flink.api.java.typeutils.TypeExtractor) Assert.assertFalse(org.junit.Assert.assertFalse) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) Optional(java.util.Optional) Utils(org.apache.flink.api.java.Utils) Queue(java.util.Queue) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) CheckpointMetaData(org.apache.flink.runtime.checkpoint.CheckpointMetaData) Watermark(org.apache.flink.streaming.api.watermark.Watermark) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StreamTaskMailboxTestHarnessBuilder(org.apache.flink.streaming.runtime.tasks.StreamTaskMailboxTestHarnessBuilder) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) RichMapFunction(org.apache.flink.api.common.functions.RichMapFunction) TupleSerializer(org.apache.flink.api.java.typeutils.runtime.TupleSerializer) Timeout(org.junit.rules.Timeout) LinkedList(java.util.LinkedList) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) TestHarnessUtil(org.apache.flink.streaming.util.TestHarnessUtil) SharedReference(org.apache.flink.testutils.junit.SharedReference) ExecutorService(java.util.concurrent.ExecutorService) DiscardingSink(org.apache.flink.streaming.api.functions.sink.DiscardingSink) Configuration(org.apache.flink.configuration.Configuration) SingleOutputStreamOperator(org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) OneInputStreamTask(org.apache.flink.streaming.runtime.tasks.OneInputStreamTask) Test(org.junit.Test) DataStream(org.apache.flink.streaming.api.datastream.DataStream) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert(org.junit.Assert) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 90 with StreamRecord

use of org.apache.flink.streaming.runtime.streamrecord.StreamRecord in project flink by apache.

the class UnorderedStreamElementQueueTest method testCompletionOrder.

/**
 * Tests that only elements before the oldest watermark are returned if they are completed.
 */
@Test
public void testCompletionOrder() {
    final UnorderedStreamElementQueue<Integer> queue = new UnorderedStreamElementQueue<>(8);
    ResultFuture<Integer> record1 = putSuccessfully(queue, new StreamRecord<>(1, 0L));
    ResultFuture<Integer> record2 = putSuccessfully(queue, new StreamRecord<>(2, 1L));
    putSuccessfully(queue, new Watermark(2L));
    ResultFuture<Integer> record3 = putSuccessfully(queue, new StreamRecord<>(3, 3L));
    ResultFuture<Integer> record4 = putSuccessfully(queue, new StreamRecord<>(4, 4L));
    putSuccessfully(queue, new Watermark(5L));
    ResultFuture<Integer> record5 = putSuccessfully(queue, new StreamRecord<>(5, 6L));
    ResultFuture<Integer> record6 = putSuccessfully(queue, new StreamRecord<>(6, 7L));
    Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
    Assert.assertEquals(8, queue.size());
    Assert.assertFalse(queue.isEmpty());
    // this should not make any item completed, because R3 is behind W1
    record3.complete(Arrays.asList(13));
    Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
    Assert.assertEquals(8, queue.size());
    Assert.assertFalse(queue.isEmpty());
    record2.complete(Arrays.asList(12));
    Assert.assertEquals(Arrays.asList(new StreamRecord<>(12, 1L)), popCompleted(queue));
    Assert.assertEquals(7, queue.size());
    Assert.assertFalse(queue.isEmpty());
    // Should not be completed because R1 has not been completed yet
    record6.complete(Arrays.asList(16));
    record4.complete(Arrays.asList(14));
    Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
    Assert.assertEquals(7, queue.size());
    Assert.assertFalse(queue.isEmpty());
    // Now W1, R3, R4 and W2 are completed and should be pollable
    record1.complete(Arrays.asList(11));
    Assert.assertEquals(Arrays.asList(new StreamRecord<>(11, 0L), new Watermark(2L), new StreamRecord<>(13, 3L), new StreamRecord<>(14, 4L), new Watermark(5L), new StreamRecord<>(16, 7L)), popCompleted(queue));
    Assert.assertEquals(1, queue.size());
    Assert.assertFalse(queue.isEmpty());
    // only R5 left in the queue
    record5.complete(Arrays.asList(15));
    Assert.assertEquals(Arrays.asList(new StreamRecord<>(15, 6L)), popCompleted(queue));
    Assert.assertEquals(0, queue.size());
    Assert.assertTrue(queue.isEmpty());
    Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Aggregations

StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)270 Test (org.junit.Test)212 ArrayList (java.util.ArrayList)156 List (java.util.List)151 Event (org.apache.flink.cep.Event)136 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)61 SubEvent (org.apache.flink.cep.SubEvent)47 Watermark (org.apache.flink.streaming.api.watermark.Watermark)45 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)39 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)36 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)34 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)31 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)31 StreamElementSerializer (org.apache.flink.streaming.runtime.streamrecord.StreamElementSerializer)30 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)28 HashMap (java.util.HashMap)23 Map (java.util.Map)23 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)23 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)20 KeySelector (org.apache.flink.api.java.functions.KeySelector)20