rkhachatryan commented on code in PR #21362:
URL: https://github.com/apache/flink/pull/21362#discussion_r1030940479


##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/heap/InternalKeyContextImpl.java:
##########
@@ -72,6 +73,10 @@ public void setCurrentKey(@Nonnull K currentKey) {
 
     @Override
     public void setCurrentKeyGroupIndex(int currentKeyGroupIndex) {
+        if (!keyGroupRange.contains(currentKeyGroupIndex)) {
+            throw KeyGroupRangeOffsets.newIllegalKeyGroupException(
+                    currentKeyGroupIndex, keyGroupRange);
+        }

Review Comment:
   This is executed in the hot path, i.e. per record, right?
   But since `contains()` isn't heavy, I think it's fine to optimistically rely 
on micro-benchmarks to detect any regression.
   Nevertheless, should we avoid this call by checking if the new KeyGroupIndex 
is the same as the current one?



##########
flink-runtime/src/test/java/org/apache/flink/runtime/state/heap/InternalKeyContextImplTest.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.runtime.state.heap;
+
+import org.apache.flink.runtime.state.KeyGroupRange;
+
+import org.junit.Test;
+
+import static org.junit.Assert.fail;
+
+/** Tests for {@link InternalKeyContextImpl}. */
+public class InternalKeyContextImplTest {
+
+    @Test
+    public void testSetKeyGroupOutOfRange() {
+        InternalKeyContextImpl<Integer> integerInternalKeyContext =
+                new InternalKeyContextImpl<>(KeyGroupRange.of(0, 128), 4096);
+        integerInternalKeyContext.setCurrentKeyGroupIndex(64);
+        try {
+            integerInternalKeyContext.setCurrentKeyGroupIndex(2048);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }

Review Comment:
   nit: I'd split this test into two and use `@Test(expected = 
IllegalArgumentException.class)` instead of empty `catch+fail` for readabiliy.



##########
flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/co/CoBroadcastWithKeyedOperatorTest.java:
##########
@@ -556,9 +559,27 @@ public void testScaleUp() throws Exception {
                                 3,
                                 2,
                                 operatorSubtaskState3)) {
-            testHarness1.processElement1(new StreamRecord<>("trigger"));
-            testHarness2.processElement1(new StreamRecord<>("trigger"));
-            testHarness3.processElement1(new StreamRecord<>("trigger"));
+
+            // Since there is a keyed operator, we should follow the key 
partition rules.
+            Map<TwoInputStreamOperatorTestHarness<String, Integer, String>, 
KeyGroupRange>
+                    keyGroupPartition = new HashMap<>();
+            keyGroupPartition.put(testHarness1, KeyGroupRange.of(0, 3));
+            keyGroupPartition.put(testHarness2, KeyGroupRange.of(4, 6));
+            keyGroupPartition.put(testHarness3, KeyGroupRange.of(7, 9));
+            while (!keyGroupPartition.isEmpty()) {
+                String triggerKey = 
String.valueOf(ThreadLocalRandom.current().nextLong());
+                for (Map.Entry<
+                                TwoInputStreamOperatorTestHarness<String, 
Integer, String>,
+                                KeyGroupRange>
+                        entry : keyGroupPartition.entrySet()) {
+                    if (entry.getValue()
+                            
.contains(KeyGroupRangeAssignment.assignToKeyGroup(triggerKey, 10))) {
+                        entry.getKey().processElement1(new 
StreamRecord<>(triggerKey));
+                        keyGroupPartition.remove(entry.getKey());
+                        break;
+                    }
+                }
+            }

Review Comment:
   This part doesn't seem obvious to me; and mapping from harnesses to key 
ranges can break.
   How about this:
   ```
               for (TwoInputStreamOperatorTestHarness<String, Integer, String> 
harness :
                       Arrays.asList(testHarness1, testHarness2, testHarness3)) 
{
                   int subtask = 
harness.getEnvironment().getTaskInfo().getIndexOfThisSubtask();
                   // find the right input element for this subtask
                   int element = 0;
                   while 
(assignKeyToParallelOperator(Integer.toString(element), 10, 3) != subtask) {
                       element++;
                   }
                   harness.processElement1(new 
StreamRecord<>(Integer.toString(element)));
               }
   ```
   ?
   
   ditto: `testScaleDown` - extract a function?



-- 
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]

Reply via email to