-
Notifications
You must be signed in to change notification settings - Fork 308
/
worker.go
211 lines (184 loc) · 6.23 KB
/
worker.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
package processor
import (
"context"
"math"
"sync"
"time"
"github.com/rudderlabs/rudder-server/jobsdb"
"github.com/rudderlabs/rudder-server/rruntime"
"github.com/rudderlabs/rudder-server/services/rsources"
"github.com/rudderlabs/rudder-server/utils/logger"
"github.com/rudderlabs/rudder-server/utils/misc"
)
// newWorker creates a new worker
func newWorker(ctx context.Context, partition string, h workerHandle) *worker {
w := &worker{
handle: h,
logger: h.logger().Child(partition),
partition: partition,
}
w.lifecycle.ctx, w.lifecycle.cancel = context.WithCancel(ctx)
w.channel.ping = make(chan struct{}, 1)
w.channel.preprocess = make(chan subJob, w.handle.config().pipelineBufferedItems)
w.channel.transform = make(chan *transformationMessage, w.handle.config().pipelineBufferedItems)
w.channel.store = make(chan *storeMessage, (w.handle.config().pipelineBufferedItems+1)*(w.handle.config().maxEventsToProcess/w.handle.config().subJobSize+1))
w.start()
return w
}
// worker picks jobs from jobsdb for the appropriate partition and performs all processing steps for them
// 1. preprocess
// 2. transform
// 3. store
type worker struct {
partition string
handle workerHandle
logger logger.Logger
lifecycle struct { // worker lifecycle related fields
ctx context.Context // worker context
cancel context.CancelFunc // worker context cancel function
wg sync.WaitGroup // worker wait group
idleMu sync.RWMutex // idle mutex
idleSince time.Time // idle since
}
channel struct { // worker channels
ping chan struct{} // ping channel triggers the worker to start working
preprocess chan subJob // preprocess channel is used to send jobs to preprocess asynchronously when pipelining is enabled
transform chan *transformationMessage // transform channel is used to send jobs to transform asynchronously when pipelining is enabled
store chan *storeMessage // store channel is used to send jobs to store asynchronously when pipelining is enabled
}
}
// start starts the various worker goroutines
func (w *worker) start() {
// ping loop
w.lifecycle.wg.Add(1)
rruntime.Go(func() {
var exponentialSleep misc.ExponentialNumber[time.Duration]
defer w.lifecycle.wg.Done()
defer close(w.channel.preprocess)
defer close(w.channel.ping)
defer w.logger.Debugf("ping loop stopped for worker: %s", w.partition)
for {
select {
case <-w.lifecycle.ctx.Done():
return
case <-w.channel.ping:
}
w.setIdleSince(time.Time{})
if w.pickJobs() {
exponentialSleep.Reset()
} else {
if err := misc.SleepCtx(w.lifecycle.ctx, exponentialSleep.Next(w.handle.config().readLoopSleep, w.handle.config().maxLoopSleep)); err != nil {
w.logger.Info("ping loop stopped for worker: ", w.partition, " due to: ", err)
return
}
w.setIdleSince(time.Now())
}
}
})
if !w.handle.config().enablePipelining {
return
}
// preprocess jobs
w.lifecycle.wg.Add(1)
rruntime.Go(func() {
defer w.lifecycle.wg.Done()
defer close(w.channel.transform)
defer w.logger.Debugf("preprocessing routine stopped for worker: %s", w.partition)
for jobs := range w.channel.preprocess {
w.channel.transform <- w.handle.processJobsForDest(w.partition, jobs, nil)
}
})
// transform jobs
w.lifecycle.wg.Add(1)
rruntime.Go(func() {
defer w.lifecycle.wg.Done()
defer close(w.channel.store)
defer w.logger.Debugf("transform routine stopped for worker: %s", w.partition)
for msg := range w.channel.transform {
w.channel.store <- w.handle.transformations(w.partition, msg)
}
})
// store jobs
w.lifecycle.wg.Add(1)
rruntime.Go(func() {
var mergedJob *storeMessage
firstSubJob := true
defer w.lifecycle.wg.Done()
defer w.logger.Debugf("store routine stopped for worker: %s", w.partition)
for subJob := range w.channel.store {
if firstSubJob && !subJob.hasMore {
w.handle.Store(w.partition, subJob)
continue
}
if firstSubJob {
mergedJob = &storeMessage{
rsourcesStats: subJob.rsourcesStats,
uniqueMessageIds: make(map[string]struct{}),
procErrorJobsByDestID: make(map[string][]*jobsdb.JobT),
sourceDupStats: make(map[string]int),
start: subJob.start,
}
firstSubJob = false
}
mergedJob.merge(subJob)
if !subJob.hasMore {
w.handle.Store(w.partition, mergedJob)
firstSubJob = true
}
}
})
}
// pickJobs picks the next set of jobs from the jobsdb and returns [true] if jobs were picked, [false] otherwise
func (w *worker) pickJobs() (picked bool) {
if w.handle.config().enablePipelining {
start := time.Now()
jobs := w.handle.getJobs(w.partition)
if len(jobs.Jobs) == 0 {
return
}
picked = true
if err := w.handle.markExecuting(jobs.Jobs); err != nil {
w.logger.Error(err)
panic(err)
}
w.handle.stats().DBReadThroughput.Count(throughputPerSecond(jobs.EventsCount, time.Since(start)))
rsourcesStats := rsources.NewStatsCollector(w.handle.rsourcesService())
rsourcesStats.BeginProcessing(jobs.Jobs)
subJobs := w.handle.jobSplitter(jobs.Jobs, rsourcesStats)
for _, subJob := range subJobs {
w.channel.preprocess <- subJob
}
// sleepRatio is dependent on the number of events read in this loop
sleepRatio := 1.0 - math.Min(1, float64(jobs.EventsCount)/float64(w.handle.config().maxEventsToProcess))
if err := misc.SleepCtx(w.lifecycle.ctx, time.Duration(sleepRatio*float64(w.handle.config().readLoopSleep))); err != nil {
return
}
return
} else {
return w.handle.handlePendingGatewayJobs(w.partition)
}
}
func (w *worker) setIdleSince(t time.Time) {
w.lifecycle.idleMu.Lock()
defer w.lifecycle.idleMu.Unlock()
w.lifecycle.idleSince = t
}
// Ping triggers the worker to pick more jobs
func (w *worker) Ping() {
select {
case w.channel.ping <- struct{}{}:
default:
}
}
// IdleSince returns the time when the worker was last idle. If the worker is not idle, it returns a zero time.
func (w *worker) IdleSince() time.Time {
w.lifecycle.idleMu.RLock()
defer w.lifecycle.idleMu.RUnlock()
return w.lifecycle.idleSince
}
// Stop stops the worker and waits until all its goroutines have stopped
func (w *worker) Stop() {
w.lifecycle.cancel()
w.lifecycle.wg.Wait()
w.logger.Debugf("Stopped worker: %s", w.partition)
}