forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomainCache.go
950 lines (824 loc) · 29.1 KB
/
domainCache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination domainCache_mock.go -self_package github.com/uber/cadence/common/cache
package cache
import (
"context"
"hash/fnv"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/errors"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
// ReplicationPolicy is the domain's replication policy,
// derived from domain's replication config
type ReplicationPolicy int
const (
// ReplicationPolicyOneCluster indicate that workflows does not need to be replicated
// applicable to local domain & global domain with one cluster
ReplicationPolicyOneCluster ReplicationPolicy = 0
// ReplicationPolicyMultiCluster indicate that workflows need to be replicated
ReplicationPolicyMultiCluster ReplicationPolicy = 1
)
const (
domainCacheInitialSize = 10 * 1024
domainCacheMaxSize = 64 * 1024
domainCacheTTL = 0 // 0 means infinity
domainCacheMinRefreshInterval = 1 * time.Second
// DomainCacheRefreshInterval domain cache refresh interval
DomainCacheRefreshInterval = 10 * time.Second
// DomainCacheRefreshFailureRetryInterval is the wait time
// if refreshment encounters error
DomainCacheRefreshFailureRetryInterval = 1 * time.Second
domainCacheRefreshPageSize = 200
domainCachePersistenceTimeout = 3 * time.Second
domainCacheInitialized int32 = 0
domainCacheStarted int32 = 1
domainCacheStopped int32 = 2
)
type (
// PrepareCallbackFn is function to be called before CallbackFn is called,
// it is guaranteed that PrepareCallbackFn and CallbackFn pair will be both called or non will be called
PrepareCallbackFn func()
// CallbackFn is function to be called when the domain cache entries are changed
// it is guaranteed that PrepareCallbackFn and CallbackFn pair will be both called or non will be called
CallbackFn func(prevDomains []*DomainCacheEntry, nextDomains []*DomainCacheEntry)
// DomainCache is used the cache domain information and configuration to avoid making too many calls to cassandra.
// This cache is mainly used by frontend for resolving domain names to domain uuids which are used throughout the
// system. Each domain entry is kept in the cache for one hour but also has an expiry of 10 seconds. This results
// in updating the domain entry every 10 seconds but in the case of a cassandra failure we can still keep on serving
// requests using the stale entry from cache upto an hour
DomainCache interface {
common.Daemon
RegisterDomainChangeCallback(shard int, initialNotificationVersion int64, prepareCallback PrepareCallbackFn, callback CallbackFn)
UnregisterDomainChangeCallback(shard int)
GetDomain(name string) (*DomainCacheEntry, error)
GetDomainByID(id string) (*DomainCacheEntry, error)
GetDomainID(name string) (string, error)
GetDomainName(id string) (string, error)
GetAllDomain() map[string]*DomainCacheEntry
GetCacheSize() (sizeOfCacheByName int64, sizeOfCacheByID int64)
GetActiveDomainByID(id string) (*DomainCacheEntry, error)
}
domainCache struct {
status int32
shutdownChan chan struct{}
cacheNameToID *atomic.Value
cacheByID *atomic.Value
domainManager persistence.DomainManager
clusterMetadata cluster.Metadata
timeSource clock.TimeSource
metricsClient metrics.Client
logger log.Logger
// refresh lock is used to guarantee at most one
// coroutine is doing domain refreshment
refreshLock sync.Mutex
lastRefreshTime time.Time
callbackLock sync.Mutex
prepareCallbacks map[int]PrepareCallbackFn
callbacks map[int]CallbackFn
}
// DomainCacheEntries is DomainCacheEntry slice
DomainCacheEntries []*DomainCacheEntry
// DomainCacheEntry contains the info and config for a domain
DomainCacheEntry struct {
clusterMetadata cluster.Metadata
sync.RWMutex
info *persistence.DomainInfo
config *persistence.DomainConfig
replicationConfig *persistence.DomainReplicationConfig
configVersion int64
failoverVersion int64
isGlobalDomain bool
failoverNotificationVersion int64
previousFailoverVersion int64
failoverEndTime *int64
notificationVersion int64
initialized bool
}
)
// NewDomainCache creates a new instance of cache for holding onto domain information to reduce the load on persistence
func NewDomainCache(
domainManager persistence.DomainManager,
clusterMetadata cluster.Metadata,
metricsClient metrics.Client,
logger log.Logger,
) DomainCache {
cache := &domainCache{
status: domainCacheInitialized,
shutdownChan: make(chan struct{}),
cacheNameToID: &atomic.Value{},
cacheByID: &atomic.Value{},
domainManager: domainManager,
clusterMetadata: clusterMetadata,
timeSource: clock.NewRealTimeSource(),
metricsClient: metricsClient,
logger: logger,
prepareCallbacks: make(map[int]PrepareCallbackFn),
callbacks: make(map[int]CallbackFn),
}
cache.cacheNameToID.Store(newDomainCache())
cache.cacheByID.Store(newDomainCache())
return cache
}
func newDomainCache() Cache {
return NewSimple(&SimpleOptions{
InitialCapacity: domainCacheInitialSize,
})
}
func newDomainCacheEntry(
clusterMetadata cluster.Metadata,
) *DomainCacheEntry {
return &DomainCacheEntry{
clusterMetadata: clusterMetadata,
initialized: false,
}
}
// NewGlobalDomainCacheEntryForTest returns an entry with test data
func NewGlobalDomainCacheEntryForTest(
info *persistence.DomainInfo,
config *persistence.DomainConfig,
repConfig *persistence.DomainReplicationConfig,
failoverVersion int64,
clusterMetadata cluster.Metadata,
) *DomainCacheEntry {
return &DomainCacheEntry{
info: info,
config: config,
isGlobalDomain: true,
replicationConfig: repConfig,
failoverVersion: failoverVersion,
clusterMetadata: clusterMetadata,
}
}
// NewLocalDomainCacheEntryForTest returns an entry with test data
func NewLocalDomainCacheEntryForTest(
info *persistence.DomainInfo,
config *persistence.DomainConfig,
targetCluster string,
clusterMetadata cluster.Metadata,
) *DomainCacheEntry {
return &DomainCacheEntry{
info: info,
config: config,
isGlobalDomain: false,
replicationConfig: &persistence.DomainReplicationConfig{
ActiveClusterName: targetCluster,
Clusters: []*persistence.ClusterReplicationConfig{{ClusterName: targetCluster}},
},
failoverVersion: common.EmptyVersion,
clusterMetadata: clusterMetadata,
}
}
// NewDomainCacheEntryForTest returns an entry with test data
func NewDomainCacheEntryForTest(
info *persistence.DomainInfo,
config *persistence.DomainConfig,
isGlobalDomain bool,
repConfig *persistence.DomainReplicationConfig,
failoverVersion int64,
failoverEndtime *int64,
clusterMetadata cluster.Metadata,
) *DomainCacheEntry {
return &DomainCacheEntry{
info: info,
config: config,
isGlobalDomain: isGlobalDomain,
replicationConfig: repConfig,
failoverVersion: failoverVersion,
failoverEndTime: failoverEndtime,
clusterMetadata: clusterMetadata,
}
}
func (c *domainCache) GetCacheSize() (sizeOfCacheByName int64, sizeOfCacheByID int64) {
return int64(c.cacheByID.Load().(Cache).Size()), int64(c.cacheNameToID.Load().(Cache).Size())
}
// Start start the background refresh of domain
func (c *domainCache) Start() {
if !atomic.CompareAndSwapInt32(&c.status, domainCacheInitialized, domainCacheStarted) {
return
}
// initialize the cache by initial scan
err := c.refreshDomains()
if err != nil {
c.logger.Fatal("Unable to initialize domain cache", tag.Error(err))
}
go c.refreshLoop()
}
// Start start the background refresh of domain
func (c *domainCache) Stop() {
if !atomic.CompareAndSwapInt32(&c.status, domainCacheStarted, domainCacheStopped) {
return
}
close(c.shutdownChan)
}
func (c *domainCache) GetAllDomain() map[string]*DomainCacheEntry {
result := make(map[string]*DomainCacheEntry)
ite := c.cacheByID.Load().(Cache).Iterator()
defer ite.Close()
for ite.HasNext() {
entry := ite.Next()
id := entry.Key().(string)
domainCacheEntry := entry.Value().(*DomainCacheEntry)
domainCacheEntry.RLock()
dup := domainCacheEntry.duplicate()
domainCacheEntry.RUnlock()
result[id] = dup
}
return result
}
// RegisterDomainChangeCallback set a domain change callback
// WARN: the beforeCallback function will be triggered by domain cache when holding the domain cache lock,
// make sure the callback function will not call domain cache again in case of dead lock
// afterCallback will be invoked when NOT holding the domain cache lock.
func (c *domainCache) RegisterDomainChangeCallback(
shard int,
initialNotificationVersion int64,
prepareCallback PrepareCallbackFn,
callback CallbackFn,
) {
c.callbackLock.Lock()
c.prepareCallbacks[shard] = prepareCallback
c.callbacks[shard] = callback
c.callbackLock.Unlock()
// this section is trying to make the shard catch up with domain changes
domains := DomainCacheEntries{}
for _, domain := range c.GetAllDomain() {
domains = append(domains, domain)
}
// we mush notify the change in a ordered fashion
// since history shard have to update the shard info
// with domain change version.
sort.Sort(domains)
prevEntries := []*DomainCacheEntry{}
nextEntries := []*DomainCacheEntry{}
for _, domain := range domains {
if domain.notificationVersion >= initialNotificationVersion {
prevEntries = append(prevEntries, nil)
nextEntries = append(nextEntries, domain)
}
}
if len(prevEntries) > 0 {
prepareCallback()
callback(prevEntries, nextEntries)
}
}
// UnregisterDomainChangeCallback delete a domain failover callback
func (c *domainCache) UnregisterDomainChangeCallback(
shard int,
) {
c.callbackLock.Lock()
defer c.callbackLock.Unlock()
delete(c.prepareCallbacks, shard)
delete(c.callbacks, shard)
}
// GetDomain retrieves the information from the cache if it exists, otherwise retrieves the information from metadata
// store and writes it to the cache with an expiry before returning back
func (c *domainCache) GetDomain(
name string,
) (*DomainCacheEntry, error) {
if name == "" {
return nil, &types.BadRequestError{Message: "Domain is empty."}
}
return c.getDomain(name)
}
// GetDomainByID retrieves the information from the cache if it exists, otherwise retrieves the information from metadata
// store and writes it to the cache with an expiry before returning back
func (c *domainCache) GetDomainByID(
id string,
) (*DomainCacheEntry, error) {
if id == "" {
return nil, &types.BadRequestError{Message: "DomainID is empty."}
}
return c.getDomainByID(id, true)
}
// GetDomainID retrieves domainID by using GetDomain
func (c *domainCache) GetDomainID(
name string,
) (string, error) {
entry, err := c.GetDomain(name)
if err != nil {
return "", err
}
return entry.info.ID, nil
}
// GetDomainName returns domain name given the domain id
func (c *domainCache) GetDomainName(
id string,
) (string, error) {
entry, err := c.getDomainByID(id, false)
if err != nil {
return "", err
}
return entry.info.Name, nil
}
func (c *domainCache) GetActiveDomainByID(
id string,
) (*DomainCacheEntry, error) {
if err := common.ValidateDomainUUID(id); err != nil {
return nil, err
}
domainEntry, err := c.GetDomainByID(id)
if err != nil {
return nil, err
}
if err = domainEntry.GetDomainNotActiveErr(); err != nil {
// TODO: currently reapply events API will check if returned domainEntry is nil or not
// when there's an error.
return domainEntry, err
}
return domainEntry, nil
}
func (c *domainCache) refreshLoop() {
timer := time.NewTicker(DomainCacheRefreshInterval)
defer timer.Stop()
for {
select {
case <-c.shutdownChan:
return
case <-timer.C:
for err := c.refreshDomains(); err != nil; err = c.refreshDomains() {
select {
case <-c.shutdownChan:
return
default:
c.logger.Error("Error refreshing domain cache", tag.Error(err))
time.Sleep(DomainCacheRefreshFailureRetryInterval)
}
}
}
}
}
func (c *domainCache) refreshDomains() error {
c.refreshLock.Lock()
defer c.refreshLock.Unlock()
return c.refreshDomainsLocked()
}
// this function only refresh the domains in the v2 table
// the domains in the v1 table will be refreshed if cache is stale
func (c *domainCache) refreshDomainsLocked() error {
now := c.timeSource.Now()
if now.Sub(c.lastRefreshTime) < domainCacheMinRefreshInterval {
return nil
}
// first load the metadata record, then load domains
// this can guarantee that domains in the cache are not updated more than metadata record
ctx, cancel := context.WithTimeout(context.Background(), domainCachePersistenceTimeout)
defer cancel()
metadata, err := c.domainManager.GetMetadata(ctx)
if err != nil {
return err
}
domainNotificationVersion := metadata.NotificationVersion
var token []byte
request := &persistence.ListDomainsRequest{PageSize: domainCacheRefreshPageSize}
var domains DomainCacheEntries
continuePage := true
for continuePage {
ctx, cancel := context.WithTimeout(context.Background(), domainCachePersistenceTimeout)
request.NextPageToken = token
response, err := c.domainManager.ListDomains(ctx, request)
cancel()
if err != nil {
return err
}
token = response.NextPageToken
for _, domain := range response.Domains {
domains = append(domains, c.buildEntryFromRecord(domain))
}
continuePage = len(token) != 0
}
// we mush apply the domain change by order
// since history shard have to update the shard info
// with domain change version.
sort.Sort(domains)
prevEntries := []*DomainCacheEntry{}
nextEntries := []*DomainCacheEntry{}
// make a copy of the existing domain cache, so we can calculate diff and do compare and swap
newCacheNameToID := newDomainCache()
newCacheByID := newDomainCache()
for _, domain := range c.GetAllDomain() {
newCacheNameToID.Put(domain.info.Name, domain.info.ID)
newCacheByID.Put(domain.info.ID, domain)
}
UpdateLoop:
for _, domain := range domains {
if domain.notificationVersion >= domainNotificationVersion {
// this guarantee that domain change events before the
// domainNotificationVersion is loaded into the cache.
// the domain change events after the domainNotificationVersion
// will be loaded into cache in the next refresh
break UpdateLoop
}
prevEntry, nextEntry, err := c.updateIDToDomainCache(newCacheByID, domain.info.ID, domain)
if err != nil {
return err
}
c.metricsClient.Scope(metrics.DomainCacheScope).Tagged(
metrics.DomainTag(nextEntry.info.Name),
metrics.ActiveClusterTag(nextEntry.replicationConfig.ActiveClusterName),
).UpdateGauge(metrics.ActiveClusterGauge, 1)
c.updateNameToIDCache(newCacheNameToID, nextEntry.info.Name, nextEntry.info.ID)
if prevEntry != nil {
prevEntries = append(prevEntries, prevEntry)
nextEntries = append(nextEntries, nextEntry)
}
}
// NOTE: READ REF BEFORE MODIFICATION
// ref: historyEngine.go registerDomainFailoverCallback function
c.callbackLock.Lock()
defer c.callbackLock.Unlock()
c.triggerDomainChangePrepareCallbackLocked()
c.cacheByID.Store(newCacheByID)
c.cacheNameToID.Store(newCacheNameToID)
c.triggerDomainChangeCallbackLocked(prevEntries, nextEntries)
// only update last refresh time when refresh succeeded
c.lastRefreshTime = now
return nil
}
func (c *domainCache) checkDomainExists(
name string,
id string,
) error {
ctx, cancel := context.WithTimeout(context.Background(), domainCachePersistenceTimeout)
defer cancel()
_, err := c.domainManager.GetDomain(ctx, &persistence.GetDomainRequest{Name: name, ID: id})
return err
}
func (c *domainCache) updateNameToIDCache(
cacheNameToID Cache,
name string,
id string,
) {
cacheNameToID.Put(name, id)
}
func (c *domainCache) updateIDToDomainCache(
cacheByID Cache,
id string,
record *DomainCacheEntry,
) (*DomainCacheEntry, *DomainCacheEntry, error) {
elem, err := cacheByID.PutIfNotExist(id, newDomainCacheEntry(c.clusterMetadata))
if err != nil {
return nil, nil, err
}
entry := elem.(*DomainCacheEntry)
entry.Lock()
defer entry.Unlock()
var prevDomain *DomainCacheEntry
triggerCallback := c.clusterMetadata.IsGlobalDomainEnabled() &&
// initialized will be true when the entry contains valid data
entry.initialized &&
record.notificationVersion > entry.notificationVersion
if triggerCallback {
prevDomain = entry.duplicate()
}
entry.info = record.info
entry.config = record.config
entry.replicationConfig = record.replicationConfig
entry.configVersion = record.configVersion
entry.failoverVersion = record.failoverVersion
entry.isGlobalDomain = record.isGlobalDomain
entry.failoverNotificationVersion = record.failoverNotificationVersion
entry.previousFailoverVersion = record.previousFailoverVersion
entry.failoverEndTime = record.failoverEndTime
entry.notificationVersion = record.notificationVersion
entry.initialized = record.initialized
nextDomain := entry.duplicate()
return prevDomain, nextDomain, nil
}
// getDomain retrieves the information from the cache if it exists, otherwise retrieves the information from metadata
// store and writes it to the cache with an expiry before returning back
func (c *domainCache) getDomain(
name string,
) (*DomainCacheEntry, error) {
id, cacheHit := c.cacheNameToID.Load().(Cache).Get(name).(string)
if cacheHit {
return c.getDomainByID(id, true)
}
if err := c.checkDomainExists(name, ""); err != nil {
return nil, err
}
c.refreshLock.Lock()
defer c.refreshLock.Unlock()
id, cacheHit = c.cacheNameToID.Load().(Cache).Get(name).(string)
if cacheHit {
return c.getDomainByID(id, true)
}
if err := c.refreshDomainsLocked(); err != nil {
return nil, err
}
id, cacheHit = c.cacheNameToID.Load().(Cache).Get(name).(string)
if cacheHit {
return c.getDomainByID(id, true)
}
// impossible case
return nil, &types.InternalServiceError{Message: "domainCache encounter case where domain exists but cannot be loaded"}
}
// getDomainByID retrieves the information from the cache if it exists, otherwise retrieves the information from metadata
// store and writes it to the cache with an expiry before returning back
func (c *domainCache) getDomainByID(
id string,
deepCopy bool,
) (*DomainCacheEntry, error) {
var result *DomainCacheEntry
entry, cacheHit := c.cacheByID.Load().(Cache).Get(id).(*DomainCacheEntry)
if cacheHit {
entry.RLock()
result = entry
if deepCopy {
result = entry.duplicate()
}
entry.RUnlock()
return result, nil
}
if err := c.checkDomainExists("", id); err != nil {
return nil, err
}
c.refreshLock.Lock()
defer c.refreshLock.Unlock()
entry, cacheHit = c.cacheByID.Load().(Cache).Get(id).(*DomainCacheEntry)
if cacheHit {
entry.RLock()
result = entry
if deepCopy {
result = entry.duplicate()
}
entry.RUnlock()
return result, nil
}
if err := c.refreshDomainsLocked(); err != nil {
return nil, err
}
entry, cacheHit = c.cacheByID.Load().(Cache).Get(id).(*DomainCacheEntry)
if cacheHit {
entry.RLock()
result = entry
if deepCopy {
result = entry.duplicate()
}
entry.RUnlock()
return result, nil
}
// impossible case
return nil, &types.InternalServiceError{Message: "domainCache encounter case where domain exists but cannot be loaded"}
}
func (c *domainCache) triggerDomainChangePrepareCallbackLocked() {
sw := c.metricsClient.StartTimer(metrics.DomainCacheScope, metrics.DomainCachePrepareCallbacksLatency)
defer sw.Stop()
for _, prepareCallback := range c.prepareCallbacks {
prepareCallback()
}
}
func (c *domainCache) triggerDomainChangeCallbackLocked(
prevDomains []*DomainCacheEntry,
nextDomains []*DomainCacheEntry,
) {
sw := c.metricsClient.StartTimer(metrics.DomainCacheScope, metrics.DomainCacheCallbacksLatency)
defer sw.Stop()
for _, callback := range c.callbacks {
callback(prevDomains, nextDomains)
}
}
func (c *domainCache) buildEntryFromRecord(
record *persistence.GetDomainResponse,
) *DomainCacheEntry {
// this is a shallow copy, but since the record is generated by persistence
// and only accessible here, it would be fine
newEntry := newDomainCacheEntry(c.clusterMetadata)
newEntry.info = record.Info
newEntry.config = record.Config
newEntry.replicationConfig = record.ReplicationConfig
newEntry.configVersion = record.ConfigVersion
newEntry.failoverVersion = record.FailoverVersion
newEntry.isGlobalDomain = record.IsGlobalDomain
newEntry.failoverNotificationVersion = record.FailoverNotificationVersion
newEntry.previousFailoverVersion = record.PreviousFailoverVersion
newEntry.failoverEndTime = record.FailoverEndTime
newEntry.notificationVersion = record.NotificationVersion
newEntry.initialized = true
return newEntry
}
func copyResetBinary(bins types.BadBinaries) types.BadBinaries {
newbins := make(map[string]*types.BadBinaryInfo, len(bins.Binaries))
for k, v := range bins.Binaries {
newbins[k] = v
}
return types.BadBinaries{
Binaries: newbins,
}
}
func (entry *DomainCacheEntry) duplicate() *DomainCacheEntry {
// this is a deep copy
result := newDomainCacheEntry(entry.clusterMetadata)
result.info = &persistence.DomainInfo{
ID: entry.info.ID,
Name: entry.info.Name,
Status: entry.info.Status,
Description: entry.info.Description,
OwnerEmail: entry.info.OwnerEmail,
}
result.info.Data = map[string]string{}
for k, v := range entry.info.Data {
result.info.Data[k] = v
}
result.config = &persistence.DomainConfig{
Retention: entry.config.Retention,
EmitMetric: entry.config.EmitMetric,
HistoryArchivalStatus: entry.config.HistoryArchivalStatus,
HistoryArchivalURI: entry.config.HistoryArchivalURI,
VisibilityArchivalStatus: entry.config.VisibilityArchivalStatus,
VisibilityArchivalURI: entry.config.VisibilityArchivalURI,
BadBinaries: copyResetBinary(entry.config.BadBinaries),
}
result.replicationConfig = &persistence.DomainReplicationConfig{
ActiveClusterName: entry.replicationConfig.ActiveClusterName,
}
for _, clusterName := range entry.replicationConfig.Clusters {
result.replicationConfig.Clusters = append(result.replicationConfig.Clusters, &*clusterName)
}
result.configVersion = entry.configVersion
result.failoverVersion = entry.failoverVersion
result.isGlobalDomain = entry.isGlobalDomain
result.failoverNotificationVersion = entry.failoverNotificationVersion
result.previousFailoverVersion = entry.previousFailoverVersion
result.failoverEndTime = entry.failoverEndTime
result.notificationVersion = entry.notificationVersion
result.initialized = entry.initialized
return result
}
// GetInfo return the domain info
func (entry *DomainCacheEntry) GetInfo() *persistence.DomainInfo {
return entry.info
}
// GetConfig return the domain config
func (entry *DomainCacheEntry) GetConfig() *persistence.DomainConfig {
return entry.config
}
// GetReplicationConfig return the domain replication config
func (entry *DomainCacheEntry) GetReplicationConfig() *persistence.DomainReplicationConfig {
return entry.replicationConfig
}
// GetConfigVersion return the domain config version
func (entry *DomainCacheEntry) GetConfigVersion() int64 {
return entry.configVersion
}
// GetFailoverVersion return the domain failover version
func (entry *DomainCacheEntry) GetFailoverVersion() int64 {
return entry.failoverVersion
}
// IsGlobalDomain return whether the domain is a global domain
func (entry *DomainCacheEntry) IsGlobalDomain() bool {
return entry.isGlobalDomain
}
// GetFailoverNotificationVersion return the global notification version of when failover happened
func (entry *DomainCacheEntry) GetFailoverNotificationVersion() int64 {
return entry.failoverNotificationVersion
}
// GetNotificationVersion return the global notification version of when domain changed
func (entry *DomainCacheEntry) GetNotificationVersion() int64 {
return entry.notificationVersion
}
// GetPreviousFailoverVersion return the last domain failover version
func (entry *DomainCacheEntry) GetPreviousFailoverVersion() int64 {
return entry.previousFailoverVersion
}
// GetFailoverEndTime return the failover end time
func (entry *DomainCacheEntry) GetFailoverEndTime() *int64 {
return entry.failoverEndTime
}
// IsDomainActive return whether the domain is active, i.e. non global domain or global domain which active cluster is the current cluster
func (entry *DomainCacheEntry) IsDomainActive() bool {
if !entry.isGlobalDomain {
// domain is not a global domain, meaning domain is always "active" within each cluster
return true
}
return entry.clusterMetadata.GetCurrentClusterName() == entry.replicationConfig.ActiveClusterName && !entry.IsDomainPendingActive()
}
// IsDomainPendingActive returns whether the domain is in pending active state
func (entry *DomainCacheEntry) IsDomainPendingActive() bool {
if !entry.isGlobalDomain {
// domain is not a global domain, meaning domain can never be in pending active state
return false
}
return entry.failoverEndTime != nil
}
// GetReplicationPolicy return the derived workflow replication policy
func (entry *DomainCacheEntry) GetReplicationPolicy() ReplicationPolicy {
// frontend guarantee that the clusters always contains the active domain, so if the # of clusters is 1
// then we do not need to send out any events for replication
if entry.isGlobalDomain && len(entry.replicationConfig.Clusters) > 1 {
return ReplicationPolicyMultiCluster
}
return ReplicationPolicyOneCluster
}
// GetDomainNotActiveErr return err if domain is not active, nil otherwise
func (entry *DomainCacheEntry) GetDomainNotActiveErr() error {
if entry.IsDomainActive() {
// domain is consider active
return nil
}
if entry.IsDomainPendingActive() {
return errors.NewDomainPendingActiveError(
entry.info.Name,
entry.clusterMetadata.GetCurrentClusterName(),
)
}
return errors.NewDomainNotActiveError(
entry.info.Name,
entry.clusterMetadata.GetCurrentClusterName(),
entry.replicationConfig.ActiveClusterName,
)
}
// Len return length
func (t DomainCacheEntries) Len() int {
return len(t)
}
// Swap implements sort.Interface.
func (t DomainCacheEntries) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
// Less implements sort.Interface
func (t DomainCacheEntries) Less(i, j int) bool {
return t[i].notificationVersion < t[j].notificationVersion
}
// CreateDomainCacheEntry create a cache entry with domainName
func CreateDomainCacheEntry(
domainName string,
) *DomainCacheEntry {
return &DomainCacheEntry{info: &persistence.DomainInfo{Name: domainName}}
}
// SampleRetentionKey is key to specify sample retention
var SampleRetentionKey = "sample_retention_days"
// SampleRateKey is key to specify sample rate
var SampleRateKey = "sample_retention_rate"
// GetRetentionDays returns retention in days for given workflow
func (entry *DomainCacheEntry) GetRetentionDays(
workflowID string,
) int32 {
if entry.IsSampledForLongerRetention(workflowID) {
if sampledRetentionValue, ok := entry.info.Data[SampleRetentionKey]; ok {
sampledRetentionDays, err := strconv.Atoi(sampledRetentionValue)
if err != nil || sampledRetentionDays < int(entry.config.Retention) {
return entry.config.Retention
}
return int32(sampledRetentionDays)
}
}
return entry.config.Retention
}
// IsSampledForLongerRetentionEnabled return whether sample for longer retention is enabled or not
func (entry *DomainCacheEntry) IsSampledForLongerRetentionEnabled(
workflowID string,
) bool {
_, ok := entry.info.Data[SampleRateKey]
return ok
}
// IsSampledForLongerRetention return should given workflow been sampled or not
func (entry *DomainCacheEntry) IsSampledForLongerRetention(
workflowID string,
) bool {
if sampledRateValue, ok := entry.info.Data[SampleRateKey]; ok {
sampledRate, err := strconv.ParseFloat(sampledRateValue, 64)
if err != nil {
return false
}
h := fnv.New32a()
_, err = h.Write([]byte(workflowID))
if err != nil {
return false
}
hash := h.Sum32()
r := float64(hash%1000) / float64(1000) // use 1000 so we support one decimal rate like 1.5%.
if r < sampledRate { // sampled
return true
}
}
return false
}