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

Remove deleted namespace from registry #2567

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions common/namespace/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ func (r *registry) refreshNamespaces(ctx context.Context) error {

var token []byte
request := &persistence.ListNamespacesRequest{PageSize: CacheRefreshPageSize}
var namespaces Namespaces
var namespacesDb Namespaces
namespaceIDsDb := make(map[ID]struct{})
continuePage := true

for continuePage {
alexshtin marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -397,30 +398,33 @@ func (r *registry) refreshNamespaces(ctx context.Context) error {
return err
}
token = response.NextPageToken
for _, namespace := range response.Namespaces {
namespaces = append(namespaces, FromPersistentState(namespace))
for _, namespaceDb := range response.Namespaces {
namespacesDb = append(namespacesDb, FromPersistentState(namespaceDb))
namespaceIDsDb[ID(namespaceDb.Namespace.Info.Id)] = struct{}{}
}
continuePage = len(token) != 0
}

// we mush apply the namespace change by order
// since history shard have to update the shard info
// with namespace change version.
sort.Sort(namespaces)

var oldEntries []*Namespace
var newEntries []*Namespace
sort.Sort(namespacesDb)

// make a copy of the existing namespace cache, so we can calculate diff and do compare and swap
// Make a copy of the existing namespace cache (excluding deleted), so we can calculate diff and do "compare and swap".
newCacheNameToID := cache.New(cacheMaxSize, &cacheOpts)
newCacheByID := cache.New(cacheMaxSize, &cacheOpts)
for _, namespace := range r.getAllNamespace() {
if _, namespaceExistsDb := namespaceIDsDb[namespace.ID()]; !namespaceExistsDb {
continue
}
newCacheNameToID.Put(Name(namespace.info.Name), ID(namespace.info.Id))
newCacheByID.Put(ID(namespace.info.Id), namespace)
}

var oldEntries []*Namespace
var newEntries []*Namespace
UpdateLoop:
for _, namespace := range namespaces {
for _, namespace := range namespacesDb {
if namespace.notificationVersion >= namespaceNotificationVersion {
// this guarantee that namespace change events before the
// namespaceNotificationVersion is loaded into the cache.
Expand Down
99 changes: 99 additions & 0 deletions common/namespace/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,105 @@ func (s *registrySuite) TestGetTriggerListAndUpdateCache_ConcurrentAccess() {
waitGroup.Wait()
}

func (s *registrySuite) TestRemoveDeletedNamespace() {
namespaceNotificationVersion := int64(0)
namespaceRecord1 := &persistence.GetNamespaceResponse{
Namespace: &persistencespb.NamespaceDetail{
Info: &persistencespb.NamespaceInfo{
Id: namespace.NewID().String(),
Name: "some random namespace name",
Data: make(map[string]string)},
Config: &persistencespb.NamespaceConfig{
Retention: timestamp.DurationFromDays(1),
BadBinaries: &namespacepb.BadBinaries{
Binaries: map[string]*namespacepb.BadBinaryInfo{},
}},
ReplicationConfig: &persistencespb.NamespaceReplicationConfig{
ActiveClusterName: cluster.TestCurrentClusterName,
Clusters: []string{
cluster.TestCurrentClusterName,
cluster.TestAlternativeClusterName,
},
},
ConfigVersion: 10,
FailoverVersion: 11,
FailoverNotificationVersion: 0,
},
NotificationVersion: namespaceNotificationVersion,
}
namespaceNotificationVersion++

namespaceRecord2 := &persistence.GetNamespaceResponse{
Namespace: &persistencespb.NamespaceDetail{
Info: &persistencespb.NamespaceInfo{
Id: namespace.NewID().String(),
Name: "another random namespace name",
Data: make(map[string]string)},
Config: &persistencespb.NamespaceConfig{
Retention: timestamp.DurationFromDays(2),
BadBinaries: &namespacepb.BadBinaries{
Binaries: map[string]*namespacepb.BadBinaryInfo{},
}},
ReplicationConfig: &persistencespb.NamespaceReplicationConfig{
ActiveClusterName: cluster.TestAlternativeClusterName,
Clusters: []string{
cluster.TestCurrentClusterName,
cluster.TestAlternativeClusterName,
},
},
ConfigVersion: 20,
FailoverVersion: 21,
FailoverNotificationVersion: 0,
},
NotificationVersion: namespaceNotificationVersion,
}
namespaceNotificationVersion++

s.regPersistence.EXPECT().GetMetadata().Return(
&persistence.GetMetadataResponse{
NotificationVersion: namespaceNotificationVersion,
}, nil)
s.regPersistence.EXPECT().ListNamespaces(&persistence.ListNamespacesRequest{
PageSize: namespace.CacheRefreshPageSize,
NextPageToken: nil,
}).Return(&persistence.ListNamespacesResponse{
Namespaces: []*persistence.GetNamespaceResponse{
namespaceRecord1,
namespaceRecord2},
NextPageToken: nil,
}, nil)

// load namespaces
s.registry.Start()
defer s.registry.Stop()

s.regPersistence.EXPECT().GetMetadata().Return(
&persistence.GetMetadataResponse{
NotificationVersion: namespaceNotificationVersion,
}, nil)
s.regPersistence.EXPECT().ListNamespaces(&persistence.ListNamespacesRequest{
PageSize: namespace.CacheRefreshPageSize,
NextPageToken: nil,
}).Return(&persistence.ListNamespacesResponse{
Namespaces: []*persistence.GetNamespaceResponse{
// namespaceRecord1 is removed
namespaceRecord2},
NextPageToken: nil,
}, nil)

s.registry.Refresh()

ns2FromRegistry, err := s.registry.GetNamespace(namespace.Name(namespaceRecord2.Namespace.Info.Name))
s.NotNil(ns2FromRegistry)
s.NoError(err)

ns1FromRegistry, err := s.registry.GetNamespace(namespace.Name(namespaceRecord1.Namespace.Info.Name))
s.Nil(ns1FromRegistry)
s.Error(err)
var notFound *serviceerror.NotFound
s.ErrorAs(err, &notFound)
}

func TestCacheByName(t *testing.T) {
nsrec := persistence.GetNamespaceResponse{
Namespace: &persistencespb.NamespaceDetail{
Expand Down