From db5594a2af1f18f44ef5e6093922dfbd83c06348 Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Tue, 1 Dec 2015 15:41:30 +0100 Subject: [PATCH] Support wildcards for getting repositories or snapshots Closes #15151 --- .../get/TransportGetRepositoriesAction.java | 17 +++++++++++++- .../get/TransportGetSnapshotsAction.java | 23 +++++++++++++++++-- .../snapshots/RepositoriesIT.java | 3 ++- .../SharedClusterSnapshotRestoreIT.java | 5 +++- docs/reference/modules/snapshots.asciidoc | 17 ++++++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java index d09c73e17b2..39d9cacbda3 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java @@ -31,6 +31,7 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.RepositoriesMetaData; import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.repositories.RepositoryMissingException; import org.elasticsearch.threadpool.ThreadPool; @@ -38,7 +39,9 @@ import org.elasticsearch.transport.TransportService; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * Transport action for get repositories operation @@ -78,8 +81,20 @@ public class TransportGetRepositoriesAction extends TransportMasterNodeReadActio } } else { if (repositories != null) { + Set repositoriesToGet = new LinkedHashSet<>(); // to keep insertion order + for (String repositoryOrPattern : request.repositories()) { + if (Regex.isSimpleMatchPattern(repositoryOrPattern) == false) { + repositoriesToGet.add(repositoryOrPattern); + } else { + for (RepositoryMetaData repository : repositories.repositories()) { + if (Regex.simpleMatch(repositoryOrPattern, repository.name())) { + repositoriesToGet.add(repository.name()); + } + } + } + } List repositoryListBuilder = new ArrayList<>(); - for (String repository : request.repositories()) { + for (String repository : repositoriesToGet) { RepositoryMetaData repositoryMetaData = repositories.repository(repository); if (repositoryMetaData == null) { listener.onFailure(new RepositoryMissingException(repository)); diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java index 7bfb0e8dc26..478146de357 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java @@ -29,6 +29,7 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.SnapshotId; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotInfo; @@ -38,7 +39,9 @@ import org.elasticsearch.transport.TransportService; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * Transport Action for get snapshots operation @@ -84,8 +87,24 @@ public class TransportGetSnapshotsAction extends TransportMasterNodeAction snapshotsToGet = new LinkedHashSet<>(); // to keep insertion order + List snapshots = null; + for (String snapshotOrPattern : request.snapshots()) { + if (Regex.isSimpleMatchPattern(snapshotOrPattern) == false) { + snapshotsToGet.add(snapshotOrPattern); + } else { + if (snapshots == null) { // lazily load snapshots + snapshots = snapshotsService.snapshots(request.repository(), request.ignoreUnavailable()); + } + for (Snapshot snapshot : snapshots) { + if (Regex.simpleMatch(snapshotOrPattern, snapshot.name())) { + snapshotsToGet.add(snapshot.name()); + } + } + } + } + for (String snapshot : snapshotsToGet) { + SnapshotId snapshotId = new SnapshotId(request.repository(), snapshot); snapshotInfoBuilder.add(new SnapshotInfo(snapshotsService.snapshot(snapshotId))); } } diff --git a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java index 0bfc76f4d90..b0de06138a8 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java @@ -94,7 +94,8 @@ public class RepositoriesIT extends AbstractSnapshotIntegTestCase { assertThat(repositoriesMetaData.repository("test-repo-2").type(), equalTo("fs")); logger.info("--> check that both repositories can be retrieved by getRepositories query"); - GetRepositoriesResponse repositoriesResponse = client.admin().cluster().prepareGetRepositories().get(); + GetRepositoriesResponse repositoriesResponse = client.admin().cluster() + .prepareGetRepositories(randomFrom("_all", "*", "test-repo-*")).get(); assertThat(repositoriesResponse.repositories().size(), equalTo(2)); assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-1"), notNullValue()); assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue()); diff --git a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index afbdf9d48d5..57a22c0dd15 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -149,7 +149,10 @@ public class SharedClusterSnapshotRestoreIT extends AbstractSnapshotIntegTestCas assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); - SnapshotInfo snapshotInfo = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0); + List snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo") + .setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots(); + assertThat(snapshotInfos.size(), equalTo(1)); + SnapshotInfo snapshotInfo = snapshotInfos.get(0); assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS)); assertThat(snapshotInfo.version(), equalTo(Version.CURRENT)); diff --git a/docs/reference/modules/snapshots.asciidoc b/docs/reference/modules/snapshots.asciidoc index b6371dc36a7..dbb9f395402 100644 --- a/docs/reference/modules/snapshots.asciidoc +++ b/docs/reference/modules/snapshots.asciidoc @@ -45,6 +45,15 @@ which returns: } ----------------------------------- +Information about multiple repositories can be fetched in one go by using a comma-delimited list of repository names. +Star wildcards are supported as well. For example, information about repositories that start with `repo` or that contain `backup` +can be obtained using the following command: + +[source,js] +----------------------------------- +GET /_snapshot/repo*,*backup* +----------------------------------- + If a repository name is not specified, or `_all` is used as repository name Elasticsearch will return information about all repositories currently registered in the cluster: @@ -251,6 +260,14 @@ GET /_snapshot/my_backup/snapshot_1 ----------------------------------- // AUTOSENSE +Similar as for repositories, information about multiple snapshots can be queried in one go, supporting wildcards as well: + +[source,sh] +----------------------------------- +GET /_snapshot/my_backup/snapshot_*,some_other_snapshot +----------------------------------- +// AUTOSENSE + All snapshots currently stored in the repository can be listed using the following command: [source,sh]