| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/hostedwire" |
| 10 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 11 | ) |
| 12 | |
| 13 | // pauseCols are the jobs columns the pause barrier and resume write. |
| 14 | type pauseCols struct { |
| 15 | state string |
| 16 | gen, epoch int64 |
| 17 | started sql.NullInt64 |
| 18 | quiescent, cancelled bool |
| 19 | } |
| 20 | |
| 21 | func (e *launchEnv) pauseCols(attempt string) pauseCols { |
| 22 | e.t.Helper() |
| 23 | var p pauseCols |
| 24 | if err := e.store.Tx(e.ctx, func(tx *sql.Tx) error { |
| 25 | return tx.QueryRowContext(e.ctx, `SELECT state, pause_generation, epoch, pause_started_t, pause_quiescent, |
| 26 | pause_cancelled FROM jobs WHERE attempt_id = ?`, attempt).Scan( |
| 27 | &p.state, &p.gen, &p.epoch, &p.started, &p.quiescent, &p.cancelled) |
| 28 | }); err != nil { |
| 29 | e.t.Fatalf("read pause columns of %s = %v", attempt, err) |
| 30 | } |
| 31 | return p |
| 32 | } |
| 33 | |
| 34 | // runningJob is a controller with att-1 launched for inst-1 (lifetime 2 h), |
| 35 | // its VM running and the job's state saved as running. |
| 36 | func runningJob(t *testing.T) *launchEnv { |
| 37 | t.Helper() |
| 38 | e := newLaunchEnv(t) |
| 39 | e.launchWith("att-1", 2*time.Hour) |
| 40 | e.fake.Tick() |
| 41 | e.refresh() |
| 42 | if p := e.pauseCols("att-1"); p.state != JobRunning { |
| 43 | t.Fatalf("att-1 after launch and refresh is %q, want running", p.state) |
| 44 | } |
| 45 | return e |
| 46 | } |
| 47 | |
| 48 | func (e *launchEnv) drain() int64 { |
| 49 | e.t.Helper() |
| 50 | g, err := e.c.Drain(e.ctx, "inst-1", instKey, "att-1") |
| 51 | if err != nil || g < 1 { |
| 52 | e.t.Fatalf("Drain = (%d, %v), want a new generation", g, err) |
| 53 | } |
| 54 | return g |
| 55 | } |
| 56 | |
| 57 | func (e *launchEnv) ack(g int64) { |
| 58 | e.t.Helper() |
| 59 | if err := e.c.Quiescent(e.ctx, "inst-1", instKey, "att-1", g); err != nil { |
| 60 | e.t.Fatalf("Quiescent(%d) = %v", g, err) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // suspendJob drains att-1, acknowledges, suspends, and lets the VM and the |
| 65 | // job settle as suspended. It returns the generation. |
| 66 | func (e *launchEnv) suspendJob() int64 { |
| 67 | e.t.Helper() |
| 68 | g := e.drain() |
| 69 | e.ack(g) |
| 70 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g); err != nil { |
| 71 | e.t.Fatalf("Suspend(%d) = %v", g, err) |
| 72 | } |
| 73 | e.fake.Tick() |
| 74 | e.refresh() |
| 75 | if p := e.pauseCols("att-1"); p.state != JobSuspended { |
| 76 | e.t.Fatalf("att-1 after suspend and refresh is %q, want suspended", p.state) |
| 77 | } |
| 78 | return g |
| 79 | } |
| 80 | |
| 81 | func TestSuspendNeedsCurrentAck(t *testing.T) { |
| 82 | t.Parallel() |
| 83 | for _, tc := range []struct { |
| 84 | name string |
| 85 | // setup opens generations and acknowledges as the case needs; it |
| 86 | // returns the generation Suspend is asked for, the current one, and |
| 87 | // whether the current one may still be suspended afterwards. |
| 88 | setup func(e *launchEnv) (asked, current int64, live bool) |
| 89 | wantErr error |
| 90 | }{ |
| 91 | {"no acknowledgement", func(e *launchEnv) (int64, int64, bool) { |
| 92 | g := e.drain() |
| 93 | return g, g, true |
| 94 | }, ErrNotQuiescent}, |
| 95 | {"an older generation", func(e *launchEnv) (int64, int64, bool) { |
| 96 | g1 := e.drain() |
| 97 | g2 := e.drain() |
| 98 | if err := e.c.Quiescent(e.ctx, "inst-1", instKey, "att-1", g1); !errors.Is(err, ErrGenerationCancelled) { |
| 99 | e.t.Fatalf("Quiescent for a superseded generation = %v, want ErrGenerationCancelled", err) |
| 100 | } |
| 101 | e.ack(g2) |
| 102 | return g1, g2, true |
| 103 | }, ErrGenerationCancelled}, |
| 104 | {"the next generation", func(e *launchEnv) (int64, int64, bool) { |
| 105 | g := e.drain() |
| 106 | e.ack(g) |
| 107 | return g + 1, g, true |
| 108 | }, ErrGenerationCancelled}, |
| 109 | {"a cancelled generation", func(e *launchEnv) (int64, int64, bool) { |
| 110 | g := e.drain() |
| 111 | e.clk.AdvanceMono(PauseAckTimeout + time.Second) |
| 112 | return g, g, false |
| 113 | }, ErrGenerationCancelled}, |
| 114 | } { |
| 115 | t.Run(tc.name, func(t *testing.T) { |
| 116 | t.Parallel() |
| 117 | e := runningJob(t) |
| 118 | asked, current, live := tc.setup(e) |
| 119 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", asked); !errors.Is(err, tc.wantErr) { |
| 120 | t.Fatalf("Suspend(%d) with current generation %d = %v, want %v", asked, current, err, tc.wantErr) |
| 121 | } |
| 122 | if a := e.lastAudit(); a.Actor != "inst-1" || a.Action != actionSuspendRefused { |
| 123 | t.Fatalf("audit after the refusal = %+v, want suspend_refused by inst-1", a) |
| 124 | } |
| 125 | if n := e.fake.Calls(isolatedjob.OpSuspend); n != 0 { |
| 126 | t.Fatalf("a refused Suspend reached the provider %d times", n) |
| 127 | } |
| 128 | if p := e.pauseCols("att-1"); p.state != JobDraining || p.gen != current { |
| 129 | t.Fatalf("after the refusal: %+v, want draining at generation %d", p, current) |
| 130 | } |
| 131 | if !live { |
| 132 | return |
| 133 | } |
| 134 | if p := e.pauseCols("att-1"); !p.quiescent { |
| 135 | e.ack(current) |
| 136 | } |
| 137 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", current); err != nil { |
| 138 | t.Fatalf("Suspend of the acknowledged current generation %d = %v", current, err) |
| 139 | } |
| 140 | if p := e.pauseCols("att-1"); p.state != JobSuspending || e.fake.Calls(isolatedjob.OpSuspend) != 1 { |
| 141 | t.Fatalf("after Suspend: %+v, %d provider suspends; want suspending, 1", p, e.fake.Calls(isolatedjob.OpSuspend)) |
| 142 | } |
| 143 | }) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestLateAckIgnored(t *testing.T) { |
| 148 | t.Parallel() |
| 149 | e := runningJob(t) |
| 150 | g := e.drain() |
| 151 | e.clk.AdvanceMono(PauseAckTimeout + time.Second) |
| 152 | if err := e.c.Quiescent(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| 153 | t.Fatalf("Quiescent at 2m1s = %v, want ErrGenerationCancelled", err) |
| 154 | } |
| 155 | if p := e.pauseCols("att-1"); !p.cancelled || p.quiescent || p.state != JobDraining { |
| 156 | t.Fatalf("after a late ack: %+v, want draining, cancelled, not quiescent", p) |
| 157 | } |
| 158 | if !e.hasAudit("", "att-1", actionPauseCancelled, "no quiescent acknowledgement") { |
| 159 | t.Fatal("no pause_cancelled audit row") |
| 160 | } |
| 161 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| 162 | t.Fatalf("Suspend after a late ack = %v, want ErrGenerationCancelled", err) |
| 163 | } |
| 164 | if n := e.fake.Calls(isolatedjob.OpSuspend); n != 0 { |
| 165 | t.Fatalf("provider suspends = %d, want 0", n) |
| 166 | } |
| 167 | |
| 168 | // The runner takes work again: admission reopens; the counter stays. |
| 169 | if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g+1); !errors.Is(err, ErrGenerationCancelled) { |
| 170 | t.Fatalf("Reopen of another generation = %v, want ErrGenerationCancelled", err) |
| 171 | } |
| 172 | if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g); err != nil { |
| 173 | t.Fatalf("Reopen(%d) = %v", g, err) |
| 174 | } |
| 175 | if p := e.pauseCols("att-1"); p.state != JobRunning || p.gen != g || p.started.Valid || p.quiescent || p.cancelled { |
| 176 | t.Fatalf("after Reopen: %+v, want running at generation %d with the pause fields reset", p, g) |
| 177 | } |
| 178 | |
| 179 | // An acknowledgement at exactly PauseAckTimeout is in time. |
| 180 | g2 := e.drain() |
| 181 | if g2 != g+1 { |
| 182 | t.Fatalf("next Drain = %d, want %d", g2, g+1) |
| 183 | } |
| 184 | e.clk.AdvanceMono(PauseAckTimeout) |
| 185 | e.ack(g2) |
| 186 | e.clk.AdvanceMono(time.Hour) // a timely ack does not expire |
| 187 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g2); err != nil { |
| 188 | t.Fatalf("Suspend after an ack at exactly 2m = %v", err) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // TestPauseAckAcrossRestartCancelled: drain, then a restart whose ten |
| 193 | // minutes of downtime the wall clock hides. The old generation's |
| 194 | // acknowledgement must be refused, before the first Refresh and after it, |
| 195 | // and its Suspend too: controller time cannot tell it was late. A |
| 196 | // generation opened after the restart is acknowledged as usual. |
| 197 | func TestPauseAckAcrossRestartCancelled(t *testing.T) { |
| 198 | t.Parallel() |
| 199 | for _, refreshFirst := range []bool{false, true} { |
| 200 | e := runningJob(t) |
| 201 | g := e.drain() |
| 202 | e.down() |
| 203 | e.clk.AdvanceMono(10 * time.Minute) |
| 204 | e.restart() |
| 205 | if refreshFirst { |
| 206 | e.refresh() |
| 207 | } |
| 208 | if err := e.c.Quiescent(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| 209 | t.Fatalf("refresh first %v: Quiescent(%d) after the restart = %v, want ErrGenerationCancelled", refreshFirst, g, err) |
| 210 | } |
| 211 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrGenerationCancelled) { |
| 212 | t.Fatalf("refresh first %v: Suspend(%d) = %v, want ErrGenerationCancelled", refreshFirst, g, err) |
| 213 | } |
| 214 | if p := e.pauseCols("att-1"); !p.cancelled || p.quiescent || p.state != JobDraining || e.fake.Calls(isolatedjob.OpSuspend) != 0 { |
| 215 | t.Fatalf("refresh first %v: %+v, want the generation cancelled and no suspend", refreshFirst, p) |
| 216 | } |
| 217 | e.clk.AdvanceMono(time.Second) |
| 218 | e.ack(e.drain()) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // TestReopenWaitsForRestartReconciliation: a job drains at 1 h 40 min of |
| 223 | // its 1 h 50 min to stop; a restart hides 15 minutes of downtime. Reopen |
| 224 | // before the first Refresh must not open admission against the stale stop |
| 225 | // time; once Refresh re-bases the job on the provider's age, Reopen sees |
| 226 | // the stop time has passed. |
| 227 | func TestReopenWaitsForRestartReconciliation(t *testing.T) { |
| 228 | t.Parallel() |
| 229 | e := runningJob(t) |
| 230 | e.clk.AdvanceMono(100 * time.Minute) |
| 231 | g := e.drain() |
| 232 | e.down() |
| 233 | e.clk.AdvanceMono(15 * time.Minute) |
| 234 | e.restart() |
| 235 | if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, isolatedjob.ErrConflict) { |
| 236 | t.Fatalf("Reopen before the first refresh = %v, want ErrConflict", err) |
| 237 | } |
| 238 | if p := e.pauseCols("att-1"); p.state != JobDraining { |
| 239 | t.Fatalf("att-1 after the refused Reopen is %q, want draining", p.state) |
| 240 | } |
| 241 | e.refresh() |
| 242 | if err := e.c.Reopen(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrPastDeadline) { |
| 243 | t.Fatalf("Reopen after the re-base = %v, want ErrPastDeadline", err) |
| 244 | } |
| 245 | if p := e.pauseCols("att-1"); p.state != JobDraining { |
| 246 | t.Fatalf("att-1 after Reopen past its stop time is %q, want draining", p.state) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestSuspendedTimeCountsToDeadline(t *testing.T) { |
| 251 | t.Parallel() |
| 252 | e := runningJob(t) |
| 253 | before := e.stopCols("att-1") |
| 254 | e.suspendJob() |
| 255 | e.clk.AdvanceMono(30 * time.Minute) |
| 256 | e.refresh() |
| 257 | v, err := e.c.Get(e.ctx, "inst-1", "att-1") |
| 258 | if err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
| 261 | now := e.store.Clock().Now() |
| 262 | if want := time.Duration(T(before.stop) - now); v.StopIn != want || v.DeadlineIn != time.Duration(T(before.deadline)-now) { |
| 263 | t.Fatalf("while suspended: StopIn %v DeadlineIn %v, want %v and %v (suspended time counts)", |
| 264 | v.StopIn, v.DeadlineIn, want, time.Duration(T(before.deadline)-now)) |
| 265 | } |
| 266 | if _, err := e.c.Resume(e.ctx, "inst-1", instKey, "att-1", "op-1"); !errors.Is(err, ErrResumePending) { |
| 267 | t.Fatalf("Resume = %v, want ErrResumePending until the VM runs", err) |
| 268 | } |
| 269 | e.fake.Tick() |
| 270 | if ep, err := e.c.Resume(e.ctx, "inst-1", instKey, "att-1", "op-1"); err != nil || ep != 1 { |
| 271 | t.Fatalf("Resume after the VM runs = (%d, %v), want (1, nil)", ep, err) |
| 272 | } |
| 273 | after := e.stopCols("att-1") |
| 274 | if after.launched != before.launched || after.deadline != before.deadline || after.drain != before.drain || after.stop != before.stop { |
| 275 | t.Fatalf("times moved across suspend and resume: %+v, want %+v", after, before) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func TestSuspendAndResumeRoles(t *testing.T) { |
| 280 | t.Parallel() |
| 281 | e := runningJob(t) |
| 282 | g := e.drain() |
| 283 | calls := map[string]func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error{ |
| 284 | actionDrainRefused: func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error { |
| 285 | _, err := e.c.Drain(e.ctx, by, key, attempt) |
| 286 | return err |
| 287 | }, |
| 288 | actionQuiescentRefused: func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error { |
| 289 | return e.c.Quiescent(e.ctx, by, key, attempt, g) |
| 290 | }, |
| 291 | actionSuspendRefused: func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error { |
| 292 | return e.c.Suspend(e.ctx, by, key, attempt, g) |
| 293 | }, |
| 294 | actionReopenRefused: func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error { |
| 295 | return e.c.Reopen(e.ctx, by, key, attempt, g) |
| 296 | }, |
| 297 | actionResumeRefused: func(by hostedwire.KeyID, key hostedwire.VerifyKey, attempt string) error { |
| 298 | _, err := e.c.Resume(e.ctx, by, key, attempt, "op-1") |
| 299 | return err |
| 300 | }, |
| 301 | } |
| 302 | for refused, call := range calls { |
| 303 | for _, tc := range []struct { |
| 304 | name string |
| 305 | by hostedwire.KeyID |
| 306 | key hostedwire.VerifyKey |
| 307 | attempt string |
| 308 | }{ |
| 309 | {"another instance", "inst-2", instKey, "att-1"}, |
| 310 | {"an operator key with the creator's id", "inst-1", opKey, "att-1"}, |
| 311 | {"no key id", "", instKey, "att-1"}, |
| 312 | {"an unknown attempt", "inst-1", instKey, "att-9"}, |
| 313 | } { |
| 314 | if err := call(tc.by, tc.key, tc.attempt); !errors.Is(err, ErrForbidden) { |
| 315 | t.Fatalf("%s by %s = %v, want ErrForbidden", refused, tc.name, err) |
| 316 | } |
| 317 | if a := e.lastAudit(); a.Actor != tc.by || a.Action != refused { |
| 318 | t.Fatalf("audit after %s by %s = %+v, want %s by %q", refused, tc.name, a, refused, tc.by) |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | if p := e.pauseCols("att-1"); p.state != JobDraining || p.gen != g || p.quiescent || p.epoch != 0 { |
| 323 | t.Fatalf("refused calls changed the job: %+v", p) |
| 324 | } |
| 325 | if n := e.fake.Calls(isolatedjob.OpSuspend) + e.fake.Calls(isolatedjob.OpResume); n != 0 { |
| 326 | t.Fatalf("refused calls reached the provider %d times", n) |
| 327 | } |
| 328 | if ops := e.resumeOps("att-1"); len(ops) != 0 { |
| 329 | t.Fatalf("refused calls prepared resume operations: %+v", ops) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | func TestSuspendRefreshSettles(t *testing.T) { |
| 334 | t.Parallel() |
| 335 | e := runningJob(t) |
| 336 | g := e.drain() |
| 337 | e.exec(`UPDATE jobs SET notfound_since_t = 1 WHERE attempt_id = 'att-1'`) // a first not-found |
| 338 | e.refresh() |
| 339 | if p, s := e.pauseCols("att-1"), e.stopCols("att-1"); p.state != JobDraining || s.notFoundSince.Valid { |
| 340 | t.Fatalf("Refresh of a draining job with a running VM left %q, not-found since %v; want draining, cleared", |
| 341 | p.state, s.notFoundSince) |
| 342 | } |
| 343 | e.ack(g) |
| 344 | |
| 345 | // The suspend call is throttled and does nothing: Refresh sends it again. |
| 346 | e.fake.Inject(isolatedjob.OpSuspend, isolatedjob.FaultThrottle) |
| 347 | if err := e.c.Suspend(e.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, isolatedjob.ErrThrottled) { |
| 348 | t.Fatalf("throttled Suspend = %v, want ErrThrottled", err) |
| 349 | } |
| 350 | if p := e.pauseCols("att-1"); p.state != JobSuspending { |
| 351 | t.Fatalf("after a throttled suspend call the job is %q, want suspending", p.state) |
| 352 | } |
| 353 | e.refresh() |
| 354 | if p, n := e.pauseCols("att-1"), e.fake.Calls(isolatedjob.OpSuspend); p.state != JobSuspending || n != 2 { |
| 355 | t.Fatalf("Refresh with the VM still running: %q, %d provider suspends; want suspending, 2", p.state, n) |
| 356 | } |
| 357 | e.refresh() // the VM is suspending now: nothing to send |
| 358 | if n := e.fake.Calls(isolatedjob.OpSuspend); n != 2 { |
| 359 | t.Fatalf("provider suspends = %d after a refresh of a suspending VM, want 2", n) |
| 360 | } |
| 361 | e.fake.Tick() |
| 362 | e.refresh() |
| 363 | if p := e.pauseCols("att-1"); p.state != JobSuspended { |
| 364 | t.Fatalf("Refresh with the VM suspended left %q, want suspended", p.state) |
| 365 | } |
| 366 | |
| 367 | // A prepared resume: Refresh keeps resuming while the VM is suspended, |
| 368 | // sends the resume again, and commits once the VM runs. |
| 369 | if _, err := e.c.Resume(e.ctx, "inst-1", instKey, "att-1", "op-1"); !errors.Is(err, ErrResumePending) { |
| 370 | t.Fatalf("Resume = %v, want ErrResumePending", err) |
| 371 | } |
| 372 | // A re-send that fails is audited and tried again; Refresh goes on. |
| 373 | e.fake.Inject(isolatedjob.OpResume, isolatedjob.FaultThrottle) |
| 374 | e.refresh() |
| 375 | if !e.hasAudit("", "att-1", actionResumeCallFailed, "op-1") { |
| 376 | t.Fatal("no resume_call_failed audit row for a failed re-send") |
| 377 | } |
| 378 | e.refresh() |
| 379 | if p, n := e.pauseCols("att-1"), e.fake.Calls(isolatedjob.OpResume); p.state != JobResuming || p.epoch != 0 || n != 3 { |
| 380 | t.Fatalf("Refresh with the VM suspended: %+v, %d provider resumes; want resuming at epoch 0, 3", p, n) |
| 381 | } |
| 382 | e.fake.Tick() |
| 383 | e.refresh() |
| 384 | if p := e.pauseCols("att-1"); p.state != JobRunning || p.epoch != 1 || p.started.Valid || p.quiescent || p.gen != g { |
| 385 | t.Fatalf("Refresh with the VM running: %+v, want running at epoch 1, pause closed, generation %d", p, g) |
| 386 | } |
| 387 | if ops := e.resumeOps("att-1"); len(ops) != 1 || ops[0].status != "done" || ops[0].result.Int64 != 1 { |
| 388 | t.Fatalf("resume operations = %+v, want op-1 done at epoch 1", ops) |
| 389 | } |
| 390 | } |
| 391 | |