-
Notifications
You must be signed in to change notification settings - Fork 214
/
SingletonStatusReporter.java
69 lines (59 loc) · 2.47 KB
/
SingletonStatusReporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.health;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Actor to report health status for a singleton on and only on the role leader of a cluster.
*/
public final class SingletonStatusReporter extends AbstractHealthCheckingActor {
private static final StatusInfo NOT_MY_BUSINESS =
StatusInfo.fromStatus(StatusInfo.Status.UP, "this is not the oldest member.");
private final Cluster cluster = Cluster.get(getContext().getSystem());
private final String clusterRole;
private final ActorRef healthReportingSingletonProxy;
@SuppressWarnings("unused")
private SingletonStatusReporter(final String clusterRole, final ActorRef healthReportingSingletonProxy) {
this.clusterRole = clusterRole;
this.healthReportingSingletonProxy = healthReportingSingletonProxy;
}
/**
* Create Props actor for this actor.
*
* @param clusterRole cluster role where the singleton starts.
* @param cleanupCoordinatorProxy proxy of the cluster singleton cleanup coordinator.
* @return Props to report status of the cleanup coordinator.
*/
public static Props props(final String clusterRole, final ActorRef cleanupCoordinatorProxy) {
return Props.create(SingletonStatusReporter.class, clusterRole, cleanupCoordinatorProxy);
}
@Override
protected Receive matchCustomMessages() {
return ReceiveBuilder.create()
.match(RetrieveHealthResponse.class, response -> updateHealth(response.getStatusInfo()))
.build();
}
@Override
protected void triggerHealthRetrieval() {
if (isThisClusterRoleLeader()) {
healthReportingSingletonProxy.tell(RetrieveHealth.newInstance(), getSelf());
} else {
updateHealth(NOT_MY_BUSINESS);
}
}
private boolean isThisClusterRoleLeader() {
return cluster.selfAddress().equals(cluster.state().getRoleLeader(clusterRole));
}
}