wangyang0918 commented on a change in pull request #13644:
URL: https://github.com/apache/flink/pull/13644#discussion_r508505644



##########
File path: 
flink-kubernetes/src/main/java/org/apache/flink/kubernetes/highavailability/KubernetesLeaderRetrievalService.java
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.kubernetes.highavailability;
+
+import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesWatch;
+import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener;
+import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.UUID;
+
+import static org.apache.flink.kubernetes.utils.Constants.LEADER_ADDRESS_KEY;
+import static 
org.apache.flink.kubernetes.utils.Constants.LEADER_SESSION_ID_KEY;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * The counterpart to the {@link 
org.apache.flink.kubernetes.highavailability.KubernetesLeaderElectionService}.
+ * This implementation of the {@link LeaderRetrievalService} retrieves the 
current leader which has
+ * been elected by the {@link 
org.apache.flink.kubernetes.highavailability.KubernetesLeaderElectionService}.
+ * The leader address as well as the current leader session ID is retrieved 
from Kubernetes ConfigMap.
+ */
+class KubernetesLeaderRetrievalService implements LeaderRetrievalService {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesLeaderRetrievalService.class);
+
+       private final Object lock = new Object();
+
+       private final FlinkKubeClient kubeClient;
+
+       private final String configMapName;
+
+       @GuardedBy("lock")
+       private volatile String lastLeaderAddress;
+
+       @GuardedBy("lock")
+       private volatile UUID lastLeaderSessionID;
+
+       @GuardedBy("lock")
+       private volatile LeaderRetrievalListener leaderListener;
+
+       @GuardedBy("lock")
+       private volatile boolean running;
+
+       private KubernetesWatch kubernetesWatch;
+
+       KubernetesLeaderRetrievalService(FlinkKubeClient kubeClient, String 
configMapName) {
+               this.kubeClient = checkNotNull(kubeClient, "Kubernetes client 
should not be null.");
+               this.configMapName = checkNotNull(configMapName, "ConfigMap 
name should not be null.");
+
+               this.leaderListener = null;
+               this.lastLeaderAddress = null;
+               this.lastLeaderSessionID = null;
+
+               running = false;
+       }
+
+       @Override
+       public void start(LeaderRetrievalListener listener) {
+               checkNotNull(listener, "Listener must not be null.");
+               Preconditions.checkState(leaderListener == null, 
"KubernetesLeaderRetrievalService can " +
+                       "only be started once.");
+
+               LOG.info("Starting {}.", this);
+
+               synchronized (lock) {
+                       running = true;
+                       leaderListener = listener;
+                       kubernetesWatch = 
kubeClient.watchConfigMaps(configMapName, new ConfigMapCallbackHandlerImpl());
+               }
+       }
+
+       @Override
+       public void stop() {
+               LOG.info("Stopping {}.", this);
+
+               synchronized (lock) {
+                       if (!running) {
+                               return;
+                       }
+                       running = false;
+                       if (kubernetesWatch != null) {
+                               kubernetesWatch.close();
+                       }
+               }
+       }
+
+       @Override
+       public String toString() {
+               return "KubernetesLeaderRetrievalService{configMapName='" + 
configMapName + "'}";
+       }
+
+       private class ConfigMapCallbackHandlerImpl implements 
FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap> {
+
+               @Override
+               public void onAdded(List<KubernetesConfigMap> configMaps) {
+                       handleEvent(configMaps);
+               }
+
+               @Override
+               public void onModified(List<KubernetesConfigMap> configMaps) {
+                       handleEvent(configMaps);
+               }
+
+               @Override
+               public void onDeleted(List<KubernetesConfigMap> configMaps) {
+                       // Nothing to do since a new ConfigMap will be created 
if it is deleted externally.
+               }
+
+               @Override
+               public void onError(List<KubernetesConfigMap> configMaps) {
+                       leaderListener.handleError(new Exception("Error while 
watching the ConfigMap " + configMapName));
+               }
+
+               @Override
+               public void handleFatalError(Throwable throwable) {
+                       leaderListener.handleError(
+                               new Exception("Fatal error while watching the 
ConfigMap " + configMapName, throwable));
+               }
+
+               private void handleEvent(List<KubernetesConfigMap> configMaps) {
+                       synchronized (lock) {
+                               if (running) {
+                                       configMaps.forEach(e -> {
+                                               if 
(e.getName().equals(configMapName)) {

Review comment:
       Yes.




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to