Skip to content
Open
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
170 changes: 107 additions & 63 deletions chasm/lib/activity/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func NewStandaloneActivity(
HeartbeatTimeout: request.GetHeartbeatTimeout(),
RetryPolicy: request.GetRetryPolicy(),
Priority: request.GetPriority(),
StartDelay: request.GetStartDelay(),
},
},
LastAttempt: chasm.NewDataField(ctx, &activitypb.ActivityAttemptState{}),
Expand Down Expand Up @@ -624,6 +625,29 @@ func (a *Activity) UpdateActivityExecutionOptions(

frontendReq := req.GetFrontendRequest()

// start_delay updates are only valid while the activity is still in its delay window.
var hasStartDelayInMask bool
if mask := frontendReq.GetUpdateMask(); mask != nil {
_, hasStartDelayInMask = util.ParseFieldMask(mask)["startDelay"]
}
if !frontendReq.GetRestoreOriginal() && hasStartDelayInMask {
newDelay := frontendReq.GetActivityOptions().GetStartDelay()
if err := validateStartDelay(newDelay); err != nil {
return nil, err
}
if newDelay.AsDuration() > 0 {
actCtx := activityContextFromChasm(ctx)
if !actCtx.config.StartDelayEnabled(frontendReq.GetNamespace()) {
return nil, serviceerror.NewInvalidArgument("start_delay is not enabled for this namespace")
}
}
if a.GetStatus() != activitypb.ACTIVITY_EXECUTION_STATUS_SCHEDULED ||
!a.firstDispatchTime().After(ctx.Now(a)) {
return nil, serviceerror.NewInvalidArgument(
"cannot update start_delay: activity is no longer in its delay window")
}
}

if frontendReq.GetRestoreOriginal() {
ogOptions := a.GetOriginalOptions()
a.TaskQueue = common.CloneProto(ogOptions.GetTaskQueue())
Expand All @@ -633,6 +657,11 @@ func (a *Activity) UpdateActivityExecutionOptions(
a.HeartbeatTimeout = common.CloneProto(ogOptions.GetHeartbeatTimeout())
a.RetryPolicy = common.CloneProto(ogOptions.GetRetryPolicy())
a.Priority = common.CloneProto(ogOptions.GetPriority())
// start_delay only governs the first dispatch. Once the first attempt has started, restoring
// the original value would shift ScheduleToClose without affecting dispatch timing.
if a.GetFirstAttemptStartedTime() == nil {
a.StartDelay = common.CloneProto(ogOptions.GetStartDelay())
}
} else {
if err := a.mergeActivityOptions(frontendReq); err != nil {
return nil, err
Expand All @@ -650,9 +679,8 @@ func (a *Activity) UpdateActivityExecutionOptions(

// Add a new ScheduleToCloseTimeoutTask at the (possibly updated) deadline.
// Increment the stamp so the previous task is invalidated by the Validate check.
if timeout := a.GetScheduleToCloseTimeout().AsDuration(); timeout > 0 {
if deadline := a.scheduleToCloseDeadline(); !deadline.IsZero() {
a.ScheduleToCloseStamp++
deadline := a.GetScheduleTime().AsTime().Add(timeout)
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: deadline},
Expand All @@ -662,68 +690,9 @@ func (a *Activity) UpdateActivityExecutionOptions(

attempt.Stamp++

if a.GetStatus() == activitypb.ACTIVITY_EXECUTION_STATUS_STARTED ||
a.GetStatus() == activitypb.ACTIVITY_EXECUTION_STATUS_CANCEL_REQUESTED ||
a.GetStatus() == activitypb.ACTIVITY_EXECUTION_STATUS_PAUSE_REQUESTED ||
a.GetStatus() == activitypb.ACTIVITY_EXECUTION_STATUS_RESET_REQUESTED {
// Re-create the start-to-close timeout task with the new stamp and (possibly updated) timeout.
// The old task was invalidated by the stamp increment above.
if timeout := a.GetStartToCloseTimeout().AsDuration(); timeout > 0 {
deadline := attempt.GetStartedTime().AsTime().Add(timeout)
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: deadline},
&activitypb.StartToCloseTimeoutTask{Stamp: attempt.GetStamp()},
)
}

if hbTimeout := a.GetHeartbeatTimeout().AsDuration(); hbTimeout > 0 {
// The next heartbeat time is the max of (the last heartbeats recorded time and
// the current attempts started time) plus the heartbeat timeout
lastHb, _ := a.LastHeartbeat.TryGet(ctx)
lastHbTime := util.MaxTime(
lastHb.GetRecordedTime().AsTime(),
attempt.GetStartedTime().AsTime(),
).Add(hbTimeout)
ctx.AddTask(
a,
chasm.TaskAttributes{
ScheduledTime: lastHbTime,
},
&activitypb.HeartbeatTimeoutTask{
Stamp: attempt.GetStamp(),
},
)
}
}

// TODO(saa-ga): need to handle the StartDelay timer

a.reissueRunningAttemptTimers(ctx, attempt)
if a.GetStatus() == activitypb.ACTIVITY_EXECUTION_STATUS_SCHEDULED {
// Re dispatch this activity
retryTime := attemptScheduleTimeForRetry(attempt)
var dispatchAttrs chasm.TaskAttributes
if retryTime != nil {
// in backoff, future retry time
dispatchAttrs.ScheduledTime = retryTime.AsTime()
}
ctx.AddTask(
a,
dispatchAttrs,
&activitypb.ActivityDispatchTask{Stamp: attempt.GetStamp()},
)

if timeout := a.GetScheduleToStartTimeout().AsDuration(); timeout > 0 {
schedToStart := ctx.Now(a).Add(timeout)
if retryTime != nil {
schedToStart = retryTime.AsTime().Add(timeout)
}
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: schedToStart},
&activitypb.ScheduleToStartTimeoutTask{Stamp: attempt.GetStamp()},
)
}
a.reissueScheduledDispatch(ctx, attempt)
}

metricsHandler, err := a.enrichMetricsHandler(ctx, metrics.ActivityUpdateOptionsScope)
Expand All @@ -742,6 +711,7 @@ func (a *Activity) UpdateActivityExecutionOptions(
HeartbeatTimeout: a.GetHeartbeatTimeout(),
RetryPolicy: a.GetRetryPolicy(),
Priority: a.GetPriority(),
StartDelay: a.GetStartDelay(),
},
},
}, nil
Expand All @@ -763,6 +733,7 @@ func (a *Activity) mergeActivityOptions(
HeartbeatTimeout: a.HeartbeatTimeout,
Priority: a.Priority,
RetryPolicy: a.RetryPolicy,
StartDelay: a.StartDelay,
}

if err := activityoptions.MergeActivityOptions(ao, req.GetActivityOptions(), updateFields); err != nil {
Expand All @@ -784,6 +755,7 @@ func (a *Activity) mergeActivityOptions(
a.HeartbeatTimeout = ao.HeartbeatTimeout
a.Priority = ao.Priority
a.RetryPolicy = ao.RetryPolicy
a.StartDelay = ao.StartDelay

return nil
}
Expand Down Expand Up @@ -1226,6 +1198,78 @@ func (a *Activity) firstDispatchTime() time.Time {
return a.ScheduleTime.AsTime().Add(a.GetStartDelay().AsDuration())
}

// reissueScheduledDispatch re-emits the ActivityDispatchTask and ScheduleToStart timeout task for
// a SCHEDULED activity. Retries fire at the retry time; first attempts dispatch now, lifted to
// honor any pending start_delay.
func (a *Activity) reissueScheduledDispatch(ctx chasm.MutableContext, attempt *activitypb.ActivityAttemptState) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refactoring due to lint complaining code complexity

var scheduleTime time.Time
if retryTime := attemptScheduleTimeForRetry(attempt); retryTime != nil {
scheduleTime = retryTime.AsTime()
} else {
scheduleTime = a.respectStartDelay(ctx.Now(a))
}
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: scheduleTime},
&activitypb.ActivityDispatchTask{Stamp: attempt.GetStamp()},
)
if timeout := a.GetScheduleToStartTimeout().AsDuration(); timeout > 0 {
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: scheduleTime.Add(timeout)},
&activitypb.ScheduleToStartTimeoutTask{Stamp: attempt.GetStamp()},
)
}
}

