fix(schedule): keep monitors created while reconciliation reads the database
Reconciliation removed every monitor missing from its database read, including ones created in the meantime; they stayed unscheduled until the next run a minute later. Upserts now record when they happened and reconciliation only prunes entries older than its read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
a6126b353e
commit
4f57cafbdd
3 files changed
+54
-10
No files matched your search
@@ -330,9 +330,13 @@ func (s *Server) RunScheduler(ctx context.Context) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case <-dispatch.C:
|
case <-dispatch.C:
|
||||||
if _, err := s.sched.Dispatch(ctx); err != nil && ctx.Err() == nil {
|
n, err := s.sched.Dispatch(ctx)
|
||||||
|
if err != nil && ctx.Err() == nil {
|
||||||
s.log.WarnContext(ctx, "dispatch failed", "error", err)
|
s.log.WarnContext(ctx, "dispatch failed", "error", err)
|
||||||
}
|
}
|
||||||
|
if n > 0 {
|
||||||
|
s.log.DebugContext(ctx, "dispatched checks", "count", n)
|
||||||
|
}
|
||||||
case <-reconcile.C:
|
case <-reconcile.C:
|
||||||
s.reconcile(ctx)
|
s.reconcile(ctx)
|
||||||
}
|
}
|
||||||
@@ -345,6 +349,9 @@ func (s *Server) reconcile(ctx context.Context) {
|
|||||||
if err != nil || !ok {
|
if err != nil || !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Taken before reading the database: monitors upserted later survive.
|
||||||
|
start := time.Now()
|
||||||
|
defer func() { s.log.DebugContext(ctx, "schedule reconciled", "took", time.Since(start)) }()
|
||||||
var rows []store.ListEnabledMonitorsForScheduleRow
|
var rows []store.ListEnabledMonitorsForScheduleRow
|
||||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||||
var err error
|
var err error
|
||||||
@@ -356,7 +363,7 @@ func (s *Server) reconcile(ctx context.Context) {
|
|||||||
for i, r := range rows {
|
for i, r := range rows {
|
||||||
want[i] = schedule.Monitor{ID: r.ID, Interval: time.Duration(r.IntervalSeconds) * time.Second, Regions: r.Regions}
|
want[i] = schedule.Monitor{ID: r.ID, Interval: time.Duration(r.IntervalSeconds) * time.Second, Regions: r.Regions}
|
||||||
}
|
}
|
||||||
err = s.sched.Reconcile(ctx, want)
|
err = s.sched.Reconcile(ctx, want, start)
|
||||||
}
|
}
|
||||||
if err != nil && ctx.Err() == nil {
|
if err != nil && ctx.Err() == nil {
|
||||||
s.log.WarnContext(ctx, "schedule reconciliation failed", "error", err)
|
s.log.WarnContext(ctx, "schedule reconciliation failed", "error", err)
|
||||||
|
|||||||
@@ -33,7 +33,10 @@ const (
|
|||||||
const (
|
const (
|
||||||
keyRegions = "sched:regions"
|
keyRegions = "sched:regions"
|
||||||
keyIntervals = "sched:intervals"
|
keyIntervals = "sched:intervals"
|
||||||
jobsPrefix = "sched:jobs:"
|
// keyTouched records when each monitor was last upserted, so a
|
||||||
|
// reconciliation based on an older database read leaves it alone.
|
||||||
|
keyTouched = "sched:touched"
|
||||||
|
jobsPrefix = "sched:jobs:"
|
||||||
// jobsTTL drops queues of instances that vanished together with every
|
// jobsTTL drops queues of instances that vanished together with every
|
||||||
// API replica that could have recovered them.
|
// API replica that could have recovered them.
|
||||||
jobsTTL = 10 * time.Minute
|
jobsTTL = 10 * time.Minute
|
||||||
@@ -74,6 +77,7 @@ func (s *Scheduler) Upsert(ctx context.Context, m Monitor, previousRegions []str
|
|||||||
id := m.ID.String()
|
id := m.ID.String()
|
||||||
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
||||||
p.HSet(ctx, keyIntervals, id, int64(m.Interval/time.Millisecond))
|
p.HSet(ctx, keyIntervals, id, int64(m.Interval/time.Millisecond))
|
||||||
|
p.HSet(ctx, keyTouched, id, now)
|
||||||
keep := map[string]bool{}
|
keep := map[string]bool{}
|
||||||
for _, r := range m.Regions {
|
for _, r := range m.Regions {
|
||||||
keep[r] = true
|
keep[r] = true
|
||||||
@@ -95,6 +99,7 @@ func (s *Scheduler) Upsert(ctx context.Context, m Monitor, previousRegions []str
|
|||||||
func (s *Scheduler) Remove(ctx context.Context, id uuid.UUID, regions []string) error {
|
func (s *Scheduler) Remove(ctx context.Context, id uuid.UUID, regions []string) error {
|
||||||
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
||||||
p.HDel(ctx, keyIntervals, id.String())
|
p.HDel(ctx, keyIntervals, id.String())
|
||||||
|
p.HDel(ctx, keyTouched, id.String())
|
||||||
for _, r := range regions {
|
for _, r := range regions {
|
||||||
p.ZRem(ctx, keyDue(r), id.String())
|
p.ZRem(ctx, keyDue(r), id.String())
|
||||||
}
|
}
|
||||||
@@ -103,10 +108,12 @@ func (s *Scheduler) Remove(ctx context.Context, id uuid.UUID, regions []string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconcile makes Redis match want, the full set of enabled monitors. It
|
// Reconcile makes Redis match want, the full set of enabled monitors as read
|
||||||
// repairs drift after a Redis failover or a missed update and is safe to run
|
// from the database at asOf. It repairs drift after a Redis failover or a
|
||||||
// from several replicas.
|
// missed update and is safe to run from several replicas. Monitors upserted
|
||||||
func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
|
// after asOf are kept even if want lacks them: they were created while the
|
||||||
|
// database was being read.
|
||||||
|
func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor, asOf time.Time) error {
|
||||||
byRegion := map[string]map[string]bool{}
|
byRegion := map[string]map[string]bool{}
|
||||||
for _, m := range want {
|
for _, m := range want {
|
||||||
if err := s.Upsert(ctx, m, nil); err != nil {
|
if err := s.Upsert(ctx, m, nil); err != nil {
|
||||||
@@ -123,13 +130,22 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
|
|||||||
for _, m := range want {
|
for _, m := range want {
|
||||||
wanted[m.ID.String()] = true
|
wanted[m.ID.String()] = true
|
||||||
}
|
}
|
||||||
|
touched, err := s.rdb.HGetAll(ctx, keyTouched).Result()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
recent := func(id string) bool {
|
||||||
|
ms, err := strconv.ParseInt(touched[id], 10, 64)
|
||||||
|
return err == nil && ms >= asOf.UnixMilli()
|
||||||
|
}
|
||||||
ids, err := s.rdb.HKeys(ctx, keyIntervals).Result()
|
ids, err := s.rdb.HKeys(ctx, keyIntervals).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
if !wanted[id] {
|
if !wanted[id] && !recent(id) {
|
||||||
s.rdb.HDel(ctx, keyIntervals, id)
|
s.rdb.HDel(ctx, keyIntervals, id)
|
||||||
|
s.rdb.HDel(ctx, keyTouched, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
regions, err := s.rdb.SMembers(ctx, keyRegions).Result()
|
regions, err := s.rdb.SMembers(ctx, keyRegions).Result()
|
||||||
@@ -142,7 +158,7 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, id := range members {
|
for _, id := range members {
|
||||||
if !byRegion[r][id] {
|
if !byRegion[r][id] && !recent(id) {
|
||||||
s.rdb.ZRem(ctx, keyDue(r), id)
|
s.rdb.ZRem(ctx, keyDue(r), id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ func TestRemoveAndReconcile(t *testing.T) {
|
|||||||
_ = s.Upsert(ctx, keep, nil)
|
_ = s.Upsert(ctx, keep, nil)
|
||||||
_ = s.Upsert(ctx, gone, nil)
|
_ = s.Upsert(ctx, gone, nil)
|
||||||
|
|
||||||
if err := s.Reconcile(ctx, []Monitor{keep}); err != nil {
|
if err := s.Reconcile(ctx, []Monitor{keep}, s.now().Add(time.Millisecond)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
mustDispatch(t, s)
|
mustDispatch(t, s)
|
||||||
@@ -203,3 +203,24 @@ func TestTakeReturnsWhenCancelled(t *testing.T) {
|
|||||||
t.Errorf("Take() returned %v after cancellation, want within about a second", took)
|
t.Errorf("Take() returned %v after cancellation, want within about a second", took)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestReconcileKeepsMonitorsCreatedDuringRead covers a monitor created after
|
||||||
|
// reconciliation read the database but before it pruned Redis.
|
||||||
|
func TestReconcileKeepsMonitorsCreatedDuringRead(t *testing.T) {
|
||||||
|
s, now := newTest(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
in := uuid.New()
|
||||||
|
_ = s.Heartbeat(ctx, "eu", in)
|
||||||
|
asOf := *now
|
||||||
|
*now = now.Add(50 * time.Millisecond)
|
||||||
|
fresh := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
|
||||||
|
_ = s.Upsert(ctx, fresh, nil)
|
||||||
|
|
||||||
|
if err := s.Reconcile(ctx, nil, asOf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mustDispatch(t, s)
|
||||||
|
if got := takeAll(t, s, in); len(got) != 1 || got[0].MonitorID != fresh.ID {
|
||||||
|
t.Errorf("jobs after Reconcile = %+v, want the freshly created monitor", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user