hlteoh37 commented on code in PR #49:
URL: 
https://github.com/apache/flink-connector-aws/pull/49#discussion_r1193446492


##########
flink-connector-aws-kinesis-streams/src/main/java/org/apache/flink/connector/kinesis/source/enumerator/assigner/UniformShardAssigner.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.connector.kinesis.source.enumerator.assigner;
+
+import org.apache.flink.annotation.Internal;
+import 
org.apache.flink.connector.kinesis.source.enumerator.KinesisShardAssigner;
+import org.apache.flink.connector.kinesis.source.split.KinesisShardSplit;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+/** An implementation of the {@link KinesisShardAssigner} that assigns splits 
uniformly. */
+@Internal
+public class UniformShardAssigner implements KinesisShardAssigner {
+    @Override
+    public int assign(KinesisShardSplit split, Context context) {
+        int selectedSubtask = -1;
+        int curMinAssignment = Integer.MAX_VALUE;
+        Map<Integer, Set<KinesisShardSplit>> splitAssignment = 
context.getCurrentSplitAssignment();
+
+        for (int subtaskId : context.getRegisteredReaders().keySet()) {
+            int subtaskAssignmentSize =
+                    splitAssignment.getOrDefault(subtaskId, 
Collections.emptySet()).size();
+            if (subtaskAssignmentSize < curMinAssignment) {
+                curMinAssignment = subtaskAssignmentSize;
+                selectedSubtask = subtaskId;
+            }
+        }
+
+        Preconditions.checkArgument(
+                selectedSubtask != -1,
+                "Expected at least one registered reader. Unable to assign 
split.");
+        return selectedSubtask;

Review Comment:
   That is correct. Thanks for finding the bug! Turns out it's because shards 
discovered in a batch does not consider the other shards already assigned 
within the same batch. I thought about providing a "batch assign" method in the 
`KinesisShardAssigner` interface, but thought that it would be a little extra, 
because we would have to first loop through to remove completed shards, then we 
would need to loop through again to assign them shards.
   
   Do you think we should add a batch `assign` interface instead?



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