Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ContinueAsNewSuggested in scheduler workflow #4990

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions service/worker/scheduler/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ type (
RecentActionCount int // The number of recent actual action results to include in Describe.
FutureActionCountForList int // The number of future action times to include in List (search attr).
RecentActionCountForList int // The number of recent actual action results to include in List (search attr).
IterationsBeforeContinueAsNew int
SleepWhilePaused bool // If true, don't set timers while paused/out of actions
IterationsBeforeContinueAsNew int // Number of iterations per run, or 0 to use server-suggested
SleepWhilePaused bool // If true, don't set timers while paused/out of actions
// MaxBufferSize limits the number of buffered starts. This also limits the number of
// workflows that can be backfilled at once (since they all have to fit in the buffer).
MaxBufferSize int
Expand Down Expand Up @@ -191,7 +191,7 @@ var (
RecentActionCount: 10,
FutureActionCountForList: 5,
RecentActionCountForList: 5,
IterationsBeforeContinueAsNew: 500,
IterationsBeforeContinueAsNew: 500, // TODO: change to 0 to use GetContinueAsNewSuggested
SleepWhilePaused: true,
MaxBufferSize: 1000,
AllowZeroSleep: true,
Expand Down Expand Up @@ -253,12 +253,17 @@ func (s *scheduler) run() error {

iters := s.tweakables.IterationsBeforeContinueAsNew
for {
// TODO: use the real GetContinueAsNewSuggested
continueAsNewSuggested := iters <= 0 || workflow.GetInfo(s.ctx).GetCurrentHistoryLength() >= impossibleHistorySize
if continueAsNewSuggested && s.pendingUpdate == nil && s.pendingPatch == nil {
info := workflow.GetInfo(s.ctx)
suggestContinueAsNew := info.GetCurrentHistoryLength() >= impossibleHistorySize
if s.tweakables.IterationsBeforeContinueAsNew > 0 {
suggestContinueAsNew = suggestContinueAsNew || iters <= 0
iters--
} else {
suggestContinueAsNew = suggestContinueAsNew || info.GetContinueAsNewSuggested()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use getVersion here since this may eagerly decide to continue as new

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workflow uses a MutableSideEffect of tweakablePolicies to get the same effect as the versioning api, but in bulk. IterationsBeforeContinueAsNew has always been > 0, we'll change it to 0 to activate this code path (in a separate PR to allow this one to go in a patch release to enable downgrade)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I was missing that context. Thanks. LGTM.

}
if suggestContinueAsNew && s.pendingUpdate == nil && s.pendingPatch == nil {
break
}
iters--

t1 := timestamp.TimeValue(s.State.LastProcessedTime)
t2 := s.now()
Expand Down
73 changes: 73 additions & 0 deletions service/worker/scheduler/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1697,3 +1697,76 @@ func (s *workflowSuite) TestExitScheduleWorkflowWhenEmpty() {
s.False(workflow.IsContinueAsNewError(s.env.GetWorkflowError()))
s.True(s.env.Now().Sub(baseStartTime) == currentTweakablePolicies.RetentionTime)
}

func (s *workflowSuite) TestCANByIterations() {
// written using low-level mocks so we can control iteration count

const iters = 30
// note: one fewer run than iters since the first doesn't start anything
for i := 1; i < iters; i++ {
t := baseStartTime.Add(5 * time.Minute * time.Duration(i))
s.expectStart(func(req *schedspb.StartWorkflowRequest) (*schedspb.StartWorkflowResponse, error) {
s.Equal("myid-"+t.Format(time.RFC3339), req.Request.WorkflowId)
return nil, nil
})
}
// this one catches and fails if we go over
s.expectStart(func(req *schedspb.StartWorkflowRequest) (*schedspb.StartWorkflowResponse, error) {
s.Fail("too many starts")
return nil, nil
}).Times(0).Maybe()

// this is ignored because we set iters explicitly
s.env.RegisterDelayedCallback(func() {
s.env.SetContinueAsNewSuggested(true)
}, 5*time.Minute*iters/2-time.Second)

s.run(&schedpb.Schedule{
Spec: &schedpb.ScheduleSpec{
Interval: []*schedpb.IntervalSpec{{
Interval: timestamp.DurationPtr(5 * time.Minute),
}},
},
Policies: &schedpb.SchedulePolicies{
OverlapPolicy: enumspb.SCHEDULE_OVERLAP_POLICY_ALLOW_ALL,
},
}, iters)
s.True(s.env.IsWorkflowCompleted())
s.True(workflow.IsContinueAsNewError(s.env.GetWorkflowError()))
}

func (s *workflowSuite) TestCANBySuggested() {
// written using low-level mocks so we can control iteration count

const iters = 30
// note: one fewer run than iters since the first doesn't start anything
for i := 1; i < iters; i++ {
t := baseStartTime.Add(5 * time.Minute * time.Duration(i))
s.expectStart(func(req *schedspb.StartWorkflowRequest) (*schedspb.StartWorkflowResponse, error) {
s.Equal("myid-"+t.Format(time.RFC3339), req.Request.WorkflowId)
return nil, nil
})
}
// this one catches and fails if we go over
s.expectStart(func(req *schedspb.StartWorkflowRequest) (*schedspb.StartWorkflowResponse, error) {
s.Fail("too many starts", req.Request.WorkflowId)
return nil, nil
}).Times(0).Maybe()

s.env.RegisterDelayedCallback(func() {
s.env.SetContinueAsNewSuggested(true)
}, 5*time.Minute*iters-time.Second)

s.run(&schedpb.Schedule{
Spec: &schedpb.ScheduleSpec{
Interval: []*schedpb.IntervalSpec{{
Interval: timestamp.DurationPtr(5 * time.Minute),
}},
},
Policies: &schedpb.SchedulePolicies{
OverlapPolicy: enumspb.SCHEDULE_OVERLAP_POLICY_ALLOW_ALL,
},
}, 0) // 0 means use suggested
s.True(s.env.IsWorkflowCompleted())
s.True(workflow.IsContinueAsNewError(s.env.GetWorkflowError()))
}
Loading