3131
3232package org .opensearch .cluster .health ;
3333
34+ import org .opensearch .Version ;
3435import org .opensearch .cluster .ClusterState ;
3536import org .opensearch .cluster .metadata .IndexMetadata ;
3637import 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}
0 commit comments