diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/Leader.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/Leader.java index 7076c9187..53e8536aa 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/Leader.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/Leader.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,11 +35,11 @@ public Leader(String role, String id) { } public String getRole() { - return this.role; + return role; } public String getId() { - return this.id; + return id; } public boolean isCandidate(Candidate candidate) { @@ -62,17 +62,17 @@ public boolean equals(Object o) { Leader leader = (Leader) o; - return Objects.equals(this.role, leader.role) && Objects.equals(this.id, leader.id); + return Objects.equals(role, leader.role) && Objects.equals(id, leader.id); } @Override public int hashCode() { - return Objects.hash(this.role, this.id); + return Objects.hash(role, id); } @Override public String toString() { - return String.format("Leader{role='%s', id='%s'}", this.role, this.id); + return String.format("Leader{role='%s', id='%s'}", role, id); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderContext.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderContext.java index ef91a2f71..4dc5d8ccd 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderContext.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,12 +35,17 @@ public LeaderContext(Candidate candidate, LeadershipController leadershipControl @Override public boolean isLeader() { - return this.leadershipController.getLocalLeader().filter(l -> l.isCandidate(this.candidate)).isPresent(); + return leadershipController.getLocalLeader().filter(l -> l.isCandidate(candidate)).isPresent(); } @Override public void yield() { - this.leadershipController.revoke(); + leadershipController.revoke(); + } + + @Override + public String getRole() { + return candidate.getRole(); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInitiator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInitiator.java index 3e6c682b8..1b51562ef 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInitiator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInitiator.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,17 +20,15 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.springframework.context.SmartLifecycle; +import org.springframework.core.log.LogAccessor; /** * @author Gytis Trikleris */ public class LeaderInitiator implements SmartLifecycle { - private static final Logger LOGGER = LoggerFactory.getLogger(LeaderInitiator.class); + private static final LogAccessor LOGGER = new LogAccessor(LeaderInitiator.class); private final LeaderProperties leaderProperties; @@ -54,33 +52,33 @@ public LeaderInitiator(LeaderProperties leaderProperties, LeadershipController l @Override public boolean isAutoStartup() { - return this.leaderProperties.isAutoStartup(); + return leaderProperties.isAutoStartup(); } @Override public void start() { if (!isRunning()) { - LOGGER.debug("Leader initiator starting"); - this.leaderRecordWatcher.start(); - this.hostPodWatcher.start(); - this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - this.scheduledExecutorService.scheduleAtFixedRate(this.leadershipController::update, - this.leaderProperties.getUpdatePeriod().toMillis(), - this.leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS); - this.isRunning = true; + LOGGER.debug(() -> "Leader initiator starting"); + leaderRecordWatcher.start(); + hostPodWatcher.start(); + scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + scheduledExecutorService.scheduleAtFixedRate(leadershipController::update, + leaderProperties.getUpdatePeriod().toMillis(), + leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS); + isRunning = true; } } @Override public void stop() { if (isRunning()) { - LOGGER.debug("Leader initiator stopping"); - this.scheduledExecutorService.shutdown(); - this.scheduledExecutorService = null; - this.hostPodWatcher.stop(); - this.leaderRecordWatcher.stop(); - this.leadershipController.revoke(); - this.isRunning = false; + LOGGER.debug(() -> "Leader initiator stopping"); + scheduledExecutorService.shutdown(); + scheduledExecutorService = null; + hostPodWatcher.stop(); + leaderRecordWatcher.stop(); + leadershipController.revoke(); + isRunning = false; } } @@ -92,7 +90,7 @@ public void stop(Runnable callback) { @Override public boolean isRunning() { - return this.isRunning; + return isRunning; } @Override diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java index 015de2328..94771916d 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java @@ -53,7 +53,6 @@ public static void guarded(ReentrantLock lock, Runnable runnable) { finally { lock.unlock(); } - } }