You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by pn...@apache.org on 2024/05/17 08:07:56 UTC

(flink) branch release-1.18 updated (1e1a7f16b6f -> 4848a50d735)

This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a change to branch release-1.18
in repository https://gitbox.apache.org/repos/asf/flink.git


    from 1e1a7f16b6f [FLINK-27741][table-planner] Fix NPE when use dense_rank() and rank()
     new 114e6b2e27a [hotfix] Make MockSourceSplit#addRecord calls chainable
     new 3f043b8bbb3 [hotfix] Allow configuring initial splits in SourceOperatorTestContext
     new 2a60769eb16 [hotfix] Allow to use per split output via SourceOperatorTestContext
     new 4848a50d735 [FLINKCC-1283] Properly initialize initial splits in WatermarkOutputMultiplexer

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../connector/source/mocks/MockSourceReader.java   | 10 +--
 .../connector/source/mocks/MockSourceSplit.java    |  3 +-
 .../streaming/api/operators/SourceOperator.java    |  1 +
 .../api/operators/SourceOperatorAlignmentTest.java |  2 +-
 .../api/operators/SourceOperatorTestContext.java   | 34 ++++++--
 .../operators/SourceOperatorWatermarksTest.java    | 95 ++++++++++++++++++++++
 6 files changed, 128 insertions(+), 17 deletions(-)
 create mode 100644 flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorWatermarksTest.java


(flink) 04/04: [FLINKCC-1283] Properly initialize initial splits in WatermarkOutputMultiplexer

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a commit to branch release-1.18
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 4848a50d73563d14d479437d64854e1da7f06b47
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Wed May 15 15:57:34 2024 +0200

    [FLINKCC-1283] Properly initialize initial splits in WatermarkOutputMultiplexer
    
    Without this fix, initial splits were registered in the multiplexer only
    when first record from that split has been emitted. This was leading to
    incorrectly emitted watermarks, as resulting watermark was not properly
    combined from the initial splits, but only from the splits that have
    already emitted at least one record.
---
 .../streaming/api/operators/SourceOperator.java    |  1 +
 .../api/operators/SourceOperatorAlignmentTest.java |  2 +-
 .../operators/SourceOperatorWatermarksTest.java    | 95 ++++++++++++++++++++++
 3 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/SourceOperator.java b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/SourceOperator.java
index c4f624e443c..ae61cfc670c 100644
--- a/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/SourceOperator.java
+++ b/flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/SourceOperator.java
@@ -341,6 +341,7 @@ public class SourceOperator<OUT, SplitT extends SourceSplit> extends AbstractStr
         final List<SplitT> splits = CollectionUtil.iterableToList(readerState.get());
         if (!splits.isEmpty()) {
             LOG.info("Restoring state for {} split(s) to reader.", splits.size());
+            outputPendingSplits.addAll(splits);
             sourceReader.addSplits(splits);
         }
 
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorAlignmentTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorAlignmentTest.java
index dc029c272d8..db3f8f0ed1f 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorAlignmentTest.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorAlignmentTest.java
@@ -311,7 +311,7 @@ class SourceOperatorAlignmentTest {
         assertThat(events).isEmpty();
     }
 
