34
34
import org .elasticsearch .cluster .routing .RoutingTable ;
35
35
import org .elasticsearch .cluster .routing .allocation .AllocationService ;
36
36
import org .elasticsearch .cluster .routing .allocation .RoutingAllocation ;
37
- import org .elasticsearch .cluster .settings .DynamicSettings ;
38
- import org .elasticsearch .common .Booleans ;
39
37
import org .elasticsearch .common .Priority ;
40
38
import org .elasticsearch .common .collect .Tuple ;
41
39
import org .elasticsearch .common .component .AbstractComponent ;
42
40
import org .elasticsearch .common .inject .Inject ;
41
+ import org .elasticsearch .common .settings .IndexScopeSettings ;
42
+ import org .elasticsearch .common .settings .Setting ;
43
43
import org .elasticsearch .common .settings .Settings ;
44
44
import org .elasticsearch .common .unit .TimeValue ;
45
- import org .elasticsearch .index .settings .IndexDynamicSettings ;
46
45
47
46
import java .util .ArrayList ;
48
47
import java .util .HashMap ;
@@ -63,18 +62,17 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements
63
62
64
63
private final AllocationService allocationService ;
65
64
66
- private final DynamicSettings dynamicSettings ;
67
-
68
65
private final IndexNameExpressionResolver indexNameExpressionResolver ;
66
+ private final IndexScopeSettings indexScopeSettings ;
69
67
70
68
@ Inject
71
- public MetaDataUpdateSettingsService (Settings settings , ClusterService clusterService , AllocationService allocationService , @ IndexDynamicSettings DynamicSettings dynamicSettings , IndexNameExpressionResolver indexNameExpressionResolver ) {
69
+ public MetaDataUpdateSettingsService (Settings settings , ClusterService clusterService , AllocationService allocationService , IndexScopeSettings indexScopeSettings , IndexNameExpressionResolver indexNameExpressionResolver ) {
72
70
super (settings );
73
71
this .clusterService = clusterService ;
74
72
this .indexNameExpressionResolver = indexNameExpressionResolver ;
75
73
this .clusterService .add (this );
76
74
this .allocationService = allocationService ;
77
- this .dynamicSettings = dynamicSettings ;
75
+ this .indexScopeSettings = indexScopeSettings ;
78
76
}
79
77
80
78
@ Override
@@ -147,40 +145,32 @@ public void onFailure(Throwable t) {
147
145
public void updateSettings (final UpdateSettingsClusterStateUpdateRequest request , final ActionListener <ClusterStateUpdateResponse > listener ) {
148
146
Settings .Builder updatedSettingsBuilder = Settings .settingsBuilder ();
149
147
updatedSettingsBuilder .put (request .settings ()).normalizePrefix (IndexMetaData .INDEX_SETTING_PREFIX );
148
+ Settings .Builder settingsForClosedIndices = Settings .builder ();
149
+ Settings .Builder settingsForOpenIndices = Settings .builder ();
150
+ Settings .Builder skipppedSettings = Settings .builder ();
151
+
152
+
150
153
// never allow to change the number of shards
151
- for (String key : updatedSettingsBuilder .internalMap ().keySet ()) {
152
- if (key .equals (IndexMetaData .SETTING_NUMBER_OF_SHARDS )) {
154
+ for (Map . Entry < String , String > entry : updatedSettingsBuilder .internalMap ().entrySet ()) {
155
+ if (entry . getKey () .equals (IndexMetaData .SETTING_NUMBER_OF_SHARDS )) {
153
156
listener .onFailure (new IllegalArgumentException ("can't change the number of shards for an index" ));
154
157
return ;
155
158
}
156
- }
157
-
158
- final Settings closeSettings = updatedSettingsBuilder .build ();
159
-
160
- final Set <String > removedSettings = new HashSet <>();
161
- final Set <String > errors = new HashSet <>();
162
- for (Map .Entry <String , String > setting : updatedSettingsBuilder .internalMap ().entrySet ()) {
163
- if (!dynamicSettings .hasDynamicSetting (setting .getKey ())) {
164
- removedSettings .add (setting .getKey ());
165
- } else {
166
- String error = dynamicSettings .validateDynamicSetting (setting .getKey (), setting .getValue (), clusterService .state ());
167
- if (error != null ) {
168
- errors .add ("[" + setting .getKey () + "] - " + error );
169
- }
159
+ Setting setting = indexScopeSettings .get (entry .getKey ());
160
+ if (setting == null ) {
161
+ throw new IllegalArgumentException ("setting [" + entry .getKey () + "] is unknown" );
170
162
}
171
- }
172
-
173
- if (!errors .isEmpty ()) {
174
- listener .onFailure (new IllegalArgumentException ("can't process the settings: " + errors .toString ()));
175
- return ;
176
- }
177
-
178
- if (!removedSettings .isEmpty ()) {
179
- for (String removedSetting : removedSettings ) {
180
- updatedSettingsBuilder .remove (removedSetting );
163
+ indexScopeSettings .validate (entry .getKey (), entry .getValue ());
164
+ settingsForClosedIndices .put (entry .getKey (), entry .getValue ());
165
+ if (setting .isDynamic ()) {
166
+ settingsForOpenIndices .put (entry .getKey (), entry .getValue ());
167
+ } else {
168
+ skipppedSettings .put (entry .getKey (), entry .getValue ());
181
169
}
182
170
}
183
- final Settings openSettings = updatedSettingsBuilder .build ();
171
+ final Settings skippedSettigns = skipppedSettings .build ();
172
+ final Settings closedSettings = settingsForClosedIndices .build ();
173
+ final Settings openSettings = settingsForOpenIndices .build ();
184
174
185
175
clusterService .submitStateUpdateTask ("update-settings" ,
186
176
new AckedClusterStateUpdateTask <ClusterStateUpdateResponse >(Priority .URGENT , request , listener ) {
@@ -208,16 +198,16 @@ public ClusterState execute(ClusterState currentState) {
208
198
}
209
199
}
210
200
211
- if (closeIndices .size () > 0 && closeSettings .get (IndexMetaData .SETTING_NUMBER_OF_REPLICAS ) != null ) {
201
+ if (closeIndices .size () > 0 && closedSettings .get (IndexMetaData .SETTING_NUMBER_OF_REPLICAS ) != null ) {
212
202
throw new IllegalArgumentException (String .format (Locale .ROOT ,
213
203
"Can't update [%s] on closed indices [%s] - can leave index in an unopenable state" , IndexMetaData .SETTING_NUMBER_OF_REPLICAS ,
214
204
closeIndices
215
205
));
216
206
}
217
- if (!removedSettings .isEmpty () && !openIndices .isEmpty ()) {
207
+ if (!skippedSettigns . getAsMap () .isEmpty () && !openIndices .isEmpty ()) {
218
208
throw new IllegalArgumentException (String .format (Locale .ROOT ,
219
209
"Can't update non dynamic settings[%s] for open indices [%s]" ,
220
- removedSettings ,
210
+ skippedSettigns . getAsMap (). keySet () ,
221
211
openIndices
222
212
));
223
213
}
@@ -272,7 +262,7 @@ public ClusterState execute(ClusterState currentState) {
272
262
273
263
if (!closeIndices .isEmpty ()) {
274
264
String [] indices = closeIndices .toArray (new String [closeIndices .size ()]);
275
- metaDataBuilder .updateSettings (closeSettings , indices );
265
+ metaDataBuilder .updateSettings (closedSettings , indices );
276
266
}
277
267
278
268
@@ -281,12 +271,18 @@ public ClusterState execute(ClusterState currentState) {
281
271
// now, reroute in case things change that require it (like number of replicas)
282
272
RoutingAllocation .Result routingResult = allocationService .reroute (updatedState , "settings update" );
283
273
updatedState = ClusterState .builder (updatedState ).routingResult (routingResult ).build ();
284
-
274
+ for (String index : openIndices ) {
275
+ indexScopeSettings .dryRun (updatedState .metaData ().index (index ).getSettings ());
276
+ }
277
+ for (String index : closeIndices ) {
278
+ indexScopeSettings .dryRun (updatedState .metaData ().index (index ).getSettings ());
279
+ }
285
280
return updatedState ;
286
281
}
287
282
});
288
283
}
289
284
285
+
290
286
public void upgradeIndexSettings (final UpgradeSettingsClusterStateUpdateRequest request , final ActionListener <ClusterStateUpdateResponse > listener ) {
291
287
292
288
0 commit comments