From 57d6dd7e3117f3e25eeacf78afbbe7076be67d16 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 18 Feb 2020 11:37:53 +0100 Subject: [PATCH] Fix Non-Verbose Snapshot List Missing Empty Snapshots (#52433) (#52456) We were not including snapshots without indices in the non-verbose listing because we used the snapshot -> indices mapping to get the snapshots. --- .../get/TransportGetSnapshotsAction.java | 5 ++-- .../snapshots/SnapshotStatusApisIT.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java index d8b47fbba9f7e..502be6d039b51 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java @@ -180,10 +180,9 @@ private List buildSimpleSnapshotInfos(final Set toReso } } } - for (Map.Entry> entry : snapshotsToIndices.entrySet()) { - final List indices = entry.getValue(); + for (SnapshotId snapshotId : toResolve) { + final List indices = snapshotsToIndices.getOrDefault(snapshotId, Collections.emptyList()); CollectionUtil.timSort(indices); - final SnapshotId snapshotId = entry.getKey(); snapshotInfos.add(new SnapshotInfo(snapshotId, indices, repositoryData.getSnapshotState(snapshotId))); } CollectionUtil.timSort(snapshotInfos); diff --git a/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java b/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java index 13d94d7eefa0c..e864f3bb2b33f 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotStatusApisIT.java @@ -41,6 +41,8 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; public class SnapshotStatusApisIT extends AbstractSnapshotIntegTestCase { @@ -167,4 +169,26 @@ public void testExceptionOnMissingShardLevelSnapBlob() throws IOException { expectThrows(SnapshotMissingException.class, () -> client().admin().cluster() .prepareSnapshotStatus("test-repo").setSnapshots("test-snap").execute().actionGet()); } + + public void testGetSnapshotsWithoutIndices() { + logger.info("--> creating repository"); + assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings( + Settings.builder().put("location", randomRepoPath()).build())); + + logger.info("--> snapshot"); + final SnapshotInfo snapshotInfo = + client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap") + .setIndices().setWaitForCompletion(true).get().getSnapshotInfo(); + + assertThat(snapshotInfo.state(), is(SnapshotState.SUCCESS)); + assertThat(snapshotInfo.totalShards(), is(0)); + + logger.info("--> verify that snapshot without index shows up in non-verbose listing"); + final List snapshotInfos = + client().admin().cluster().prepareGetSnapshots("test-repo").setVerbose(false).get().getSnapshots(); + assertThat(snapshotInfos, hasSize(1)); + final SnapshotInfo found = snapshotInfos.get(0); + assertThat(found.snapshotId(), is(snapshotInfo.snapshotId())); + assertThat(found.state(), is(SnapshotState.SUCCESS)); + } }