-
Notifications
You must be signed in to change notification settings - Fork 32
/
bigqueue.go
234 lines (196 loc) · 4.73 KB
/
bigqueue.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
package bigqueue
import (
"errors"
"fmt"
"sync"
"time"
)
const (
cInt64Size = 8
cFilePerm = 0744
cDefaultConsumer = "__default__"
)
var (
// ErrInvalidArenaSize is returned when persisted arena size
// doesn't match with desired arena size.
ErrInvalidArenaSize = errors.New("mismatch in arena size")
// ErrDifferentQueues is returned when caller wants to copy
// offsets from a consumer from a different queue.
ErrDifferentQueues = errors.New("consumers from different queues")
)
// MmapQueue implements Queue interface.
type MmapQueue struct {
conf *bqConfig
am *arenaManager
md *metadata
dc int64 // default consumer
mutOps int64
lastFlush time.Time
lock sync.Mutex // protects bigqueue
drain chan struct{}
quit chan struct{}
wg sync.WaitGroup
br bytesReader
sr stringReader
bw bytesWriter
sw stringWriter
}
// NewMmapQueue constructs a new persistent queue.
func NewMmapQueue(dir string, opts ...Option) (*MmapQueue, error) {
complete := false
conf := newConfig()
for _, opt := range opts {
if err := opt(conf); err != nil {
return nil, err
}
}
md, err := newMetadata(dir, conf.arenaSize)
if err != nil {
return nil, err
}
defer func() {
if !complete {
_ = md.close()
}
}()
// create arena manager
am, err := newArenaManager(dir, conf, md)
if err != nil {
return nil, err
}
defer func() {
if !complete {
_ = am.close()
}
}()
// ensure that the arena size, if queue had existed,
// matches with the given arena size.
existingSize := md.getArenaSize()
if existingSize == 0 {
md.putArenaSize(conf.arenaSize)
} else if existingSize != conf.arenaSize {
return nil, ErrInvalidArenaSize
}
dc, err := md.getConsumer(cDefaultConsumer)
if err != nil {
return nil, fmt.Errorf("error in adding default consumer :: %w", err)
}
bq := &MmapQueue{
conf: conf,
am: am,
md: md,
dc: dc,
drain: make(chan struct{}, 1),
quit: make(chan struct{}),
}
bq.wg.Add(1)
go bq.periodicFlush()
complete = true
return bq, nil
}
// NewConsumer creates a new consumer or finds an existing one with same name.
func (q *MmapQueue) NewConsumer(name string) (*Consumer, error) {
q.lock.Lock()
defer q.lock.Unlock()
base, err := q.md.getConsumer(name)
if err != nil {
return nil, err
}
return &Consumer{mq: q, base: base}, nil
}
// FromConsumer creates a new consumer or finds an existing one with same name.
// It also copies the offsets from the given consumer to this consumer.
func (q *MmapQueue) FromConsumer(name string, from *Consumer) (*Consumer, error) {
if q != from.mq {
return nil, ErrDifferentQueues
}
q.lock.Lock()
defer q.lock.Unlock()
base, err := q.md.getConsumer(name)
if err != nil {
return nil, err
}
// update offsets to given consumer
aid, pos := q.md.getConsumerHead(from.base)
q.md.putConsumerHead(base, aid, pos)
return &Consumer{mq: q, base: base}, nil
}
// Close will close metadata and arena manager.
func (q *MmapQueue) Close() error {
// wait for background go routines to finish.
// we need to acquire lock afterwards so that we avoid a livelock
// between periodic flush goroutine and Close() function.
// A possible lock cycle is here:
// Close() after it has acquired the lock ->
// -> Close() waiting for periodic flush goroutine to stop ->
// -> Periodic flush goroutine is trying to acquire the lock ->
// -> Close() to release the lock
close(q.quit)
q.wg.Wait()
q.lock.Lock()
defer q.lock.Unlock()
var retErr error
if err := q.md.close(); err != nil {
retErr = err
}
if err := q.am.close(); err != nil {
retErr = err
}
return retErr
}
// Flush syncs the in memory content of bigqueue to disk.
func (q *MmapQueue) Flush() error {
q.lock.Lock()
defer q.lock.Unlock()
if err := q.am.flush(); err != nil {
return err
}
if err := q.md.flush(); err != nil {
return err
}
q.mutOps = 0
q.lastFlush = time.Now()
return nil
}
func (q *MmapQueue) incrMutOps() {
if q.conf.flushMutOps <= 0 {
return
}
q.mutOps++
if q.mutOps >= q.conf.flushMutOps {
select {
case q.drain <- struct{}{}:
default:
}
}
}
// setupFlush sets up background go routine to periodically flush data.
func (q *MmapQueue) periodicFlush() {
defer q.wg.Done()
if q.conf.flushPeriod <= 0 && q.conf.flushMutOps <= 0 {
return
}
timer := &time.Timer{C: make(chan time.Time)}
if q.conf.flushPeriod > 0 {
timer = time.NewTimer(time.Duration(q.conf.flushPeriod))
}
var drainFlag bool
for {
if q.conf.flushPeriod != 0 {
if !drainFlag && !timer.Stop() {
<-timer.C
}
timer.Reset(time.Duration(q.conf.flushPeriod))
drainFlag = false
}
select {
case <-q.quit:
return
case <-q.drain:
_ = q.Flush()
case <-timer.C:
drainFlag = true
_ = q.Flush()
}
}
}