xintongsong commented on a change in pull request #15524:
URL: https://github.com/apache/flink/pull/15524#discussion_r636608218



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/resourcemanager/ResourceManagerServiceImpl.java
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.flink.runtime.resourcemanager;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.clusterframework.ApplicationStatus;
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.entrypoint.ClusterInformation;
+import org.apache.flink.runtime.heartbeat.HeartbeatServices;
+import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
+import org.apache.flink.runtime.leaderelection.LeaderContender;
+import org.apache.flink.runtime.leaderelection.LeaderElectionService;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
+import org.apache.flink.runtime.rpc.RpcService;
+import org.apache.flink.util.ConfigurationException;
+import org.apache.flink.util.FlinkException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** Default implementation of {@link ResourceManagerService}. */
+public class ResourceManagerServiceImpl implements ResourceManagerService, 
LeaderContender {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ResourceManagerServiceImpl.class);
+
+    private final ResourceManagerFactory<?> resourceManagerFactory;
+    private final ResourceManagerProcessContext rmProcessContext;
+
+    private final LeaderElectionService leaderElectionService;
+    private final FatalErrorHandler fatalErrorHandler;
+    private final Executor ioExecutor;
+
+    private final Executor handleLeaderEventExecutor;
+    private final CompletableFuture<Void> serviceTerminationFuture;
+
+    private final Object lock = new Object();
+
+    @GuardedBy("lock")
+    private boolean running;
+
+    @Nullable
+    @GuardedBy("lock")
+    private ResourceManager<?> leaderResourceManager;
+
+    @Nullable
+    @GuardedBy("lock")
+    private UUID leaderSessionID;
+
+    @GuardedBy("lock")
+    private CompletableFuture<Void> previousResourceManagerTerminationFuture;
+
+    private ResourceManagerServiceImpl(
+            ResourceManagerFactory<?> resourceManagerFactory,
+            ResourceManagerProcessContext rmProcessContext) {
+        this.resourceManagerFactory = checkNotNull(resourceManagerFactory);
+        this.rmProcessContext = checkNotNull(rmProcessContext);
+
+        this.leaderElectionService =
+                rmProcessContext
+                        .getHighAvailabilityServices()
+                        .getResourceManagerLeaderElectionService();
+        this.fatalErrorHandler = rmProcessContext.getFatalErrorHandler();
+        this.ioExecutor = rmProcessContext.getIoExecutor();
+
+        this.handleLeaderEventExecutor = Executors.newSingleThreadExecutor();
+        this.serviceTerminationFuture = new CompletableFuture<>();
+
+        this.running = false;
+        this.leaderResourceManager = null;
+        this.leaderSessionID = null;
+        this.previousResourceManagerTerminationFuture = 
FutureUtils.completedVoidFuture();
+    }
+
+    // ------------------------------------------------------------------------
+    //  ResourceManagerService
+    // ------------------------------------------------------------------------
+
+    @Override
+    public void start() throws Exception {
+        synchronized (lock) {
+            if (running) {
+                LOG.debug("Resource manager service has already started.");
+                return;
+            }
+            running = true;
+        }
+
+        LOG.info("Starting resource manager service.");
+
+        leaderElectionService.start(this);
+    }
+
+    @Override
+    public CompletableFuture<Void> getTerminationFuture() {
+        return serviceTerminationFuture;
+    }
+
+    @Override
+    public CompletableFuture<Void> deregisterApplication(
+            final ApplicationStatus applicationStatus, final @Nullable String 
diagnostics) {
+        synchronized (lock) {
+            if (running && leaderResourceManager != null) {
+                return leaderResourceManager
+                        .getSelfGateway(ResourceManagerGateway.class)
+                        .deregisterApplication(applicationStatus, diagnostics)
+                        .thenApply(ack -> null);
+            } else {
+                return FutureUtils.completedExceptionally(
+                        new FlinkException(
+                                "Cannot deregister application. Resource 
manager service is not available."));
+            }
+        }
+    }
+
+    @Override
+    public CompletableFuture<Void> closeAsync() {
+        synchronized (lock) {
+            if (running) {
+                LOG.info("Stopping resource manager service.");
+                running = false;
+                stopLeaderElectionService();
+                stopLeaderResourceManager();
+            } else {
+                LOG.debug("Resource manager service is not running.");
+            }
+
+            FutureUtils.forward(previousResourceManagerTerminationFuture, 
serviceTerminationFuture);
+        }
+
+        return serviceTerminationFuture;
+    }
+
+    // ------------------------------------------------------------------------
+    //  LeaderContender
+    // ------------------------------------------------------------------------
+
+    @Override
+    public void grantLeadership(UUID newLeaderSessionID) {
+        handleLeaderEventExecutor.execute(
+                () -> {
+                    synchronized (lock) {
+                        if (!running) {
+                            LOG.info(
+                                    "Resource manager service is not running. 
Ignore granting leadership with session ID {}.",
+                                    newLeaderSessionID);
+                            return;
+                        }
+
+                        LOG.info(
+                                "Resource manager service is granted 
leadership with session id {}.",
+                                newLeaderSessionID);
+
+                        try {
+                            startNewLeaderResourceManager(newLeaderSessionID);
+                        } catch (Throwable t) {
+                            fatalErrorHandler.onFatalError(
+                                    new FlinkException("Cannot start resource 
manager.", t));
+                        }
+                    }
+                });
+    }
+
+    @Override
+    public void revokeLeadership() {
+        handleLeaderEventExecutor.execute(
+                () -> {
+                    synchronized (lock) {
+                        if (!running) {
+                            LOG.info(
+                                    "Resource manager service is not running. 
Ignore revoking leadership.");
+                            return;
+                        }
+
+                        LOG.info(
+                                "Resource manager service is revoked 
leadership with session id {}.",
+                                leaderSessionID);
+
+                        stopLeaderResourceManager();
+                    }
+                });
+    }
+
+    @Override
+    public void handleError(Exception exception) {
+        fatalErrorHandler.onFatalError(
+                new FlinkException(
+                        "Exception during leader election of resource manager 
occurred.",
+                        exception));
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal
+    // ------------------------------------------------------------------------
+
+    @GuardedBy("lock")
+    private void startNewLeaderResourceManager(UUID newLeaderSessionID) throws 
Exception {
+        stopLeaderResourceManager();
+
+        this.leaderSessionID = newLeaderSessionID;
+        this.leaderResourceManager =
+                resourceManagerFactory.createResourceManager(
+                        rmProcessContext, newLeaderSessionID, 
ResourceID.generate());
+
+        final ResourceManager<?> newLeaderResourceManager = 
this.leaderResourceManager;
+
+        previousResourceManagerTerminationFuture
+                .thenComposeAsync(
+                        (ignore) -> {
+                            synchronized (lock) {
+                                return 
startResourceManagerIfIsLeader(newLeaderResourceManager);
+                            }
+                        },
+                        handleLeaderEventExecutor)
+                .thenAcceptAsync(
+                        (isStillLeader) -> {
+                            if (isStillLeader) {
+                                leaderElectionService.confirmLeadership(
+                                        newLeaderSessionID, 
newLeaderResourceManager.getAddress());
+                            }
+                        },
+                        ioExecutor);
+    }
+
+    /**
+     * Returns a future that completes as {@code true} if the resource manager 
is still leader and
+     * started, and {@code false} if it's no longer leader.
+     */
+    @GuardedBy("lock")
+    private CompletableFuture<Boolean> startResourceManagerIfIsLeader(
+            ResourceManager<?> resourceManager) {
+        if (isLeader(resourceManager)) {
+            resourceManager.start();
+            forwardTerminationFuture(resourceManager);

Review comment:
       IIUC, your point is that in case the leader RM is terminated 
unexpectedly, the RMService can try start another RM without failing the entire 
process?
   
   In fact, it's not clear to me why would a leader RM be terminated 
unexpectedly without losing leadership. Ideally, this should not happen. 
Failing strictly rather than restarting silently in such cases may help drawing 
attentions to such unexpected cases.
   
   Besides, failing strictly does not introduce any regression comparing to 
previous behaviors. Trying to start a new RM can be considered as an future 
improvement if it is proven necessary.

##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/resourcemanager/ResourceManagerServiceImpl.java
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.flink.runtime.resourcemanager;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.clusterframework.ApplicationStatus;
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.entrypoint.ClusterInformation;
+import org.apache.flink.runtime.heartbeat.HeartbeatServices;
+import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
+import org.apache.flink.runtime.leaderelection.LeaderContender;
+import org.apache.flink.runtime.leaderelection.LeaderElectionService;
+import org.apache.flink.runtime.metrics.MetricRegistry;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
+import org.apache.flink.runtime.rpc.RpcService;
+import org.apache.flink.util.ConfigurationException;
+import org.apache.flink.util.FlinkException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/** Default implementation of {@link ResourceManagerService}. */
+public class ResourceManagerServiceImpl implements ResourceManagerService, 
LeaderContender {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ResourceManagerServiceImpl.class);
+
+    private final ResourceManagerFactory<?> resourceManagerFactory;
+    private final ResourceManagerProcessContext rmProcessContext;
+
+    private final LeaderElectionService leaderElectionService;
+    private final FatalErrorHandler fatalErrorHandler;
+    private final Executor ioExecutor;
+
+    private final Executor handleLeaderEventExecutor;
+    private final CompletableFuture<Void> serviceTerminationFuture;
+
+    private final Object lock = new Object();
+
+    @GuardedBy("lock")
+    private boolean running;
+
+    @Nullable
+    @GuardedBy("lock")
+    private ResourceManager<?> leaderResourceManager;
+
+    @Nullable
+    @GuardedBy("lock")
+    private UUID leaderSessionID;
+
+    @GuardedBy("lock")
+    private CompletableFuture<Void> previousResourceManagerTerminationFuture;
+
+    private ResourceManagerServiceImpl(
+            ResourceManagerFactory<?> resourceManagerFactory,
+            ResourceManagerProcessContext rmProcessContext) {
+        this.resourceManagerFactory = checkNotNull(resourceManagerFactory);
+        this.rmProcessContext = checkNotNull(rmProcessContext);
+
+        this.leaderElectionService =
+                rmProcessContext
+                        .getHighAvailabilityServices()
+                        .getResourceManagerLeaderElectionService();
+        this.fatalErrorHandler = rmProcessContext.getFatalErrorHandler();
+        this.ioExecutor = rmProcessContext.getIoExecutor();
+
+        this.handleLeaderEventExecutor = Executors.newSingleThreadExecutor();
+        this.serviceTerminationFuture = new CompletableFuture<>();
+
+        this.running = false;
+        this.leaderResourceManager = null;
+        this.leaderSessionID = null;
+        this.previousResourceManagerTerminationFuture = 
FutureUtils.completedVoidFuture();
+    }
+
+    // ------------------------------------------------------------------------
+    //  ResourceManagerService
+    // ------------------------------------------------------------------------
+
+    @Override
+    public void start() throws Exception {
+        synchronized (lock) {
+            if (running) {
+                LOG.debug("Resource manager service has already started.");
+                return;
+            }
+            running = true;
+        }
+
+        LOG.info("Starting resource manager service.");
+
+        leaderElectionService.start(this);
+    }
+
+    @Override
+    public CompletableFuture<Void> getTerminationFuture() {
+        return serviceTerminationFuture;
+    }
+
+    @Override
+    public CompletableFuture<Void> deregisterApplication(
+            final ApplicationStatus applicationStatus, final @Nullable String 
diagnostics) {
+        synchronized (lock) {
+            if (running && leaderResourceManager != null) {
+                return leaderResourceManager
+                        .getSelfGateway(ResourceManagerGateway.class)
+                        .deregisterApplication(applicationStatus, diagnostics)
+                        .thenApply(ack -> null);
+            } else {
+                return FutureUtils.completedExceptionally(
+                        new FlinkException(
+                                "Cannot deregister application. Resource 
manager service is not available."));
+            }
+        }
+    }
+
+    @Override
+    public CompletableFuture<Void> closeAsync() {
+        synchronized (lock) {
+            if (running) {
+                LOG.info("Stopping resource manager service.");
+                running = false;
+                stopLeaderElectionService();
+                stopLeaderResourceManager();
+            } else {
+                LOG.debug("Resource manager service is not running.");
+            }
+
+            FutureUtils.forward(previousResourceManagerTerminationFuture, 
serviceTerminationFuture);
+        }
+
+        return serviceTerminationFuture;
+    }
+
+    // ------------------------------------------------------------------------
+    //  LeaderContender
+    // ------------------------------------------------------------------------
+
+    @Override
+    public void grantLeadership(UUID newLeaderSessionID) {
+        handleLeaderEventExecutor.execute(
+                () -> {
+                    synchronized (lock) {
+                        if (!running) {
+                            LOG.info(
+                                    "Resource manager service is not running. 
Ignore granting leadership with session ID {}.",
+                                    newLeaderSessionID);
+                            return;
+                        }
+
+                        LOG.info(
+                                "Resource manager service is granted 
leadership with session id {}.",
+                                newLeaderSessionID);
+
+                        try {
+                            startNewLeaderResourceManager(newLeaderSessionID);
+                        } catch (Throwable t) {
+                            fatalErrorHandler.onFatalError(
+                                    new FlinkException("Cannot start resource 
manager.", t));
+                        }
+                    }
+                });
+    }
+
+    @Override
+    public void revokeLeadership() {
+        handleLeaderEventExecutor.execute(
+                () -> {
+                    synchronized (lock) {
+                        if (!running) {
+                            LOG.info(
+                                    "Resource manager service is not running. 
Ignore revoking leadership.");
+                            return;
+                        }
+
+                        LOG.info(
+                                "Resource manager service is revoked 
leadership with session id {}.",
+                                leaderSessionID);
+
+                        stopLeaderResourceManager();
+                    }
+                });
+    }
+
+    @Override
+    public void handleError(Exception exception) {
+        fatalErrorHandler.onFatalError(
+                new FlinkException(
+                        "Exception during leader election of resource manager 
occurred.",
+                        exception));
+    }
+
+    // ------------------------------------------------------------------------
+    //  Internal
+    // ------------------------------------------------------------------------
+
+    @GuardedBy("lock")
+    private void startNewLeaderResourceManager(UUID newLeaderSessionID) throws 
Exception {
+        stopLeaderResourceManager();
+
+        this.leaderSessionID = newLeaderSessionID;
+        this.leaderResourceManager =
+                resourceManagerFactory.createResourceManager(
+                        rmProcessContext, newLeaderSessionID, 
ResourceID.generate());
+
+        final ResourceManager<?> newLeaderResourceManager = 
this.leaderResourceManager;
+
+        previousResourceManagerTerminationFuture
+                .thenComposeAsync(
+                        (ignore) -> {
+                            synchronized (lock) {
+                                return 
startResourceManagerIfIsLeader(newLeaderResourceManager);
+                            }
+                        },
+                        handleLeaderEventExecutor)
+                .thenAcceptAsync(
+                        (isStillLeader) -> {
+                            if (isStillLeader) {
+                                leaderElectionService.confirmLeadership(
+                                        newLeaderSessionID, 
newLeaderResourceManager.getAddress());
+                            }
+                        },
+                        ioExecutor);
+    }
+
+    /**
+     * Returns a future that completes as {@code true} if the resource manager 
is still leader and
+     * started, and {@code false} if it's no longer leader.
+     */
+    @GuardedBy("lock")
+    private CompletableFuture<Boolean> startResourceManagerIfIsLeader(
+            ResourceManager<?> resourceManager) {
+        if (isLeader(resourceManager)) {
+            resourceManager.start();
+            forwardTerminationFuture(resourceManager);

Review comment:
       IIUC, your point is that in case the leader RM is terminated 
unexpectedly, the RMService can try start another RM without failing the entire 
process?
   
   In fact, it's not clear to me why would a leader RM be terminated 
unexpectedly without losing leadership. Ideally, this should not happen. 
Failing strictly rather than restarting silently in such cases may help drawing 
attentions to such unexpected cases.
   
   Besides, failing strictly does not introduce any regression comparing to 
previous behaviors. Trying to start a new RM can be considered as a future 
improvement if it is proven necessary.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to