forked from temporalio/temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonebox.go
861 lines (767 loc) · 28.3 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
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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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 (
"encoding/json"
"fmt"
"net"
"sync"
"time"
"github.com/uber-go/tally"
"github.com/uber/tchannel-go"
"go.temporal.io/api/workflowservice/v1"
sdkclient "go.temporal.io/sdk/client"
"go.uber.org/fx"
"go.temporal.io/server/common/persistence/visibility"
esclient "go.temporal.io/server/common/persistence/visibility/store/elasticsearch/client"
"google.golang.org/grpc"
"go.temporal.io/server/api/adminservice/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common"
carchiver "go.temporal.io/server/common/archiver"
"go.temporal.io/server/common/archiver/provider"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/membership"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
persistenceClient "go.temporal.io/server/common/persistence/client"
"go.temporal.io/server/common/resolver"
"go.temporal.io/server/common/resource"
"go.temporal.io/server/common/rpc"
"go.temporal.io/server/service/frontend"
"go.temporal.io/server/service/history"
"go.temporal.io/server/service/matching"
"go.temporal.io/server/service/worker"
"go.temporal.io/server/service/worker/archiver"
"go.temporal.io/server/service/worker/replicator"
)
// Temporal hosts all of temporal services in one process
type Temporal interface {
Start() error
Stop()
GetAdminClient() adminservice.AdminServiceClient
GetFrontendClient() workflowservice.WorkflowServiceClient
GetHistoryClient() historyservice.HistoryServiceClient
GetExecutionManager() persistence.ExecutionManager
RefreshNamespaceCache()
}
type (
temporalImpl struct {
frontendService resource.Resource
matchingService resource.Resource
historyServices []resource.Resource
workerService resource.Resource
adminClient adminservice.AdminServiceClient
frontendClient workflowservice.WorkflowServiceClient
historyClient historyservice.HistoryServiceClient
logger log.Logger
clusterMetadataConfig *config.ClusterMetadata
persistenceConfig config.Persistence
metadataMgr persistence.MetadataManager
clusterMetadataMgr persistence.ClusterMetadataManager
shardMgr persistence.ShardManager
taskMgr persistence.TaskManager
executionManager persistence.ExecutionManager
namespaceReplicationQueue persistence.NamespaceReplicationQueue
shutdownCh chan struct{}
shutdownWG sync.WaitGroup
clusterNo int // cluster number
replicator *replicator.Replicator
clientWorker archiver.ClientWorker
archiverMetadata carchiver.ArchivalMetadata
archiverProvider provider.ArchiverProvider
historyConfig *HistoryConfig
esConfig *esclient.Config
esClient esclient.Client
workerConfig *WorkerConfig
mockAdminClient map[string]adminservice.AdminServiceClient
namespaceReplicationTaskExecutor namespace.ReplicationTaskExecutor
}
// HistoryConfig contains configs for history service
HistoryConfig struct {
NumHistoryShards int32
NumHistoryHosts int
HistoryCountLimitError int
HistoryCountLimitWarn int
}
// TemporalParams contains everything needed to bootstrap Temporal
TemporalParams struct {
ClusterMetadataConfig *config.ClusterMetadata
PersistenceConfig config.Persistence
MetadataMgr persistence.MetadataManager
ClusterMetadataManager persistence.ClusterMetadataManager
ShardMgr persistence.ShardManager
ExecutionManager persistence.ExecutionManager
TaskMgr persistence.TaskManager
NamespaceReplicationQueue persistence.NamespaceReplicationQueue
Logger log.Logger
ClusterNo int
ArchiverMetadata carchiver.ArchivalMetadata
ArchiverProvider provider.ArchiverProvider
EnableReadHistoryFromArchival bool
HistoryConfig *HistoryConfig
ESConfig *esclient.Config
ESClient esclient.Client
WorkerConfig *WorkerConfig
MockAdminClient map[string]adminservice.AdminServiceClient
NamespaceReplicationTaskExecutor namespace.ReplicationTaskExecutor
}
membershipFactoryImpl struct {
serviceName string
hosts map[string][]string
}
)
// NewTemporal returns an instance that hosts full temporal in one process
func NewTemporal(params *TemporalParams) *temporalImpl {
return &temporalImpl{
logger: params.Logger,
clusterMetadataConfig: params.ClusterMetadataConfig,
persistenceConfig: params.PersistenceConfig,
metadataMgr: params.MetadataMgr,
clusterMetadataMgr: params.ClusterMetadataManager,
shardMgr: params.ShardMgr,
taskMgr: params.TaskMgr,
executionManager: params.ExecutionManager,
namespaceReplicationQueue: params.NamespaceReplicationQueue,
shutdownCh: make(chan struct{}),
clusterNo: params.ClusterNo,
esConfig: params.ESConfig,
esClient: params.ESClient,
archiverMetadata: params.ArchiverMetadata,
archiverProvider: params.ArchiverProvider,
historyConfig: params.HistoryConfig,
workerConfig: params.WorkerConfig,
mockAdminClient: params.MockAdminClient,
namespaceReplicationTaskExecutor: params.NamespaceReplicationTaskExecutor,
}
}
func (c *temporalImpl) enableWorker() bool {
return c.workerConfig.EnableArchiver || c.workerConfig.EnableReplicator
}
func (c *temporalImpl) Start() error {
hosts := make(map[string][]string)
hosts[common.FrontendServiceName] = []string{c.FrontendGRPCAddress()}
hosts[common.MatchingServiceName] = []string{c.MatchingGRPCServiceAddress()}
hosts[common.HistoryServiceName] = c.HistoryServiceAddress(3)
if c.enableWorker() {
hosts[common.WorkerServiceName] = []string{c.WorkerGRPCServiceAddress()}
}
// create temporal-system namespace, this must be created before starting
// the services - so directly use the metadataManager to create this
if err := c.createSystemNamespace(); err != nil {
return err
}
var startWG sync.WaitGroup
startWG.Add(2)
go c.startHistory(hosts, &startWG)
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 *temporalImpl) Stop() {
if c.enableWorker() {
c.shutdownWG.Add(4)
c.workerService.Stop()
} else {
c.shutdownWG.Add(3)
}
c.frontendService.Stop()
for _, historyService := range c.historyServices {
historyService.Stop()
}
c.matchingService.Stop()
if c.workerConfig.EnableReplicator {
c.replicator.Stop()
}
if c.workerConfig.EnableArchiver {
c.clientWorker.Stop()
}
close(c.shutdownCh)
c.shutdownWG.Wait()
}
func (c *temporalImpl) FrontendGRPCAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7134"
case 1:
return "127.0.0.1:8134"
case 2:
return "127.0.0.1:9134"
case 3:
return "127.0.0.1:10134"
default:
return "127.0.0.1:7134"
}
}
func (c *temporalImpl) FrontendRingpopAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7124"
case 1:
return "127.0.0.1:8124"
case 2:
return "127.0.0.1:9124"
case 3:
return "127.0.0.1:10124"
default:
return "127.0.0.1:7124"
}
}
// penultimatePortDigit: 2 - ringpop, 3 - gRPC
func (c *temporalImpl) HistoryServiceAddress(penultimatePortDigit int) []string {
var hosts []string
startPort := penultimatePortDigit * 10
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 *temporalImpl) MatchingGRPCServiceAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7136"
case 1:
return "127.0.0.1:8136"
case 2:
return "127.0.0.1:9136"
case 3:
return "127.0.0.1:10136"
default:
return "127.0.0.1:7136"
}
}
func (c *temporalImpl) MatchingServiceRingpopAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7126"
case 1:
return "127.0.0.1:8126"
case 2:
return "127.0.0.1:9126"
case 3:
return "127.0.0.1:10126"
default:
return "127.0.0.1:7126"
}
}
func (c *temporalImpl) WorkerGRPCServiceAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7138"
case 1:
return "127.0.0.1:8138"
case 2:
return "127.0.0.1:9138"
case 3:
return "127.0.0.1:10138"
default:
return "127.0.0.1:7138"
}
}
func (c *temporalImpl) WorkerServiceRingpopAddress() string {
switch c.clusterNo {
case 0:
return "127.0.0.1:7128"
case 1:
return "127.0.0.1:8128"
case 2:
return "127.0.0.1:9128"
case 3:
return "127.0.0.1:10128"
default:
return "127.0.0.1:7128"
}
}
func (c *temporalImpl) GetAdminClient() adminservice.AdminServiceClient {
return c.adminClient
}
func (c *temporalImpl) GetFrontendClient() workflowservice.WorkflowServiceClient {
return c.frontendClient
}
func (c *temporalImpl) GetHistoryClient() historyservice.HistoryServiceClient {
return c.historyClient
}
func (c *temporalImpl) startFrontend(hosts map[string][]string, startWG *sync.WaitGroup) {
params := &resource.BootstrapParams{}
params.DCRedirectionPolicy = config.DCRedirectionPolicy{}
params.Name = common.FrontendServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.RPCFactory = newRPCFactoryImpl(common.FrontendServiceName, c.FrontendGRPCAddress(), c.FrontendRingpopAddress(),
c.logger)
params.MetricsScope = tally.NewTestScope(common.FrontendServiceName, make(map[string]string))
params.MembershipFactoryInitializer = func(x persistenceClient.Bean, y log.Logger) (resource.MembershipMonitorFactory, error) {
return newMembershipFactory(params.Name, hosts), nil
}
params.ClusterMetadataConfig = c.clusterMetadataConfig
params.MetricsClient = metrics.NewClient(params.MetricsScope, metrics.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfigClient = newIntegrationConfigClient(dynamicconfig.NewNoopClient())
params.ArchivalMetadata = c.archiverMetadata
params.ArchiverProvider = c.archiverProvider
params.ESConfig = c.esConfig
params.ESClient = c.esClient
params.Authorizer = authorization.NewNoopAuthorizer()
var err error
params.PersistenceConfig, err = copyPersistenceConfig(c.persistenceConfig)
if err != nil {
c.logger.Fatal("Failed to copy persistence config for frontend", tag.Error(err))
}
params.PersistenceServiceResolver = resolver.NewNoopResolver()
if c.esConfig != nil {
esDataStoreName := "es-visibility"
params.PersistenceConfig.AdvancedVisibilityStore = esDataStoreName
params.PersistenceConfig.DataStores[esDataStoreName] = config.DataStore{
Elasticsearch: c.esConfig,
}
}
stoppedCh := make(chan struct{})
var frontendService *frontend.Service
feApp := fx.New(
fx.Supply(
params,
stoppedCh,
),
frontend.Module,
fx.Populate(&frontendService),
)
err = feApp.Err()
if err != nil {
params.Logger.Fatal("unable to construct frontend service", tag.Error(err))
}
if c.mockAdminClient != nil {
clientBean := frontendService.GetClientBean()
if clientBean != nil {
for serviceName, client := range c.mockAdminClient {
clientBean.SetRemoteAdminClient(serviceName, client)
}
}
}
c.frontendService = frontendService
connection := params.RPCFactory.CreateFrontendGRPCConnection(c.FrontendGRPCAddress())
c.frontendClient = NewFrontendClient(connection)
c.adminClient = NewAdminClient(connection)
go frontendService.Start()
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *temporalImpl) startHistory(
hosts map[string][]string,
startWG *sync.WaitGroup,
) {
membershipPorts := c.HistoryServiceAddress(2)
for i, grpcPort := range c.HistoryServiceAddress(3) {
params := &resource.BootstrapParams{}
params.Name = common.HistoryServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.RPCFactory = newRPCFactoryImpl(common.HistoryServiceName, grpcPort, membershipPorts[i], c.logger)
params.MetricsScope = tally.NewTestScope(common.HistoryServiceName, make(map[string]string))
params.MembershipFactoryInitializer = func(x persistenceClient.Bean, y log.Logger) (resource.MembershipMonitorFactory, error) {
return newMembershipFactory(params.Name, hosts), nil
}
params.ClusterMetadataConfig = c.clusterMetadataConfig
params.MetricsClient = metrics.NewClient(params.MetricsScope, metrics.GetMetricsServiceIdx(params.Name, c.logger))
integrationClient := newIntegrationConfigClient(dynamicconfig.NewNoopClient())
c.overrideHistoryDynamicConfig(integrationClient)
params.DynamicConfigClient = integrationClient
var err error
params.SdkClient, err = sdkclient.NewClient(sdkclient.Options{
HostPort: c.FrontendGRPCAddress(),
Namespace: common.SystemLocalNamespace,
MetricsScope: params.MetricsScope,
ConnectionOptions: sdkclient.ConnectionOptions{
DisableHealthCheck: true,
},
})
if err != nil {
c.logger.Fatal("Failed to create client for history", tag.Error(err))
}
params.ArchivalMetadata = c.archiverMetadata
params.ArchiverProvider = c.archiverProvider
params.ESConfig = c.esConfig
params.ESClient = c.esClient
params.PersistenceConfig, err = copyPersistenceConfig(c.persistenceConfig)
if err != nil {
c.logger.Fatal("Failed to copy persistence config for history", tag.Error(err))
}
params.PersistenceServiceResolver = resolver.NewNoopResolver()
if c.esConfig != nil {
esDataStoreName := "es-visibility"
params.PersistenceConfig.AdvancedVisibilityStore = esDataStoreName
params.PersistenceConfig.DataStores[esDataStoreName] = config.DataStore{
Elasticsearch: c.esConfig,
}
}
stoppedCh := make(chan struct{})
var historyService *history.Service
app := fx.New(
fx.Supply(
params,
stoppedCh,
),
history.Module,
fx.Populate(&historyService))
err = app.Err()
if err != nil {
params.Logger.Fatal("unable to construct history service", tag.Error(err))
}
if c.mockAdminClient != nil {
clientBean := historyService.GetClientBean()
if clientBean != nil {
for serviceName, client := range c.mockAdminClient {
clientBean.SetRemoteAdminClient(serviceName, client)
}
}
}
// TODO: this is not correct when there are multiple history hosts as later client will overwrite previous ones.
// However current interface for getting history client doesn't specify which client it needs and the tests that use this API
// depends on the fact that there's only one history host.
// Need to change those tests and modify the interface for getting history client.
historyConnection, err := rpc.Dial(c.HistoryServiceAddress(3)[0], nil, c.logger)
if err != nil {
c.logger.Fatal("Failed to create connection for history", tag.Error(err))
}
c.historyClient = NewHistoryClient(historyConnection)
c.historyServices = append(c.historyServices, historyService)
go historyService.Start()
}
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *temporalImpl) startMatching(hosts map[string][]string, startWG *sync.WaitGroup) {
params := &resource.BootstrapParams{}
params.Name = common.MatchingServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.RPCFactory = newRPCFactoryImpl(common.MatchingServiceName, c.MatchingGRPCServiceAddress(), c.MatchingServiceRingpopAddress(), c.logger)
params.MetricsScope = tally.NewTestScope(common.MatchingServiceName, make(map[string]string))
params.MembershipFactoryInitializer = func(x persistenceClient.Bean, y log.Logger) (resource.MembershipMonitorFactory, error) {
return newMembershipFactory(params.Name, hosts), nil
}
params.ClusterMetadataConfig = c.clusterMetadataConfig
params.MetricsClient = metrics.NewClient(params.MetricsScope, metrics.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfigClient = newIntegrationConfigClient(dynamicconfig.NewNoopClient())
params.ArchivalMetadata = c.archiverMetadata
params.ArchiverProvider = c.archiverProvider
var err error
params.PersistenceConfig, err = copyPersistenceConfig(c.persistenceConfig)
if err != nil {
c.logger.Fatal("Failed to copy persistence config for matching", tag.Error(err))
}
params.PersistenceServiceResolver = resolver.NewNoopResolver()
stoppedCh := make(chan struct{})
var matchingService *matching.Service
app := fx.New(
fx.Supply(stoppedCh, params),
matching.Module,
fx.Populate(&matchingService),
)
err = app.Err()
if err != nil {
params.Logger.Fatal("unable to start matching service", tag.Error(err))
}
if c.mockAdminClient != nil {
clientBean := matchingService.GetClientBean()
if clientBean != nil {
for serviceName, client := range c.mockAdminClient {
clientBean.SetRemoteAdminClient(serviceName, client)
}
}
}
c.matchingService = matchingService
go c.matchingService.Start()
startWG.Done()
<-c.shutdownCh
c.shutdownWG.Done()
}
func (c *temporalImpl) startWorker(hosts map[string][]string, startWG *sync.WaitGroup) {
params := &resource.BootstrapParams{}
params.Name = common.WorkerServiceName
params.Logger = c.logger
params.ThrottledLogger = c.logger
params.RPCFactory = newRPCFactoryImpl(common.WorkerServiceName, c.WorkerGRPCServiceAddress(), c.WorkerServiceRingpopAddress(), c.logger)
params.MetricsScope = tally.NewTestScope(common.WorkerServiceName, make(map[string]string))
params.MembershipFactoryInitializer = func(x persistenceClient.Bean, y log.Logger) (resource.MembershipMonitorFactory, error) {
return newMembershipFactory(params.Name, hosts), nil
}
params.ClusterMetadataConfig = c.clusterMetadataConfig
params.MetricsClient = metrics.NewClient(params.MetricsScope, metrics.GetMetricsServiceIdx(params.Name, c.logger))
params.DynamicConfigClient = newIntegrationConfigClient(dynamicconfig.NewNoopClient())
params.ArchivalMetadata = c.archiverMetadata
params.ArchiverProvider = c.archiverProvider
var err error
params.PersistenceConfig, err = copyPersistenceConfig(c.persistenceConfig)
if err != nil {
c.logger.Fatal("Failed to copy persistence config for worker", tag.Error(err))
}
params.PersistenceServiceResolver = resolver.NewNoopResolver()
params.SdkClient, err = sdkclient.NewClient(sdkclient.Options{
HostPort: c.FrontendGRPCAddress(),
Namespace: common.SystemLocalNamespace,
MetricsScope: params.MetricsScope,
ConnectionOptions: sdkclient.ConnectionOptions{
DisableHealthCheck: true,
},
})
if err != nil {
c.logger.Fatal("Failed to create client for worker", tag.Error(err))
}
service, err := resource.New(
params,
common.WorkerServiceName,
dynamicconfig.GetIntPropertyFn(5000),
dynamicconfig.GetIntPropertyFn(5000),
dynamicconfig.GetIntPropertyFn(10000),
)
if err != nil {
params.Logger.Fatal("unable to create worker service", tag.Error(err))
}
c.workerService = service
service.Start()
clusterMetadata := cluster.NewTestClusterMetadata(c.clusterMetadataConfig)
var replicatorNamespaceCache namespace.Registry
if c.workerConfig.EnableReplicator {
metadataManager := persistence.NewMetadataPersistenceMetricsClient(c.metadataMgr, service.GetMetricsClient(), c.logger)
replicatorNamespaceCache = namespace.NewRegistry(metadataManager, clusterMetadata.IsGlobalNamespaceEnabled(), service.GetMetricsClient(), service.GetLogger())
replicatorNamespaceCache.Start()
c.startWorkerReplicator(service, clusterMetadata)
}
var clientWorkerNamespaceCache namespace.Registry
if c.workerConfig.EnableArchiver {
metadataProxyManager := persistence.NewMetadataPersistenceMetricsClient(c.metadataMgr, service.GetMetricsClient(), c.logger)
clientWorkerNamespaceCache = namespace.NewRegistry(metadataProxyManager, clusterMetadata.IsGlobalNamespaceEnabled(), service.GetMetricsClient(), service.GetLogger())
clientWorkerNamespaceCache.Start()
c.startWorkerClientWorker(params, service, clientWorkerNamespaceCache)
}
startWG.Done()
<-c.shutdownCh
if c.workerConfig.EnableReplicator {
replicatorNamespaceCache.Stop()
}
if c.workerConfig.EnableArchiver {
clientWorkerNamespaceCache.Stop()
}
c.shutdownWG.Done()
}
func (c *temporalImpl) startWorkerReplicator(service resource.Resource, clusterMetadata cluster.Metadata) {
serviceResolver, err := service.GetMembershipMonitor().GetResolver(common.WorkerServiceName)
if err != nil {
c.logger.Fatal("Fail to start replicator when start worker", tag.Error(err))
}
c.replicator = replicator.NewReplicator(
clusterMetadata,
service.GetClientBean(),
c.logger,
service.GetMetricsClient(),
service.GetHostInfo(),
serviceResolver,
c.namespaceReplicationQueue,
c.namespaceReplicationTaskExecutor,
)
c.replicator.Start()
}
func (c *temporalImpl) startWorkerClientWorker(params *resource.BootstrapParams, service resource.Resource, namespaceRegistry namespace.Registry) {
workerConfig := worker.NewConfig(params)
workerConfig.ArchiverConfig.ArchiverConcurrency = dynamicconfig.GetIntPropertyFn(10)
bc := &archiver.BootstrapContainer{
SdkClient: params.SdkClient,
MetricsClient: service.GetMetricsClient(),
Logger: c.logger,
HistoryV2Manager: c.executionManager,
NamespaceCache: namespaceRegistry,
Config: workerConfig.ArchiverConfig,
ArchiverProvider: c.archiverProvider,
}
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 *temporalImpl) createSystemNamespace() error {
err := c.metadataMgr.InitializeSystemNamespaces(c.clusterMetadataConfig.CurrentClusterName)
if err != nil {
return fmt.Errorf("failed to create temporal-system namespace: %v", err)
}
return nil
}
func (c *temporalImpl) GetExecutionManager() persistence.ExecutionManager {
return c.executionManager
}
func (c *temporalImpl) overrideHistoryDynamicConfig(client *dynamicClient) {
client.OverrideValue(dynamicconfig.ReplicationTaskProcessorStartWait, time.Nanosecond)
if c.workerConfig.EnableIndexer {
client.OverrideValue(dynamicconfig.AdvancedVisibilityWritingMode, visibility.AdvancedVisibilityWritingModeDual)
}
if c.historyConfig.HistoryCountLimitWarn != 0 {
client.OverrideValue(dynamicconfig.HistoryCountLimitWarn, c.historyConfig.HistoryCountLimitWarn)
}
if c.historyConfig.HistoryCountLimitError != 0 {
client.OverrideValue(dynamicconfig.HistoryCountLimitError, c.historyConfig.HistoryCountLimitError)
}
}
func (c *temporalImpl) RefreshNamespaceCache() {
c.frontendService.GetNamespaceRegistry().Refresh()
c.matchingService.GetNamespaceRegistry().Refresh()
for _, r := range c.historyServices {
r.GetNamespaceRegistry().Refresh()
}
if c.workerService != nil {
c.workerService.GetNamespaceRegistry().Refresh()
}
}
// copyPersistenceConfig makes a deepcopy of persistence config.
// This is just a temp fix for the race condition of persistence config.
// The race condition happens because all the services are using the same datastore map in the config.
// Also all services will retry to modify the maxQPS field in the datastore during start up and use the modified maxQPS value to create a persistence factory.
func copyPersistenceConfig(pConfig config.Persistence) (config.Persistence, error) {
copiedDataStores := make(map[string]config.DataStore)
for name, value := range pConfig.DataStores {
copiedDataStore := config.DataStore{}
encodedDataStore, err := json.Marshal(value)
if err != nil {
return pConfig, err
}
if err = json.Unmarshal(encodedDataStore, &copiedDataStore); err != nil {
return pConfig, err
}
copiedDataStores[name] = copiedDataStore
}
pConfig.DataStores = copiedDataStores
return pConfig, nil
}
func newMembershipFactory(serviceName string, hosts map[string][]string) resource.MembershipMonitorFactory {
return &membershipFactoryImpl{
serviceName: serviceName,
hosts: hosts,
}
}
func (p *membershipFactoryImpl) GetMembershipMonitor() (membership.Monitor, error) {
return newSimpleMonitor(p.serviceName, p.hosts), nil
}
type rpcFactoryImpl struct {
serviceName string
ringpopServiceName string
grpcHostPort string
ringpopHostPort string
logger log.Logger
sync.Mutex
listener net.Listener
ringpopChannel *tchannel.Channel
serverCfg config.GroupTLS
}
func (c *rpcFactoryImpl) GetFrontendGRPCServerOptions() ([]grpc.ServerOption, error) {
return nil, nil
}
func (c *rpcFactoryImpl) GetInternodeGRPCServerOptions() ([]grpc.ServerOption, error) {
return nil, nil
}
func (c *rpcFactoryImpl) CreateFrontendGRPCConnection(hostName string) *grpc.ClientConn {
return c.CreateGRPCConnection(hostName)
}
func (c *rpcFactoryImpl) CreateInternodeGRPCConnection(hostName string) *grpc.ClientConn {
return c.CreateGRPCConnection(hostName)
}
func newRPCFactoryImpl(sName, grpcHostPort, ringpopHostPort string, logger log.Logger) common.RPCFactory {
return &rpcFactoryImpl{
serviceName: sName,
grpcHostPort: grpcHostPort,
ringpopHostPort: ringpopHostPort,
logger: logger,
}
}
func (c *rpcFactoryImpl) GetGRPCListener() net.Listener {
if c.listener != nil {
return c.listener
}
c.Lock()
defer c.Unlock()
if c.listener == nil {
var err error
c.listener, err = net.Listen("tcp", c.grpcHostPort)
if err != nil {
c.logger.Fatal("Failed create gRPC listener", tag.Error(err), tag.Service(c.serviceName), tag.Address(c.grpcHostPort))
}
c.logger.Info("Created gRPC listener", tag.Service(c.serviceName), tag.Address(c.grpcHostPort))
}
return c.listener
}
func (c *rpcFactoryImpl) GetRingpopChannel() *tchannel.Channel {
if c.ringpopChannel != nil {
return c.ringpopChannel
}
c.Lock()
defer c.Unlock()
if c.ringpopChannel == nil {
ringpopServiceName := fmt.Sprintf("%v-ringpop", c.serviceName)
var err error
c.ringpopChannel, err = tchannel.NewChannel(ringpopServiceName, nil)
if err != nil {
c.logger.Fatal("Failed to create ringpop TChannel", tag.Error(err))
}
err = c.ringpopChannel.ListenAndServe(c.ringpopHostPort)
if err != nil {
c.logger.Fatal("Failed to start ringpop listener", tag.Error(err), tag.Address(c.ringpopHostPort))
}
}
return c.ringpopChannel
}
// CreateGRPCConnection creates connection for gRPC calls
func (c *rpcFactoryImpl) CreateGRPCConnection(hostName string) *grpc.ClientConn {
connection, err := rpc.Dial(hostName, nil, c.logger)
if err != nil {
c.logger.Fatal("Failed to create gRPC connection", tag.Error(err))
}
return connection
}