forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocksdb.go
304 lines (262 loc) · 6.72 KB
/
rocksdb.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
package storage
import (
"encoding/binary"
"fmt"
"os"
"sort"
"sync"
"sync/atomic"
"regexp"
"github.com/contribsys/faktory/storage/brodal"
"github.com/contribsys/faktory/util"
"github.com/contribsys/gorocksdb"
)
type rocksStore struct {
Name string
db *gorocksdb.DB
opts *gorocksdb.Options
retries *rocksSortedSet
scheduled *rocksSortedSet
working *rocksSortedSet
dead *rocksSortedSet
clients *rocksSortedSet
defalt *gorocksdb.ColumnFamilyHandle
queues *gorocksdb.ColumnFamilyHandle
stats *gorocksdb.ColumnFamilyHandle
queueSet map[string]*rocksQueue
mu sync.Mutex
history *processingHistory
}
func DefaultOptions() *gorocksdb.Options {
opts := gorocksdb.NewDefaultOptions()
opts.SetCreateIfMissing(true)
opts.SetCreateIfMissingColumnFamilies(true)
opts.IncreaseParallelism(2)
// 16MB buffer minimizes the number of jobs we'll write to disk.
// Ideally jobs are processed in-memory, before they need to be
// flushed to disk.
opts.SetWriteBufferSize(16 * 1024 * 1024)
// default is 6 hrs, set to 1 hr
opts.SetDeleteObsoleteFilesPeriodMicros(1000000 * 3600)
opts.SetKeepLogFileNum(10)
return opts
}
var registerMutex sync.Mutex
func OpenRocks(path string) (Store, error) {
util.Infof("Initializing storage at %s", path)
util.Debugf("Using RocksDB v%s", gorocksdb.RocksDBVersion())
err := os.MkdirAll(path, os.ModeDir|0755)
if err != nil {
return nil, err
}
opts := DefaultOptions()
sopts := gorocksdb.NewDefaultOptions()
// the global registration function in gorocksdb, registerMergeOperator seems to be racy
registerMutex.Lock()
sopts.SetMergeOperator(&int64CounterMerge{})
registerMutex.Unlock()
db, handles, err := gorocksdb.OpenDbColumnFamilies(opts, path,
[]string{"scheduled", "retries", "working", "dead", "clients", "default", "queues", "stats"},
[]*gorocksdb.Options{opts, opts, opts, opts, opts, opts, opts, sopts})
if err != nil {
return nil, err
}
ro := gorocksdb.NewDefaultReadOptions()
wo := gorocksdb.NewDefaultWriteOptions()
rs := &rocksStore{
Name: path,
db: db,
opts: opts,
scheduled: (&rocksSortedSet{name: "scheduled", db: db, cf: handles[0], ro: ro, wo: wo, size: 0}).init(),
retries: (&rocksSortedSet{name: "retries", db: db, cf: handles[1], ro: ro, wo: wo, size: 0}).init(),
working: (&rocksSortedSet{name: "working", db: db, cf: handles[2], ro: ro, wo: wo, size: 0}).init(),
dead: (&rocksSortedSet{name: "dead", db: db, cf: handles[3], ro: ro, wo: wo, size: 0}).init(),
clients: (&rocksSortedSet{name: "clients", db: db, cf: handles[4], ro: ro, wo: wo, size: 0}).init(),
defalt: handles[5],
queues: handles[6],
stats: handles[7],
queueSet: make(map[string]*rocksQueue),
mu: sync.Mutex{},
history: &processingHistory{},
}
err = rs.init()
if err != nil {
return nil, err
}
return rs, nil
}
func (store *rocksStore) Stats() map[string]string {
return map[string]string{
"stats": store.db.GetProperty("rocksdb.stats"),
"name": store.db.Name(),
}
}
func (store *rocksStore) Processed() int64 {
return atomic.LoadInt64(&store.history.TotalProcessed)
}
func (store *rocksStore) Failures() int64 {
return atomic.LoadInt64(&store.history.TotalFailures)
}
// queues are iterated in sorted, lexigraphical order
func (store *rocksStore) EachQueue(x func(Queue)) {
store.mu.Lock()
keys := make([]string, 0, len(store.queueSet))
for k := range store.queueSet {
keys = append(keys, k)
}
store.mu.Unlock()
sort.Strings(keys)
for _, k := range keys {
x(store.queueSet[k])
}
}
func (store *rocksStore) Flush() error {
// This is a very slow implementation. Are there lower-level
// RocksDB operations we can use to bulk-delete everything
// in a column family or database?
store.mu.Lock()
defer store.mu.Unlock()
var err error
keys := make([]string, 0, len(store.queueSet))
for k, q := range store.queueSet {
keys = append(keys, k)
_, err = q.Clear()
if err != nil {
return err
}
}
for _, k := range keys {
delete(store.queueSet, k)
}
_, err = store.retries.Clear()
if err != nil {
return err
}
_, err = store.scheduled.Clear()
if err != nil {
return err
}
_, err = store.dead.Clear()
if err != nil {
return err
}
_, err = store.working.Clear()
if err != nil {
return err
}
_, err = store.clients.Clear()
// flush doesn't clear the stats or default space
return err
}
func (store *rocksStore) init() error {
ro := queueReadOptions(false)
ro.SetFillCache(false)
defer ro.Destroy()
it := store.db.NewIteratorCF(ro, store.queues)
defer it.Close()
it.SeekToFirst()
cur := ""
for ; it.Valid(); it.Next() {
if it.Err() != nil {
return it.Err()
}
k := it.Key()
key := k.Data()
for i := 0; i < len(key); i++ {
if key[i] == uint8(255) {
name := string(key[0:i])
if cur != name {
if cur != "" {
store.GetQueue(name)
}
cur = name
}
break
}
}
k.Free()
}
if cur != "" {
_, err := store.GetQueue(cur)
if err != nil {
return err
}
}
ro = gorocksdb.NewDefaultReadOptions()
defer ro.Destroy()
value, err := store.db.GetBytesCF(ro, store.stats, []byte("Processed"))
if err != nil {
return err
}
if value != nil {
store.history.TotalProcessed, _ = binary.Varint(value)
}
value, err = store.db.GetBytesCF(ro, store.stats, []byte("Failures"))
if err != nil {
return err
}
if value != nil {
store.history.TotalFailures, _ = binary.Varint(value)
}
return nil
}
var (
ValidQueueName = regexp.MustCompile(`\A[a-zA-Z0-9._-]+\z`)
)
func (store *rocksStore) GetQueue(name string) (Queue, error) {
if name == "" {
return nil, fmt.Errorf("queue name cannot be blank")
}
store.mu.Lock()
defer store.mu.Unlock()
q, ok := store.queueSet[name]
if ok {
return q, nil
}
if !ValidQueueName.MatchString(name) {
return nil, fmt.Errorf("queue names must match %v", ValidQueueName)
}
q = &rocksQueue{
name: name,
size: 0,
store: store,
cf: store.queues,
maxsz: DefaultMaxSize,
pointers: make(map[uint8]*queuePointer),
orderedPointers: brodal.NewHeap(),
}
err := q.Init()
if err != nil {
return nil, err
}
store.queueSet[name] = q
return q, nil
}
func (store *rocksStore) Close() error {
util.Info("Stopping storage")
store.mu.Lock()
defer store.mu.Unlock()
for _, q := range store.queueSet {
q.Close()
}
store.dead.Close()
store.retries.Close()
store.working.Close()
store.scheduled.Close()
store.defalt.Destroy()
store.queues.Destroy()
store.db.Close()
return nil
}
func (store *rocksStore) Retries() SortedSet {
return store.retries
}
func (store *rocksStore) Scheduled() SortedSet {
return store.scheduled
}
func (store *rocksStore) Working() SortedSet {
return store.working
}
func (store *rocksStore) Dead() SortedSet {
return store.dead
}