npawar commented on a change in pull request #6661: URL: https://github.com/apache/incubator-pinot/pull/6661#discussion_r630559861
########## File path: pinot-plugins/pinot-stream-ingestion/pinot-kinesis/src/main/java/org/apache/pinot/plugin/stream/kinesis/KinesisStreamMetadataProvider.java ########## @@ -0,0 +1,177 @@ +/** + * 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.pinot.plugin.stream.kinesis; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import org.apache.commons.lang3.StringUtils; +import org.apache.pinot.spi.stream.MessageBatch; +import org.apache.pinot.spi.stream.OffsetCriteria; +import org.apache.pinot.spi.stream.PartitionGroupConsumer; +import org.apache.pinot.spi.stream.PartitionGroupConsumptionStatus; +import org.apache.pinot.spi.stream.PartitionGroupMetadata; +import org.apache.pinot.spi.stream.StreamConfig; +import org.apache.pinot.spi.stream.StreamConsumerFactory; +import org.apache.pinot.spi.stream.StreamConsumerFactoryProvider; +import org.apache.pinot.spi.stream.StreamMetadataProvider; +import org.apache.pinot.spi.stream.StreamPartitionMsgOffset; +import software.amazon.awssdk.services.kinesis.model.Shard; + + +/** + * A {@link StreamMetadataProvider} implementation for the Kinesis stream + */ +public class KinesisStreamMetadataProvider implements StreamMetadataProvider { + private final KinesisConnectionHandler _kinesisConnectionHandler; + private final StreamConsumerFactory _kinesisStreamConsumerFactory; + private final String _clientId; + private final int _fetchTimeoutMs; + + public KinesisStreamMetadataProvider(String clientId, StreamConfig streamConfig) { + KinesisConfig kinesisConfig = new KinesisConfig(streamConfig); + _kinesisConnectionHandler = new KinesisConnectionHandler(kinesisConfig.getStream(), kinesisConfig.getAwsRegion()); + _kinesisStreamConsumerFactory = StreamConsumerFactoryProvider.create(streamConfig); + _clientId = clientId; + _fetchTimeoutMs = streamConfig.getFetchTimeoutMillis(); + } + + public KinesisStreamMetadataProvider(String clientId, StreamConfig streamConfig, + KinesisConnectionHandler kinesisConnectionHandler, StreamConsumerFactory streamConsumerFactory) { + KinesisConfig kinesisConfig = new KinesisConfig(streamConfig); + _kinesisConnectionHandler = kinesisConnectionHandler; + _kinesisStreamConsumerFactory = streamConsumerFactory; + _clientId = clientId; + _fetchTimeoutMs = streamConfig.getFetchTimeoutMillis(); + } + + @Override + public int fetchPartitionCount(long timeoutMillis) { + throw new UnsupportedOperationException(); + } + + @Override + public long fetchPartitionOffset(@Nonnull OffsetCriteria offsetCriteria, long timeoutMillis) { + throw new UnsupportedOperationException(); + } + + /** + * This call returns all active shards, taking into account the consumption status for those shards. + * {@link PartitionGroupMetadata} is returned for a shard if: + * 1. It is a branch new shard AND its parent has been consumed completely + * 2. It is still being actively consumed from i.e. the consuming partition has not reached the end of the shard + */ + @Override + public List<PartitionGroupMetadata> computePartitionGroupMetadata(String clientId, StreamConfig streamConfig, + List<PartitionGroupConsumptionStatus> partitionGroupConsumptionStatuses, int timeoutMillis) + throws IOException, TimeoutException { + + List<PartitionGroupMetadata> newPartitionGroupMetadatas = new ArrayList<>(); + + Map<String, Shard> shardIdToShardMap = _kinesisConnectionHandler.getShards().stream() + .collect(Collectors.toMap(Shard::shardId, s -> s, (s1, s2) -> s1)); + Set<String> shardsInCurrent = new HashSet<>(); + Set<String> shardsEnded = new HashSet<>(); + + // TODO: Once we start supporting multiple shards in a PartitionGroup, + // we need to iterate over all shards to check if any of them have reached end + + // Process existing shards. Add them to new list if still consuming from them + for (PartitionGroupConsumptionStatus currentPartitionGroupConsumptionStatus : partitionGroupConsumptionStatuses) { + KinesisPartitionGroupOffset kinesisStartCheckpoint = + (KinesisPartitionGroupOffset) currentPartitionGroupConsumptionStatus.getStartOffset(); + String shardId = kinesisStartCheckpoint.getShardToStartSequenceMap().keySet().iterator().next(); + Shard shard = shardIdToShardMap.get(shardId); + shardsInCurrent.add(shardId); + + StreamPartitionMsgOffset newStartOffset; + StreamPartitionMsgOffset currentEndOffset = currentPartitionGroupConsumptionStatus.getEndOffset(); + if (currentEndOffset != null) { // Segment DONE (committing/committed) + String endingSequenceNumber = shard.sequenceNumberRange().endingSequenceNumber(); + if (endingSequenceNumber != null) { // Shard has ended, check if we're also done consuming it + if (consumedEndOfShard(currentEndOffset, currentPartitionGroupConsumptionStatus)) { + shardsEnded.add(shardId); + continue; // Shard ended and we're done consuming it. Skip + } + } + newStartOffset = currentEndOffset; + } else { // Segment IN_PROGRESS + newStartOffset = currentPartitionGroupConsumptionStatus.getStartOffset(); + } + newPartitionGroupMetadatas.add( + new PartitionGroupMetadata(currentPartitionGroupConsumptionStatus.getPartitionGroupId(), newStartOffset)); + } + + // Add new shards. Parent should be null (new table case, very first shards) + // OR it should be flagged as reached EOL and completely consumed. + for (Map.Entry<String, Shard> entry : shardIdToShardMap.entrySet()) { + String newShardId = entry.getKey(); + if (shardsInCurrent.contains(newShardId)) { + continue; + } + StreamPartitionMsgOffset newStartOffset; + Shard newShard = entry.getValue(); + String parentShardId = newShard.parentShardId(); + + if (parentShardId == null || shardsEnded.contains(parentShardId)) { Review comment: great catch! yes this is possible. Let me see if there's an easy fix -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org