2222import com .carrotsearch .hppc .cursors .IntObjectCursor ;
2323import com .carrotsearch .hppc .cursors .ObjectCursor ;
2424import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
25+
26+ import org .elasticsearch .Version ;
2527import org .elasticsearch .client .transport .TransportClient ;
2628import org .elasticsearch .cluster .block .ClusterBlock ;
2729import org .elasticsearch .cluster .block .ClusterBlocks ;
@@ -172,16 +174,19 @@ default boolean isPrivate() {
172174
173175 private final boolean wasReadFromDiff ;
174176
177+ private final int minimumMasterNodesOnPublishingMaster ;
178+
175179 // built on demand
176180 private volatile RoutingNodes routingNodes ;
177181
178182 public ClusterState (long version , String stateUUID , ClusterState state ) {
179183 this (state .clusterName , version , stateUUID , state .metaData (), state .routingTable (), state .nodes (), state .blocks (), state .customs (),
180- false );
184+ - 1 , false );
181185 }
182186
183187 public ClusterState (ClusterName clusterName , long version , String stateUUID , MetaData metaData , RoutingTable routingTable ,
184- DiscoveryNodes nodes , ClusterBlocks blocks , ImmutableOpenMap <String , Custom > customs , boolean wasReadFromDiff ) {
188+ DiscoveryNodes nodes , ClusterBlocks blocks , ImmutableOpenMap <String , Custom > customs ,
189+ int minimumMasterNodesOnPublishingMaster , boolean wasReadFromDiff ) {
185190 this .version = version ;
186191 this .stateUUID = stateUUID ;
187192 this .clusterName = clusterName ;
@@ -190,6 +195,7 @@ public ClusterState(ClusterName clusterName, long version, String stateUUID, Met
190195 this .nodes = nodes ;
191196 this .blocks = blocks ;
192197 this .customs = customs ;
198+ this .minimumMasterNodesOnPublishingMaster = minimumMasterNodesOnPublishingMaster ;
193199 this .wasReadFromDiff = wasReadFromDiff ;
194200 }
195201
@@ -257,6 +263,17 @@ public ClusterName getClusterName() {
257263 return this .clusterName ;
258264 }
259265
266+ /**
267+ * The node-level `discovery.zen.minimum_master_nodes` setting on the master node that published this cluster state, for use in rolling
268+ * upgrades from 6.x to 7.x. Once all the 6.x master-eligible nodes have left the cluster, the 7.x nodes use this value to determine how
269+ * many master-eligible nodes must be discovered before the cluster can be bootstrapped. Note that this method returns the node-level
270+ * value of this setting, and ignores any cluster-level override that was set via the API. Callers are expected to combine this value
271+ * with any value set in the cluster-level settings. This should be removed once we no longer need support for {@link Version#V_6_7_0}.
272+ */
273+ public int getMinimumMasterNodesOnPublishingMaster () {
274+ return minimumMasterNodesOnPublishingMaster ;
275+ }
276+
260277 // Used for testing and logging to determine how this cluster state was send over the wire
261278 public boolean wasReadFromDiff () {
262279 return wasReadFromDiff ;
@@ -598,7 +615,7 @@ public static class Builder {
598615 private ClusterBlocks blocks = ClusterBlocks .EMPTY_CLUSTER_BLOCK ;
599616 private final ImmutableOpenMap .Builder <String , Custom > customs ;
600617 private boolean fromDiff ;
601-
618+ private int minimumMasterNodesOnPublishingMaster = - 1 ;
602619
603620 public Builder (ClusterState state ) {
604621 this .clusterName = state .clusterName ;
@@ -609,6 +626,7 @@ public Builder(ClusterState state) {
609626 this .metaData = state .metaData ();
610627 this .blocks = state .blocks ();
611628 this .customs = ImmutableOpenMap .builder (state .customs ());
629+ this .minimumMasterNodesOnPublishingMaster = state .minimumMasterNodesOnPublishingMaster ;
612630 this .fromDiff = false ;
613631 }
614632
@@ -669,6 +687,11 @@ public Builder stateUUID(String uuid) {
669687 return this ;
670688 }
671689
690+ public Builder minimumMasterNodesOnPublishingMaster (int minimumMasterNodesOnPublishingMaster ) {
691+ this .minimumMasterNodesOnPublishingMaster = minimumMasterNodesOnPublishingMaster ;
692+ return this ;
693+ }
694+
672695 public Builder putCustom (String type , Custom custom ) {
673696 customs .put (type , custom );
674697 return this ;
@@ -693,7 +716,8 @@ public ClusterState build() {
693716 if (UNKNOWN_UUID .equals (uuid )) {
694717 uuid = UUIDs .randomBase64UUID ();
695718 }
696- return new ClusterState (clusterName , version , uuid , metaData , routingTable , nodes , blocks , customs .build (), fromDiff );
719+ return new ClusterState (clusterName , version , uuid , metaData , routingTable , nodes , blocks , customs .build (),
720+ minimumMasterNodesOnPublishingMaster , fromDiff );
697721 }
698722
699723 public static byte [] toBytes (ClusterState state ) throws IOException {
@@ -736,6 +760,7 @@ public static ClusterState readFrom(StreamInput in, DiscoveryNode localNode) thr
736760 Custom customIndexMetaData = in .readNamedWriteable (Custom .class );
737761 builder .putCustom (customIndexMetaData .getWriteableName (), customIndexMetaData );
738762 }
763+ builder .minimumMasterNodesOnPublishingMaster = in .getVersion ().onOrAfter (Version .V_6_7_0 ) ? in .readVInt () : -1 ;
739764 return builder .build ();
740765 }
741766
@@ -761,6 +786,9 @@ public void writeTo(StreamOutput out) throws IOException {
761786 out .writeNamedWriteable (cursor .value );
762787 }
763788 }
789+ if (out .getVersion ().onOrAfter (Version .V_6_7_0 )) {
790+ out .writeVInt (minimumMasterNodesOnPublishingMaster );
791+ }
764792 }
765793
766794 private static class ClusterStateDiff implements Diff <ClusterState > {
@@ -783,6 +811,8 @@ private static class ClusterStateDiff implements Diff<ClusterState> {
783811
784812 private final Diff <ImmutableOpenMap <String , Custom >> customs ;
785813
814+ private final int minimumMasterNodesOnPublishingMaster ;
815+
786816 ClusterStateDiff (ClusterState before , ClusterState after ) {
787817 fromUuid = before .stateUUID ;
788818 toUuid = after .stateUUID ;
@@ -793,6 +823,7 @@ private static class ClusterStateDiff implements Diff<ClusterState> {
793823 metaData = after .metaData .diff (before .metaData );
794824 blocks = after .blocks .diff (before .blocks );
795825 customs = DiffableUtils .diff (before .customs , after .customs , DiffableUtils .getStringKeySerializer (), CUSTOM_VALUE_SERIALIZER );
826+ minimumMasterNodesOnPublishingMaster = after .minimumMasterNodesOnPublishingMaster ;
796827 }
797828
798829 ClusterStateDiff (StreamInput in , DiscoveryNode localNode ) throws IOException {
@@ -805,6 +836,7 @@ private static class ClusterStateDiff implements Diff<ClusterState> {
805836 metaData = MetaData .readDiffFrom (in );
806837 blocks = ClusterBlocks .readDiffFrom (in );
807838 customs = DiffableUtils .readImmutableOpenMapDiff (in , DiffableUtils .getStringKeySerializer (), CUSTOM_VALUE_SERIALIZER );
839+ minimumMasterNodesOnPublishingMaster = in .getVersion ().onOrAfter (Version .V_6_7_0 ) ? in .readVInt () : -1 ;
808840 }
809841
810842 @ Override
@@ -818,6 +850,9 @@ public void writeTo(StreamOutput out) throws IOException {
818850 metaData .writeTo (out );
819851 blocks .writeTo (out );
820852 customs .writeTo (out );
853+ if (out .getVersion ().onOrAfter (Version .V_6_7_0 )) {
854+ out .writeVInt (minimumMasterNodesOnPublishingMaster );
855+ }
821856 }
822857
823858 @ Override
@@ -837,6 +872,7 @@ public ClusterState apply(ClusterState state) {
837872 builder .metaData (metaData .apply (state .metaData ));
838873 builder .blocks (blocks .apply (state .blocks ));
839874 builder .customs (customs .apply (state .customs ));
875+ builder .minimumMasterNodesOnPublishingMaster (minimumMasterNodesOnPublishingMaster );
840876 builder .fromDiff (true );
841877 return builder .build ();
842878 }
0 commit comments