@@ -549,13 +549,8 @@ func (s *ContextImpl) UpdateHandoverNamespaces(namespaces []*namespace.Namespace
549549func (s * ContextImpl ) AddTasks (
550550 request * persistence.AddHistoryTasksRequest ,
551551) error {
552- if err := s .errorByState (); err != nil {
553- return err
554- }
555-
556- namespaceID := namespace .ID (request .NamespaceID )
557-
558552 // do not try to get namespace cache within shard lock
553+ namespaceID := namespace .ID (request .NamespaceID )
559554 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespaceID )
560555 if err != nil {
561556 return err
@@ -564,20 +559,19 @@ func (s *ContextImpl) AddTasks(
564559 s .wLock ()
565560 defer s .wUnlock ()
566561
562+ if err := s .errorByStateLocked (); err != nil {
563+ return err
564+ }
565+
567566 return s .addTasksLocked (request , namespaceEntry )
568567}
569568
570569func (s * ContextImpl ) CreateWorkflowExecution (
571570 request * persistence.CreateWorkflowExecutionRequest ,
572571) (* persistence.CreateWorkflowExecutionResponse , error ) {
573- if err := s .errorByState (); err != nil {
574- return nil , err
575- }
576-
572+ // do not try to get namespace cache within shard lock
577573 namespaceID := namespace .ID (request .NewWorkflowSnapshot .ExecutionInfo .NamespaceId )
578574 workflowID := request .NewWorkflowSnapshot .ExecutionInfo .WorkflowId
579-
580- // do not try to get namespace cache within shard lock
581575 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespaceID )
582576 if err != nil {
583577 return nil , err
@@ -586,6 +580,10 @@ func (s *ContextImpl) CreateWorkflowExecution(
586580 s .wLock ()
587581 defer s .wUnlock ()
588582
583+ if err := s .errorByStateLocked (); err != nil {
584+ return nil , err
585+ }
586+
589587 transferMaxReadLevel := int64 (0 )
590588 if err := s .allocateTaskIDsLocked (
591589 namespaceEntry ,
@@ -608,14 +606,9 @@ func (s *ContextImpl) CreateWorkflowExecution(
608606func (s * ContextImpl ) UpdateWorkflowExecution (
609607 request * persistence.UpdateWorkflowExecutionRequest ,
610608) (* persistence.UpdateWorkflowExecutionResponse , error ) {
611- if err := s .errorByState (); err != nil {
612- return nil , err
613- }
614-
609+ // do not try to get namespace cache within shard lock
615610 namespaceID := namespace .ID (request .UpdateWorkflowMutation .ExecutionInfo .NamespaceId )
616611 workflowID := request .UpdateWorkflowMutation .ExecutionInfo .WorkflowId
617-
618- // do not try to get namespace cache within shard lock
619612 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespaceID )
620613 if err != nil {
621614 return nil , err
@@ -624,6 +617,10 @@ func (s *ContextImpl) UpdateWorkflowExecution(
624617 s .wLock ()
625618 defer s .wUnlock ()
626619
620+ if err := s .errorByStateLocked (); err != nil {
621+ return nil , err
622+ }
623+
627624 transferMaxReadLevel := int64 (0 )
628625 if err := s .allocateTaskIDsLocked (
629626 namespaceEntry ,
@@ -656,14 +653,9 @@ func (s *ContextImpl) UpdateWorkflowExecution(
656653func (s * ContextImpl ) ConflictResolveWorkflowExecution (
657654 request * persistence.ConflictResolveWorkflowExecutionRequest ,
658655) (* persistence.ConflictResolveWorkflowExecutionResponse , error ) {
659- if err := s .errorByState (); err != nil {
660- return nil , err
661- }
662-
656+ // do not try to get namespace cache within shard lock
663657 namespaceID := namespace .ID (request .ResetWorkflowSnapshot .ExecutionInfo .NamespaceId )
664658 workflowID := request .ResetWorkflowSnapshot .ExecutionInfo .WorkflowId
665-
666- // do not try to get namespace cache within shard lock
667659 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespaceID )
668660 if err != nil {
669661 return nil , err
@@ -672,6 +664,10 @@ func (s *ContextImpl) ConflictResolveWorkflowExecution(
672664 s .wLock ()
673665 defer s .wUnlock ()
674666
667+ if err := s .errorByStateLocked (); err != nil {
668+ return nil , err
669+ }
670+
675671 transferMaxReadLevel := int64 (0 )
676672 if request .CurrentWorkflowMutation != nil {
677673 if err := s .allocateTaskIDsLocked (
@@ -714,14 +710,9 @@ func (s *ContextImpl) ConflictResolveWorkflowExecution(
714710func (s * ContextImpl ) SetWorkflowExecution (
715711 request * persistence.SetWorkflowExecutionRequest ,
716712) (* persistence.SetWorkflowExecutionResponse , error ) {
717- if err := s .errorByState (); err != nil {
718- return nil , err
719- }
720-
713+ // do not try to get namespace cache within shard lock
721714 namespaceID := namespace .ID (request .SetWorkflowSnapshot .ExecutionInfo .NamespaceId )
722715 workflowID := request .SetWorkflowSnapshot .ExecutionInfo .WorkflowId
723-
724- // do not try to get namespace cache within shard lock
725716 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespaceID )
726717 if err != nil {
727718 return nil , err
@@ -730,6 +721,10 @@ func (s *ContextImpl) SetWorkflowExecution(
730721 s .wLock ()
731722 defer s .wUnlock ()
732723
724+ if err := s .errorByStateLocked (); err != nil {
725+ return nil , err
726+ }
727+
733728 transferMaxReadLevel := int64 (0 )
734729 if err := s .allocateTaskIDsLocked (
735730 namespaceEntry ,
@@ -777,9 +772,12 @@ func (s *ContextImpl) AppendHistoryEvents(
777772 namespaceID namespace.ID ,
778773 execution commonpb.WorkflowExecution ,
779774) (int , error ) {
780- if err := s .errorByState (); err != nil {
775+ s .rLock ()
776+ if err := s .errorByStateLocked (); err != nil {
777+ s .rUnlock ()
781778 return 0 , err
782779 }
780+ s .rUnlock ()
783781
784782 request .ShardID = s .shardID
785783
@@ -830,10 +828,6 @@ func (s *ContextImpl) DeleteWorkflowExecution(
830828 // The history branch won't be accessible (because mutable state is deleted) and special garbage collection workflow will delete it eventually.
831829 // Step 4 shouldn't be done earlier because if this func fails after it, workflow execution will be accessible but won't have history (inconsistent state).
832830
833- if err := s .errorByState (); err != nil {
834- return err
835- }
836-
837831 // Do not get namespace cache within shard lock.
838832 namespaceEntry , err := s .GetNamespaceRegistry ().GetNamespaceByID (namespace .ID (key .NamespaceID ))
839833 deleteVisibilityRecord := true
@@ -850,6 +844,10 @@ func (s *ContextImpl) DeleteWorkflowExecution(
850844 s .wLock ()
851845 defer s .wUnlock ()
852846
847+ if err := s .errorByStateLocked (); err != nil {
848+ return err
849+ }
850+
853851 // Step 1. Delete visibility.
854852 if deleteVisibilityRecord {
855853 addTasksRequest := & persistence.AddHistoryTasksRequest {
@@ -948,12 +946,6 @@ func (s *ContextImpl) getRangeIDLocked() int64 {
948946 return s .shardInfo .GetRangeId ()
949947}
950948
951- func (s * ContextImpl ) errorByState () error {
952- s .rLock ()
953- defer s .rUnlock ()
954- return s .errorByStateLocked ()
955- }
956-
957949func (s * ContextImpl ) errorByStateLocked () error {
958950 switch s .state {
959951 case contextStateInitialized , contextStateAcquiring :
0 commit comments