-    private static class PunctuatedGenerator implements WatermarkGenerator<Integer> {
+    static class PunctuatedGenerator implements WatermarkGenerator<Integer> {
 
         private enum GenerationMode {
             ALL,
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorWatermarksTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorWatermarksTest.java
new file mode 100644
index 00000000000..6906c755517
--- /dev/null
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorWatermarksTest.java
@@ -0,0 +1,95 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package org.apache.flink.streaming.api.operators;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.connector.source.mocks.MockSourceSplit;
+import org.apache.flink.streaming.api.operators.source.CollectingDataOutput;
+import org.apache.flink.streaming.api.watermark.Watermark;
+import org.apache.flink.streaming.runtime.io.DataInputStatus;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Unit test for {@link SourceOperator} watermark alignment. */
+@SuppressWarnings("serial")
+class SourceOperatorWatermarksTest {
+
+    @Nullable private SourceOperatorTestContext context;
+    @Nullable private SourceOperator<Integer, MockSourceSplit> operator;
+
+    @BeforeEach
+    void setup() throws Exception {
+        context =
+                new SourceOperatorTestContext(
+                        false,
+                        true,
+                        WatermarkStrategy.forGenerator(
+                                        ctx ->
+                                                new SourceOperatorAlignmentTest
+                                                        .PunctuatedGenerator())
+                                .withTimestampAssigner((r, t) -> r));
+        operator = context.getOperator();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        context.close();
+        context = null;
+        operator = null;
+    }
+
+    @Test
+    void testWatermark() throws Exception {
+        List<MockSourceSplit> initialSplits = new ArrayList<>();
+        initialSplits.add(new MockSourceSplit(0, 1000).addRecord(1042).addRecord(1044));
+        initialSplits.add(new MockSourceSplit(1, 1000).addRecord(42).addRecord(44));
+        operator.initializeState(context.createStateContext(initialSplits));
+        operator.open();
+
+        CollectingDataOutput<Integer> actualOutput = new CollectingDataOutput<>();
+
+        // after emitting first element from first split, there can not be watermark emitted, as
+        // watermark from the other split is still unknown.
+        assertThat(operator.emitNext(actualOutput)).isEqualTo(DataInputStatus.MORE_AVAILABLE);
+        assertNoWatermarks(actualOutput);
+
+        // after emitting two more elements (in this order: [1042, 1044, 42] but order doesn't
+        // matter for this test), three in total, watermark 42 can be finally emitted
+        assertThat(operator.emitNext(actualOutput)).isEqualTo(DataInputStatus.MORE_AVAILABLE);
+        assertThat(operator.emitNext(actualOutput)).isEqualTo(DataInputStatus.MORE_AVAILABLE);
+        assertWatermark(actualOutput, new Watermark(42));
+    }
+
+    private static void assertNoWatermarks(CollectingDataOutput<Integer> actualOutput) {
+        assertThat(actualOutput.getEvents()).noneMatch(element -> element instanceof Watermark);
+    }
+
+    private void assertWatermark(CollectingDataOutput<Integer> actualOutput, Watermark watermark) {
+        assertThat(actualOutput.getEvents()).containsOnlyOnce(watermark);
+    }
+}


(flink) 02/04: [hotfix] Allow configuring initial splits in SourceOperatorTestContext

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a commit to branch release-1.18
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 3f043b8bbb312b1a5028478178bee8fef3ea53ef
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Wed May 15 17:27:28 2024 +0200

    [hotfix] Allow configuring initial splits in SourceOperatorTestContext
---
 .../api/operators/SourceOperatorTestContext.java     | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
index 7f83e7304af..0d93e3ee54c 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
@@ -45,7 +45,9 @@ import org.apache.flink.streaming.util.MockOutput;
 import org.apache.flink.streaming.util.MockStreamConfig;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 
 import static org.apache.flink.util.Preconditions.checkState;
 
@@ -115,10 +117,18 @@ public class SourceOperatorTestContext implements AutoCloseable {
     }
 
     public StateInitializationContext createStateContext() throws Exception {
-        // Create a mock split.
-        byte[] serializedSplitWithVersion =
-                SimpleVersionedSerialization.writeVersionAndSerialize(
-                        new MockSourceSplitSerializer(), MOCK_SPLIT);
+        return createStateContext(Collections.singletonList(MOCK_SPLIT));
+    }
+
+    public StateInitializationContext createStateContext(Collection<MockSourceSplit> initialSplits)
+            throws Exception {
+
+        List<byte[]> serializedSplits = new ArrayList<>();
+        for (MockSourceSplit initialSplit : initialSplits) {
+            serializedSplits.add(
+                    SimpleVersionedSerialization.writeVersionAndSerialize(
+                            new MockSourceSplitSerializer(), initialSplit));
+        }
 
         // Crate the state context.
         OperatorStateStore operatorStateStore = createOperatorStateStore();
@@ -129,7 +139,7 @@ public class SourceOperatorTestContext implements AutoCloseable {
         stateContext
                 .getOperatorStateStore()
                 .getListState(SourceOperator.SPLITS_STATE_DESC)
-                .update(Collections.singletonList(serializedSplitWithVersion));
+                .update(serializedSplits);
 
         return stateContext;
     }


(flink) 01/04: [hotfix] Make MockSourceSplit#addRecord calls chainable

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a commit to branch release-1.18
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 114e6b2e27a246ae4d0f3411858f237262f10d2d
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Wed May 15 17:17:59 2024 +0200

    [hotfix] Make MockSourceSplit#addRecord calls chainable
---
 .../org/apache/flink/api/connector/source/mocks/MockSourceSplit.java   | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceSplit.java b/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceSplit.java
index c2578596809..c45add729cd 100644
--- a/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceSplit.java
+++ b/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceSplit.java
@@ -84,10 +84,11 @@ public class MockSourceSplit implements SourceSplit, Serializable {
     }
 
     /** Add a record to this split. */
-    public void addRecord(int record) {
+    public MockSourceSplit addRecord(int record) {
         if (!records.offer(record)) {
             throw new IllegalStateException("Failed to add record to split.");
         }
+        return this;
     }
 
     @Override


(flink) 03/04: [hotfix] Allow to use per split output via SourceOperatorTestContext

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a commit to branch release-1.18
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 2a60769eb168fa067944986c50a6d75880e9b4a5
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Wed May 15 17:31:03 2024 +0200

    [hotfix] Allow to use per split output via SourceOperatorTestContext
---
 .../flink/api/connector/source/mocks/MockSourceReader.java | 10 +---------
 .../streaming/api/operators/SourceOperatorTestContext.java | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceReader.java b/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceReader.java
index ce5660bd30f..2c1d4b40867 100644
--- a/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceReader.java
+++ b/flink-core/src/test/java/org/apache/flink/api/connector/source/mocks/MockSourceReader.java
@@ -61,15 +61,7 @@ public class MockSourceReader implements SourceReader<Integer, MockSourceSplit>
     private CompletableFuture<Void> availableFuture;
 
     public MockSourceReader() {
-        this(false, false);
-    }
-
-    public MockSourceReader(boolean waitingForMoreSplits, boolean markIdleOnNoSplits) {
-        this(
-                waitingForMoreSplits
-                        ? WaitingForSplits.WAIT_UNTIL_ALL_SPLITS_ASSIGNED
-                        : WaitingForSplits.DO_NOT_WAIT_FOR_SPLITS,
-                markIdleOnNoSplits);
+        this(WaitingForSplits.DO_NOT_WAIT_FOR_SPLITS, false);
     }
 
     public MockSourceReader(
diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
index 0d93e3ee54c..4dd3641e7c0 100644
--- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
+++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/SourceOperatorTestContext.java
@@ -73,8 +73,20 @@ public class SourceOperatorTestContext implements AutoCloseable {
 
     public SourceOperatorTestContext(boolean idle, WatermarkStrategy<Integer> watermarkStrategy)
             throws Exception {
+        this(idle, false, watermarkStrategy);
+    }
+
+    public SourceOperatorTestContext(
+            boolean idle, boolean usePerSplitOutputs, WatermarkStrategy<Integer> watermarkStrategy)
+            throws Exception {
 
-        mockSourceReader = new MockSourceReader(idle, idle);
+        mockSourceReader =
+                new MockSourceReader(
+                        idle
+                                ? MockSourceReader.WaitingForSplits.WAIT_UNTIL_ALL_SPLITS_ASSIGNED
+                                : MockSourceReader.WaitingForSplits.DO_NOT_WAIT_FOR_SPLITS,
+                        idle,
+                        usePerSplitOutputs);
         mockGateway = new MockOperatorEventGateway();
         timeService = new TestProcessingTimeService();
         operator =