Skip to content

Commit c0da353

Browse files
committed
Snapshot/Restore: add support for changing index settings during restore process
Closes #7887
1 parent 8162184 commit c0da353

File tree

6 files changed

+404
-24
lines changed

6 files changed

+404
-24
lines changed

docs/reference/modules/snapshots.asciidoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ restore such indices by setting `partial` to `true`. Please note, that only succ
243243
restored in this case and all missing shards will be recreated empty.
244244

245245

246+
[float]
247+
=== Changing index settings during restore
248+
249+
Most of index settings can be overridden during the restore process. For example, the following command will restore
250+
the index `index_1` without creating any replicas while switching back to default refresh interval:
251+
252+
[source,js]
253+
-----------------------------------
254+
$ curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore" -d '{
255+
"indices": "index_1",
256+
"index_settings" : {
257+
"index.number_of_replicas": 0
258+
},
259+
"ignore_index_settings": ["index.refresh_interval"]
260+
}'
261+
-----------------------------------
262+
263+
246264
[float]
247265
=== Snapshot status
248266

src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public class RestoreSnapshotRequest extends MasterNodeOperationRequest<RestoreSn
7474

7575
private Settings settings = EMPTY_SETTINGS;
7676

77+
private Settings indexSettings = EMPTY_SETTINGS;
78+
79+
private String[] ignoreIndexSettings = Strings.EMPTY_ARRAY;
80+
7781
RestoreSnapshotRequest() {
7882
}
7983

@@ -106,7 +110,12 @@ public ActionRequestValidationException validate() {
106110
if (settings == null) {
107111
validationException = addValidationError("settings are missing", validationException);
108112
}
109-
113+
if (indexSettings == null) {
114+
validationException = addValidationError("indexSettings are missing", validationException);
115+
}
116+
if (ignoreIndexSettings == null) {
117+
validationException = addValidationError("ignoreIndexSettings are missing", validationException);
118+
}
110119
return validationException;
111120
}
112121

@@ -364,6 +373,29 @@ public Settings settings() {
364373
return this.settings;
365374
}
366375

