jonvex commented on code in PR #9558: URL: https://github.com/apache/hudi/pull/9558#discussion_r1309020323
########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/TableServicePipeline.java: ########## @@ -0,0 +1,40 @@ +/* + * + * * 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.multitable; + +import java.util.ArrayList; +import java.util.List; + +public class TableServicePipeline { Review Comment: javadoc ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/MultiTableServiceUtils.java: ########## @@ -0,0 +1,166 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.client.common.HoodieSparkEngineContext; +import org.apache.hudi.common.config.SerializableConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.exception.HoodieException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.spark.api.java.JavaSparkContext; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Collectors; + +import static org.apache.hudi.common.table.HoodieTableMetaClient.METAFOLDER_NAME; + +public class MultiTableServiceUtils { + + public static class Constants { + public static final String TABLES_TO_BE_INGESTED_PROP = "hoodie.tableservice.tablesToServe"; + + public static final String COMMA_SEPARATOR = ","; + + public static final String LOCAL_SPARK_MASTER = "local[2]"; Review Comment: not sure if we want this hardcoded ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/HoodieMultiTableServicesMain.java: ########## @@ -0,0 +1,249 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.exception.HoodieIOException; +import org.apache.hudi.utilities.HoodieCompactor; +import org.apache.hudi.utilities.IdentitySplitter; +import org.apache.hudi.utilities.UtilHelpers; +import org.apache.hudi.utilities.streamer.HoodieStreamer; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.spark.api.java.JavaSparkContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.apache.hudi.utilities.multitable.MultiTableServiceUtils.Constants.LOCAL_SPARK_MASTER; + +public class HoodieMultiTableServicesMain { + private static final Logger LOG = LoggerFactory.getLogger(HoodieStreamer.class); + final Config cfg; + final TypedProperties props; + + private final JavaSparkContext jsc; + + private ScheduledExecutorService executorService; + + private void batchRunTableServices(List<String> tablePaths) throws InterruptedException, ExecutionException { + ExecutorService executorService = Executors.newFixedThreadPool(cfg.poolSize); Review Comment: do you want to create a local variable here? I think no, so you can use your cancel method ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/ClusteringTask.java: ########## @@ -0,0 +1,97 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.utilities.HoodieClusteringJob; + +import org.apache.spark.api.java.JavaSparkContext; + +class ClusteringTask extends TableServiceTask { Review Comment: javadocs ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/CompactionTask.java: ########## @@ -0,0 +1,100 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.table.action.compact.strategy.LogFileSizeBasedCompactionStrategy; +import org.apache.hudi.utilities.HoodieCompactor; + +import org.apache.spark.api.java.JavaSparkContext; + +class CompactionTask extends TableServiceTask { Review Comment: javadocs ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/CleanTask.java: ########## @@ -0,0 +1,79 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.utilities.HoodieCleaner; +import org.apache.hudi.utilities.UtilHelpers; + +import org.apache.spark.api.java.JavaSparkContext; + +class CleanTask extends TableServiceTask { Review Comment: javadocs ########## hudi-utilities/src/main/java/org/apache/hudi/utilities/multitable/HoodieMultiTableServicesMain.java: ########## @@ -0,0 +1,249 @@ +/* + * + * * 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.multitable; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.exception.HoodieIOException; +import org.apache.hudi.utilities.HoodieCompactor; +import org.apache.hudi.utilities.IdentitySplitter; +import org.apache.hudi.utilities.UtilHelpers; +import org.apache.hudi.utilities.streamer.HoodieStreamer; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.spark.api.java.JavaSparkContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.apache.hudi.utilities.multitable.MultiTableServiceUtils.Constants.LOCAL_SPARK_MASTER; + +public class HoodieMultiTableServicesMain { + private static final Logger LOG = LoggerFactory.getLogger(HoodieStreamer.class); + final Config cfg; + final TypedProperties props; + + private final JavaSparkContext jsc; + + private ScheduledExecutorService executorService; + + private void batchRunTableServices(List<String> tablePaths) throws InterruptedException, ExecutionException { + ExecutorService executorService = Executors.newFixedThreadPool(cfg.poolSize); + List<CompletableFuture<Void>> futures = tablePaths.stream() + .map(basePath -> CompletableFuture.runAsync( + () -> MultiTableServiceUtils.buildTableServicePipeline(jsc, basePath, cfg, props).execute(), + executorService)) + .collect(Collectors.toList()); + CompletableFuture<?> allComplete = + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); + CompletableFuture<?> anyException = new CompletableFuture<>(); + for (CompletableFuture<?> future : futures) { + future.exceptionally((e) -> { + anyException.completeExceptionally(e); + return null; + }); + } + try { + CompletableFuture.anyOf(allComplete, anyException).get(); + } catch (ExecutionException ee) { + throw new ExecutionException("some table service failed", ee); + } finally { + executorService.shutdownNow(); + } + } + + private void streamRunTableServices(List<String> tablePaths) throws InterruptedException { + executorService = Executors.newScheduledThreadPool(cfg.poolSize); + for (String tablePath : tablePaths) { + TableServicePipeline pipeline = MultiTableServiceUtils.buildTableServicePipeline(jsc, tablePath, cfg, props); + executorService.scheduleAtFixedRate(pipeline::execute, 0, cfg.scheduleDelay, TimeUnit.MILLISECONDS); + } + executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES); + } + + public void cancel() { + if (executorService != null) { + executorService.shutdown(); + } + } + + public HoodieMultiTableServicesMain(JavaSparkContext jsc, Config cfg) { + this.cfg = cfg; + this.jsc = jsc; + this.props = cfg.propsFilePath == null ? UtilHelpers.buildProperties(cfg.configs) : readConfigFromFileSystem(jsc, cfg); + } + + public void startServices() throws ExecutionException, InterruptedException { + LOG.info("StartServices Config: " + cfg); + List<String> tablePaths; + if (cfg.autoDiscovering) { + // We support defining multi base paths + tablePaths = cfg.basePath.stream() + .filter(this::pathExists) + .flatMap(p -> MultiTableServiceUtils.findHoodieTablesUnderPath(jsc, p).stream()) + .collect(Collectors.toList()); + } else { + tablePaths = MultiTableServiceUtils.getTablesToBeIngestedFromProps(props); + } + LOG.info("All table paths: " + String.join(",", tablePaths)); + if (cfg.batch) { + batchRunTableServices(tablePaths); + } else { + streamRunTableServices(tablePaths); + } + } + + private TypedProperties readConfigFromFileSystem(JavaSparkContext jsc, Config cfg) { + return UtilHelpers.readConfig(jsc.hadoopConfiguration(), new Path(cfg.propsFilePath), cfg.configs).getProps(true); + } + + private boolean pathExists(String path) { + try { + Path p = new Path(path); + FileSystem fs = p.getFileSystem(jsc.hadoopConfiguration()); + return fs.exists(p); + } catch (IOException e) { + throw new HoodieIOException("Error checking path existing:", e); + } + } + + /** + * Command line configs to run table services + */ + public static class Config implements Serializable { + @Parameter(names = {"--base-path"}, description = "Base path for all the tables, this can be repeated", + required = true, splitter = IdentitySplitter.class) + public List<String> basePath = Collections.emptyList(); + + @Parameter(names = {"--auto", "-a"}, description = "Whether to discover hudi tables in the base path") Review Comment: maybe "--auto-discovery"? -- 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]
