forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
662 lines (543 loc) · 14.5 KB
/
queue.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package hh
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
var (
ErrNotOpen = fmt.Errorf("queue not open")
ErrQueueFull = fmt.Errorf("queue is full")
ErrSegmentFull = fmt.Errorf("segment is full")
)
const (
defaultSegmentSize = 10 * 1024 * 1024
footerSize = 8
)
// queue is a bounded, disk-backed, append-only type that combines queue and
// log semantics. byte slices can be appended and read back in-order.
// The queue maintains a pointer to the current head
// byte slice and can re-read from the head until it has been advanced.
//
// Internally, the queue writes byte slices to multiple segment files so
// that disk space can be reclaimed. When a segment file is larger than
// the max segment size, a new file is created. Segments are removed
// after their head pointer has advanced past the last entry. The first
// segment is the head, and the last segment is the tail. Reads are from
// the head segment and writes tail segment.
//
// queues can have a max size configured such that when the size of all
// segments on disk exceeds the size, write will fail.
//
// ┌─────┐
// │Head │
// ├─────┘
// │
// ▼
// ┌─────────────────┐ ┌─────────────────┐┌─────────────────┐
// │Segment 1 - 10MB │ │Segment 2 - 10MB ││Segment 3 - 10MB │
// └─────────────────┘ └─────────────────┘└─────────────────┘
// ▲
// │
// │
// ┌─────┐
// │Tail │
// └─────┘
type queue struct {
mu sync.RWMutex
// Directory to create segments
dir string
// The head and tail segments. Reads are from the beginning of head,
// writes are appended to the tail.
head, tail *segment
// The maximum size in bytes of a segment file before a new one should be created
maxSegmentSize int64
// The maximum size allowed in bytes of all segments before writes will return
// an error
maxSize int64
// The segments that exist on disk
segments segments
}
type segments []*segment
// newQueue create a queue that will store segments in dir and that will
// consume more than maxSize on disk.
func newQueue(dir string, maxSize int64) (*queue, error) {
return &queue{
dir: dir,
maxSegmentSize: defaultSegmentSize,
maxSize: maxSize,
segments: segments{},
}, nil
}
// Open opens the queue for reading and writing
func (l *queue) Open() error {
l.mu.Lock()
defer l.mu.Unlock()
segments, err := l.loadSegments()
if err != nil {
return err
}
l.segments = segments
if len(l.segments) == 0 {
_, err := l.addSegment()
if err != nil {
return err
}
}
l.head = l.segments[0]
l.tail = l.segments[len(l.segments)-1]
// If the head has been fully advanced and the segment size is modified,
// existing segments an get stuck and never allow clients to advance further.
// This advances the segment if the current head is already at the end.
_, err = l.head.current()
if err == io.EOF {
return l.trimHead()
}
return nil
}
// Close stops the queue for reading and writing
func (l *queue) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
for _, s := range l.segments {
if err := s.close(); err != nil {
return err
}
}
l.head = nil
l.tail = nil
l.segments = nil
return nil
}
// SetMaxSegmentSize updates the max segment size for new and existing
// segments.
func (l *queue) SetMaxSegmentSize(size int64) error {
l.mu.Lock()
defer l.mu.Unlock()
l.maxSegmentSize = size
for _, s := range l.segments {
s.SetMaxSegmentSize(size)
}
if l.tail.diskUsage() >= l.maxSegmentSize {
segment, err := l.addSegment()
if err != nil {
return err
}
l.tail = segment
}
return nil
}
func (l *queue) PurgeOlderThan(when time.Time) error {
l.mu.Lock()
defer l.mu.Unlock()
// Add a new empty segment so old ones can be reclaimed
if _, err := l.addSegment(); err != nil {
return err
}
cutoff := when.Truncate(time.Second)
for {
mod, err := l.head.lastModified()
if err != nil {
return err
}
if mod.After(cutoff) || mod.Equal(cutoff) {
return nil
}
if err := l.trimHead(); err != nil {
return err
}
}
}
// diskUsage returns the total size on disk used by the queue
func (l *queue) diskUsage() int64 {
var size int64
for _, s := range l.segments {
size += s.diskUsage()
}
return size
}
// addSegment creates a new empty segment file
func (l *queue) addSegment() (*segment, error) {
nextID, err := l.nextSegmentID()
if err != nil {
return nil, err
}
segment, err := newSegment(filepath.Join(l.dir, strconv.FormatUint(nextID, 10)), l.maxSegmentSize)
if err != nil {
return nil, err
}
l.segments = append(l.segments, segment)
return segment, nil
}
// loadSegments loads all segments on disk
func (l *queue) loadSegments() (segments, error) {
segments := []*segment{}
files, err := ioutil.ReadDir(l.dir)
if err != nil {
return segments, err
}
for _, segment := range files {
// Segments should be files. Skip anything that is not a dir.
if segment.IsDir() {
continue
}
// Segments file names are all numeric
_, err := strconv.ParseUint(segment.Name(), 10, 64)
if err != nil {
continue
}
segment, err := newSegment(filepath.Join(l.dir, segment.Name()), l.maxSegmentSize)
if err != nil {
return segments, err
}
segments = append(segments, segment)
}
return segments, nil
}
// nextSegmentID returns the next segment ID that is free
func (l *queue) nextSegmentID() (uint64, error) {
segments, err := ioutil.ReadDir(l.dir)
if err != nil {
return 0, err
}
var maxID uint64
for _, segment := range segments {
// Segments should be files. Skip anything that is not a dir.
if segment.IsDir() {
continue
}
// Segments file names are all numeric
segmentID, err := strconv.ParseUint(segment.Name(), 10, 64)
if err != nil {
continue
}
if segmentID > maxID {
maxID = segmentID
}
}
return maxID + 1, nil
}
// Append appends a byte slice to the end of the queue
func (l *queue) Append(b []byte) error {
l.mu.Lock()
defer l.mu.Unlock()
if l.tail == nil {
return ErrNotOpen
}
if l.diskUsage()+int64(len(b)) > l.maxSize {
return ErrQueueFull
}
// Append the entry to the tail, if the segment is full,
// try to create new segment and retry the append
if err := l.tail.append(b); err == ErrSegmentFull {
segment, err := l.addSegment()
if err != nil {
return err
}
l.tail = segment
return l.tail.append(b)
}
return nil
}
// Current returns the current byte slice at the head of the queue
func (l *queue) Current() ([]byte, error) {
if l.head == nil {
return nil, ErrNotOpen
}
return l.head.current()
}
// Advance moves the head point to the next byte slice in the queue
func (l *queue) Advance() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.head == nil {
return ErrNotOpen
}
err := l.head.advance()
if err == io.EOF {
if err := l.trimHead(); err != nil {
return err
}
}
return nil
}
func (l *queue) trimHead() error {
if len(l.segments) > 1 {
l.segments = l.segments[1:]
if err := l.head.close(); err != nil {
return err
}
if err := os.Remove(l.head.path); err != nil {
return err
}
l.head = l.segments[0]
}
return nil
}
// Segment is a queue using a single file. The structure of a segment is a series
// lengths + block with a single footer point to the position in the segment of the
// current head block.
//
// ┌──────────────────────────┐ ┌──────────────────────────┐ ┌────────────┐
// │ Block 1 │ │ Block 2 │ │ Footer │
// └──────────────────────────┘ └──────────────────────────┘ └────────────┘
// ┌────────────┐┌────────────┐ ┌────────────┐┌────────────┐ ┌────────────┐
// │Block 1 Len ││Block 1 Body│ │Block 2 Len ││Block 2 Body│ │Head Offset │
// │ 8 bytes ││ N bytes │ │ 8 bytes ││ N bytes │ │ 8 bytes │
// └────────────┘└────────────┘ └────────────┘└────────────┘ └────────────┘
//
// The footer holds the pointer to the head entry at the end of the segment to allow writes
// to seek to the end and write sequentially (vs having to seek back to the beginning of
// the segment to update the head pointer). Reads must seek to the end then back into the
// segment offset stored in the footer.
//
// Segments store arbitrary byte slices and leave the serialization to the caller. Segments
// are created with a max size and will block writes when the segment is full.
type segment struct {
mu sync.RWMutex
size int64
file *os.File
path string
pos int64
currentSize int64
maxSize int64
}
func newSegment(path string, maxSize int64) (*segment, error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}
stats, err := os.Stat(path)
if err != nil {
return nil, err
}
s := &segment{file: f, path: path, size: stats.Size(), maxSize: maxSize}
if err := s.open(); err != nil {
return nil, err
}
return s, nil
}
func (l *segment) open() error {
l.mu.Lock()
defer l.mu.Unlock()
// If it's a new segment then write the location of the current record in this segment
if l.size == 0 {
l.pos = 0
l.currentSize = 0
if err := l.writeUint64(uint64(l.pos)); err != nil {
return err
}
if err := l.file.Sync(); err != nil {
return err
}
l.size = footerSize
return nil
}
// Existing segment so read the current position and the size of the current block
if err := l.seekEnd(-footerSize); err != nil {
return err
}
pos, err := l.readUint64()
if err != nil {
return err
}
l.pos = int64(pos)
if err := l.seekToCurrent(); err != nil {
return err
}
// If we're at the end of the segment, don't read the current block size,
// it's 0.
if l.pos < l.size-footerSize {
currentSize, err := l.readUint64()
if err != nil {
return err
}
l.currentSize = int64(currentSize)
}
return nil
}
// append adds byte slice to the end of segment
func (l *segment) append(b []byte) error {
l.mu.Lock()
defer l.mu.Unlock()
if l.file == nil {
return ErrNotOpen
}
if l.size+int64(len(b)) > l.maxSize {
return ErrSegmentFull
}
if err := l.seekEnd(-footerSize); err != nil {
return err
}
if err := l.writeUint64(uint64(len(b))); err != nil {
return err
}
if err := l.writeBytes(b); err != nil {
return err
}
if err := l.writeUint64(uint64(l.pos)); err != nil {
return err
}
if err := l.file.Sync(); err != nil {
return err
}
if l.currentSize == 0 {
l.currentSize = int64(len(b))
}
l.size += int64(len(b)) + 8 // uint64 for slice length
return nil
}
// current returns byte slice that the current segment points
func (l *segment) current() ([]byte, error) {
l.mu.Lock()
defer l.mu.Unlock()
if int64(l.pos) == l.size-8 {
return nil, io.EOF
}
if err := l.seekToCurrent(); err != nil {
return nil, err
}
// read the record size
sz, err := l.readUint64()
if err != nil {
return nil, err
}
l.currentSize = int64(sz)
b := make([]byte, sz)
if err := l.readBytes(b); err != nil {
return nil, err
}
return b, nil
}
// advance advances the current value pointer
func (l *segment) advance() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.file == nil {
return ErrNotOpen
}
// If we're at the end of the file, can't advance
if int64(l.pos) == l.size-footerSize {
l.currentSize = 0
return io.EOF
}
if err := l.seekEnd(-footerSize); err != nil {
return err
}
pos := l.pos + l.currentSize + 8
if err := l.writeUint64(uint64(pos)); err != nil {
return err
}
if err := l.file.Sync(); err != nil {
return err
}
l.pos = pos
if err := l.seekToCurrent(); err != nil {
return err
}
sz, err := l.readUint64()
if err != nil {
return err
}
l.currentSize = int64(sz)
if int64(l.pos) == l.size-footerSize {
l.currentSize = 0
return io.EOF
}
return nil
}
func (l *segment) close() error {
l.mu.Lock()
defer l.mu.Unlock()
if err := l.file.Close(); err != nil {
return err
}
l.file = nil
return nil
}
func (l *segment) lastModified() (time.Time, error) {
l.mu.RLock()
defer l.mu.RUnlock()
stats, err := os.Stat(l.file.Name())
if err != nil {
return time.Time{}, err
}
return stats.ModTime(), nil
}
func (l *segment) diskUsage() int64 {
l.mu.RLock()
defer l.mu.RUnlock()
return l.size
}
func (l *segment) SetMaxSegmentSize(size int64) {
l.mu.Lock()
defer l.mu.Unlock()
l.maxSize = size
}
func (l *segment) seekToCurrent() error {
return l.seek(int64(l.pos))
}
func (l *segment) seek(pos int64) error {
n, err := l.file.Seek(pos, os.SEEK_SET)
if err != nil {
return err
}
if n != pos {
return fmt.Errorf("bad seek. exp %v, got %v", 0, n)
}
return nil
}
func (l *segment) seekEnd(pos int64) error {
_, err := l.file.Seek(pos, os.SEEK_END)
if err != nil {
return err
}
return nil
}
func (l *segment) filePos() int64 {
n, _ := l.file.Seek(0, os.SEEK_CUR)
return n
}
func (l *segment) readUint64() (uint64, error) {
b := make([]byte, 8)
if err := l.readBytes(b); err != nil {
return 0, err
}
return btou64(b), nil
}
func (l *segment) writeUint64(sz uint64) error {
return l.writeBytes(u64tob(sz))
}
func (l *segment) writeBytes(b []byte) error {
n, err := l.file.Write(b)
if err != nil {
return err
}
if n != len(b) {
return fmt.Errorf("short write. got %d, exp %d", n, len(b))
}
return nil
}
func (l *segment) readBytes(b []byte) error {
n, err := l.file.Read(b)
if err != nil {
return err
}
if n != len(b) {
return fmt.Errorf("bad read. exp %v, got %v", 0, n)
}
return nil
}
func u64tob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}
func btou64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}