Skip to content

Commit

Permalink
Add function to get custom search attributes mapper in namespace regi…
Browse files Browse the repository at this point in the history
…stry (#4317)
  • Loading branch information
rodrigozhou authored May 25, 2023
1 parent fea13ad commit 378e1e6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
27 changes: 27 additions & 0 deletions common/namespace/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type (
// State, ReplicationState, ActiveCluster, or isGlobalNamespace config changed.
RegisterStateChangeCallback(key any, cb StateChangeCallbackFn)
UnregisterStateChangeCallback(key any)
// GetCustomSearchAttributesMapper is a temporary solution to be able to get search attributes
// with from persistence if forceSearchAttributesCacheRefreshOnRead is true.
GetCustomSearchAttributesMapper(name Name) (CustomSearchAttributesMapper, error)
}

registry struct {
Expand Down Expand Up @@ -172,6 +175,9 @@ type (
// readthroughNotFoundCache stores namespaces that missed the above caches
// AND was not found when reading through to the persistence layer
readthroughNotFoundCache cache.Cache

// Temporary solution to force read search attributes from persistence
forceSearchAttributesCacheRefreshOnRead dynamicconfig.BoolPropertyFn
}
)

Expand All @@ -181,6 +187,7 @@ func NewRegistry(
persistence Persistence,
enableGlobalNamespaces bool,
refreshInterval dynamicconfig.DurationPropertyFn,
forceSearchAttributesCacheRefreshOnRead dynamicconfig.BoolPropertyFn,
metricsHandler metrics.Handler,
logger log.Logger,
) Registry {
Expand All @@ -196,6 +203,8 @@ func NewRegistry(
refreshInterval: refreshInterval,
stateChangeCallbacks: make(map[any]StateChangeCallbackFn),
readthroughNotFoundCache: cache.New(cacheMaxSize, &readthroughNotFoundCacheOpts),

forceSearchAttributesCacheRefreshOnRead: forceSearchAttributesCacheRefreshOnRead,
}
return reg
}
Expand Down Expand Up @@ -335,6 +344,24 @@ func (r *registry) GetNamespaceName(
return ns.Name(), nil
}

// GetCustomSearchAttributesMapper is a temporary solution to be able to get search attributes
// with from persistence if forceSearchAttributesCacheRefreshOnRead is true.
func (r *registry) GetCustomSearchAttributesMapper(name Name) (CustomSearchAttributesMapper, error) {
var ns *Namespace
var err error
if r.forceSearchAttributesCacheRefreshOnRead() {
r.readthroughLock.Lock()
defer r.readthroughLock.Unlock()
ns, err = r.getNamespaceByNamePersistence(name)
} else {
ns, err = r.GetNamespace(name)
}
if err != nil {
return CustomSearchAttributesMapper{}, err
}
return ns.CustomSearchAttributesMapper(), nil
}

func (r *registry) refreshLoop(ctx context.Context) error {
// Put timer events on our channel so we can select on just one below.
go func() {
Expand Down
15 changes: 15 additions & 0 deletions common/namespace/registry_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/namespace/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (s *registrySuite) SetupTest() {
s.regPersistence,
true,
dynamicconfig.GetDurationPropertyFn(time.Second),
dynamicconfig.GetBoolPropertyFn(false),
metrics.NoopMetricsHandler,
log.NewTestLogger())
}
Expand Down
1 change: 1 addition & 0 deletions common/resource/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func NamespaceRegistryProvider(
metadataManager,
clusterMetadata.IsGlobalNamespaceEnabled(),
dynamicCollection.GetDurationProperty(dynamicconfig.NamespaceCacheRefreshInterval, 10*time.Second),
dynamicCollection.GetBoolProperty(dynamicconfig.ForceSearchAttributesCacheRefreshOnRead, false),
metricsHandler,
logger,
)
Expand Down
3 changes: 1 addition & 2 deletions common/searchattribute/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ func (m *mapperProviderImpl) GetMapper(nsName namespace.Name) (Mapper, error) {
if !m.enableMapperFromNamespace {
return &noopMapper{}, nil
}
ns, err := m.namespaceRegistry.GetNamespace(nsName)
saMapper, err := m.namespaceRegistry.GetCustomSearchAttributesMapper(nsName)
if err != nil {
return nil, err
}
saMapper := ns.CustomSearchAttributesMapper()
// if there's an error, it returns an empty object, which is expected here
emptyStringNameTypeMap, _ := m.searchAttributesProvider.GetSearchAttributes("", false)
return &backCompMapper_v1_20{
Expand Down

0 comments on commit 378e1e6

Please sign in to comment.