baiyangtx commented on code in PR #3924:
URL: https://github.com/apache/amoro/pull/3924#discussion_r2544751933


##########
amoro-ams/src/main/java/org/apache/amoro/server/process/ProcessService.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.amoro.server.process;
+
+import org.apache.amoro.AmoroTable;
+import org.apache.amoro.TableFormat;
+import org.apache.amoro.TableRuntime;
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.config.TableConfiguration;
+import org.apache.amoro.process.ProcessStatus;
+import org.apache.amoro.process.TableProcess;
+import org.apache.amoro.process.TableProcessState;
+import org.apache.amoro.server.manager.AbstractPluginManager;
+import org.apache.amoro.server.optimizing.OptimizingStatus;
+import org.apache.amoro.server.persistence.PersistentBase;
+import org.apache.amoro.server.persistence.mapper.TableProcessMapper;
+import org.apache.amoro.server.process.executor.EngineType;
+import org.apache.amoro.server.process.executor.ExecuteEngine;
+import org.apache.amoro.server.process.executor.ExecuteOption;
+import org.apache.amoro.server.process.executor.TableProcessExecutor;
+import org.apache.amoro.server.table.RuntimeHandlerChain;
+import org.apache.amoro.server.table.TableService;
+import 
org.apache.amoro.shade.guava32.com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class ProcessService extends PersistentBase {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ProcessService.class);
+  private final TableService tableService;
+
+  private final Map<String, ActionCoordinatorScheduler> actionCoordinators =
+      new ConcurrentHashMap<>();
+  private final Map<EngineType, ExecuteEngine> executeEngines = new 
ConcurrentHashMap<>();
+
+  private final ActionCoordinatorManager actionCoordinatorManager;
+  private final ExecuteEngineManager executeEngineManager;
+  private final ProcessRuntimeHandler tableRuntimeHandler = new 
ProcessRuntimeHandler();
+  private final ThreadPoolExecutor processExecutionPool =
+      new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new 
LinkedBlockingQueue<>());
+
+  private final TableProcessTracker tableProcessTracker = new 
TableProcessTracker();
+
+  public ProcessService(Configurations serviceConfig, TableService 
tableService) {
+    this(serviceConfig, tableService, new ActionCoordinatorManager(), new 
ExecuteEngineManager());
+  }
+
+  public ProcessService(
+      Configurations serviceConfig,
+      TableService tableService,
+      ActionCoordinatorManager actionCoordinatorManager,
+      ExecuteEngineManager executeEngineManager) {
+    this.tableService = tableService;
+    this.actionCoordinatorManager = actionCoordinatorManager;
+    this.executeEngineManager = executeEngineManager;
+    this.tableProcessTracker.configure(serviceConfig.toMap());
+  }
+
+  public RuntimeHandlerChain getTableHandlerChain() {
+    return tableRuntimeHandler;
+  }
+
+  public void register(TableProcess<TableProcessState> process) {
+    synchronized (process) {
+      persistProcess(process, false);
+      executeOrTraceProcess(process);
+    }
+  }
+
+  public void recover(TableProcess<TableProcessState> process) {
+    synchronized (process) {
+      // TODO: init some status
+      persistProcess(process, true);
+      executeOrTraceProcess(process);
+    }
+  }
+
+  public void retry(TableProcess<TableProcessState> process) {
+    synchronized (process) {
+      // TODO: init some status
+      persistProcess(process, true);
+      executeOrTraceProcess(process);
+    }
+  }
+
+  public void cancel(TableProcess<TableProcessState> process) {
+    synchronized (process) {
+      // TODO: init some status
+      cancelProcess(process);
+    }
+  }
+
+  public void dispose() {
+    actionCoordinatorManager.close();
+    executeEngineManager.close();
+    processExecutionPool.shutdown();
+    tableProcessTracker.close();
+  }
+
+  private void initialize(List<TableRuntime> tableRuntimes) {
+    LOG.info("Initializing process service");
+    actionCoordinatorManager.initialize();
+    actionCoordinatorManager
+        .installedPlugins()
+        .forEach(
+            actionCoordinator -> {
+              actionCoordinators.put(
+                  actionCoordinator.action().getName(),
+                  new ActionCoordinatorScheduler(
+                      actionCoordinator, tableService, ProcessService.this));
+            });
+    executeEngineManager.initialize();
+    executeEngineManager
+        .installedPlugins()
+        .forEach(
+            executeEngine -> {
+              executeEngines.put(executeEngine.engineType(), executeEngine);
+            });
+    recoverProcesses(tableRuntimes);
+    actionCoordinators.values().forEach(s -> s.initialize(tableRuntimes));
+  }
+
+  @VisibleForTesting
+  public void recoverProcesses(List<TableRuntime> tableRuntimes) {
+    Map<Long, TableRuntime> tableIdToRuntimes =
+        tableRuntimes.stream()
+            .collect(Collectors.toMap(t -> t.getTableIdentifier().getId(), t 
-> t));
+    List<TableProcessMeta> activeProcesses =
+        getAs(TableProcessMapper.class, 
TableProcessMapper::selectAllActiveProcesses);
+    activeProcesses.forEach(
+        processMeta -> {
+          TableRuntime tableRuntime = 
tableIdToRuntimes.get(processMeta.getTableId());
+          ActionCoordinatorScheduler scheduler =
+              actionCoordinators.get(processMeta.getProcessType());
+          if (tableRuntime != null && scheduler != null) {
+            scheduler.recover(tableRuntime, processMeta);
+          }
+        });
+  }
+
+  private void executeOrTraceProcess(TableProcess<TableProcessState> process) {
+
+    if (!isExecutable(process)) {
+      LOG.info(
+          "Table process {} with identifier {} may have been in canceling or 
canceled, cancel execute process.",
+          process.getId(),
+          process.getExternalProcessIdentifier());
+      return;
+    }
+
+    ExecuteEngine executeEngine =
+        
executeEngines.get(EngineType.valueOf(process.getState().getExecutionEngine()));
+
+    TableProcessExecutor executor =
+        new TableProcessExecutor(process, executeEngine, ExecuteOption.RUN);
+    executor.onProcessFinished(
+        () -> {
+          ActionCoordinatorScheduler scheduler =
+              actionCoordinators.get(process.getState().getAction().getName());
+          if (scheduler != null
+              && process.getStatus() == ProcessStatus.FAILED
+              && process.getState().getRetryNumber() < 
scheduler.PROCESS_MAX_RETRY_NUMBER
+              && process.getTableRuntime() != null) {
+            process.getState().addRetryNumber();

Review Comment:
   use TableProcessStore. 



-- 
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]

Reply via email to