hudi-agent commented on code in PR #18405: URL: https://github.com/apache/hudi/pull/18405#discussion_r3237271129
########## hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/validator/SparkKafkaOffsetValidator.java: ########## @@ -0,0 +1,59 @@ +/* + * 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.streamer.validator; + +import org.apache.hudi.client.validator.StreamingOffsetValidator; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.table.checkpoint.StreamerCheckpointV1; +import org.apache.hudi.common.util.CheckpointUtils.CheckpointFormat; + +/** + * Spark/HoodieStreamer-specific Kafka offset validator. + * + * <p>Validates that the number of records written matches the Kafka offset difference + * between the current and previous HoodieStreamer checkpoints. Uses the Spark Kafka + * checkpoint format stored with key {@code deltastreamer.checkpoint.key} in extraMetadata.</p> + * + * <p>Configuration: + * <ul> + * <li>{@code hoodie.precommit.validators}: Include + * {@code org.apache.hudi.utilities.streamer.validator.SparkKafkaOffsetValidator}</li> + * <li>{@code hoodie.precommit.validators.streaming.offset.tolerance.percentage}: + * Acceptable deviation (default: 0.0 = strict)</li> + * <li>{@code hoodie.precommit.validators.failure.policy}: + * FAIL (default) or WARN_LOG</li> + * </ul></p> + * + * <p>This validator is primarily intended for append-only ingestion from Kafka via HoodieStreamer. + * For upsert workloads with deduplication, configure a higher tolerance or use WARN_LOG.</p> + * + * <p><b>Important:</b> This class extends {@link org.apache.hudi.client.validator.BasePreCommitValidator} + * and is invoked by {@link SparkStreamerValidatorUtils}, NOT by {@code SparkValidatorUtils} + * (which expects {@code SparkPreCommitValidator} with a different constructor signature). + * Listing this class in {@code hoodie.precommit.validators} while also using the standard + * Spark table write-path validators will cause an instantiation failure in {@code SparkValidatorUtils}. + * Use this validator exclusively with HoodieStreamer pipelines.</p> + */ +public class SparkKafkaOffsetValidator extends StreamingOffsetValidator { + + public SparkKafkaOffsetValidator(TypedProperties config) { + super(config, StreamerCheckpointV1.STREAMER_CHECKPOINT_KEY_V1, CheckpointFormat.SPARK_KAFKA); Review Comment: 🤖 This hardcodes `STREAMER_CHECKPOINT_KEY_V1`, but `CheckpointUtils.shouldTargetCheckpointV2` is true for `writeTableVersion >= 8` (Hudi 1.0+), and steady-state V2 streamers only write `streamer.checkpoint.key.v2` (no V1 fallback unless during a V1→V2 transition via `addV1Props`). On a v8+ table the validator would find neither current nor previous V1 key in commit metadata and silently skip — turning into a no-op for the configuration most new users will hit. Could the validator look for the V2 key (or both) so it works for both table versions? @yihua for visibility, since this affects whether this validator is useful on Hudi 1.0+ tables. <sub><i>- AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> ########## hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/client/utils/SparkValidatorUtils.java: ########## @@ -84,9 +84,28 @@ public static void runValidators(HoodieWriteConfig config, Dataset<Row> beforeState = getRecordsFromCommittedFiles(sqlContext, partitionsModified, table, afterState.schema()); Stream<SparkPreCommitValidator> validators = Arrays.stream(config.getPreCommitValidators().split(",")) - .map(validatorClass -> ((SparkPreCommitValidator) ReflectionUtils.loadClass(validatorClass, - new Class<?>[] {HoodieSparkTable.class, HoodieEngineContext.class, HoodieWriteConfig.class}, - table, context, config))); + .map(String::trim) + .filter(s -> !s.isEmpty()) + .flatMap(validatorClass -> { Review Comment: 🤖 nit: this flatMap lambda has grown to ~16 lines doing class loading, type checking, reflection and two distinct exception translations. Could you extract it into a small private helper like `instantiateSparkValidator(String validatorClass, HoodieSparkTable table, HoodieEngineContext context, HoodieWriteConfig config)` returning `Stream<SparkPreCommitValidator>`? Would make the stream pipeline read at a glance. <sub><i>- AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> -- 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]
