forked from BTrDB/btrdb-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quasar.go
331 lines (305 loc) · 8 KB
/
quasar.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
package btrdb
import (
"fmt"
"sync"
"time"
"github.com/pborman/uuid"
"github.com/SoftwareDefinedBuildings/btrdb/internal/bstore"
"github.com/SoftwareDefinedBuildings/btrdb/qtree"
"github.com/op/go-logging"
)
var log *logging.Logger
func init() {
log = logging.MustGetLogger("log")
}
type openTree struct {
store []qtree.Record
id uuid.UUID
sigEC chan bool
}
const MinimumTime = -(16 << 56)
const MaximumTime = (48 << 56)
const LatestGeneration = bstore.LatestGeneration
type Quasar struct {
cfg QuasarConfig
bs *bstore.BlockStore
//Transaction coalescence
globlock sync.Mutex
treelocks map[[16]byte]*sync.Mutex
openTrees map[[16]byte]*openTree
}
func newOpenTree(id uuid.UUID) *openTree {
return &openTree{
id: id,
}
}
type QuasarConfig struct {
//Measured in the number of datablocks
//So 1000 is 8 MB cache
DatablockCacheSize uint64
//This enables the grouping of value inserts
//with a commit every Interval millis
//If the number of stored values exceeds
//EarlyTrip
TransactionCoalesceEnable bool
TransactionCoalesceInterval uint64
TransactionCoalesceEarlyTrip uint64
Params map[string]string
}
// Return true if there are uncommited results to be written to disk
// Should only be used during shutdown as it hogs the glock
func (q *Quasar) IsPending() bool {
isPend := false
q.globlock.Lock()
for uuid, ot := range q.openTrees {
q.treelocks[uuid].Lock()
if len(ot.store) != 0 {
isPend = true
q.treelocks[uuid].Unlock()
break
}
q.treelocks[uuid].Unlock()
}
q.globlock.Unlock()
return isPend
}
func NewQuasar(cfg *QuasarConfig) (*Quasar, error) {
bs, err := bstore.NewBlockStore(cfg.Params)
if err != nil {
return nil, err
}
rv := &Quasar{
cfg: *cfg,
bs: bs,
openTrees: make(map[[16]byte]*openTree, 128),
treelocks: make(map[[16]byte]*sync.Mutex, 128),
}
return rv, nil
}
func (q *Quasar) getTree(id uuid.UUID) (*openTree, *sync.Mutex) {
mk := bstore.UUIDToMapKey(id)
q.globlock.Lock()
ot, ok := q.openTrees[mk]
if !ok {
ot := newOpenTree(id)
mtx := &sync.Mutex{}
q.openTrees[mk] = ot
q.treelocks[mk] = mtx
q.globlock.Unlock()
return ot, mtx
}
mtx, ok := q.treelocks[mk]
if !ok {
log.Panicf("This should not happen")
}
q.globlock.Unlock()
return ot, mtx
}
func (t *openTree) commit(q *Quasar) {
if len(t.store) == 0 {
//This might happen with a race in the timeout commit
fmt.Println("no store in commit")
return
}
tr, err := qtree.NewWriteQTree(q.bs, t.id)
if err != nil {
log.Panic(err)
}
if err := tr.InsertValues(t.store); err != nil {
log.Error("BAD INSERT: ", err)
}
tr.Commit()
t.store = nil
}
func (q *Quasar) InsertValues(id uuid.UUID, r []qtree.Record) {
defer func() {
if r := recover(); r != nil {
log.Error("BAD INSERT: ", r)
}
}()
tr, mtx := q.getTree(id)
mtx.Lock()
if tr == nil {
log.Panicf("This should not happen")
}
if tr.store == nil {
//Empty store
tr.store = make([]qtree.Record, 0, len(r)*2)
tr.sigEC = make(chan bool, 1)
//Also spawn the coalesce timeout goroutine
go func(abrt chan bool) {
tmt := time.After(time.Duration(q.cfg.TransactionCoalesceInterval) * time.Millisecond)
select {
case <-tmt:
//do coalesce
mtx.Lock()
//In case we early tripped between waiting for lock and getting it, commit will return ok
//log.Debug("Coalesce timeout %v", id.String())
tr.commit(q)
mtx.Unlock()
case <-abrt:
return
}
}(tr.sigEC)
}
tr.store = append(tr.store, r...)
if uint64(len(tr.store)) >= q.cfg.TransactionCoalesceEarlyTrip {
tr.sigEC <- true
log.Debug("Coalesce early trip %v", id.String())
tr.commit(q)
}
mtx.Unlock()
}
func (q *Quasar) Flush(id uuid.UUID) error {
tr, mtx := q.getTree(id)
mtx.Lock()
if len(tr.store) != 0 {
tr.sigEC <- true
tr.commit(q)
fmt.Printf("Commit done %+v\n", id)
} else {
fmt.Printf("no store\n")
}
mtx.Unlock()
return nil
}
//These functions are the API. TODO add all the bounds checking on PW, and sanity on start/end
func (q *Quasar) QueryValues(id uuid.UUID, start int64, end int64, gen uint64) ([]qtree.Record, uint64, error) {
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return nil, 0, err
}
rv, err := tr.ReadStandardValuesBlock(start, end)
return rv, tr.Generation(), err
}
func (q *Quasar) QueryValuesStream(id uuid.UUID, start int64, end int64, gen uint64) (chan qtree.Record, chan error, uint64) {
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return nil, nil, 0
}
recordc := make(chan qtree.Record)
errc := make(chan error)
go tr.ReadStandardValuesCI(recordc, errc, start, end)
return recordc, errc, tr.Generation()
}
func (q *Quasar) QueryStatisticalValues(id uuid.UUID, start int64, end int64,
gen uint64, pointwidth uint8) ([]qtree.StatRecord, uint64, error) {
//fmt.Printf("QSV0 s=%v e=%v pw=%v\n", start, end, pointwidth)
start &^= ((1 << pointwidth) - 1)
end &^= ((1 << pointwidth) - 1)
end -= 1
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return nil, 0, err
}
rv, err := tr.QueryStatisticalValuesBlock(start, end, pointwidth)
if err != nil {
return nil, 0, err
}
return rv, tr.Generation(), nil
}
func (q *Quasar) QueryStatisticalValuesStream(id uuid.UUID, start int64, end int64,
gen uint64, pointwidth uint8) (chan qtree.StatRecord, chan error, uint64) {
fmt.Printf("QSV1 s=%v e=%v pw=%v\n", start, end, pointwidth)
start &^= ((1 << pointwidth) - 1)
end &^= ((1 << pointwidth) - 1)
end -= 1
rvv := make(chan qtree.StatRecord, 1024)
rve := make(chan error)
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return nil, nil, 0
}
go tr.QueryStatisticalValues(rvv, rve, start, end, pointwidth)
return rvv, rve, tr.Generation()
}
func (q *Quasar) QueryWindow(id uuid.UUID, start int64, end int64,
gen uint64, width uint64, depth uint8) (chan qtree.StatRecord, uint64) {
rvv := make(chan qtree.StatRecord, 1024)
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return nil, 0
}
go tr.QueryWindow(start, end, width, depth, rvv)
return rvv, tr.Generation()
}
func (q *Quasar) QueryGeneration(id uuid.UUID) (uint64, error) {
sb := q.bs.LoadSuperblock(id, bstore.LatestGeneration)
if sb == nil {
return 0, qtree.ErrNoSuchStream
}
return sb.Gen(), nil
}
func (q *Quasar) QueryNearestValue(id uuid.UUID, time int64, backwards bool, gen uint64) (qtree.Record, uint64, error) {
tr, err := qtree.NewReadQTree(q.bs, id, gen)
if err != nil {
return qtree.Record{}, 0, err
}
rv, err := tr.FindNearestValue(time, backwards)
return rv, tr.Generation(), err
}
type ChangedRange struct {
Start int64
End int64
}
//Resolution is how far down the tree to go when working out which blocks have changed. Higher resolutions are faster
//but will give you back coarser results.
func (q *Quasar) QueryChangedRanges(id uuid.UUID, startgen uint64, endgen uint64, resolution uint8) ([]ChangedRange, uint64, error) {
//0 is a reserved generation, so is 1, which means "before first"
if startgen == 0 {
startgen = 1
}
tr, err := qtree.NewReadQTree(q.bs, id, endgen)
if err != nil {
log.Debug("Error on QCR open tree")
return nil, 0, err
}
rv := make([]ChangedRange, 0, 1024)
rch := tr.FindChangedSince(startgen, resolution)
var lr *ChangedRange = nil
for {
select {
case cr, ok := <-rch:
if !ok {
//This is the end.
//Do we have an unsaved LR?
if lr != nil {
rv = append(rv, *lr)
}
return rv, tr.Generation(), nil
}
if !cr.Valid {
log.Panicf("Didn't think this could happen")
}
//Coalesce
if lr != nil && cr.Start == lr.End {
lr.End = cr.End
} else {
if lr != nil {
rv = append(rv, *lr)
}
lr = &ChangedRange{Start: cr.Start, End: cr.End}
}
}
}
return rv, tr.Generation(), nil
}
func (q *Quasar) DeleteRange(id uuid.UUID, start int64, end int64) error {
tr, mtx := q.getTree(id)
mtx.Lock()
if len(tr.store) != 0 {
tr.sigEC <- true
tr.commit(q)
}
wtr, err := qtree.NewWriteQTree(q.bs, id)
if err != nil {
log.Panic(err)
}
err = wtr.DeleteRange(start, end)
if err != nil {
log.Panic(err)
}
wtr.Commit()
mtx.Unlock()
return nil
}