go.temporal.io/server/chasm/statemachine.go

79 LOC · 21 covered · 58 uncovered · 7 ranges · 16076 concepts · 5 introducers · 7977 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filescheduler.go ×1 · 11 introduced LOCscheduler.go ×1outbound_queue_factory.go ×8 · 144 introduced LOCoutbound_queue_factory.g…operation.go ×8 · 68 introduced LOCoperation.go ×8node_backend_mock.go ×3 · 4 introduced LOCnode_backend_mock.go ×3chasm_engine.go ×1 · 3 introduced LOCchasm_engine.go ×1data_interfaces.go ×1 · 6 introduced LOCdata_interfaces.go ×1chasm_engine.go ×1 · 3 introduced LOCchasm_engine.go ×1data_interfaces.go ×1 · 6 introduced LOCdata_interfaces.go ×1data_interfaces.go ×1 · 6 introduced LOCdata_interfaces.go ×1data_interfaces.go ×1 · 6 introduced LOCdata_interfaces.go ×1context.go ×1 · 3 introduced LOCcontext.go ×1nexus_events.go ×2 · 6 introduced LOCnexus_events.go ×2operation.pb.go ×1 · 2 introduced LOCoperation.pb.go ×1executors.go ×1 · 4 introduced LOCexecutors.go ×1context.go ×2 · 11 introduced LOCcontext.go ×2activity.go ×6 · 35 introduced LOCactivity.go ×6executions.pb.go ×1 · 4 introduced LOCexecutions.pb.go ×1terminate_started_activity, terminate_scheduled_activity, +1 · 0 introduced LOCterminate_started_activi…context_mock.go ×1 · 11 introduced LOCcontext_mock.go ×1operation_statemachine.go ×1 · 4 introduced LOCoperation_statemachine.g…activity_state.pb.go ×1 · 2 introduced LOCactivity_state.pb.go ×1activity_state.pb.go ×1 · 2 introduced LOCactivity_state.pb.go ×1activity_state.pb.go ×1 · 2 introduced LOCactivity_state.pb.go ×1error_on_completed_activity · 0 introduced LOCerror_on_completed_activ…activity.go ×2 · 7 introduced LOCactivity.go ×2cancellation_tasks.go ×1 · 3 introduced LOCcancellation_tasks.go ×1cancellation_statemachine.go ×1 · 12 introduced LOCcancellation_statemachin…registry.go ×2 · 11 introduced LOCregistry.go ×2executions.pb.go ×1 · 2 introduced LOCexecutions.pb.go ×1statemachine.go ×3 · 27 introduced LOCstatemachine.go ×3executions.pb.go ×1 · 2 introduced LOCexecutions.pb.go ×1context_mock.go ×1 · 2 introduced LOCcontext_mock.go ×1TestCancellationFullLifecycle · 0 introduced LOCTestCancellationFullLife…activity.go ×1 · 1 introduced LOCactivity.go ×1TestTransitionCancellationAttemptFailed · 0 introduced LOCTestTransitionCancellati…cancellation_statemachine.go ×1 · 7 introduced LOCcancellation_statemachin…activity_state.pb.go ×1 · 2 introduced LOCactivity_state.pb.go ×1statemachine.go ×1 · 3 introduced LOCstatemachine.go ×1activity_state.pb.go ×1 · 2 introduced LOCactivity_state.pb.go ×1activity_tasks.go ×1 · 2 introduced LOCactivity_tasks.go ×1activity_tasks.go ×1 · 1 introduced LOCactivity_tasks.go ×1cancellation_statemachine.go ×1 · 18 introduced LOCcancellation_statemachin…operation_statemachine.go ×1 · 12 introduced LOCoperation_statemachine.g…tags.go ×1 · 3 introduced LOCtags.go ×1TestTransitionCancellationSucceeded · 0 introduced LOCTestTransitionCancellati…activity_tasks.go ×3 · 8 introduced LOCactivity_tasks.go ×3cancellation_statemachine.go ×1 · 11 introduced LOCcancellation_statemachin…cancellation_statemachine.go ×1 · 8 introduced LOCcancellation_statemachin…operation.go ×1 · 3 introduced LOCoperation.go ×1operation_tasks.go ×1 · 3 introduced LOCoperation_tasks.go ×1operation_tasks.go ×1 · 3 introduced LOCoperation_tasks.go ×1tags.go ×1 · 3 introduced LOCtags.go ×1TestAPIPrioritiesOrdered, TestCallerTypeToPriorityMapping · 0 introduced LOCTestAPIPrioritiesOrdered…operation.go ×1 · 3 introduced LOCoperation.go ×1operation_tasks.go ×1 · 3 introduced LOCoperation_tasks.go ×1operation_tasks.go ×1 · 4 introduced LOCoperation_tasks.go ×1operation_tasks.go ×1 · 3 introduced LOCoperation_tasks.go ×1statemachine.go ×1 · 2 introduced LOCstatemachine.go ×1context_mock.go ×1 · 2 introduced LOCcontext_mock.go ×1operation.pb.go ×1 · 4 introduced LOCoperation.pb.go ×1context_mock.go ×1 · 2 introduced LOCcontext_mock.go ×1operation.pb.go ×2 · 92 introduced LOCoperation.pb.go ×2node_backend_mock.go ×1 · 2 introduced LOCnode_backend_mock.go ×1tags.go ×2 · 3 introduced LOCtags.go ×2setting_gen.go ×2 · 5 introduced LOCsetting_gen.go ×2statemachine.go ×2 · 3 introduced LOCstatemachine.go ×2context_mock.go ×1 · 3 introduced LOCcontext_mock.go ×1context_mock.go ×2 · 3 introduced LOCcontext_mock.go ×2statemachine.go ×2 · 6 introduced LOCstatemachine.go ×2message.pb.go ×2 · 44 introduced LOCmessage.pb.go ×2context_mock.go ×1 · 2 introduced LOCcontext_mock.go ×1statemachine.go ×1 · 3 introduced LOCstatemachine.go ×1statemachine.go ×1 · 7 introduced LOCstatemachine.go ×1error_on_canceled_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/error_on_canceled_activityerror_on_canceled_activi…error_on_completed_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/error_on_completed_activityerror_on_completed_activ…error_on_failed_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/error_on_failed_activityerror_on_failed_activityerror_on_timed_out_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/error_on_timed_out_activityerror_on_timed_out_activ…terminate_cancel-requested_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/terminate_cancel-requested_activityterminate_cancel-request…terminate_scheduled_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/terminate_scheduled_activityterminate_scheduled_acti…terminate_started_activity · introduced test · go.temporal.io/server/chasm/lib/activity/TestActivityTerminate/terminate_started_activityterminate_started_activi…paused_during_retry_backoff,_stale_StartedTime · introduced test · go.temporal.io/server/chasm/lib/activity/TestHandleCancellationRequestedDirectCancelMetrics/paused_during_retry_backoff,_stale_StartedTimepaused_during_retry_back…scheduled_during_retry_backoff,_stale_StartedTime · introduced test · go.temporal.io/server/chasm/lib/activity/TestHandleCancellationRequestedDirectCancelMetrics/scheduled_during_retry_backoff,_stale_StartedTimescheduled_during_retry_b…successful_transition_from_scheduled · introduced test · go.temporal.io/server/chasm/lib/activity/TestHandleStarted/successful_transition_from_scheduledsuccessful_transition_fr…legacy_task_valid_while_activity_stamp_still_zero · introduced test · go.temporal.io/server/chasm/lib/activity/TestScheduleToCloseTimeoutTaskValidateStamp/legacy_task_valid_while_activity_stamp_still_zerolegacy_task_valid_while_…legacy_zero-stamp_task_invalid_after_stamp_bumped · introduced test · go.temporal.io/server/chasm/lib/activity/TestScheduleToCloseTimeoutTaskValidateStamp/legacy_zero-stamp_task_invalid_after_stamp_bumpedlegacy_zero-stamp_task_i…matching_stamp_valid · introduced test · go.temporal.io/server/chasm/lib/activity/TestScheduleToCloseTimeoutTaskValidateStamp/matching_stamp_validmatching_stamp_validstale_non-zero_stamp_invalid · introduced test · go.temporal.io/server/chasm/lib/activity/TestScheduleToCloseTimeoutTaskValidateStamp/stale_non-zero_stamp_invalidstale_non-zero_stamp_inv…invalid_when_scheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestBackoffTaskHandler_Validate/invalid_when_scheduledinvalid_when_scheduledTestCancellationBackoffTaskHandler_Execute · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestCancellationBackoffTaskHandler_ExecuteTestCancellationBackoffT…TestCancellationFullLifecycle · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestCancellationFullLifecycleTestCancellationFullLife…invalid_when_started · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestInvocationTaskHandler_Validate/invalid_when_startedinvalid_when_startedTestOperation_BuildExecutionInfo_ReturnsIsolatedSearchAttributes · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestOperation_BuildExecutionInfo_ReturnsIsolatedSearchAttributesTestOperation_BuildExecu…invalid_when_succeeded · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToCloseTimeoutTaskHandler_Validate/invalid_when_succeededinvalid_when_succeededinvalid_when_timed_out · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToCloseTimeoutTaskHandler_Validate/invalid_when_timed_outinvalid_when_timed_outvalid_when_scheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToCloseTimeoutTaskHandler_Validate/valid_when_scheduledvalid_when_scheduledvalid_when_started · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToCloseTimeoutTaskHandler_Validate/valid_when_startedvalid_when_startedinvalid_when_started · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToStartTimeoutTaskHandler_Validate/invalid_when_startedinvalid_when_startedinvalid_when_succeeded · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToStartTimeoutTaskHandler_Validate/invalid_when_succeededinvalid_when_succeededinvalid_when_timed_out · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToStartTimeoutTaskHandler_Validate/invalid_when_timed_outinvalid_when_timed_outvalid_when_backing_off · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToStartTimeoutTaskHandler_Validate/valid_when_backing_offvalid_when_backing_offvalid_when_scheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestScheduleToStartTimeoutTaskHandler_Validate/valid_when_scheduledvalid_when_scheduledinvalid_when_backing_off · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestStartToCloseTimeoutTaskHandler_Validate/invalid_when_backing_offinvalid_when_backing_offinvalid_when_scheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestStartToCloseTimeoutTaskHandler_Validate/invalid_when_scheduledinvalid_when_scheduledinvalid_when_succeeded · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestStartToCloseTimeoutTaskHandler_Validate/invalid_when_succeededinvalid_when_succeededinvalid_when_timed_out · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestStartToCloseTimeoutTaskHandler_Validate/invalid_when_timed_outinvalid_when_timed_outvalid_when_started · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestStartToCloseTimeoutTaskHandler_Validate/valid_when_startedvalid_when_startedTestTransitionCancellationAttemptFailed · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionCancellationAttemptFailedTestTransitionCancellati…TestTransitionCancellationFailed · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionCancellationFailedTestTransitionCancellati…TestTransitionCancellationRescheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionCancellationRescheduledTestTransitionCancellati…TestTransitionCancellationScheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionCancellationScheduledTestTransitionCancellati…TestTransitionCancellationSucceeded · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionCancellationSucceededTestTransitionCancellati…TestTransitionRescheduled · introduced test · go.temporal.io/server/chasm/lib/nexusoperation/TestTransitionRescheduledTestTransitionReschedule…TestHandleNexusCompletion_Idempotent · introduced test · go.temporal.io/server/chasm/lib/scheduler/TestHandleNexusCompletion_IdempotentTestHandleNexusCompletio…TestCancelRequestCompletedEventDefinitionApply · introduced test · go.temporal.io/server/chasm/lib/workflow/TestCancelRequestCompletedEventDefinitionApplyTestCancelRequestComplet…started_double_apply_fails · introduced test · go.temporal.io/server/chasm/lib/workflow/TestCherryPick/started_double_apply_failsstarted_double_apply_fai…TestProcessBackoffTask · introduced test · go.temporal.io/server/components/callbacks/TestProcessBackoffTaskTestProcessBackoffTaskAppendHistoryTimeoutError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/AppendHistoryTimeoutErrorAppendHistoryTimeoutErro…ConditionFailedError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/ConditionFailedErrorConditionFailedErrorCurrentWorkflowConditionFailedError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/CurrentWorkflowConditionFailedErrorCurrentWorkflowCondition…TimeoutError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/TimeoutErrorTimeoutErrorTransactionSizeLimitError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/TransactionSizeLimitErrorTransactionSizeLimitErro…WorkflowConditionFailedError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/PersistenceErrors/WorkflowConditionFailedErrorWorkflowConditionFailedE…WrappedPersistenceError · introduced test · go.temporal.io/server/service/history/TestChasmEngineSuite/TestConvertError/WrappedErrors/WrappedPersistenceErrorWrappedPersistenceErrorTestOutboundQueueFactory_ChasmTaskGroupWiring · introduced test · go.temporal.io/server/service/history/TestOutboundQueueFactory_ChasmTaskGroupWiringTestOutboundQueueFactory…TestAPIPrioritiesOrdered · introduced test · go.temporal.io/server/service/history/configs/TestQuotasSuite/TestAPIPrioritiesOrderedTestAPIPrioritiesOrderedTestCallerTypeToPriorityMapping · introduced test · go.temporal.io/server/service/history/configs/TestQuotasSuite/TestCallerTypeToPriorityMappingTestCallerTypeToPriority…Focused file · go.temporal.io/server/chasm/statemachine.go · 79 LOCchasm/statemachine.go

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package chasm
2
3 import (
4 "fmt"
5 "slices"
6
7 "go.opentelemetry.io/otel/attribute"
8 "go.opentelemetry.io/otel/trace"
9 "go.temporal.io/api/serviceerror"
10 "go.temporal.io/server/common/telemetry"
11 )
12
13 // ErrInvalidTransition is returned from [Transition.Apply] on an invalid state transition.
14 var ErrInvalidTransition = serviceerror.NewFailedPrecondition("invalid transition")
15
16 // A StateMachine is anything that can get and set a comparable state S and re-generate tasks based on current state.
17 // It is meant to be used with [Transition] objects to safely transition their state on a given event.
18 type StateMachine[S comparable] interface {
19 StateMachineState() S
20 SetStateMachineState(S)
21 }
22
23 // Transition represents a state machine transition for a machine of type SM with state S and event E.
24 type Transition[S comparable, SM StateMachine[S], E any] struct {
25 // Source states that are valid for this transition.
26 Sources []S
27 // Destination state to transition to.
28 Destination S
29 // Function to apply the transition. Mutate the state machine object here and schedule tasks.
30 apply func(SM, MutableContext, E) error
31 }
32
33 // NewTransition creates a new [Transition] from the given source states to a destination state for a given event.
34 // The apply function is called after verifying the transition is possible but before setting the destination state,
35 // so it can inspect the current (source) state.
36 > func NewTransition[S comparable, SM StateMachine[S], E any](src []S, dst S, apply func(SM, MutableContext, E) error) Transition[S, SM, E] { statemachine.go ×1
37 > return Transition[S, SM, E]{
38 > Sources: src,
39 > Destination: dst,
40 > apply: apply,
41 > }
42 > }
43
44 // Possible returns a boolean indicating whether the transition is possible for the current state.
45 > func (t Transition[S, SM, E]) Possible(sm SM) bool { statemachine.go ×1
46 > return slices.Contains(t.Sources, sm.StateMachineState())
47 > }
48
49 // Apply applies a transition event to the given state machine changing the state machine's state to the transition's
50 // Destination on success. The apply function is called before the state is changed, so it can inspect the current
51 // (source) state.
52 > func (t Transition[S, SM, E]) Apply(sm SM, ctx MutableContext, event E) (retErr error) { statemachine.go ×2
53 > prevState := sm.StateMachineState()
54 >
55 > // Defer to always emit the transition telemetry event.
56 > if telemetry.DebugMode() {
57 defer func() {
58 attrs := []attribute.KeyValue{
59 attribute.String("chasm.transition.source", fmt.Sprintf("%v", prevState)),
60 attribute.String("chasm.transition.destination", fmt.Sprintf("%v", t.Destination)),
61 }
62 if retErr != nil {
63 attrs = append(attrs, attribute.String("chasm.transition.error", retErr.Error()))
64 }
65 span := trace.SpanFromContext(ctx.goContext())
66 span.AddEvent("chasm.transition", trace.WithAttributes(attrs...))
67 }()
68 }
69
70 > if !t.Possible(sm) { statemachine.go ×2
71 > return fmt.Errorf("%w from %v", ErrInvalidTransition, prevState) statemachine.go ×1
72 > }
73
74 > if err := t.apply(sm, ctx, event); err != nil { statemachine.go ×2
75 return err
76 }
77 > sm.SetStateMachineState(t.Destination) statemachine.go ×2
78 > return nil
79 }