use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.
the class SnapshotRequestConvertersTests method testCreateSnapshot.
public void testCreateSnapshot() throws IOException {
Map<String, String> expectedParams = new HashMap<>();
String repository = RequestConvertersTests.randomIndicesNames(1, 1)[0];
String snapshot = "snapshot-" + generateRandomStringArray(1, randomInt(10), false, false)[0];
String endpoint = "/_snapshot/" + repository + "/" + snapshot;
CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(repository, snapshot);
RequestConvertersTests.setRandomMasterTimeout(createSnapshotRequest, expectedParams);
Boolean waitForCompletion = randomBoolean();
createSnapshotRequest.waitForCompletion(waitForCompletion);
expectedParams.put("wait_for_completion", waitForCompletion.toString());
Request request = SnapshotRequestConverters.createSnapshot(createSnapshotRequest);
assertThat(request.getEndpoint(), equalTo(endpoint));
assertThat(request.getMethod(), equalTo(HttpPut.METHOD_NAME));
assertThat(request.getParameters(), equalTo(expectedParams));
RequestConvertersTests.assertToXContentBody(createSnapshotRequest, request.getEntity());
}
use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.
the class SnapshotIT method testCreateSnapshot.
public void testCreateSnapshot() throws Exception {
String repository = "test_repository";
assertTrue(createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());
String snapshot = "test_snapshot";
CreateSnapshotRequest request = new CreateSnapshotRequest(repository, snapshot);
boolean waitForCompletion = randomBoolean();
request.waitForCompletion(waitForCompletion);
if (randomBoolean()) {
request.userMetadata(randomUserMetadata());
}
request.partial(randomBoolean());
request.includeGlobalState(randomBoolean());
CreateSnapshotResponse response = createTestSnapshot(request);
assertEquals(waitForCompletion ? RestStatus.OK : RestStatus.ACCEPTED, response.status());
if (waitForCompletion == false) {
// If we don't wait for the snapshot to complete we have to cancel it to not leak the snapshot task
AcknowledgedResponse deleteResponse = execute(new DeleteSnapshotRequest(repository, snapshot), highLevelClient().snapshot()::delete, highLevelClient().snapshot()::deleteAsync);
assertTrue(deleteResponse.isAcknowledged());
}
}
use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.
the class DedicatedClusterSnapshotRestoreIT method testCreateSnapshotLegacyPath.
/**
* Tests for the legacy snapshot path that is normally executed if the cluster contains any nodes older than
* {@link SnapshotsService#NO_REPO_INITIALIZE_VERSION}.
* Makes sure that blocking as well as non-blocking snapshot create paths execute cleanly as well as that error handling works out
* correctly by testing a snapshot name collision.
*/
public void testCreateSnapshotLegacyPath() throws Exception {
final String masterNode = internalCluster().startMasterOnlyNode();
internalCluster().startDataOnlyNode();
final String repoName = "test-repo";
createRepository(repoName, "fs");
createIndex("some-index");
final SnapshotsService snapshotsService = internalCluster().getMasterNodeInstance(SnapshotsService.class);
final Snapshot snapshot1 = PlainActionFuture.get(f -> snapshotsService.createSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-1"), f));
awaitNoMoreRunningOperations(masterNode);
final InvalidSnapshotNameException sne = expectThrows(InvalidSnapshotNameException.class, () -> PlainActionFuture.<SnapshotInfo, Exception>get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, snapshot1.getSnapshotId().getName()), f)));
assertThat(sne.getMessage(), containsString("snapshot with the same name already exists"));
final SnapshotInfo snapshot2 = PlainActionFuture.get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-2"), f));
assertThat(snapshot2.state(), is(SnapshotState.SUCCESS));
final SnapshotInfo snapshot3 = PlainActionFuture.get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-3").indices("does-not-exist-*"), f));
assertThat(snapshot3.state(), is(SnapshotState.SUCCESS));
}
use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.
the class SnapshotRequestsTests method testCreateSnapshotRequestParsing.
public void testCreateSnapshotRequestParsing() throws IOException {
CreateSnapshotRequest request = new CreateSnapshotRequest("test-repo", "test-snap");
XContentBuilder builder = jsonBuilder().startObject();
if (randomBoolean()) {
builder.field("indices", "foo,bar,baz");
} else {
builder.startArray("indices");
builder.value("foo");
builder.value("bar");
builder.value("baz");
builder.endArray();
}
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
if (indicesOptions.expandWildcardsClosed()) {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "all");
} else {
builder.field("expand_wildcards", "closed");
}
} else {
if (indicesOptions.expandWildcardsOpen()) {
builder.field("expand_wildcards", "open");
} else {
builder.field("expand_wildcards", "none");
}
}
builder.field("allow_no_indices", indicesOptions.allowNoIndices());
boolean partial = randomBoolean();
builder.field("partial", partial);
builder.startObject("settings").field("set1", "val1").endObject();
builder.startObject("index_settings").field("set1", "val2").endObject();
if (randomBoolean()) {
builder.field("ignore_index_settings", "set2,set3");
} else {
builder.startArray("ignore_index_settings");
builder.value("set2");
builder.value("set3");
builder.endArray();
}
boolean includeIgnoreUnavailable = randomBoolean();
if (includeIgnoreUnavailable) {
builder.field("ignore_unavailable", indicesOptions.ignoreUnavailable());
}
BytesReference bytes = BytesReference.bytes(builder.endObject());
request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
assertEquals("test-repo", request.repository());
assertEquals("test-snap", request.snapshot());
assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
assertEquals(partial, request.partial());
assertEquals("val1", request.settings().get("set1"));
boolean expectedIgnoreAvailable = includeIgnoreUnavailable ? indicesOptions.ignoreUnavailable() : IndicesOptions.strictExpandOpen().ignoreUnavailable();
assertEquals(expectedIgnoreAvailable, request.indicesOptions().ignoreUnavailable());
}
use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.
the class SnapshotIT method testRestoreSnapshot.
public void testRestoreSnapshot() throws IOException {
String testRepository = "test";
String testSnapshot = "snapshot_1";
String testIndex = "test_index";
String restoredIndex = testIndex + "_restored";
AcknowledgedResponse putRepositoryResponse = createTestRepository(testRepository, FsRepository.TYPE, "{\"location\": \".\"}");
assertTrue(putRepositoryResponse.isAcknowledged());
createIndex(testIndex, Settings.EMPTY);
assertTrue("index [" + testIndex + "] should have been created", indexExists(testIndex));
CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(testRepository, testSnapshot);
createSnapshotRequest.indices(testIndex);
createSnapshotRequest.waitForCompletion(true);
if (randomBoolean()) {
createSnapshotRequest.userMetadata(randomUserMetadata());
}
CreateSnapshotResponse createSnapshotResponse = createTestSnapshot(createSnapshotRequest);
assertEquals(RestStatus.OK, createSnapshotResponse.status());
deleteIndex(testIndex);
assertFalse("index [" + testIndex + "] should have been deleted", indexExists(testIndex));
RestoreSnapshotRequest request = new RestoreSnapshotRequest(testRepository, testSnapshot);
request.waitForCompletion(true);
request.renamePattern(testIndex);
request.renameReplacement(restoredIndex);
RestoreSnapshotResponse response = execute(request, highLevelClient().snapshot()::restore, highLevelClient().snapshot()::restoreAsync);
RestoreInfo restoreInfo = response.getRestoreInfo();
assertThat(restoreInfo.name(), equalTo(testSnapshot));
assertThat(restoreInfo.indices(), equalTo(Collections.singletonList(restoredIndex)));
assertThat(restoreInfo.successfulShards(), greaterThan(0));
assertThat(restoreInfo.failedShards(), equalTo(0));
}
Aggregations