Files
tasks/service/history/workflow/state_machine_timers.go
Yichao YangandGitHub c99bbf5718 Partial task refresh (#6305)
## What changed?
<!-- Describe what has changed in this PR -->
- Partial task refresh: only refresh tasks for components that got
updated after a given versioned transition

## Why?
<!-- Tell your future self why have you made these changes -->
- Needed in the new state based replication stack, where we sync mutable
state mutations and only want to generated tasks for those mutated
components.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
- Unit test.
- Backfilled tests for the entire task refresher.

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
- None. Not used anywhere.

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->
- N/A

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
- No
2024-08-07 12:00:19 -07:00

125 lines
4.7 KiB
Go

// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package workflow
import (
"errors"
"slices"
"time"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/service/history/hsm"
"go.temporal.io/server/service/history/tasks"
"google.golang.org/protobuf/types/known/timestamppb"
)
// AddNextStateMachineTimerTask generates a state machine timer task if the first deadline doesn't have a task scheduled
// yet.
func AddNextStateMachineTimerTask(ms MutableState) {
timers := ms.GetExecutionInfo().StateMachineTimers
if len(timers) == 0 {
return
}
timerGroup := timers[0]
// We already have a timer for this deadline.
if timerGroup.Scheduled {
return
}
ms.AddTasks(&tasks.StateMachineTimerTask{
WorkflowKey: ms.GetWorkflowKey(),
VisibilityTimestamp: timerGroup.Deadline.AsTime(),
Version: ms.GetCurrentVersion(),
})
timerGroup.Scheduled = true
}
// TrackStateMachineTimer tracks a timer task in the mutable state's StateMachineTimers slice sorted and grouped by
// deadline.
func TrackStateMachineTimer(ms MutableState, deadline time.Time, taskInfo *persistencespb.StateMachineTaskInfo) {
execInfo := ms.GetExecutionInfo()
group := &persistencespb.StateMachineTimerGroup{
Deadline: timestamppb.New(deadline),
Infos: []*persistencespb.StateMachineTaskInfo{taskInfo},
}
idx, found := slices.BinarySearchFunc(execInfo.StateMachineTimers, group, func(a, b *persistencespb.StateMachineTimerGroup) int {
return a.Deadline.AsTime().Compare(b.Deadline.AsTime())
})
if found {
execInfo.StateMachineTimers[idx].Infos = append(execInfo.StateMachineTimers[idx].Infos, taskInfo)
} else {
execInfo.StateMachineTimers = slices.Insert(execInfo.StateMachineTimers, idx, group)
}
}
// TrimStateMachineTimers returns of copy of trimmed the StateMachineTimers slice by removing any timer tasks that are
// associated with an HSM node that has been deleted or updated on or after the provided minVersionedTransition.
func TrimStateMachineTimers(
mutableState MutableState,
minVersionedTransition *persistencespb.VersionedTransition,
) error {
if CompareVersionedTransition(minVersionedTransition, EmptyVersionedTransition) == 0 {
// Reset all the state machine timers, we'll recreate them all.
mutableState.GetExecutionInfo().StateMachineTimers = nil
return nil
}
hsmRoot := mutableState.HSM()
trimmedStateMachineTimers := make([]*persistencespb.StateMachineTimerGroup, 0, len(mutableState.GetExecutionInfo().StateMachineTimers))
for _, timerGroup := range mutableState.GetExecutionInfo().StateMachineTimers {
trimmedTaskInfos := make([]*persistencespb.StateMachineTaskInfo, 0, len(timerGroup.Infos))
for _, taskInfo := range timerGroup.GetInfos() {
node, err := hsmRoot.Child(hsm.Ref{
StateMachineRef: taskInfo.Ref,
}.StateMachinePath())
if err != nil {
if errors.Is(err, hsm.ErrStateMachineNotFound) {
// node deleted, trim the task
continue
}
return err
}
if CompareVersionedTransition(
node.InternalRepr().LastUpdateVersionedTransition,
minVersionedTransition,
) >= 0 {
// node recently updated, trim the task.
// A complementary step is then required to regenerate timer tasks for it.
continue
}
trimmedTaskInfos = append(trimmedTaskInfos, taskInfo)
}
if len(trimmedTaskInfos) > 0 {
trimmedStateMachineTimers = append(trimmedStateMachineTimers, &persistencespb.StateMachineTimerGroup{
Infos: trimmedTaskInfos,
Deadline: timerGroup.Deadline,
Scheduled: timerGroup.Scheduled,
})
}
}
mutableState.GetExecutionInfo().StateMachineTimers = trimmedStateMachineTimers
return nil
}