CAMEL-11331: Removed unnecessary change in leadership for followers

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a0b23207
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a0b23207
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a0b23207

Branch: refs/heads/master
Commit: a0b232073d368377994e26fe16a073b7ef058775
Parents: 41eec62
Author: Nicola Ferraro <ni.ferr...@gmail.com>
Authored: Wed Jul 19 16:34:51 2017 +0200
Committer: Nicola Ferraro <ni.ferr...@gmail.com>
Committed: Tue Aug 8 16:39:43 2017 +0200

----------------------------------------------------------------------
 .../ha/lock/KubernetesLeaseBasedLeadershipController.java   | 9 +++++----
 .../kubernetes/ha/KubernetesClusterServiceTest.java         | 2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a0b23207/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeaseBasedLeadershipController.java
----------------------------------------------------------------------
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeaseBasedLeadershipController.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeaseBasedLeadershipController.java
index 1366ee1..76e91bf 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeaseBasedLeadershipController.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeaseBasedLeadershipController.java
@@ -265,6 +265,7 @@ public class KubernetesLeaseBasedLeadershipController 
implements Service {
 
             LOG.debug("ConfigMap {} successfully created and local pod is 
leader", this.lockConfiguration.getConfigMapName());
             updateLatestLeaderInfo(newConfigMap);
+            scheduleCheckForPossibleLeadershipLoss();
             return true;
         } else {
             LOG.debug("Lock configmap already present in the Kubernetes 
namespace. Checking...");
@@ -285,6 +286,7 @@ public class KubernetesLeaseBasedLeadershipController 
implements Service {
 
                     LOG.debug("ConfigMap {} successfully updated and local pod 
is leader", this.lockConfiguration.getConfigMapName());
                     updateLatestLeaderInfo(updatedConfigMap);
+                    scheduleCheckForPossibleLeadershipLoss();
                     return true;
                 } catch (Exception ex) {
                     LOG.warn("An attempt to become leader has failed. It's 
possible that the leadership has been taken by another pod");
@@ -341,13 +343,12 @@ public class KubernetesLeaseBasedLeadershipController 
implements Service {
     private void updateLatestLeaderInfo(ConfigMap configMap) {
         LOG.debug("Updating internal status about the current leader");
         this.latestLeaderInfo = ConfigMapLockUtils.getLeaderInfo(configMap, 
this.lockConfiguration.getGroupName());
+    }
 
-        // Notify about changes in current leader if any
-        this.eventDispatcherExecutor.execute(this::checkAndNotifyNewLeader);
+    private void scheduleCheckForPossibleLeadershipLoss() {
+        // Adding check for the case of main thread busy on http calls
         if 
(this.latestLeaderInfo.isLeader(this.lockConfiguration.getPodName())) {
             
this.eventDispatcherExecutor.schedule(this::checkAndNotifyNewLeader, 
this.lockConfiguration.getRenewDeadlineSeconds() * 1000 + 
FIXED_ADDITIONAL_DELAY, TimeUnit.MILLISECONDS);
-        } else if (this.latestLeaderInfo.getLeader() != null) {
-            
this.eventDispatcherExecutor.schedule(this::checkAndNotifyNewLeader, 
this.lockConfiguration.getLeaseDurationSeconds() * 1000 + 
FIXED_ADDITIONAL_DELAY, TimeUnit.MILLISECONDS);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a0b23207/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterServiceTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterServiceTest.java
 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterServiceTest.java
index 4baebc6..3bdffbd 100644
--- 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterServiceTest.java
+++ 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterServiceTest.java
@@ -205,7 +205,7 @@ public class KubernetesClusterServiceTest extends 
CamelTestSupport {
         a1.waitForAnyLeader(2, TimeUnit.SECONDS);
         a2.waitForAnyLeader(2, TimeUnit.SECONDS);
         b1.waitForAnyLeader(2, TimeUnit.SECONDS);
-        b1.waitForAnyLeader(2, TimeUnit.SECONDS);
+        b2.waitForAnyLeader(2, TimeUnit.SECONDS);
 
         assertNotNull(a1.getCurrentLeader());
         assertTrue(a1.getCurrentLeader().startsWith("a"));

Reply via email to