From 16945837e16922de83eb4e32091d567b7070e206 Mon Sep 17 00:00:00 2001 From: liketic Date: Fri, 20 Oct 2017 12:32:45 +0800 Subject: [PATCH 1/3] Fix creating snapshot on missing index (#25359) --- .../cluster/snapshots/create/CreateSnapshotRequest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 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..347b81b0037ad 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.strictExpandOpen())); return this; } From 9c657d7fb08eddd8e58e8cf4239b99f2c7c1b4d0 Mon Sep 17 00:00:00 2001 From: liketic Date: Thu, 16 Nov 2017 11:02:01 +0800 Subject: [PATCH 2/3] Fix default indicesOptions in parsing RestoreSnapshotRequest --- .../create/CreateSnapshotRequest.java | 2 +- .../restore/RestoreSnapshotRequest.java | 3 ++- .../snapshots/SnapshotRequestsTests.java | 21 ++++++++++++++++--- 3 files changed, 21 insertions(+), 5 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 347b81b0037ad..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 @@ -403,7 +403,7 @@ public CreateSnapshotRequest source(Map source) { includeGlobalState = nodeBooleanValue(entry.getValue(), "include_global_state"); } } - indicesOptions(IndicesOptions.fromMap(source, IndicesOptions.strictExpandOpen())); + 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()); } } From 97929927e31d97cb269affc88055e2fedebaef5f Mon Sep 17 00:00:00 2001 From: liketic Date: Thu, 16 Nov 2017 18:16:25 +0800 Subject: [PATCH 3/3] Add REST test --- .../test/snapshot.create/10_basic.yml | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 }