CAMEL-11331: Fixed logging and avoid unnecessary wait

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

Branch: refs/heads/master
Commit: b72cf4d48553bec5dae68e7f69feb9314683df11
Parents: 5d6fe41
Author: Nicola Ferraro <ni.ferr...@gmail.com>
Authored: Mon Jul 31 17:56:08 2017 +0200
Committer: Nicola Ferraro <ni.ferr...@gmail.com>
Committed: Tue Aug 8 16:39:43 2017 +0200

----------------------------------------------------------------------
 .../ha/lock/KubernetesLeadershipController.java | 22 ++++++++++----------
 .../kubernetes/ha/lock/TimedLeaderNotifier.java |  2 ++
 2 files changed, 13 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b72cf4d4/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeadershipController.java
----------------------------------------------------------------------
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeadershipController.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeadershipController.java
index b0c2110..2f79bd7 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeadershipController.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/KubernetesLeadershipController.java
@@ -130,7 +130,7 @@ public class KubernetesLeadershipController implements 
Service {
             LOG.info("{} The cluster has no leaders. Trying to acquire the 
leadership...", logPrefix());
             boolean acquired = tryAcquireLeadership();
             if (acquired) {
-                LOG.info("{} Leadership acquired by current pod ({}) with 
immediate effect", logPrefix(), this.lockConfiguration.getPodName());
+                LOG.info("{} Leadership acquired by current pod with immediate 
effect", logPrefix());
                 this.currentState = State.LEADER;
                 this.serializedExecutor.execute(this::refreshStatus);
                 return;
@@ -142,7 +142,7 @@ public class KubernetesLeadershipController implements 
Service {
             LOG.info("{} Leadership has been lost by old owner. Trying to 
acquire the leadership...", logPrefix());
             boolean acquired = tryAcquireLeadership();
             if (acquired) {
-                LOG.info("{} Leadership acquired by current pod ({})", 
logPrefix(), this.lockConfiguration.getPodName());
+                LOG.info("{} Leadership acquired by current pod", logPrefix());
                 this.currentState = State.BECOMING_LEADER;
                 this.serializedExecutor.execute(this::refreshStatus);
                 return;
@@ -151,7 +151,7 @@ public class KubernetesLeadershipController implements 
Service {
             }
         } else if 
(this.latestLeaderInfo.isValidLeader(this.lockConfiguration.getPodName())) {
             // We are leaders for some reason (e.g. pod restart on failure)
-            LOG.info("{} Leadership is already owned by current pod ({})", 
logPrefix(), this.lockConfiguration.getPodName());
+            LOG.info("{} Leadership is already owned by current pod", 
logPrefix());
             this.currentState = State.BECOMING_LEADER;
             this.serializedExecutor.execute(this::refreshStatus);
             return;
@@ -172,7 +172,7 @@ public class KubernetesLeadershipController implements 
Service {
         // Wait always the same amount of time before becoming the leader
         // Even if the current pod is already leader, we should let a possible 
old version of the pod to shut down
         long delay = this.lockConfiguration.getLeaseDurationMillis();
-        LOG.info("{} Current pod ({}) owns the leadership, but it will be 
effective in {} seconds...", logPrefix(), this.lockConfiguration.getPodName(), 
new BigDecimal(delay).divide(BigDecimal
+        LOG.info("{} Current pod owns the leadership, but it will be effective 
in {} seconds...", logPrefix(), new BigDecimal(delay).divide(BigDecimal
                 .valueOf(1000), 2, BigDecimal.ROUND_HALF_UP));
 
         try {
@@ -181,7 +181,7 @@ public class KubernetesLeadershipController implements 
Service {
             LOG.warn("Thread interrupted", e);
         }
 
-        LOG.info("{} Current pod ({}) is becoming the new leader now...", 
logPrefix(), this.lockConfiguration.getPodName());
+        LOG.info("{} Current pod is becoming the new leader now...", 
logPrefix());
         this.currentState = State.LEADER;
         this.serializedExecutor.execute(this::refreshStatus);
     }
@@ -196,7 +196,7 @@ public class KubernetesLeadershipController implements 
Service {
         }
 
         if 
(this.latestLeaderInfo.isValidLeader(this.lockConfiguration.getPodName())) {
-            LOG.debug("{} Current Pod ({}) is still the leader", logPrefix(), 
this.lockConfiguration.getPodName());
+            LOG.debug("{} Current Pod is still the leader", logPrefix());
             
this.leaderNotifier.refreshLeadership(Optional.of(this.lockConfiguration.getPodName()),
                     timeBeforePulling,
                     this.lockConfiguration.getRenewDeadlineMillis(),
@@ -204,7 +204,7 @@ public class KubernetesLeadershipController implements 
Service {
             rescheduleAfterDelay();
             return;
         } else {
-            LOG.debug("{} Current Pod ({}) has lost the leadership", 
logPrefix(), this.lockConfiguration.getPodName());
+            LOG.debug("{} Current Pod has lost the leadership", logPrefix());
             this.currentState = State.NOT_LEADER;
             // set a empty leader to signal leadership loss
             this.leaderNotifier.refreshLeadership(Optional.empty(),
@@ -212,8 +212,8 @@ public class KubernetesLeadershipController implements 
Service {
                     lockConfiguration.getLeaseDurationMillis(),
                     this.latestLeaderInfo.getMembers());
 
-            // wait a lease time and restart
-            this.serializedExecutor.schedule(this::refreshStatus, 
this.lockConfiguration.getLeaseDurationMillis(), TimeUnit.MILLISECONDS);
+            // restart from scratch to acquire leadership
+            this.serializedExecutor.execute(this::refreshStatus);
         }
     }
 
@@ -257,7 +257,7 @@ public class KubernetesLeadershipController implements 
Service {
             LOG.warn(logPrefix() + " Unexpected condition. Latest leader info 
or list of members is empty.");
             return false;
         } else if (!members.contains(this.lockConfiguration.getPodName())) {
-            LOG.warn(logPrefix() + " The list of cluster members " + 
latestLeaderInfo.getMembers() + " does not contain the current pod (" + 
this.lockConfiguration.getPodName() + "). Cannot acquire"
+            LOG.warn(logPrefix() + " The list of cluster members " + 
latestLeaderInfo.getMembers() + " does not contain the current Pod. Cannot 
acquire"
                     + " leadership.");
             return false;
         }
@@ -311,7 +311,7 @@ public class KubernetesLeadershipController implements 
Service {
                 }
             } else {
                 // Another pod is the leader and it's still active
-                LOG.debug("{} Another pod ({}) is the current leader and it is 
still active", logPrefix(), this.latestLeaderInfo.getLeader());
+                LOG.debug("{} Another Pod ({}) is the current leader and it is 
still active", logPrefix(), this.latestLeaderInfo.getLeader());
                 return false;
             }
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/b72cf4d4/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/TimedLeaderNotifier.java
----------------------------------------------------------------------
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/TimedLeaderNotifier.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/TimedLeaderNotifier.java
index 6ada830..6c51226 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/TimedLeaderNotifier.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/lock/TimedLeaderNotifier.java
@@ -155,6 +155,7 @@ public class TimedLeaderNotifier implements Service {
         final Optional<String> newLeader = leader;
         if (!newLeader.equals(lastCommunicatedLeader)) {
             lastCommunicatedLeader = newLeader;
+            LOG.debug("Communicating new leader: {}" + newLeader);
             handler.onKubernetesClusterEvent(new 
KubernetesClusterEvent.KubernetesClusterLeaderChangedEvent() {
                 @Override
                 public Optional<String> getData() {
@@ -166,6 +167,7 @@ public class TimedLeaderNotifier implements Service {
         final Set<String> newMembers = members;
         if (!newMembers.equals(lastCommunicatedMembers)) {
             lastCommunicatedMembers = newMembers;
+            LOG.debug("Communicating new cluster members: {}" + newMembers);
             handler.onKubernetesClusterEvent(new 
KubernetesClusterEvent.KubernetesClusterMemberListChangedEvent() {
                 @Override
                 public Set<String> getData() {

Reply via email to