Skip to content

Commit d64ebaa

Browse files
authored
[automatic failover] Replace 'SimpleEntry' with 'HealthCheckResult' (#4236)
* - replace SimpleEntry with HealthCheckResult * - format
1 parent e195ddd commit d64ebaa

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/main/java/redis/clients/jedis/mcf/HealthCheck.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package redis.clients.jedis.mcf;
33

4-
import java.util.AbstractMap.SimpleEntry;
54
import java.util.concurrent.ExecutionException;
65
import java.util.concurrent.ExecutorService;
76
import java.util.concurrent.Executors;
@@ -17,11 +16,29 @@
1716

1817
public class HealthCheck {
1918

19+
private static class HealthCheckResult {
20+
private final long timestamp;
21+
private final HealthStatus status;
22+
23+
public HealthCheckResult(long timestamp, HealthStatus status) {
24+
this.timestamp = timestamp;
25+
this.status = status;
26+
}
27+
28+
public long getTimestamp() {
29+
return timestamp;
30+
}
31+
32+
public HealthStatus getStatus() {
33+
return status;
34+
}
35+
}
36+
2037
private static final Logger log = LoggerFactory.getLogger(HealthCheck.class);
2138

2239
private Endpoint endpoint;
2340
private HealthCheckStrategy strategy;
24-
private AtomicReference<SimpleEntry<Long, HealthStatus>> statusRef = new AtomicReference<SimpleEntry<Long, HealthStatus>>();
41+
private AtomicReference<HealthCheckResult> resultRef = new AtomicReference<HealthCheckResult>();
2542
private Consumer<HealthStatusChangeEvent> statusChangeCallback;
2643

2744
private ScheduledExecutorService scheduler;
@@ -32,7 +49,7 @@ public class HealthCheck {
3249
this.endpoint = endpoint;
3350
this.strategy = strategy;
3451
this.statusChangeCallback = statusChangeCallback;
35-
statusRef.set(new SimpleEntry<>(0L, HealthStatus.UNKNOWN));
52+
resultRef.set(new HealthCheckResult(0L, HealthStatus.UNKNOWN));
3653

3754
scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
3855
Thread t = new Thread(r, "jedis-healthcheck-" + this.endpoint);
@@ -46,7 +63,7 @@ public Endpoint getEndpoint() {
4663
}
4764

4865
public HealthStatus getStatus() {
49-
return statusRef.get().getValue();
66+
return resultRef.get().getStatus();
5067
}
5168

5269
public void start() {
@@ -100,16 +117,16 @@ private void healthCheck() {
100117

101118
// just to avoid to replace status with an outdated result from another healthCheck
102119
private void safeUpdate(long owner, HealthStatus status) {
103-
SimpleEntry<Long, HealthStatus> newStatus = new SimpleEntry<>(owner, status);
104-
SimpleEntry<Long, HealthStatus> oldStatus = statusRef.getAndUpdate(current -> {
105-
if (current.getKey() < owner) {
106-
return newStatus;
120+
HealthCheckResult newResult = new HealthCheckResult(owner, status);
121+
HealthCheckResult oldResult = resultRef.getAndUpdate(current -> {
122+
if (current.getTimestamp() < owner) {
123+
return newResult;
107124
}
108125
return current;
109126
});
110-
if (oldStatus.getValue() != status) {
127+
if (oldResult.getStatus() != status) {
111128
// notify listeners
112-
notifyListeners(oldStatus.getValue(), status);
129+
notifyListeners(oldResult.getStatus(), status);
113130
}
114131
}
115132

0 commit comments

Comments
 (0)