-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultraqueue.go
478 lines (404 loc) · 12.8 KB
/
ultraqueue.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package main
import (
"errors"
"fmt"
"sync"
"time"
"github.com/danthegoodman1/UltraQueue/taskdb"
"github.com/google/btree"
"github.com/rs/zerolog/log"
)
var (
// Will only process up to 10_000 items per tick to prevent massive stalls
DelayInFlightIteratorMaxItems = 10_000
ErrNotFoundInFlight = errors.New("task not found in flight")
)
type UltraQueue struct {
Partition string
TaskDB taskdb.TaskDB
topics map[string]*Topic
topicMu *sync.RWMutex
inFlightTree *btree.BTree
inFlightTreeMu *sync.Mutex
delayTree *btree.BTree
delayTreeMu *sync.Mutex
inFlightTicker *time.Ticker
closeChan chan chan struct{}
// TODO: Check if draining for enqueue and dequeue
IsDraining bool
}
func NewUltraQueue(partition string, bufferLen int64) (*UltraQueue, error) {
// Initialize taskdb based on config
// FIXME: Temporary in task db initialization
// taskDB, err := taskdb.NewMemoryTaskDB()
taskDB, err := taskdb.NewKVTaskDB(partition)
if err != nil {
return nil, fmt.Errorf("error creating new task db: %w", err)
}
uq := &UltraQueue{
Partition: partition,
inFlightTree: btree.New(32),
inFlightTreeMu: &sync.Mutex{},
delayTree: btree.New(32),
delayTreeMu: &sync.Mutex{},
inFlightTicker: time.NewTicker(time.Millisecond * 5),
closeChan: make(chan chan struct{}),
topics: make(map[string]*Topic),
topicMu: &sync.RWMutex{},
TaskDB: taskDB,
IsDraining: false,
}
// Attach TaskDB
s := time.Now()
attachIter := taskDB.Attach()
for {
tasks, err := attachIter.Next()
// fmt.Println("taskls: ", len(tasks))
if len(tasks) == 0 && err == nil {
log.Debug().Str("partition", uq.Partition).Msg("Finished attach in " + time.Since(s).String())
break
} else if err != nil {
log.Fatal().Err(err).Msg("error attaching to taskdb")
}
// Process tasks
// TODO: Wait for write commits
results := make([]taskdb.WriteResult, 0)
for _, task := range tasks {
// Enqueue all tasks for now, maybe add per-state handling later
result := uq.enqueueTask(&Task{
ID: task.ID,
Topic: task.Topic,
Payload: "",
CreatedAt: task.CreatedAt,
Version: task.Version,
DeliveryAttempts: task.DeliveryAttempts,
Priority: task.Priority,
})
results = append(results, result)
}
// Wait for all of the write results
for _, result := range results {
err := result.Get()
if err != nil {
// We need to fail because now we have it enqueued locally but not in the remote db
return nil, fmt.Errorf("error waiting for write result: %w", err)
}
}
}
// Start background inflight and delay tree scanner
go uq.pollDelayAndInFlightTrees(time.NewTicker(time.Millisecond * 200))
return uq, nil
}
func (uq *UltraQueue) Shutdown() {
log.Info().Str("partition", uq.Partition).Msg("Shutting down ultra queue...")
returnChan := make(chan struct{}, 1)
uq.closeChan <- returnChan
<-returnChan
log.Info().Str("partition", uq.Partition).Msg("Shut down ultra queue")
}
func (uq *UltraQueue) Enqueue(topics []string, payload string, priority int32, delaySeconds int32) error {
for _, topicName := range topics {
task := NewTask(topicName, uq.Partition, payload, priority)
// Insert task payload
payloadResult := uq.TaskDB.PutPayload(task.Topic, task.ID, payload)
// Strip the payload so we don't store it in the topic
task.Payload = ""
var stateResult taskdb.WriteResult
if delaySeconds > 0 {
stateResult = uq.enqueueDelayedTask(task, delaySeconds)
} else {
stateResult = uq.enqueueTask(task)
}
// TODO: Wait for commits
payloadErr, stateErr := payloadResult.Get(), stateResult.Get()
if payloadErr != nil {
return fmt.Errorf("error putting payload: %w", payloadErr)
}
if stateErr != nil {
return fmt.Errorf("error enqueueing: %w", stateErr)
}
}
// TODO: Increment enqueue metric
return nil
}
func (uq *UltraQueue) Dequeue(topicName string, numTasks, inFlightTTLSeconds int32) (tasks []*InTreeTask, err error) {
// Get numTasks from the topic
dequeuedTasks, err := uq.dequeueTask(topicName, numTasks, inFlightTTLSeconds)
if err != nil {
log.Error().Err(err).Msg("Error dequeuing task")
return nil, err
}
// Get task payloads
for _, task := range dequeuedTasks {
payload, err := uq.TaskDB.GetPayload(topicName, task.Task.ID)
if err != nil {
// TODO: Handle this properly
log.Error().Err(err).Str("partition", uq.Partition).Str("topicName", topicName).Str("taskID", task.Task.ID).Msg("failed to get task payload")
return nil, fmt.Errorf("failed to get task payload: %w", err)
}
// Create a new task going out so we can assign the payload without storing it
tasks = append(tasks, &InTreeTask{
TreeID: task.TreeID,
Task: &Task{
ID: task.Task.ID,
Topic: task.Task.Topic,
Payload: payload,
CreatedAt: task.Task.CreatedAt,
Version: task.Task.Version,
DeliveryAttempts: task.Task.DeliveryAttempts,
Priority: task.Task.Priority,
},
})
}
// TODO: Increment dequeue metric
return
}
func (uq *UltraQueue) Ack(inFlightTaskID string) (err error) {
// Check if in the in-flight tree
topicName, taskID, deleted := uq.ack(inFlightTaskID)
if !deleted {
return ErrNotFoundInFlight
}
// Delete from TaskDB
uq.TaskDB.Delete(topicName, taskID)
// TODO: optionally wait for commit?
// TODO: Increment ack metric
return nil
}
func (uq *UltraQueue) Nack(inFlightTaskID string, delaySeconds int32) (err error) {
// TODO: insert new task state
nacked := uq.nack(inFlightTaskID, delaySeconds)
if !nacked {
return ErrNotFoundInFlight
}
// TODO: Increment nack metric
return
}
func (uq *UltraQueue) enqueueDelayedTask(task *Task, delaySeconds int32) taskdb.WriteResult {
treeID := task.genTimeTreeID(task.CreatedAt.Add(time.Second * time.Duration(delaySeconds)))
treeTask := NewInTreeTask(treeID, task)
// Add task state to DB
task.Version++
wr := uq.TaskDB.PutState(&taskdb.TaskDBTaskState{
Topic: task.Topic,
Partition: uq.Partition,
ID: task.ID,
State: taskdb.TASK_STATE_DELAYED,
Version: task.Version,
DeliveryAttempts: task.DeliveryAttempts,
CreatedAt: task.CreatedAt,
Priority: task.Priority,
})
// TODO: Wait for commit, Optionally MUST
uq.delayTreeMu.Lock()
defer uq.delayTreeMu.Unlock()
uq.delayTree.ReplaceOrInsert(treeTask)
return wr
}
func (uq *UltraQueue) enqueueTask(task *Task) taskdb.WriteResult {
log.Debug().Str("partition", uq.Partition).Str("topic", task.Topic).Msg("Enqueuing topic")
// Add task state to DB
task.Version++
wr := uq.TaskDB.PutState(&taskdb.TaskDBTaskState{
Topic: task.Topic,
Partition: uq.Partition,
ID: task.ID,
State: taskdb.TASK_STATE_ENQUEUED,
Version: task.Version,
DeliveryAttempts: task.DeliveryAttempts,
CreatedAt: task.CreatedAt,
Priority: task.Priority,
})
// TODO: Wait for ack, Optionally MUST
// Add to topic outbox tree
topic := uq.getSafeTopic(task.Topic)
if topic == nil {
topic = uq.putSafeTopic(task.Topic)
}
treeID := task.genPriorityTreeID()
topic.Enqueue(&InTreeTask{
TreeID: treeID,
Task: task,
})
return wr
}
func (uq *UltraQueue) dequeueTask(topicName string, numTasks, inFlightTTLSeconds int32) (tasks []*InTreeTask, err error) {
log.Debug().Str("partition", uq.Partition).Str("topic", topicName).Msg("Dequeueing topic")
dequeueTime := time.Now()
topic := uq.getSafeTopic(topicName)
if topic == nil {
return nil, nil
}
uq.inFlightTreeMu.Lock()
defer uq.inFlightTreeMu.Unlock()
// Get tasks and add to inflight tree
inTreeTasks := topic.Dequeue(numTasks)
for _, itt := range inTreeTasks {
itt.Task.DeliveryAttempts++
itt.TreeID = itt.Task.genExternalID(uq.Partition, dequeueTime.Add(time.Second*time.Duration(inFlightTTLSeconds)))
tasks = append(tasks, itt)
// Add task state to DB
itt.Task.Version++
uq.TaskDB.PutState(&taskdb.TaskDBTaskState{
Topic: itt.Task.Topic,
Partition: uq.Partition,
ID: itt.Task.ID,
State: taskdb.TASK_STATE_INFLIGHT,
Version: itt.Task.Version,
DeliveryAttempts: itt.Task.DeliveryAttempts,
CreatedAt: itt.Task.CreatedAt,
Priority: itt.Task.Priority,
})
// TODO: Wait for commit? Probably not needed
uq.inFlightTree.ReplaceOrInsert(itt)
}
return
}
// Safely gets a topic respecting read lock
func (uq *UltraQueue) getSafeTopic(topicName string) *Topic {
uq.topicMu.RLock()
defer uq.topicMu.RUnlock()
if topic, exists := uq.topics[topicName]; exists {
return topic
}
return nil
}
// Creates or overwrites a topic
func (uq *UltraQueue) putSafeTopic(topicName string) *Topic {
uq.topicMu.Lock()
defer uq.topicMu.Unlock()
topic := NewTopic(topicName)
uq.topics[topicName] = topic
return topic
}
func (uq *UltraQueue) getTopicLengths() map[string]int {
uq.topicMu.RLock()
defer uq.topicMu.RUnlock()
topicLengths := make(map[string]int)
// Iterate over all of the topics and get their current lengths, non-sync read is ok
for _, topic := range uq.topics {
topicLengths[topic.Name] = topic.tree.Len()
}
return topicLengths
}
// Launched as goroutine, moves tasks from delay and inflight trees when they expire
func (uq *UltraQueue) pollDelayAndInFlightTrees(t *time.Ticker) {
for {
select {
case tickTime := <-t.C:
// Poll each
uq.expireDelayedTasks(tickTime)
uq.expireInFlightTasks(tickTime)
case returnChan := <-uq.closeChan:
log.Info().Str("partition", uq.Partition).Msg("Delay and InFlight poll got stop channel, exiting")
returnChan <- struct{}{}
return
}
}
}
// Moves tasks from the delayed queue to the topic queue
func (uq *UltraQueue) expireDelayedTasks(t time.Time) {
uq.delayTreeMu.Lock()
defer uq.delayTreeMu.Unlock()
tasks := make([]*InTreeTask, 0)
// UnixMS prefix
treeID := fmt.Sprintf("%d", t.UnixMilli())
count := 0
uq.delayTree.AscendLessThan(&InTreeTask{
TreeID: treeID,
}, func(i btree.Item) bool {
itt, _ := i.(*InTreeTask)
tasks = append(tasks, itt)
// Stall protection
count++
return count < DelayInFlightIteratorMaxItems
})
// Delete from delayed and insert into topic queues
for _, itt := range tasks {
uq.delayTree.Delete(itt)
uq.enqueueTask(itt.Task)
}
// TODO: Increment delay expire metric?
}
func (uq *UltraQueue) expireInFlightTasks(t time.Time) {
uq.inFlightTreeMu.Lock()
defer uq.inFlightTreeMu.Unlock()
tasks := make([]*InTreeTask, 0)
// UnixMS prefix
treeID := fmt.Sprintf("%d", t.UnixMilli())
count := 0
uq.inFlightTree.AscendLessThan(&InTreeTask{
TreeID: treeID,
}, func(i btree.Item) bool {
itt, _ := i.(*InTreeTask)
tasks = append(tasks, itt)
// Stall protection
count++
return count < DelayInFlightIteratorMaxItems
})
// Delete from delayed and insert into topic queues
for _, itt := range tasks {
uq.inFlightTree.Delete(itt)
uq.enqueueTask(itt.Task)
}
// TODO: Increment inflight ttl metric
}
// Removes from the in-flight tree, returns whether the task existed
func (uq *UltraQueue) ack(inTreeTaskID string) (topic, taskID string, deleted bool) {
uq.inFlightTreeMu.Lock()
defer uq.inFlightTreeMu.Unlock()
// Use a fake item to delete from the tree
deletedTask := uq.inFlightTree.Delete(&InTreeTask{
TreeID: inTreeTaskID,
})
if deletedTask != nil {
// Delete from inflight tree
deletedTask, ok := deletedTask.(*InTreeTask)
if !ok {
log.Error().Str("inTreeTaskID", inTreeTaskID).Msg("failed to cast deleted task")
return "", "", false
}
return deletedTask.Task.Topic, deletedTask.Task.ID, true
} else {
return "", "", false
}
}
func (uq *UltraQueue) nack(inTreeTaskID string, delaySeconds int32) bool {
uq.inFlightTreeMu.Lock()
defer uq.inFlightTreeMu.Unlock()
// Use a fake item to delete from the tree
deletedTask := uq.inFlightTree.Delete(&InTreeTask{
TreeID: inTreeTaskID,
})
if deletedTask != nil {
deletedTask, ok := deletedTask.(*InTreeTask)
if !ok {
log.Error().Str("inTreeTaskID", inTreeTaskID).Msg("failed to cast deleted task")
return false
}
if delaySeconds > 0 {
// // Update time id
// foundTask.TreeID = foundTask.Task.genTimeTreeID(time.Now().Add(time.Second * time.Duration(delaySeconds)))
// // Put in delay tree
// uq.delayTreeMu.Lock()
// defer uq.delayTreeMu.Unlock()
// uq.delayTree.ReplaceOrInsert(foundTask)
uq.enqueueDelayedTask(deletedTask.Task, delaySeconds)
} else {
// // Put in topic
// topic := uq.getSafeTopic(foundTask.Task.Topic)
// if topic == nil {
// // Create if it doesn't exist
// topic = uq.putSafeTopic(foundTask.Task.Topic)
// }
// topic.Enqueue(NewInTreeTask(foundTask.Task.genPriorityTreeID(), foundTask.Task))
uq.enqueueTask(deletedTask.Task)
}
return true
} else {
return false
}
}
func (uq *UltraQueue) GetDrainIterator() taskdb.DrainIterator {
return uq.TaskDB.Drain()
}