27
27
public class AllocateAction implements LifecycleAction {
28
28
29
29
public static final String NAME = "allocate" ;
30
+ public static final ParseField NUMBER_OF_REPLICAS_FIELD = new ParseField ("number_of_replicas" );
30
31
public static final ParseField INCLUDE_FIELD = new ParseField ("include" );
31
32
public static final ParseField EXCLUDE_FIELD = new ParseField ("exclude" );
32
33
public static final ParseField REQUIRE_FIELD = new ParseField ("require" );
33
34
34
35
@ SuppressWarnings ("unchecked" )
35
36
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 ]));
37
38
38
39
static {
40
+ PARSER .declareInt (ConstructingObjectParser .optionalConstructorArg (), NUMBER_OF_REPLICAS_FIELD );
39
41
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), INCLUDE_FIELD );
40
42
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), EXCLUDE_FIELD );
41
43
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .mapStrings (), REQUIRE_FIELD );
42
44
}
43
45
46
+ private final Integer numberOfReplicas ;
44
47
private final Map <String , String > include ;
45
48
private final Map <String , String > exclude ;
46
49
private final Map <String , String > require ;
@@ -49,7 +52,7 @@ public static AllocateAction parse(XContentParser parser) {
49
52
return PARSER .apply (parser , null );
50
53
}
51
54
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 ) {
53
56
if (include == null ) {
54
57
this .include = Collections .emptyMap ();
55
58
} else {
@@ -65,19 +68,27 @@ public AllocateAction(Map<String, String> include, Map<String, String> exclude,
65
68
} else {
66
69
this .require = require ;
67
70
}
68
- if (this .include .isEmpty () && this .exclude .isEmpty () && this .require .isEmpty ()) {
71
+ if (this .include .isEmpty () && this .exclude .isEmpty () && this .require .isEmpty () && numberOfReplicas == null ) {
69
72
throw new IllegalArgumentException (
70
73
"At least one of " + INCLUDE_FIELD .getPreferredName () + ", " + EXCLUDE_FIELD .getPreferredName () + " or "
71
74
+ REQUIRE_FIELD .getPreferredName () + "must contain attributes for action " + NAME );
72
75
}
76
+ if (numberOfReplicas != null && numberOfReplicas < 0 ) {
77
+ throw new IllegalArgumentException ("[" + NUMBER_OF_REPLICAS_FIELD .getPreferredName () + "] must be >= 0" );
78
+ }
79
+ this .numberOfReplicas = numberOfReplicas ;
73
80
}
74
81
75
82
@ SuppressWarnings ("unchecked" )
76
83
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 (),
78
85
(Map <String , String >) in .readGenericValue ());
79
86
}
80
87
88
+ public Integer getNumberOfReplicas () {
89
+ return numberOfReplicas ;
90
+ }
91
+
81
92
public Map <String , String > getInclude () {
82
93
return include ;
83
94
}
@@ -92,6 +103,7 @@ public Map<String, String> getRequire() {
92
103
93
104
@ Override
94
105
public void writeTo (StreamOutput out ) throws IOException {
106
+ out .writeOptionalVInt (numberOfReplicas );
95
107
out .writeGenericValue (include );
96
108
out .writeGenericValue (exclude );
97
109
out .writeGenericValue (require );
@@ -105,6 +117,9 @@ public String getWriteableName() {
105
117
@ Override
106
118
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
107
119
builder .startObject ();
120
+ if (numberOfReplicas != null ) {
121
+ builder .field (NUMBER_OF_REPLICAS_FIELD .getPreferredName (), numberOfReplicas );
122
+ }
108
123
builder .field (INCLUDE_FIELD .getPreferredName (), include );
109
124
builder .field (EXCLUDE_FIELD .getPreferredName (), exclude );
110
125
builder .field (REQUIRE_FIELD .getPreferredName (), require );
@@ -123,6 +138,9 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
123
138
StepKey allocationRoutedKey = new StepKey (phase , NAME , AllocationRoutedStep .NAME );
124
139
125
140
Settings .Builder newSettings = Settings .builder ();
141
+ if (numberOfReplicas != null ) {
142
+ newSettings .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , numberOfReplicas );
143
+ }
126
144
include .forEach ((key , value ) -> newSettings .put (IndexMetaData .INDEX_ROUTING_INCLUDE_GROUP_SETTING .getKey () + key , value ));
127
145
exclude .forEach ((key , value ) -> newSettings .put (IndexMetaData .INDEX_ROUTING_EXCLUDE_GROUP_SETTING .getKey () + key , value ));
128
146
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) {
140
158
141
159
@ Override
142
160
public int hashCode () {
143
- return Objects .hash (include , exclude , require );
161
+ return Objects .hash (numberOfReplicas , include , exclude , require );
144
162
}
145
163
146
164
@ Override
@@ -152,7 +170,10 @@ public boolean equals(Object obj) {
152
170
return false ;
153
171
}
154
172
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 ) &&
174
+ Objects .equals (include , other .include ) &&
175
+ Objects .equals (exclude , other .exclude ) &&
176
+ Objects .equals (require , other .require );
156
177
}
157
178
158
179
@ Override
0 commit comments