Skip to content

Commit a0e7aac

Browse files
authored
Merge pull request #2112 from wind57/move-leader-election-utilities-to-common
2 parents 3de3d4d + c66a4e6 commit a0e7aac

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInitiatorUtil.java renamed to spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionInitiatorUtil.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,51 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.cloud.kubernetes.fabric8.leader.election;
17+
package org.springframework.cloud.kubernetes.commons.leader.election;
1818

1919
import java.util.concurrent.CompletableFuture;
2020
import java.util.concurrent.ExecutorService;
2121
import java.util.concurrent.TimeUnit;
2222

23-
import io.fabric8.kubernetes.client.KubernetesClient;
24-
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderElectionConfig;
25-
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderElector;
26-
27-
import org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionProperties;
2823
import org.springframework.core.log.LogAccessor;
2924

30-
final class Fabric8LeaderElectionInitiatorUtil {
25+
/**
26+
* @author wind57
27+
*/
28+
public final class LeaderElectionInitiatorUtil {
29+
30+
private static final LogAccessor LOG = new LogAccessor(LeaderElectionInitiatorUtil.class);
3131

32-
private static final LogAccessor LOG = new LogAccessor(Fabric8LeaderElectionInitiatorUtil.class);
32+
private LeaderElectionInitiatorUtil() {
3333

34-
private Fabric8LeaderElectionInitiatorUtil() {
34+
}
3535

36+
public static void blockReadinessCheck(CompletableFuture<?> ready) {
37+
try {
38+
ready.get();
39+
}
40+
catch (Exception e) {
41+
LOG.error(e, () -> "block readiness check failed with : " + e.getMessage());
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
46+
public static void shutDownExecutor(ExecutorService podReadyWaitingExecutor, String candidateIdentity) {
47+
LOG.debug(() -> "podReadyWaitingExecutor will be shutdown for : " + candidateIdentity);
48+
podReadyWaitingExecutor.shutdownNow();
49+
try {
50+
podReadyWaitingExecutor.awaitTermination(3, TimeUnit.SECONDS);
51+
}
52+
catch (InterruptedException e) {
53+
Thread.currentThread().interrupt();
54+
}
3655
}
3756

3857
/**
3958
* if 'ready' is already completed at this point, thread will run this, otherwise it
4059
* will attach the pipeline and move on to 'blockReadinessCheck'.
4160
*/
42-
static CompletableFuture<?> attachReadinessLoggerPipeline(CompletableFuture<?> innerPodReadyFuture,
61+
public static CompletableFuture<?> attachReadinessLoggerPipeline(CompletableFuture<?> innerPodReadyFuture,
4362
String candidateIdentity) {
4463
return innerPodReadyFuture.whenComplete((ok, error) -> {
4564
if (error != null) {
@@ -51,7 +70,7 @@ static CompletableFuture<?> attachReadinessLoggerPipeline(CompletableFuture<?> i
5170
});
5271
}
5372

54-
static void sleep(LeaderElectionProperties leaderElectionProperties) {
73+
public static void sleep(LeaderElectionProperties leaderElectionProperties) {
5574
try {
5675
TimeUnit.SECONDS.sleep(leaderElectionProperties.waitAfterRenewalFailure().toSeconds());
5776
}
@@ -60,29 +79,4 @@ static void sleep(LeaderElectionProperties leaderElectionProperties) {
6079
}
6180
}
6281

63-
static LeaderElector leaderElector(LeaderElectionConfig config, KubernetesClient fabric8KubernetesClient) {
64-
return fabric8KubernetesClient.leaderElector().withConfig(config).build();
65-
}
66-
67-
static void blockReadinessCheck(CompletableFuture<?> ready) {
68-
try {
69-
ready.get();
70-
}
71-
catch (Exception e) {
72-
LOG.error(e, () -> "block readiness check failed with : " + e.getMessage());
73-
throw new RuntimeException(e);
74-
}
75-
}
76-
77-
static void shutDownExecutor(ExecutorService podReadyWaitingExecutor, String candidateIdentity) {
78-
LOG.debug(() -> "podReadyWaitingExecutor will be shutdown for : " + candidateIdentity);
79-
podReadyWaitingExecutor.shutdownNow();
80-
try {
81-
podReadyWaitingExecutor.awaitTermination(3, TimeUnit.SECONDS);
82-
}
83-
catch (InterruptedException e) {
84-
Thread.currentThread().interrupt();
85-
}
86-
}
87-
8882
}

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ LeaderElectionConfig fabric8LeaderElectionConfig(LeaderElectionProperties proper
107107

108108
@Bean
109109
@ConditionalOnMissingBean
110-
Lock fabric8LeaderElectionLock(KubernetesClient fabric8KubernetesClient, LeaderElectionProperties properties, String candidateIdentity) {
110+
Lock fabric8LeaderElectionLock(KubernetesClient fabric8KubernetesClient, LeaderElectionProperties properties,
111+
String candidateIdentity) {
111112
boolean leaseSupported = fabric8KubernetesClient.getApiGroups()
112113
.getGroups()
113114
.stream()

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInitiator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131
import org.springframework.core.log.LogAccessor;
3232

3333
import static java.util.concurrent.Executors.newSingleThreadExecutor;
34-
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionInitiatorUtil.attachReadinessLoggerPipeline;
35-
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionInitiatorUtil.blockReadinessCheck;
36-
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionInitiatorUtil.leaderElector;
37-
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionInitiatorUtil.shutDownExecutor;
38-
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionInitiatorUtil.sleep;
34+
import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionInitiatorUtil.attachReadinessLoggerPipeline;
35+
import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionInitiatorUtil.blockReadinessCheck;
36+
import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionInitiatorUtil.shutDownExecutor;
37+
import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionInitiatorUtil.sleep;
3938

4039
/**
4140
* @author wind57
@@ -152,7 +151,7 @@ CompletableFuture<?> leaderFeature() {
152151

153152
private void startLeaderElection() {
154153

155-
leaderFuture = leaderElector(leaderElectionConfig, fabric8KubernetesClient).start();
154+
leaderFuture = fabric8KubernetesClient.leaderElector().withConfig(leaderElectionConfig).build().start();
156155

157156
leaderFuture.whenComplete((ok, error) -> {
158157

0 commit comments

Comments
 (0)