Skip to content

Commit e1a2517

Browse files
committed
Merge branch 'main' into move-to-a-common-configuration-for-health
2 parents b6e2d16 + 8ee9390 commit e1a2517

File tree

25 files changed

+635
-594
lines changed

25 files changed

+635
-594
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/Leader.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,19 +35,19 @@ public Leader(String role, String id) {
3535
}
3636

3737
public String getRole() {
38-
return this.role;
38+
return role;
3939
}
4040

4141
public String getId() {
42-
return this.id;
42+
return id;
4343
}
4444

4545
public boolean isCandidate(Candidate candidate) {
4646
if (candidate == null) {
4747
return false;
4848
}
4949

50-
return Objects.equals(this.role, candidate.getRole()) && Objects.equals(this.id, candidate.getId());
50+
return Objects.equals(role, candidate.getRole()) && Objects.equals(id, candidate.getId());
5151
}
5252

5353
@Override
@@ -62,17 +62,17 @@ public boolean equals(Object o) {
6262

6363
Leader leader = (Leader) o;
6464

65-
return Objects.equals(this.role, leader.role) && Objects.equals(this.id, leader.id);
65+
return Objects.equals(role, leader.role) && Objects.equals(id, leader.id);
6666
}
6767

6868
@Override
6969
public int hashCode() {
70-
return Objects.hash(this.role, this.id);
70+
return Objects.hash(role, id);
7171
}
7272

7373
@Override
7474
public String toString() {
75-
return String.format("Leader{role='%s', id='%s'}", this.role, this.id);
75+
return String.format("Leader{role='%s', id='%s'}", role, id);
7676
}
7777

