linliu-code commented on code in PR #18224: URL: https://github.com/apache/hudi/pull/18224#discussion_r2912857114
########## hudi-utilities/src/main/java/org/apache/hudi/utilities/sources/helpers/KinesisOffsetGen.java: ########## @@ -0,0 +1,500 @@ +/* + * 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.hudi.utilities.sources.helpers; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.table.checkpoint.Checkpoint; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.utilities.exception.HoodieReadFromSourceException; +import org.apache.hudi.utilities.config.KinesisSourceConfig; +import org.apache.hudi.utilities.ingestion.HoodieIngestionMetrics; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.kinesis.KinesisClient; +import software.amazon.awssdk.services.kinesis.KinesisClientBuilder; +import software.amazon.awssdk.services.kinesis.model.ExpiredIteratorException; +import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest; +import software.amazon.awssdk.services.kinesis.model.GetRecordsResponse; +import software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest; +import software.amazon.awssdk.services.kinesis.model.InvalidArgumentException; +import software.amazon.awssdk.services.kinesis.model.LimitExceededException; +import software.amazon.awssdk.services.kinesis.model.ListShardsRequest; +import software.amazon.awssdk.services.kinesis.model.ListShardsResponse; +import software.amazon.awssdk.services.kinesis.model.ProvisionedThroughputExceededException; +import software.amazon.awssdk.services.kinesis.model.ResourceNotFoundException; +import software.amazon.awssdk.services.kinesis.model.Record; +import software.amazon.awssdk.services.kinesis.model.Shard; +import software.amazon.awssdk.services.kinesis.model.ShardIteratorType; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import static org.apache.hudi.common.util.ConfigUtils.checkRequiredConfigProperties; +import static org.apache.hudi.common.util.ConfigUtils.getBooleanWithAltKeys; +import static org.apache.hudi.common.util.ConfigUtils.getLongWithAltKeys; +import static org.apache.hudi.common.util.ConfigUtils.getStringWithAltKeys; + +/** + * Helper for reading from Kinesis Data Streams and managing checkpoints. + * Checkpoint format: streamName,shardId:sequenceNumber,shardId:sequenceNumber,... + */ +@Slf4j +@Getter +public class KinesisOffsetGen { + + public static class CheckpointUtils { + /** Separator between lastSeq and endSeq for closed shards. Seq numbers are numeric, so this is safe. */ + private static final String END_SEQ_SEPARATOR = "|"; + /** + * Kinesis checkpoint pattern. + * Format: streamName,shardId:lastSeq,shardId:lastSeq|endSeq,... + * For closed shards we store lastSeq|endSeq so we can detect data loss when shard expires. + */ + private static final Pattern PATTERN = Pattern.compile(".*,.*:.*"); + + /** + * Parse checkpoint string to shardId -> value map. Value is lastSeq or lastSeq|endSeq for closed shards. + */ + public static Map<String, String> strToOffsets(String checkpointStr) { + Map<String, String> offsetMap = new HashMap<>(); + String[] splits = checkpointStr.split(","); + for (int i = 1; i < splits.length; i++) { + String part = splits[i]; + int colonIdx = part.indexOf(':'); + if (colonIdx > 0 && colonIdx < part.length() - 1) { + String shardId = part.substring(0, colonIdx); + String value = part.substring(colonIdx + 1); + offsetMap.put(shardId, value); + } + } + return offsetMap; + } + + /** + * Extract lastSeq from checkpoint value (which may be "lastSeq" or "lastSeq|endSeq"). + */ + public static String getLastSeqFromValue(String value) { + if (value == null || value.isEmpty()) { + return value; + } + int sep = value.indexOf(END_SEQ_SEPARATOR); + return sep >= 0 ? value.substring(0, sep) : value; + } + + /** + * Extract endSeq from checkpoint value if present. Returns null for open shards. + */ + public static String getEndSeqFromValue(String value) { + if (value == null || value.isEmpty()) { + return null; + } + int sep = value.indexOf(END_SEQ_SEPARATOR); + return sep >= 0 && sep < value.length() - 1 ? value.substring(sep + 1) : null; + } + + /** + * Build checkpoint value: "lastSeq" or "lastSeq|endSeq" when endSeq is present (closed shards). + */ + public static String buildCheckpointValue(String lastSeq, String endSeq) { + if (endSeq != null && !endSeq.isEmpty()) { + return lastSeq + END_SEQ_SEPARATOR + endSeq; + } + return lastSeq; + } + + /** + * String representation of checkpoint. + * Format: streamName,shardId:value,shardId:value,... where value is lastSeq or lastSeq|endSeq. + */ + public static String offsetsToStr(String streamName, Map<String, String> shardToValue) { + String parts = shardToValue.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .map(e -> e.getKey() + ":" + e.getValue()) + .collect(Collectors.joining(",")); + return streamName + "," + parts; + } + + public static boolean checkStreamCheckpoint(Option<String> lastCheckpointStr) { + return lastCheckpointStr.isPresent() && PATTERN.matcher(lastCheckpointStr.get()).matches(); + } + } + + /** LocalStack returns Long.MAX_VALUE for closed shards' endingSequenceNumber; real AWS returns actual value. */ + public static final String LOCALSTACK_END_SEQ_SENTINEL = "9223372036854775807"; + Review Comment: done. -- 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]
