mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docdb): retry a delete that a failed write left deleting (#9529)
DeleteDBCluster deferred the delete of any cluster that read maintenance or deleting, taking that for one the startup restore was starting again. An ordinary delete also marks the cluster deleting before it removes it, so one whose last storage write failed was left deleting, and every retry then deferred to a restore that was not running: the cluster stayed until the next start. A delete now defers only while the restore owns the cluster, which it does from once the cluster and its instances are marked until the restore ends, whatever way it ends: started, failed, the record gone, or a shutdown stopping it before or during the cluster's start. When the restore decides whether the cluster is available or removed, it releases it under the cluster's lock, so a delete either waits for that decision or comes after it. Ownership is taken only after the instances are marked, so a marking that fails leaves no delete waiting either. Follow-up to #9498.
This commit is contained in:
1 parent
c63897e171
commit
646a3a5671
2 files changed
+131
-5
No files matched your search
@@ -36,6 +36,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -83,6 +84,8 @@ public class DocDbService {
|
||||
* writes that can outlive a delete check the record under the key is still theirs.
|
||||
*/
|
||||
private final ConcurrentHashMap<String, Object> writeLocks = new ConcurrentHashMap<>();
|
||||
/** The clusters whose containers the startup restore is bringing back, by account and storage key. */
|
||||
private final Set<String> restoringClusters = ConcurrentHashMap.newKeySet();
|
||||
|
||||
/**
|
||||
* Storage key for a record: AWS scopes these identifiers per region, so the region is part of
|
||||
@@ -627,10 +630,8 @@ public class DocDbService {
|
||||
throw new AwsException("InvalidDBClusterStateFault",
|
||||
"Cannot delete DocDB cluster " + id + " — it still has DB instances.", 400);
|
||||
}
|
||||
if ("maintenance".equals(cluster.getStatus()) || "deleting".equals(cluster.getStatus())) {
|
||||
// Only the startup restore reports maintenance, while the cluster's container starts again, and a
|
||||
// stored cluster reads deleting only while a delete waits on that restore, which removes the cluster
|
||||
// once the container has started.
|
||||
if (restoringClusters.contains(regionResolver.getAccountId() + "/" + key(region, id))) {
|
||||
// Its container is starting again after a restart: the restore removes the cluster once it has.
|
||||
cluster.setStatus("deleting");
|
||||
clusters.put(key(region, id), cluster);
|
||||
LOG.infov("DocDB cluster {0} is deleted once its container has started", id);
|
||||
@@ -717,7 +718,9 @@ public class DocDbService {
|
||||
List<RuntimeException> failures = new ArrayList<>(prepareFailures);
|
||||
for (RestoringCluster restoring : pending) {
|
||||
if (containerManager.isShuttingDown()) {
|
||||
// Clusters not reached yet stay as they are, which the next start restores.
|
||||
// Clusters not reached yet stay as they are, which the next start restores; a delete meanwhile
|
||||
// removes one at once, as no container of it is starting.
|
||||
pending.forEach(left -> restoringClusters.remove(left.restoringKey()));
|
||||
break;
|
||||
}
|
||||
try {
|
||||
@@ -775,6 +778,9 @@ public class DocDbService {
|
||||
clusters.put(key, cluster);
|
||||
}
|
||||
updateInstances(region, candidate.id(), members, "starting", null, 0);
|
||||
// Owned by the restore only once nothing here can fail, so one that did leaves no delete waiting on a restore
|
||||
// that never runs. Nothing is started before the restore runs, so a delete before this removes the cluster.
|
||||
restoringClusters.add(restoring.restoringKey());
|
||||
return restoring;
|
||||
}
|
||||
|
||||
@@ -793,6 +799,7 @@ public class DocDbService {
|
||||
try {
|
||||
DocDbCluster cluster = restoreTarget(restoring);
|
||||
if (cluster == null) {
|
||||
restoringClusters.remove(restoring.restoringKey());
|
||||
return;
|
||||
}
|
||||
String password = cluster.getMasterPassword() != null ? cluster.getMasterPassword()
|
||||
@@ -819,6 +826,7 @@ public class DocDbService {
|
||||
}
|
||||
if (containerManager.isShuttingDown()) {
|
||||
// Left as it is, which the next start restores.
|
||||
restoringClusters.remove(restoring.restoringKey());
|
||||
LOG.debugv("DocDB cluster {0} not restored: {1}", restoring.id(), e.getMessage());
|
||||
return;
|
||||
}
|
||||
@@ -841,6 +849,8 @@ public class DocDbService {
|
||||
DocDbCluster cluster;
|
||||
List<String> members;
|
||||
synchronized (lockFor("cluster:" + key)) {
|
||||
// The restore ends here: from now on a delete removes the cluster itself.
|
||||
restoringClusters.remove(restoring.restoringKey());
|
||||
cluster = restoreTarget(restoring);
|
||||
if (cluster == null) {
|
||||
if (handle != null) {
|
||||
@@ -888,6 +898,7 @@ public class DocDbService {
|
||||
DocDbCluster cluster;
|
||||
List<String> members;
|
||||
synchronized (lockFor("cluster:" + key)) {
|
||||
restoringClusters.remove(restoring.restoringKey());
|
||||
cluster = restoreTarget(restoring);
|
||||
if (cluster == null) {
|
||||
return;
|
||||
@@ -950,6 +961,10 @@ public class DocDbService {
|
||||
*/
|
||||
private record RestoringCluster(String accountId, String region, String id, String resourceId,
|
||||
boolean deleting) {
|
||||
/** The key a cluster's restore owns it under, its account's and storage key. */
|
||||
String restoringKey() {
|
||||
return accountId + "/" + DocDbService.key(region, id);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Cluster snapshots ─────────────────────────────────────────────────────
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -61,6 +62,8 @@ class DocDbRestartTest {
|
||||
private volatile String requestRegion = "us-east-1";
|
||||
/** Fails every write of an available instance, once set. */
|
||||
private final AtomicBoolean refuseAvailableInstances = new AtomicBoolean();
|
||||
/** An instance and status, {@code id:status}, the instance store refuses to write once. */
|
||||
private volatile String refuseInstance;
|
||||
/** Runs once, inside the next read of instance {@code c-1}. */
|
||||
private volatile Runnable onInstanceRead;
|
||||
/** The cluster whose marking for the restore the cluster store refuses to write. */
|
||||
@@ -529,6 +532,110 @@ class DocDbRestartTest {
|
||||
assertFalse(service.hasCluster("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeleteLeftDeletingWithNoRestoreRunningIsRetriedAtOnce() {
|
||||
DocDbCluster cluster = service.createDbCluster("c", null, "admin", "secret", false);
|
||||
cluster.setStatus("deleting");
|
||||
|
||||
service.deleteDbCluster("c");
|
||||
|
||||
assertFalse(service.hasCluster("c"));
|
||||
verify(containers).removeStorage(cluster.getVolumeId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterWhoseInstancesCannotBeMarkedLeavesLaterDeletesToRemoveItAtOnce() {
|
||||
service.createDbCluster("c", null, "admin", "secret", false);
|
||||
service.createDbInstance("c-1", "c", null, null, false);
|
||||
refuseInstance = "c-1:starting";
|
||||
clearInvocations(containers);
|
||||
|
||||
assertThrows(CompletionException.class, () -> service.restorePersistedRuntime().join());
|
||||
service.deleteDbInstance("c-1");
|
||||
service.deleteDbCluster("c");
|
||||
|
||||
assertFalse(service.hasCluster("c"));
|
||||
verify(containers, never()).tryStart(anyString(), anyString(), anyString(), anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterGoneDuringItsRestoreAndCreatedAgainIsDeletedAtOnce() throws Exception {
|
||||
service.createDbCluster("c", null, "admin", "secret", false);
|
||||
CountDownLatch starting = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
blockStarts(starting, release);
|
||||
|
||||
CompletableFuture<Void> restore = service.restorePersistedRuntime();
|
||||
assertTrue(starting.await(10, TimeUnit.SECONDS));
|
||||
clusterStore.delete("us-east-1::c");
|
||||
release.countDown();
|
||||
restore.join();
|
||||
startsContainers();
|
||||
service.createDbCluster("c", null, "admin", "secret", false);
|
||||
service.deleteDbCluster("c");
|
||||
|
||||
assertFalse(service.hasCluster("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aShutdownDuringTheRestoreLeavesLaterDeletesToRemoveTheClustersAtOnce() {
|
||||
service.createDbCluster("first", null, "admin", "secret", false);
|
||||
service.createDbCluster("second", null, "admin", "secret", false);
|
||||
doAnswer(inv -> {
|
||||
shuttingDown.set(true);
|
||||
throw new IllegalStateException("DocumentDB is shutting down");
|
||||
}).when(containers).tryStart(anyString(), anyString(), anyString(), anyString(), anyString(), any());
|
||||
|
||||
service.restorePersistedRuntime().join();
|
||||
service.deleteDbCluster("first");
|
||||
service.deleteDbCluster("second");
|
||||
|
||||
assertFalse(service.hasCluster("first"));
|
||||
assertFalse(service.hasCluster("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterGoneBeforeItsStartAndCreatedAgainIsDeletedAtOnce() throws Exception {
|
||||
service.createDbCluster("a", null, "admin", "secret", false);
|
||||
service.createDbCluster("b", null, "admin", "secret", false);
|
||||
CountDownLatch starting = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
List<String> startedFirst = new CopyOnWriteArrayList<>();
|
||||
doAnswer(inv -> {
|
||||
startedFirst.add(inv.getArgument(0));
|
||||
starting.countDown();
|
||||
assertTrue(release.await(10, TimeUnit.SECONDS));
|
||||
int n = started.incrementAndGet();
|
||||
return new DocDbContainerHandle("container-" + n, inv.getArgument(0), "localhost", 40000 + n);
|
||||
}).when(containers).tryStart(anyString(), anyString(), anyString(), anyString(), anyString(), any());
|
||||
|
||||
CompletableFuture<Void> restore = service.restorePersistedRuntime();
|
||||
assertTrue(starting.await(10, TimeUnit.SECONDS));
|
||||
String other = "a".equals(startedFirst.getFirst()) ? "b" : "a";
|
||||
clusterStore.delete("us-east-1::" + other);
|
||||
release.countDown();
|
||||
restore.join();
|
||||
startsContainers();
|
||||
service.createDbCluster(other, null, "admin", "secret", false);
|
||||
service.deleteDbCluster(other);
|
||||
|
||||
assertFalse(service.hasCluster(other));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aShutdownDuringTheLastClustersStartLeavesALaterDeleteToRemoveItAtOnce() {
|
||||
service.createDbCluster("only", null, "admin", "secret", false);
|
||||
doAnswer(inv -> {
|
||||
shuttingDown.set(true);
|
||||
throw new IllegalStateException("DocumentDB is shutting down");
|
||||
}).when(containers).tryStart(anyString(), anyString(), anyString(), anyString(), anyString(), any());
|
||||
|
||||
service.restorePersistedRuntime().join();
|
||||
service.deleteDbCluster("only");
|
||||
|
||||
assertFalse(service.hasCluster("only"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeleteAfterTheRestoreRemovesTheClusterAtOnce() {
|
||||
service.createDbCluster("c", null, "admin", "secret", false);
|
||||
@@ -602,6 +709,10 @@ class DocDbRestartTest {
|
||||
private final class InstanceStore extends InMemoryStorage<String, DocDbInstance> {
|
||||
@Override
|
||||
public synchronized void put(String key, DocDbInstance value) {
|
||||
if ((value.getDbInstanceIdentifier() + ":" + value.getStatus()).equals(refuseInstance)) {
|
||||
refuseInstance = null;
|
||||
throw new IllegalStateException("the instance store refused the write");
|
||||
}
|
||||
if (refuseAvailableInstances.get() && "available".equals(value.getStatus())) {
|
||||
throw new IllegalStateException("the instance store refused the write");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user