mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 15:49:33 -05:00
feat(rds-data): opt into retryable database resume errors (#5703)
* feat(rds-data): opt into retryable database resume errors * fix(rds): keep lease release safe after timer shutdown * refactor(rds): share pause cancellation between acquisition modes * docs(rds-data): list resume errors in configuration references
This commit is contained in:
1 parent
ac83817802
commit
fb6adc00d8
14 files changed
+408
-17
No files matched your search
@@ -1712,6 +1712,9 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("180")
|
||||
long transactionTtlSeconds();
|
||||
|
||||
@WithDefault("false")
|
||||
boolean databaseResumingExceptionEnabled();
|
||||
}
|
||||
|
||||
interface RedshiftDataServiceConfig {
|
||||
|
||||
+36
-8
@@ -81,6 +81,7 @@ final class AutoPauseController {
|
||||
// was cancelled or belongs to an earlier state, and does nothing.
|
||||
private Future<?> timer;
|
||||
private long timerToken;
|
||||
private boolean resumeRequested;
|
||||
private boolean pauseCanceled;
|
||||
private boolean closed;
|
||||
private Instant lastSignal = Instant.EPOCH;
|
||||
@@ -163,6 +164,10 @@ final class AutoPauseController {
|
||||
* @throws IllegalStateException when Docker cannot resume the container, which stays paused
|
||||
*/
|
||||
RdsBackendGate.Lease acquire() throws InterruptedException {
|
||||
return acquire(true);
|
||||
}
|
||||
|
||||
RdsBackendGate.Lease acquire(boolean waitForResume) throws InterruptedException {
|
||||
lock.lock();
|
||||
try {
|
||||
while (true) {
|
||||
@@ -174,12 +179,19 @@ final class AutoPauseController {
|
||||
cancelTimer();
|
||||
return new ControllerLease();
|
||||
}
|
||||
if (state == State.PAUSING) {
|
||||
pauseCanceled = true;
|
||||
}
|
||||
if (!waitForResume) {
|
||||
if (state == State.PAUSED && !resumeRequested) {
|
||||
startTimer(this::resumeInBackground, Duration.ZERO);
|
||||
resumeRequested = true;
|
||||
}
|
||||
throw new RdsBackendGate.ResumingException();
|
||||
}
|
||||
if (state == State.PAUSED) {
|
||||
resume();
|
||||
} else {
|
||||
if (state == State.PAUSING) {
|
||||
pauseCanceled = true;
|
||||
}
|
||||
settled.await();
|
||||
}
|
||||
}
|
||||
@@ -312,8 +324,8 @@ final class AutoPauseController {
|
||||
if (thawFailure != null) {
|
||||
LOG.warnv(thawFailure, "Could not undo the auto-pause of RDS container {0}", containerId);
|
||||
state = State.PAUSED;
|
||||
armPausedReport();
|
||||
settled.signalAll();
|
||||
armPausedReport();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -334,15 +346,21 @@ final class AutoPauseController {
|
||||
RuntimeException failure = withoutLock(() -> freezer.unpause(containerId));
|
||||
if (failure != null) {
|
||||
state = State.PAUSED;
|
||||
armPausedReport();
|
||||
settled.signalAll();
|
||||
armPausedReport();
|
||||
throw new IllegalStateException(
|
||||
"Could not resume auto-paused RDS container " + containerId, failure);
|
||||
}
|
||||
if (closed || resumeDelay.isZero()) {
|
||||
finishResume();
|
||||
} else {
|
||||
startTimer(this::resumeDelayElapsed, resumeDelay);
|
||||
try {
|
||||
startTimer(this::resumeDelayElapsed, resumeDelay);
|
||||
} catch (IllegalStateException e) {
|
||||
// Docker has thawed the backend; a stopped timer must not strand its waiters.
|
||||
finishResume();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,12 +401,12 @@ final class AutoPauseController {
|
||||
if (closed || state != State.RUNNING || leases > 0 || secondsUntilAutoPause == null) {
|
||||
return;
|
||||
}
|
||||
startTimer(this::idleTimerFired, Duration.ofSeconds(secondsUntilAutoPause));
|
||||
startBestEffortTimer(this::idleTimerFired, Duration.ofSeconds(secondsUntilAutoPause));
|
||||
}
|
||||
|
||||
private void armPausedReport() {
|
||||
if (!closed) {
|
||||
startTimer(this::pausedReportDue, PAUSED_REPORT_INTERVAL);
|
||||
startBestEffortTimer(this::pausedReportDue, PAUSED_REPORT_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,6 +422,15 @@ final class AutoPauseController {
|
||||
}
|
||||
}
|
||||
|
||||
private void startBestEffortTimer(LongConsumer task, Duration delay) {
|
||||
try {
|
||||
startTimer(task, delay);
|
||||
} catch (IllegalStateException e) {
|
||||
// Shutdown must not turn a completed request's lease release into a failure.
|
||||
LOG.debugv(e, "Could not schedule best-effort RDS timer for {0}", containerId);
|
||||
}
|
||||
}
|
||||
|
||||
private void startTimer(LongConsumer task, Duration delay) {
|
||||
cancelTimer();
|
||||
long token = timerToken;
|
||||
@@ -411,6 +438,7 @@ final class AutoPauseController {
|
||||
}
|
||||
|
||||
private void cancelTimer() {
|
||||
resumeRequested = false;
|
||||
if (timer != null) {
|
||||
timer.cancel(false);
|
||||
timer = null;
|
||||
|
||||
@@ -19,6 +19,19 @@ public interface RdsBackendGate {
|
||||
*/
|
||||
Lease enter(String host, int port) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Like {@link #enter(String, int)}, but with {@code waitForResume=false} starts resume in the
|
||||
* background and throws {@link ResumingException} if the backend is not yet ready.
|
||||
* Backends without auto-pause may use this default implementation.
|
||||
*/
|
||||
default Lease enter(String host, int port, boolean waitForResume) throws InterruptedException {
|
||||
return enter(host, port);
|
||||
}
|
||||
|
||||
/** The backend is being resumed; no lease was acquired and the caller must retry. */
|
||||
class ResumingException extends RuntimeException {
|
||||
}
|
||||
|
||||
/** Held for as long as a client uses the backend; closing it more than once is harmless. */
|
||||
interface Lease extends AutoCloseable {
|
||||
|
||||
|
||||
+7
-4
@@ -37,7 +37,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -486,12 +485,17 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port) throws InterruptedException {
|
||||
return enter(host, port, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port, boolean waitForResume) throws InterruptedException {
|
||||
if (host == null) {
|
||||
return Lease.NONE;
|
||||
}
|
||||
for (AutoPauseController controller : autoPauses.values()) {
|
||||
if (controller.serves(host, port)) {
|
||||
return controller.acquire();
|
||||
return controller.acquire(waitForResume);
|
||||
}
|
||||
}
|
||||
return Lease.NONE;
|
||||
@@ -577,8 +581,7 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
() -> Thread.ofVirtual().name("rds-auto-pause").start(task),
|
||||
delay.toMillis(), TimeUnit.MILLISECONDS);
|
||||
} catch (RejectedExecutionException e) {
|
||||
LOG.debugv("RDS auto-pause timer is shut down; not scheduling: {0}", e.getMessage());
|
||||
return CompletableFuture.completedFuture(null);
|
||||
throw new IllegalStateException("RDS auto-pause timer is shut down", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ public class RdsDataService implements Resettable {
|
||||
private final ConcurrentMap<String, TransactionContext> transactions = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService transactionCleanupExecutor;
|
||||
private final RdsBackendGate backendGate;
|
||||
private final boolean databaseResumingExceptionEnabled;
|
||||
|
||||
@Inject
|
||||
public RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
@@ -81,7 +82,8 @@ public class RdsDataService implements Resettable {
|
||||
EmulatorConfig config,
|
||||
RdsBackendGate backendGate) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory,
|
||||
Duration.ofSeconds(config.services().rdsData().transactionTtlSeconds()), backendGate);
|
||||
Duration.ofSeconds(config.services().rdsData().transactionTtlSeconds()), backendGate,
|
||||
config.services().rdsData().databaseResumingExceptionEnabled());
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
@@ -99,12 +101,23 @@ public class RdsDataService implements Resettable {
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl, backendGate, false);
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
SecretsManagerService secretsManagerService,
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate,
|
||||
boolean databaseResumingExceptionEnabled) {
|
||||
this.resourceResolver = resourceResolver;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.transactionTtl = transactionTtl;
|
||||
this.backendGate = backendGate;
|
||||
this.databaseResumingExceptionEnabled = databaseResumingExceptionEnabled;
|
||||
this.transactionCleanupExecutor = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread thread = new Thread(r, "rds-data-transaction-cleanup");
|
||||
thread.setDaemon(true);
|
||||
@@ -298,12 +311,15 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resumes an auto-paused Aurora cluster before the Data API uses its database, as Aurora
|
||||
* does, and keeps it from pausing again until the returned lease is closed.
|
||||
* Keeps a ready backend awake. By default resume is awaited; the opt-in AWS error mode
|
||||
* initiates resume and asks the caller to retry instead, without opening a connection.
|
||||
*/
|
||||
private RdsBackendGate.Lease enterBackend(RdsDataResourceResolver.DatabaseTarget target) {
|
||||
try {
|
||||
return backendGate.enter(target.host(), target.port());
|
||||
return backendGate.enter(target.host(), target.port(), !databaseResumingExceptionEnabled);
|
||||
} catch (RdsBackendGate.ResumingException e) {
|
||||
throw new AwsException("DatabaseResumingException",
|
||||
"The Aurora Serverless v2 DB instance is resuming. Wait a few seconds and try again.", 400);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new AwsException("InternalServerErrorException",
|
||||
|
||||
Reference in new issue
Block a user