Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/changelog/131677.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 131677
summary: Throw better exception if verifying empty repo
area: Snapshot/Restore
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.action.support.SubscribableListener;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand All @@ -35,6 +36,7 @@
import org.elasticsearch.repositories.blobstore.RepositoryFileType;
import org.elasticsearch.repositories.blobstore.testkit.SnapshotRepositoryTestKit;
import org.elasticsearch.repositories.fs.FsRepository;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase;
import org.elasticsearch.snapshots.SnapshotId;
import org.elasticsearch.snapshots.SnapshotInfo;
Expand Down Expand Up @@ -73,6 +75,7 @@
import static org.elasticsearch.repositories.blobstore.BlobStoreRepository.SNAPSHOT_FORMAT;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -713,6 +716,21 @@ public void testBlobInSnapshotNotShardGeneration() throws IOException {
}, "blob in snapshot but not shard generation");
}

public void testFreshRepository() {
final var repositoryName = randomIdentifier();
final var repositoryRootPath = randomRepoPath();

createRepository(repositoryName, FsRepository.TYPE, repositoryRootPath);
try {
final var request = new Request("POST", "/_snapshot/" + repositoryName + "/_verify_integrity");
final var responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request));
assertEquals(RestStatus.BAD_REQUEST.getStatus(), responseException.getResponse().getStatusLine().getStatusCode());
assertThat(responseException.getMessage(), containsString("repository is empty, cannot verify its integrity"));
} finally {
deleteRepository(repositoryName);
}
}

private void runInconsistentShardGenerationBlobTest(
TestContext testContext,
UnaryOperator<BlobStoreIndexShardSnapshots> shardGenerationUpdater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void writeResponseChunk(RepositoryVerifyIntegrityResponseChunk responseCh

.<RepositoryData>newForked(l -> repository.getRepositoryData(executor, l))
.andThenApply(repositoryData -> {
ensureValidGenId(repositoryData.getGenId());
final var cancellableThreads = new CancellableThreads();
task.addListener(() -> cancellableThreads.cancel("task cancelled"));
final var verifier = new RepositoryIntegrityVerifier(
Expand All @@ -155,4 +156,17 @@ public void writeResponseChunk(RepositoryVerifyIntegrityResponseChunk responseCh
.<RepositoryVerifyIntegrityResponse>andThen((l, repositoryIntegrityVerifier) -> repositoryIntegrityVerifier.start(l))
.addListener(listener);
}

static void ensureValidGenId(long repositoryGenId) {
if (repositoryGenId == RepositoryData.EMPTY_REPO_GEN) {
throw new IllegalArgumentException("repository is empty, cannot verify its integrity");
}
if (repositoryGenId < 0) {
final var exception = new IllegalStateException(
"repository is in an unexpected state [" + repositoryGenId + "], cannot verify its integrity"
);
assert false : exception; // cannot be unknown, and if corrupt we throw a corruptedStateException from getRepositoryData
throw exception;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.repositories.blobstore.testkit.integrity;

import org.elasticsearch.repositories.RepositoryData;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.equalTo;

public class TransportRepositoryVerifyIntegrityActionTests extends ESTestCase {
public void testEnsureValidGenId() {
TransportRepositoryVerifyIntegrityAction.ensureValidGenId(0);
TransportRepositoryVerifyIntegrityAction.ensureValidGenId(randomNonNegativeLong());
assertThat(
expectThrows(
IllegalArgumentException.class,
() -> TransportRepositoryVerifyIntegrityAction.ensureValidGenId(RepositoryData.EMPTY_REPO_GEN)
).getMessage(),
equalTo("repository is empty, cannot verify its integrity")
);
assertThat(expectThrows(IllegalStateException.class, () -> {
try {
TransportRepositoryVerifyIntegrityAction.ensureValidGenId(RepositoryData.CORRUPTED_REPO_GEN);
} catch (AssertionError e) {
// if assertions disabled, we throw the cause directly
throw e.getCause();
}
}).getMessage(), equalTo("repository is in an unexpected state [-3], cannot verify its integrity"));
}
}
Loading