Skip to content

Commit bc1da88

Browse files
Allow Parallel Snapshot Restore And Delete (elastic#51608)
There is no reason not to allow deletes in parallel to restores if they're dealing with different snapshots. A delete will not remove any files related to the snapshot that is being restored if it is different from the deleted snapshot because those files will still be referenced by the restoring snapshot. Loading RepositoryData concurrently to modifying it is concurrency safe nowadays as well since the repo generation is tracked in the cluster state. Closes elastic#41463
1 parent 2e8a2c4 commit bc1da88

File tree

5 files changed

+95
-65
lines changed

5 files changed

+95
-65
lines changed

server/src/main/java/org/elasticsearch/snapshots/RestoreService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ public ClusterState execute(ClusterState currentState) {
234234
}
235235
// Check if the snapshot to restore is currently being deleted
236236
SnapshotDeletionsInProgress deletionsInProgress = currentState.custom(SnapshotDeletionsInProgress.TYPE);
237-
if (deletionsInProgress != null && deletionsInProgress.hasDeletionsInProgress()) {
237+
if (deletionsInProgress != null
238+
&& deletionsInProgress.getEntries().stream().anyMatch(entry -> entry.getSnapshot().equals(snapshot))) {
238239
throw new ConcurrentSnapshotExecutionException(snapshot,
239240
"cannot restore a snapshot while a snapshot deletion is in-progress [" +
240241
deletionsInProgress.getEntries().get(0).getSnapshot() + "]");

server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,12 @@ public ClusterState execute(ClusterState currentState) {
12931293
// don't allow snapshot deletions while a restore is taking place,
12941294
// otherwise we could end up deleting a snapshot that is being restored
12951295
// and the files the restore depends on would all be gone
1296-
if (restoreInProgress.isEmpty() == false) {
1297-
throw new ConcurrentSnapshotExecutionException(snapshot,
1298-
"cannot delete snapshot during a restore in progress in [" + restoreInProgress + "]");
1296+
1297+
for (RestoreInProgress.Entry entry : restoreInProgress) {
1298+
if (entry.snapshot().equals(snapshot)) {
1299+
throw new ConcurrentSnapshotExecutionException(snapshot,
1300+
"cannot delete snapshot during a restore in progress in [" + restoreInProgress + "]");
1301+
}
12991302
}
13001303
}
13011304
ClusterState.Builder clusterStateBuilder = ClusterState.builder(currentState);

server/src/test/java/org/elasticsearch/snapshots/MinThreadsSnapshotRestoreIT.java

-54
Original file line numberDiff line numberDiff line change
@@ -152,58 +152,4 @@ public void testSnapshottingWithInProgressDeletionNotAllowed() throws Exception
152152
client().admin().cluster().prepareCreateSnapshot(repo, snapshot2).setWaitForCompletion(true).get();
153153
assertEquals(1, client().admin().cluster().prepareGetSnapshots(repo).setSnapshots("_all").get().getSnapshots().size());
154154
}
155-
156-
public void testRestoreWithInProgressDeletionsNotAllowed() throws Exception {
157-
logger.info("--> creating repository");
158-
final String repo = "test-repo";
159-
assertAcked(client().admin().cluster().preparePutRepository(repo).setType("mock").setSettings(
160-
Settings.builder()
161-
.put("location", randomRepoPath())
162-
.put("random", randomAlphaOfLength(10))
163-
.put("wait_after_unblock", 200)).get());
164-
165-
logger.info("--> snapshot");
166-
final String index = "test-idx";
167-
assertAcked(prepareCreate(index, 1, Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0)));
168-
for (int i = 0; i < 10; i++) {
169-
index(index, "_doc", Integer.toString(i), "foo", "bar" + i);
170-
}
171-
refresh();
172-
final String snapshot1 = "test-snap1";
173-
client().admin().cluster().prepareCreateSnapshot(repo, snapshot1).setWaitForCompletion(true).get();
174-
final String index2 = "test-idx2";
175-
assertAcked(prepareCreate(index2, 1, Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0)));
176-
for (int i = 0; i < 10; i++) {
177-
index(index2, "_doc", Integer.toString(i), "foo", "bar" + i);
178-
}
179-
refresh();
180-
final String snapshot2 = "test-snap2";
181-
client().admin().cluster().prepareCreateSnapshot(repo, snapshot2).setWaitForCompletion(true).get();
182-
client().admin().indices().prepareClose(index, index2).get();
183-
184-
String blockedNode = internalCluster().getMasterName();
185-
((MockRepository)internalCluster().getInstance(RepositoriesService.class, blockedNode).repository(repo)).blockOnDataFiles(true);
186-
logger.info("--> start deletion of snapshot");
187-
ActionFuture<AcknowledgedResponse> future = client().admin().cluster().prepareDeleteSnapshot(repo, snapshot2).execute();
188-
logger.info("--> waiting for block to kick in on node [{}]", blockedNode);
189-
waitForBlock(blockedNode, repo, TimeValue.timeValueSeconds(10));
190-
191-
logger.info("--> try restoring the other snapshot, should fail because the deletion is in progress");
192-
try {
193-
client().admin().cluster().prepareRestoreSnapshot(repo, snapshot1).setWaitForCompletion(true).get();
194-
fail("should not be able to restore a snapshot while another is being deleted");
195-
} catch (ConcurrentSnapshotExecutionException e) {
196-
assertThat(e.getMessage(), containsString("cannot restore a snapshot while a snapshot deletion is in-progress"));
197-
}
198-
199-
logger.info("--> unblocking blocked node [{}]", blockedNode);
200-
unblockNode(repo, blockedNode);
201-
202-
logger.info("--> wait until snapshot deletion is finished");
203-
assertAcked(future.actionGet());
204-
205-
logger.info("--> restoring snapshot, which should now work");
206-
client().admin().cluster().prepareRestoreSnapshot(repo, snapshot1).setWaitForCompletion(true).get();
207-
assertEquals(1, client().admin().cluster().prepareGetSnapshots(repo).setSnapshots("_all").get().getSnapshots().size());
208-
}
209155
}