376+
/**
377+
* Sets the list of index settings and index settings groups that shouldn't be restored from snapshot
378+
*/
379+
public RestoreSnapshotRequest ignoreIndexSettings(String... ignoreIndexSettings) {
380+
this.ignoreIndexSettings = ignoreIndexSettings;
381+
return this;
382+
}
383+
384+
/**
385+
* Sets the list of index settings and index settings groups that shouldn't be restored from snapshot
386+
*/
387+
public RestoreSnapshotRequest ignoreIndexSettings(List<String> ignoreIndexSettings) {
388+
this.ignoreIndexSettings = ignoreIndexSettings.toArray(new String[ignoreIndexSettings.size()]);
389+
return this;
390+
}
391+
392+
/**
393+
* Returns the list of index settings and index settings groups that shouldn't be restored from snapshot
394+
*/
395+
public String[] ignoreIndexSettings() {
396+
return ignoreIndexSettings;
397+
}
398+
367399
/**
368400
* If set to true the restore procedure will restore global cluster state.
369401
* <p/>
@@ -406,6 +438,51 @@ public boolean includeAliases() {
406438
return includeAliases;
407439
}
408440

441+
/**
442+
* Sets settings that should be added/changed in all restored indices
443+
*/
444+
public RestoreSnapshotRequest indexSettings(Settings settings) {
445+
this.indexSettings = settings;
446+
return this;
447+
}
448+
449+
/**
450+
* Sets settings that should be added/changed in all restored indices
451+
*/
452+
public RestoreSnapshotRequest indexSettings(Settings.Builder settings) {
453+
this.indexSettings = settings.build();
454+
return this;
455+
}
456+
457+
/**
458+
* Sets settings that should be added/changed in all restored indices
459+
*/
460+
public RestoreSnapshotRequest indexSettings(String source) {
461+
this.indexSettings = ImmutableSettings.settingsBuilder().loadFromSource(source).build();
462+
return this;
463+
}
464+
465+
/**
466+
* Sets settings that should be added/changed in all restored indices
467+
*/
468+
public RestoreSnapshotRequest indexSettings(Map<String, Object> source) {
469+
try {
470+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
471+
builder.map(source);
472+
indexSettings(builder.string());
473+
} catch (IOException e) {
474+
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
475+
}
476+
return this;
477+
}
478+
479+
/**
480+
* Returns settings that should be added/changed in all restored indices
481+
*/
482+
public Settings indexSettings() {
483+
return this.indexSettings;
484+
}
485+
409486
/**
410487
* Parses restore definition
411488
*
@@ -454,7 +531,7 @@ public RestoreSnapshotRequest source(Map source) {
454531
partial(nodeBooleanValue(entry.getValue()));
455532
} else if (name.equals("settings")) {
456533
if (!(entry.getValue() instanceof Map)) {
457-
throw new ElasticsearchIllegalArgumentException("malformed settings section, should indices an inner object");
534+
throw new ElasticsearchIllegalArgumentException("malformed settings section");
458535
}
459536
settings((Map<String, Object>) entry.getValue());
460537
} else if (name.equals("include_global_state")) {
@@ -473,6 +550,19 @@ public RestoreSnapshotRequest source(Map source) {
473550
} else {
474551
throw new ElasticsearchIllegalArgumentException("malformed rename_replacement");
475552
}
553+
} else if (name.equals("index_settings")) {
554+
if (!(entry.getValue() instanceof Map)) {
555+
throw new ElasticsearchIllegalArgumentException("malformed index_settings section");
556+
}
557+
indexSettings((Map<String, Object>) entry.getValue());
558+
} else if (name.equals("ignore_index_settings")) {
559+
if (entry.getValue() instanceof String) {
560+
ignoreIndexSettings(Strings.splitStringByCommaToArray((String) entry.getValue()));
561+
} else if (entry.getValue() instanceof List) {
562+
ignoreIndexSettings((List<String>) entry.getValue());
563+
} else {
564+
throw new ElasticsearchIllegalArgumentException("malformed ignore_index_settings section, should be an array of strings");
565+
}
476566
} else {
477567
throw new ElasticsearchIllegalArgumentException("Unknown parameter " + name);
478568
}
@@ -563,6 +653,10 @@ public void readFrom(StreamInput in) throws IOException {
563653
partial = in.readBoolean();
564654
includeAliases = in.readBoolean();
565655
settings = readSettingsFromStream(in);
656+
if (in.getVersion().onOrAfter(Version.V_1_5_0)) {
657+
indexSettings = readSettingsFromStream(in);
658+
ignoreIndexSettings = in.readStringArray();
659+
}
566660
}
567661

568662
@Override
@@ -579,5 +673,9 @@ public void writeTo(StreamOutput out) throws IOException {
579673
out.writeBoolean(partial);
580674
out.writeBoolean(includeAliases);
581675
writeSettingsToStream(settings, out);
676+
if (out.getVersion().onOrAfter(Version.V_1_5_0)) {
677+
writeSettingsToStream(indexSettings, out);
678+
out.writeStringArray(ignoreIndexSettings);
679+
}
582680
}
583681
}

src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequestBuilder.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.client.ClusterAdminClient;
2626
import org.elasticsearch.common.settings.Settings;
2727

28+
import java.util.List;
2829
import java.util.Map;
2930

3031
/**
@@ -230,6 +231,68 @@ public RestoreSnapshotRequestBuilder setIncludeAliases(boolean restoreAliases) {
230231
return this;
231232
}
232233

234+
/**
235+
* Sets index settings that should be added or replaced during restore
236+
237+
* @param settings index settings
238+
* @return this builder
239+
*/
240+
public RestoreSnapshotRequestBuilder setIndexSettings(Settings settings) {
241+
request.indexSettings(settings);
242+
return this;
243+
}
244+
245+
/**
246+
* Sets index settings that should be added or replaced during restore
247+
248+
* @param settings index settings
249+
* @return this builder
250+
*/
251+
public RestoreSnapshotRequestBuilder setIndexSettings(Settings.Builder settings) {
252+
request.indexSettings(settings);
253+
return this;
254+
}
255+
256+
/**
257+
* Sets index settings that should be added or replaced during restore
258+
259+
* @param source index settings
260+
* @return this builder
261+
*/
262+
public RestoreSnapshotRequestBuilder setIndexSettings(String source) {
263+
request.indexSettings(source);
264+
return this;
265+
}
266+
267+
/**
268+
* Sets index settings that should be added or replaced during restore
269+
270+
* @param source index settings
271+
* @return this builder
272+
*/
273+
public RestoreSnapshotRequestBuilder setIndexSettings(Map<String, Object> source) {
274+
request.indexSettings(source);
275+
return this;
276+
}
277+
278+
279+
/**
280+
* Sets the list of index settings and index settings groups that shouldn't be restored from snapshot
281+
*/
282+
public RestoreSnapshotRequestBuilder setIgnoreIndexSettings(String... ignoreIndexSettings) {
283+
request.ignoreIndexSettings(ignoreIndexSettings);
284+
return this;
285+
}
286+
287+
/**
288+
* Sets the list of index settings and index settings groups that shouldn't be restored from snapshot
289+
*/
290+
public RestoreSnapshotRequestBuilder setIgnoreIndexSettings(List<String> ignoreIndexSettings) {
291+
request.ignoreIndexSettings(ignoreIndexSettings);
292+
return this;
293+
}
294+
295+
233296
@Override
234297
protected void doExecute(ActionListener<RestoreSnapshotResponse> listener) {
235298
client.restoreSnapshot(request, listener);

src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/TransportRestoreSnapshotAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ protected void masterOperation(final RestoreSnapshotRequest request, ClusterStat
7373
RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(
7474
"restore_snapshot[" + request.snapshot() + "]", request.repository(), request.snapshot(),
7575
request.indices(), request.indicesOptions(), request.renamePattern(), request.renameReplacement(),
76-
request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases());
76+
request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases(),
77+
request.indexSettings(), request.ignoreIndexSettings());
7778

7879
restoreService.restoreSnapshot(restoreRequest, new ActionListener<RestoreInfo>() {
7980
@Override

0 commit comments

Comments
 (0)