// reissueRunningAttemptTimers re-emits the StartToClose and Heartbeat timeout tasks for the
// currently-running attempt, anchored to the attempt's StartedTime. Called from options-update
// paths after stamp bump so the old tasks are invalidated and replaced with the (possibly
// updated) timeouts. No-op unless the activity is in a status where a worker holds the task token
// (STARTED / CANCEL_REQUESTED / PAUSE_REQUESTED / RESET_REQUESTED).
func (a *Activity) reissueRunningAttemptTimers(ctx chasm.MutableContext, attempt *activitypb.ActivityAttemptState) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refactoring due to lint complaining code complexity

if a.GetStatus() != activitypb.ACTIVITY_EXECUTION_STATUS_STARTED &&
a.GetStatus() != activitypb.ACTIVITY_EXECUTION_STATUS_CANCEL_REQUESTED &&
a.GetStatus() != activitypb.ACTIVITY_EXECUTION_STATUS_PAUSE_REQUESTED &&
a.GetStatus() != activitypb.ACTIVITY_EXECUTION_STATUS_RESET_REQUESTED {
return
}
if timeout := a.GetStartToCloseTimeout().AsDuration(); timeout > 0 {
deadline := attempt.GetStartedTime().AsTime().Add(timeout)
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: deadline},
&activitypb.StartToCloseTimeoutTask{Stamp: attempt.GetStamp()},
)
}
if hbTimeout := a.GetHeartbeatTimeout().AsDuration(); hbTimeout > 0 {
// Next heartbeat fires at max(last recorded heartbeat, current attempt start) + heartbeat timeout.
lastHb, _ := a.LastHeartbeat.TryGet(ctx)
lastHbTime := util.MaxTime(
lastHb.GetRecordedTime().AsTime(),
attempt.GetStartedTime().AsTime(),
).Add(hbTimeout)
ctx.AddTask(
a,
chasm.TaskAttributes{ScheduledTime: lastHbTime},
&activitypb.HeartbeatTimeoutTask{Stamp: attempt.GetStamp()},
)
}
}

