Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple repositories in get snapshot request #41799

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static Request createSnapshot(CreateSnapshotRequest createSnapshotRequest) throw

static Request getSnapshots(GetSnapshotsRequest getSnapshotsRequest) {
RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot")
.addPathPart(getSnapshotsRequest.repository());
.addCommaSeparatedPathParts(getSnapshotsRequest.repositories());
String endpoint;
if (getSnapshotsRequest.snapshots().length == 0) {
endpoint = endpointBuilder.addPathPart("_all").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -155,37 +153,44 @@ public void testCreateSnapshot() throws IOException {
}

public void testGetSnapshots() throws IOException {
String repository = "test_repository";
String repository1 = "test_repository1";
String repository2 = "test_repository2";
String snapshot1 = "test_snapshot1";
String snapshot2 = "test_snapshot2";

AcknowledgedResponse putRepositoryResponse = createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}");
AcknowledgedResponse putRepositoryResponse =
createTestRepository(repository1, FsRepository.TYPE, "{\"location\": \"loc1\"}");
assertTrue(putRepositoryResponse.isAcknowledged());

CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository, snapshot1);
AcknowledgedResponse putRepositoryResponse2 =
createTestRepository(repository2, FsRepository.TYPE, "{\"location\": \"loc2\"}");
assertTrue(putRepositoryResponse2.isAcknowledged());

CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository1, snapshot1);
createSnapshotRequest1.waitForCompletion(true);
CreateSnapshotResponse putSnapshotResponse1 = createTestSnapshot(createSnapshotRequest1);
CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest(repository, snapshot2);
CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest(repository2, snapshot2);
createSnapshotRequest2.waitForCompletion(true);
CreateSnapshotResponse putSnapshotResponse2 = createTestSnapshot(createSnapshotRequest2);
// check that the request went ok without parsing JSON here. When using the high level client, check acknowledgement instead.
assertEquals(RestStatus.OK, putSnapshotResponse1.status());
assertEquals(RestStatus.OK, putSnapshotResponse2.status());

GetSnapshotsRequest request;
if (randomBoolean()) {
request = new GetSnapshotsRequest(repository);
} else if (randomBoolean()) {
request = new GetSnapshotsRequest(repository, new String[] {"_all"});
GetSnapshotsRequest request = new GetSnapshotsRequest(
randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{repository1, repository2}),
randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{snapshot1, snapshot2})
);
request.ignoreUnavailable(true);

} else {
request = new GetSnapshotsRequest(repository, new String[] {snapshot1, snapshot2});
}
GetSnapshotsResponse response = execute(request, highLevelClient().snapshot()::get, highLevelClient().snapshot()::getAsync);

logger.info(response.getSnapshots());
assertEquals(2, response.getSnapshots().size());
assertThat(response.getSnapshots().stream().map((s) -> s.snapshotId().getName()).collect(Collectors.toList()),
contains("test_snapshot1", "test_snapshot2"));
assertEquals(repository1, response.getSnapshots().get(0).repository());
assertEquals(snapshot1, response.getSnapshots().get(0).snapshotId().getName());

assertEquals(repository2, response.getSnapshots().get(1).repository());
assertEquals(snapshot2, response.getSnapshots().get(1).snapshotId().getName());
}

