pnowojski commented on code in PR #29658:
URL: https://github.com/apache/flink/pull/29658#discussion_r4412077327
##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/sorted/state/BatchExecutionKeyedStateBackend.java:
##########
@@ -104,8 +104,27 @@ public BatchExecutionKeyedStateBackend(
this.executionConfig = executionConfig;
}
+ /**
+ * Only allowed for the current key. Keys are processed one at a time, in
sorted order, and may
+ * only advance via {@link #moveToNextKey}: switching to another key here
would fire the
+ * outgoing key's timers early and drop its state, or revisit a key whose
state is already gone.
+ */
@Override
public void setCurrentKey(K newKey) {
+ if (!Objects.equals(newKey, currentKey)) {
Review Comment:
Thanks for checking. Agreed, in BATCH `applyToAllPartitions` already read
empty state for every key but the current one, so failing is better than
silently wrong results. I'll mention this in the PR description as a behaviour
change.
##########
flink-runtime/src/test/java/org/apache/flink/streaming/api/operators/sorted/state/BatchExecutionKeyOrderTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.sorted.state;
+
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * The single-key-at-a-time {@link BatchExecutionKeyedStateBackend} only moves
forward through the
+ * sorted keys. Switching back to an earlier key would silently fire its
timers early and lose its
+ * state, so it must fail loudly.
+ */
+class BatchExecutionKeyOrderTest {
+
+ @Test
+ void testInputPathMovesThroughKeys() throws Exception {
+ try (KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Integer>
harness =
+ createBatchHarness(new KeyRevisitingOperator(false))) {
+ harness.open();
+ for (int key = 1; key <= 3; key++) {
+ harness.processElement(new StreamRecord<>(key));
+ harness.processElement(new StreamRecord<>(key));
+ }
+ assertThat(harness.extractOutputValues()).containsExactly(1, 1, 2,
2, 3, 3);
+ }
+ }
+
+ @Test
+ void testOperatorSwitchingBackToPreviousKeyFails() throws Exception {
+ try (KeyedOneInputStreamOperatorTestHarness<Integer, Integer, Integer>
harness =
+ createBatchHarness(new KeyRevisitingOperator(true))) {
+ harness.open();
+ harness.processElement(new StreamRecord<>(1));
+ assertThatThrownBy(() -> harness.processElement(new
StreamRecord<>(2)))
+ .hasStackTraceContaining("BATCH execution");
Review Comment:
Good point, done in 6625d54277a: the assertion now checks for "Cannot switch
the current key from 2 to 1".
##########
flink-python/src/main/java/org/apache/flink/streaming/api/operators/python/AbstractPythonFunctionOperator.java:
##########
@@ -219,6 +219,12 @@ public void setCurrentKey(Object key) {
super.setCurrentKey(key);
}
+ @Override
+ public void moveToNextKey(Object key) {
+ processElementsOfCurrentKeyIfNeeded(key);
Review Comment:
Thanks, I agree with your analysis: `invokeFinishBundle()` doesn't yield to
the mailbox, so the timer mails run only after the key has moved on. Fixed in
fa46d8d29dc by calling `drainUnregisteredTimers()` right after
`invokeFinishBundle()`, while the previous key is still current. Like you, I've
only traced this; I haven't run a PyFlink BATCH job with timers.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]