mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
Reinstate what was reverted in https://github.com/temporalio/temporal/pull/7024 ## What changed? Add deletion calls for Nexus operation nodes once they reach a terminal state (Completed, Failed, Canceled, Timed Out). Previously, terminal operations lingered in the state machine, potentially causing confusion and wasting resources. In addition, this PR addresses previously existing TODOs related to node cleanup. ### Key Changes - **Automatic Node Deletion on Terminal States:** Operations are now removed from the state machine immediately after they complete, fail, cancel, or time out. - **Handler Updates:** The command handlers (schedule and cancel) now reflect the new terminal state deletion. Attempts to cancel an operation that has already reached a terminal state will produce a clear, non-retryable error. - **Tests for Terminal State Deletion:** Introduced two new test suites: - `TestTerminalStatesDeletion` to verify that applying terminal events removes the operation node as expected. - `TestOperationNodeDeletionOnTerminalEvents` to ensure no further modifications (like cancelation) can be made after an operation is terminal. ### Areas for Review Focus 1. **Deletion Semantics:** Verify that node removal is triggered correctly for all terminal states and that no non-terminal states inadvertently trigger deletion. 2. **Handler Consistency:** Ensure that the updated schedule/cancel handlers function correctly when dealing with non-existent or already terminated operations. 3. **Error Handling:** Confirm that attempts to modify a deleted operation produce the intended error behavior. 4. **Test Coverage:** Review the newly added test suites to ensure all terminal states and edge cases are thoroughly tested. ## Why? - Prevent confusion and wasted resources by removing irrelevant operation nodes. - Prevent customers from experiencing workflow task failures prematurely. ## How did you test it? - **New Test Suites:** Added `TestTerminalStatesDeletion` and `TestOperationNodeDeletionOnTerminalEvents` to specifically target and validate the new deletion behavior. - **Existing Tests:** All pre-existing tests have been re-run to ensure no regressions. ## Potential risks - **Assumptions About Completed Operations:** If any internal or external code implicitly relied on the presence of completed operations, its assumptions might break. Such usage was never officially supported. ## Documentation No external or user-facing documentation changes are required since this feature matches the intended design. ## Is hotfix candidate? No. This change is an enhancement to internal logic and state management, not a fix for a critical bug, so it should follow the normal release process. --------- Co-authored-by: Roey Berman <[email protected]> Co-authored-by: Roey Berman <[email protected]> ## What changed? <!-- Describe what has changed in this PR --> ## Why? <!-- Tell your future self why have you made these changes --> ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> ## 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/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> Co-authored-by: Roey Berman <[email protected]> Co-authored-by: Roey Berman <[email protected]>
240 lines
8.7 KiB
Go
240 lines
8.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 nexusoperations_test
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
historypb "go.temporal.io/api/history/v1"
|
|
"go.temporal.io/server/components/nexusoperations"
|
|
"go.temporal.io/server/service/history/hsm"
|
|
"go.temporal.io/server/service/history/hsm/hsmtest"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func TestCherryPick(t *testing.T) {
|
|
setup := func(t *testing.T) (*hsm.Node, nexusoperations.Operation, int64) {
|
|
node := newOperationNode(t, &hsmtest.NodeBackend{}, mustNewScheduledEvent(time.Now(), time.Hour))
|
|
op, err := hsm.MachineData[nexusoperations.Operation](node)
|
|
require.NoError(t, err)
|
|
eventID, err := hsm.EventIDFromToken(op.ScheduledEventToken)
|
|
require.NoError(t, err)
|
|
return node, op, eventID
|
|
}
|
|
|
|
t.Run("NoRequestID", func(t *testing.T) {
|
|
node, _, eventID := setup(t)
|
|
err := nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID,
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.NoError(t, err)
|
|
})
|
|
t.Run("ValidRequestID", func(t *testing.T) {
|
|
node, op, eventID := setup(t)
|
|
err := nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID,
|
|
RequestId: op.RequestId,
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.NoError(t, err)
|
|
})
|
|
t.Run("InvalidRequestID", func(t *testing.T) {
|
|
node, _, eventID := setup(t)
|
|
err := nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID,
|
|
RequestId: "invalid",
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.ErrorIs(t, err, hsm.ErrNotCherryPickable)
|
|
})
|
|
t.Run("InvalidScheduledEventID", func(t *testing.T) {
|
|
node, _, eventID := setup(t)
|
|
err := nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID + 1,
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.ErrorIs(t, err, hsm.ErrStateMachineNotFound)
|
|
})
|
|
t.Run("DoubleApply", func(t *testing.T) {
|
|
node, _, eventID := setup(t)
|
|
err := nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID,
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.NoError(t, err)
|
|
err = nexusoperations.StartedEventDefinition{}.CherryPick(node.Parent, &historypb.HistoryEvent{
|
|
Attributes: &historypb.HistoryEvent_NexusOperationStartedEventAttributes{
|
|
NexusOperationStartedEventAttributes: &historypb.NexusOperationStartedEventAttributes{
|
|
ScheduledEventId: eventID,
|
|
},
|
|
},
|
|
},
|
|
nil,
|
|
)
|
|
require.ErrorIs(t, err, hsm.ErrInvalidTransition)
|
|
})
|
|
t.Run("shouldExcludeNexusEvents", func(t *testing.T) {
|
|
node, _, _ := setup(t)
|
|
nexusOperations := []hsm.EventDefinition{
|
|
nexusoperations.ScheduledEventDefinition{},
|
|
nexusoperations.StartedEventDefinition{},
|
|
nexusoperations.CompletedEventDefinition{},
|
|
nexusoperations.CancelRequestedEventDefinition{},
|
|
nexusoperations.CanceledEventDefinition{},
|
|
nexusoperations.FailedEventDefinition{},
|
|
nexusoperations.TimedOutEventDefinition{},
|
|
}
|
|
|
|
excludeNexusOperation := map[enumspb.ResetReapplyExcludeType]struct{}{enumspb.RESET_REAPPLY_EXCLUDE_TYPE_NEXUS: {}}
|
|
for _, nexusOperation := range nexusOperations {
|
|
err := nexusOperation.CherryPick(node.Parent, &historypb.HistoryEvent{}, excludeNexusOperation)
|
|
require.ErrorIs(t, err, hsm.ErrNotCherryPickable, "%T should not be cherrypickable when shouldExcludeNexusEvent=true", nexusOperation)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTerminalStatesDeletion(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
def hsm.EventDefinition
|
|
attributes interface{}
|
|
}{
|
|
{
|
|
name: "CompletedDeletesStateMachine",
|
|
def: nexusoperations.CompletedEventDefinition{},
|
|
attributes: &historypb.NexusOperationCompletedEventAttributes{
|
|
ScheduledEventId: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "FailedDeletesStateMachine",
|
|
def: nexusoperations.FailedEventDefinition{},
|
|
attributes: &historypb.NexusOperationFailedEventAttributes{
|
|
ScheduledEventId: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "CanceledDeletesStateMachine",
|
|
def: nexusoperations.CanceledEventDefinition{},
|
|
attributes: &historypb.NexusOperationCanceledEventAttributes{
|
|
ScheduledEventId: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "TimedOutDeletesStateMachine",
|
|
def: nexusoperations.TimedOutEventDefinition{},
|
|
attributes: &historypb.NexusOperationTimedOutEventAttributes{
|
|
ScheduledEventId: 0,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
node := newOperationNode(t, &hsmtest.NodeBackend{}, mustNewScheduledEvent(time.Now(), time.Hour))
|
|
op, err := hsm.MachineData[nexusoperations.Operation](node)
|
|
require.NoError(t, err)
|
|
eventID, err := hsm.EventIDFromToken(op.ScheduledEventToken)
|
|
require.NoError(t, err)
|
|
|
|
// Update the event ID in attributes
|
|
switch a := tc.attributes.(type) {
|
|
case *historypb.NexusOperationCompletedEventAttributes:
|
|
a.ScheduledEventId = eventID
|
|
case *historypb.NexusOperationFailedEventAttributes:
|
|
a.ScheduledEventId = eventID
|
|
case *historypb.NexusOperationCanceledEventAttributes:
|
|
a.ScheduledEventId = eventID
|
|
case *historypb.NexusOperationTimedOutEventAttributes:
|
|
a.ScheduledEventId = eventID
|
|
}
|
|
|
|
event := &historypb.HistoryEvent{
|
|
EventTime: timestamppb.Now(),
|
|
}
|
|
|
|
switch d := tc.def.(type) {
|
|
case nexusoperations.CompletedEventDefinition:
|
|
event.EventType = d.Type()
|
|
event.Attributes = &historypb.HistoryEvent_NexusOperationCompletedEventAttributes{
|
|
NexusOperationCompletedEventAttributes: tc.attributes.(*historypb.NexusOperationCompletedEventAttributes),
|
|
}
|
|
case nexusoperations.FailedEventDefinition:
|
|
event.EventType = d.Type()
|
|
event.Attributes = &historypb.HistoryEvent_NexusOperationFailedEventAttributes{
|
|
NexusOperationFailedEventAttributes: tc.attributes.(*historypb.NexusOperationFailedEventAttributes),
|
|
}
|
|
case nexusoperations.CanceledEventDefinition:
|
|
event.EventType = d.Type()
|
|
event.Attributes = &historypb.HistoryEvent_NexusOperationCanceledEventAttributes{
|
|
NexusOperationCanceledEventAttributes: tc.attributes.(*historypb.NexusOperationCanceledEventAttributes),
|
|
}
|
|
case nexusoperations.TimedOutEventDefinition:
|
|
event.EventType = d.Type()
|
|
event.Attributes = &historypb.HistoryEvent_NexusOperationTimedOutEventAttributes{
|
|
NexusOperationTimedOutEventAttributes: tc.attributes.(*historypb.NexusOperationTimedOutEventAttributes),
|
|
}
|
|
default:
|
|
t.Fatalf("unknown event definition type: %T", tc.def)
|
|
}
|
|
|
|
err = tc.def.Apply(node.Parent, event)
|
|
require.NoError(t, err)
|
|
|
|
coll := nexusoperations.MachineCollection(node.Parent)
|
|
_, err = coll.Node(strconv.FormatInt(eventID, 10))
|
|
require.ErrorIs(t, err, hsm.ErrStateMachineNotFound)
|
|
})
|
|
}
|
|
}
|