Skip to content

Commit 733d9c6

Browse files
authored
Apply fix for health API response to distinguish no master (#819)
Signed-off-by: Mohit Godwani <mgodwan@amazon.com>
1 parent b4c697a commit 733d9c6

File tree

6 files changed

+135
-11
lines changed

6 files changed

+135
-11
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/cat.health/10_basic.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
status .+ \n
1313
node.total .+ \n
1414
node.data .+ \n
15+
discovered_master .+ \n
1516
shards .+ \n
1617
pri .+ \n
1718
relo .+ \n
@@ -39,6 +40,7 @@
3940
\w+ \s+ # status
4041
\d+ \s+ # node.total
4142
\d+ \s+ # node.data
43+
\w+ \s+ # discovered_master
4244
\d+ \s+ # shards
4345
\d+ \s+ # pri
4446
\d+ \s+ # relo
@@ -66,6 +68,7 @@
6668
\w+ \s+ # status
6769
\d+ \s+ # node.total
6870
\d+ \s+ # node.data
71+
\w+ \s+ # discovered_master
6972
\d+ \s+ # shards
7073
\d+ \s+ # pri
7174
\d+ \s+ # relo

server/src/main/java/org/opensearch/action/admin/cluster/health/ClusterHealthResponse.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
6666
private static final String TIMED_OUT = "timed_out";
6767
private static final String NUMBER_OF_NODES = "number_of_nodes";
6868
private static final String NUMBER_OF_DATA_NODES = "number_of_data_nodes";
69+
private static final String DISCOVERED_MASTER = "discovered_master";
6970
private static final String NUMBER_OF_PENDING_TASKS = "number_of_pending_tasks";
7071
private static final String NUMBER_OF_IN_FLIGHT_FETCH = "number_of_in_flight_fetch";
7172
private static final String DELAYED_UNASSIGNED_SHARDS = "delayed_unassigned_shards";
@@ -87,6 +88,7 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
8788
// ClusterStateHealth fields
8889
int numberOfNodes = (int) parsedObjects[i++];
8990
int numberOfDataNodes = (int) parsedObjects[i++];
91+
boolean hasDiscoveredMaster = (boolean) parsedObjects[i++];
9092
int activeShards = (int) parsedObjects[i++];
9193
int relocatingShards = (int) parsedObjects[i++];
9294
int activePrimaryShards = (int) parsedObjects[i++];
@@ -106,9 +108,8 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
106108
}
107109
}
108110
ClusterStateHealth stateHealth = new ClusterStateHealth(activePrimaryShards, activeShards, relocatingShards,
109-
initializingShards, unassignedShards, numberOfNodes, numberOfDataNodes, activeShardsPercent, status,
110-
indices);
111-
111+
initializingShards, unassignedShards, numberOfNodes, numberOfDataNodes, hasDiscoveredMaster,
112+
activeShardsPercent, status, indices);
112113
// ClusterHealthResponse fields
113114
String clusterName = (String) parsedObjects[i++];
114115
int numberOfPendingTasks = (int) parsedObjects[i++];
@@ -127,6 +128,7 @@ public class ClusterHealthResponse extends ActionResponse implements StatusToXCo
127128
// ClusterStateHealth fields
128129
PARSER.declareInt(constructorArg(), new ParseField(NUMBER_OF_NODES));
129130
PARSER.declareInt(constructorArg(), new ParseField(NUMBER_OF_DATA_NODES));
131+
PARSER.declareBoolean(constructorArg(), new ParseField(DISCOVERED_MASTER));
130132
PARSER.declareInt(constructorArg(), new ParseField(ACTIVE_SHARDS));
131133
PARSER.declareInt(constructorArg(), new ParseField(RELOCATING_SHARDS));
132134
PARSER.declareInt(constructorArg(), new ParseField(ACTIVE_PRIMARY_SHARDS));
@@ -237,6 +239,10 @@ public int getNumberOfDataNodes() {
237239
return clusterStateHealth.getNumberOfDataNodes();
238240
}
239241

