forked from uber/cadence
/
replicatorQueueProcessor.go
492 lines (435 loc) · 17.8 KB
/
replicatorQueueProcessor.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
// 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 history
import (
"errors"
"time"
"github.com/uber/cadence/.gen/go/replicator"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/messaging"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
)
type (
replicatorQueueProcessorImpl struct {
currentClusterNamer string
shard ShardContext
historyCache *historyCache
replicationTaskFilter queueTaskFilter
executionMgr persistence.ExecutionManager
historyMgr persistence.HistoryManager
historyV2Mgr persistence.HistoryV2Manager
replicator messaging.Producer
metricsClient metrics.Client
options *QueueProcessorOptions
logger log.Logger
*queueProcessorBase
queueAckMgr
lastShardSyncTimestamp time.Time
}
)
var (
errUnknownReplicationTask = errors.New("Unknown replication task")
errHistoryNotFoundTask = errors.New("History not found")
defaultHistoryPageSize = 1000
)
func newReplicatorQueueProcessor(shard ShardContext, historyCache *historyCache, replicator messaging.Producer,
executionMgr persistence.ExecutionManager, historyMgr persistence.HistoryManager, historyV2Mgr persistence.HistoryV2Manager, logger log.Logger) queueProcessor {
currentClusterNamer := shard.GetService().GetClusterMetadata().GetCurrentClusterName()
config := shard.GetConfig()
options := &QueueProcessorOptions{
BatchSize: config.ReplicatorTaskBatchSize,
WorkerCount: config.ReplicatorTaskWorkerCount,
MaxPollRPS: config.ReplicatorProcessorMaxPollRPS,
MaxPollInterval: config.ReplicatorProcessorMaxPollInterval,
MaxPollIntervalJitterCoefficient: config.ReplicatorProcessorMaxPollIntervalJitterCoefficient,
UpdateAckInterval: config.ReplicatorProcessorUpdateAckInterval,
UpdateAckIntervalJitterCoefficient: config.ReplicatorProcessorUpdateAckIntervalJitterCoefficient,
MaxRetryCount: config.ReplicatorTaskMaxRetryCount,
MetricScope: metrics.ReplicatorQueueProcessorScope,
}
logger = logger.WithTags(tag.ComponentReplicatorQueue)
replicationTaskFilter := func(qTask queueTaskInfo) (bool, error) {
return true, nil
}
processor := &replicatorQueueProcessorImpl{
currentClusterNamer: currentClusterNamer,
shard: shard,
historyCache: historyCache,
replicationTaskFilter: replicationTaskFilter,
executionMgr: executionMgr,
historyMgr: historyMgr,
historyV2Mgr: historyV2Mgr,
replicator: replicator,
metricsClient: shard.GetMetricsClient(),
options: options,
logger: logger,
}
queueAckMgr := newQueueAckMgr(shard, options, processor, shard.GetReplicatorAckLevel(), logger)
queueProcessorBase := newQueueProcessorBase(currentClusterNamer, shard, options, processor, queueAckMgr, logger)
processor.queueAckMgr = queueAckMgr
processor.queueProcessorBase = queueProcessorBase
return processor
}
func (p *replicatorQueueProcessorImpl) getTaskFilter() queueTaskFilter {
return p.replicationTaskFilter
}
func (p *replicatorQueueProcessorImpl) process(qTask queueTaskInfo, shouldProcessTask bool) (int, error) {
task, ok := qTask.(*persistence.ReplicationTaskInfo)
if !ok {
return metrics.ReplicatorQueueProcessorScope, errUnexpectedQueueTask
}
// replication queue should always process all tasks
// so should not do anything to shouldProcessTask variable
switch task.TaskType {
case persistence.ReplicationTaskTypeSyncActivity:
err := p.processSyncActivityTask(task)
if err == nil {
err = p.executionMgr.CompleteReplicationTask(&persistence.CompleteReplicationTaskRequest{TaskID: task.GetTaskID()})
}
return metrics.ReplicatorTaskSyncActivityScope, err
case persistence.ReplicationTaskTypeHistory:
err := p.processHistoryReplicationTask(task)
if _, ok := err.(*shared.EntityNotExistsError); ok {
err = errHistoryNotFoundTask
}
if err == nil {
err = p.executionMgr.CompleteReplicationTask(&persistence.CompleteReplicationTaskRequest{TaskID: task.GetTaskID()})
}
return metrics.ReplicatorTaskHistoryScope, err
default:
return metrics.ReplicatorQueueProcessorScope, errUnknownReplicationTask
}
}
func (p *replicatorQueueProcessorImpl) queueShutdown() error {
// there is no shutdown specific behavior for replication queue
return nil
}
func (p *replicatorQueueProcessorImpl) processSyncActivityTask(task *persistence.ReplicationTaskInfo) (retError error) {
domainID := task.DomainID
execution := shared.WorkflowExecution{
WorkflowId: common.StringPtr(task.WorkflowID),
RunId: common.StringPtr(task.RunID),
}
context, release, err := p.historyCache.getOrCreateWorkflowExecutionForBackground(domainID, execution)
if err != nil {
return err
}
defer func() { release(retError) }()
msBuilder, err := context.loadWorkflowExecution()
if err != nil {
if _, ok := err.(*shared.EntityNotExistsError); ok {
return nil
}
return err
}
if !msBuilder.IsWorkflowExecutionRunning() {
// workflow already finished, no need to process the timer
return nil
}
activityInfo, ok := msBuilder.GetActivityInfo(task.ScheduledID)
if !ok {
return nil
}
var startedTime *int64
var heartbeatTime *int64
scheduledTime := common.Int64Ptr(activityInfo.ScheduledTime.UnixNano())
if activityInfo.StartedID != common.EmptyEventID {
startedTime = common.Int64Ptr(activityInfo.StartedTime.UnixNano())
}
// LastHeartBeatUpdatedTime must be valid when getting the sync activity replication task
heartbeatTime = common.Int64Ptr(activityInfo.LastHeartBeatUpdatedTime.UnixNano())
replicationTask := &replicator.ReplicationTask{
TaskType: replicator.ReplicationTaskType.Ptr(replicator.ReplicationTaskTypeSyncActivity),
SyncActicvityTaskAttributes: &replicator.SyncActicvityTaskAttributes{
DomainId: common.StringPtr(task.DomainID),
WorkflowId: common.StringPtr(task.WorkflowID),
RunId: common.StringPtr(task.RunID),
Version: common.Int64Ptr(activityInfo.Version),
ScheduledId: common.Int64Ptr(activityInfo.ScheduleID),
ScheduledTime: scheduledTime,
StartedId: common.Int64Ptr(activityInfo.StartedID),
StartedTime: startedTime,
LastHeartbeatTime: heartbeatTime,
Details: activityInfo.Details,
Attempt: common.Int32Ptr(activityInfo.Attempt),
LastFailureReason: common.StringPtr(activityInfo.LastFailureReason),
LastWorkerIdentity: common.StringPtr(activityInfo.LastWorkerIdentity),
},
}
return p.replicator.Publish(replicationTask)
}
func (p *replicatorQueueProcessorImpl) processHistoryReplicationTask(task *persistence.ReplicationTaskInfo) error {
domainEntry, err := p.shard.GetDomainCache().GetDomainByID(task.DomainID)
if err != nil {
return err
}
targetClusters := []string{}
for _, cluster := range domainEntry.GetReplicationConfig().Clusters {
targetClusters = append(targetClusters, cluster.ClusterName)
}
replicationTask, err := GenerateReplicationTask(targetClusters, task, p.historyMgr, p.historyV2Mgr, p.metricsClient, p.logger, nil, common.IntPtr(p.shard.GetShardID()))
if err != nil || replicationTask == nil {
return err
}
err = p.replicator.Publish(replicationTask)
if err == messaging.ErrMessageSizeLimit {
// message size exceeds the server messaging size limit
// for this specific case, just send out a metadata message and
// let receiver fetch from source (for the concrete history events)
err = p.replicator.Publish(p.generateHistoryMetadataTask(targetClusters, task))
}
return err
}
func (p *replicatorQueueProcessorImpl) generateHistoryMetadataTask(targetClusters []string, task *persistence.ReplicationTaskInfo) *replicator.ReplicationTask {
return &replicator.ReplicationTask{
TaskType: replicator.ReplicationTaskTypeHistoryMetadata.Ptr(),
HistoryMetadataTaskAttributes: &replicator.HistoryMetadataTaskAttributes{
TargetClusters: targetClusters,
DomainId: common.StringPtr(task.DomainID),
WorkflowId: common.StringPtr(task.WorkflowID),
RunId: common.StringPtr(task.RunID),
FirstEventId: common.Int64Ptr(task.FirstEventID),
NextEventId: common.Int64Ptr(task.NextEventID),
},
}
}
// GenerateReplicationTask generate replication task
func GenerateReplicationTask(targetClusters []string, task *persistence.ReplicationTaskInfo,
historyMgr persistence.HistoryManager, historyV2Mgr persistence.HistoryV2Manager,
metricsClient metrics.Client, logger log.Logger, history *shared.History, shardID *int,
) (*replicator.ReplicationTask, error) {
var err error
if history == nil {
history, _, err = GetAllHistory(historyMgr, historyV2Mgr, metricsClient, logger, false,
task.DomainID, task.WorkflowID, task.RunID, task.FirstEventID, task.NextEventID, task.EventStoreVersion, task.BranchToken, shardID)
if err != nil {
return nil, err
}
for _, event := range history.Events {
if task.Version != event.GetVersion() {
return nil, nil
}
}
}
var newRunHistory *shared.History
events := history.Events
if len(events) > 0 {
lastEvent := events[len(events)-1]
if lastEvent.GetEventType() == shared.EventTypeWorkflowExecutionContinuedAsNew {
// Check if this is replication task for ContinueAsNew event, then retrieve the history for new execution
newRunID := lastEvent.WorkflowExecutionContinuedAsNewEventAttributes.GetNewExecutionRunId()
newRunHistory, _, err = GetAllHistory(
historyMgr,
historyV2Mgr,
metricsClient,
logger,
false,
task.DomainID,
task.WorkflowID,
newRunID,
common.FirstEventID,
common.FirstEventID+1, // [common.FirstEventID to common.FirstEventID+1) will get the first batch
task.NewRunEventStoreVersion,
task.NewRunBranchToken,
shardID)
if err != nil {
return nil, err
}
}
}
ret := &replicator.ReplicationTask{
TaskType: replicator.ReplicationTaskType.Ptr(replicator.ReplicationTaskTypeHistory),
HistoryTaskAttributes: &replicator.HistoryTaskAttributes{
TargetClusters: targetClusters,
DomainId: common.StringPtr(task.DomainID),
WorkflowId: common.StringPtr(task.WorkflowID),
RunId: common.StringPtr(task.RunID),
FirstEventId: common.Int64Ptr(task.FirstEventID),
NextEventId: common.Int64Ptr(task.NextEventID),
Version: common.Int64Ptr(task.Version),
ReplicationInfo: convertLastReplicationInfo(task.LastReplicationInfo),
History: history,
NewRunHistory: newRunHistory,
EventStoreVersion: common.Int32Ptr(task.EventStoreVersion),
NewRunEventStoreVersion: common.Int32Ptr(task.NewRunEventStoreVersion),
ResetWorkflow: common.BoolPtr(task.ResetWorkflow),
},
}
return ret, nil
}
func (p *replicatorQueueProcessorImpl) readTasks(readLevel int64) ([]queueTaskInfo, bool, error) {
response, err := p.executionMgr.GetReplicationTasks(&persistence.GetReplicationTasksRequest{
ReadLevel: readLevel,
MaxReadLevel: p.shard.GetTransferMaxReadLevel(),
BatchSize: p.options.BatchSize(),
})
if err != nil {
return nil, false, err
}
tasks := make([]queueTaskInfo, len(response.Tasks))
for i := range response.Tasks {
tasks[i] = response.Tasks[i]
}
return tasks, len(response.NextPageToken) != 0, nil
}
func (p *replicatorQueueProcessorImpl) updateAckLevel(ackLevel int64) error {
err := p.shard.UpdateReplicatorAckLevel(ackLevel)
// this is a hack, since there is not dedicated ticker on the queue processor
// to periodically send out sync shard message, put it here
now := clock.NewRealTimeSource().Now()
if p.lastShardSyncTimestamp.Add(p.shard.GetConfig().ShardSyncMinInterval()).Before(now) {
syncStatusTask := &replicator.ReplicationTask{
TaskType: replicator.ReplicationTaskType.Ptr(replicator.ReplicationTaskTypeSyncShardStatus),
SyncShardStatusTaskAttributes: &replicator.SyncShardStatusTaskAttributes{
SourceCluster: common.StringPtr(p.currentClusterNamer),
ShardId: common.Int64Ptr(int64(p.shard.GetShardID())),
Timestamp: common.Int64Ptr(now.UnixNano()),
},
}
// ignore the error
if syncErr := p.replicator.Publish(syncStatusTask); syncErr == nil {
p.lastShardSyncTimestamp = now
}
}
return err
}
// GetAllHistory return history
func GetAllHistory(historyMgr persistence.HistoryManager, historyV2Mgr persistence.HistoryV2Manager,
metricsClient metrics.Client, logger log.Logger, byBatch bool,
domainID string, workflowID string, runID string, firstEventID int64,
nextEventID int64, eventStoreVersion int32, branchToken []byte, shardID *int) (*shared.History, []*shared.History, error) {
// overall result
historyEvents := []*shared.HistoryEvent{}
historyBatches := []*shared.History{}
historySize := 0
var err error
// variable used for each page
pageHistoryEvents := []*shared.HistoryEvent{}
pageHistoryBatches := []*shared.History{}
var pageToken []byte
var pageHistorySize int
for hasMore := true; hasMore; hasMore = len(pageToken) > 0 {
pageHistoryEvents, pageHistoryBatches, pageToken, pageHistorySize, err = PaginateHistory(
historyMgr, historyV2Mgr, metricsClient, logger, byBatch,
domainID, workflowID, runID, firstEventID, nextEventID, pageToken,
eventStoreVersion, branchToken, defaultHistoryPageSize, shardID,
)
if err != nil {
return nil, nil, err
}
historyEvents = append(historyEvents, pageHistoryEvents...)
historyBatches = append(historyBatches, pageHistoryBatches...)
historySize += pageHistorySize
}
// Emit metric and log for history size
if metricsClient != nil {
metricsClient.RecordTimer(metrics.ReplicatorQueueProcessorScope, metrics.HistorySize, time.Duration(historySize))
}
history := &shared.History{
Events: historyEvents,
}
return history, historyBatches, nil
}
// PaginateHistory return paged history
func PaginateHistory(historyMgr persistence.HistoryManager, historyV2Mgr persistence.HistoryV2Manager,
metricsClient metrics.Client, logger log.Logger, byBatch bool,
domainID, workflowID, runID string, firstEventID,
nextEventID int64, tokenIn []byte, eventStoreVersion int32, branchToken []byte, pageSize int, shardID *int) ([]*shared.HistoryEvent, []*shared.History, []byte, int, error) {
historyEvents := []*shared.HistoryEvent{}
historyBatches := []*shared.History{}
var tokenOut []byte
var historySize int
if eventStoreVersion == persistence.EventStoreVersionV2 {
req := &persistence.ReadHistoryBranchRequest{
BranchToken: branchToken,
MinEventID: firstEventID,
MaxEventID: nextEventID,
PageSize: pageSize,
NextPageToken: tokenIn,
ShardID: shardID,
}
if byBatch {
response, err := historyV2Mgr.ReadHistoryBranchByBatch(req)
if err != nil {
return nil, nil, nil, 0, err
}
// Keep track of total history size
historySize += response.Size
historyBatches = append(historyBatches, response.History...)
tokenOut = response.NextPageToken
} else {
response, err := historyV2Mgr.ReadHistoryBranch(req)
if err != nil {
return nil, nil, nil, 0, err
}
// Keep track of total history size
historySize += response.Size
historyEvents = append(historyEvents, response.HistoryEvents...)
tokenOut = response.NextPageToken
}
} else {
req := &persistence.GetWorkflowExecutionHistoryRequest{
DomainID: domainID,
Execution: shared.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: common.StringPtr(runID),
},
FirstEventID: firstEventID,
NextEventID: nextEventID,
PageSize: pageSize,
NextPageToken: tokenIn,
}
if byBatch {
response, err := historyMgr.GetWorkflowExecutionHistoryByBatch(req)
if err != nil {
return nil, nil, nil, 0, err
}
// Keep track of total history size
historySize += response.Size
historyBatches = append(historyBatches, response.History...)
tokenOut = response.NextPageToken
} else {
response, err := historyMgr.GetWorkflowExecutionHistory(req)
if err != nil {
return nil, nil, nil, 0, err
}
// Keep track of total history size
historySize += response.Size
historyEvents = append(historyEvents, response.History.Events...)
tokenOut = response.NextPageToken
}
}
return historyEvents, historyBatches, tokenOut, historySize, nil
}
func convertLastReplicationInfo(info map[string]*persistence.ReplicationInfo) map[string]*shared.ReplicationInfo {
replicationInfoMap := make(map[string]*shared.ReplicationInfo)
for k, v := range info {
replicationInfoMap[k] = &shared.ReplicationInfo{
Version: common.Int64Ptr(v.Version),
LastEventId: common.Int64Ptr(v.LastEventID),
}
}
return replicationInfoMap
}