fix(schedule): decide and prune in one step during reconciliation
The previous fix read upsert times once and pruned later, so monitors created in between were still dropped. A Lua script now checks the upsert time and removes the entry atomically. 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
e6c3deb54d
commit
20664dd5dd
1 file changed
+28
-13
@@ -130,22 +130,17 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor, asOf time.Tim
|
|||||||
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()
|
asOfMS := asOf.UnixMilli()
|
||||||
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] && !recent(id) {
|
if wanted[id] {
|
||||||
s.rdb.HDel(ctx, keyIntervals, id)
|
continue
|
||||||
s.rdb.HDel(ctx, keyTouched, id)
|
}
|
||||||
|
if err := pruneScript.Run(ctx, s.rdb, []string{keyIntervals, keyTouched}, id, asOfMS).Err(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
regions, err := s.rdb.SMembers(ctx, keyRegions).Result()
|
regions, err := s.rdb.SMembers(ctx, keyRegions).Result()
|
||||||
@@ -158,14 +153,34 @@ func (s *Scheduler) Reconcile(ctx context.Context, want []Monitor, asOf time.Tim
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, id := range members {
|
for _, id := range members {
|
||||||
if !byRegion[r][id] && !recent(id) {
|
if byRegion[r][id] {
|
||||||
s.rdb.ZRem(ctx, keyDue(r), id)
|
continue
|
||||||
|
}
|
||||||
|
if err := pruneScript.Run(ctx, s.rdb, []string{keyDue(r), keyTouched}, id, asOfMS).Err(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pruneScript removes a monitor from a hash or sorted set unless it was
|
||||||
|
// upserted at or after asOf. Checking and removing in one step closes the
|
||||||
|
// window in which a concurrent upsert would be undone.
|
||||||
|
//
|
||||||
|
// KEYS: hash or sorted set to prune, touched hash. ARGV: monitor id, asOf ms.
|
||||||
|
var pruneScript = redis.NewScript(`
|
||||||
|
local touched = tonumber(redis.call('HGET', KEYS[2], ARGV[1]) or '0')
|
||||||
|
if touched >= tonumber(ARGV[2]) then return 0 end
|
||||||
|
if redis.call('TYPE', KEYS[1]).ok == 'zset' then
|
||||||
|
redis.call('ZREM', KEYS[1], ARGV[1])
|
||||||
|
else
|
||||||
|
redis.call('HDEL', KEYS[1], ARGV[1])
|
||||||
|
redis.call('HDEL', KEYS[2], ARGV[1])
|
||||||
|
end
|
||||||
|
return 1
|
||||||
|
`)
|
||||||
|
|
||||||
// Heartbeat records that instance of region is alive.
|
// Heartbeat records that instance of region is alive.
|
||||||
func (s *Scheduler) Heartbeat(ctx context.Context, region string, instance uuid.UUID) error {
|
func (s *Scheduler) Heartbeat(ctx context.Context, region string, instance uuid.UUID) error {
|
||||||
return s.rdb.ZAdd(ctx, keyInstances(region), redis.Z{Score: float64(s.now().UnixMilli()), Member: instance.String()}).Err()
|
return s.rdb.ZAdd(ctx, keyInstances(region), redis.Z{Score: float64(s.now().UnixMilli()), Member: instance.String()}).Err()
|
||||||
|
|||||||
Reference in new issue
Block a user