ashokkumar-allu commented on code in PR #18585: URL: https://github.com/apache/hudi/pull/18585#discussion_r3399875574
########## hudi-common/src/main/java/org/apache/hudi/common/engine/HoodiePreCommitValidatorEngineContext.java: ########## @@ -0,0 +1,252 @@ +/* + * 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.engine; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.data.HoodieAccumulator; +import org.apache.hudi.common.data.HoodieData; +import org.apache.hudi.common.data.HoodieData.HoodieDataCacheKey; +import org.apache.hudi.common.data.HoodiePairData; +import org.apache.hudi.common.function.FunctionWrapper; +import org.apache.hudi.common.function.SerializableBiFunction; +import org.apache.hudi.common.function.SerializableConsumer; +import org.apache.hudi.common.function.SerializableFunction; +import org.apache.hudi.common.function.SerializablePairFlatMapFunction; +import org.apache.hudi.common.function.SerializablePairFunction; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.util.Functions; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.ImmutablePair; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.keygen.KeyGenerator; +import org.apache.hudi.storage.StorageConfiguration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ForkJoinPool; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * An engine context for running pre-commit validators in parallel. + * + * <p>Uses a dedicated classloader-aware {@link ExecutorService} so that validator classes loaded + * by the application classloader can be resolved inside worker threads on Java 11+. All methods + * except {@link #map} are delegated to an internal {@link HoodieLocalEngineContext} instance. + * The pool is lazily created once per JVM and shared across all instances. + */ +public class HoodiePreCommitValidatorEngineContext extends HoodieEngineContext { Review Comment: Done — renamed to `ExecutorServiceBasedEngineContext`. Agreed it's a better name: the class is generic, not coupled to pre-commit validation, and can be reused anywhere a classloader-safe parallel engine context is needed. ########## hudi-common/src/main/java/org/apache/hudi/common/engine/ExecutorServiceBasedEngineContext.java: ########## @@ -0,0 +1,252 @@ +/* + * 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.engine; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.data.HoodieAccumulator; +import org.apache.hudi.common.data.HoodieData; +import org.apache.hudi.common.data.HoodieData.HoodieDataCacheKey; +import org.apache.hudi.common.data.HoodiePairData; +import org.apache.hudi.common.function.FunctionWrapper; +import org.apache.hudi.common.function.SerializableBiFunction; +import org.apache.hudi.common.function.SerializableConsumer; +import org.apache.hudi.common.function.SerializableFunction; +import org.apache.hudi.common.function.SerializablePairFlatMapFunction; +import org.apache.hudi.common.function.SerializablePairFunction; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.util.Functions; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.ImmutablePair; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.keygen.KeyGenerator; +import org.apache.hudi.storage.StorageConfiguration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ForkJoinPool; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * An engine context for running pre-commit validators in parallel. + * + * <p>Uses a dedicated classloader-aware {@link ExecutorService} so that validator classes loaded + * by the application classloader can be resolved inside worker threads on Java 11+. All methods + * except {@link #map} are delegated to an internal {@link HoodieLocalEngineContext} instance. + * The pool is lazily created once per JVM and shared across all instances. + */ +public class HoodiePreCommitValidatorEngineContext extends HoodieEngineContext { + + private static final Logger LOG = LoggerFactory.getLogger(HoodiePreCommitValidatorEngineContext.class); + + // Lazy-initialized fixed thread pool whose workers explicitly carry the classloader of this + // class, preventing ClassNotFoundException on Java 11+ where thread-pool workers do not inherit + // the submitting thread's context classloader. + // JLS 12.4.2 guarantees thread-safe initialization via class-loading locks. + private static class PoolHolder { + static final ExecutorService INSTANCE = createExecutorService(); + } + + private static ExecutorService createExecutorService() { + int parallelism = ForkJoinPool.commonPool().getParallelism(); + ClassLoader cl = HoodiePreCommitValidatorEngineContext.class.getClassLoader(); + ExecutorService executor = Executors.newFixedThreadPool(parallelism, r -> { + Thread t = Executors.defaultThreadFactory().newThread(r); + t.setContextClassLoader(cl); + t.setDaemon(true); + return t; + }); + LOG.info("Created pre-commit validator executor service with {} threads", parallelism); + return executor; + } + + // Delegate for all methods except map() — avoids inheriting from final HoodieLocalEngineContext. + private final HoodieLocalEngineContext delegate; + + public HoodiePreCommitValidatorEngineContext(StorageConfiguration<?> conf) { + super(conf, new LocalTaskContextSupplier()); + this.delegate = new HoodieLocalEngineContext(conf); + } + + /** + * Runs {@code func} over {@code data} in parallel using a classloader-aware fixed thread pool. + * Results are returned in input order. Unchecked exceptions are re-thrown as-is; checked + * exceptions are wrapped in {@link org.apache.hudi.exception.HoodieException}. + */ + @Override + public <I, O> List<O> map(List<I> data, SerializableFunction<I, O> func, int parallelism) { + return mapAsync(data, FunctionWrapper.throwingMapWrapper(func)); + } + + private <I, O> List<O> mapAsync(List<I> data, Function<I, O> func) { + List<CompletableFuture<O>> futures = data.stream() + .map(item -> CompletableFuture.supplyAsync(() -> func.apply(item), PoolHolder.INSTANCE)) + .collect(Collectors.toList()); + try { + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); + } catch (CompletionException e) { + throw rethrowUnwrapped(e); + } + return futures.stream().map(CompletableFuture::join).collect(Collectors.toList()); + } + + private static RuntimeException rethrowUnwrapped(CompletionException e) { + Throwable cause = e.getCause(); + if (cause instanceof RuntimeException) { + throw (RuntimeException) cause; + } + if (cause instanceof Error) { + throw (Error) cause; + } + throw e; + } + + // ---- All remaining methods delegate to HoodieLocalEngineContext ---- + + @Override + public HoodieAccumulator newAccumulator() { + return delegate.newAccumulator(); + } + + @Override + public <T> HoodieData<T> emptyHoodieData() { + return delegate.emptyHoodieData(); + } + + @Override + public <K, V> HoodiePairData<K, V> emptyHoodiePairData() { + return delegate.emptyHoodiePairData(); + } + + @Override + public <T> HoodieData<T> parallelize(List<T> data, int parallelism) { + return delegate.parallelize(data, parallelism); + } + + @Override + public <I, K, V> List<V> mapToPairAndReduceByKey(List<I> data, SerializablePairFunction<I, K, V> mapToPairFunc, + SerializableBiFunction<V, V, V> reduceFunc, int parallelism) { Review Comment: Done — all parallel operations now use the executor service directly with no delegation to `HoodieLocalEngineContext`: - `map`, `flatMap`, `mapToPair`, `mapToPairAndReduceByKey` — one `CompletableFuture` per element via a shared `mapAsync` helper - `foreach` — one `runAsync` per element - `reduceByKey` — groups by key sequentially, then reduces each group in parallel via `mapAsync` - `mapPartitionsToPairAndReduceByKey` — single `supplyAsync` so the function runs on an executor thread with the correct classloader The `HoodieLocalEngineContext` delegate field is also gone — non-parallel methods (`newAccumulator`, `parallelize`, cache/status no-ops, etc.) are implemented directly. -- 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]