public void testSnapshotsStatus() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -148,15 +147,16 @@ public void testCreateSnapshot() throws IOException {

public void testGetSnapshots() {
Map<String, String> expectedParams = new HashMap<>();
String repository = RequestConvertersTests.randomIndicesNames(1, 1)[0];
String repository1 = randomAlphaOfLength(10);
String repository2 = randomAlphaOfLength(10);
String snapshot1 = "snapshot1-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT);
String snapshot2 = "snapshot2-" + randomAlphaOfLengthBetween(2, 5).toLowerCase(Locale.ROOT);

String endpoint = String.format(Locale.ROOT, "/_snapshot/%s/%s,%s", repository, snapshot1, snapshot2);
String endpoint = String.format(Locale.ROOT, "/_snapshot/%s,%s/%s,%s", repository1, repository2, snapshot1, snapshot2);

GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest();
getSnapshotsRequest.repository(repository);
getSnapshotsRequest.snapshots(Arrays.asList(snapshot1, snapshot2).toArray(new String[0]));
getSnapshotsRequest.repositories(repository1, repository2);
getSnapshotsRequest.snapshots(new String[]{snapshot1, snapshot2});
RequestConvertersTests.setRandomMasterTimeout(getSnapshotsRequest, expectedParams);

if (randomBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public void testSnapshotGetSnapshots() throws IOException {
// end::get-snapshots-request

// tag::get-snapshots-request-repositoryName
request.repository(repositoryName); // <1>
request.repositories(repositoryName); // <1>
// end::get-snapshots-request-repositoryName

// tag::get-snapshots-request-snapshots
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/cat/snapshots.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Which looks like:

[source,txt]
--------------------------------------------------
id status start_epoch start_time end_epoch end_time duration indices successful_shards failed_shards total_shards
snap1 FAILED 1445616705 18:11:45 1445616978 18:16:18 4.6m 1 4 1 5
snap2 SUCCESS 1445634298 23:04:58 1445634672 23:11:12 6.2m 2 10 0 10
id repository status start_epoch start_time end_epoch end_time duration indices successful_shards failed_shards total_shards
snap1 repo1 FAILED 1445616705 18:11:45 1445616978 18:16:18 4.6m 1 4 1 5
snap2 repo1 SUCCESS 1445634298 23:04:58 1445634672 23:11:12 6.2m 2 10 0 10
--------------------------------------------------
// TESTRESPONSE[s/FAILED/SUCCESS/ s/14456\d+/\\d+/ s/\d+(\.\d+)?(m|s|ms)/\\d+(\\.\\d+)?(m|s|ms)/]
// TESTRESPONSE[s/\d+:\d+:\d+/\\d+:\\d+:\\d+/]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
---
"Help":
- skip:
version: " - 7.2.99"
reason: PR https://github.com/elastic/elasticsearch/pull/41799 not yet backported

- do:
cat.snapshots:
help: true

- match:
$body: |
/^ id .+ \n
repository .+ \n
status .+ \n
start_epoch .+ \n
start_time .+ \n
Expand All @@ -21,6 +26,9 @@
$/
---
"Test cat snapshots output":
- skip:
version: " - 7.2.99"
reason: PR https://github.com/elastic/elasticsearch/pull/41799 not yet backported

- do:
snapshot.create_repository:
Expand Down Expand Up @@ -74,6 +82,6 @@

- match:
$body: |
/^ snap1\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
snap2\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
/^ snap1\s+ test_cat_snapshots_1\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
snap2\s+ test_cat_snapshots_1\s+ SUCCESS\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \d+\s+ \d\d\:\d\d\:\d\d\s+ \S+\s+ 2\s+ 2\s+ 0\s+ 2\s*\n
$/
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public List<RepositoryMetaData> repositories() {
return repositories.repositories();
}

public GetRepositoriesResponse(StreamInput in) throws IOException {
readFrom(in);
}

@Override
public void readFrom(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.elasticsearch.action.admin.cluster.snapshots.get;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

public class GetSnapshotResponseFailureItem implements Streamable, ToXContentObject, Comparable<GetSnapshotResponseFailureItem> {
static final ConstructingObjectParser<GetSnapshotResponseFailureItem, Void> PARSER =
new ConstructingObjectParser<>(GetSnapshotResponseFailureItem.class.getName(), true,
(args) -> new GetSnapshotResponseFailureItem((String) args[0], (ElasticsearchException) args[1]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("repository"));
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p),
new ParseField("error"));
}

private String repository;
private Exception error;


public String getRepository() {
return repository;
}

public Exception getError() {
return error;
}


GetSnapshotResponseFailureItem(String repository, ElasticsearchException error) {
this.repository = repository;
this.error = error;
}

public GetSnapshotResponseFailureItem(StreamInput in) throws IOException {
readFrom(in);
}

@Override
public void readFrom(StreamInput in) throws IOException {
repository = in.readString();
error = in.readException();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(repository);
out.writeException(error);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("repository", repository);
ElasticsearchException.generateFailureXContent(builder, params, error, true);
builder.endObject();
return builder;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetSnapshotResponseFailureItem that = (GetSnapshotResponseFailureItem) o;
return Objects.equals(repository, that.repository) && Objects.equals(error, that.error);
}

@Override
public int hashCode() {
return Objects.hash(repository, error);
}

@Override
public int compareTo(GetSnapshotResponseFailureItem o) {
return repository.compareTo(o.repository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.admin.cluster.snapshots.get;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.Strings;
Expand All @@ -37,8 +38,9 @@ public class GetSnapshotsRequest extends MasterNodeRequest<GetSnapshotsRequest>
public static final String ALL_SNAPSHOTS = "_all";
public static final String CURRENT_SNAPSHOT = "_current";
public static final boolean DEFAULT_VERBOSE_MODE = true;
public static final Version MULTIPLE_REPOSITORIES_SUPPORT_ADDED = Version.V_8_0_0;

private String repository;
private String[] repositories;

private String[] snapshots = Strings.EMPTY_ARRAY;

Expand All @@ -50,28 +52,32 @@ public GetSnapshotsRequest() {
}

/**
* Constructs a new get snapshots request with given repository name and list of snapshots
* Constructs a new get snapshots request with given repository names and list of snapshots
*
* @param repository repository name
* @param repositories repository names
* @param snapshots list of snapshots
*/
public GetSnapshotsRequest(String repository, String[] snapshots) {
this.repository = repository;
public GetSnapshotsRequest(String[] repositories, String[] snapshots) {
this.repositories = repositories;
this.snapshots = snapshots;
}

/**
* Constructs a new get snapshots request with given repository name
* Constructs a new get snapshots request with given repository names
*
* @param repository repository name
* @param repositories repository names
*/
public GetSnapshotsRequest(String repository) {
this.repository = repository;
public GetSnapshotsRequest(String... repositories) {
this.repositories = repositories;
}

public GetSnapshotsRequest(StreamInput in) throws IOException {
super(in);
repository = in.readString();
if (in.getVersion().onOrAfter(MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
repositories = in.readStringArray();
} else {
repositories = new String[]{in.readString()};
}
snapshots = in.readStringArray();
ignoreUnavailable = in.readBoolean();
verbose = in.readBoolean();
Expand All @@ -80,7 +86,11 @@ public GetSnapshotsRequest(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(repository);
if (out.getVersion().onOrAfter(MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
out.writeStringArray(repositories);
} else {
out.writeString(repositories[0]);
}
out.writeStringArray(snapshots);
out.writeBoolean(ignoreUnavailable);
out.writeBoolean(verbose);
Expand All @@ -89,30 +99,30 @@ public void writeTo(StreamOutput out) throws IOException {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (repository == null) {
validationException = addValidationError("repository is missing", validationException);
if (repositories == null) {
validationException = addValidationError("repositories are missing", validationException);
}
return validationException;
}

/**
* Sets repository name
* Sets repository names
*
* @param repository repository name
* @param repositories repository names
* @return this request
*/
public GetSnapshotsRequest repository(String repository) {
this.repository = repository;
public GetSnapshotsRequest repositories(String... repositories) {
this.repositories = repositories;
return this;
}

/**
* Returns repository name
* Returns repository names
*
* @return repository name
* @return repository names
*/
public String repository() {
return this.repository;
public String[] repositories() {
return this.repositories;
}

/**
Expand Down
Loading