voonhous commented on code in PR #18961: URL: https://github.com/apache/hudi/pull/18961#discussion_r3820925940
########## hudi-common/src/main/java/org/apache/hudi/common/avro/VariantShreddingRuntime.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.avro; + +import org.apache.hudi.common.util.Option; + +import lombok.extern.slf4j.Slf4j; + +/** + * Classpath detection of engine-specific variant shredding components. + * + * <p>Engine modules (currently the Spark 4.x bundles) ship implementations of + * {@link VariantShreddingProvider} and {@link VariantShreddingSchemaInferrer}; hudi-common + * discovers them by probing well-known class names so that it stays free of engine + * dependencies. Probes are memoized: classpath content does not change within a JVM.</p> + */ +@Slf4j +public final class VariantShreddingRuntime { + + /** Provider candidates, most specific first. Mirrors what each Spark bundle ships. */ + private static final String[] PROVIDER_CANDIDATES = { + "org.apache.hudi.variant.Spark4VariantShreddingProvider" + }; + + /** + * Inferrer candidates, one per Spark version module that ships one (inference exists only in + * Spark 4.1+, SPARK-53659), most recent first. Each spark4.x profile builds only its own + * version module, so every version that should infer needs its own entry here: a runtime whose + * module is missing from this list silently writes unshredded. + */ + private static final String[] INFERRER_CANDIDATES = { + "org.apache.hudi.variant.Spark42VariantShreddingSchemaInferrer", + "org.apache.hudi.variant.Spark41VariantShreddingSchemaInferrer" + }; + + private static final Option<String> PROVIDER_CLASS = probe(PROVIDER_CANDIDATES); + private static final Option<VariantShreddingSchemaInferrer> INFERRER = loadInferrer(); + + private VariantShreddingRuntime() { + } + + /** + * The fully-qualified name of the first {@link VariantShreddingProvider} implementation + * found on the classpath, if any. + */ + public static Option<String> getProviderClass() { + return PROVIDER_CLASS; + } + + /** + * A shared {@link VariantShreddingSchemaInferrer} instance from the classpath, if any. + * Implementations are stateless and thread-safe by contract, so one instance is shared. + * Tests also use this as the capability probe to filter inference tests to classpaths + * that ship an inferrer. + */ + public static Option<VariantShreddingSchemaInferrer> lookupInferrer() { + return INFERRER; + } + + /** + * Both probes run from this class's static initializer, so nothing here may let an error + * escape: an escaping {@link LinkageError} would fail {@code <clinit>} and every later use + * (including {@link #getProviderClass()} on the main Avro write path) would see a bare + * "Could not initialize class" with the original cause lost. Candidates are therefore loaded + * WITHOUT initialization (no static initializer of theirs runs here), and every Review Comment: Doc fixed in 5c2b9eed298e: the no-initialization sentence is scoped to `probe`, with a note that `loadInferrer` does initialize the candidate it instantiates. The catch stays as is: a throwing static initializer surfaces as `ExceptionInInitializerError`, which is a `LinkageError` and already caught and latched; `Throwable` would also swallow VirtualMachineErrors. -- 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]
