Skip to content

GetSnapshotsResponse should re-wrap failed exceptions so callsites are visible in the stacktrace #43462

Closed
@dakrone

Description

@dakrone

From the following code:

public List<SnapshotInfo> getSnapshots(String repo) {
List<SnapshotInfo> snapshots = successfulResponses.get(repo);
if (snapshots != null) {
return snapshots;
}
ElasticsearchException error = failedResponses.get(repo);
if (error == null) {
throw new IllegalArgumentException("No such repository");
}
throw error;
}

It's possible for snapshot exceptions to be generated on a different line than where they are actually thrown, for example, code that prior to #42090 worked like this:

GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(new String[]{repo}, new String[]{snapshotName});
final GetSnapshotsResponse snaps;
try {
    snaps = client.snapshot().get(getSnapshotsRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
    if (e.getMessage().contains("snapshot_missing_exception")) {
        fail("snapshot does not exist: " + snapshotName);
    }
    throw e;
}

Optional<SnapshotInfo> info = snaps.getSnapshots(repo).stream().findFirst();
if (info.isPresent()) {
    info.ifPresent(si -> {
        assertThat(si.snapshotId().getName(), equalTo(snapshotName));
        assertThat(si.state(), equalTo(SnapshotState.SUCCESS));
    });
} else {
    fail("unable to find snapshot; " + snapshotName);
}

Now fails.

The exception is generated from the snaps.getSnapshots(repo) call above, however, the stacktrace for the exception actually traces back to the line

    snaps = client.snapshot().get(getSnapshotsRequest, RequestOptions.DEFAULT);

which is very confusing, because that line itself is wrapped in a try/catch.

GetSnapshotsResponse should change this line:

Into something like

throw new ElasticsearchException(error);

So that the stacktrace correctly identifies the caller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Distributed Coordination/Snapshot/RestoreAnything directly related to the `_snapshot/*` APIsTeam:Distributed (Obsolete)Meta label for distributed team (obsolete). Replaced by Distributed Indexing/Coordination.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions