-
Notifications
You must be signed in to change notification settings - Fork 214
/
ClusterStatusAndHealthHelper.java
266 lines (228 loc) · 13.5 KB
/
ClusterStatusAndHealthHelper.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 2017 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.gateway.service.health;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Supplier;
import org.eclipse.ditto.base.service.CompletableFutureUtils;
import org.eclipse.ditto.gateway.service.util.config.health.HealthCheckConfig;
import org.eclipse.ditto.internal.utils.pekko.SimpleCommand;
import org.eclipse.ditto.internal.utils.pekko.SimpleCommandResponse;
import org.eclipse.ditto.internal.utils.health.StatusDetailMessage;
import org.eclipse.ditto.internal.utils.health.StatusInfo;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterRoleStatus;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterStatus;
import org.eclipse.ditto.internal.utils.health.status.StatusSupplierActor;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonValue;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.pattern.Patterns;
/**
* Helper for retrieving status and health information via the cluster.
*/
final class ClusterStatusAndHealthHelper {
private static final String JSON_KEY_ROLES = "roles";
private static final String JSON_KEY_EXPECTED_ROLES = "expected-roles";
private static final String JSON_KEY_MISSING_ROLES = "missing-roles";
private static final String JSON_KEY_EXTRA_ROLES = "extra-roles";
private static final String STATUS_SUPPLIER_PATH = "/user/" + StatusSupplierActor.ACTOR_NAME;
private final ActorSystem actorSystem;
private final Supplier<ClusterStatus> clusterStateSupplier;
private final HealthCheckConfig healthCheckConfig;
private ClusterStatusAndHealthHelper(final ActorSystem actorSystem,
final Supplier<ClusterStatus> clusterStateSupplier, final HealthCheckConfig healthCheckConfig) {
this.actorSystem = checkNotNull(actorSystem, "ActorSystem");
this.clusterStateSupplier = checkNotNull(clusterStateSupplier, "cluster state Supplier");
this.healthCheckConfig = checkNotNull(healthCheckConfig, "HealthCheckConfig");
}
/**
* Returns a new {@code ClusterStatusAndHealthHelper}.
*
* @param actorSystem the ActorSystem to use.
* @param clusterStateSupplier the {@link ClusterStatus} supplier to use in order to find out the reachable cluster
* nodes.
* @param healthCheckConfig the config of for health checking.
* @return the ClusterStatusAndHealthHelper.
* @throws NullPointerException if any argument is {@code null}.
*/
static ClusterStatusAndHealthHelper of(final ActorSystem actorSystem,
final Supplier<ClusterStatus> clusterStateSupplier, final HealthCheckConfig healthCheckConfig) {
return new ClusterStatusAndHealthHelper(actorSystem, clusterStateSupplier, healthCheckConfig);
}
/**
* Retrieves the static "status" information from each reachable cluster member in the cluster grouped by the
* cluster {@code role} of the services and containing the address in the cluster as additional Json key nodes. The
* result is wrapped as a JSON object with a single field with key {@link #JSON_KEY_ROLES}.
*
* @return a CompletionStage of a list of all "status" JsonObjects containing the cluster {@code role}s as keys.
*/
CompletionStage<JsonObject> retrieveOverallRolesStatus() {
final SimpleCommand command = SimpleCommand.of(StatusSupplierActor.SIMPLE_COMMAND_RETRIEVE_STATUS, null, null);
final List<CompletableFuture<JsonObject>> statuses =
clusterStateSupplier.get().getRoles().stream().map(clusterRoleStatus -> {
final String role = clusterRoleStatus.getRole();
final List<CompletableFuture<JsonObject>> statusList =
sendCommandToRemoteAddresses(actorSystem, command, clusterRoleStatus.getReachable(),
(o, throwable, addressString) -> {
if (throwable != null) {
return JsonObject.newBuilder()
.set(addressString, throwable.getMessage()).build();
} else {
return ((SimpleCommandResponse) o).getPayload()
.map(JsonValue::asObject)
.orElse(JsonObject.newBuilder().build());
}
});
final CompletableFuture<JsonObject> roleStatusFuture =
CompletableFutureUtils.collectAsList(statusList).thenApply(statusObjects -> {
final JsonObjectBuilder roleStatusBuilder = JsonFactory.newObjectBuilder();
statusObjects.forEach(subStatusObj -> {
String key = subStatusObj.getValue("instance")
.map(JsonValue::asString)
.orElse("?");
final JsonObjectBuilder subBuilder = JsonObject.newBuilder();
subStatusObj.forEach(subBuilder::set);
if (roleStatusBuilder.build().contains(key)) {
key = key + "_" + UUID.randomUUID().toString().hashCode();
}
roleStatusBuilder.set(key, subBuilder.build());
});
return roleStatusBuilder.build();
});
return roleStatusFuture.thenApply(rolesStatus ->
JsonObject.newBuilder().set(role, rolesStatus).build());
}).toList();
return CompletableFutureUtils.collectAsList(statuses).thenApply(roleStatuses -> {
final JsonObjectBuilder rolesStatusBuilder = JsonFactory.newObjectBuilder();
roleStatuses.forEach(subStatusObj -> subStatusObj.forEach(rolesStatusBuilder::set));
final JsonObjectBuilder rolesStatusWrapperBuilder = JsonFactory.newObjectBuilder();
rolesStatusWrapperBuilder.set(JSON_KEY_ROLES, rolesStatusBuilder.build());
return rolesStatusWrapperBuilder.build();
});
}
/**
* Retrieves the "health" information from each reachable cluster member in the ditto-cluster grouped by the cluster
* {@code role} of the services and containing the address in the cluster as additional Json key.
*
* @return a CompletionStage of a list of all "health" JsonObjects containing the cluster {@code role}s as keys.
*/
CompletionStage<StatusInfo> retrieveOverallRolesHealth() {
final ClusterStatus clusterStatus = clusterStateSupplier.get();
final List<CompletableFuture<StatusInfo>> healths = new ArrayList<>();
final HealthCheckConfig.ClusterRolesConfig clusterRolesConfig = healthCheckConfig.getClusterRolesConfig();
if (clusterRolesConfig.isEnabled()) {
final JsonArray expectedRoles = clusterRolesConfig.getExpectedClusterRoles()
.stream()
.sorted(Comparator.comparing(Function.identity()))
.map(JsonValue::of)
.collect(JsonCollectors.valuesToArray());
final JsonArray actualRoles = clusterStatus.getRoles()
.stream()
.map(ClusterRoleStatus::getRole)
.sorted(Comparator.comparing(Function.identity()))
.map(JsonValue::of)
.collect(JsonCollectors.valuesToArray());
final JsonArray missingRoles = expectedRoles.stream()
.filter(role -> !actualRoles.contains(role))
.collect(JsonCollectors.valuesToArray());
final JsonArray extraRoles = actualRoles.stream()
.filter(role -> !expectedRoles.contains(role))
.collect(JsonCollectors.valuesToArray());
final boolean allExpectedRolesPresent = missingRoles.isEmpty();
final StatusInfo.Status status = allExpectedRolesPresent ? StatusInfo.Status.UP : StatusInfo.Status.DOWN;
final StatusDetailMessage.Level level =
allExpectedRolesPresent ? StatusDetailMessage.Level.INFO : StatusDetailMessage.Level.ERROR;
final StatusDetailMessage statusDetailMessage = StatusDetailMessage.of(level, JsonObject.newBuilder()
.set(JSON_KEY_MISSING_ROLES, missingRoles)
.set(JSON_KEY_EXTRA_ROLES, extraRoles)
.build());
final StatusInfo statusInfo =
StatusInfo.fromStatus(status, Collections.singletonList(statusDetailMessage))
.label(JSON_KEY_EXPECTED_ROLES);
healths.add(CompletableFuture.completedFuture(statusInfo));
}
final SimpleCommand command = SimpleCommand.of(StatusSupplierActor.SIMPLE_COMMAND_RETRIEVE_HEALTH, null, null);
healths.addAll(clusterStatus.getRoles()
.stream()
.map(clusterRoleStatus -> {
final String roleLabel = clusterRoleStatus.getRole();
final Set<String> reachable = clusterRoleStatus.getReachable();
final List<CompletableFuture<StatusInfo>> remoteStatusFutures =
sendCommandToRemoteAddresses(
actorSystem, command, reachable,
(o, throwable, addressString) -> {
final StatusInfo statusInfo;
if (throwable != null) {
statusInfo =
StatusInfo.fromDetail(StatusDetailMessage.of(
StatusDetailMessage.Level.ERROR, "Exception: " +
throwable.getMessage()));
} else {
statusInfo = (StatusInfo) o;
}
return statusInfo.label(addressString);
});
return CompletableFutureUtils.collectAsList(remoteStatusFutures)
.thenApply(remoteStatuses -> {
if (reachable.isEmpty()) {
return StatusInfo.fromDetail(StatusDetailMessage.of(
StatusDetailMessage.Level.ERROR,
"Role is not available on any remote address"));
} else {
return StatusInfo.composite(remoteStatuses);
}
})
.thenApply(roleStatus -> roleStatus.label(roleLabel));
})
.toList());
return CompletableFutureUtils.collectAsList(healths)
.thenApply(statuses -> StatusInfo.composite(statuses).label(JSON_KEY_ROLES));
}
private <T> List<CompletableFuture<T>> sendCommandToRemoteAddresses(final ActorSystem actorSystem,
final SimpleCommand command,
final Collection<String> reachable,
final RemoteResponseTransformer<Object, Throwable, T> responseTransformer) {
final Duration healthCheckServiceTimeout = healthCheckConfig.getServiceTimeout();
return reachable.stream()
.map(addressString -> addressString + STATUS_SUPPLIER_PATH)
.map(actorSystem::actorSelection)
.map(selection -> {
final String addressString = selection.toSerializationFormat()
.substring(selection.toSerializationFormat().indexOf('@') + 1)
.replace(STATUS_SUPPLIER_PATH, "");
return Patterns.ask(selection, command, healthCheckServiceTimeout)
.handle((response, throwable) ->
responseTransformer.apply(response, throwable, addressString))
.toCompletableFuture();
})
.toList();
}
@FunctionalInterface
private interface RemoteResponseTransformer<T, U extends Throwable, R> {
R apply(T responseToBeMapped, U throwable, String addressString);
}
}