Skip to content

Commit

Permalink
Support wildcards for getting repositories or snapshots
Browse files Browse the repository at this point in the history
Closes #15151
  • Loading branch information
Yannick Welsch committed Dec 11, 2015
1 parent 7f4ef9f commit db5594a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
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;
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
Expand Down Expand Up @@ -78,8 +81,20 @@ protected void masterOperation(final GetRepositoriesRequest request, ClusterStat
}
} else {
if (repositories != null) {
Set<String> 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<RepositoryMetaData> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand All @@ -38,7 +39,9 @@

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
Expand Down Expand Up @@ -84,8 +87,24 @@ protected void masterOperation(final GetSnapshotsRequest request, ClusterState s
snapshotInfoBuilder.add(new SnapshotInfo(snapshot));
}
} else {
for (int i = 0; i < request.snapshots().length; i++) {
SnapshotId snapshotId = new SnapshotId(request.repository(), request.snapshots()[i]);
Set<String> snapshotsToGet = new LinkedHashSet<>(); // to keep insertion order
List<Snapshot> 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)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void testRepositoryCreation() throws Exception {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ public void testBasicWorkFlow() throws Exception {
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<SnapshotInfo> 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));

Expand Down
17 changes: 17 additions & 0 deletions docs/reference/modules/snapshots.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit db5594a

Please sign in to comment.