server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

-7
Original file line numberDiff line numberDiff line change
@@ -2616,13 +2616,6 @@ public void testDeleteSnapshotWhileRestoringFails() throws Exception {
26162616
assertEquals(repoName, e.getRepositoryName());
26172617
assertEquals(snapshotName, e.getSnapshotName());
26182618
assertThat(e.getMessage(), containsString("cannot delete snapshot during a restore"));
2619-
2620-
logger.info("-- try deleting another snapshot while the restore is in progress (should throw an error)");
2621-
e = expectThrows(ConcurrentSnapshotExecutionException.class, () ->
2622-
client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotName2).get());
2623-
assertEquals(repoName, e.getRepositoryName());
2624-
assertEquals(snapshotName2, e.getSnapshotName());
2625-
assertThat(e.getMessage(), containsString("cannot delete snapshot during a restore"));
26262619
} finally {
26272620
// unblock even if the try block fails otherwise we will get bogus failures when we delete all indices in test teardown.
26282621
logger.info("--> unblocking all data nodes");

server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java

+87
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
import static org.elasticsearch.action.support.ActionTestUtils.assertNoFailureListener;
211211
import static org.elasticsearch.env.Environment.PATH_HOME_SETTING;
212212
import static org.elasticsearch.node.Node.NODE_NAME_SETTING;
213+
import static org.hamcrest.Matchers.contains;
213214
import static org.hamcrest.Matchers.containsInAnyOrder;
214215
import static org.hamcrest.Matchers.either;
215216
import static org.hamcrest.Matchers.empty;
@@ -520,6 +521,92 @@ public void testConcurrentSnapshotCreateAndDeleteOther() {
520521
}
521522
}
522523

