shangxinli commented on code in PR #18068: URL: https://github.com/apache/hudi/pull/18068#discussion_r2880668721
########## hudi-common/src/main/java/org/apache/hudi/client/validator/ValidationContext.java: ########## @@ -0,0 +1,179 @@ +/* + * 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.client.validator; + +import org.apache.hudi.ApiMaturityLevel; +import org.apache.hudi.PublicAPIClass; +import org.apache.hudi.PublicAPIMethod; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.util.Option; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * Provides validators with access to commit information. + * Engine-specific implementations (Spark, Flink, Java) provide concrete implementations + * for the core methods; computed convenience methods are provided as defaults. + * + * <p>This interface abstracts away engine-specific details while providing consistent + * access to validation data across all write engines.</p> + * + * <p>Example implementations:</p> + * <ul> + * <li>SparkValidationContext (Phase 3): Accesses Spark RDD write metadata</li> + * <li>FlinkValidationContext (Phase 2): Accesses Flink checkpoint state</li> + * <li>JavaValidationContext (Future): Accesses Java client write metadata</li> + * </ul> + */ +@PublicAPIClass(maturity = ApiMaturityLevel.EVOLVING) +public interface ValidationContext { + + /** + * Get the current commit instant time being validated. + * + * @return Instant time string (format: yyyyMMddHHmmss) + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + String getInstantTime(); + + /** + * Get commit metadata for the current commit being validated. + * Contains extraMetadata (checkpoints, custom metadata), operation type, write stats, etc. + * + * @return Optional commit metadata + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + Option<HoodieCommitMetadata> getCommitMetadata(); + + /** + * Get write statistics for the current commit. + * Contains record counts, partition info, file info, bytes written, etc. + * + * @return Optional list of write statistics per partition + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + Option<List<HoodieWriteStat>> getWriteStats(); + + /** + * Get the active timeline for accessing previous commits. + * Used to navigate commit history and extract previous checkpoints. + * + * @return Active timeline + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + HoodieActiveTimeline getActiveTimeline(); + + /** + * Get the previous completed commit instant. + * Used to access previous checkpoint for delta validation. + * + * @return Optional previous instant + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + Option<HoodieInstant> getPreviousCommitInstant(); + + /** + * Get commit metadata for the previous commit. + * Used to extract previous checkpoint for comparison. + * + * @return Optional previous commit metadata + */ + @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING) + Option<HoodieCommitMetadata> getPreviousCommitMetadata(); + + // ========== Default convenience methods derived from core methods ========== + + /** + * Get all extra metadata from the current commit. + * Derived from {@link #getCommitMetadata()}. + * + * @return Map of metadata key to value, or empty map if no metadata + */ + default Map<String, String> getExtraMetadata() { + return getCommitMetadata() + .map(HoodieCommitMetadata::getExtraMetadata) + .orElse(Collections.emptyMap()); + } + + /** + * Get a specific extra metadata value by key. + * Derived from {@link #getCommitMetadata()}. + * + * @param key Metadata key + * @return Optional metadata value + */ + default Option<String> getExtraMetadata(String key) { + return getCommitMetadata() + .flatMap(metadata -> Option.ofNullable(metadata.getMetadata(key))); Review Comment: `flatMap` is intentional here. `getCommitMetadata()` returns `Option<HoodieCommitMetadata>`, and the lambda returns `Option<String>` (via `Option.ofNullable()`). Using `map` would produce `Option<Option<String>>`, which is not what we want. `flatMap` flattens this to `Option<String>`. ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, Review Comment: Good point — renamed to `SPARK_KAFKA` to align with the `HoodieStreamer` rename and be consistent with `FLINK_KAFKA`. Done. ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, + + /** Flink Kafka format: base64-encoded Map<TopicPartition, Long> */ + FLINK_KAFKA, + + /** Pulsar format: "0:123:45,1:234:56" (ledgerId:entryId) */ + PULSAR, Review Comment: Added a Javadoc on the enum explaining the rationale: Kafka formats are engine-prefixed because different engines (Spark, Flink) use different checkpoint serialization for the same source. Pulsar and Kinesis are source-specific and use the same format regardless of engine, so no engine prefix is needed for those. ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, Review Comment: Good call — I checked `KafkaOffsetGen` and both offset-based and timestamp-based Kafka sources produce the same checkpoint string format (`"topic,partition:offset,..."`). The offsets may originate from `consumer.endOffsets()` or `consumer.offsetsForTimes()`, but the serialized format is identical. Added this as a doc note on the `SPARK_KAFKA` enum and in the class-level Javadoc. ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, + + /** Flink Kafka format: base64-encoded Map<TopicPartition, Long> */ + FLINK_KAFKA, + + /** Pulsar format: "0:123:45,1:234:56" (ledgerId:entryId) */ + PULSAR, + + /** Kinesis format: "shard-0:12345,shard-1:67890" */ + KINESIS, + + /** Custom user-defined format */ + CUSTOM + } + + /** + * Parse checkpoint string into partition → offset mapping. + * + * @param format Checkpoint format + * @param checkpointStr Checkpoint string + * @return Map from partition number to offset/sequence number + * @throws IllegalArgumentException if format is invalid + */ + public static Map<Integer, Long> parseCheckpoint(CheckpointFormat format, String checkpointStr) { + switch (format) { + case DELTASTREAMER_KAFKA: + return parseDeltaStreamerKafkaCheckpoint(checkpointStr); + case FLINK_KAFKA: + throw new UnsupportedOperationException( + "Flink Kafka checkpoint parsing not yet implemented. " + + "This will be added in Phase 2 with Flink checkpoint deserialization support."); + case PULSAR: + throw new UnsupportedOperationException( + "Pulsar checkpoint parsing not yet implemented. Planned for Phase 4."); + case KINESIS: + throw new UnsupportedOperationException( + "Kinesis checkpoint parsing not yet implemented. Planned for Phase 4."); + default: + throw new IllegalArgumentException("Unsupported checkpoint format: " + format); + } + } + + /** + * Calculate offset difference between two checkpoints. + * Handles partition additions, removals, and resets. + * + * Algorithm: + * 1. For each partition in current checkpoint: + * - If partition exists in previous: diff = current - previous + * - If partition is new: diff = current (count from 0) + * - If diff is negative (reset): use current offset + * 2. Sum all partition diffs + * + * @param format Checkpoint format + * @param previousCheckpoint Previous checkpoint string + * @param currentCheckpoint Current checkpoint string + * @return Total offset difference across all partitions + */ + public static long calculateOffsetDifference(CheckpointFormat format, + String previousCheckpoint, + String currentCheckpoint) { + Map<Integer, Long> previousOffsets = parseCheckpoint(format, previousCheckpoint); + Map<Integer, Long> currentOffsets = parseCheckpoint(format, currentCheckpoint); + + long totalDiff = 0; + + for (Map.Entry<Integer, Long> entry : currentOffsets.entrySet()) { + int partition = entry.getKey(); + long currentOffset = entry.getValue(); + Long previousOffset = previousOffsets.get(partition); + + if (previousOffset != null) { + // Partition exists in both checkpoints + long diff = currentOffset - previousOffset; + + // Handle offset reset (negative diff) - topic/partition recreated + if (diff < 0) { + // Use current offset as diff (count from 0 to current) Review Comment: Added a `LOG.warn()` here that logs the partition number, previous offset, and current offset when a reset is detected. Done. ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, + + /** Flink Kafka format: base64-encoded Map<TopicPartition, Long> */ + FLINK_KAFKA, + + /** Pulsar format: "0:123:45,1:234:56" (ledgerId:entryId) */ + PULSAR, + + /** Kinesis format: "shard-0:12345,shard-1:67890" */ + KINESIS, + + /** Custom user-defined format */ + CUSTOM + } + + /** + * Parse checkpoint string into partition → offset mapping. + * + * @param format Checkpoint format + * @param checkpointStr Checkpoint string + * @return Map from partition number to offset/sequence number + * @throws IllegalArgumentException if format is invalid + */ + public static Map<Integer, Long> parseCheckpoint(CheckpointFormat format, String checkpointStr) { + switch (format) { + case DELTASTREAMER_KAFKA: + return parseDeltaStreamerKafkaCheckpoint(checkpointStr); + case FLINK_KAFKA: + throw new UnsupportedOperationException( + "Flink Kafka checkpoint parsing not yet implemented. " + + "This will be added in Phase 2 with Flink checkpoint deserialization support."); + case PULSAR: + throw new UnsupportedOperationException( + "Pulsar checkpoint parsing not yet implemented. Planned for Phase 4."); + case KINESIS: + throw new UnsupportedOperationException( + "Kinesis checkpoint parsing not yet implemented. Planned for Phase 4."); + default: + throw new IllegalArgumentException("Unsupported checkpoint format: " + format); + } + } + + /** + * Calculate offset difference between two checkpoints. + * Handles partition additions, removals, and resets. + * + * Algorithm: + * 1. For each partition in current checkpoint: + * - If partition exists in previous: diff = current - previous + * - If partition is new: diff = current (count from 0) + * - If diff is negative (reset): use current offset + * 2. Sum all partition diffs + * + * @param format Checkpoint format + * @param previousCheckpoint Previous checkpoint string + * @param currentCheckpoint Current checkpoint string + * @return Total offset difference across all partitions + */ + public static long calculateOffsetDifference(CheckpointFormat format, + String previousCheckpoint, + String currentCheckpoint) { + Map<Integer, Long> previousOffsets = parseCheckpoint(format, previousCheckpoint); + Map<Integer, Long> currentOffsets = parseCheckpoint(format, currentCheckpoint); + + long totalDiff = 0; Review Comment: Yes, there is overlap. Added a TODO in the Javadoc to consolidate with `KafkaOffsetGen.CheckpointUtils` once #18125 lands. The current duplication exists to avoid Kafka client dependencies in hudi-common (we return `Map<Integer, Long>` vs `Map<TopicPartition, Long>`). ########## hudi-common/src/main/java/org/apache/hudi/common/util/CheckpointUtils.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.common.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for parsing and working with streaming checkpoints. + * Supports multiple checkpoint formats used by different engines and sources. + * + * Checkpoint formats: + * - DELTASTREAMER_KAFKA: "topic,partition:offset,partition:offset,..." + * Example: "events,0:1000,1:2000,2:1500" + * Used by: DeltaStreamer (Spark) + * + * - FLINK_KAFKA: Base64-encoded serialized Map (TopicPartition → Long) + * Example: "eyJ0b3BpY..." (base64) + * Used by: Flink streaming connector + * Note: Actual implementation requires Flink checkpoint deserialization (Phase 2) + * + * - PULSAR: "partition:ledgerId:entryId,partition:ledgerId:entryId,..." + * Example: "0:123:45,1:234:56" + * Used by: Pulsar sources + * Note: To be implemented in Phase 4 + * + * - KINESIS: "shardId:sequenceNumber,shardId:sequenceNumber,..." + * Example: "shardId-000000000000:49590338271490256608559692538361571095921575989136588898" + * Used by: Kinesis sources + * Note: To be implemented in Phase 4 + */ +public class CheckpointUtils { + + /** + * Supported checkpoint formats across engines and sources. + */ + public enum CheckpointFormat { + /** DeltaStreamer (Spark) Kafka format: "topic,0:1000,1:2000" */ + DELTASTREAMER_KAFKA, + + /** Flink Kafka format: base64-encoded Map<TopicPartition, Long> */ + FLINK_KAFKA, + + /** Pulsar format: "0:123:45,1:234:56" (ledgerId:entryId) */ + PULSAR, + + /** Kinesis format: "shard-0:12345,shard-1:67890" */ + KINESIS, + + /** Custom user-defined format */ + CUSTOM + } + + /** + * Parse checkpoint string into partition → offset mapping. + * + * @param format Checkpoint format + * @param checkpointStr Checkpoint string + * @return Map from partition number to offset/sequence number + * @throws IllegalArgumentException if format is invalid + */ + public static Map<Integer, Long> parseCheckpoint(CheckpointFormat format, String checkpointStr) { + switch (format) { + case DELTASTREAMER_KAFKA: + return parseDeltaStreamerKafkaCheckpoint(checkpointStr); + case FLINK_KAFKA: + throw new UnsupportedOperationException( + "Flink Kafka checkpoint parsing not yet implemented. " + + "This will be added in Phase 2 with Flink checkpoint deserialization support."); + case PULSAR: + throw new UnsupportedOperationException( + "Pulsar checkpoint parsing not yet implemented. Planned for Phase 4."); + case KINESIS: + throw new UnsupportedOperationException( + "Kinesis checkpoint parsing not yet implemented. Planned for Phase 4."); + default: + throw new IllegalArgumentException("Unsupported checkpoint format: " + format); + } + } + + /** + * Calculate offset difference between two checkpoints. + * Handles partition additions, removals, and resets. + * + * Algorithm: + * 1. For each partition in current checkpoint: + * - If partition exists in previous: diff = current - previous + * - If partition is new: diff = current (count from 0) + * - If diff is negative (reset): use current offset + * 2. Sum all partition diffs + * + * @param format Checkpoint format + * @param previousCheckpoint Previous checkpoint string + * @param currentCheckpoint Current checkpoint string + * @return Total offset difference across all partitions + */ + public static long calculateOffsetDifference(CheckpointFormat format, + String previousCheckpoint, + String currentCheckpoint) { + Map<Integer, Long> previousOffsets = parseCheckpoint(format, previousCheckpoint); + Map<Integer, Long> currentOffsets = parseCheckpoint(format, currentCheckpoint); + + long totalDiff = 0; + + for (Map.Entry<Integer, Long> entry : currentOffsets.entrySet()) { + int partition = entry.getKey(); + long currentOffset = entry.getValue(); + Long previousOffset = previousOffsets.get(partition); + + if (previousOffset != null) { + // Partition exists in both checkpoints + long diff = currentOffset - previousOffset; + + // Handle offset reset (negative diff) - topic/partition recreated + if (diff < 0) { + // Use current offset as diff (count from 0 to current) + totalDiff += currentOffset; + } else { + totalDiff += diff; + } + } else { + // New partition - count from 0 to current + totalDiff += currentOffset; + } + } + + return totalDiff; + } + + /** + * Validate checkpoint format. + * + * @param format Expected checkpoint format + * @param checkpointStr Checkpoint string to validate + * @return true if valid format + */ + public static boolean isValidCheckpointFormat(CheckpointFormat format, String checkpointStr) { Review Comment: Reviewed all access specifiers. `parseCheckpoint`, `calculateOffsetDifference`, and `isValidCheckpointFormat` remain `public` since they are used cross-module by `StreamingOffsetValidator` (now in hudi-client-common). `extractTopicName` is now package-private since nothing outside the package uses it. `parseSparkKafkaCheckpoint` was already `private`. -- 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]