242+
public boolean hasDiscoveredMaster() {
243+
return clusterStateHealth.hasDiscoveredMaster();
244+
}
245+
240246
public int getNumberOfPendingTasks() {
241247
return this.numberOfPendingTasks;
242248
}
@@ -334,6 +340,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
334340
builder.field(TIMED_OUT, isTimedOut());
335341
builder.field(NUMBER_OF_NODES, getNumberOfNodes());
336342
builder.field(NUMBER_OF_DATA_NODES, getNumberOfDataNodes());
343+
builder.field(DISCOVERED_MASTER, hasDiscoveredMaster());
337344
builder.field(ACTIVE_PRIMARY_SHARDS, getActivePrimaryShards());
338345
builder.field(ACTIVE_SHARDS, getActiveShards());
339346
builder.field(RELOCATING_SHARDS, getRelocatingShards());

server/src/main/java/org/opensearch/cluster/health/ClusterStateHealth.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package org.opensearch.cluster.health;
3333

34+
import org.opensearch.Version;
3435
import org.opensearch.cluster.ClusterState;
3536
import org.opensearch.cluster.metadata.IndexMetadata;
3637
import org.opensearch.cluster.routing.IndexRoutingTable;
@@ -53,6 +54,7 @@ public final class ClusterStateHealth implements Iterable<ClusterIndexHealth>, W
5354

