z3d1k commented on code in PR #145: URL: https://github.com/apache/flink-connector-aws/pull/145#discussion_r1672525373
########## flink-connector-aws/flink-connector-aws-kinesis-streams/src/main/java/org/apache/flink/connector/kinesis/source/enumerator/tracker/SplitTracker.java: ########## @@ -0,0 +1,165 @@ +/* + * 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.tracker; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.kinesis.source.enumerator.KinesisShardSplitWithAssignmentStatus; +import org.apache.flink.connector.kinesis.source.enumerator.SplitAssignmentStatus; +import org.apache.flink.connector.kinesis.source.split.KinesisShardSplit; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** This class is used to track shard hierarchy. */ +@Internal +public class SplitTracker { + /** + * Flag controlling if tracker should wait before all parent splits will be completed before + * assigning split to readers. + */ + private final boolean preserveShardOrdering; + + /** Map of all discovered splits that have not been completed. */ + private final Map<String, KinesisShardSplit> knownSplits = new ConcurrentHashMap<>(); + + /** Set of currently assigned split id. */ + private final Set<String> assignedSplits = new HashSet<>(); + + public SplitTracker(boolean preserveShardOrdering) { + this(preserveShardOrdering, Collections.emptyList()); + } + + public SplitTracker( + boolean preserveShardOrdering, + List<KinesisShardSplitWithAssignmentStatus> initialState) { + this.preserveShardOrdering = preserveShardOrdering; + + initialState.forEach( + splitWithStatus -> { + knownSplits.put(splitWithStatus.split().splitId(), splitWithStatus.split()); + if (SplitAssignmentStatus.ASSIGNED.equals(splitWithStatus.assignmentStatus())) { + assignedSplits.add(splitWithStatus.split().splitId()); + } + }); + } + + /** + * Add newly discovered splits to tracker. + * + * @param splitsToAdd collection of splits to add to tracking + */ + public void addSplits(Collection<KinesisShardSplit> splitsToAdd) { + splitsToAdd.forEach(split -> knownSplits.put(split.splitId(), split)); + } + + /** + * Mark splits as assigned. Assigned splits will no longer be returned as pending splits. + * + * @param splitsToAssign collection of splits to mark as assigned + */ + public void markAsAssigned(Collection<KinesisShardSplit> splitsToAssign) { + splitsToAssign.forEach(split -> assignedSplits.add(split.splitId())); + } + + /** + * Mark splits with specified ids as finished. + * + * @param finishedSplitIds collection of split ids to mark as finished + */ + public void markAsFinished(Collection<String> finishedSplitIds) { Review Comment: Added tests to verify that no exception is thrown in this case. This will be a no-op. ########## flink-connector-aws/flink-connector-aws-kinesis-streams/src/main/java/org/apache/flink/connector/kinesis/source/enumerator/tracker/SplitTracker.java: ########## @@ -0,0 +1,165 @@ +/* + * 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.tracker; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.kinesis.source.enumerator.KinesisShardSplitWithAssignmentStatus; +import org.apache.flink.connector.kinesis.source.enumerator.SplitAssignmentStatus; +import org.apache.flink.connector.kinesis.source.split.KinesisShardSplit; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** This class is used to track shard hierarchy. */ +@Internal +public class SplitTracker { + /** + * Flag controlling if tracker should wait before all parent splits will be completed before + * assigning split to readers. + */ + private final boolean preserveShardOrdering; + + /** Map of all discovered splits that have not been completed. */ + private final Map<String, KinesisShardSplit> knownSplits = new ConcurrentHashMap<>(); + + /** Set of currently assigned split id. */ + private final Set<String> assignedSplits = new HashSet<>(); + + public SplitTracker(boolean preserveShardOrdering) { + this(preserveShardOrdering, Collections.emptyList()); + } + + public SplitTracker( + boolean preserveShardOrdering, + List<KinesisShardSplitWithAssignmentStatus> initialState) { + this.preserveShardOrdering = preserveShardOrdering; + + initialState.forEach( + splitWithStatus -> { + knownSplits.put(splitWithStatus.split().splitId(), splitWithStatus.split()); + if (SplitAssignmentStatus.ASSIGNED.equals(splitWithStatus.assignmentStatus())) { + assignedSplits.add(splitWithStatus.split().splitId()); + } + }); + } + + /** + * Add newly discovered splits to tracker. + * + * @param splitsToAdd collection of splits to add to tracking + */ + public void addSplits(Collection<KinesisShardSplit> splitsToAdd) { + splitsToAdd.forEach(split -> knownSplits.put(split.splitId(), split)); + } + + /** + * Mark splits as assigned. Assigned splits will no longer be returned as pending splits. + * + * @param splitsToAssign collection of splits to mark as assigned + */ + public void markAsAssigned(Collection<KinesisShardSplit> splitsToAssign) { + splitsToAssign.forEach(split -> assignedSplits.add(split.splitId())); + } Review Comment: Added tests to verify that no exception is thrown in this case. This will be a no-op. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org