db forward round 1, fix 3 (Astra P2): the owner keeps the deadline it published in picked
serve ran a fresh OwnerDeadline from whenever the election answered, so a create that landed and answered late could run past picked.Deadline and past the console's deadline-plus-grace. It now runs to picked.Deadline on the agent's clock (context.WithDeadline(parent, p.Deadline) in production) and starts no work at all when that deadline has passed. Test first: TestServeKeepsThePublishedDeadline: a picked create that answers 300 ms late ran 320 ms past its deadline, and one that answered after its deadline still dialed a worker.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+80
−4
internal/dbreads/read.go+21 −4internal/dbreads/read_test.go+59 −0
diff --git a/internal/dbreads/read.go b/internal/dbreads/read.go| index 0f3af221b..8ba085390 100644 |
| --- a/internal/dbreads/read.go |
| +++ b/internal/dbreads/read.go |
| @@ -23,12 +23,29 @@ import ( |
| func (a *Agent) serve(parent context.Context, req Request, p Picked) { |
| defer a.reads.Done() |
| defer a.adm.finish(req.ID) |
| - ctx, cancel := context.WithTimeout(parent, a.cfg.OwnerDeadline) |
| + // The deadline is the one picked published (spec §5.7), measured on |
| + // this agent's clock, not a fresh OwnerDeadline from now: an election |
| + // whose answer came back late has already spent part of it, and the |
| + // console stops waiting a minute after the published one (Astra round |
| + // 1, finding 3). In production a.now is time.Now, so this is |
| + // context.WithDeadline(parent, p.Deadline). |
| + started := a.now() |
| + left := p.Deadline.Sub(started) |
| + ctx, cancel := context.WithTimeout(parent, left) |
| defer cancel() |
| var cancelled atomic.Bool |
| - go a.watchCancel(ctx, req.ID, cancel, &cancelled) |
| - started := a.now() |
| - res := a.read(ctx, req) |
| + var res Result |
| + if left <= 0 { |
| + // Past the deadline before any work: start nothing. |
| + a.logf("dbreads: %s: picked, but its deadline %s had passed when the election answered", req.ID, p.Deadline.Format(time.RFC3339Nano)) |
| + res = Result{State: StateFailed, Code: CodeInterrupted} |
| + if pub, err := ParseReplyKey(req.ReplyKey); err == nil { |
| + res = a.ended(pub, req.ID, StateFailed, CodeInterrupted, Summary{}, 0) |
| + } |
| + } else { |
| + go a.watchCancel(ctx, req.ID, cancel, &cancelled) |
| + res = a.read(ctx, req) |
| + } |
| if cancelled.Load() { |
| res.State, res.Code = StateCancelled, "" |
| } |
diff --git a/internal/dbreads/read_test.go b/internal/dbreads/read_test.go| index b478103d7..b754e860a 100644 |
| --- a/internal/dbreads/read_test.go |
| +++ b/internal/dbreads/read_test.go |
| @@ -508,3 +508,62 @@ func (v *vanishThenChange) OpenLTXFile(ctx context.Context, level int, lo, hi lt |
| } |
| return v.ReplicaReader.OpenLTXFile(ctx, level, lo, hi, off, size) |
| } |
| + |
| +// slowPicked lands the picked create and then answers late: an election |
| +// whose response takes delay (Astra round 1, finding 3). |
| +type slowPicked struct { |
| + release.Store |
| + delay time.Duration |
| +} |
| + |
| +func (s slowPicked) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + etag, err := s.Store.Put(ctx, key, body, ifMatch) |
| + if strings.HasSuffix(key, "/picked") { |
| + time.Sleep(s.delay) |
| + } |
| + return etag, err |
| +} |
| + |
| +// The owner works to the deadline it published in picked, not to a fresh |
| +// one from whenever the election's answer arrived (Astra round 1, finding |
| +// 3): a hung read ends by picked.Deadline, and a read whose election |
| +// answered after its deadline starts no work at all. |
| +func TestServeKeepsThePublishedDeadline(t *testing.T) { |
| + t.Parallel() |
| + for name, c := range map[string]struct { |
| + deadline, delay time.Duration |
| + dials int64 |
| + }{ |
| + "a late answer inside the deadline": {deadline: 600 * time.Millisecond, delay: 300 * time.Millisecond, dials: 1}, |
| + "an answer after the deadline": {deadline: 200 * time.Millisecond, delay: 300 * time.Millisecond, dials: 0}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + t.Parallel() |
| + fx := newFixture(t, 1) |
| + a, w := fx.agent("uk-1", func(cfg *AgentConfig, w *inProcWorkers) { |
| + cfg.Store = slowPicked{Store: fx.store, delay: c.delay} |
| + cfg.OwnerDeadline = c.deadline |
| + w.opt.Restore = func(ctx context.Context, _ litestream.ReplicaClient, _ string, _ uint64) error { |
| + <-ctx.Done() |
| + return ctx.Err() |
| + } |
| + }) |
| + req := fx.request("SELECT 1", "") |
| + _, _, r := serveOne(t, a, fx, req) |
| + raw, _, err := fx.store.Get(context.Background(), PickedKey(req.ID)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var p Picked |
| + if err := json.Unmarshal(raw, &p); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r.State != StateFailed || r.Code != CodeInterrupted || w.dials.Load() != c.dials { |
| + t.Fatalf("result %+v, %d worker connections", r, w.dials.Load()) |
| + } |
| + if over := r.Ended.Sub(p.Deadline); over > 150*time.Millisecond { |
| + t.Fatalf("the read ran %s past the deadline it published", over) |
| + } |
| + }) |
| + } |
| +} |