-
Notifications
You must be signed in to change notification settings - Fork 766
/
coordinator.go
338 lines (292 loc) · 9.6 KB
/
coordinator.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
// 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.
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination coordinator_mock.go -self_package github.com/uber/cadence/service/history/failover
package failover
import (
ctx "context"
"sync/atomic"
"time"
workflow "github.com/uber/cadence/.gen/go/history"
"github.com/uber/cadence/.gen/go/replicator"
"github.com/uber/cadence/client/history"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/backoff"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/domain"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/service/history/config"
)
const (
notificationChanBufferSize = 800
receiveChanBufferSize = 400
cleanupMarkerInterval = 30 * time.Minute
invalidMarkerDuration = 1 * time.Hour
updateDomainRetryInitialInterval = 50 * time.Millisecond
updateDomainRetryCoefficient = 2.0
updateDomainMaxRetry = 2
)
type (
// Coordinator manages the failover markers on sending and receiving
Coordinator interface {
common.Daemon
NotifyFailoverMarkers(shardID int32, markers []*replicator.FailoverMarkerAttributes)
ReceiveFailoverMarkers(shardIDs []int32, marker *replicator.FailoverMarkerAttributes)
}
coordinatorImpl struct {
status int32
recorder map[string]*failoverRecord
notificationChan chan *notificationRequest
receiveChan chan *receiveRequest
shutdownChan chan struct{}
retryPolicy backoff.RetryPolicy
metadataMgr persistence.MetadataManager
historyClient history.Client
config *config.Config
timeSource clock.TimeSource
metrics metrics.Client
logger log.Logger
}
notificationRequest struct {
shardID int32
markers []*replicator.FailoverMarkerAttributes
}
receiveRequest struct {
shardIDs []int32
marker *replicator.FailoverMarkerAttributes
}
failoverRecord struct {
failoverVersion int64
shards map[int32]struct{}
lastUpdatedTime time.Time
}
)
// NewCoordinator initialize a failover coordinator
func NewCoordinator(
metadataMgr persistence.MetadataManager,
historyClient history.Client,
timeSource clock.TimeSource,
config *config.Config,
metrics metrics.Client,
logger log.Logger,
) Coordinator {
retryPolicy := backoff.NewExponentialRetryPolicy(updateDomainRetryInitialInterval)
retryPolicy.SetBackoffCoefficient(updateDomainRetryCoefficient)
retryPolicy.SetMaximumAttempts(updateDomainMaxRetry)
return &coordinatorImpl{
status: common.DaemonStatusInitialized,
recorder: make(map[string]*failoverRecord),
notificationChan: make(chan *notificationRequest, notificationChanBufferSize),
receiveChan: make(chan *receiveRequest, receiveChanBufferSize),
shutdownChan: make(chan struct{}),
retryPolicy: retryPolicy,
metadataMgr: metadataMgr,
historyClient: historyClient,
timeSource: timeSource,
config: config,
metrics: metrics,
logger: logger.WithTags(tag.ComponentFailoverCoordinator),
}
}
func (c *coordinatorImpl) Start() {
if !atomic.CompareAndSwapInt32(
&c.status,
common.DaemonStatusInitialized,
common.DaemonStatusStarted,
) {
return
}
go c.receiveFailoverMarkersLoop()
go c.notifyFailoverMarkerLoop()
c.logger.Info("Coordinator state changed", tag.LifeCycleStarted)
}
func (c *coordinatorImpl) Stop() {
if !atomic.CompareAndSwapInt32(
&c.status,
common.DaemonStatusStarted,
common.DaemonStatusStopped,
) {
return
}
close(c.shutdownChan)
c.logger.Info("Coordinator state changed", tag.LifeCycleStopped)
}
func (c *coordinatorImpl) NotifyFailoverMarkers(
shardID int32,
markers []*replicator.FailoverMarkerAttributes,
) {
c.notificationChan <- ¬ificationRequest{
shardID: shardID,
markers: markers,
}
}
func (c *coordinatorImpl) ReceiveFailoverMarkers(
shardIDs []int32,
marker *replicator.FailoverMarkerAttributes,
) {
c.receiveChan <- &receiveRequest{
shardIDs: shardIDs,
marker: marker,
}
}
func (c *coordinatorImpl) receiveFailoverMarkersLoop() {
ticker := time.NewTicker(cleanupMarkerInterval)
defer ticker.Stop()
for {
select {
case <-c.shutdownChan:
return
case <-ticker.C:
c.cleanupInvalidMarkers()
case request := <-c.receiveChan:
c.handleFailoverMarkers(request)
}
}
}
func (c *coordinatorImpl) notifyFailoverMarkerLoop() {
timer := time.NewTimer(backoff.JitDuration(
c.config.NotifyFailoverMarkerInterval(),
c.config.NotifyFailoverMarkerTimerJitterCoefficient(),
))
defer timer.Stop()
requestByMarker := make(map[*replicator.FailoverMarkerAttributes]*receiveRequest)
for {
select {
case <-c.shutdownChan:
return
case notificationReq := <-c.notificationChan:
// if there is a shard movement happen, it is fine to have duplicate shard ID in the request
// The receiver side will de-dup the shard IDs. See: handleFailoverMarkers
aggregateNotificationRequests(notificationReq, requestByMarker)
case <-timer.C:
c.notifyRemoteCoordinator(requestByMarker)
timer.Reset(backoff.JitDuration(
c.config.NotifyFailoverMarkerInterval(),
c.config.NotifyFailoverMarkerTimerJitterCoefficient(),
))
}
}
}
func (c *coordinatorImpl) handleFailoverMarkers(
request *receiveRequest,
) {
marker := request.marker
domainID := marker.GetDomainID()
if record, ok := c.recorder[domainID]; ok {
// if the local failover version is smaller than the new received marker,
// it means there is another failover happened and the local one should be invalid.
if record.failoverVersion < marker.GetFailoverVersion() {
delete(c.recorder, domainID)
}
// if the local failover version is larger than the new received marker,
// ignore the incoming marker
if record.failoverVersion > marker.GetFailoverVersion() {
return
}
}
if _, ok := c.recorder[domainID]; !ok {
// initialize the failover record
c.recorder[marker.GetDomainID()] = &failoverRecord{
failoverVersion: marker.GetFailoverVersion(),
shards: make(map[int32]struct{}),
}
}
record := c.recorder[domainID]
record.lastUpdatedTime = c.timeSource.Now()
for _, shardID := range request.shardIDs {
record.shards[shardID] = struct{}{}
}
if len(record.shards) == c.config.NumberOfShards {
if err := domain.CleanPendingActiveState(
c.metadataMgr,
domainID,
record.failoverVersion,
c.retryPolicy,
); err != nil {
c.logger.Error("Coordinator failed to update domain after receiving all failover markers",
tag.WorkflowDomainID(domainID))
c.metrics.IncCounter(metrics.FailoverMarkerScope, metrics.GracefulFailoverFailure)
return
}
delete(c.recorder, domainID)
now := c.timeSource.Now()
c.metrics.Scope(
metrics.FailoverMarkerScope,
).RecordTimer(
metrics.GracefulFailoverLatency,
now.Sub(time.Unix(0, marker.GetCreationTime())),
)
c.logger.Info("Updated domain from pending-active to active",
tag.WorkflowDomainID(domainID),
tag.FailoverVersion(*marker.FailoverVersion),
)
}
}
func (c *coordinatorImpl) cleanupInvalidMarkers() {
for domainID, record := range c.recorder {
if c.timeSource.Now().Sub(record.lastUpdatedTime) > invalidMarkerDuration {
delete(c.recorder, domainID)
}
}
}
func (c *coordinatorImpl) notifyRemoteCoordinator(
requestByMarker map[*replicator.FailoverMarkerAttributes]*receiveRequest,
) {
if len(requestByMarker) > 0 {
var tokens []*workflow.FailoverMarkerToken
for _, request := range requestByMarker {
tokens = append(tokens, &workflow.FailoverMarkerToken{
ShardIDs: request.shardIDs,
FailoverMarker: request.marker,
})
}
if err := c.historyClient.NotifyFailoverMarkers(
ctx.Background(),
&workflow.NotifyFailoverMarkersRequest{
FailoverMarkerTokens: tokens,
},
); err != nil {
c.metrics.IncCounter(metrics.FailoverMarkerScope, metrics.FailoverMarkerNotificationFailure)
c.logger.Error("Failed to notify failover markers", tag.Error(err))
}
for marker := range requestByMarker {
delete(requestByMarker, marker)
}
}
}
func aggregateNotificationRequests(
request *notificationRequest,
requestByMarker map[*replicator.FailoverMarkerAttributes]*receiveRequest,
) {
for _, marker := range request.markers {
if _, ok := requestByMarker[marker]; !ok {
requestByMarker[marker] = &receiveRequest{
shardIDs: []int32{},
marker: marker,
}
}
req := requestByMarker[marker]
req.shardIDs = append(req.shardIDs, request.shardID)
}
}