diff --git a/service/history/historyEngine.go b/service/history/historyEngine.go index a5174a9816c..070bee91c74 100644 --- a/service/history/historyEngine.go +++ b/service/history/historyEngine.go @@ -301,7 +301,7 @@ func (e *historyEngineImpl) registerNamespaceStateChangeCallback() { e.shard.GetNamespaceRegistry().RegisterStateChangeCallback(e, func(ns *namespace.Namespace, deletedFromDb bool) { if e.shard.GetClusterMetadata().IsGlobalNamespaceEnabled() { - e.shard.UpdateHandoverNamespaces(ns, deletedFromDb) + e.shard.UpdateHandoverNamespace(ns, deletedFromDb) } if deletedFromDb { diff --git a/service/history/shard/context.go b/service/history/shard/context.go index c62f7be7346..c37415ff917 100644 --- a/service/history/shard/context.go +++ b/service/history/shard/context.go @@ -103,7 +103,7 @@ type ( // TODO: deprecate UpdateNamespaceNotificationVersion in v1.21 and remove // NamespaceNotificationVersion from shardInfo proto blob UpdateNamespaceNotificationVersion(namespaceNotificationVersion int64) error - UpdateHandoverNamespaces(ns *namespace.Namespace, deletedFromDb bool) + UpdateHandoverNamespace(ns *namespace.Namespace, deletedFromDb bool) AppendHistoryEvents(ctx context.Context, request *persistence.AppendHistoryNodesRequest, namespaceID namespace.ID, execution commonpb.WorkflowExecution) (int, error) diff --git a/service/history/shard/context_impl.go b/service/history/shard/context_impl.go index f6bba8746fb..f8d4a1d7a3e 100644 --- a/service/history/shard/context_impl.go +++ b/service/history/shard/context_impl.go @@ -578,11 +578,17 @@ func (s *ContextImpl) UpdateNamespaceNotificationVersion(namespaceNotificationVe return nil } -func (s *ContextImpl) UpdateHandoverNamespaces(ns *namespace.Namespace, deletedFromDb bool) { +func (s *ContextImpl) UpdateHandoverNamespace(ns *namespace.Namespace, deletedFromDb bool) { nsName := ns.Name() + // NOTE: replication state field won't be replicated and currently we only update a namespace + // to handover state from active cluster, so the second condition will always be true. Adding + // it here to be more safe in case above assumption no longer holds in the future. + isHandoverNamespace := ns.IsGlobalNamespace() && + ns.ActiveInCluster(s.GetClusterMetadata().GetCurrentClusterName()) && + ns.ReplicationState() == enums.REPLICATION_STATE_HANDOVER s.wLock() - if deletedFromDb { + if deletedFromDb || !isHandoverNamespace { delete(s.handoverNamespaces, ns.Name()) s.wUnlock() return @@ -595,23 +601,15 @@ func (s *ContextImpl) UpdateHandoverNamespaces(ns *namespace.Namespace, deletedF maxReplicationTaskID = pendingMaxReplicationTaskID } - // NOTE: replication state field won't be replicated and currently we only update a namespace - // to handover state from active cluster, so the second condition will always be true. Adding - // it here to be more safe in case above assumption no longer holds in the future. - if ns.IsGlobalNamespace() && - ns.ActiveInCluster(s.GetClusterMetadata().GetCurrentClusterName()) && - ns.ReplicationState() == enums.REPLICATION_STATE_HANDOVER { - - if handover, ok := s.handoverNamespaces[nsName]; ok { - if handover.NotificationVersion < ns.NotificationVersion() { - handover.NotificationVersion = ns.NotificationVersion() - handover.MaxReplicationTaskID = maxReplicationTaskID - } - } else { - s.handoverNamespaces[nsName] = &namespaceHandOverInfo{ - NotificationVersion: ns.NotificationVersion(), - MaxReplicationTaskID: maxReplicationTaskID, - } + if handover, ok := s.handoverNamespaces[nsName]; ok { + if handover.NotificationVersion < ns.NotificationVersion() { + handover.NotificationVersion = ns.NotificationVersion() + handover.MaxReplicationTaskID = maxReplicationTaskID + } + } else { + s.handoverNamespaces[nsName] = &namespaceHandOverInfo{ + NotificationVersion: ns.NotificationVersion(), + MaxReplicationTaskID: maxReplicationTaskID, } } diff --git a/service/history/shard/context_mock.go b/service/history/shard/context_mock.go index c133b4c7158..43f14e1ad41 100644 --- a/service/history/shard/context_mock.go +++ b/service/history/shard/context_mock.go @@ -703,16 +703,16 @@ func (mr *MockContextMockRecorder) UpdateFailoverLevel(category, failoverID, lev return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateFailoverLevel", reflect.TypeOf((*MockContext)(nil).UpdateFailoverLevel), category, failoverID, level) } -// UpdateHandoverNamespaces mocks base method. -func (m *MockContext) UpdateHandoverNamespaces(ns *namespace.Namespace, deletedFromDb bool) { +// UpdateHandoverNamespace mocks base method. +func (m *MockContext) UpdateHandoverNamespace(ns *namespace.Namespace, deletedFromDb bool) { m.ctrl.T.Helper() - m.ctrl.Call(m, "UpdateHandoverNamespaces", ns, deletedFromDb) + m.ctrl.Call(m, "UpdateHandoverNamespace", ns, deletedFromDb) } -// UpdateHandoverNamespaces indicates an expected call of UpdateHandoverNamespaces. -func (mr *MockContextMockRecorder) UpdateHandoverNamespaces(ns, deletedFromDb interface{}) *gomock.Call { +// UpdateHandoverNamespace indicates an expected call of UpdateHandoverNamespace. +func (mr *MockContextMockRecorder) UpdateHandoverNamespace(ns, deletedFromDb interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateHandoverNamespaces", reflect.TypeOf((*MockContext)(nil).UpdateHandoverNamespaces), ns, deletedFromDb) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateHandoverNamespace", reflect.TypeOf((*MockContext)(nil).UpdateHandoverNamespace), ns, deletedFromDb) } // UpdateNamespaceNotificationVersion mocks base method.