2727public class AllocateAction implements LifecycleAction {
2828
2929 public static final String NAME = "allocate" ;
30+ public static final ParseField NUMBER_OF_REPLICAS_FIELD = new ParseField ("number_of_replicas" );
3031 public static final ParseField INCLUDE_FIELD = new ParseField ("include" );
3132 public static final ParseField EXCLUDE_FIELD = new ParseField ("exclude" );
3233 public static final ParseField REQUIRE_FIELD = new ParseField ("require" );
3334
3435 @ SuppressWarnings ("unchecked" )
3536 private static final ConstructingObjectParser <AllocateAction , Void > PARSER = new ConstructingObjectParser <>(NAME ,
36- a -> new AllocateAction ((Map <String , String >) a [0 ], (Map <String , String >) a [1 ], (Map <String , String >) a [2 ]));
37+ a -> new AllocateAction ((Integer ) a [ 0 ], ( Map <String , String >) a [1 ], (Map <String , String >) a [2 ], (Map <String , String >) a [3 ]));
3738
3839 static {
40+ PARSER .declareInt (ConstructingObjectParser .optionalConstructorArg (), NUMBER_OF_REPLICAS_FIELD );
3941 PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), INCLUDE_FIELD );
4042 PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), EXCLUDE_FIELD );
4143 PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), REQUIRE_FIELD );
4244 }
4345
46+ private final Integer numberOfReplicas ;
4447 private final Map <String , String > include ;
4548 private final Map <String , String > exclude ;
4649 private final Map <String , String > require ;
@@ -49,7 +52,7 @@ public static AllocateAction parse(XContentParser parser) {
4952 return PARSER .apply (parser , null );
5053 }
5154
52- public AllocateAction (Map <String , String > include , Map <String , String > exclude , Map <String , String > require ) {
55+ public AllocateAction (Integer numberOfReplicas , Map <String , String > include , Map <String , String > exclude , Map <String , String > require ) {
5356 if (include == null ) {
5457 this .include = Collections .emptyMap ();
5558 } else {
@@ -65,19 +68,27 @@ public AllocateAction(Map<String, String> include, Map<String, String> exclude,
6568 } else {
6669 this .require = require ;
6770 }
68- if (this .include .isEmpty () && this .exclude .isEmpty () && this .require .isEmpty ()) {
71+ if (this .include .isEmpty () && this .exclude .isEmpty () && this .require .isEmpty () && numberOfReplicas == null ) {
6972 throw new IllegalArgumentException (
7073 "At least one of " + INCLUDE_FIELD .getPreferredName () + ", " + EXCLUDE_FIELD .getPreferredName () + " or "
7174 + REQUIRE_FIELD .getPreferredName () + "must contain attributes for action " + NAME );
7275 }
76+ if (numberOfReplicas != null && numberOfReplicas < 0 ) {
77+ throw new IllegalArgumentException ("[" + NUMBER_OF_REPLICAS_FIELD .getPreferredName () + "] must be >= 0" );
78+ }
79+ this .numberOfReplicas = numberOfReplicas ;
7380 }
7481
7582 @ SuppressWarnings ("unchecked" )
7683 public AllocateAction (StreamInput in ) throws IOException {
77- this ((Map <String , String >) in .readGenericValue (), (Map <String , String >) in .readGenericValue (),
84+ this (in . readOptionalVInt (), (Map <String , String >) in .readGenericValue (), (Map <String , String >) in .readGenericValue (),
7885 (Map <String , String >) in .readGenericValue ());
7986 }
8087
88+ public Integer getNumberOfReplicas () {
89+ return numberOfReplicas ;
90+ }
91+
8192 public Map <String , String > getInclude () {
8293 return include ;
8394 }
@@ -92,6 +103,7 @@ public Map<String, String> getRequire() {
92103
93104 @ Override
94105 public void writeTo (StreamOutput out ) throws IOException {
106+ out .writeOptionalVInt (numberOfReplicas );
95107 out .writeGenericValue (include );
96108 out .writeGenericValue (exclude );
97109 out .writeGenericValue (require );
@@ -105,6 +117,9 @@ public String getWriteableName() {
105117 @ Override
106118 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
107119 builder .startObject ();
120+ if (numberOfReplicas != null ) {
121+ builder .field (NUMBER_OF_REPLICAS_FIELD .getPreferredName (), numberOfReplicas );
122+ }
108123 builder .field (INCLUDE_FIELD .getPreferredName (), include );
109124 builder .field (EXCLUDE_FIELD .getPreferredName (), exclude );
110125 builder .field (REQUIRE_FIELD .getPreferredName (), require );
@@ -123,6 +138,9 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
123138 StepKey allocationRoutedKey = new StepKey (phase , NAME , AllocationRoutedStep .NAME );
124139
125140 Settings .Builder newSettings = Settings .builder ();
141+ if (numberOfReplicas != null ) {
142+ newSettings .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , numberOfReplicas );
143+ }
126144 include .forEach ((key , value ) -> newSettings .put (IndexMetaData .INDEX_ROUTING_INCLUDE_GROUP_SETTING .getKey () + key , value ));
127145 exclude .forEach ((key , value ) -> newSettings .put (IndexMetaData .INDEX_ROUTING_EXCLUDE_GROUP_SETTING .getKey () + key , value ));
128146 require .forEach ((key , value ) -> newSettings .put (IndexMetaData .INDEX_ROUTING_REQUIRE_GROUP_SETTING .getKey () + key , value ));
@@ -140,7 +158,7 @@ public List<StepKey> toStepKeys(String phase) {
140158
141159 @ Override
142160 public int hashCode () {
143- return Objects .hash (include , exclude , require );
161+ return Objects .hash (numberOfReplicas , include , exclude , require );
144162 }
145163
146164 @ Override
@@ -152,7 +170,8 @@ public boolean equals(Object obj) {
152170 return false ;
153171 }
154172 AllocateAction other = (AllocateAction ) obj ;
155- return Objects .equals (include , other .include ) && Objects .equals (exclude , other .exclude ) && Objects .equals (require , other .require );
173+ return Objects .equals (numberOfReplicas , other .numberOfReplicas ) && Objects .equals (include , other .include )
174+ && Objects .equals (exclude , other .exclude ) && Objects .equals (require , other .require );
156175 }
157176
158177 @ Override
0 commit comments