gguptp commented on code in PR #146: URL: https://github.com/apache/flink-connector-aws/pull/146#discussion_r1662903372
########## flink-connector-aws/flink-connector-dynamodb/src/main/java/org/apache/flink/connector/dynamodb/source/enumerator/DynamoDbStreamsSourceEnumerator.java: ########## @@ -0,0 +1,332 @@ +/* + * 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.dynamodb.source.enumerator; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.connector.source.ReaderInfo; +import org.apache.flink.api.connector.source.SplitEnumerator; +import org.apache.flink.api.connector.source.SplitEnumeratorContext; +import org.apache.flink.api.connector.source.SplitsAssignment; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.dynamodb.source.config.DynamodbStreamsSourceConfigConstants.InitialPosition; +import org.apache.flink.connector.dynamodb.source.exception.DynamoDbStreamsSourceException; +import org.apache.flink.connector.dynamodb.source.proxy.StreamProxy; +import org.apache.flink.connector.dynamodb.source.split.DynamoDbStreamsShardSplit; +import org.apache.flink.connector.dynamodb.source.split.StartingPosition; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.services.dynamodb.model.Shard; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import static org.apache.flink.connector.dynamodb.source.config.DynamodbStreamsSourceConfigConstants.SHARD_DISCOVERY_INTERVAL_MILLIS; +import static org.apache.flink.connector.dynamodb.source.config.DynamodbStreamsSourceConfigConstants.STREAM_INITIAL_POSITION; + +/** + * This class is used to discover and assign DynamoDb Streams splits to subtasks on the Flink cluster. This + * runs on the JobManager. + */ +@Internal +public class DynamoDbStreamsSourceEnumerator + implements SplitEnumerator<DynamoDbStreamsShardSplit, DynamoDbStreamsSourceEnumeratorState> { + + private static final Logger LOG = LoggerFactory.getLogger(DynamoDbStreamsSourceEnumerator.class); + + private final SplitEnumeratorContext<DynamoDbStreamsShardSplit> context; + private final String streamArn; + private final Configuration sourceConfig; + private final StreamProxy streamProxy; + private final DynamoDbStreamsShardAssigner shardAssigner; + private final ShardAssignerContext shardAssignerContext; + + private final Map<Integer, Set<DynamoDbStreamsShardSplit>> splitAssignment = new HashMap<>(); + private final Set<String> assignedSplitIds = new HashSet<>(); + private final Set<DynamoDbStreamsShardSplit> unassignedSplits; + + private String lastSeenShardId; + + public DynamoDbStreamsSourceEnumerator( + SplitEnumeratorContext<DynamoDbStreamsShardSplit> context, + String streamArn, + Configuration sourceConfig, + StreamProxy streamProxy, + DynamoDbStreamsShardAssigner shardAssigner, + DynamoDbStreamsSourceEnumeratorState state) { + this.context = context; + this.streamArn = streamArn; + this.sourceConfig = sourceConfig; + this.streamProxy = streamProxy; + this.shardAssigner = shardAssigner; + this.shardAssignerContext = new ShardAssignerContext(splitAssignment, context); + if (state == null) { + this.lastSeenShardId = null; + this.unassignedSplits = new HashSet<>(); + } else { + this.lastSeenShardId = state.getLastSeenShardId(); + this.unassignedSplits = state.getUnassignedSplits(); + } + } + + @Override + public void start() { + if (lastSeenShardId == null) { + context.callAsync(this::initialDiscoverSplits, this::assignSplits); + } + + final long shardDiscoveryInterval = sourceConfig.get(SHARD_DISCOVERY_INTERVAL_MILLIS); + context.callAsync( + this::periodicallyDiscoverSplits, + this::assignSplits, + shardDiscoveryInterval, + shardDiscoveryInterval); + } + + @Override + public void handleSplitRequest(int subtaskId, @Nullable String requesterHostname) { + // Do nothing, since we assign splits eagerly + } + + @Override + public void addSplitsBack(List<DynamoDbStreamsShardSplit> splits, int subtaskId) { + if (!splitAssignment.containsKey(subtaskId)) { + LOG.warn( + "Unable to add splits back for subtask {} since it is not assigned any splits. Splits: {}", + subtaskId, + splits); + return; + } + + for (DynamoDbStreamsShardSplit split : splits) { + splitAssignment.get(subtaskId).remove(split); + assignedSplitIds.remove(split.splitId()); + unassignedSplits.add(split); + } + + // Assign the unassignedSplits + // We did not discover any new splits, so we put in an empty list + assignSplits(Collections.emptyList(), null); + } + + @Override + public void addReader(int subtaskId) { + splitAssignment.putIfAbsent(subtaskId, new HashSet<>()); + } + + @Override + public DynamoDbStreamsSourceEnumeratorState snapshotState(long checkpointId) throws Exception { + return new DynamoDbStreamsSourceEnumeratorState(unassignedSplits, lastSeenShardId); + } + + @Override + public void close() throws IOException { + streamProxy.close(); + } + + private List<DynamoDbStreamsShardSplit> initialDiscoverSplits() { + List<Shard> shards = streamProxy.listShards(streamArn, lastSeenShardId); + return mapToSplits(shards, sourceConfig.get(STREAM_INITIAL_POSITION)); + } + + /** + * This method is used to discover DynamoDb Streams splits the job can subscribe to. It can be run in + * parallel, is important to not mutate any shared state. + * + * @return list of discovered splits + */ + private List<DynamoDbStreamsShardSplit> periodicallyDiscoverSplits() { + List<Shard> shards = streamProxy.listShards(streamArn, lastSeenShardId); + + // Any shard discovered after the initial startup should be read from the start, since they + // come from resharding + return mapToSplits(shards, InitialPosition.TRIM_HORIZON); + } + + private List<DynamoDbStreamsShardSplit> mapToSplits( + List<Shard> shards, InitialPosition initialPosition) { + StartingPosition startingPosition; + switch (initialPosition) { + case LATEST: + //TODO: Figure out how to handle this for DDB Streams + // For dynamodb streams If LATEST is requested, we still set the starting position to the time of + // startup. This way, the job starts reading from a deterministic timestamp + // (i.e. time of job submission), even if it enters a restart loop immediately + // after submission. + startingPosition = StartingPosition.latest(); Review Comment: Good catch. It needs to change. Will do -- 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