-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.go
342 lines (296 loc) · 8.54 KB
/
scheduler.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
339
340
341
342
package gojob
import (
"encoding/json"
"fmt"
"log/slog"
"sync"
"sync/atomic"
"time"
"github.com/WangYihang/gojob/pkg/utils"
"github.com/WangYihang/uio"
"github.com/google/uuid"
)
type schedulerMetadata map[string]interface{}
// Scheduler is a task scheduler
type Scheduler struct {
id string
numWorkers int
metadata schedulerMetadata
maxRetries int
maxRuntimePerTaskSeconds int
numShards int64
shard int64
isStarted atomic.Bool
currentIndex atomic.Int64
statusManager *statusManager
taskChan chan *basicTask
resultChans []chan *basicTask
resultFilePath string
statusFilePath string
metadataFilePath string
prometheusPushGatewayUrl string
prometheusPushGatewayJob string
taskWg *sync.WaitGroup
recorderWg *sync.WaitGroup
}
type schedulerOption func(*Scheduler) error
func New(options ...schedulerOption) *Scheduler {
id := uuid.New().String()
svr := &Scheduler{
id: id,
numWorkers: 1,
metadata: schedulerMetadata{"id": id},
maxRetries: 4,
maxRuntimePerTaskSeconds: 16,
numShards: 1,
shard: 0,
isStarted: atomic.Bool{},
currentIndex: atomic.Int64{},
statusManager: newStatusManager(),
taskChan: make(chan *basicTask),
resultChans: []chan *basicTask{},
resultFilePath: "result.json",
statusFilePath: "status.json",
metadataFilePath: "metadata.json",
prometheusPushGatewayUrl: "",
prometheusPushGatewayJob: "gojob",
taskWg: &sync.WaitGroup{},
recorderWg: &sync.WaitGroup{},
}
for _, opt := range options {
err := opt(svr)
if err != nil {
panic(err)
}
}
go svr.statusManager.Start()
svr.recorderWg.Add(4)
chanRecorder(svr.resultFilePath, svr.ResultChan(), svr.recorderWg)
chanRecorder(svr.statusFilePath, svr.StatusChan(), svr.recorderWg)
chanRecorder("-", svr.StatusChan(), svr.recorderWg)
metadataChan := make(chan schedulerMetadata)
chanRecorder(svr.metadataFilePath, metadataChan, svr.recorderWg)
metadataChan <- svr.metadata
close(metadataChan)
if svr.prometheusPushGatewayUrl != "" {
svr.recorderWg.Add(1)
prometheusPusher(svr.prometheusPushGatewayUrl, svr.prometheusPushGatewayJob, svr.StatusChan(), svr.recorderWg)
}
return svr
}
// SetNumShards sets the number of shards, default is 1 which means no sharding
func WithNumShards(numShards int64) schedulerOption {
return func(s *Scheduler) error {
if numShards <= 0 {
return fmt.Errorf("numShards must be greater than 0")
}
s.numShards = numShards
return nil
}
}
// SetShard sets the shard (from 0 to NumShards-1)
func WithShard(shard int64) schedulerOption {
return func(s *Scheduler) error {
if shard < 0 || shard >= s.numShards {
return fmt.Errorf("shard must be in [0, NumShards)")
}
s.shard = shard
return nil
}
}
// SetNumWorkers sets the number of workers
func WithNumWorkers(numWorkers int) schedulerOption {
return func(s *Scheduler) error {
if numWorkers <= 0 {
return fmt.Errorf("numWorkers must be greater than 0")
}
s.numWorkers = numWorkers
return nil
}
}
// SetMaxRetries sets the max retries
func WithMaxRetries(maxRetries int) schedulerOption {
return func(s *Scheduler) error {
if maxRetries <= 0 {
return fmt.Errorf("maxRetries must be greater than 0")
}
s.maxRetries = maxRetries
return nil
}
}
// SetMaxRuntimePerTaskSeconds sets the max runtime per task seconds
func WithMaxRuntimePerTaskSeconds(maxRuntimePerTaskSeconds int) schedulerOption {
return func(s *Scheduler) error {
if maxRuntimePerTaskSeconds <= 0 {
return fmt.Errorf("maxRuntimePerTaskSeconds must be greater than 0")
}
s.maxRuntimePerTaskSeconds = maxRuntimePerTaskSeconds
return nil
}
}
// WithTotalTasks sets the total number of tasks, and calculates the number of tasks for this shard
func WithTotalTasks(numTotalTasks int64) schedulerOption {
return func(s *Scheduler) error {
// Check if NumShards is set and is greater than 0
if s.numShards <= 0 {
return fmt.Errorf("number of shards must be greater than 0")
}
// Check if Shard is set and is within the valid range [0, NumShards)
if s.shard < 0 || s.shard >= s.numShards {
return fmt.Errorf("shard must be within the range [0, NumShards)")
}
// Calculate the base number of tasks per shard
baseTasksPerShard := numTotalTasks / int64(s.numShards)
// Calculate the remainder
remainder := numTotalTasks % int64(s.numShards)
// Adjust task count for shards that need to handle an extra task due to the remainder
if int64(s.shard) < remainder {
baseTasksPerShard++
}
// Store the number of tasks for this shard
s.statusManager.SetTotal(baseTasksPerShard)
return nil
}
}
// AddMetadata adds metadata
func WithMetadata(key string, value interface{}) schedulerOption {
return func(s *Scheduler) error {
s.metadata[key] = value
return nil
}
}
// WithResultFilePath sets the file path for results
func WithResultFilePath(path string) schedulerOption {
return func(s *Scheduler) error {
s.resultFilePath = path
return nil
}
}
// WithStatusFilePath sets the file path for status
func WithStatusFilePath(path string) schedulerOption {
return func(s *Scheduler) error {
s.statusFilePath = path
return nil
}
}
func WithPrometheusPushGateway(url string, job string) schedulerOption {
return func(s *Scheduler) error {
s.prometheusPushGatewayUrl = url
s.prometheusPushGatewayJob = job
return nil
}
}
// WithMetadataFilePath sets the file path for metadata
func WithMetadataFilePath(path string) schedulerOption {
return func(s *Scheduler) error {
s.metadataFilePath = path
return nil
}
}
// chanRecorder records the channel to a given file path
func chanRecorder[T *basicTask | Status | schedulerMetadata](path string, ch <-chan T, wg *sync.WaitGroup) {
fd, err := uio.Open(path)
if err != nil {
slog.Error("error occurred while opening file", slog.String("path", path), slog.String("error", err.Error()))
return
}
go func() {
encoder := json.NewEncoder(fd)
for item := range ch {
if err := encoder.Encode(item); err != nil {
slog.Error("error occurred while serializing data", slog.String("path", path), slog.String("error", err.Error()))
}
}
fd.Close()
wg.Done()
}()
}
// ResultChan returns a newly created channel to receive results
// Everytime ResultChan is called, a new channel is created, and the results are written to all channels
// This is useful for multiple consumers (e.g. writing to multiple files)
func (s *Scheduler) ResultChan() <-chan *basicTask {
c := make(chan *basicTask)
s.resultChans = append(s.resultChans, c)
return c
}
// StatusChan returns a newly created channel to receive status
// Everytime StatusChan is called, a new channel is created, and the status are written to all channels
// This is useful for multiple consumers (e.g. writing to multiple files, report to prometheus, etc.)
func (s *Scheduler) StatusChan() <-chan Status {
return s.statusManager.StatusChan()
}
func (s *Scheduler) Metadata() map[string]interface{} {
return s.metadata
}
// Submit submits a task to the scheduler
func (s *Scheduler) Submit(task Task) {
if !s.isStarted.Load() {
s.Start()
}
index := s.currentIndex.Load()
if (index % s.numShards) == s.shard {
s.taskWg.Add(1)
s.taskChan <- newBasicTask(index, task)
}
s.currentIndex.Add(1)
}
// Start starts the scheduler
func (s *Scheduler) Start() *Scheduler {
for i := 0; i < s.numWorkers; i++ {
go s.Worker()
}
s.isStarted.Store(true)
return s
}
// Wait waits for all tasks to finish
func (s *Scheduler) Wait() {
// Wait for all tasks to finish
s.taskWg.Wait()
// Close task channel
close(s.taskChan)
// Close result channels
for _, resultChan := range s.resultChans {
close(resultChan)
}
// Wait for all recorders to finish
s.statusManager.Stop()
// Wait for all recorders to finish
s.recorderWg.Wait()
}
func (s *Scheduler) Status() Status {
return s.statusManager.Snapshot()
}
// Worker is a worker
func (s *Scheduler) Worker() {
for task := range s.taskChan {
// Start task
for i := 0; i < s.maxRetries; i++ {
err := func() error {
task.StartedAt = time.Now().UnixMicro()
defer func() {
task.NumTries++
task.FinishedAt = time.Now().UnixMicro()
}()
return utils.RunWithTimeout(task.Task.Do, time.Duration(s.maxRuntimePerTaskSeconds)*time.Second)
}()
if err != nil {
task.Error = err.Error()
} else {
task.Error = ""
break
}
}
// Write log
for _, resultChan := range s.resultChans {
resultChan <- task
}
// Update status
if task.Error != "" {
s.statusManager.IncFailed()
} else {
s.statusManager.IncSucceed()
}
// Notify task is done
s.taskWg.Done()
}
}