5455
private final int numberOfNodes;
5556
private final int numberOfDataNodes;
57+
private final boolean hasDiscoveredMaster;
5658
private final int activeShards;
5759
private final int relocatingShards;
5860
private final int activePrimaryShards;
@@ -80,6 +82,7 @@ public ClusterStateHealth(final ClusterState clusterState) {
8082
public ClusterStateHealth(final ClusterState clusterState, final String[] concreteIndices) {
8183
numberOfNodes = clusterState.nodes().getSize();
8284
numberOfDataNodes = clusterState.nodes().getDataNodes().size();
85+
hasDiscoveredMaster = clusterState.nodes().getMasterNodeId() != null;
8386
indices = new HashMap<>();
8487
for (String index : concreteIndices) {
8588
IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(index);
@@ -147,6 +150,11 @@ public ClusterStateHealth(final StreamInput in) throws IOException {
147150
unassignedShards = in.readVInt();
148151
numberOfNodes = in.readVInt();
149152
numberOfDataNodes = in.readVInt();
153+
if (in.getVersion().onOrAfter(Version.V_1_0_0)) {
154+
hasDiscoveredMaster = in.readBoolean();
155+
} else {
156+
hasDiscoveredMaster = true;
157+
}
150158
status = ClusterHealthStatus.fromValue(in.readByte());
151159
int size = in.readVInt();
152160
indices = new HashMap<>(size);
@@ -161,7 +169,7 @@ public ClusterStateHealth(final StreamInput in) throws IOException {
161169
* For ClusterHealthResponse's XContent Parser
162170
*/
163171
public ClusterStateHealth(int activePrimaryShards, int activeShards, int relocatingShards, int initializingShards, int unassignedShards,
164-
int numberOfNodes, int numberOfDataNodes, double activeShardsPercent, ClusterHealthStatus status,
172+
int numberOfNodes, int numberOfDataNodes, boolean hasDiscoveredMaster, double activeShardsPercent, ClusterHealthStatus status,
165173
Map<String, ClusterIndexHealth> indices) {
166174
this.activePrimaryShards = activePrimaryShards;
167175
this.activeShards = activeShards;
@@ -170,6 +178,7 @@ public ClusterStateHealth(int activePrimaryShards, int activeShards, int relocat
170178
this.unassignedShards = unassignedShards;
171179
this.numberOfNodes = numberOfNodes;
172180
this.numberOfDataNodes = numberOfDataNodes;
181+
this.hasDiscoveredMaster = hasDiscoveredMaster;
173182
this.activeShardsPercent = activeShardsPercent;
174183
this.status = status;
175184
this.indices = indices;
@@ -215,6 +224,10 @@ public double getActiveShardsPercent() {
215224
return activeShardsPercent;
216225
}
217226

227+
public boolean hasDiscoveredMaster() {
228+
return hasDiscoveredMaster;
229+
}
230+
218231
@Override
219232
public Iterator<ClusterIndexHealth> iterator() {
220233
return indices.values().iterator();
@@ -229,6 +242,9 @@ public void writeTo(final StreamOutput out) throws IOException {
229242
out.writeVInt(unassignedShards);
230243
out.writeVInt(numberOfNodes);
231244
out.writeVInt(numberOfDataNodes);
245+
if (out.getVersion().onOrAfter(Version.V_1_0_0)) {
246+
out.writeBoolean(hasDiscoveredMaster);
247+
}
232248
out.writeByte(status.value());
233249
out.writeVInt(indices.size());
234250
for (ClusterIndexHealth indexHealth : this) {
@@ -242,6 +258,7 @@ public String toString() {
242258
return "ClusterStateHealth{" +
243259
"numberOfNodes=" + numberOfNodes +
244260
", numberOfDataNodes=" + numberOfDataNodes +
261+
", hasDiscoveredMaster=" + hasDiscoveredMaster +
245262
", activeShards=" + activeShards +
246263
", relocatingShards=" + relocatingShards +
247264
", activePrimaryShards=" + activePrimaryShards +
@@ -260,6 +277,7 @@ public boolean equals(Object o) {
260277
ClusterStateHealth that = (ClusterStateHealth) o;
261278
return numberOfNodes == that.numberOfNodes &&
262279
numberOfDataNodes == that.numberOfDataNodes &&
280+
hasDiscoveredMaster == that.hasDiscoveredMaster &&
263281
activeShards == that.activeShards &&
264282
relocatingShards == that.relocatingShards &&
265283
activePrimaryShards == that.activePrimaryShards &&
@@ -272,7 +290,7 @@ public boolean equals(Object o) {
272290

273291
@Override
274292
public int hashCode() {
275-
return Objects.hash(numberOfNodes, numberOfDataNodes, activeShards, relocatingShards, activePrimaryShards, initializingShards,
276-
unassignedShards, activeShardsPercent, status, indices);
293+
return Objects.hash(numberOfNodes, numberOfDataNodes, hasDiscoveredMaster, activeShards, relocatingShards,
294+
activePrimaryShards, initializingShards, unassignedShards, activeShardsPercent, status, indices);
277295
}
278296
}

server/src/main/java/org/opensearch/rest/action/cat/RestHealthAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected Table getTableWithHeader(final RestRequest request) {
8989
t.addCell("status", "alias:st;desc:health status");
9090
t.addCell("node.total", "alias:nt,nodeTotal;text-align:right;desc:total number of nodes");
9191
t.addCell("node.data", "alias:nd,nodeData;text-align:right;desc:number of nodes that can store data");
92+
t.addCell("discovered_master", "alias:dm;text-align:right;desc:discovered master");
9293
t.addCell("shards", "alias:t,sh,shards.total,shardsTotal;text-align:right;desc:total number of shards");
9394
t.addCell("pri", "alias:p,shards.primary,shardsPrimary;text-align:right;desc:number of primary shards");
9495
t.addCell("relo", "alias:r,shards.relocating,shardsRelocating;text-align:right;desc:number of relocating nodes");
@@ -109,6 +110,7 @@ private Table buildTable(final ClusterHealthResponse health, final RestRequest r
109110
t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT));
110111
t.addCell(health.getNumberOfNodes());
111112
t.addCell(health.getNumberOfDataNodes());
113+
t.addCell(health.hasDiscoveredMaster());
112114
t.addCell(health.getActiveShards());
113115
t.addCell(health.getActivePrimaryShards());
114116
t.addCell(health.getRelocatingShards());

0 commit comments

Comments
 (0)