-
Notifications
You must be signed in to change notification settings - Fork 779
/
service.go
347 lines (322 loc) · 13.4 KB
/
service.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
// 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 worker
import (
"sync/atomic"
"time"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
carchiver "github.com/uber/cadence/common/archiver"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/definition"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/loggerimpl"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
persistencefactory "github.com/uber/cadence/common/persistence/persistence-factory"
"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/worker/archiver"
"github.com/uber/cadence/service/worker/batcher"
"github.com/uber/cadence/service/worker/indexer"
"github.com/uber/cadence/service/worker/replicator"
"github.com/uber/cadence/service/worker/scanner"
)
type (
// Service represents the cadence-worker service. This service hosts all background processing needed for cadence cluster:
// 1. Replicator: Handles applying replication tasks generated by remote clusters.
// 2. Indexer: Handles uploading of visibility records to elastic search.
// 3. Archiver: Handles archival of workflow histories.
Service struct {
stopC chan struct{}
isStopped int32
params *service.BootstrapParams
config *Config
logger log.Logger
metricsClient metrics.Client
}
// Config contains all the service config for worker
Config struct {
ReplicationCfg *replicator.Config
ArchiverConfig *archiver.Config
IndexerCfg *indexer.Config
ScannerCfg *scanner.Config
BatcherCfg *batcher.Config
ThrottledLogRPS dynamicconfig.IntPropertyFn
EnableBatcher dynamicconfig.BoolPropertyFn
}
)
const domainRefreshInterval = time.Second * 30
// NewService builds a new cadence-worker service
func NewService(params *service.BootstrapParams) common.Daemon {
config := NewConfig(params)
params.ThrottledLogger = loggerimpl.NewThrottledLogger(params.Logger, config.ThrottledLogRPS)
params.UpdateLoggerWithServiceName(common.WorkerServiceName)
return &Service{
params: params,
config: config,
stopC: make(chan struct{}),
}
}
// NewConfig builds the new Config for cadence-worker service
func NewConfig(params *service.BootstrapParams) *Config {
dc := dynamicconfig.NewCollection(params.DynamicConfig, params.Logger)
return &Config{
ReplicationCfg: &replicator.Config{
PersistenceMaxQPS: dc.GetIntProperty(dynamicconfig.WorkerPersistenceMaxQPS, 500),
ReplicatorMetaTaskConcurrency: dc.GetIntProperty(dynamicconfig.WorkerReplicatorMetaTaskConcurrency, 64),
ReplicatorTaskConcurrency: dc.GetIntProperty(dynamicconfig.WorkerReplicatorTaskConcurrency, 256),
ReplicatorMessageConcurrency: dc.GetIntProperty(dynamicconfig.WorkerReplicatorMessageConcurrency, 2048),
ReplicatorActivityBufferRetryCount: dc.GetIntProperty(dynamicconfig.WorkerReplicatorActivityBufferRetryCount, 8),
ReplicatorHistoryBufferRetryCount: dc.GetIntProperty(dynamicconfig.WorkerReplicatorHistoryBufferRetryCount, 8),
ReplicationTaskMaxRetryCount: dc.GetIntProperty(dynamicconfig.WorkerReplicationTaskMaxRetryCount, 400),
ReplicationTaskMaxRetryDuration: dc.GetDurationProperty(dynamicconfig.WorkerReplicationTaskMaxRetryDuration, 15*time.Minute),
},
ArchiverConfig: &archiver.Config{
ArchiverConcurrency: dc.GetIntProperty(dynamicconfig.WorkerArchiverConcurrency, 50),
ArchivalsPerIteration: dc.GetIntProperty(dynamicconfig.WorkerArchivalsPerIteration, 1000),
TimeLimitPerArchivalIteration: dc.GetDurationProperty(dynamicconfig.WorkerTimeLimitPerArchivalIteration, archiver.MaxArchivalIterationTimeout()),
},
IndexerCfg: &indexer.Config{
IndexerConcurrency: dc.GetIntProperty(dynamicconfig.WorkerIndexerConcurrency, 1000),
ESProcessorNumOfWorkers: dc.GetIntProperty(dynamicconfig.WorkerESProcessorNumOfWorkers, 1),
ESProcessorBulkActions: dc.GetIntProperty(dynamicconfig.WorkerESProcessorBulkActions, 1000),
ESProcessorBulkSize: dc.GetIntProperty(dynamicconfig.WorkerESProcessorBulkSize, 2<<24), // 16MB
ESProcessorFlushInterval: dc.GetDurationProperty(dynamicconfig.WorkerESProcessorFlushInterval, 1*time.Second),
ValidSearchAttributes: dc.GetMapProperty(dynamicconfig.ValidSearchAttributes, definition.GetDefaultIndexedKeys()),
},
ScannerCfg: &scanner.Config{
PersistenceMaxQPS: dc.GetIntProperty(dynamicconfig.ScannerPersistenceMaxQPS, 100),
Persistence: ¶ms.PersistenceConfig,
ClusterMetadata: params.ClusterMetadata,
},
BatcherCfg: &batcher.Config{
AdminOperationToken: dc.GetStringProperty(dynamicconfig.AdminOperationToken, common.DefaultAdminOperationToken),
ClusterMetadata: params.ClusterMetadata,
},
EnableBatcher: dc.GetBoolProperty(dynamicconfig.EnableBatcher, false),
ThrottledLogRPS: dc.GetIntProperty(dynamicconfig.WorkerThrottledLogRPS, 20),
}
}
// Start is called to start the service
func (s *Service) Start() {
base := service.New(s.params)
base.Start()
s.logger = base.GetLogger()
s.metricsClient = base.GetMetricsClient()
s.logger.Info("service starting", tag.ComponentWorker)
if s.params.ESConfig.Enable {
s.startIndexer(base)
}
replicatorEnabled := base.GetClusterMetadata().IsGlobalDomainEnabled()
archiverEnabled := base.GetArchivalMetadata().GetHistoryConfig().ClusterConfiguredForArchival()
scannerEnabled := s.config.ScannerCfg.Persistence.DefaultStoreType() == config.StoreTypeSQL
batcherEnabled := s.config.EnableBatcher()
if replicatorEnabled || archiverEnabled || scannerEnabled || batcherEnabled {
pConfig := s.params.PersistenceConfig
pConfig.SetMaxQPS(pConfig.DefaultStore, s.config.ReplicationCfg.PersistenceMaxQPS())
pFactory := persistencefactory.New(&pConfig, s.params.ClusterMetadata.GetCurrentClusterName(), s.metricsClient, s.logger)
if archiverEnabled || scannerEnabled {
s.ensureSystemDomainExists(pFactory, base.GetClusterMetadata().GetCurrentClusterName())
}
if replicatorEnabled {
s.startReplicator(base, pFactory)
}
if archiverEnabled {
s.startArchiver(base, pFactory)
}
if scannerEnabled {
s.startScanner(base)
}
if batcherEnabled {
s.startBatcher(base)
}
}
s.logger.Info("service started", tag.ComponentWorker)
<-s.stopC
base.Stop()
}
// Stop is called to stop the service
func (s *Service) Stop() {
if !atomic.CompareAndSwapInt32(&s.isStopped, 0, 1) {
return
}
close(s.stopC)
s.params.Logger.Info("service stopped", tag.ComponentWorker)
}
func (s *Service) startBatcher(base service.Service) {
params := &batcher.BootstrapParams{
Config: *s.config.BatcherCfg,
ServiceClient: s.params.PublicClient,
MetricsClient: s.metricsClient,
Logger: s.logger,
TallyScope: s.params.MetricScope,
ClientBean: base.GetClientBean(),
}
batcher := batcher.New(params)
if err := batcher.Start(); err != nil {
s.logger.Fatal("error starting batcher", tag.Error(err))
}
}
func (s *Service) startScanner(base service.Service) {
params := &scanner.BootstrapParams{
Config: *s.config.ScannerCfg,
SDKClient: s.params.PublicClient,
MetricsClient: s.metricsClient,
Logger: s.logger,
TallyScope: s.params.MetricScope,
}
scanner := scanner.New(params)
if err := scanner.Start(); err != nil {
s.logger.Fatal("error starting scanner", tag.Error(err))
}
}
func (s *Service) startReplicator(base service.Service, pFactory persistencefactory.Factory) {
metadataV2Mgr, err := pFactory.NewMetadataManager(persistencefactory.MetadataV2)
if err != nil {
s.logger.Fatal("failed to start replicator, could not create MetadataManager", tag.Error(err))
}
domainCache := cache.NewDomainCache(metadataV2Mgr, base.GetClusterMetadata(), s.metricsClient, s.logger)
domainCache.Start()
replicator := replicator.NewReplicator(
base.GetClusterMetadata(),
metadataV2Mgr,
domainCache,
base.GetClientBean(),
s.config.ReplicationCfg,
base.GetMessagingClient(),
s.logger,
s.metricsClient)
if err := replicator.Start(); err != nil {
replicator.Stop()
s.logger.Fatal("fail to start replicator", tag.Error(err))
}
}
func (s *Service) startIndexer(base service.Service) {
indexer := indexer.NewIndexer(
s.config.IndexerCfg,
base.GetMessagingClient(),
s.params.ESClient,
s.params.ESConfig,
s.logger,
s.metricsClient)
if err := indexer.Start(); err != nil {
indexer.Stop()
s.logger.Fatal("fail to start indexer", tag.Error(err))
}
}
func (s *Service) startArchiver(base service.Service, pFactory persistencefactory.Factory) {
publicClient := s.params.PublicClient
historyManager, err := pFactory.NewHistoryManager()
if err != nil {
s.logger.Fatal("failed to start archiver, could not create HistoryManager", tag.Error(err))
}
historyV2Manager, err := pFactory.NewHistoryV2Manager()
if err != nil {
s.logger.Fatal("failed to start archiver, could not create HistoryV2Manager", tag.Error(err))
}
metadataMgr, err := pFactory.NewMetadataManager(persistencefactory.MetadataV1V2)
if err != nil {
s.logger.Fatal("failed to start archiver, could not create MetadataManager", tag.Error(err))
}
domainCache := cache.NewDomainCache(metadataMgr, s.params.ClusterMetadata, s.metricsClient, s.logger)
domainCache.Start()
historyArchiverBootstrapContainer := &carchiver.HistoryBootstrapContainer{
HistoryManager: historyManager,
HistoryV2Manager: historyV2Manager,
Logger: s.logger,
MetricsClient: s.metricsClient,
ClusterMetadata: base.GetClusterMetadata(),
DomainCache: domainCache,
}
archiverProvider := base.GetArchiverProvider()
err = archiverProvider.RegisterBootstrapContainer(common.WorkerServiceName, historyArchiverBootstrapContainer, &carchiver.VisibilityBootstrapContainer{})
if err != nil {
s.logger.Fatal("failed to register archiver bootstrap container", tag.Error(err))
}
bc := &archiver.BootstrapContainer{
PublicClient: publicClient,
MetricsClient: s.metricsClient,
Logger: s.logger,
HistoryManager: historyManager,
HistoryV2Manager: historyV2Manager,
DomainCache: domainCache,
Config: s.config.ArchiverConfig,
ArchiverProvider: archiverProvider,
}
clientWorker := archiver.NewClientWorker(bc)
if err := clientWorker.Start(); err != nil {
clientWorker.Stop()
s.logger.Fatal("failed to start archiver", tag.Error(err))
}
}
func (s *Service) ensureSystemDomainExists(pFactory persistencefactory.Factory, clusterName string) {
metadataProxy, err := pFactory.NewMetadataManager(persistencefactory.MetadataV1V2)
if err != nil {
s.logger.Fatal("error creating metadataMgr proxy", tag.Error(err))
}
defer metadataProxy.Close()
_, err = metadataProxy.GetDomain(&persistence.GetDomainRequest{Name: common.SystemLocalDomainName})
switch err.(type) {
case nil:
return
case *shared.EntityNotExistsError:
s.logger.Info("cadence-system domain does not exist, attempting to register domain")
s.registerSystemDomain(pFactory, clusterName)
default:
s.logger.Fatal("failed to verify if cadence system domain exists", tag.Error(err))
}
}
func (s *Service) registerSystemDomain(pFactory persistencefactory.Factory, clusterName string) {
metadataV2, err := pFactory.NewMetadataManager(persistencefactory.MetadataV2)
if err != nil {
s.logger.Fatal("error creating metadataV2Mgr", tag.Error(err))
}
defer metadataV2.Close()
_, err = metadataV2.CreateDomain(&persistence.CreateDomainRequest{
Info: &persistence.DomainInfo{
ID: common.SystemDomainID,
Name: common.SystemLocalDomainName,
Description: "Cadence internal system domain",
},
Config: &persistence.DomainConfig{
Retention: common.SystemDomainRetentionDays,
EmitMetric: true,
},
ReplicationConfig: &persistence.DomainReplicationConfig{
ActiveClusterName: clusterName,
Clusters: persistence.GetOrUseDefaultClusters(clusterName, nil),
},
IsGlobalDomain: false,
FailoverVersion: common.EmptyVersion,
})
if err != nil {
if _, ok := err.(*shared.DomainAlreadyExistsError); ok {
return
}
s.logger.Fatal("failed to register system domain", tag.Error(err))
}
// this is needed because frontend domainCache will take about 10s to load the
// domain after its created first time. Archiver/Scanner cannot start their cadence
// workers until this refresh happens
time.Sleep(domainRefreshInterval)
}