/
taskReader.go
334 lines (296 loc) · 10.2 KB
/
taskReader.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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 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 matching
import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
enumsspb "go.temporal.io/server/api/enums/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/backoff"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/internal/goro"
)
const (
taskReaderOfferThrottleWait = time.Second
taskReaderThrottleRetryDelay = 3 * time.Second
)
type (
taskReader struct {
status int32
taskBuffer chan *persistencespb.AllocatedTaskInfo // tasks loaded from persistence
notifyC chan struct{} // Used as signal to notify pump of new tasks
tlMgr *taskQueueManagerImpl
taskValidator taskValidator
gorogrp goro.Group
backoffTimerLock sync.Mutex
backoffTimer *time.Timer
retrier backoff.Retrier
}
)
func newTaskReader(tlMgr *taskQueueManagerImpl) *taskReader {
return &taskReader{
status: common.DaemonStatusInitialized,
tlMgr: tlMgr,
taskValidator: newTaskValidator(tlMgr.newIOContext, tlMgr.clusterMeta, tlMgr.namespaceRegistry, tlMgr.engine.historyClient, tlMgr.metricsHandler),
notifyC: make(chan struct{}, 1),
// we always dequeue the head of the buffer and try to dispatch it to a poller
// so allocate one less than desired target buffer size
taskBuffer: make(chan *persistencespb.AllocatedTaskInfo, tlMgr.config.GetTasksBatchSize()-1),
retrier: backoff.NewRetrier(
common.CreateReadTaskRetryPolicy(),
backoff.SystemClock,
),
}
}
// Start reading pump for the given task queue.
// The pump fills up taskBuffer from persistence.
func (tr *taskReader) Start() {
if !atomic.CompareAndSwapInt32(
&tr.status,
common.DaemonStatusInitialized,
common.DaemonStatusStarted,
) {
return
}
tr.gorogrp.Go(tr.dispatchBufferedTasks)
tr.gorogrp.Go(tr.getTasksPump)
}
// Stop pump that fills up taskBuffer from persistence.
func (tr *taskReader) Stop() {
if !atomic.CompareAndSwapInt32(
&tr.status,
common.DaemonStatusStarted,
common.DaemonStatusStopped,
) {
return
}
tr.gorogrp.Cancel()
}
func (tr *taskReader) Signal() {
var event struct{}
select {
case tr.notifyC <- event:
default: // channel already has an event, don't block
}
}
func (tr *taskReader) dispatchBufferedTasks(ctx context.Context) error {
ctx = tr.tlMgr.callerInfoContext(ctx)
dispatchLoop:
for ctx.Err() == nil {
select {
case taskInfo, ok := <-tr.taskBuffer:
if !ok { // Task queue getTasks pump is shutdown
break dispatchLoop
}
task := newInternalTask(taskInfo, tr.tlMgr.completeTask, enumsspb.TASK_SOURCE_DB_BACKLOG, "", false)
for ctx.Err() == nil {
if !tr.taskValidator.maybeValidate(taskInfo, tr.tlMgr.taskQueueID.taskType) {
task.finish(nil)
tr.taggedMetricsHandler().Counter(metrics.ExpiredTasksPerTaskQueueCounter.GetMetricName()).Record(1)
// Don't try to set read level here because it may have been advanced already.
continue dispatchLoop
}
taskCtx, cancel := context.WithTimeout(ctx, taskReaderOfferTimeout)
err := tr.tlMgr.engine.DispatchSpooledTask(taskCtx, task, tr.tlMgr.taskQueueID, tr.tlMgr.stickyInfo)
cancel()
if err == nil {
continue dispatchLoop
}
// if task is still valid (truly valid or unable to verify if task is valid)
tr.taggedMetricsHandler().Counter(metrics.BufferThrottlePerTaskQueueCounter.GetMetricName()).Record(1)
if !errors.Is(err, errUserDataDisabled) && !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) {
// Don't log here if encounters missing user data error when dispatch a versioned task.
tr.throttledLogger().Error("taskReader: unexpected error dispatching task", tag.Error(err))
}
common.InterruptibleSleep(ctx, taskReaderOfferThrottleWait)
}
return ctx.Err()
case <-ctx.Done():
return ctx.Err()
}
}
return ctx.Err()
}
func (tr *taskReader) getTasksPump(ctx context.Context) error {
ctx = tr.tlMgr.callerInfoContext(ctx)
if err := tr.tlMgr.WaitUntilInitialized(ctx); err != nil {
return err
}
updateAckTimer := time.NewTimer(tr.tlMgr.config.UpdateAckInterval())
defer updateAckTimer.Stop()
tr.Signal() // prime pump
Loop:
for {
// Prioritize exiting over other processing
select {
case <-ctx.Done():
return nil
default:
}
select {
case <-ctx.Done():
return nil
case <-tr.notifyC:
tasks, readLevel, isReadBatchDone, err := tr.getTaskBatch(ctx)
tr.tlMgr.signalIfFatal(err)
if err != nil {
// TODO: Should we ever stop retrying on db errors?
if common.IsResourceExhausted(err) {
tr.backoff(taskReaderThrottleRetryDelay)
} else {
tr.backoff(tr.retrier.NextBackOff())
}
continue Loop
}
tr.retrier.Reset()
if len(tasks) == 0 {
tr.tlMgr.taskAckManager.setReadLevelAfterGap(readLevel)
if !isReadBatchDone {
tr.Signal()
}
continue Loop
}
// only error here is due to context cancelation which we also
// handle above
_ = tr.addTasksToBuffer(ctx, tasks)
// There maybe more tasks. We yield now, but signal pump to check again later.
tr.Signal()
case <-updateAckTimer.C:
err := tr.persistAckLevel(ctx)
isConditionFailed := tr.tlMgr.signalIfFatal(err)
if err != nil && !isConditionFailed {
tr.logger().Error("Persistent store operation failure",
tag.StoreOperationUpdateTaskQueue,
tag.Error(err))
// keep going as saving ack is not critical
}
tr.Signal() // periodically signal pump to check persistence for tasks
updateAckTimer = time.NewTimer(tr.tlMgr.config.UpdateAckInterval())
}
}
}
func (tr *taskReader) getTaskBatchWithRange(
ctx context.Context,
readLevel int64,
maxReadLevel int64,
) ([]*persistencespb.AllocatedTaskInfo, error) {
response, err := tr.tlMgr.db.GetTasks(ctx, readLevel+1, maxReadLevel+1, tr.tlMgr.config.GetTasksBatchSize())
if err != nil {
return nil, err
}
return response.Tasks, err
}
// Returns a batch of tasks from persistence starting form current read level.
// Also return a number that can be used to update readLevel
// Also return a bool to indicate whether read is finished
func (tr *taskReader) getTaskBatch(ctx context.Context) ([]*persistencespb.AllocatedTaskInfo, int64, bool, error) {
var tasks []*persistencespb.AllocatedTaskInfo
readLevel := tr.tlMgr.taskAckManager.getReadLevel()
maxReadLevel := tr.tlMgr.taskWriter.GetMaxReadLevel()
// counter i is used to break and let caller check whether taskqueue is still alive and need resume read.
for i := 0; i < 10 && readLevel < maxReadLevel; i++ {
upper := readLevel + tr.tlMgr.config.RangeSize
if upper > maxReadLevel {
upper = maxReadLevel
}
tasks, err := tr.getTaskBatchWithRange(ctx, readLevel, upper)
if err != nil {
return nil, readLevel, true, err
}
// return as long as it grabs any tasks
if len(tasks) > 0 {
return tasks, upper, true, nil
}
readLevel = upper
}
return tasks, readLevel, readLevel == maxReadLevel, nil // caller will update readLevel when no task grabbed
}
func (tr *taskReader) addTasksToBuffer(
ctx context.Context,
tasks []*persistencespb.AllocatedTaskInfo,
) error {
for _, t := range tasks {
if IsTaskExpired(t) {
tr.taggedMetricsHandler().Counter(metrics.ExpiredTasksPerTaskQueueCounter.GetMetricName()).Record(1)
// Also increment readLevel for expired tasks otherwise it could result in
// looping over the same tasks if all tasks read in the batch are expired
tr.tlMgr.taskAckManager.setReadLevel(t.GetTaskId())
continue
}
if err := tr.addSingleTaskToBuffer(ctx, t); err != nil {
return err
}
}
return nil
}
func (tr *taskReader) addSingleTaskToBuffer(
ctx context.Context,
task *persistencespb.AllocatedTaskInfo,
) error {
tr.tlMgr.taskAckManager.addTask(task.GetTaskId())
select {
case tr.taskBuffer <- task:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (tr *taskReader) persistAckLevel(ctx context.Context) error {
ackLevel := tr.tlMgr.taskAckManager.getAckLevel()
tr.emitTaskLagMetric(ackLevel)
return tr.tlMgr.db.UpdateState(ctx, ackLevel)
}
func (tr *taskReader) logger() log.Logger {
return tr.tlMgr.logger
}
func (tr *taskReader) throttledLogger() log.ThrottledLogger {
return tr.tlMgr.throttledLogger
}
func (tr *taskReader) taggedMetricsHandler() metrics.Handler {
return tr.tlMgr.metricsHandler
}
func (tr *taskReader) emitTaskLagMetric(ackLevel int64) {
// note: this metric is only an estimation for the lag.
// taskID in DB may not be continuous, especially when task list ownership changes.
maxReadLevel := tr.tlMgr.taskWriter.GetMaxReadLevel()
tr.taggedMetricsHandler().Gauge(metrics.TaskLagPerTaskQueueGauge.GetMetricName()).Record(float64(maxReadLevel - ackLevel))
}
func (tr *taskReader) backoff(duration time.Duration) {
tr.backoffTimerLock.Lock()
defer tr.backoffTimerLock.Unlock()
if tr.backoffTimer == nil {
tr.backoffTimer = time.AfterFunc(duration, func() {
tr.backoffTimerLock.Lock()
defer tr.backoffTimerLock.Unlock()
tr.Signal() // re-enqueue the event
tr.backoffTimer = nil
})
}
}