-
Notifications
You must be signed in to change notification settings - Fork 787
/
domain_replication_processor.go
233 lines (207 loc) · 7.81 KB
/
domain_replication_processor.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
// The MIT License (MIT)
//
// Copyright (c) 2017-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 replicator
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/uber/cadence/client/admin"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/backoff"
"github.com/uber/cadence/common/domain"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/membership"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/common/types"
)
const (
fetchTaskRequestTimeout = 10 * time.Second
pollTimerJitterCoefficient = 0.2
pollIntervalSecs = 1
taskProcessorErrorRetryWait = time.Second
taskProcessorErrorRetryBackoffCoefficient = 1
)
type (
domainReplicationProcessor struct {
hostInfo membership.HostInfo
membershipResolver membership.Resolver
status int32
sourceCluster string
currentCluster string
logger log.Logger
remotePeer admin.Client
taskExecutor domain.ReplicationTaskExecutor
metricsClient metrics.Client
throttleRetry *backoff.ThrottleRetry
lastProcessedMessageID int64
lastRetrievedMessageID int64
done chan struct{}
domainReplicationQueue domain.ReplicationQueue
}
)
func newDomainReplicationProcessor(
sourceCluster string,
currentCluster string,
logger log.Logger,
remotePeer admin.Client,
metricsClient metrics.Client,
taskExecutor domain.ReplicationTaskExecutor,
hostInfo membership.HostInfo,
resolver membership.Resolver,
domainReplicationQueue domain.ReplicationQueue,
replicationMaxRetry time.Duration,
) *domainReplicationProcessor {
retryPolicy := backoff.NewExponentialRetryPolicy(taskProcessorErrorRetryWait)
retryPolicy.SetBackoffCoefficient(taskProcessorErrorRetryBackoffCoefficient)
retryPolicy.SetExpirationInterval(replicationMaxRetry)
throttleRetry := backoff.NewThrottleRetry(
backoff.WithRetryPolicy(retryPolicy),
backoff.WithRetryableError(isTransientRetryableError),
)
return &domainReplicationProcessor{
hostInfo: hostInfo,
membershipResolver: resolver,
status: common.DaemonStatusInitialized,
sourceCluster: sourceCluster,
currentCluster: currentCluster,
logger: logger,
remotePeer: remotePeer,
taskExecutor: taskExecutor,
metricsClient: metricsClient,
throttleRetry: throttleRetry,
lastProcessedMessageID: -1,
lastRetrievedMessageID: -1,
done: make(chan struct{}),
domainReplicationQueue: domainReplicationQueue,
}
}
func (p *domainReplicationProcessor) Start() {
if !atomic.CompareAndSwapInt32(&p.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
go p.processorLoop()
}
func (p *domainReplicationProcessor) processorLoop() {
timer := time.NewTimer(getWaitDuration())
for {
select {
case <-timer.C:
p.fetchDomainReplicationTasks()
timer.Reset(getWaitDuration())
case <-p.done:
timer.Stop()
return
}
}
}
func (p *domainReplicationProcessor) fetchDomainReplicationTasks() {
// The following is a best effort to make sure only one worker is processing tasks for a
// particular source cluster. When the ring is under reconfiguration, it is possible that
// for a small period of time two or more workers think they are the owner and try to execute
// the processing logic. This will not result in correctness issue as domain replication task
// processing will be protected by version check.
info, err := p.membershipResolver.Lookup(service.Worker, p.sourceCluster)
if err != nil {
p.logger.Info("Failed to lookup host info. Skip current run.")
return
}
if info.Identity() != p.hostInfo.Identity() {
p.logger.Debug(fmt.Sprintf("Worker not responsible for source cluster %v.", p.sourceCluster))
return
}
ctx, cancel := context.WithTimeout(context.Background(), fetchTaskRequestTimeout)
request := &types.GetDomainReplicationMessagesRequest{
LastRetrievedMessageID: common.Int64Ptr(p.lastRetrievedMessageID),
LastProcessedMessageID: common.Int64Ptr(p.lastProcessedMessageID),
ClusterName: p.currentCluster,
}
response, err := p.remotePeer.GetDomainReplicationMessages(ctx, request)
defer cancel()
if err != nil {
p.logger.Error("Failed to get replication tasks", tag.Error(err))
return
}
p.logger.Debug("Successfully fetched domain replication tasks.", tag.Counter(len(response.Messages.ReplicationTasks)))
for taskIndex := range response.Messages.ReplicationTasks {
task := response.Messages.ReplicationTasks[taskIndex]
err := p.throttleRetry.Do(context.Background(), func() error {
return p.handleDomainReplicationTask(task)
})
if err != nil {
p.logger.Error("Failed to apply domain replication tasks", tag.Error(err))
dlqErr := p.throttleRetry.Do(context.Background(), func() error {
return p.putDomainReplicationTaskToDLQ(task)
})
if dlqErr != nil {
p.logger.Error("Failed to put replication tasks to DLQ", tag.Error(dlqErr))
p.metricsClient.IncCounter(metrics.DomainReplicationTaskScope, metrics.ReplicatorDLQFailures)
return
}
}
}
p.lastProcessedMessageID = response.Messages.GetLastRetrievedMessageID()
p.lastRetrievedMessageID = response.Messages.GetLastRetrievedMessageID()
}
func (p *domainReplicationProcessor) putDomainReplicationTaskToDLQ(
task *types.ReplicationTask,
) error {
domainAttribute := task.GetDomainTaskAttributes()
if domainAttribute == nil {
return &types.InternalServiceError{
Message: "Domain replication task does not set domain task attribute",
}
}
p.metricsClient.Scope(
metrics.DomainReplicationTaskScope,
metrics.DomainTag(domainAttribute.GetInfo().GetName()),
).IncCounter(metrics.DomainReplicationEnqueueDLQCount)
return p.domainReplicationQueue.PublishToDLQ(context.Background(), task)
}
func (p *domainReplicationProcessor) handleDomainReplicationTask(
task *types.ReplicationTask,
) error {
p.metricsClient.IncCounter(metrics.DomainReplicationTaskScope, metrics.ReplicatorMessages)
sw := p.metricsClient.StartTimer(metrics.DomainReplicationTaskScope, metrics.ReplicatorLatency)
defer sw.Stop()
err := p.taskExecutor.Execute(task.DomainTaskAttributes)
if err != nil {
p.metricsClient.IncCounter(metrics.DomainReplicationTaskScope, metrics.ReplicatorFailures)
}
return err
}
func (p *domainReplicationProcessor) Stop() {
close(p.done)
}
func getWaitDuration() time.Duration {
return backoff.JitDuration(time.Duration(pollIntervalSecs)*time.Second, pollTimerJitterCoefficient)
}
func isTransientRetryableError(err error) bool {
switch err.(type) {
case *types.BadRequestError:
return false
default:
return true
}
}