From 6b817489f308c07e31454327486fc589e19e5be2 Mon Sep 17 00:00:00 2001 From: kel Date: Thu, 16 Nov 2017 09:03:09 -0600 Subject: [PATCH] Fix default value of ignore_unavailable for snapshot REST API (#27056) The default value for ignore_unavailable did not match what was documented when using the REST APIs for snapshot creation and restore. This commit sets the default value of ignore_unavailable to false, the way it is documented and ensures it's the same when using either REST API or transport client. Closes #25359 --- .../create/CreateSnapshotRequest.java | 5 +-- .../restore/RestoreSnapshotRequest.java | 3 +- .../snapshots/SnapshotRequestsTests.java | 21 +++++++++-- .../test/snapshot.create/10_basic.yml | 35 +++++++++++++++++++ 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java index 8b3ca8bdfb208..90ba0ba187cd4 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java @@ -380,8 +380,9 @@ public boolean includeGlobalState() { * @param source snapshot definition * @return this request */ + @SuppressWarnings("unchecked") public CreateSnapshotRequest source(Map source) { - for (Map.Entry entry : ((Map) source).entrySet()) { + for (Map.Entry entry : source.entrySet()) { String name = entry.getKey(); if (name.equals("indices")) { if (entry.getValue() instanceof String) { @@ -402,7 +403,7 @@ public CreateSnapshotRequest source(Map source) { includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state"); } } - indicesOptions(IndicesOptions.fromMap((Map) source, IndicesOptions.lenientExpandOpen())); + indicesOptions(IndicesOptions.fromMap(source, indicesOptions)); return this; } diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java index a6717b2c71c6e..0feb04e2823c8 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java @@ -505,6 +505,7 @@ public Settings indexSettings() { * @param source restore definition * @return this request */ + @SuppressWarnings("unchecked") public RestoreSnapshotRequest source(Map source) { for (Map.Entry entry : source.entrySet()) { String name = entry.getKey(); @@ -558,7 +559,7 @@ public RestoreSnapshotRequest source(Map source) { } } } - indicesOptions(IndicesOptions.fromMap((Map) source, IndicesOptions.lenientExpandOpen())); + indicesOptions(IndicesOptions.fromMap(source, indicesOptions)); return this; } diff --git a/core/src/test/java/org/elasticsearch/snapshots/SnapshotRequestsTests.java b/core/src/test/java/org/elasticsearch/snapshots/SnapshotRequestsTests.java index a8e1eeaec106b..f3d8bba3edb68 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SnapshotRequestsTests.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SnapshotRequestsTests.java @@ -37,7 +37,7 @@ public void testRestoreSnapshotRequestParsing() throws IOException { XContentBuilder builder = jsonBuilder().startObject(); - if(randomBoolean()) { + if (randomBoolean()) { builder.field("indices", "foo,bar,baz"); } else { builder.startArray("indices"); @@ -76,6 +76,10 @@ public void testRestoreSnapshotRequestParsing() throws IOException { builder.value("set3"); builder.endArray(); } + boolean includeIgnoreUnavailable = randomBoolean(); + if (includeIgnoreUnavailable) { + builder.field("ignore_unavailable", indicesOptions.ignoreUnavailable()); + } BytesReference bytes = builder.endObject().bytes(); @@ -89,7 +93,10 @@ public void testRestoreSnapshotRequestParsing() throws IOException { assertEquals(partial, request.partial()); assertEquals("val1", request.settings().get("set1")); assertArrayEquals(request.ignoreIndexSettings(), new String[]{"set2", "set3"}); - + boolean expectedIgnoreAvailable = includeIgnoreUnavailable + ? indicesOptions.ignoreUnavailable() + : IndicesOptions.strictExpandOpen().ignoreUnavailable(); + assertEquals(expectedIgnoreAvailable, request.indicesOptions().ignoreUnavailable()); } public void testCreateSnapshotRequestParsing() throws IOException { @@ -97,7 +104,7 @@ public void testCreateSnapshotRequestParsing() throws IOException { XContentBuilder builder = jsonBuilder().startObject(); - if(randomBoolean()) { + if (randomBoolean()) { builder.field("indices", "foo,bar,baz"); } else { builder.startArray("indices"); @@ -134,6 +141,10 @@ public void testCreateSnapshotRequestParsing() throws IOException { builder.value("set3"); builder.endArray(); } + boolean includeIgnoreUnavailable = randomBoolean(); + if (includeIgnoreUnavailable) { + builder.field("ignore_unavailable", indicesOptions.ignoreUnavailable()); + } BytesReference bytes = builder.endObject().bytes(); @@ -144,6 +155,10 @@ public void testCreateSnapshotRequestParsing() throws IOException { assertArrayEquals(request.indices(), new String[]{"foo", "bar", "baz"}); assertEquals(partial, request.partial()); assertEquals("val1", request.settings().get("set1")); + boolean expectedIgnoreAvailable = includeIgnoreUnavailable + ? indicesOptions.ignoreUnavailable() + : IndicesOptions.strictExpandOpen().ignoreUnavailable(); + assertEquals(expectedIgnoreAvailable, request.indicesOptions().ignoreUnavailable()); } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.create/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.create/10_basic.yml index b12b9d09b6fea..4afa158cb0a11 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.create/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.create/10_basic.yml @@ -37,3 +37,38 @@ setup: snapshot: test_snapshot - match: { acknowledged: true } + +--- +"Create a snapshot for missing index": + - skip: + version: " - 6.99.99" + reason: ignore_unavailable default is false in 7.0.0 + + - do: + catch: missing + snapshot.create: + repository: test_repo_create_1 + snapshot: test_snapshot_1 + wait_for_completion: true + body: | + { "indices": "missing_1" } + + - do: + snapshot.create: + repository: test_repo_create_1 + snapshot: test_snapshot_2 + wait_for_completion: true + body: | + { "indices": "missing_2", "ignore_unavailable": true } + + - match: { snapshot.snapshot: test_snapshot_2 } + - match: { snapshot.state : SUCCESS } + - match: { snapshot.shards.successful: 0 } + - match: { snapshot.shards.failed : 0 } + + - do: + snapshot.delete: + repository: test_repo_create_1 + snapshot: test_snapshot_2 + + - match: { acknowledged: true }