mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
Changed the operation cancelation logic to not cancel the operation if the operation hasn't been started yet. Before this change canceling an operation would add a NEXUS_OPERATION_CANCELED event and immediately transition to the `Canceled` state, which had issues with replication and buffered events. E.g. the `Started` event would be applied before the `CancelRequested` event on one cluster and then applied in reverse order when replicating to another cluster. Example history batch taken from a test cluster contains these events: ``` EVENT_TYPE_WORKFLOW_TASK_COMPLETED EVENT_TYPE_NEXUS_OPERATION_CANCEL_REQUESTED EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED EVENT_TYPE_NEXUS_OPERATION_STARTED // <-- this had to be buffered EVENT_TYPE_WORKFLOW_TASK_SCHEDULED EVENT_TYPE_WORKFLOW_TASK_STARTED ``` The new code path simply creates the cancelation state machine but doesn't actually cancel it, instead it lets the task executor attempt to resolve the machine as canceled. Eventually we'll want to properly support cancel-before-started. At the moment, we may end up starting an operation and leave it oblivious of the cancelation request. I didn't test the replication issue but the new code path avoids ordering issues by not transitioning to canceled state. Added unit tests for the executor changes and modified the state machine tests. We should patch 120 and maybe even 119. The latter will be promoted to OSS 1.25.0 but since we explicitly state that Nexus in 1.25.0 isn't suited for multi cluster deployments, we could skip this patch.
262 lines
6.9 KiB
Go
262 lines
6.9 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 nexusoperations
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.temporal.io/server/api/enums/v1"
|
|
"go.temporal.io/server/service/history/consts"
|
|
"go.temporal.io/server/service/history/hsm"
|
|
)
|
|
|
|
const (
|
|
TaskTypeTimeout = "nexusoperations.Timeout"
|
|
TaskTypeInvocation = "nexusoperations.Invocation"
|
|
TaskTypeBackoff = "nexusoperations.Backoff"
|
|
TaskTypeCancelation = "nexusoperations.Cancelation"
|
|
TaskTypeCancelationBackoff = "nexusoperations.CancelationBackoff"
|
|
)
|
|
|
|
type TimeoutTask struct {
|
|
Deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = TimeoutTask{}
|
|
|
|
func (TimeoutTask) Type() string {
|
|
return TaskTypeTimeout
|
|
}
|
|
|
|
func (t TimeoutTask) Kind() hsm.TaskKind {
|
|
return hsm.TaskKindTimer{Deadline: t.Deadline}
|
|
}
|
|
|
|
func (TimeoutTask) Concurrent() bool {
|
|
return true
|
|
}
|
|
|
|
// Validate checks if the timeout task is still valid to execute for the given node state.
|
|
func (t TimeoutTask) Validate(node *hsm.Node) error {
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !TransitionTimedOut.Possible(op) {
|
|
return fmt.Errorf(
|
|
"%w: %w: cannot timeout machine in state %v",
|
|
consts.ErrStaleReference,
|
|
hsm.ErrInvalidTransition,
|
|
op.State(),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type TimeoutTaskSerializer struct{}
|
|
|
|
func (TimeoutTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
|
|
if kind, ok := kind.(hsm.TaskKindTimer); ok {
|
|
return TimeoutTask{Deadline: kind.Deadline}, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: expected timer", hsm.ErrInvalidTaskKind)
|
|
}
|
|
|
|
func (TimeoutTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type InvocationTask struct {
|
|
EndpointName string
|
|
}
|
|
|
|
var _ hsm.Task = InvocationTask{}
|
|
|
|
func (InvocationTask) Type() string {
|
|
return TaskTypeInvocation
|
|
}
|
|
|
|
func (t InvocationTask) Kind() hsm.TaskKind {
|
|
return hsm.TaskKindOutbound{Destination: t.EndpointName}
|
|
}
|
|
|
|
func (InvocationTask) Concurrent() bool {
|
|
return true
|
|
}
|
|
|
|
func (t InvocationTask) Validate(node *hsm.Node) error {
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if op.State() != enums.NEXUS_OPERATION_STATE_SCHEDULED {
|
|
return fmt.Errorf(
|
|
"%w: operation is not in Scheduled state, current state: %v",
|
|
consts.ErrStaleReference,
|
|
op.State(),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type InvocationTaskSerializer struct{}
|
|
|
|
func (InvocationTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
|
|
if kind, ok := kind.(hsm.TaskKindOutbound); ok {
|
|
return InvocationTask{EndpointName: kind.Destination}, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: expected outbound", hsm.ErrInvalidTaskKind)
|
|
}
|
|
|
|
func (InvocationTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type BackoffTask struct {
|
|
Deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = BackoffTask{}
|
|
|
|
func (BackoffTask) Type() string {
|
|
return TaskTypeBackoff
|
|
}
|
|
|
|
func (t BackoffTask) Kind() hsm.TaskKind {
|
|
return hsm.TaskKindTimer{Deadline: t.Deadline}
|
|
}
|
|
|
|
func (BackoffTask) Concurrent() bool {
|
|
return true
|
|
}
|
|
|
|
func (t BackoffTask) Validate(node *hsm.Node) error {
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if op.State() != enums.NEXUS_OPERATION_STATE_BACKING_OFF {
|
|
return fmt.Errorf(
|
|
"%w: operation is not in BackingOff state, current state: %v",
|
|
consts.ErrStaleReference,
|
|
op.State(),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type BackoffTaskSerializer struct{}
|
|
|
|
func (BackoffTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
|
|
if kind, ok := kind.(hsm.TaskKindTimer); ok {
|
|
return BackoffTask{Deadline: kind.Deadline}, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: expected timer", hsm.ErrInvalidTaskKind)
|
|
}
|
|
|
|
func (BackoffTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type CancelationTask struct {
|
|
EndpointName string
|
|
}
|
|
|
|
var _ hsm.Task = CancelationTask{}
|
|
|
|
func (CancelationTask) Type() string {
|
|
return TaskTypeCancelation
|
|
}
|
|
|
|
func (t CancelationTask) Kind() hsm.TaskKind {
|
|
return hsm.TaskKindOutbound{Destination: t.EndpointName}
|
|
}
|
|
|
|
func (CancelationTask) Concurrent() bool {
|
|
return false
|
|
}
|
|
|
|
type CancelationTaskSerializer struct{}
|
|
|
|
func (CancelationTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
|
|
if kind, ok := kind.(hsm.TaskKindOutbound); ok {
|
|
return CancelationTask{EndpointName: kind.Destination}, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: expected outbound", hsm.ErrInvalidTaskKind)
|
|
}
|
|
|
|
func (CancelationTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type CancelationBackoffTask struct {
|
|
Deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = CancelationBackoffTask{}
|
|
|
|
func (CancelationBackoffTask) Type() string {
|
|
return TaskTypeCancelationBackoff
|
|
}
|
|
|
|
func (t CancelationBackoffTask) Kind() hsm.TaskKind {
|
|
return hsm.TaskKindTimer{Deadline: t.Deadline}
|
|
}
|
|
|
|
func (CancelationBackoffTask) Concurrent() bool {
|
|
return false
|
|
}
|
|
|
|
type CancelationBackoffTaskSerializer struct{}
|
|
|
|
func (CancelationBackoffTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
|
|
if kind, ok := kind.(hsm.TaskKindTimer); ok {
|
|
return CancelationBackoffTask{Deadline: kind.Deadline}, nil
|
|
}
|
|
return nil, fmt.Errorf("%w: expected timer", hsm.ErrInvalidTaskKind)
|
|
}
|
|
|
|
func (CancelationBackoffTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func RegisterTaskSerializers(reg *hsm.Registry) error {
|
|
if err := reg.RegisterTaskSerializer(TaskTypeTimeout, TimeoutTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeInvocation, InvocationTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeBackoff, BackoffTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeCancelation, CancelationTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeCancelationBackoff, CancelationBackoffTaskSerializer{}); err != nil { // nolint:revive
|
|
return err
|
|
}
|
|
return nil
|
|
}
|