chore(redshift): move the remaining Redshift schedulers onto SweepWorker (#6988)

* chore(redshift): move the remaining Redshift schedulers onto SweepWorker

* chore(redshift): share the shutdown priority check across the scheduler tests
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-30 06:55:10 -04:00
1 parent 83ed905253
commit 485ec4e39f
6 files changed
+77 -192

No files matched your search

@@ -2,78 +2,46 @@ package io.github.hectorvent.floci.services.redshift;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.SweepWorker;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/** Drives automated snapshots outside AWS request threads. */
@ApplicationScoped
public class RedshiftSnapshotScheduler implements Resettable {
private static final Logger LOG = Logger.getLogger(RedshiftSnapshotScheduler.class);
private final RedshiftService service;
private final ScheduledExecutorService executor;
private final long intervalSeconds;
private final ReentrantLock sweepLock = new ReentrantLock();
private volatile boolean paused;
private final SweepWorker worker;
@Inject
public RedshiftSnapshotScheduler(RedshiftService service, EmulatorConfig config) {
this.service = service;
this.intervalSeconds = Math.max(1, config.services().redshift().snapshotSweepIntervalSeconds());
this.executor = config.services().redshift().enabled() && config.services().redshift().automatedSnapshotsEnabled()
? Executors.newSingleThreadScheduledExecutor(task -> {
Thread thread = new Thread(task, "redshift-snapshot-scheduler");
thread.setDaemon(true);
return thread;
}) : null;
EmulatorConfig.RedshiftServiceConfig redshift = config.services().redshift();
this.worker = new SweepWorker("redshift-snapshot-scheduler",
redshift.enabled() && redshift.automatedSnapshotsEnabled(),
redshift.snapshotSweepIntervalSeconds(), false, service::runSnapshotSchedules);
}
void onStart(@Observes StartupEvent ignored) {
if (executor != null) {
executor.scheduleWithFixedDelay(this::tickSafely, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
}
worker.start();
}
void onStop(@Observes ShutdownEvent ignored) {
if (executor != null) {
executor.shutdownNow();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
LOG.warn("Redshift snapshot scheduler is still finishing a dump during shutdown");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.debugv(e, "Interrupted while stopping the Redshift snapshot scheduler");
}
}
// Priority below APPLICATION (2000) so the worker drains before EmulatorLifecycle.onStop flushes storage.
void onStop(@Observes @Priority(1000) ShutdownEvent ignored) {
worker.stop();
}
public void tick(Instant now) {
sweepLock.lock();
try {
if (!paused) {
service.runSnapshotSchedules(now);
}
} finally {
sweepLock.unlock();
}
worker.tick(now);
}
/** Drains a running sweep so it cannot write old snapshots back into storage that reset is about to clear. */
@Override
public void beforeReset() {
paused = true;
sweepLock.lock();
sweepLock.unlock();
worker.pause();
}
@Override
@@ -83,14 +51,6 @@ public class RedshiftSnapshotScheduler implements Resettable {
@Override
public void afterReset() {
paused = false;
}
private void tickSafely() {
try {
tick(Instant.now());
} catch (RuntimeException e) {
LOG.warnv(e, "Redshift automated snapshot sweep failed");
}
worker.resume();
}
}
@@ -2,85 +2,52 @@ package io.github.hectorvent.floci.services.redshiftserverless;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.SweepWorker;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/** Drives Serverless scheduled actions outside AWS request threads, one sweep at a time. */
@ApplicationScoped
public class RedshiftServerlessScheduledActionScheduler implements Resettable {
private static final Logger LOG = Logger.getLogger(RedshiftServerlessScheduledActionScheduler.class);
private final RedshiftServerlessScheduledActionService service;
private final ScheduledExecutorService executor;
private final long intervalSeconds;
private final ReentrantLock sweepLock = new ReentrantLock();
private volatile boolean paused;
private final SweepWorker worker;
@Inject
public RedshiftServerlessScheduledActionScheduler(RedshiftServerlessScheduledActionService service,
EmulatorConfig config) {
EmulatorConfig.RedshiftServerlessServiceConfig serverless = config.services().redshiftServerless();
this.service = service;
this.intervalSeconds = Math.max(1, serverless.scheduledActionSweepIntervalSeconds());
this.executor = serverless.enabled() && serverless.scheduledActionsEnabled()
? Executors.newSingleThreadScheduledExecutor(task -> {
Thread thread = new Thread(task, "redshift-serverless-scheduled-actions");
thread.setDaemon(true);
return thread;
}) : null;
this.worker = new SweepWorker("redshift-serverless-scheduled-actions",
serverless.enabled() && serverless.scheduledActionsEnabled(),
serverless.scheduledActionSweepIntervalSeconds(), false, service::runScheduledActions);
}
void onStart(@Observes StartupEvent ignored) {
if (executor != null) {
executor.scheduleWithFixedDelay(this::tickSafely, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
}
worker.start();
}
void onStop(@Observes ShutdownEvent ignored) {
if (executor != null) {
executor.shutdownNow();
try {
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
LOG.warn("The Redshift Serverless scheduled action worker is still finishing a snapshot during shutdown");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.debugv(e, "Interrupted while stopping the Redshift Serverless scheduled action worker");
}
}
// Priority below APPLICATION (2000) so the worker drains before EmulatorLifecycle.onStop flushes storage.
void onStop(@Observes @Priority(1000) ShutdownEvent ignored) {
worker.stop();
}
/** Whether a background worker exists, which the two configuration switches decide. */
boolean active() {
return executor != null;
return worker.active();
}
public void tick(Instant now) {
sweepLock.lock();
try {
if (!paused) {
service.runScheduledActions(now);
}
} finally {
sweepLock.unlock();
}
worker.tick(now);
}
/** Drains a running sweep so it cannot write a claim back into storage that reset is about to clear. */
@Override
public void beforeReset() {
paused = true;
sweepLock.lock();
sweepLock.unlock();
worker.pause();
}
@Override
@@ -90,14 +57,6 @@ public class RedshiftServerlessScheduledActionScheduler implements Resettable {
@Override
public void afterReset() {
paused = false;
}
private void tickSafely() {
try {
tick(Instant.now());
} catch (RuntimeException e) {
LOG.warnv(e, "The Redshift Serverless scheduled action sweep failed");
}
worker.resume();
}
}