-
Notifications
You must be signed in to change notification settings - Fork 214
/
ClusterRoleStatus.java
executable file
·157 lines (135 loc) · 5.03 KB
/
ClusterRoleStatus.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
/*
* 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.internal.utils.health.cluster;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.json.Jsonifiable;
/**
* Holds the status of a specific pekko cluster role.
*/
@Immutable
public final class ClusterRoleStatus implements Jsonifiable<JsonObject> {
/**
* JSON array of reachable members.
*/
public static final JsonFieldDefinition<JsonArray> JSON_KEY_REACHABLE =
JsonFactory.newJsonArrayFieldDefinition("reachable");
/**
* JSON array of unreachable members.
*/
public static final JsonFieldDefinition<JsonArray> JSON_KEY_UNREACHABLE =
JsonFactory.newJsonArrayFieldDefinition("unreachable");
/**
* JSON value of the leaders address.
*/
public static final JsonFieldDefinition<String> JSON_KEY_LEADER = JsonFactory.newStringFieldDefinition("leader");
private final String role;
private final Set<String> reachable;
private final Set<String> unreachable;
private final String leader;
private ClusterRoleStatus(final String role,
final Set<String> reachable,
final Set<String> unreachable,
final String leader) {
this.role = role;
this.reachable = Collections.unmodifiableSet(reachable);
this.unreachable = Collections.unmodifiableSet(unreachable);
this.leader = leader;
}
/**
* Returns a new {@code ClusterRoleStatus} instance with the specified {@code leader}, {@code reachable} and {@code
* unreachable} members.
*
* @param role the cluster role.
* @param reachable a list of all reachable members.
* @param unreachable a list of all unreachable members.
* @param leader the leader.
* @return the ClusterRoleStatus instance.
*/
public static ClusterRoleStatus of(final String role, final Set<String> reachable, final Set<String> unreachable,
final String leader) {
return new ClusterRoleStatus(role, reachable, unreachable, leader);
}
/**
* Returns the cluster role.
*
* @return the cluster role.
*/
public String getRole() { return role; }
/**
* Returns a list of all reachable members for the given {@code role}.
*
* @return a list of all reachable members for the given {@code role}.
*/
public Set<String> getReachable() {
return reachable;
}
/**
* Returns a list of all unreachable members for the given {@code role}.
*
* @return a list of all unreachable members for the given {@code role}.
*/
public Set<String> getUnreachable() {
return unreachable;
}
/**
* Returns the optional leader for the given {@code role}.
*
* @return the leader or an empty optional.
*/
public Optional<String> getLeader() {
return Optional.ofNullable(leader);
}
@Override
public JsonObject toJson() {
final JsonObjectBuilder jsonObjectBuilder = JsonFactory.newObjectBuilder();
jsonObjectBuilder.set(JSON_KEY_REACHABLE,
reachable.stream().map(JsonValue::of).collect(JsonCollectors.valuesToArray()));
jsonObjectBuilder.set(JSON_KEY_UNREACHABLE,
unreachable.stream().map(JsonValue::of).collect(JsonCollectors.valuesToArray()));
jsonObjectBuilder.set(JSON_KEY_LEADER, getLeader().orElse("<unknown>"));
return jsonObjectBuilder.build();
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ClusterRoleStatus)) {
return false;
}
final ClusterRoleStatus that = (ClusterRoleStatus) o;
return Objects.equals(reachable, that.reachable) && Objects.equals(unreachable, that.unreachable) && Objects
.equals(role, that.role) && Objects.equals(leader, that.leader);
}
@Override
public int hashCode() {
return Objects.hash(role, reachable, unreachable, leader);
}
@Override
public String toString() {
return getClass().getSimpleName() + "[" + "role=" + role + ", reachable=" + reachable + ", unreachable="
+ unreachable + ", leader='" + leader + '\'' + ']';
}
}