| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 11 | ) |
| 12 | |
| 13 | type coordinatorRetryAPI struct { |
| 14 | contained int |
| 15 | *coordinatorAPIStub |
| 16 | operation string |
| 17 | failure error |
| 18 | calls int |
| 19 | queued bool |
| 20 | } |
| 21 | |
| 22 | func (a *coordinatorRetryAPI) CoordinatorCall(ctx context.Context, op string, q runnerproto.CoordinatorRequest, out any) error { |
| 23 | if op == "containment" { |
| 24 | a.contained++ |
| 25 | } |
| 26 | match := op == a.operation || a.operation == "delivery" && op == "event" && q.Event.Kind == "coordinator.delivery" |
| 27 | if match { |
| 28 | a.calls++ |
| 29 | if a.failure != nil { |
| 30 | return a.failure |
| 31 | } |
| 32 | } |
| 33 | if op == "recover" && a.queued { |
| 34 | return nil |
| 35 | } |
| 36 | return a.coordinatorAPIStub.CoordinatorCall(ctx, op, q, out) |
| 37 | } |
| 38 | func reopenCoordinator(t *testing.T, s *CoordinatorService, l *coordinatorFixtureLauncher) { |
| 39 | t.Helper() |
| 40 | path := s.Journal.path |
| 41 | mustJournal(t, s.Journal.Close()) |
| 42 | j, err := OpenJournal(context.Background(), path) |
| 43 | mustJournal(t, err) |
| 44 | t.Cleanup(func() { j.Close() }) |
| 45 | s.Journal, l.j = j, j |
| 46 | } |
| 47 | func TestCoordinatorRefusalMatrix(t *testing.T) { |
| 48 | kinds := []struct { |
| 49 | name string |
| 50 | err error |
| 51 | final bool |
| 52 | }{ |
| 53 | {"network", ErrTransient, false}, {"5xx", &RefusalError{Status: 503}, false}, |
| 54 | {"401", &RefusalError{Status: 401}, false}, {"403", &RefusalError{Status: 403}, false}, |
| 55 | {"409", &RefusalError{Status: 409}, false}, {"422", &RefusalError{Status: 422}, false}, |
| 56 | {"unknown_code", &RefusalError{Status: 403, Reason: "unknown"}, false}, |
| 57 | {"fenced", &RefusalError{Status: 409, Reason: "coordinator.fenced"}, true}, |
| 58 | {"reset", &RefusalError{Status: 403, Reason: "coordinator.reset"}, true}, |
| 59 | {"superseded", &RefusalError{Status: 422, Reason: "coordinator.superseded"}, true}, |
| 60 | {"revoked", &RefusalError{Status: 401, Reason: "coordinator.revoked"}, true}, |
| 61 | } |
| 62 | for _, k := range kinds { |
| 63 | for _, op := range []string{"claim", "recover", "containment", "delivery"} { |
| 64 | for _, phase := range []string{"queued", "claimed", "completed-unreported"} { |
| 65 | t.Run(k.name+"/"+op+"/"+phase, func(t *testing.T) { |
| 66 | ctx := context.Background() |
| 67 | s, base, l, c := coordinatorServiceFixture(t) |
| 68 | api := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: op, failure: k.err, queued: phase == "queued"} |
| 69 | s.API = api |
| 70 | // A lost claim response can hide any server state. Saved local claims must |
| 71 | // never return to claim/recover, and queued work has no report to send. |
| 72 | reachable := op == "recover" || op == "claim" && phase == "queued" || (op == "containment" && phase != "queued") || op == "delivery" && phase == "completed-unreported" |
| 73 | if phase == "completed-unreported" && op != "recover" { |
| 74 | setup := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: "delivery", failure: ErrTransient} |
| 75 | s.API = setup |
| 76 | _ = s.RunTurn(ctx, c) |
| 77 | s.API = api |
| 78 | } else if phase == "claimed" && op != "recover" { |
| 79 | mustJournal(t, s.Journal.saveState(ctx, "coordinator", c.Turn.ID, coordinatorState{Claim: c})) |
| 80 | } |
| 81 | if op == "recover" { |
| 82 | mustJournal(t, s.Journal.saveState(ctx, "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) |
| 83 | } |
| 84 | starts := l.starts |
| 85 | var err error |
| 86 | if op == "claim" { |
| 87 | err = s.Poll(ctx) |
| 88 | } else { |
| 89 | err = s.Recover(ctx) |
| 90 | } |
| 91 | if fatalServiceError(err) { |
| 92 | t.Fatal("remote refusal was fatal", err) |
| 93 | } |
| 94 | var states []coordinatorState |
| 95 | mustJournal(t, s.Journal.readStates(ctx, "coordinator", &states)) |
| 96 | if reachable && api.calls != 1 { |
| 97 | t.Fatalf("refusal not exercised: calls=%d", api.calls) |
| 98 | } |
| 99 | if !reachable && api.calls != 0 { |
| 100 | t.Fatal("invalid lifecycle call", api.calls) |
| 101 | } |
| 102 | if reachable { |
| 103 | final := len(states) == 1 && states[0].Finished && states[0].Refused |
| 104 | if final != k.final { |
| 105 | t.Fatalf("finalised=%v want=%v states=%d", final, k.final, len(states)) |
| 106 | } |
| 107 | } |
| 108 | if l.starts != starts { |
| 109 | t.Fatal("report retry resent prompt") |
| 110 | } |
| 111 | if !reachable { |
| 112 | return |
| 113 | } |
| 114 | // A new service with the reopened journal must retry only transient calls. |
| 115 | reopenCoordinator(t, s, l) |
| 116 | restarted := *s |
| 117 | s = &restarted |
| 118 | s.retry = nil |
| 119 | api.failure = nil |
| 120 | before := api.calls |
| 121 | if op == "claim" { |
| 122 | api.queued = true |
| 123 | } |
| 124 | mustJournal(t, s.Recover(ctx)) |
| 125 | if op == "claim" && !k.final { |
| 126 | base.polled = false |
| 127 | mustJournal(t, s.Poll(ctx)) |
| 128 | } |
| 129 | if k.final && api.calls != before { |
| 130 | t.Fatal("definitive refusal repeated") |
| 131 | } |
| 132 | if !k.final && api.calls != before+1 { |
| 133 | t.Fatal("transient refusal lost retry", api.calls, before) |
| 134 | } |
| 135 | if op != "claim" && l.starts != starts { |
| 136 | t.Fatal("restart resent provider prompt") |
| 137 | } |
| 138 | }) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestCoordinatorTransientRefusalKeepsServing(t *testing.T) { |
| 145 | for _, op := range []string{"recover", "containment", "delivery"} { |
| 146 | t.Run(op, func(t *testing.T) { |
| 147 | s, base, l, c := coordinatorServiceFixture(t) |
| 148 | api := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: op, failure: &RefusalError{Status: 403}} |
| 149 | if op == "recover" { |
| 150 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) |
| 151 | } else if op == "containment" { |
| 152 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator", c.Turn.ID, coordinatorState{Claim: c})) |
| 153 | } else { |
| 154 | s.API = api |
| 155 | _ = s.RunTurn(context.Background(), c) |
| 156 | } |
| 157 | s.API = api |
| 158 | before := l.starts |
| 159 | runSpecialistAfterCoordinator(t, s) |
| 160 | if l.starts != before { |
| 161 | t.Fatal("transient recovery restarted provider") |
| 162 | } |
| 163 | // Other offers must progress while the original report remains retryable. |
| 164 | next := c |
| 165 | next.Turn.ID = "other" |
| 166 | next.Scope.Job++ |
| 167 | next.Handle.Attempt++ |
| 168 | next.Admission.Dispatch.Action++ |
| 169 | next.Admission.Scope = next.Scope |
| 170 | next.Admission.Handle = next.Handle |
| 171 | base.c = next |
| 172 | base.polled = false |
| 173 | // Fail only the original report; the other coordinator can complete. |
| 174 | s.API = &selectiveCoordinatorRetryAPI{api: api, id: c.Turn.ID} |
| 175 | if err := s.Poll(context.Background()); err != nil && !errors.Is(err, ErrTransient) { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | if l.starts != before+1 { |
| 179 | t.Fatal("transient refusal blocked other coordinator", fmt.Sprint(l.starts)) |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | type selectiveCoordinatorRetryAPI struct { |
| 186 | api *coordinatorRetryAPI |
| 187 | id string |
| 188 | } |
| 189 | |
| 190 | func (a *selectiveCoordinatorRetryAPI) CoordinatorCall(ctx context.Context, op string, q runnerproto.CoordinatorRequest, out any) error { |
| 191 | if q.TurnID == a.id || q.Claim != nil && q.Claim.Turn.ID == a.id { |
| 192 | return a.api.CoordinatorCall(ctx, op, q, out) |
| 193 | } |
| 194 | return a.api.coordinatorAPIStub.CoordinatorCall(ctx, op, q, out) |
| 195 | } |
| 196 | |
| 197 | func TestCoordinatorRetryCadence(t *testing.T) { |
| 198 | s, base, _, c := coordinatorServiceFixture(t) |
| 199 | api := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: "recover", failure: &RefusalError{Status: 401}, queued: true} |
| 200 | s.API = api |
| 201 | now := time.Now() |
| 202 | s.now = func() time.Time { return now } |
| 203 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) |
| 204 | for attempt := 1; attempt <= 8; attempt++ { |
| 205 | mustJournal(t, s.Recover(context.Background())) |
| 206 | if api.calls != attempt { |
| 207 | t.Fatal("retry missing", api.calls, attempt) |
| 208 | } |
| 209 | delay := s.retry[c.Turn.ID].after.Sub(now) |
| 210 | if delay < time.Second || delay > leaseRenewInterval { |
| 211 | t.Fatal("unbounded retry delay", delay) |
| 212 | } |
| 213 | for poll := 0; poll < 3; poll++ { |
| 214 | mustJournal(t, s.Recover(context.Background())) |
| 215 | mustJournal(t, s.Poll(context.Background())) |
| 216 | } |
| 217 | if api.calls != attempt || base.polled && base.delivery { |
| 218 | t.Fatal("retry churn", api.calls) |
| 219 | } |
| 220 | now = now.Add(delay) |
| 221 | } |
| 222 | reopenCoordinator(t, s, &coordinatorFixtureLauncher{}) |
| 223 | s.retry = nil |
| 224 | mustJournal(t, s.Recover(context.Background())) |
| 225 | if api.calls != 9 { |
| 226 | t.Fatal("restart did not retry") |
| 227 | } |
| 228 | } |
| 229 | func TestCoordinatorLegacyRefusalRetries(t *testing.T) { |
| 230 | for _, op := range []string{"recover", "containment", "delivery"} { |
| 231 | t.Run(op, func(t *testing.T) { |
| 232 | s, base, l, c := coordinatorServiceFixture(t) |
| 233 | state := coordinatorState{Claim: c, Finished: true, Refused: true, RefusalOperation: op, RefusalStatus: 403} |
| 234 | if op == "recover" { |
| 235 | state.Claim = runnerproto.CoordinatorClaim{Turn: c.Turn} |
| 236 | } |
| 237 | api := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: op, queued: true} |
| 238 | s.API = api |
| 239 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator", c.Turn.ID, state)) |
| 240 | reopenCoordinator(t, s, l) |
| 241 | mustJournal(t, s.Recover(context.Background())) |
| 242 | if op != "delivery" && api.calls != 1 { |
| 243 | t.Fatal("legacy refusal not retried", api.calls) |
| 244 | } |
| 245 | if l.starts != 0 { |
| 246 | t.Fatal("legacy refusal resent prompt") |
| 247 | } |
| 248 | if op == "recover" { |
| 249 | base.polled = false |
| 250 | mustJournal(t, s.Poll(context.Background())) |
| 251 | if l.starts != 1 { |
| 252 | t.Fatal("queued legacy refusal wedged claim") |
| 253 | } |
| 254 | } |
| 255 | }) |
| 256 | } |
| 257 | } |
| 258 | func TestCoordinatorExpiredDeliveryStillReportsContainment(t *testing.T) { |
| 259 | s, base, _, c := coordinatorServiceFixture(t) |
| 260 | api := &coordinatorRetryAPI{coordinatorAPIStub: base, operation: "delivery", failure: &RefusalError{Status: 403}} |
| 261 | s.API = api |
| 262 | if err := s.RunTurn(context.Background(), c); !errors.Is(err, ErrTransient) { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | if api.contained != 0 { |
| 266 | t.Fatal("transient delivery lost authority before expiry") |
| 267 | } |
| 268 | s.now = func() time.Time { return c.AttemptDeadline.Add(time.Second) } |
| 269 | mustJournal(t, s.Recover(context.Background())) |
| 270 | var states []coordinatorState |
| 271 | mustJournal(t, s.Journal.readStates(context.Background(), "coordinator", &states)) |
| 272 | if states[0].Finished { |
| 273 | t.Fatal("transient delivery finalised at expiry") |
| 274 | } |
| 275 | if states[0].Containment == nil || api.contained != 1 { |
| 276 | t.Fatal("expired delivery lost containment retry", api.contained) |
| 277 | } |
| 278 | // The instance can now respond with a committed fence; only that permits retirement. |
| 279 | api.failure = &RefusalError{Status: 409, Reason: "coordinator.fenced"} |
| 280 | s.retry = nil |
| 281 | mustJournal(t, s.Recover(context.Background())) |
| 282 | states = nil |
| 283 | mustJournal(t, s.Journal.readStates(context.Background(), "coordinator", &states)) |
| 284 | if !states[0].Finished || !states[0].Refused { |
| 285 | t.Fatal("fenced delivery not finalised") |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | func TestCoordinatorPendingClaimPollNeverDispatches(t *testing.T) { |
| 290 | s, _, l, c := coordinatorServiceFixture(t) |
| 291 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) |
| 292 | mustJournal(t, s.Poll(context.Background())) |
| 293 | if l.starts != 0 { |
| 294 | t.Fatal("unrecovered claim intent redispatched") |
| 295 | } |
| 296 | } |
| 297 | |