go.temporal.io/server/common/searchattribute/manager.go
189 LOC · 102 covered · 87 uncovered · 25 ranges · 544 concepts · 13 introducers · 286 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package searchattribute
import (
"context"
"maps"
"math/rand"
"sync"
"sync/atomic"
"time"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/persistence"
)
const (
cacheRefreshTimeout = 5 * time.Second
cacheRefreshInterval = 60 * time.Second
cacheRefreshIfUnavailableInterval = 20 * time.Second
cacheRefreshColdInterval = 1 * time.Second
)
type (
managerImpl struct {
logger log.Logger
timeSource clock.TimeSource
clusterMetadataManager persistence.ClusterMetadataManager
forceRefresh dynamicconfig.BoolPropertyFn
cacheUpdateMutex sync.Mutex
cache atomic.Value // of type cache
}
cache struct {
// indexName -> NameTypeMap
searchAttributes map[string]NameTypeMap
dbVersion int64
expireOn time.Time
}
)
var _ Manager = (*managerImpl)(nil)
func NewManager(
timeSource clock.TimeSource,
clusterMetadataManager persistence.ClusterMetadataManager,
logger log.Logger,
forceRefresh dynamicconfig.BoolPropertyFn,
var saCache atomic.Value
saCache.Store(cache{
searchAttributes: map[string]NameTypeMap{},
dbVersion: 0,
expireOn: time.Time{},
})
return &managerImpl{
logger: logger,
timeSource: timeSource,
cache: saCache,
clusterMetadataManager: clusterMetadataManager,
forceRefresh: forceRefresh,
}
}
// GetSearchAttributes returns all search attributes (including system and build-in) for specified index.
// indexName can be an empty string for backward compatibility.
func (m *managerImpl) GetSearchAttributes(
indexName string,
forceRefreshCache bool,
now := m.timeSource.Now()
result := NewNameTypeMap(nil)
saCache, err := m.refreshCache(forceRefreshCache, now)
if err != nil {
return result, err
}
result.customSearchAttributes = maps.Clone(indexSearchAttributes.customSearchAttributes)
manager.go ×3
}
}
func (m *managerImpl) needRefreshCache(saCache cache, forceRefreshCache bool, now time.Time) bool {
manager.go ×7
return forceRefreshCache || saCache.expireOn.Before(now) || m.forceRefresh()
}
func (m *managerImpl) refreshCache(forceRefreshCache bool, now time.Time) (cache, error) {
manager.go ×7
//nolint:revive // cache value is always of type `cache`
saCache := m.cache.Load().(cache)
if !m.needRefreshCache(saCache, forceRefreshCache, now) {
}
defer m.cacheUpdateMutex.Unlock()
//nolint:revive // cache value is always of type `cache`
saCache = m.cache.Load().(cache)
if !m.needRefreshCache(saCache, forceRefreshCache, now) {
}
}
func (m *managerImpl) refreshCacheLocked(saCache cache, now time.Time) (cache, error) {
manager.go ×7
ctx, cancel := context.WithTimeout(context.Background(), cacheRefreshTimeout)
defer cancel()
if saCache.dbVersion == 0 {
// if cache is cold, use the highest priority caller
ctx = headers.SetCallerInfo(ctx, headers.SystemOperatorCallerInfo)
} else {
}
if err != nil {
// NotFound means cluster metadata was never persisted and custom search attributes are not defined.
// Ignore the error.
saCache.expireOn = now.Add(cacheRefreshInterval)
err = nil
if saCache.dbVersion == 0 {
// If the cache is still cold, and persistence is Unavailable, retry more aggressively
// within cacheRefreshColdInterval.
saCache.expireOn = now.Add(time.Duration(rand.Int63n(int64(cacheRefreshColdInterval))))
} else {
// If persistence is Unavailable, but cache was loaded at least once, then ignore the error
// and use existing cache for cacheRefreshIfUnavailableInterval.
saCache.expireOn = now.Add(cacheRefreshIfUnavailableInterval)
err = nil
}
}
return saCache, err
}
// clusterMetadata.Version <= saCache.dbVersion means DB is not changed.
m.cache.Store(saCache)
return saCache, nil
}
searchAttributes: buildIndexNameTypeMap(clusterMetadata.GetIndexSearchAttributes()),
expireOn: now.Add(cacheRefreshInterval),
dbVersion: clusterMetadata.Version,
}
m.cache.Store(saCache)
return saCache, nil
}
// SaveSearchAttributes saves search attributes to cluster metadata.
// indexName can be an empty string when Elasticsearch is not configured.
func (m *managerImpl) SaveSearchAttributes(
ctx context.Context,
indexName string,
newCustomSearchAttributes map[string]enumspb.IndexedValueType,
clusterMetadataResponse, err := m.clusterMetadataManager.GetCurrentClusterMetadata(ctx)
if err != nil {
return err
}
if clusterMetadata.IndexSearchAttributes == nil {
clusterMetadata.IndexSearchAttributes = map[string]*persistencespb.IndexSearchAttributes{indexName: nil}
}
clusterMetadata.IndexSearchAttributes[indexName] = &persistencespb.IndexSearchAttributes{CustomSearchAttributes: newCustomSearchAttributes}
manager.go ×3
_, err = m.clusterMetadataManager.SaveClusterMetadata(ctx, &persistence.SaveClusterMetadataRequest{
ClusterMetadata: clusterMetadata,
Version: clusterMetadataResponse.Version,
})
// Flush local cache, even if there was an error, which is most likely version mismatch (=stale cache).
m.cache.Store(cache{})
return err
}