/
queueAckMgr.go
238 lines (207 loc) · 7.28 KB
/
queueAckMgr.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
// 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 (
"fmt"
"sort"
"sync"
"time"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/backoff"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/service/history/shard"
"github.com/uber/cadence/service/history/task"
)
type (
// queueAckMgr is created by QueueProcessor to keep track of the queue ackLevel for the shard.
// It keeps track of read level when dispatching tasks to processor and maintains a map of outstanding tasks.
// Outstanding tasks map uses the task id sequencer as the key, which is used by updateAckLevel to move the ack level
// for the shard when all preceding tasks are acknowledged.
queueAckMgrImpl struct {
isFailover bool
shard shard.Context
options *QueueProcessorOptions
processor processor
logger log.Logger
metricsClient metrics.Client
finishedChan chan struct{}
sync.RWMutex
outstandingTasks map[int64]bool
readLevel int64
ackLevel int64
isReadFinished bool
}
)
const (
warnPendingTasks = 2000
)
var (
persistenceOperationRetryPolicy = common.CreatePersistenceRetryPolicy()
)
func newQueueAckMgr(shard shard.Context, options *QueueProcessorOptions, processor processor, ackLevel int64, logger log.Logger) *queueAckMgrImpl {
return &queueAckMgrImpl{
isFailover: false,
shard: shard,
options: options,
processor: processor,
outstandingTasks: make(map[int64]bool),
readLevel: ackLevel,
ackLevel: ackLevel,
logger: logger,
metricsClient: shard.GetMetricsClient(),
finishedChan: nil,
}
}
func newQueueFailoverAckMgr(shard shard.Context, options *QueueProcessorOptions, processor processor, ackLevel int64, logger log.Logger) *queueAckMgrImpl {
return &queueAckMgrImpl{
isFailover: true,
shard: shard,
options: options,
processor: processor,
outstandingTasks: make(map[int64]bool),
readLevel: ackLevel,
ackLevel: ackLevel,
logger: logger,
metricsClient: shard.GetMetricsClient(),
finishedChan: make(chan struct{}, 1),
}
}
func (a *queueAckMgrImpl) readQueueTasks() ([]task.Info, bool, error) {
a.RLock()
readLevel := a.readLevel
a.RUnlock()
var tasks []task.Info
var morePage bool
op := func() error {
var err error
tasks, morePage, err = a.processor.readTasks(readLevel)
return err
}
err := backoff.Retry(op, persistenceOperationRetryPolicy, common.IsPersistenceTransientError)
if err != nil {
return nil, false, err
}
a.Lock()
defer a.Unlock()
if a.isFailover && !morePage {
a.isReadFinished = true
}
TaskFilterLoop:
for _, task := range tasks {
_, isLoaded := a.outstandingTasks[task.GetTaskID()]
if isLoaded {
// task already loaded
a.logger.Debug(fmt.Sprintf("Skipping transfer task: %v.", task))
continue TaskFilterLoop
}
if a.readLevel >= task.GetTaskID() {
a.logger.Fatal("Next task ID is less than current read level.",
tag.TaskID(task.GetTaskID()),
tag.ReadLevel(a.readLevel))
}
a.logger.Debug(fmt.Sprintf("Moving read level: %v", task.GetTaskID()))
a.readLevel = task.GetTaskID()
a.outstandingTasks[task.GetTaskID()] = false
}
return tasks, morePage, nil
}
// CompleteQueueTask implements the task.QueueAckMgr interface
// TODO: this method should be removed. See the comments in task/task.go L81
func (a *queueAckMgrImpl) CompleteQueueTask(taskID int64) {
a.completeQueueTask(taskID)
}
func (a *queueAckMgrImpl) completeQueueTask(taskID int64) {
a.Lock()
if _, ok := a.outstandingTasks[taskID]; ok {
a.outstandingTasks[taskID] = true
}
a.Unlock()
}
func (a *queueAckMgrImpl) getQueueAckLevel() int64 {
a.Lock()
defer a.Unlock()
return a.ackLevel
}
func (a *queueAckMgrImpl) getQueueReadLevel() int64 {
a.Lock()
defer a.Unlock()
return a.readLevel
}
func (a *queueAckMgrImpl) getFinishedChan() <-chan struct{} {
return a.finishedChan
}
func (a *queueAckMgrImpl) updateQueueAckLevel() error {
a.metricsClient.IncCounter(a.options.MetricScope, metrics.AckLevelUpdateCounter)
a.Lock()
ackLevel := a.ackLevel
a.logger.Debug(fmt.Sprintf("Moving timer ack level from %v, with %v.", ackLevel, a.outstandingTasks))
// task ID is not sequancial, meaning there are a ton of missing chunks,
// so to optimize the performance, a sort is required
var taskIDs []int64
for k := range a.outstandingTasks {
taskIDs = append(taskIDs, k)
}
sort.Slice(taskIDs, func(i, j int) bool { return taskIDs[i] < taskIDs[j] })
pendingTasks := len(taskIDs)
if pendingTasks > warnPendingTasks {
a.logger.Warn("Too many pending tasks.")
}
switch a.options.MetricScope {
case metrics.ReplicatorQueueProcessorScope:
a.metricsClient.RecordTimer(metrics.ShardInfoScope, metrics.ShardInfoReplicationPendingTasksTimer, time.Duration(pendingTasks))
case metrics.TransferActiveQueueProcessorScope:
a.metricsClient.RecordTimer(metrics.ShardInfoScope, metrics.ShardInfoTransferActivePendingTasksTimer, time.Duration(pendingTasks))
case metrics.TransferStandbyQueueProcessorScope:
a.metricsClient.RecordTimer(metrics.ShardInfoScope, metrics.ShardInfoTransferStandbyPendingTasksTimer, time.Duration(pendingTasks))
}
MoveAckLevelLoop:
for _, current := range taskIDs {
acked := a.outstandingTasks[current]
if acked {
ackLevel = current
delete(a.outstandingTasks, current)
a.logger.Debug(fmt.Sprintf("Moving timer ack level to %v.", ackLevel))
} else {
break MoveAckLevelLoop
}
}
a.ackLevel = ackLevel
if a.isFailover && a.isReadFinished && len(a.outstandingTasks) == 0 {
a.Unlock()
// this means in failover mode, all possible failover transfer tasks
// are processed and we are free to shundown
a.logger.Debug("Queue ack manager shutdown.")
a.finishedChan <- struct{}{}
err := a.processor.queueShutdown()
if err != nil {
a.logger.Error("Error shutdown queue", tag.Error(err))
}
return nil
}
a.Unlock()
if err := a.processor.updateAckLevel(ackLevel); err != nil {
a.metricsClient.IncCounter(a.options.MetricScope, metrics.AckLevelUpdateFailedCounter)
a.logger.Error("Error updating ack level for shard", tag.Error(err), tag.OperationFailed)
return err
}
return nil
}