// respectStartDelay lifts a candidate dispatch time up to scheduleTime + start_delay when the
// activity has not yet been picked up by a worker, so pre-dispatch re-scheduling (unpause, Reset+
// RestoreOriginalOptions, options update) honors start_delay. No-op once dispatched.
func (a *Activity) respectStartDelay(scheduleTime time.Time) time.Time {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

important helper reused in downstream PRs

if a.GetFirstAttemptStartedTime() != nil {
return scheduleTime
}
if firstDispatch := a.firstDispatchTime(); firstDispatch.After(scheduleTime) {
return firstDispatch
}
return scheduleTime
}

// scheduleToCloseDeadline returns the absolute time at which the ScheduleToClose timeout expires,
// accounting for start delay. Returns zero time if no ScheduleToClose timeout is set.
func (a *Activity) scheduleToCloseDeadline() time.Time {
Expand Down
69 changes: 41 additions & 28 deletions chasm/lib/activity/gen/activitypb/v1/activity_state.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions chasm/lib/activity/proto/v1/activity_state.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ message ActivityState {
// that when the worker yields the activity lands back in PAUSED rather than SCHEDULED. Consumed
// when the activity transitions out of RESET_REQUESTED.
bool reset_keep_paused = 18;

// Time at which a worker first picked up the activity (the first attempt's started time). Set
// once on the first SCHEDULED->STARTED transition and never updated thereafter, so it survives
// retries and resets. Used as the discriminator for whether start_delay still applies on
// pre-dispatch rescheduling paths.
google.protobuf.Timestamp first_attempt_started_time = 19;
}

message ActivityCancelState {
Expand Down
Loading
Loading