mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(scheduler): keep a schedule's last fire across a restart (#8928)
With persistence on, schedules survived a restart but the dispatcher's record of when each last fired lived in memory, so a one-time at() schedule fired again and every rate and cron schedule fired an immediate catch-up. The last fire is now stored with the schedule and never returned by the API. The dispatcher records it under the same lock as UpdateSchedule, DeleteSchedule and DeleteScheduleGroup, skipping a schedule deleted or recreated meanwhile, and UpdateSchedule keeps it. A one-time schedule fires once its time is due unless it has fired since, so an update to a later time re-arms it; rate and cron schedules count from the stored fire.
This commit is contained in:
1 parent
fa965882ca
commit
6ea09ee520
6 files changed
+220
-26
No files matched your search
@@ -36,8 +36,9 @@ import java.util.concurrent.TimeUnit;
|
||||
*
|
||||
* A single background thread ticks on a fixed interval, scans all persisted
|
||||
* schedules, and invokes the target of any schedule whose next fire time has
|
||||
* passed. Per-schedule "last fire" state is kept in memory (by schedule ARN);
|
||||
* restarts reset it, matching the emulator's loose durability expectations.
|
||||
* passed. When a schedule last fired is stored with the schedule
|
||||
* ({@link Schedule#getLastFiredAt}), so with persistence on a restart neither fires a one-time
|
||||
* schedule again nor fires an immediate catch-up for a rate or cron schedule.
|
||||
*
|
||||
* Scope of the initial implementation:
|
||||
* <ul>
|
||||
@@ -76,8 +77,6 @@ public class ScheduleDispatcher implements Resettable {
|
||||
private final long tickIntervalSeconds;
|
||||
private final boolean enabled;
|
||||
private final ScheduledExecutorService executor;
|
||||
private final ConcurrentHashMap<String, Instant> lastFireByArn = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Boolean> firedOnceByArn = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<Occurrence, Delivery> pendingRetries = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
@@ -115,8 +114,6 @@ public class ScheduleDispatcher implements Resettable {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
lastFireByArn.clear();
|
||||
firedOnceByArn.clear();
|
||||
pendingRetries.clear();
|
||||
}
|
||||
|
||||
@@ -216,18 +213,16 @@ public class ScheduleDispatcher implements Resettable {
|
||||
private Instant computeNextFire(Schedule schedule, Kind kind, Instant now) {
|
||||
String expr = schedule.getScheduleExpression();
|
||||
String tz = schedule.getScheduleExpressionTimezone();
|
||||
String arn = schedule.getArn();
|
||||
Instant lastFire = schedule.getLastFiredAt();
|
||||
|
||||
return switch (kind) {
|
||||
case AT -> {
|
||||
if (firedOnceByArn.containsKey(arn)) {
|
||||
yield null;
|
||||
}
|
||||
yield SchedulerExpressionParser.parseAt(expr, tz);
|
||||
// Fired once its time came, unless it has fired since: an update to a later time re-arms it.
|
||||
Instant at = SchedulerExpressionParser.parseAt(expr, tz);
|
||||
yield lastFire != null && !lastFire.isBefore(at) ? null : at;
|
||||
}
|
||||
case RATE -> {
|
||||
long intervalMs = SchedulerExpressionParser.parseRateMillis(expr);
|
||||
Instant lastFire = lastFireByArn.get(arn);
|
||||
if (lastFire != null) {
|
||||
yield lastFire.plusMillis(intervalMs);
|
||||
}
|
||||
@@ -235,9 +230,8 @@ public class ScheduleDispatcher implements Resettable {
|
||||
: schedule.getCreationDate() != null ? schedule.getCreationDate() : now;
|
||||
}
|
||||
case CRON -> {
|
||||
Instant base = lastFireByArn.getOrDefault(arn, schedule.getCreationDate() != null
|
||||
? schedule.getCreationDate()
|
||||
: now.minusSeconds(1));
|
||||
Instant base = lastFire != null ? lastFire
|
||||
: schedule.getCreationDate() != null ? schedule.getCreationDate() : now.minusSeconds(1);
|
||||
yield SchedulerExpressionParser.nextCronFire(expr, base, tz);
|
||||
}
|
||||
};
|
||||
@@ -280,8 +274,6 @@ public class ScheduleDispatcher implements Resettable {
|
||||
try {
|
||||
schedulerService.deleteScheduleForAccount(
|
||||
schedule.getAccountId(), schedule.getName(), schedule.getGroupName(), regionOf(schedule));
|
||||
lastFireByArn.remove(schedule.getArn());
|
||||
firedOnceByArn.remove(schedule.getArn());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Post-completion delete failed for {0}: {1}", schedule.getArn(), e.getMessage());
|
||||
}
|
||||
@@ -363,8 +355,8 @@ public class ScheduleDispatcher implements Resettable {
|
||||
}
|
||||
|
||||
private void recordFire(Schedule schedule, Instant now) {
|
||||
lastFireByArn.put(schedule.getArn(), now);
|
||||
firedOnceByArn.put(schedule.getArn(), Boolean.TRUE);
|
||||
schedule.setLastFiredAt(now);
|
||||
schedulerService.recordFire(schedule, regionOf(schedule), now);
|
||||
}
|
||||
|
||||
private static boolean isDeleteAfterCompletion(Schedule schedule) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -178,7 +179,8 @@ public class SchedulerService implements StoredResourceArns {
|
||||
"ScheduleGroup not found: " + effectiveName, 404));
|
||||
}
|
||||
|
||||
public void deleteScheduleGroup(String name, String region) {
|
||||
/** Synchronized with {@link #recordFire}, so a fire recorded during the cascade cannot restore a schedule. */
|
||||
public synchronized void deleteScheduleGroup(String name, String region) {
|
||||
validateName(name);
|
||||
if (DEFAULT_GROUP.equals(name)) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -304,7 +306,7 @@ public class SchedulerService implements StoredResourceArns {
|
||||
"Schedule not found: " + name, 404));
|
||||
}
|
||||
|
||||
public Schedule updateSchedule(ScheduleRequest req, String region) {
|
||||
public synchronized Schedule updateSchedule(ScheduleRequest req, String region) {
|
||||
validateName(req.getName());
|
||||
validateScheduleRequest(req);
|
||||
String effectiveGroup = resolveAndValidateGroup(req.getGroupName());
|
||||
@@ -331,13 +333,14 @@ public class SchedulerService implements StoredResourceArns {
|
||||
updated.setKmsKeyArn(req.getKmsKeyArn());
|
||||
updated.setCreationDate(existing.getCreationDate());
|
||||
updated.setLastModificationDate(now);
|
||||
updated.setLastFiredAt(existing.getLastFiredAt());
|
||||
|
||||
scheduleStore.put(key, updated);
|
||||
LOG.infov("Updated schedule: {0} in group {1}", req.getName(), effectiveGroup);
|
||||
return updated;
|
||||
}
|
||||
|
||||
public void deleteSchedule(String name, String groupName, String region) {
|
||||
public synchronized void deleteSchedule(String name, String groupName, String region) {
|
||||
validateName(name);
|
||||
String effectiveGroup = resolveAndValidateGroup(groupName);
|
||||
String key = scheduleKey(region, effectiveGroup, name);
|
||||
@@ -360,7 +363,24 @@ public class SchedulerService implements StoredResourceArns {
|
||||
return scheduleStore.scan(k -> k.startsWith("schedule:"));
|
||||
}
|
||||
|
||||
public void deleteScheduleForAccount(String accountId, String name, String groupName, String region) {
|
||||
/**
|
||||
* Records in the stored schedule that the dispatcher fired it at {@code firedAt}. Synchronized
|
||||
* with update and delete, so it never writes back a stale copy over an update, and a schedule
|
||||
* deleted meanwhile, or deleted and created again under the same name, is left as it is.
|
||||
*/
|
||||
public synchronized void recordFire(Schedule schedule, String region, Instant firedAt) {
|
||||
String key = scheduleKey(region, schedule.getGroupName(), schedule.getName());
|
||||
AccountAwareStorageBackend.getInAccount(scheduleStore, schedule.getAccountId(), key)
|
||||
.filter(stored -> Objects.equals(stored.getArn(), schedule.getArn())
|
||||
&& Objects.equals(stored.getCreationDate(), schedule.getCreationDate()))
|
||||
.ifPresent(stored -> {
|
||||
stored.setLastFiredAt(firedAt);
|
||||
AccountAwareStorageBackend.putInAccount(scheduleStore, schedule.getAccountId(), key, stored);
|
||||
});
|
||||
}
|
||||
|
||||
public synchronized void deleteScheduleForAccount(String accountId, String name, String groupName,
|
||||
String region) {
|
||||
String effectiveGroup = resolveAndValidateGroup(groupName);
|
||||
String key = scheduleKey(region, effectiveGroup, name);
|
||||
if (scheduleStore instanceof AccountAwareStorageBackend<Schedule> aware) {
|
||||
|
||||
@@ -23,6 +23,12 @@ public class Schedule {
|
||||
private String kmsKeyArn;
|
||||
private Instant creationDate;
|
||||
private Instant lastModificationDate;
|
||||
/**
|
||||
* When the dispatcher last fired the schedule, Floci's own record and never part of an API
|
||||
* response. Stored with the schedule so a restart neither fires a one-time schedule again nor
|
||||
* catches a recurring one up.
|
||||
*/
|
||||
private Instant lastFiredAt;
|
||||
|
||||
public Schedule() {}
|
||||
|
||||
@@ -73,4 +79,7 @@ public class Schedule {
|
||||
|
||||
public Instant getLastModificationDate() { return lastModificationDate; }
|
||||
public void setLastModificationDate(Instant lastModificationDate) { this.lastModificationDate = lastModificationDate; }
|
||||
|
||||
public Instant getLastFiredAt() { return lastFiredAt; }
|
||||
public void setLastFiredAt(Instant lastFiredAt) { this.lastFiredAt = lastFiredAt; }
|
||||
}
|
||||
Reference in new issue
Block a user