Skip to content

Commit b35d3f0

Browse files
Fix Issue with Concurrent Snapshot Init + Delete (#38518)
* Fix Issue with Concurrent Snapshot Init + Delete by ensuring that we're not finalizing a snapshot in the repository while it is initializing on another thread * Closes #38489
1 parent c921a87 commit b35d3f0

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ public void onFailure(final Exception e) {
331331
public TimeValue timeout() {
332332
return request.masterNodeTimeout();
333333
}
334-
335334
});
336335
}
337336

@@ -394,6 +393,8 @@ private void beginSnapshot(final ClusterState clusterState,
394393

395394
boolean snapshotCreated;
396395

396+
boolean hadAbortedInitializations;
397+
397398
@Override
398399
protected void doRun() {
399400
assert initializingSnapshots.contains(snapshot.snapshot());
@@ -433,6 +434,8 @@ public ClusterState execute(ClusterState currentState) {
433434

434435
if (entry.state() == State.ABORTED) {
435436
entries.add(entry);
437+
assert entry.shards().isEmpty();
438+
hadAbortedInitializations = true;
436439
} else {
437440
// Replace the snapshot that was just initialized
438441
ImmutableOpenMap<ShardId, ShardSnapshotStatus> shards =
@@ -491,6 +494,14 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
491494
// completion listener in this method. For the snapshot completion to work properly, the snapshot
492495
// should still exist when listener is registered.
493496
userCreateSnapshotListener.onResponse(snapshot.snapshot());
497+
498+
if (hadAbortedInitializations) {
499+
final SnapshotsInProgress snapshotsInProgress = newState.custom(SnapshotsInProgress.TYPE);
500+
assert snapshotsInProgress != null;
501+
final SnapshotsInProgress.Entry entry = snapshotsInProgress.snapshot(snapshot.snapshot());
502+
assert entry != null;
503+
endSnapshot(entry);
504+
}
494505
}
495506
});
496507
}
@@ -701,8 +712,8 @@ public void applyClusterState(ClusterChangedEvent event) {
701712
// 3. Snapshots in any other state that have all their shard tasks completed
702713
snapshotsInProgress.entries().stream().filter(
703714
entry -> entry.state().completed()
704-
|| entry.state() == State.INIT && initializingSnapshots.contains(entry.snapshot()) == false
705-
|| entry.state() != State.INIT && completed(entry.shards().values())
715+
|| initializingSnapshots.contains(entry.snapshot()) == false
716+
&& (entry.state() == State.INIT || completed(entry.shards().values()))
706717
).forEach(this::endSnapshot);
707718
}
708719
if (newMaster) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,6 @@ public void testMasterShutdownDuringSnapshot() throws Exception {
855855
assertEquals(0, snapshotInfo.failedShards());
856856
}
857857

858-
859858
public void testMasterAndDataShutdownDuringSnapshot() throws Exception {
860859
logger.info("--> starting three master nodes and two data nodes");
861860
internalCluster().startMasterOnlyNodes(3);

0 commit comments

Comments
 (0)