/
db.go
549 lines (467 loc) 路 11.9 KB
/
db.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
package frostdb
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/ulid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/thanos-io/objstore"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"github.com/polarsignals/frostdb/dynparquet"
walpb "github.com/polarsignals/frostdb/gen/proto/go/frostdb/wal/v1alpha1"
"github.com/polarsignals/frostdb/query/logicalplan"
"github.com/polarsignals/frostdb/wal"
)
type ColumnStore struct {
mtx *sync.RWMutex
dbs map[string]*DB
reg prometheus.Registerer
logger log.Logger
granuleSize int
activeMemorySize int64
storagePath string
bucket objstore.Bucket
enableWAL bool
// indexDegree is the degree of the btree index (default = 2)
indexDegree int
// splitSize is the number of new granules that are created when granules are split (default =2)
splitSize int
}
type Option func(*ColumnStore) error
func New(
logger log.Logger,
reg prometheus.Registerer,
options ...Option,
) (*ColumnStore, error) {
if reg == nil {
reg = prometheus.NewRegistry()
}
s := &ColumnStore{
mtx: &sync.RWMutex{},
dbs: map[string]*DB{},
reg: reg,
logger: logger,
indexDegree: 2,
splitSize: 2,
granuleSize: 8192,
activeMemorySize: 512 * 1024 * 1024, // 512MB
}
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
if s.enableWAL && s.storagePath == "" {
return nil, fmt.Errorf("storage path must be configured if WAL is enabled")
}
return s, nil
}
func WithGranuleSize(size int) Option {
return func(s *ColumnStore) error {
s.granuleSize = size
return nil
}
}
func WithActiveMemorySize(size int64) Option {
return func(s *ColumnStore) error {
s.activeMemorySize = size
return nil
}
}
func WithIndexDegree(indexDegree int) Option {
return func(s *ColumnStore) error {
s.indexDegree = indexDegree
return nil
}
}
func WithSplitSize(size int) Option {
return func(s *ColumnStore) error {
s.splitSize = size
return nil
}
}
func WithBucketStorage(bucket objstore.Bucket) Option {
return func(s *ColumnStore) error {
s.bucket = bucket
return nil
}
}
func WithWAL() Option {
return func(s *ColumnStore) error {
s.enableWAL = true
return nil
}
}
func WithStoragePath(path string) Option {
return func(s *ColumnStore) error {
s.storagePath = path
return nil
}
}
func (s *ColumnStore) Close() error {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, db := range s.dbs {
if err := db.Close(); err != nil {
return err
}
}
return nil
}
func (s *ColumnStore) DatabasesDir() string {
return filepath.Join(s.storagePath, "databases")
}
// ReplayWALs replays the write-ahead log for each database.
func (s *ColumnStore) ReplayWALs(ctx context.Context) error {
if !s.enableWAL {
return nil
}
dir := s.DatabasesDir()
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
level.Debug(s.logger).Log("msg", "WAL directory does not exist, no WAL to replay")
return nil
}
return err
}
files, err := os.ReadDir(dir)
if err != nil {
return err
}
g, ctx := errgroup.WithContext(ctx)
for _, f := range files {
databaseName := f.Name()
g.Go(func() error {
db, err := s.DB(databaseName)
if err != nil {
return err
}
return db.replayWAL(ctx)
})
}
return g.Wait()
}
type dbMetrics struct {
txHighWatermark prometheus.GaugeFunc
}
type DB struct {
columnStore *ColumnStore
logger log.Logger
name string
mtx *sync.RWMutex
tables map[string]*Table
reg prometheus.Registerer
storagePath string
wal WAL
bucket objstore.Bucket
// Databases monotonically increasing transaction id
tx *atomic.Uint64
// TxPool is a waiting area for finished transactions that haven't been added to the watermark
txPool *TxPool
// highWatermark maintains the highest consecutively completed tx number
highWatermark *atomic.Uint64
metrics *dbMetrics
}
func (s *ColumnStore) DB(name string) (*DB, error) {
s.mtx.RLock()
db, ok := s.dbs[name]
s.mtx.RUnlock()
if ok {
return db, nil
}
s.mtx.Lock()
defer s.mtx.Unlock()
// Need to double-check that in the meantime a database with the same name
// wasn't concurrently created.
db, ok = s.dbs[name]
if ok {
return db, nil
}
highWatermark := atomic.NewUint64(0)
reg := prometheus.WrapRegistererWith(prometheus.Labels{"db": name}, s.reg)
db = &DB{
columnStore: s,
name: name,
mtx: &sync.RWMutex{},
tables: map[string]*Table{},
reg: reg,
tx: atomic.NewUint64(0),
highWatermark: highWatermark,
storagePath: filepath.Join(s.DatabasesDir(), name),
logger: s.logger,
wal: &wal.NopWAL{},
metrics: &dbMetrics{
txHighWatermark: promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{
Name: "tx_high_watermark",
Help: "The highest transaction number that has been released to be read",
}, func() float64 {
return float64(highWatermark.Load())
}),
},
}
if s.bucket != nil {
db.bucket = NewPrefixedBucket(s.bucket, db.name)
}
if s.enableWAL {
var err error
db.wal, err = db.openWAL()
if err != nil {
return nil, err
}
}
db.txPool = NewTxPool(db.highWatermark)
s.dbs[name] = db
return db, nil
}
func (db *DB) openWAL() (WAL, error) {
return wal.Open(
db.logger,
db.reg,
db.walDir(),
)
}
func (db *DB) walDir() string {
return filepath.Join(db.storagePath, "wal")
}
func (db *DB) replayWAL(ctx context.Context) error {
persistedBlocks := map[ulid.ULID]struct{}{}
if err := db.wal.Replay(func(tx uint64, record *walpb.Record) error {
switch e := record.Entry.EntryType.(type) {
case *walpb.Entry_TableBlockPersisted_:
entry := e.TableBlockPersisted
var id ulid.ULID
if err := id.UnmarshalBinary(entry.BlockId); err != nil {
return err
}
persistedBlocks[id] = struct{}{}
}
return nil
}); err != nil {
return fmt.Errorf("first WAL replay: %w", err)
}
lastTx := uint64(0)
if err := db.wal.Replay(func(tx uint64, record *walpb.Record) error {
lastTx = tx
switch e := record.Entry.EntryType.(type) {
case *walpb.Entry_NewTableBlock_:
entry := e.NewTableBlock
var id ulid.ULID
if err := id.UnmarshalBinary(entry.BlockId); err != nil {
return err
}
if _, ok := persistedBlocks[id]; ok {
// This block has already been successfully persisted, so we can skip it.
return nil
}
tableName := entry.TableName
table, err := db.GetTable(tableName)
var tableErr ErrTableNotFound
if errors.As(err, &tableErr) {
schema, err := dynparquet.SchemaFromDefinition(entry.Schema)
if err != nil {
return fmt.Errorf("initialize schema: %w", err)
}
table, err = newTable(
db,
tableName,
NewTableConfig(schema),
db.reg,
db.logger,
db.wal,
)
if err != nil {
return fmt.Errorf("instantiate table: %w", err)
}
db.tables[tableName] = table
table.active, err = newTableBlock(table, 0, tx, id)
if err != nil {
return err
}
return nil
}
if err != nil {
return fmt.Errorf("get table: %w", err)
}
// If we get to this point it means a block was finished but did
// not get persisted.
table.pendingBlocks[table.active] = struct{}{}
go table.writeBlock(table.active)
if !proto.Equal(entry.Schema, table.config.schema.Definition()) {
// If schemas are identical from block to block we should we
// reuse the previous schema in order to retain pooled memory
// for it.
schema, err := dynparquet.SchemaFromDefinition(entry.Schema)
if err != nil {
return fmt.Errorf("instantiate schema: %w", err)
}
table.config.schema = schema
}
table.active, err = newTableBlock(table, table.active.minTx, tx, id)
if err != nil {
return err
}
case *walpb.Entry_Write_:
entry := e.Write
tableName := entry.TableName
table, err := db.GetTable(tableName)
var tableErr ErrTableNotFound
if errors.As(err, &tableErr) {
// This means the WAL was truncated at a point where this write
// was already successfully persisted to disk in more optimized
// form than the WAL.
return nil
}
if err != nil {
return fmt.Errorf("get table: %w", err)
}
serBuf, err := dynparquet.ReaderFromBytes(entry.Data)
if err != nil {
return fmt.Errorf("deserialize buffer: %w", err)
}
if err := table.active.Insert(ctx, tx, serBuf); err != nil {
return fmt.Errorf("insert buffer into block: %w", err)
}
case *walpb.Entry_TableBlockPersisted_:
return nil
default:
return fmt.Errorf("unexpected WAL entry type: %t", e)
}
return nil
}); err != nil {
return fmt.Errorf("second WAL replay: %w", err)
}
db.tx.Store(lastTx)
db.highWatermark.Store(lastTx)
return nil
}
func (db *DB) Close() error {
if db.columnStore.enableWAL {
if err := db.wal.Close(); err != nil {
return err
}
}
return nil
}
func (db *DB) maintainWAL() {
db.mtx.RLock()
defer db.mtx.RUnlock()
minTx := uint64(0)
for _, table := range db.tables {
table.mtx.RLock()
tableMinTxPersisted := table.lastCompleted
table.mtx.RUnlock()
if minTx == 0 || tableMinTxPersisted < minTx {
minTx = tableMinTxPersisted
}
}
if minTx > 0 {
if err := db.wal.Truncate(minTx); err != nil {
return
}
}
}
func (db *DB) Table(name string, config *TableConfig) (*Table, error) {
db.mtx.RLock()
table, ok := db.tables[name]
db.mtx.RUnlock()
if ok {
return table, nil
}
db.mtx.Lock()
defer db.mtx.Unlock()
// Need to double-check that in the meantime another table with the same
// name wasn't concurrently created.
table, ok = db.tables[name]
if ok {
return table, nil
}
table, err := newTable(
db,
name,
config,
db.reg,
db.logger,
db.wal,
)
if err != nil {
return nil, fmt.Errorf("failed to create table: %w", err)
}
tx, _, commit := db.begin()
defer commit()
id := generateULID()
if err := table.newTableBlock(0, tx, id); err != nil {
return nil, err
}
db.tables[name] = table
return table, nil
}
type ErrTableNotFound struct {
tableName string
}
func (e ErrTableNotFound) Error() string {
return fmt.Sprintf("table %q not found", e.tableName)
}
func (db *DB) GetTable(name string) (*Table, error) {
db.mtx.RLock()
table, ok := db.tables[name]
db.mtx.RUnlock()
if !ok {
return nil, ErrTableNotFound{tableName: name}
}
return table, nil
}
func (db *DB) TableProvider() *DBTableProvider {
return NewDBTableProvider(db)
}
type DBTableProvider struct {
db *DB
}
func NewDBTableProvider(db *DB) *DBTableProvider {
return &DBTableProvider{
db: db,
}
}
func (p *DBTableProvider) GetTable(name string) logicalplan.TableReader {
p.db.mtx.RLock()
defer p.db.mtx.RUnlock()
return p.db.tables[name]
}
// beginRead returns the high watermark. Reads can safely access any write that has a lower or equal tx id than the returned number.
func (db *DB) beginRead() uint64 {
return db.highWatermark.Load()
}
// begin is an internal function that Tables call to start a transaction for writes.
// It returns:
// the write tx id
// The current high watermark
// A function to complete the transaction
func (db *DB) begin() (uint64, uint64, func()) {
tx := db.tx.Inc()
watermark := db.highWatermark.Load()
return tx, watermark, func() {
if mark := db.highWatermark.Load(); mark+1 == tx { // This is the next consecutive transaction; increate the watermark
db.highWatermark.Inc()
}
// place completed transaction in the waiting pool
db.txPool.Prepend(tx)
}
}
// Wait is a blocking function that returns once the high watermark has equaled or exceeded the transaction id.
// Wait makes no differentiation between completed and aborted transactions.
func (db *DB) Wait(tx uint64) {
for {
if db.highWatermark.Load() >= tx {
return
}
time.Sleep(10 * time.Millisecond)
}
}