forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onebox.go
750 lines (683 loc) · 24.9 KB
/
onebox.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
// 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.
package host
import (
"context"
"fmt"
"sync"
"github.com/pborman/uuid"
"github.com/stretchr/testify/mock"
"github.com/uber-go/tally"
"github.com/uber/cadence/.gen/go/admin/adminserviceclient"
"github.com/uber/cadence/.gen/go/cadence/workflowserviceclient"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/client"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/blobstore"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/elasticsearch"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/membership"
"github.com/uber/cadence/common/messaging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/mocks"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/common/service/config"
"github.com/uber/cadence/common/service/dynamicconfig"
"github.com/uber/cadence/service/frontend"
"github.com/uber/cadence/service/history"
"github.com/uber/cadence/service/matching"
"github.com/uber/cadence/service/worker"
"github.com/uber/cadence/service/worker/archiver"
"github.com/uber/cadence/service/worker/indexer"
"github.com/uber/cadence/service/worker/replicator"
cwsc "go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/transport/tchannel"
)
const archivalBlobSize = 5 * 1024 // 5KB
// Cadence hosts all of cadence services in one process
type Cadence interface {
Start() error
Stop()
GetAdminClient() adminserviceclient.Interface
GetFrontendClient() workflowserviceclient.Interface
FrontendAddress() string
GetFrontendService() service.Service
}
type (
cadenceImpl struct {
adminHandler *frontend.AdminHandler
frontendHandler *frontend.WorkflowHandler
matchingHandler *matching.Handler
historyHandlers []*history.Handler
logger log.Logger
clusterMetadata cluster.Metadata
persistenceConfig config.Persistence
dispatcherProvider client.DispatcherProvider
messagingClient messaging.Client
metadataMgr persistence.MetadataManager
metadataMgrV2 persistence.MetadataManager
shardMgr persistence.ShardManager
historyMgr persistence.HistoryManager
historyV2Mgr persistence.HistoryV2Manager
taskMgr persistence.TaskManager
visibilityMgr persistence.VisibilityManager
executionMgrFactory persistence.ExecutionManagerFactory
shutdownCh chan struct{}
shutdownWG sync.WaitGroup
frontEndService service.Service
clusterNo int // cluster number
replicator *replicator.Replicator
clientWorker archiver.ClientWorker
indexer *indexer.Indexer
enableEventsV2 bool
blobstoreClient blobstore.Client
historyConfig *HistoryConfig
esConfig *elasticsearch.Config
esClient elasticsearch.Client
workerConfig *WorkerConfig
}
// HistoryConfig contains configs for history service
HistoryConfig struct {
NumHistoryShards int
NumHistoryHosts int
HistoryCountLimitError int
HistoryCountLimitWarn int
}
// CadenceParams contains everything needed to bootstrap Cadence
CadenceParams struct {
ClusterMetadata cluster.Metadata
PersistenceConfig config.Persistence
DispatcherProvider client.DispatcherProvider
MessagingClient messaging.Client
MetadataMgr persistence.MetadataManager
MetadataMgrV2 persistence.MetadataManager
ShardMgr persistence.ShardManager
HistoryMgr persistence.HistoryManager
HistoryV2Mgr persistence.HistoryV2Manager
ExecutionMgrFactory persistence.ExecutionManagerFactory
TaskMgr persistence.TaskManager
VisibilityMgr persistence.VisibilityManager
Logger log.Logger
ClusterNo int
EnableEventsV2 bool
Blobstore blobstore.Client
EnableReadHistoryFromArchival bool
HistoryConfig *HistoryConfig
ESConfig *elasticsearch.Config
ESClient elasticsearch.Client
WorkerConfig *WorkerConfig
}
membershipFactoryImpl struct {
serviceName string
hosts map[string][]string
}
)
// NewCadence returns an instance that hosts full cadence in one process
func NewCadence(params *CadenceParams) Cadence {
return &cadenceImpl{
logger: params.Logger,
clusterMetadata: params.ClusterMetadata,
persistenceConfig: params.PersistenceConfig,
dispatcherProvider: params.DispatcherProvider,
messagingClient: params.MessagingClient,
metadataMgr: params.MetadataMgr,
metadataMgrV2: params.MetadataMgrV2,
visibilityMgr: params.VisibilityMgr,
shardMgr: params.ShardMgr,
historyMgr: params.HistoryMgr,
historyV2Mgr: params.HistoryV2Mgr,
taskMgr: params.TaskMgr,
executionMgrFactory: params.ExecutionMgrFactory,
shutdownCh: make(chan struct{}),
clusterNo: params.ClusterNo,
enableEventsV2: params.EnableEventsV2,
esConfig: params.ESConfig,
esClient: params.ESClient,
blobstoreClient: params.Blobstore,
historyConfig: params.HistoryConfig,
workerConfig: params.WorkerConfig,
}
}
func (c *cadenceImpl) enableWorker() bool {
return c.workerConfig.EnableArchiver || c.workerConfig.EnableIndexer || c.workerConfig.EnableReplicator
}
func (c *cadenceImpl) Start() error {
hosts := make(map[string][]string)
hosts[common.FrontendServiceName] = []string{c.FrontendAddress()}
hosts[common.MatchingServiceName] = []string{c.MatchingServiceAddress()}
hosts[common.HistoryServiceName] = c.HistoryServiceAddress()
if c.enableWorker() {
hosts[common.WorkerServiceName] = []string{c.WorkerServiceAddress()}
}
// create cadence-system domain, this must be created before starting
// the services - so directly use the metadataManager to create this
if err := c.createSystemDomain(); err != nil {
return err
}
var startWG sync.WaitGroup
startWG.Add(2)
go c.startHistory(hosts, &startWG, c.enableEventsV2)
go c.startMatching(hosts, &startWG)
startWG.Wait()
startWG.Add(1)
go c.startFrontend(hosts, &startWG)
startWG.Wait()
if c.enableWorker() {
startWG.Add(1)
go c.startWorker(hosts, &startWG)
startWG.Wait()
}
return nil
}
func (c *cadenceImpl) Stop() {
if c.enableWorker() {
c.shutdownWG.Add(4)
} else {
c.shutdownWG.Add(3)
}
c.frontendHandler.Stop()
c.adminHandler.Stop()
for _, historyHandler := range c.historyHandlers {
historyHandler.Stop()
}
c.matchingHandler.Stop()
if c.workerConfig.EnableReplicator {
c.replicator.Stop()
}
if c.workerConfig.EnableArchiver {
c.clientWorker.Stop()
}
close(c.shutdownCh)
c.shutdownWG.Wait()
}
func (c *cadenceImpl) FrontendAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7104"
case 1:
return "127.0.0.1:8104"
case 2:
return "127.0.0.1:9104"
case 3:
return "127.0.0.1:10104"
default:
return "127.0.0.1:7104"
}
}
func (c *cadenceImpl) FrontendPProfPort() int {
switch c.clusterNo {
case 0:
return 7105
case 1:
return 8105
case 2:
return 9105
case 3:
return 10105
default:
return 7105
}
}
func (c *cadenceImpl) HistoryServiceAddress() []string {
hosts := []string{}
startPort := 7201
switch c.clusterNo {
case 0:
startPort = 7201
case 1:
startPort = 8201
case 2:
startPort = 9201
case 3:
startPort = 10201
default:
startPort = 7201
}
for i := 0; i < c.historyConfig.NumHistoryHosts; i++ {
port := startPort + i
hosts = append(hosts, fmt.Sprintf("127.0.0.1:%v", port))
}
c.logger.Info("History hosts", tag.Addresses(hosts))
return hosts
}
func (c *cadenceImpl) HistoryPProfPort() []int {
ports := []int{}
startPort := 7301
switch c.clusterNo {
case 0:
startPort = 7301
case 1:
startPort = 8301
case 2:
startPort = 9301
case 3:
startPort = 10301
default:
startPort = 7301
}
for i := 0; i < c.historyConfig.NumHistoryHosts; i++ {
port := startPort + i
ports = append(ports, port)
}
c.logger.Info("History pprof ports", tag.Value(ports))
return ports
}
func (c *cadenceImpl) MatchingServiceAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7106"
case 1:
return "127.0.0.1:8106"
case 2:
return "127.0.0.1:9106"
case 3:
return "127.0.0.1:10106"
default:
return "127.0.0.1:7106"
}
}
func (c *cadenceImpl) MatchingPProfPort() int {
switch c.clusterNo {
case 0:
return 7107
case 1:
return 8107
case 2:
return 9107
case 3:
return 10107
default:
return 7107
}
}
func (c *cadenceImpl) WorkerServiceAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7108"
case 1:
return "127.0.0.1:8108"
case 2:
return "127.0.0.1:9108"
case 3:
return "127.0.0.1:10108"
default:
return "127.0.0.1:7108"
}
}
func (c *cadenceImpl) WorkerPProfPort() int {
switch c.clusterNo {
case 0:
return 7109
case 1:
return 8109
case 2:
return 9109
case 3:
return 10109
default:
return 7109
}
}
func (c *cadenceImpl) GetAdminClient() adminserviceclient.Interface {
return NewAdminClient(c.frontEndService.GetDispatcher())
}
func (c *cadenceImpl) GetFrontendClient() workflowserviceclient.Interface {
return NewFrontendClient(c.frontEndService.GetDispatcher())
}
// For integration tests to get hold of FE instance.
func (c *cadenceImpl) GetFrontendService() service.Service {
return c.frontEndService
}
func (c *cadenceImpl) startFrontend(hosts map[string][]string, startWG *sync.WaitGroup) {
params := new(service.BootstrapParams)
params.DCRedirectionPolicy = config.DCRedirectionPolicy{}
params.Name = common.FrontendServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.PProfInitializer = newPProfInitializerImpl(c.logger, c.FrontendPProfPort())
params.RPCFactory = newRPCFactoryImpl(common.FrontendServiceName, c.FrontendAddress(), c.logger)
params.MetricScope = tally.NewTestScope(common.FrontendServiceName, make(map[string]string))
params.MembershipFactory = newMembershipFactory(params.Name, hosts)
params.ClusterMetadata = c.clusterMetadata
params.DispatcherProvider = c.dispatcherProvider
params.MessagingClient = c.messagingClient
params.BlobstoreClient = c.blobstoreClient
params.PersistenceConfig = c.persistenceConfig
params.MetricsClient = metrics.NewClient(params.MetricScope, service.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfig = newIntegrationConfigClient(dynamicconfig.NewNopClient())
// TODO when cross DC is public, remove this temporary override
var kafkaProducer messaging.Producer
var err error
if c.workerConfig.EnableReplicator {
kafkaProducer, err = c.messagingClient.NewProducerWithClusterName(c.clusterMetadata.GetCurrentClusterName())
if err != nil {
c.logger.Fatal("Failed to create kafka producer when start frontend", tag.Error(err))
}
} else {
kafkaProducer = &mocks.KafkaProducer{}
kafkaProducer.(*mocks.KafkaProducer).On("Publish", mock.Anything).Return(nil)
}
c.frontEndService = service.New(params)
c.adminHandler = frontend.NewAdminHandler(
c.frontEndService, c.historyConfig.NumHistoryShards, c.metadataMgr, c.historyMgr, c.historyV2Mgr)
c.adminHandler.RegisterHandler()
dc := dynamicconfig.NewCollection(params.DynamicConfig, c.logger)
frontendConfig := frontend.NewConfig(dc, c.historyConfig.NumHistoryShards, c.workerConfig.EnableIndexer)
c.frontendHandler = frontend.NewWorkflowHandler(
c.frontEndService, frontendConfig, c.metadataMgr, c.historyMgr, c.historyV2Mgr,
c.visibilityMgr, kafkaProducer, params.BlobstoreClient)
dcRedirectionHandler := frontend.NewDCRedirectionHandler(c.frontendHandler, params.DCRedirectionPolicy)
dcRedirectionHandler.RegisterHandler()
// must start base service first
c.frontEndService.Start()
err = c.adminHandler.Start()
if err != nil {
c.logger.Fatal("Failed to start admin", tag.Error(err))
}
err = dcRedirectionHandler.Start()
if err != nil {
c.logger.Fatal("Failed to start frontend", tag.Error(err))
}
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *cadenceImpl) startHistory(hosts map[string][]string, startWG *sync.WaitGroup, enableEventsV2 bool) {
pprofPorts := c.HistoryPProfPort()
for i, hostport := range c.HistoryServiceAddress() {
params := new(service.BootstrapParams)
params.Name = common.HistoryServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.PProfInitializer = newPProfInitializerImpl(c.logger, pprofPorts[i])
params.RPCFactory = newRPCFactoryImpl(common.HistoryServiceName, hostport, c.logger)
params.MetricScope = tally.NewTestScope(common.HistoryServiceName, make(map[string]string))
params.MembershipFactory = newMembershipFactory(params.Name, hosts)
params.ClusterMetadata = c.clusterMetadata
params.DispatcherProvider = c.dispatcherProvider
params.MessagingClient = c.messagingClient
params.PersistenceConfig = c.persistenceConfig
params.MetricsClient = metrics.NewClient(params.MetricScope, service.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfig = dynamicconfig.NewNopClient()
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress())
if err != nil {
c.logger.Fatal("Failed to get dispatcher for frontend", tag.Error(err))
}
params.PublicClient = cwsc.New(dispatcher.ClientConfig(common.FrontendServiceName))
service := service.New(params)
hConfig := c.historyConfig
historyConfig := history.NewConfig(dynamicconfig.NewCollection(params.DynamicConfig, c.logger), hConfig.NumHistoryShards, c.workerConfig.EnableIndexer, config.StoreTypeCassandra)
historyConfig.HistoryMgrNumConns = dynamicconfig.GetIntPropertyFn(hConfig.NumHistoryShards)
historyConfig.ExecutionMgrNumConns = dynamicconfig.GetIntPropertyFn(hConfig.NumHistoryShards)
historyConfig.EnableEventsV2 = dynamicconfig.GetBoolPropertyFnFilteredByDomain(enableEventsV2)
if hConfig.HistoryCountLimitWarn != 0 {
historyConfig.HistoryCountLimitWarn = dynamicconfig.GetIntPropertyFilteredByDomain(hConfig.HistoryCountLimitWarn)
}
if hConfig.HistoryCountLimitError != 0 {
historyConfig.HistoryCountLimitError = dynamicconfig.GetIntPropertyFilteredByDomain(hConfig.HistoryCountLimitError)
}
handler := history.NewHandler(service, historyConfig, c.shardMgr, c.metadataMgr,
c.visibilityMgr, c.historyMgr, c.historyV2Mgr, c.executionMgrFactory, params.PublicClient)
handler.RegisterHandler()
service.Start()
err = handler.Start()
if err != nil {
c.logger.Fatal("Failed to start history", tag.Error(err))
}
c.historyHandlers = append(c.historyHandlers, handler)
}
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *cadenceImpl) startMatching(hosts map[string][]string, startWG *sync.WaitGroup) {
params := new(service.BootstrapParams)
params.Name = common.MatchingServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.PProfInitializer = newPProfInitializerImpl(c.logger, c.MatchingPProfPort())
params.RPCFactory = newRPCFactoryImpl(common.MatchingServiceName, c.MatchingServiceAddress(), c.logger)
params.MetricScope = tally.NewTestScope(common.MatchingServiceName, make(map[string]string))
params.MembershipFactory = newMembershipFactory(params.Name, hosts)
params.ClusterMetadata = c.clusterMetadata
params.DispatcherProvider = c.dispatcherProvider
params.PersistenceConfig = c.persistenceConfig
params.MetricsClient = metrics.NewClient(params.MetricScope, service.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfig = newIntegrationConfigClient(dynamicconfig.NewNopClient())
service := service.New(params)
c.matchingHandler = matching.NewHandler(
service, matching.NewConfig(dynamicconfig.NewCollection(params.DynamicConfig, c.logger)), c.taskMgr, c.metadataMgr,
)
c.matchingHandler.RegisterHandler()
service.Start()
err := c.matchingHandler.Start()
if err != nil {
c.logger.Fatal("Failed to start history", tag.Error(err))
}
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *cadenceImpl) startWorker(hosts map[string][]string, startWG *sync.WaitGroup) {
params := new(service.BootstrapParams)
params.Name = common.WorkerServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.PProfInitializer = newPProfInitializerImpl(c.logger, c.WorkerPProfPort())
params.RPCFactory = newRPCFactoryImpl(common.WorkerServiceName, c.WorkerServiceAddress(), c.logger)
params.MetricScope = tally.NewTestScope(common.WorkerServiceName, make(map[string]string))
params.MembershipFactory = newMembershipFactory(params.Name, hosts)
params.ClusterMetadata = c.clusterMetadata
params.DispatcherProvider = c.dispatcherProvider
params.PersistenceConfig = c.persistenceConfig
params.MetricsClient = metrics.NewClient(params.MetricScope, service.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfig = newIntegrationConfigClient(dynamicconfig.NewNopClient())
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress())
if err != nil {
c.logger.Fatal("Failed to get dispatcher for frontend", tag.Error(err))
}
params.PublicClient = cwsc.New(dispatcher.ClientConfig(common.FrontendServiceName))
service := service.New(params)
service.Start()
var replicatorDomainCache cache.DomainCache
if c.workerConfig.EnableReplicator {
metadataManager := persistence.NewMetadataPersistenceMetricsClient(c.metadataMgrV2, service.GetMetricsClient(), c.logger)
replicatorDomainCache = cache.NewDomainCache(metadataManager, params.ClusterMetadata, service.GetMetricsClient(), service.GetLogger())
replicatorDomainCache.Start()
c.startWorkerReplicator(params, service, replicatorDomainCache)
}
var clientWorkerDomainCache cache.DomainCache
if c.workerConfig.EnableArchiver {
metadataProxyManager := persistence.NewMetadataPersistenceMetricsClient(c.metadataMgr, service.GetMetricsClient(), c.logger)
clientWorkerDomainCache = cache.NewDomainCache(metadataProxyManager, params.ClusterMetadata, service.GetMetricsClient(), service.GetLogger())
clientWorkerDomainCache.Start()
c.startWorkerClientWorker(params, service, clientWorkerDomainCache)
}
if c.workerConfig.EnableIndexer {
c.startWorkerIndexer(params, service)
}
startWG.Done()
<-c.shutdownCh
if c.workerConfig.EnableReplicator {
replicatorDomainCache.Stop()
}
if c.workerConfig.EnableArchiver {
clientWorkerDomainCache.Stop()
}
c.shutdownWG.Done()
}
func (c *cadenceImpl) startWorkerReplicator(params *service.BootstrapParams, service service.Service, domainCache cache.DomainCache) {
metadataManager := persistence.NewMetadataPersistenceMetricsClient(c.metadataMgrV2, service.GetMetricsClient(), c.logger)
workerConfig := worker.NewConfig(params)
workerConfig.ReplicationCfg.ReplicatorMessageConcurrency = dynamicconfig.GetIntPropertyFn(10)
c.replicator = replicator.NewReplicator(
c.clusterMetadata,
metadataManager,
domainCache,
service.GetClientBean(),
workerConfig.ReplicationCfg,
c.messagingClient,
c.logger,
service.GetMetricsClient())
if err := c.replicator.Start(); err != nil {
c.replicator.Stop()
c.logger.Fatal("Fail to start replicator when start worker", tag.Error(err))
}
}
func (c *cadenceImpl) startWorkerClientWorker(params *service.BootstrapParams, service service.Service, domainCache cache.DomainCache) {
blobstoreClient := blobstore.NewRetryableClient(
blobstore.NewMetricClient(c.blobstoreClient, service.GetMetricsClient()),
c.blobstoreClient.GetRetryPolicy(),
c.blobstoreClient.IsRetryableError)
workerConfig := worker.NewConfig(params)
workerConfig.ArchiverConfig.ArchiverConcurrency = dynamicconfig.GetIntPropertyFn(10)
workerConfig.ArchiverConfig.TargetArchivalBlobSize = dynamicconfig.GetIntPropertyFilteredByDomain(archivalBlobSize)
bc := &archiver.BootstrapContainer{
PublicClient: params.PublicClient,
MetricsClient: service.GetMetricsClient(),
Logger: c.logger,
ClusterMetadata: service.GetClusterMetadata(),
HistoryManager: c.historyMgr,
HistoryV2Manager: c.historyV2Mgr,
Blobstore: blobstoreClient,
DomainCache: domainCache,
Config: workerConfig.ArchiverConfig,
}
c.clientWorker = archiver.NewClientWorker(bc)
if err := c.clientWorker.Start(); err != nil {
c.clientWorker.Stop()
c.logger.Fatal("Fail to start archiver when start worker", tag.Error(err))
}
}
func (c *cadenceImpl) startWorkerIndexer(params *service.BootstrapParams, service service.Service) {
workerConfig := worker.NewConfig(params)
c.indexer = indexer.NewIndexer(
workerConfig.IndexerCfg,
c.messagingClient,
c.esClient,
c.esConfig,
c.logger,
service.GetMetricsClient())
if err := c.indexer.Start(); err != nil {
c.indexer.Stop()
c.logger.Fatal("Fail to start indexer when start worker", tag.Error(err))
}
}
func (c *cadenceImpl) createSystemDomain() error {
if c.metadataMgrV2 == nil {
return nil
}
_, err := c.metadataMgrV2.CreateDomain(&persistence.CreateDomainRequest{
Info: &persistence.DomainInfo{
ID: uuid.New(),
Name: "cadence-system",
Status: persistence.DomainStatusRegistered,
Description: "Cadence system domain",
},
Config: &persistence.DomainConfig{
Retention: 1,
ArchivalStatus: shared.ArchivalStatusDisabled,
},
ReplicationConfig: &persistence.DomainReplicationConfig{},
})
if err != nil {
if _, ok := err.(*shared.DomainAlreadyExistsError); ok {
return nil
}
return fmt.Errorf("failed to create cadence-system domain: %v", err)
}
return nil
}
func newMembershipFactory(serviceName string, hosts map[string][]string) service.MembershipMonitorFactory {
return &membershipFactoryImpl{
serviceName: serviceName,
hosts: hosts,
}
}
func (p *membershipFactoryImpl) Create(dispatcher *yarpc.Dispatcher) (membership.Monitor, error) {
return newSimpleMonitor(p.serviceName, p.hosts), nil
}
func newPProfInitializerImpl(logger log.Logger, port int) common.PProfInitializer {
return &config.PProfInitializerImpl{
PProf: &config.PProf{
Port: port,
},
Logger: logger,
}
}
type rpcFactoryImpl struct {
ch *tchannel.ChannelTransport
serviceName string
hostPort string
logger log.Logger
}
func newRPCFactoryImpl(sName string, hostPort string, logger log.Logger) common.RPCFactory {
return &rpcFactoryImpl{
serviceName: sName,
hostPort: hostPort,
logger: logger,
}
}
func (c *rpcFactoryImpl) CreateDispatcher() *yarpc.Dispatcher {
// Setup dispatcher for onebox
var err error
c.ch, err = tchannel.NewChannelTransport(
tchannel.ServiceName(c.serviceName), tchannel.ListenAddr(c.hostPort))
if err != nil {
c.logger.Fatal("Failed to create transport channel", tag.Error(err))
}
return yarpc.NewDispatcher(yarpc.Config{
Name: c.serviceName,
Inbounds: yarpc.Inbounds{c.ch.NewInbound()},
// For integration tests to generate client out of the same outbound.
Outbounds: yarpc.Outbounds{
c.serviceName: {Unary: c.ch.NewSingleOutbound(c.hostPort)},
},
InboundMiddleware: yarpc.InboundMiddleware{
Unary: &versionMiddleware{},
},
})
}
type versionMiddleware struct {
}
func (vm *versionMiddleware) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, h transport.UnaryHandler) error {
req.Headers = req.Headers.With(common.LibraryVersionHeaderName, "1.0.0").With(common.FeatureVersionHeaderName, "1.0.0").With(common.ClientImplHeaderName, "uber-go")
return h.Handle(ctx, req, resw)
}
func (c *rpcFactoryImpl) CreateDispatcherForOutbound(
callerName, serviceName, hostName string) *yarpc.Dispatcher {
// Setup dispatcher(outbound) for onebox
d := yarpc.NewDispatcher(yarpc.Config{
Name: callerName,
Outbounds: yarpc.Outbounds{
serviceName: {Unary: c.ch.NewSingleOutbound(hostName)},
},
})
if err := d.Start(); err != nil {
c.logger.Fatal("Failed to create outbound transport channel", tag.Error(err))
}
return d
}