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

Wrap Exceptions Thrown by GetSnapshotsResponse #49522

Closed
wants to merge 3 commits into from
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 @@ -162,7 +162,7 @@ public List<SnapshotInfo> getSnapshots(String repo) {
if (error == null) {
throw new IllegalArgumentException("No such repository");
}
throw error;
throw new ElasticsearchException(error);
}

/**
Expand Down Expand Up @@ -265,4 +265,4 @@ public static GetSnapshotsResponse fromXContent(XContentParser parser) throws IO
public String toString() {
return Strings.toString(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
Expand All @@ -55,7 +54,7 @@ protected Settings repositorySettings() {
return settings.build();
}

public void testMissingDirectoriesNotCreatedInReadonlyRepository() throws IOException, ExecutionException, InterruptedException {
public void testMissingDirectoriesNotCreatedInReadonlyRepository() throws IOException, InterruptedException {
final String repoName = randomName();
final Path repoPath = randomRepoPath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoryMissingException;
import org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase;
import org.elasticsearch.rest.AbstractRestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
Expand Down Expand Up @@ -490,7 +491,7 @@ public void testSnapshotWithStuckNode() throws Exception {
}

logger.info("--> making sure that snapshot no longer exists");
expectThrows(SnapshotMissingException.class,
ESBlobStoreRepositoryIntegTestCase.expectSnapshotMissingException(
() -> client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap")
.execute().actionGet().getSnapshots("test-repo"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.shard.IndexShardTests.getEngineFromShard;
import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.expectSnapshotMissingException;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
Expand Down Expand Up @@ -1405,7 +1406,7 @@ public void testGetSnapshotsMultipleRepos() throws Exception {
.get();

for (String repo : repoList) {
expectThrows(SnapshotMissingException.class, () -> getSnapshotsResponse2.getSnapshots(repo));
expectSnapshotMissingException(() -> getSnapshotsResponse2.getSnapshots(repo));
}


Expand Down Expand Up @@ -1470,8 +1471,8 @@ public void testDeleteSnapshotWithMissingIndexAndShardMetadata() throws Exceptio

logger.info("--> make sure snapshot doesn't exist");

expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots("test-repo")
.addSnapshots("test-snap-1").get().getSnapshots("test-repo"));
expectSnapshotMissingException(
() -> client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1").get().getSnapshots("test-repo"));

for (String index : indices) {
assertTrue(Files.notExists(indicesPath.resolve(indexIds.get(index).getId())));
Expand Down Expand Up @@ -1510,7 +1511,7 @@ public void testDeleteSnapshotWithMissingMetadata() throws Exception {
client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get();

logger.info("--> make sure snapshot doesn't exist");
expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots("test-repo")
expectSnapshotMissingException(() -> client.admin().cluster().prepareGetSnapshots("test-repo")
.addSnapshots("test-snap-1").get().getSnapshots("test-repo"));
}

Expand Down Expand Up @@ -1547,9 +1548,8 @@ public void testDeleteSnapshotWithCorruptedSnapshotFile() throws Exception {
client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get();

logger.info("--> make sure snapshot doesn't exist");
expectThrows(SnapshotMissingException.class,
() -> client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1").get().
getSnapshots("test-repo"));
expectSnapshotMissingException(() -> client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1").get().
getSnapshots("test-repo"));

logger.info("--> make sure that we can create the snapshot again");
createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1")
Expand Down Expand Up @@ -1606,10 +1606,10 @@ public void testDeleteSnapshotWithCorruptedGlobalState() throws Exception {
assertThat(snapshotStatusResponse.getSnapshots().get(0).getSnapshot().getSnapshotId().getName(), equalTo("test-snap"));

assertAcked(client().admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get());
expectThrows(SnapshotMissingException.class, () -> client().admin().cluster()
.prepareGetSnapshots("test-repo").addSnapshots("test-snap").get().getSnapshots("test-repo"));
assertThrows(client().admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap"),
SnapshotMissingException.class);
expectSnapshotMissingException(
() -> client().admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap").get().getSnapshots("test-repo"));
expectSnapshotMissingException(
() -> client().admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap").get());

createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap")
.setIncludeGlobalState(true)
Expand Down Expand Up @@ -2756,13 +2756,10 @@ public void testSnapshotName() throws Exception {

expectThrows(InvalidSnapshotNameException.class,
() -> client.admin().cluster().prepareCreateSnapshot("test-repo", "_foo").get());
expectThrows(SnapshotMissingException.class,
() -> client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("_foo")
.get().getSnapshots("test-repo"));
expectThrows(SnapshotMissingException.class,
() -> client.admin().cluster().prepareDeleteSnapshot("test-repo", "_foo").get());
expectThrows(SnapshotMissingException.class,
() -> client.admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("_foo").get());
expectSnapshotMissingException(
() -> client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("_foo").get().getSnapshots("test-repo"));
expectSnapshotMissingException(() -> client.admin().cluster().prepareDeleteSnapshot("test-repo", "_foo").get());
expectSnapshotMissingException(() -> client.admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("_foo").get());
}

public void testListCorruptedSnapshot() throws Exception {
Expand Down Expand Up @@ -2811,8 +2808,9 @@ public void testListCorruptedSnapshot() throws Exception {
assertThat(snapshotInfos.get(0).state(), equalTo(SnapshotState.SUCCESS));
assertThat(snapshotInfos.get(0).snapshotId().getName(), equalTo("test-snap-1"));

final SnapshotException ex = expectThrows(SnapshotException.class, () ->
final ElasticsearchException e = expectThrows(ElasticsearchException.class, () ->
client.admin().cluster().prepareGetSnapshots("test-repo").setIgnoreUnavailable(false).get().getSnapshots("test-repo"));
final SnapshotException ex = (SnapshotException) ExceptionsHelper.unwrap(e, SnapshotException.class);
assertThat(ex.getRepositoryName(), equalTo("test-repo"));
assertThat(ex.getSnapshotName(), equalTo("test-snap-2"));
}
Expand Down Expand Up @@ -3131,12 +3129,12 @@ public void testGetSnapshotsRequest() throws Exception {
.put("wait_after_unblock", 200)));

logger.info("--> get snapshots on an empty repository");
expectThrows(SnapshotMissingException.class, () -> client.admin()
.cluster()
.prepareGetSnapshots(repositoryName)
.addSnapshots("non-existent-snapshot")
.get()
.getSnapshots(repositoryName));
expectSnapshotMissingException(() -> client.admin()
.cluster()
.prepareGetSnapshots(repositoryName)
.addSnapshots("non-existent-snapshot")
.get()
.getSnapshots(repositoryName));
// with ignore unavailable set to true, should not throw an exception
GetSnapshotsResponse getSnapshotsResponse = client.admin()
.cluster()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.elasticsearch.repositories.blobstore;

import org.apache.lucene.util.SetOnce;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequestBuilder;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequestBuilder;
Expand Down Expand Up @@ -62,6 +64,13 @@ public static RepositoryData getRepositoryData(Repository repository) {
return PlainActionFuture.get(repository::getRepositoryData);
}

public static SnapshotMissingException expectSnapshotMissingException(ThrowingRunnable runnable) {
final ElasticsearchException ex = expectThrows(ElasticsearchException.class, runnable);
final Throwable sme = ExceptionsHelper.unwrap(ex, SnapshotMissingException.class);
assertThat(sme, notNullValue());
return (SnapshotMissingException) sme;
}

protected abstract String repositoryType();

protected Settings repositorySettings() {
Expand Down Expand Up @@ -153,7 +162,7 @@ public void testSnapshotAndRestore() throws Exception {
logger.info("--> delete snapshot {}:{}", repoName, snapshotName);
assertAcked(client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotName).get());

expectThrows(SnapshotMissingException.class, () ->
expectSnapshotMissingException(() ->
client().admin().cluster().prepareGetSnapshots(repoName).setSnapshots(snapshotName).get().getSnapshots(repoName));

expectThrows(SnapshotMissingException.class, () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.elasticsearch.xpack.slm;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse;
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus;
Expand Down Expand Up @@ -53,6 +55,7 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

/**
* Tests for Snapshot Lifecycle Management that require a slow or blocked snapshot repo (using {@link MockRepository}
Expand Down Expand Up @@ -298,7 +301,7 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex
.prepareGetSnapshots(REPO).setSnapshots(failedSnapshotName.get()).get();
SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots(REPO).get(0);
assertEquals(expectedUnsuccessfulState, snapshotInfo.state());
} catch (SnapshotMissingException ex) {
} catch (ElasticsearchException ex) {
logger.info("failed to find snapshot {}, retrying", failedSnapshotName);
throw new AssertionError(ex);
}
Expand Down Expand Up @@ -359,7 +362,8 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex
GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster()
.prepareGetSnapshots(REPO).setSnapshots(failedSnapshotName.get()).get();
assertThat(snapshotsStatusResponse.getSnapshots(REPO), empty());
} catch (SnapshotMissingException e) {
} catch (ElasticsearchException e) {
assertThat(ExceptionsHelper.unwrap(e, SnapshotMissingException.class), notNullValue());
// This is what we want to happen
}
logger.info("--> {} snapshot [{}] has been deleted, checking successful snapshot [{}] still exists",
Expand Down