524+
public void testConcurrentSnapshotRestoreAndDeleteOther() {
525+
setupTestCluster(randomFrom(1, 3, 5), randomIntBetween(2, 10));
526+
527+
String repoName = "repo";
528+
String snapshotName = "snapshot";
529+
final String index = "test";
530+
final int shards = randomIntBetween(1, 10);
531+
532+
TestClusterNodes.TestClusterNode masterNode =
533+
testClusterNodes.currentMaster(testClusterNodes.nodes.values().iterator().next().clusterService.state());
534+
535+
final StepListener<CreateSnapshotResponse> createSnapshotResponseStepListener = new StepListener<>();
536+
537+
final int documentsFirstSnapshot = randomIntBetween(0, 100);
538+
539+
continueOrDie(createRepoAndIndex(repoName, index, shards), createIndexResponse -> indexNDocuments(
540+
documentsFirstSnapshot, index, () -> client().admin().cluster()
541+
.prepareCreateSnapshot(repoName, snapshotName).setWaitForCompletion(true).execute(createSnapshotResponseStepListener)));
542+
543+
final int documentsSecondSnapshot = randomIntBetween(0, 100);
544+
545+
final StepListener<CreateSnapshotResponse> createOtherSnapshotResponseStepListener = new StepListener<>();
546+
547+
final String secondSnapshotName = "snapshot-2";
548+
continueOrDie(createSnapshotResponseStepListener, createSnapshotResponse -> indexNDocuments(
549+
documentsSecondSnapshot, index, () -> client().admin().cluster().prepareCreateSnapshot(repoName, secondSnapshotName)
550+
.setWaitForCompletion(true).execute(createOtherSnapshotResponseStepListener)));
551+
552+
final StepListener<AcknowledgedResponse> deleteSnapshotStepListener = new StepListener<>();
553+
final StepListener<RestoreSnapshotResponse> restoreSnapshotResponseListener = new StepListener<>();
554+
555+
continueOrDie(createOtherSnapshotResponseStepListener,
556+
createSnapshotResponse -> {
557+
scheduleNow(
558+
() -> client().admin().cluster().prepareDeleteSnapshot(repoName, snapshotName).execute(deleteSnapshotStepListener));
559+
scheduleNow(() -> client().admin().cluster().restoreSnapshot(
560+
new RestoreSnapshotRequest(repoName, secondSnapshotName).waitForCompletion(true)
561+
.renamePattern("(.+)").renameReplacement("restored_$1"),
562+
restoreSnapshotResponseListener));
563+
});
564+
565+
final StepListener<SearchResponse> searchResponseListener = new StepListener<>();
566+
continueOrDie(restoreSnapshotResponseListener, restoreSnapshotResponse -> {
567+
assertEquals(shards, restoreSnapshotResponse.getRestoreInfo().totalShards());
568+
client().search(new SearchRequest("restored_" + index).source(new SearchSourceBuilder().size(0).trackTotalHits(true)),
569+
searchResponseListener);
570+
});
571+
572+
deterministicTaskQueue.runAllRunnableTasks();
573+
574+
assertEquals(documentsFirstSnapshot + documentsSecondSnapshot,
575+
Objects.requireNonNull(searchResponseListener.result().getHits().getTotalHits()).value);
576+
assertThat(deleteSnapshotStepListener.result().isAcknowledged(), is(true));
577+
assertThat(restoreSnapshotResponseListener.result().getRestoreInfo().failedShards(), is(0));
578+
579+
final Repository repository = masterNode.repositoriesService.repository(repoName);
580+
Collection<SnapshotId> snapshotIds = getRepositoryData(repository).getSnapshotIds();
581+
assertThat(snapshotIds, contains(createOtherSnapshotResponseStepListener.result().getSnapshotInfo().snapshotId()));
582+
583+
for (SnapshotId snapshotId : snapshotIds) {
584+
final SnapshotInfo snapshotInfo = repository.getSnapshotInfo(snapshotId);
585+
assertEquals(SnapshotState.SUCCESS, snapshotInfo.state());
586+
assertThat(snapshotInfo.indices(), containsInAnyOrder(index));
587+
assertEquals(shards, snapshotInfo.successfulShards());
588+
assertEquals(0, snapshotInfo.failedShards());
589+
}
590+
}
591+
592+
private void indexNDocuments(int documents, String index, Runnable afterIndexing) {
593+
if (documents == 0) {
594+
afterIndexing.run();
595+
return;
596+
}
597+
final BulkRequest bulkRequest = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
598+
for (int i = 0; i < documents; ++i) {
599+
bulkRequest.add(new IndexRequest(index).source(Collections.singletonMap("foo", "bar" + i)));
600+
}
601+
final StepListener<BulkResponse> bulkResponseStepListener = new StepListener<>();
602+
client().bulk(bulkRequest, bulkResponseStepListener);
603+
continueOrDie(bulkResponseStepListener, bulkResponse -> {
604+
assertFalse("Failures in bulk response: " + bulkResponse.buildFailureMessage(), bulkResponse.hasFailures());
605+
assertEquals(documents, bulkResponse.getItems().length);
606+
afterIndexing.run();
607+
});
608+
}
609+
523610
public void testConcurrentSnapshotDeleteAndDeleteIndex() throws IOException {
524611
setupTestCluster(randomFrom(1, 3, 5), randomIntBetween(2, 10));
525612

0 commit comments

Comments
 (0)