/
sequentialTaskProcessor.go
192 lines (166 loc) · 5.41 KB
/
sequentialTaskProcessor.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
// 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 task
import (
"sync"
"sync/atomic"
"time"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/collection"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
)
type (
sequentialTaskProcessorImpl struct {
status int32
shutdownChan chan struct{}
waitGroup sync.WaitGroup
coroutineSize int
taskqueues collection.ConcurrentTxMap
taskQueueFactory SequentialTaskQueueFactory
taskqueueChan chan SequentialTaskQueue
metricsScope metrics.Scope
logger log.Logger
}
)
// NewSequentialTaskProcessor create a new sequential tasks processor
func NewSequentialTaskProcessor(
coroutineSize int,
taskQueueHashFn collection.HashFunc,
taskQueueFactory SequentialTaskQueueFactory,
metricsClient metrics.Client,
logger log.Logger,
) Processor {
return &sequentialTaskProcessorImpl{
status: common.DaemonStatusInitialized,
shutdownChan: make(chan struct{}),
coroutineSize: coroutineSize,
taskqueues: collection.NewShardedConcurrentTxMap(1024, taskQueueHashFn),
taskQueueFactory: taskQueueFactory,
taskqueueChan: make(chan SequentialTaskQueue, coroutineSize),
metricsScope: metricsClient.Scope(metrics.SequentialTaskProcessingScope),
logger: logger,
}
}
func (t *sequentialTaskProcessorImpl) Start() {
if !atomic.CompareAndSwapInt32(&t.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
t.waitGroup.Add(t.coroutineSize)
for i := 0; i < t.coroutineSize; i++ {
go t.pollAndProcessTaskQueue()
}
t.logger.Info("Task processor started.")
}
func (t *sequentialTaskProcessorImpl) Stop() {
if !atomic.CompareAndSwapInt32(&t.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
return
}
close(t.shutdownChan)
if success := common.AwaitWaitGroup(&t.waitGroup, time.Minute); !success {
t.logger.Warn("Task processor timeout trying to stop.")
}
t.logger.Info("Task processor stopped.")
}
func (t *sequentialTaskProcessorImpl) Submit(task Task) error {
t.metricsScope.IncCounter(metrics.SequentialTaskSubmitRequest)
metricsTimer := t.metricsScope.StartTimer(metrics.SequentialTaskSubmitLatency)
defer metricsTimer.Stop()
taskqueue := t.taskQueueFactory(task)
taskqueue.Add(task)
_, fnEvaluated, err := t.taskqueues.PutOrDo(
taskqueue.QueueID(),
taskqueue,
func(key interface{}, value interface{}) error {
value.(SequentialTaskQueue).Add(task)
return nil
},
)
if err != nil {
return err
}
// if function evaluated, meaning that the task set is
// already dispatched
if fnEvaluated {
t.metricsScope.IncCounter(metrics.SequentialTaskSubmitRequestTaskQueueExist)
return nil
}
// need to dispatch this task set
t.metricsScope.IncCounter(metrics.SequentialTaskSubmitRequestTaskQueueMissing)
select {
case <-t.shutdownChan:
case t.taskqueueChan <- taskqueue:
}
return nil
}
func (t *sequentialTaskProcessorImpl) pollAndProcessTaskQueue() {
defer t.waitGroup.Done()
for {
select {
case <-t.shutdownChan:
return
case taskqueue := <-t.taskqueueChan:
metricsTimer := t.metricsScope.StartTimer(metrics.SequentialTaskQueueProcessingLatency)
t.processTaskQueue(taskqueue)
metricsTimer.Stop()
}
}
}
func (t *sequentialTaskProcessorImpl) processTaskQueue(taskqueue SequentialTaskQueue) {
for {
select {
case <-t.shutdownChan:
return
default:
queueSize := taskqueue.Len()
t.metricsScope.RecordTimer(metrics.SequentialTaskQueueSize, time.Duration(queueSize))
if queueSize > 0 {
t.processTaskOnce(taskqueue)
} else {
deleted := t.taskqueues.RemoveIf(taskqueue.QueueID(), func(key interface{}, value interface{}) bool {
return value.(SequentialTaskQueue).IsEmpty()
})
if deleted {
return
}
// if deletion failed, meaning that task queue is offered with new task
// continue execution
}
}
}
}
func (t *sequentialTaskProcessorImpl) processTaskOnce(taskqueue SequentialTaskQueue) {
metricsTimer := t.metricsScope.StartTimer(metrics.SequentialTaskTaskProcessingLatency)
defer metricsTimer.Stop()
task := taskqueue.Remove()
err := task.Execute()
err = task.HandleErr(err)
if err != nil {
if task.RetryErr(err) {
taskqueue.Add(task)
} else {
t.logger.Error("Unable to process task", tag.Error(err))
task.Nack()
}
} else {
task.Ack()
}
}