Skip to content

Commit ce50fb4

Browse files
author
Yannick Welsch
committed
Merge pull request #15151 from ywelsch/feature/get-snapshot-wildcards
Support wildcards for getting repositories and snapshots
2 parents 7f4ef9f + db5594a commit ce50fb4

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@
3131
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
3232
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
3333
import org.elasticsearch.common.inject.Inject;
34+
import org.elasticsearch.common.regex.Regex;
3435
import org.elasticsearch.common.settings.Settings;
3536
import org.elasticsearch.repositories.RepositoryMissingException;
3637
import org.elasticsearch.threadpool.ThreadPool;
3738
import org.elasticsearch.transport.TransportService;
3839

3940
import java.util.ArrayList;
4041
import java.util.Collections;
42+
import java.util.LinkedHashSet;
4143
import java.util.List;
44+
import java.util.Set;
4245

4346
/**
4447
* Transport action for get repositories operation
@@ -78,8 +81,20 @@ protected void masterOperation(final GetRepositoriesRequest request, ClusterStat
7881
}
7982
} else {
8083
if (repositories != null) {
84+
Set<String> repositoriesToGet = new LinkedHashSet<>(); // to keep insertion order
85+
for (String repositoryOrPattern : request.repositories()) {
86+
if (Regex.isSimpleMatchPattern(repositoryOrPattern) == false) {
87+
repositoriesToGet.add(repositoryOrPattern);
88+
} else {
89+
for (RepositoryMetaData repository : repositories.repositories()) {
90+
if (Regex.simpleMatch(repositoryOrPattern, repository.name())) {
91+
repositoriesToGet.add(repository.name());
92+
}
93+
}
94+
}
95+
}
8196
List<RepositoryMetaData> repositoryListBuilder = new ArrayList<>();
82-
for (String repository : request.repositories()) {
97+
for (String repository : repositoriesToGet) {
8398
RepositoryMetaData repositoryMetaData = repositories.repository(repository);
8499
if (repositoryMetaData == null) {
85100
listener.onFailure(new RepositoryMissingException(repository));

core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/TransportGetSnapshotsAction.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
3030
import org.elasticsearch.cluster.metadata.SnapshotId;
3131
import org.elasticsearch.common.inject.Inject;
32+
import org.elasticsearch.common.regex.Regex;
3233
import org.elasticsearch.common.settings.Settings;
3334
import org.elasticsearch.snapshots.Snapshot;
3435
import org.elasticsearch.snapshots.SnapshotInfo;
@@ -38,7 +39,9 @@
3839

3940
import java.util.ArrayList;
4041
import java.util.Collections;
42+
import java.util.LinkedHashSet;
4143
import java.util.List;
44+
import java.util.Set;
4245

4346
/**
4447
* Transport Action for get snapshots operation
@@ -84,8 +87,24 @@ protected void masterOperation(final GetSnapshotsRequest request, ClusterState s
8487
snapshotInfoBuilder.add(new SnapshotInfo(snapshot));
8588
}
8689
} else {
87-
for (int i = 0; i < request.snapshots().length; i++) {
88-
SnapshotId snapshotId = new SnapshotId(request.repository(), request.snapshots()[i]);
90+
Set<String> snapshotsToGet = new LinkedHashSet<>(); // to keep insertion order
91+
List<Snapshot> snapshots = null;
92+
for (String snapshotOrPattern : request.snapshots()) {
93+
if (Regex.isSimpleMatchPattern(snapshotOrPattern) == false) {
94+
snapshotsToGet.add(snapshotOrPattern);
95+
} else {
96+
if (snapshots == null) { // lazily load snapshots
97+
snapshots = snapshotsService.snapshots(request.repository(), request.ignoreUnavailable());
98+
}
99+
for (Snapshot snapshot : snapshots) {
100+
if (Regex.simpleMatch(snapshotOrPattern, snapshot.name())) {
101+
snapshotsToGet.add(snapshot.name());
102+
}
103+
}
104+
}
105+
}
106+
for (String snapshot : snapshotsToGet) {
107+
SnapshotId snapshotId = new SnapshotId(request.repository(), snapshot);
89108
snapshotInfoBuilder.add(new SnapshotInfo(snapshotsService.snapshot(snapshotId)));
90109
}
91110
}

core/src/test/java/org/elasticsearch/snapshots/RepositoriesIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public void testRepositoryCreation() throws Exception {
9494
assertThat(repositoriesMetaData.repository("test-repo-2").type(), equalTo("fs"));
9595

9696
logger.info("--> check that both repositories can be retrieved by getRepositories query");
97-
GetRepositoriesResponse repositoriesResponse = client.admin().cluster().prepareGetRepositories().get();
97+
GetRepositoriesResponse repositoriesResponse = client.admin().cluster()
98+
.prepareGetRepositories(randomFrom("_all", "*", "test-repo-*")).get();
9899
assertThat(repositoriesResponse.repositories().size(), equalTo(2));
99100
assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-1"), notNullValue());
100101
assertThat(findRepository(repositoriesResponse.repositories(), "test-repo-2"), notNullValue());

core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public void testBasicWorkFlow() throws Exception {
149149
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
150150
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
151151

152-
SnapshotInfo snapshotInfo = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0);
152+
List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo")
153+
.setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots();
154+
assertThat(snapshotInfos.size(), equalTo(1));
155+
SnapshotInfo snapshotInfo = snapshotInfos.get(0);
153156
assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
154157
assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
155158

docs/reference/modules/snapshots.asciidoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ which returns:
4545
}
4646
-----------------------------------
4747

48+
Information about multiple repositories can be fetched in one go by using a comma-delimited list of repository names.
49+
Star wildcards are supported as well. For example, information about repositories that start with `repo` or that contain `backup`
50+
can be obtained using the following command:
51+
52+
[source,js]
53+
-----------------------------------
54+
GET /_snapshot/repo*,*backup*
55+
-----------------------------------
56+
4857
If a repository name is not specified, or `_all` is used as repository name Elasticsearch will return information about
4958
all repositories currently registered in the cluster:
5059

@@ -251,6 +260,14 @@ GET /_snapshot/my_backup/snapshot_1
251260
-----------------------------------
252261
// AUTOSENSE
253262

263+
Similar as for repositories, information about multiple snapshots can be queried in one go, supporting wildcards as well:
264+
265+
[source,sh]
266+
-----------------------------------
267+
GET /_snapshot/my_backup/snapshot_*,some_other_snapshot
268+
-----------------------------------
269+
// AUTOSENSE
270+
254271
All snapshots currently stored in the repository can be listed using the following command:
255272

256273
[source,sh]

0 commit comments

Comments
 (0)