You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/09/17 12:14:41 UTC

[GitHub] [incubator-seatunnel] Hisoka-X commented on a diff in pull request #2761: [Feature][ST-Engine] Add CoordinatorService & Coordinator can reinit when Master Node actived

Hisoka-X commented on code in PR #2761:
URL: https://github.com/apache/incubator-seatunnel/pull/2761#discussion_r973575917


##########
seatunnel-engine/seatunnel-engine-core/src/main/java/org/apache/seatunnel/engine/core/parse/ConnectorInstanceLoader.java:
##########
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.seatunnel.engine.client.job;
+package org.apache.seatunnel.engine.core.parse;

Review Comment:
   Why `ConnectorInstanceLoader` in package named `parse`?



##########
seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/TaskExecutionServiceTest.java:
##########
@@ -64,20 +64,21 @@ public void before() {
     @Test
     public void testAll() throws InterruptedException {
         logger.info("----------start Cancel test----------");
-        testCancel();
+        //testCancel();

Review Comment:
   Hi, why remove these method?



##########
seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/SeaTunnelServer.java:
##########
@@ -220,181 +146,31 @@ public LiveOperationRegistry getLiveOperationRegistry() {
         return liveOperationRegistry;
     }
 
-    /**
-     * Lazy load for resource manager
-     */
-    public ResourceManager getResourceManager() {
-        if (resourceManager == null) {
-            synchronized (this) {
-                if (resourceManager == null) {
-                    ResourceManager manager = new ResourceManagerFactory(nodeEngine).getResourceManager();
-                    manager.init();
-                    resourceManager = manager;
-                }
-            }
-        }
-        return resourceManager;
-    }
-
-    public TaskExecutionService getTaskExecutionService() {
-        return taskExecutionService;
-    }
-
-    /**
-     * call by client to submit job
-     */
-    public PassiveCompletableFuture<Void> submitJob(long jobId, Data jobImmutableInformation) {
-        CompletableFuture<Void> voidCompletableFuture = new CompletableFuture<>();
-        JobMaster jobMaster = new JobMaster(jobImmutableInformation,
-            this.nodeEngine,
-            executorService,
-            getResourceManager(),
-            runningJobStateIMap,
-            runningJobStateTimestampsIMap,
-            ownedSlotProfilesIMap);
-        executorService.submit(() -> {
-            try {
-                runningJobInfoIMap.put(jobId, new RunningJobInfo(System.currentTimeMillis(), jobImmutableInformation));
-                runningJobMasterMap.put(jobId, jobMaster);
-                jobMaster.init(runningJobInfoIMap.get(jobId).getInitializationTimestamp());
-                jobMaster.getPhysicalPlan().initStateFuture();
-            } catch (Throwable e) {
-                LOGGER.severe(String.format("submit job %s error %s ", jobId, ExceptionUtils.getMessage(e)));
-                voidCompletableFuture.completeExceptionally(e);
-            } finally {
-                // We specify that when init is complete, the submitJob is complete
-                voidCompletableFuture.complete(null);
-            }
-
+    @SuppressWarnings("checkstyle:MagicNumber")
+    public CoordinatorService getCoordinatorService() {
+        int retryCount = 0;
+        while (!coordinatorService.isCoordinatorActive() && retryCount < 20) {

Review Comment:
   Please add log in here, because this method could wait too long time.



##########
seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/CoordinatorServiceTest.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.seatunnel.engine.server;
+
+import static org.awaitility.Awaitility.await;
+
+import org.apache.seatunnel.engine.common.Constant;
+import org.apache.seatunnel.engine.core.dag.logical.LogicalDag;
+import org.apache.seatunnel.engine.core.job.JobImmutableInformation;
+import org.apache.seatunnel.engine.core.job.JobStatus;
+
+import com.hazelcast.instance.impl.HazelcastInstanceImpl;
+import com.hazelcast.internal.serialization.Data;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+
+public class CoordinatorServiceTest {
+    @Test
+    public void testMasterNodeActive() {
+        HazelcastInstanceImpl instance1 = TestUtils.createHazelcastInstance("CoordinatorServiceTest_testMasterNodeActive");
+        HazelcastInstanceImpl instance2 = TestUtils.createHazelcastInstance("CoordinatorServiceTest_testMasterNodeActive");
+
+        SeaTunnelServer server1 = instance1.node.getNodeEngine().getService(SeaTunnelServer.SERVICE_NAME);
+        SeaTunnelServer server2 = instance2.node.getNodeEngine().getService(SeaTunnelServer.SERVICE_NAME);
+
+        CoordinatorService coordinatorService1 = server1.getCoordinatorService();
+        Assert.assertTrue(coordinatorService1.isCoordinatorActive());
+

Review Comment:
   Please add `Assert.assertFalse(coordinatorService2.isCoordinatorActive());`



##########
seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/CoordinatorService.java:
##########
@@ -0,0 +1,416 @@
+/*
+ * 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.seatunnel.engine.server;
+
+import org.apache.seatunnel.common.utils.ExceptionUtils;
+import org.apache.seatunnel.engine.common.exception.JobException;
+import org.apache.seatunnel.engine.common.exception.SeaTunnelEngineException;
+import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture;
+import org.apache.seatunnel.engine.core.job.JobStatus;
+import org.apache.seatunnel.engine.core.job.RunningJobInfo;
+import org.apache.seatunnel.engine.server.dag.physical.PhysicalVertex;
+import org.apache.seatunnel.engine.server.dag.physical.PipelineLocation;
+import org.apache.seatunnel.engine.server.dag.physical.SubPlan;
+import org.apache.seatunnel.engine.server.execution.ExecutionState;
+import org.apache.seatunnel.engine.server.execution.TaskExecutionState;
+import org.apache.seatunnel.engine.server.execution.TaskGroupLocation;
+import org.apache.seatunnel.engine.server.master.JobMaster;
+import org.apache.seatunnel.engine.server.resourcemanager.ResourceManager;
+import org.apache.seatunnel.engine.server.resourcemanager.ResourceManagerFactory;
+import org.apache.seatunnel.engine.server.resourcemanager.resource.SlotProfile;
+
+import com.hazelcast.cluster.Address;
+import com.hazelcast.internal.serialization.Data;
+import com.hazelcast.internal.services.MembershipServiceEvent;
+import com.hazelcast.logging.ILogger;
+import com.hazelcast.map.IMap;
+import com.hazelcast.spi.impl.NodeEngineImpl;
+import lombok.NonNull;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+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;
+
+public class CoordinatorService {
+    private NodeEngineImpl nodeEngine;
+    private final ILogger logger;
+
+    private volatile ResourceManager resourceManager;
+
+    /**
+     * IMap key is jobId and value is a Tuple2
+     * Tuple2 key is JobMaster init timestamp and value is the jobImmutableInformation which is sent by client when submit job
+     * <p>
+     * This IMap is used to recovery runningJobInfoIMap in JobMaster when a new master node active
+     */
+    private IMap<Long, RunningJobInfo> runningJobInfoIMap;
+
+    /**
+     * IMap key is one of jobId {@link org.apache.seatunnel.engine.server.dag.physical.PipelineLocation} and
+     * {@link org.apache.seatunnel.engine.server.execution.TaskGroupLocation}
+     * <p>
+     * The value of IMap is one of {@link JobStatus} {@link org.apache.seatunnel.engine.core.job.PipelineState}
+     * {@link org.apache.seatunnel.engine.server.execution.ExecutionState}
+     * <p>
+     * This IMap is used to recovery runningJobStateIMap in JobMaster when a new master node active
+     */
+    IMap<Object, Object> runningJobStateIMap;
+
+    /**
+     * IMap key is one of jobId {@link org.apache.seatunnel.engine.server.dag.physical.PipelineLocation} and
+     * {@link org.apache.seatunnel.engine.server.execution.TaskGroupLocation}
+     * <p>
+     * The value of IMap is one of {@link org.apache.seatunnel.engine.server.dag.physical.PhysicalPlan} stateTimestamps
+     * {@link org.apache.seatunnel.engine.server.dag.physical.SubPlan} stateTimestamps
+     * {@link org.apache.seatunnel.engine.server.dag.physical.PhysicalVertex} stateTimestamps
+     * <p>
+     * This IMap is used to recovery runningJobStateTimestampsIMap in JobMaster when a new master node active
+     */
+    IMap<Object, Long[]> runningJobStateTimestampsIMap;
+
+    /**
+     * key: job id;
+     * <br> value: job master;
+     */
+    private Map<Long, JobMaster> runningJobMasterMap = new ConcurrentHashMap<>();
+
+    /**
+     * IMap key is {@link PipelineLocation}
+     * <p>
+     * The value of IMap is map of {@link TaskGroupLocation} and the {@link SlotProfile} it used.
+     * <p>
+     * This IMap is used to recovery ownedSlotProfilesIMap in JobMaster when a new master node active
+     */
+    private IMap<PipelineLocation, Map<TaskGroupLocation, SlotProfile>> ownedSlotProfilesIMap;
+
+    /**
+     * If this node is a master node
+     */
+    private volatile boolean isMaster = false;
+
+    private final ExecutorService executorService;
+
+    @SuppressWarnings("checkstyle:MagicNumber")
+    public CoordinatorService(@NonNull NodeEngineImpl nodeEngine, @NonNull ExecutorService executorService) {
+        this.nodeEngine = nodeEngine;
+        this.logger = nodeEngine.getLogger(getClass());
+        this.executorService = executorService;
+
+        ScheduledExecutorService masterActiveListener = Executors.newSingleThreadScheduledExecutor();
+        masterActiveListener.scheduleAtFixedRate(() -> checkNewActiveMaster(), 0, 100, TimeUnit.MILLISECONDS);
+    }
+
+    public JobMaster getJobMaster(Long jobId) {
+        return runningJobMasterMap.get(jobId);
+    }
+
+    // On the new master node
+    // 1. If runningJobStateIMap.get(jobId) == null and runningJobInfoIMap.get(jobId) != null. We will do
+    //    runningJobInfoIMap.remove(jobId)
+    //
+    // 2. If runningJobStateIMap.get(jobId) != null and the value equals JobStatus End State. We need new a
+    //    JobMaster and generate PhysicalPlan again and then try to remove all of PipelineLocation and
+    //    TaskGroupLocation key in the runningJobStateIMap.
+    //
+    // 3. If runningJobStateIMap.get(jobId) != null and the value equals JobStatus.SCHEDULED. We need cancel the job
+    //    and then call submitJob(long jobId, Data jobImmutableInformation) to resubmit it.
+    //
+    // 4. If runningJobStateIMap.get(jobId) != null and the value is CANCELING or RUNNING. We need recover the JobMaster
+    //    from runningJobStateIMap and then waiting for it complete.
+    private void initCoordinatorService() {
+        runningJobInfoIMap = nodeEngine.getHazelcastInstance().getMap("runningJobInfo");
+        runningJobStateIMap = nodeEngine.getHazelcastInstance().getMap("runningJobState");
+        runningJobStateTimestampsIMap = nodeEngine.getHazelcastInstance().getMap("stateTimestamps");
+        ownedSlotProfilesIMap = nodeEngine.getHazelcastInstance().getMap("ownedSlotProfilesIMap");
+
+        List<CompletableFuture<Void>> collect = runningJobInfoIMap.entrySet().stream().map(entry -> {
+            return CompletableFuture.runAsync(() -> restoreJobFromMasterActiveSwitch(entry.getKey(), entry.getValue()),
+                executorService);
+        }).collect(Collectors.toList());
+
+        try {
+            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(
+                collect.toArray(new CompletableFuture[0]));
+            voidCompletableFuture.get();
+        } catch (Exception e) {
+            throw new SeaTunnelEngineException(e);

Review Comment:
   If some job already restore, but another not successed when restore, do we need to cancel all job or do what?



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org