You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/01/24 18:38:59 UTC

[GitHub] [flink] tillrohrmann commented on a change in pull request #17485: [FLINK-24038] Add support for single leader election per JobManager process

tillrohrmann commented on a change in pull request #17485:
URL: https://github.com/apache/flink/pull/17485#discussion_r791046741



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/leaderelection/DefaultMultipleComponentLeaderElectionService.java
##########
@@ -0,0 +1,285 @@
+/*
+ * 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.leaderelection;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.runtime.rpc.FatalErrorHandler;
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.ExecutorUtils;
+import org.apache.flink.util.FlinkException;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.concurrent.ExecutorThreadFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+/**
+ * Default implementation of a {@link MultipleComponentLeaderElectionService} that allows to
+ * register multiple {@link LeaderElectionEventHandler}.
+ */
+public class DefaultMultipleComponentLeaderElectionService
+        implements MultipleComponentLeaderElectionService,
+                MultipleComponentLeaderElectionDriver.Listener {
+    private static final Logger LOG =
+            LoggerFactory.getLogger(DefaultMultipleComponentLeaderElectionService.class);
+
+    private final Object lock = new Object();
+
+    private final MultipleComponentLeaderElectionDriver multipleComponentLeaderElectionDriver;
+
+    private final FatalErrorHandler fatalErrorHandler;
+
+    @GuardedBy("lock")
+    private final ExecutorService leadershipOperationExecutor;
+
+    @GuardedBy("lock")
+    private final Map<String, LeaderElectionEventHandler> leaderElectionEventHandlers;
+
+    private boolean running = true;
+
+    @Nullable
+    @GuardedBy("lock")
+    private UUID currentLeaderSessionId = null;
+
+    @VisibleForTesting
+    DefaultMultipleComponentLeaderElectionService(
+            FatalErrorHandler fatalErrorHandler,
+            String leaderContenderDescription,
+            MultipleComponentLeaderElectionDriverFactory
+                    multipleComponentLeaderElectionDriverFactory,
+            ExecutorService leadershipOperationExecutor)
+            throws Exception {
+        this.fatalErrorHandler = fatalErrorHandler;
+
+        this.leadershipOperationExecutor = leadershipOperationExecutor;
+
+        leaderElectionEventHandlers = new HashMap<>();
+
+        multipleComponentLeaderElectionDriver =
+                multipleComponentLeaderElectionDriverFactory.create(
+                        leaderContenderDescription, this);
+    }
+
+    public DefaultMultipleComponentLeaderElectionService(
+            FatalErrorHandler fatalErrorHandler,
+            String leaderContenderDescription,
+            MultipleComponentLeaderElectionDriverFactory
+                    multipleComponentLeaderElectionDriverFactory)
+            throws Exception {
+        this(
+                fatalErrorHandler,
+                leaderContenderDescription,
+                multipleComponentLeaderElectionDriverFactory,
+                java.util.concurrent.Executors.newSingleThreadExecutor(
+                        new ExecutorThreadFactory(
+                                String.format(
+                                        "leadershipOperation-%s", leaderContenderDescription))));
+    }
+
+    @Override
+    public void close() throws Exception {
+        synchronized (lock) {
+            if (!running) {
+                return;
+            }
+            running = false;
+
+            LOG.info("Closing {}.", this);
+
+            ExecutorUtils.gracefulShutdown(10L, TimeUnit.SECONDS, leadershipOperationExecutor);
+
+            Exception exception = null;
+            try {
+                multipleComponentLeaderElectionDriver.close();
+            } catch (Exception e) {
+                exception = e;
+            }
+
+            ExceptionUtils.tryRethrowException(exception);
+        }
+    }
+
+    @Override
+    public LeaderElectionDriverFactory createDriverFactory(String leaderName) {
+        return new MultipleComponentLeaderElectionDriverAdapterFactory(leaderName, this);
+    }
+
+    @Override
+    public void publishLeaderInformation(String leaderName, LeaderInformation leaderInformation) {
+        try {
+            multipleComponentLeaderElectionDriver.publishLeaderInformation(
+                    leaderName, leaderInformation);
+        } catch (Exception e) {
+            fatalErrorHandler.onFatalError(
+                    new FlinkException(
+                            String.format(
+                                    "Could not write leader information %s for leader %s.",
+                                    leaderInformation, leaderName),
+                            e));
+        }
+    }
+
+    @Override
+    public void registerLeaderElectionEventHandler(
+            String componentId, LeaderElectionEventHandler leaderElectionEventHandler) {
+
+        synchronized (lock) {
+            Preconditions.checkState(
+                    !leaderElectionEventHandlers.containsKey(componentId),
+                    "Do not support duplicate LeaderElectionEventHandler registration under %s",
+                    componentId);
+            leaderElectionEventHandlers.put(componentId, leaderElectionEventHandler);
+
+            if (currentLeaderSessionId != null) {
+                leadershipOperationExecutor.execute(
+                        () -> leaderElectionEventHandler.onGrantLeadership(currentLeaderSessionId));
+            }
+        }
+    }
+
+    @Override
+    public void unregisterLeaderElectionEventHandler(String componentId) throws Exception {
+        final LeaderElectionEventHandler unregisteredLeaderElectionEventHandler;
+        synchronized (lock) {
+            unregisteredLeaderElectionEventHandler =
+                    leaderElectionEventHandlers.remove(componentId);
+
+            if (unregisteredLeaderElectionEventHandler != null) {

Review comment:
       True. Will add it.




-- 
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: issues-unsubscribe@flink.apache.org

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