fix(store): a restart re-queues a running task at its interrupted step and parks it after 3 restarts (#1841)
This commit is contained in:
11 files changed
+338
-62
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package taskstate
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlanRecovery(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
give RecoveryInput
|
||||
want Recovery
|
||||
}{
|
||||
{
|
||||
name: "resumes at the interrupted step with the finished steps' results",
|
||||
give: RecoveryInput{Stage: "build", PriorResults: []byte(`{"plan":1}`)},
|
||||
want: Recovery{RetryMode: string(RetryKeepWorktree), ResumeFrom: "build", ResumeResults: []byte(`{"plan":1}`)},
|
||||
},
|
||||
{
|
||||
name: "a crash inside the first resumed step keeps the row's resume point",
|
||||
give: RecoveryInput{Stage: "build", ResumeFrom: "build", ResumeResults: []byte(`{"plan":1}`)},
|
||||
want: Recovery{RetryMode: string(RetryKeepWorktree), ResumeFrom: "build", ResumeResults: []byte(`{"plan":1}`)},
|
||||
},
|
||||
{
|
||||
name: "the first step of a fresh attempt starts over",
|
||||
give: RecoveryInput{Stage: "plan", RetryMode: string(RetryContinuePushedWork), ResumeResults: []byte(`{}`)},
|
||||
want: Recovery{RetryMode: string(RetryContinuePushedWork), ResumeResults: []byte(`{}`)},
|
||||
},
|
||||
{
|
||||
name: "no running step leaves the row as it is",
|
||||
give: RecoveryInput{ResumeResults: []byte(`{}`)},
|
||||
want: Recovery{ResumeResults: []byte(`{}`)},
|
||||
},
|
||||
{
|
||||
name: "parks once the cap is spent",
|
||||
give: RecoveryInput{Restarts: MaxRestartRequeues, Stage: "build", PriorResults: []byte(`{}`)},
|
||||
want: Recovery{Park: true, ParkReason: "interrupted by 3 daemon restarts in a row; retry to run it again"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := PlanRecovery(tt.give)
|
||||
if got.Park != tt.want.Park || got.ParkReason != tt.want.ParkReason || got.RetryMode != tt.want.RetryMode ||
|
||||
got.ResumeFrom != tt.want.ResumeFrom || string(got.ResumeResults) != string(tt.want.ResumeResults) {
|
||||
t.Errorf("PlanRecovery() = %+v, want %+v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -356,3 +356,56 @@ func TransientRetryDue(class string, retries int, parkedAt, now time.Time) bool
|
||||
}
|
||||
return !now.Before(parkedAt.Add(transientBackoffBase << retries))
|
||||
}
|
||||
|
||||
// MaxRestartRequeues is how many times daemon restarts may re-queue a task
|
||||
// that was running before it parks for an operator.
|
||||
const MaxRestartRequeues = 3
|
||||
|
||||
// RecoveryInput is what the store knows about a task a restart found running.
|
||||
type RecoveryInput struct {
|
||||
// Restarts is how many earlier restarts re-queued the task.
|
||||
Restarts int
|
||||
// Stage is the root step the restart interrupted, "" when none was running.
|
||||
Stage string
|
||||
// PriorResults are the step results recorded by the last step this attempt
|
||||
// finished before Stage, nil when it finished none.
|
||||
PriorResults []byte
|
||||
// RetryMode, ResumeFrom and ResumeResults are the task row's own.
|
||||
RetryMode, ResumeFrom string
|
||||
ResumeResults []byte
|
||||
}
|
||||
|
||||
// Recovery is how a task a restart found running continues.
|
||||
type Recovery struct {
|
||||
// Park reports that the task goes to an operator instead of the queue.
|
||||
Park bool
|
||||
ParkReason string
|
||||
// RetryMode, ResumeFrom and ResumeResults are written to the re-queued row.
|
||||
RetryMode, ResumeFrom string
|
||||
ResumeResults []byte
|
||||
}
|
||||
|
||||
// PlanRecovery decides how a restart treats a task left running: it resumes
|
||||
// at the interrupted step in its kept worktree, with the results of the steps
|
||||
// before it, rather than repaying them. A task already re-queued
|
||||
// MaxRestartRequeues times parks instead.
|
||||
func PlanRecovery(in RecoveryInput) Recovery {
|
||||
if in.Restarts >= MaxRestartRequeues {
|
||||
return Recovery{Park: true, ParkReason: fmt.Sprintf("interrupted by %d daemon restarts in a row; retry to run it again", in.Restarts)}
|
||||
}
|
||||
rec := Recovery{RetryMode: in.RetryMode, ResumeFrom: in.ResumeFrom, ResumeResults: in.ResumeResults}
|
||||
if in.Stage == "" {
|
||||
return rec
|
||||
}
|
||||
switch {
|
||||
case in.PriorResults != nil:
|
||||
rec.ResumeFrom, rec.ResumeResults = in.Stage, in.PriorResults
|
||||
case in.ResumeFrom == in.Stage:
|
||||
// The attempt was itself resumed at this step and finished none since.
|
||||
default:
|
||||
// The first step of a fresh attempt: nothing is finished, so start over.
|
||||
return Recovery{RetryMode: in.RetryMode, ResumeResults: []byte("{}")}
|
||||
}
|
||||
rec.RetryMode = string(RetryKeepWorktree)
|
||||
return rec
|
||||
}
|
||||
Reference in new issue
Block a user