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.

1 package searchattribute
2
3 import (
4 "context"
5 "maps"
6 "math/rand"
7 "sync"
8 "sync/atomic"
9 "time"
10
11 enumspb "go.temporal.io/api/enums/v1"
12 "go.temporal.io/api/serviceerror"
13 persistencespb "go.temporal.io/server/api/persistence/v1"
14 "go.temporal.io/server/common/clock"
15 "go.temporal.io/server/common/dynamicconfig"
16 "go.temporal.io/server/common/headers"
17 "go.temporal.io/server/common/log"
18 "go.temporal.io/server/common/log/tag"
19 "go.temporal.io/server/common/persistence"
20 )
21
22 const (
23 cacheRefreshTimeout = 5 * time.Second
24 cacheRefreshInterval = 60 * time.Second
25 cacheRefreshIfUnavailableInterval = 20 * time.Second
26 cacheRefreshColdInterval = 1 * time.Second
27 )
28
29 type (
30 managerImpl struct {
31 logger log.Logger
32 timeSource clock.TimeSource
33 clusterMetadataManager persistence.ClusterMetadataManager
34 forceRefresh dynamicconfig.BoolPropertyFn
35
36 cacheUpdateMutex sync.Mutex
37 cache atomic.Value // of type cache
38 }
39
40 cache struct {
41 // indexName -> NameTypeMap
42 searchAttributes map[string]NameTypeMap
43 dbVersion int64
44 expireOn time.Time
45 }
46 )
47
48 var _ Manager = (*managerImpl)(nil)
49
50 func NewManager(
51 timeSource clock.TimeSource,
52 clusterMetadataManager persistence.ClusterMetadataManager,
53 logger log.Logger,
54 forceRefresh dynamicconfig.BoolPropertyFn,
55 > ) *managerImpl { manager.go ×1
56 > var saCache atomic.Value
57 > saCache.Store(cache{
58 > searchAttributes: map[string]NameTypeMap{},
59 > dbVersion: 0,
60 > expireOn: time.Time{},
61 > })
62 >
63 > return &managerImpl{
64 > logger: logger,
65 > timeSource: timeSource,
66 > cache: saCache,
67 > clusterMetadataManager: clusterMetadataManager,
68 > forceRefresh: forceRefresh,
69 > }
70 > }
71
72 // GetSearchAttributes returns all search attributes (including system and build-in) for specified index.
73 // indexName can be an empty string for backward compatibility.
74 func (m *managerImpl) GetSearchAttributes(
75 indexName string,
76 forceRefreshCache bool,
77 > ) (NameTypeMap, error) { manager.go ×7
78 > now := m.timeSource.Now()
79 > result := NewNameTypeMap(nil)
80 > saCache, err := m.refreshCache(forceRefreshCache, now)
81 > if err != nil {
82 > m.logger.Error("failed to refresh search attributes cache", tag.Error(err)) manager.go ×1
83 > return result, err
84 > }
85 > if indexSearchAttributes, ok := saCache.searchAttributes[indexName]; ok { manager.go ×2
86 > result.customSearchAttributes = maps.Clone(indexSearchAttributes.customSearchAttributes) manager.go ×3
87 > }
88 > return result, nil manager.go ×2
89 }
90
91 > func (m *managerImpl) needRefreshCache(saCache cache, forceRefreshCache bool, now time.Time) bool { manager.go ×7
92 > return forceRefreshCache || saCache.expireOn.Before(now) || m.forceRefresh()
93 > }
94
95 > func (m *managerImpl) refreshCache(forceRefreshCache bool, now time.Time) (cache, error) { manager.go ×7
96 > //nolint:revive // cache value is always of type `cache`
97 > saCache := m.cache.Load().(cache)
98 > if !m.needRefreshCache(saCache, forceRefreshCache, now) {
99 > return saCache, nil manager.go ×1
100 > }
101
102 > m.cacheUpdateMutex.Lock() manager.go ×7
103 > defer m.cacheUpdateMutex.Unlock()
104 > //nolint:revive // cache value is always of type `cache`
105 > saCache = m.cache.Load().(cache)
106 > if !m.needRefreshCache(saCache, forceRefreshCache, now) {
107 > return saCache, nil manager.go ×1
108 > }
109
110 > return m.refreshCacheLocked(saCache, now) manager.go ×7
111 }
112
113 > func (m *managerImpl) refreshCacheLocked(saCache cache, now time.Time) (cache, error) { manager.go ×7
114 > ctx, cancel := context.WithTimeout(context.Background(), cacheRefreshTimeout)
115 > defer cancel()
116 > if saCache.dbVersion == 0 {
117 > // if cache is cold, use the highest priority caller
118 > ctx = headers.SetCallerInfo(ctx, headers.SystemOperatorCallerInfo)
119 > } else {
120 > ctx = headers.SetCallerInfo(ctx, headers.SystemBackgroundHighCallerInfo) manager.go ×1
121 > }
122
123 > clusterMetadata, err := m.clusterMetadataManager.GetCurrentClusterMetadata(ctx) manager.go ×7
124 > if err != nil {
125 > switch err.(type) { manager.go ×2
126 > case *serviceerror.NotFound: manager.go ×1
127 > // NotFound means cluster metadata was never persisted and custom search attributes are not defined.
128 > // Ignore the error.
129 > saCache.expireOn = now.Add(cacheRefreshInterval)
130 > err = nil
131 > case *serviceerror.Unavailable: manager.go ×1
132 > if saCache.dbVersion == 0 {
133 > // If the cache is still cold, and persistence is Unavailable, retry more aggressively
134 > // within cacheRefreshColdInterval.
135 > saCache.expireOn = now.Add(time.Duration(rand.Int63n(int64(cacheRefreshColdInterval))))
136 > } else {
137 > // If persistence is Unavailable, but cache was loaded at least once, then ignore the error
138 > // and use existing cache for cacheRefreshIfUnavailableInterval.
139 > saCache.expireOn = now.Add(cacheRefreshIfUnavailableInterval)
140 > err = nil
141 > }
142 }
143 > m.cache.Store(saCache) manager.go ×2
144 > return saCache, err
145 }
146
147 // clusterMetadata.Version <= saCache.dbVersion means DB is not changed.
148 > if clusterMetadata.Version <= saCache.dbVersion { manager.go ×3
149 > saCache.expireOn = now.Add(cacheRefreshInterval) manager.go ×1
150 > m.cache.Store(saCache)
151 > return saCache, nil
152 > }
153
154 > saCache = cache{ manager.go ×3
155 > searchAttributes: buildIndexNameTypeMap(clusterMetadata.GetIndexSearchAttributes()),
156 > expireOn: now.Add(cacheRefreshInterval),
157 > dbVersion: clusterMetadata.Version,
158 > }
159 > m.cache.Store(saCache)
160 > return saCache, nil
161 }
162
163 // SaveSearchAttributes saves search attributes to cluster metadata.
164 // indexName can be an empty string when Elasticsearch is not configured.
165 func (m *managerImpl) SaveSearchAttributes(
166 ctx context.Context,
167 indexName string,
168 newCustomSearchAttributes map[string]enumspb.IndexedValueType,
169 > ) error { manager.go ×3
170 >
171 > clusterMetadataResponse, err := m.clusterMetadataManager.GetCurrentClusterMetadata(ctx)
172 > if err != nil {
173 return err
174 }
175
176 > clusterMetadata := clusterMetadataResponse.ClusterMetadata manager.go ×3
177 > if clusterMetadata.IndexSearchAttributes == nil {
178 clusterMetadata.IndexSearchAttributes = map[string]*persistencespb.IndexSearchAttributes{indexName: nil}
179 }
180 > clusterMetadata.IndexSearchAttributes[indexName] = &persistencespb.IndexSearchAttributes{CustomSearchAttributes: newCustomSearchAttributes} manager.go ×3
181 > _, err = m.clusterMetadataManager.SaveClusterMetadata(ctx, &persistence.SaveClusterMetadataRequest{
182 > ClusterMetadata: clusterMetadata,
183 > Version: clusterMetadataResponse.Version,
184 > })
185 > // Flush local cache, even if there was an error, which is most likely version mismatch (=stale cache).
186 > m.cache.Store(cache{})
187 >
188 > return err
189 }