7878
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderContext.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,17 @@ public LeaderContext(Candidate candidate, LeadershipController leadershipControl
3535

3636
@Override
3737
public boolean isLeader() {
38-
return this.leadershipController.getLocalLeader().filter(l -> l.isCandidate(this.candidate)).isPresent();
38+
return leadershipController.getLocalLeader().filter(l -> l.isCandidate(candidate)).isPresent();
3939
}
4040

4141
@Override
4242
public void yield() {
43-
this.leadershipController.revoke();
43+
leadershipController.revoke();
44+
}
45+
46+
@Override
47+
public String getRole() {
48+
return candidate.getRole();
4449
}
4550

4651
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInfoContributor.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2019 the original author or authors.
2+
* Copyright 2019-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21-
import java.util.Optional;
2221

2322
import org.springframework.boot.actuate.info.Info.Builder;
2423
import org.springframework.boot.actuate.info.InfoContributor;
@@ -38,16 +37,12 @@ public LeaderInfoContributor(LeadershipController leadershipController, Candidat
3837
@Override
3938
public void contribute(Builder builder) {
4039
Map<String, Object> details = new HashMap<>();
41-
Optional<Leader> leader = leadershipController.getLocalLeader();
42-
if (leader.isPresent()) {
43-
Leader l = leader.get();
44-
details.put("leaderId", l.getId());
45-
details.put("role", l.getRole());
46-
details.put("isLeader", l.isCandidate(candidate));
47-
}
48-
else {
49-
details.put("leaderId", "Unknown");
50-
}
40+
leadershipController.getLocalLeader().ifPresentOrElse(leader -> {
41+
details.put("leaderId", leader.getId());
42+
details.put("role", leader.getRole());
43+
details.put("isLeader", leader.isCandidate(candidate));
44+
}, () -> details.put("leaderId", "Unknown"));
45+
5146
builder.withDetail("leaderElection", details);
5247
}
5348

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInitiator.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2019 the original author or authors.
2+
* Copyright 2013-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,17 +20,15 @@
2020
import java.util.concurrent.ScheduledExecutorService;
2121
import java.util.concurrent.TimeUnit;
2222

23-
import org.slf4j.Logger;
24-
import org.slf4j.LoggerFactory;
25-
2623
import org.springframework.context.SmartLifecycle;
24+
import org.springframework.core.log.LogAccessor;
2725

2826
/**
2927
* @author Gytis Trikleris
3028
*/
3129
public class LeaderInitiator implements SmartLifecycle {
3230

33-
private static final Logger LOGGER = LoggerFactory.getLogger(LeaderInitiator.class);
31+
private static final LogAccessor LOGGER = new LogAccessor(LeaderInitiator.class);
3432

3533
private final LeaderProperties leaderProperties;
3634

@@ -54,33 +52,33 @@ public LeaderInitiator(LeaderProperties leaderProperties, LeadershipController l
5452

5553
@Override
5654
public boolean isAutoStartup() {
57-
return this.leaderProperties.isAutoStartup();
55+
return leaderProperties.isAutoStartup();
5856
}
5957

6058
@Override
6159
public void start() {
6260
if (!isRunning()) {
63-
LOGGER.debug("Leader initiator starting");
64-
this.leaderRecordWatcher.start();
65-
this.hostPodWatcher.start();
66-
this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
67-
this.scheduledExecutorService.scheduleAtFixedRate(this.leadershipController::update,
68-
this.leaderProperties.getUpdatePeriod().toMillis(),
69-
this.leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS);
70-
this.isRunning = true;
61+
LOGGER.debug(() -> "Leader initiator starting");
62+
leaderRecordWatcher.start();
63+
hostPodWatcher.start();
64+
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
65+
scheduledExecutorService.scheduleAtFixedRate(leadershipController::update,
66+
leaderProperties.getUpdatePeriod().toMillis(), leaderProperties.getUpdatePeriod().toMillis(),
67+
TimeUnit.MILLISECONDS);
68+
isRunning = true;
7169
}
7270
}
7371

7472
@Override
7573
public void stop() {
7674
if (isRunning()) {
77-
LOGGER.debug("Leader initiator stopping");
78-
this.scheduledExecutorService.shutdown();
79-
this.scheduledExecutorService = null;
80-
this.hostPodWatcher.stop();
81-
this.leaderRecordWatcher.stop();
82-
this.leadershipController.revoke();
83-
this.isRunning = false;
75+
LOGGER.debug(() -> "Leader initiator stopping");
76+
scheduledExecutorService.shutdown();
77+
scheduledExecutorService = null;
78+
hostPodWatcher.stop();
79+
leaderRecordWatcher.stop();
80+
leadershipController.revoke();
81+
isRunning = false;
8482
}
8583
}
8684

@@ -92,7 +90,7 @@ public void stop(Runnable callback) {
9290

9391
@Override
9492
public boolean isRunning() {
95-
return this.isRunning;
93+
return isRunning;
9694
}
9795

9896
@Override

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.util.StringUtils;
2223

2324
/**
2425
* @author Gytis Trikleris
@@ -105,7 +106,7 @@ public void setNamespace(String namespace) {
105106
}
106107

107108
public String getNamespace(String defaultValue) {
108-
if (namespace == null || namespace.isEmpty()) {
109+
if (!StringUtils.hasText(defaultValue)) {
109110
return defaultValue;
110111
}
111112

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.net.InetAddress;
2020
import java.net.UnknownHostException;
21+
import java.util.concurrent.locks.ReentrantLock;
2122

2223
import org.springframework.cloud.kubernetes.commons.EnvReader;
2324
import org.springframework.util.StringUtils;
@@ -44,4 +45,14 @@ public static String hostName() throws UnknownHostException {
4445
}
4546
}
4647

48+
public static void guarded(ReentrantLock lock, Runnable runnable) {
49+
try {
50+
lock.lock();
51+
runnable.run();
52+
}
53+
finally {
54+
lock.unlock();
55+
}
56+
}
57+
4758
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeadershipController.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import java.util.Objects;
2222
import java.util.Optional;
2323

24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
24+
import org.springframework.core.log.LogAccessor;
2725
import org.springframework.integration.leader.Candidate;
2826
import org.springframework.integration.leader.Context;
2927
import org.springframework.integration.leader.event.LeaderEventPublisher;
@@ -34,7 +32,7 @@
3432
*/
3533
public abstract class LeadershipController {
3634

37-
private static final Logger LOGGER = LoggerFactory.getLogger(LeadershipController.class);
35+
private static final LogAccessor LOGGER = new LogAccessor(LeadershipController.class);
3836

3937
protected static final String PROVIDER_KEY = "provider";
4038

@@ -62,15 +60,15 @@ public LeadershipController(Candidate candidate, LeaderProperties leaderProperti
6260
}
6361

6462
public Optional<Leader> getLocalLeader() {
65-
return Optional.ofNullable(this.localLeader);
63+
return Optional.ofNullable(localLeader);
6664
}
6765

6866
public abstract void update();
6967

7068
public abstract void revoke();
7169

7270
protected String getLeaderKey() {
73-
return this.leaderProperties.getLeaderIdPrefix() + this.candidate.getRole();
71+
return leaderProperties.getLeaderIdPrefix() + candidate.getRole();
7472
}
7573

7674
protected Map<String, String> getLeaderData(Candidate candidate) {
@@ -85,72 +83,73 @@ protected Leader extractLeader(Map<String, String> data) {
8583

8684
String leaderKey = getLeaderKey();
8785
String leaderId = data.get(leaderKey);
86+
LOGGER.debug(() -> "retrieved leaderId: " + leaderId + " from leaderKey : " + leaderId);
8887
if (!StringUtils.hasText(leaderId)) {
8988
return null;
9089
}
9190

92-
return new Leader(this.candidate.getRole(), leaderId);
91+
return new Leader(candidate.getRole(), leaderId);
9392
}
9493

9594
protected void handleLeaderChange(Leader newLeader) {
96-
if (Objects.equals(this.localLeader, newLeader)) {
97-
LOGGER.debug("Leader is still '{}'", this.localLeader);
95+
if (Objects.equals(localLeader, newLeader)) {
96+
LOGGER.debug(() -> "Leader is still : " + localLeader);
9897
return;
9998
}
10099

101-
Leader oldLeader = this.localLeader;
102-
this.localLeader = newLeader;
100+
Leader oldLeader = localLeader;
101+
localLeader = newLeader;
103102

104-
if (oldLeader != null && oldLeader.isCandidate(this.candidate)) {
103+
if (oldLeader != null && oldLeader.isCandidate(candidate)) {
105104
notifyOnRevoked();
106105
}
107-
else if (newLeader != null && newLeader.isCandidate(this.candidate)) {
106+
else if (newLeader != null && newLeader.isCandidate(candidate)) {
108107
notifyOnGranted();
109108
}
110109

111110
restartLeaderReadinessWatcher();
112111

113-
LOGGER.debug("New leader is '{}'", this.localLeader);
112+
LOGGER.debug(() -> "New leader is " + localLeader);
114113
}
115114

116115
protected void notifyOnGranted() {
117-
LOGGER.debug("Leadership has been granted for '{}'", this.candidate);
116+
LOGGER.debug(() -> "Leadership has been granted to : " + candidate);
118117

119-
Context context = new LeaderContext(this.candidate, this);
120-
this.leaderEventPublisher.publishOnGranted(this, context, this.candidate.getRole());
118+
Context context = new LeaderContext(candidate, this);
119+
leaderEventPublisher.publishOnGranted(this, context, candidate.getRole());
121120
try {
122-
this.candidate.onGranted(context);
121+
candidate.onGranted(context);
123122
}
124123
catch (InterruptedException e) {
125-
LOGGER.warn(e.getMessage());
124+
LOGGER.warn(e::getMessage);
126125
Thread.currentThread().interrupt();
127126
}
128127
}
129128

130129
protected void notifyOnRevoked() {
131-
LOGGER.debug("Leadership has been revoked for '{}'", this.candidate);
130+
LOGGER.debug(() -> "Leadership has been revoked from :" + candidate);
132131

133-
Context context = new LeaderContext(this.candidate, this);
134-
this.leaderEventPublisher.publishOnRevoked(this, context, this.candidate.getRole());
135-
this.candidate.onRevoked(context);
132+
Context context = new LeaderContext(candidate, this);
133+
leaderEventPublisher.publishOnRevoked(this, context, candidate.getRole());
134+
candidate.onRevoked(context);
136135
}
137136

138137
protected void notifyOnFailedToAcquire() {
139-
if (this.leaderProperties.isPublishFailedEvents()) {
140-
Context context = new LeaderContext(this.candidate, this);
141-
this.leaderEventPublisher.publishOnFailedToAcquire(this, context, this.candidate.getRole());
138+
if (leaderProperties.isPublishFailedEvents()) {
139+
Context context = new LeaderContext(candidate, this);
140+
leaderEventPublisher.publishOnFailedToAcquire(this, context, candidate.getRole());
142141
}
143142
}
144143

145144
protected void restartLeaderReadinessWatcher() {
146-
if (this.leaderReadinessWatcher != null) {
147-
this.leaderReadinessWatcher.stop();
148-
this.leaderReadinessWatcher = null;
145+
if (leaderReadinessWatcher != null) {
146+
leaderReadinessWatcher.stop();
147+
leaderReadinessWatcher = null;
149148
}
150149

151-
if (this.localLeader != null && !this.localLeader.isCandidate(this.candidate)) {
152-
this.leaderReadinessWatcher = createPodReadinessWatcher(this.localLeader.getId());
153-
this.leaderReadinessWatcher.start();
150+
if (localLeader != null && !localLeader.isCandidate(candidate)) {
151+
leaderReadinessWatcher = createPodReadinessWatcher(localLeader.getId());
152+
leaderReadinessWatcher.start();
154153
}
155154
}
156155

0 commit comments

Comments
 (0)