Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ErrStateMachineNotFound handling in HSM state replication #7032

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
39 changes: 35 additions & 4 deletions service/history/ndc/hsm_state_replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,41 @@ func (r *HSMStateReplicatorImpl) syncHSMNode(
incomingNodePath := incomingNode.Path()
currentNode, err := currentHSM.Child(incomingNodePath)
if err != nil {
// 1. Already done history resend if needed before,
// and node creation today always associated with an event
// 2. Node deletion is not supported right now.
// Based on 1 and 2, node should always be found here.
// The node may not be found if:
// State machine was deleted in terminal state.
// Both state machine creation and deletion are always associated with an event, so any missing state
// machine must have a corresponding event in history.
if errors.Is(err, hsm.ErrStateMachineNotFound) {
notFoundErr := err
// Get the last items from both version histories
currentVersionHistory, err := versionhistory.GetCurrentVersionHistory(
mutableState.GetExecutionInfo().GetVersionHistories(),
)
if err != nil {
return err
}
lastLocalItem, err := versionhistory.GetLastVersionHistoryItem(currentVersionHistory)
if err != nil {
return err
}
lastIncomingItem, err := versionhistory.GetLastVersionHistoryItem(request.EventVersionHistory)
if err != nil {
return err
}

// Only accept "not found" if our version history is ahead
if versionhistory.CompareVersionHistoryItem(lastLocalItem, lastIncomingItem) > 0 {
r.logger.Debug("State machine not found - likely deleted in terminal state",
tag.WorkflowNamespaceID(mutableState.GetExecutionInfo().NamespaceId),
tag.WorkflowID(mutableState.GetExecutionInfo().WorkflowId),
tag.WorkflowRunID(mutableState.GetExecutionInfo().OriginalExecutionRunId),
)
return nil
}

// Otherwise, we might be missing events
return notFoundErr
}
return err
}

Expand Down
41 changes: 41 additions & 0 deletions service/history/ndc/hsm_state_replicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,47 @@ func (s *hsmStateReplicatorSuite) TestSyncHSM_IncomingStateNewer_WorkflowClosed(
s.NoError(err)
}

func (s *hsmStateReplicatorSuite) TestSyncHSM_StateMachineNotFound() {
persistedState := s.buildWorkflowMutableState()
// Remove the child1 state machine so it doesn't exist
delete(persistedState.ExecutionInfo.SubStateMachinesByType[s.stateMachineDef.Type()].MachinesById, "child1")

s.mockExecutionMgr.EXPECT().GetWorkflowExecution(gomock.Any(), &persistence.GetWorkflowExecutionRequest{
ShardID: s.mockShard.GetShardID(),
NamespaceID: s.workflowKey.NamespaceID,
WorkflowID: s.workflowKey.WorkflowID,
RunID: s.workflowKey.RunID,
}).Return(&persistence.GetWorkflowExecutionResponse{
State: persistedState,
DBRecordVersion: 777,
}, nil).Times(1)

err := s.nDCHSMStateReplicator.SyncHSMState(context.Background(), &shard.SyncHSMRequest{
WorkflowKey: s.workflowKey,
EventVersionHistory: persistedState.ExecutionInfo.VersionHistories.Histories[0],
StateMachineNode: &persistencespb.StateMachineNode{
Children: map[string]*persistencespb.StateMachineMap{
s.stateMachineDef.Type(): {
MachinesById: map[string]*persistencespb.StateMachineNode{
"child1": {
Data: []byte(hsmtest.State3),
InitialVersionedTransition: &persistencespb.VersionedTransition{
NamespaceFailoverVersion: s.namespaceEntry.FailoverVersion(),
},
LastUpdateVersionedTransition: &persistencespb.VersionedTransition{
NamespaceFailoverVersion: s.namespaceEntry.FailoverVersion() + 100,
},
TransitionCount: 50,
},
},
},
},
},
})

s.NoError(err) // Expect no error as we should gracefully handle missing state machines
}

func (s *hsmStateReplicatorSuite) buildWorkflowMutableState() *persistencespb.WorkflowMutableState {

info := &persistencespb.WorkflowExecutionInfo{
Expand Down
Loading