Repository navigation
server,controller: bound task reconciliation deadline under the task lock TTL - #483
aprajitaarora13 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces per-reconcile context deadlines to bound task reconciliation operations under the distributed lock TTL, preventing lock expiration issues. It also handles transient substrate errors during actor suspension by transitioning the task to a "Pending" phase with a "SubstrateUnavailable" status. Feedback on the changes suggests returning early from deleteTaskTemplates when the context is canceled or expired to avoid unnecessary subsequent RPC calls, rather than just breaking the inner retry loop.
| retryLoop: | ||
| for attempt := 0; attempt < 5; attempt++ { | ||
| delErr = r.client.DeleteActorTemplate(ctx, atespace, name) | ||
| if delErr == nil || status.Code(delErr) == codes.NotFound { | ||
| delErr = nil | ||
| break | ||
| } | ||
| if ctx.Err() != nil { | ||
| delErr = ctx.Err() | ||
| break | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| delErr = ctx.Err() | ||
| break | ||
| break retryLoop | ||
| case <-time.After(500 * time.Millisecond): | ||
| } | ||
| } |
There was a problem hiding this comment.
When the context is canceled or expired, continuing to iterate over the remaining templates in the outer loop is inefficient and will result in unnecessary, immediately-failing RPC calls. Instead of breaking the inner retry loop, we should return early from deleteTaskTemplates with the accumulated errors and the context error. This also allows us to simplify the code by removing the retryLoop label.
for attempt := 0; attempt < 5; attempt++ {
delErr = r.client.DeleteActorTemplate(ctx, atespace, name)
if delErr == nil || status.Code(delErr) == codes.NotFound {
delErr = nil
break
}
if ctx.Err() != nil {
return errors.Join(append(errs, ctx.Err())...)
}
select {
case <-ctx.Done():
return errors.Join(append(errs, ctx.Err())...)
case <-time.After(500 * time.Millisecond):
}
}There was a problem hiding this comment.
Done — updated deleteTaskTemplates to return early with errors.Join(append(errs, ctx.Err())...) when the context is canceled or expired.
…lock TTL When Substrate flaps across multiple sequential RPCs during a single reconciliation, in-flight retry loops (`retryOnCodes`) can accumulate beyond the fixed 30s Redis task lock TTL (`lock.DefaultTTL`) while the reconcile is still mutating Substrate and store state. - Export `lock.DefaultTTL` (30s) and define `server.DefaultReconcileTimeout` (`lock.DefaultTTL - 5*time.Second` = 25s, configurable via `server.Options.ReconcileTimeout`). - Bound `Reconcile` and `ReconcileDelete` calls in `CreateTask`, `ResumeTask`, `SuspendTask`, and `DeleteTask` with a derived `context.WithTimeout` so an in-flight reconciliation aborts with `context.DeadlineExceeded` before the task lock expires, leaving headroom to persist the resulting task status while the lock is still held. - Mark initial task creation as `Pending` (`SubstrateUnavailable`) if `SuspendActor` fails transiently or hits the reconcile deadline, and exit the template cleanup loop immediately when the context expires. Fixes google#473
9def8f8 to
7598a36
Compare
|
Thanks @aprajitaarora13, this is clean. Heads up that #474 adds lock-lease renewal, which changes the premise here (the lock no longer expires while work is running). Could you two agree on the combination? I'd lean to renewal as the base and this deadline as a looser cap, so legitimate long suspends aren't cut off. |
|
@aprajitaarora13 #490 makes the lock renew while work is in flight and raises the CLI's suspend/resume timeout to 5 minutes for slow microVM snapshots, so a 25s server-side cap would now cut those off. Could you close this in favor of #490, or keep only a much looser cap as a safety net? Your call. |
|
Closing this in favor of #490, which renews the lock while work is in flight. Thanks @aprajitaarora13, the deadline analysis was useful in getting there. |
Summary
Follow-up from #463 (review) and #468.
Because
retryOnCodesininternal/substrate/client.gocan retrycodes.Unavailableandcodes.Abortedfor up to ~6s–20s per RPC, a single task reconciliation that encounters Substrate flapping across multiple sequential RPCs (EnsureAtespace,EnsureActorTemplateWithImage,EnsureActor,SuspendActor/ResumeActor, plus workspace polling) could exceed the fixed 30s Redis task lock TTL (lock.DefaultTTL) while still mutating Substrate and store state.Changes
internal/lock/lock.go: Exportlock.DefaultTTL = 30 * time.Second.internal/server/server.go:server.DefaultReconcileTimeout = lock.DefaultTTL - 5*time.Second(25s, configurable viaserver.Options.ReconcileTimeout).context.WithTimeout(ctx, s.reconcileTimeout)forReconcileandReconcileDeleteinCreateTask,ResumeTask,SuspendTask, andDeleteTaskafter acquiring the task lock so an in-flight reconciliation aborts withcontext.DeadlineExceededbefore the 30s lock expires, leaving headroom to persist the resulting task status while the lock is still held.internal/controller/reconciler.go:SuspendActorfails transiently (or hitscontext.DeadlineExceeded) during initial task creation, recordPhase: PendingandReady=False (Reason: SubstrateUnavailable).deleteTaskTemplatesretry loop immediately when the context is canceled or expires.internal/server/server_test.go&internal/controller/reconciler_test.go:TestServer_ReconcileTimeoutBoundedUnderLockTTLcovering reconcile timeout behavior acrossCreateTask,ResumeTask,SuspendTask, andDeleteTask.TestTaskReconciler_TransientSubstrateUnavailable.Fixes #473