-
Notifications
You must be signed in to change notification settings - Fork 17
/
ldb_reader.go
711 lines (618 loc) · 18.2 KB
/
ldb_reader.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package ctlstore
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/segmentio/errors-go"
"github.com/segmentio/events/v2"
"github.com/segmentio/stats/v4"
"github.com/segmentio/ctlstore/pkg/errs"
"github.com/segmentio/ctlstore/pkg/globalstats"
"github.com/segmentio/ctlstore/pkg/ldb"
"github.com/segmentio/ctlstore/pkg/scanfunc"
"github.com/segmentio/ctlstore/pkg/schema"
"github.com/segmentio/ctlstore/pkg/sqlgen"
)
// LDBReader reads data from the LDB. The external interface is
// thread-safe and it is safe to create as many of these as needed
// across multiple processes.
type LDBReader struct {
Db *sql.DB
path string
pkCache map[string]schema.PrimaryKey // keyed by ldbTableName()
getRowByKeyStmtCache map[string]*sql.Stmt // keyed by ldbTableName()
getRowsByKeyPrefixStmtCache map[prefixCacheKey]*sql.Stmt
mu sync.RWMutex
cancelWatcher context.CancelFunc
}
type prefixCacheKey struct {
ldbTableName string
numKeys int
}
var (
ErrTableHasNoPrimaryKey = errors.New("Table provided has no primary key")
ErrNeedFullKey = errors.New("All primary key fields are required")
ErrNoLedgerUpdates = errors.New("no ledger updates have been received yet")
)
type RowRetriever interface {
GetRowsByKeyPrefix(ctx context.Context, familyName string, tableName string, key ...interface{}) (*Rows, error)
GetRowByKey(ctx context.Context, out interface{}, familyName string, tableName string, key ...interface{}) (found bool, err error)
}
func newLDBReader(path string) (*LDBReader, error) {
db, err := newLDB(path)
if err != nil {
return nil, err
}
return &LDBReader{Db: db, path: path}, nil
}
func newVersionedLDBReader(dirPath string) (*LDBReader, error) {
ctx, cancel := context.WithCancel(context.Background())
reader := &LDBReader{
cancelWatcher: cancel,
}
// To initialize this reader, we must first load an LDB:
last, err := lookupLastLDBSync(dirPath)
if err != nil {
return nil, errors.Wrap(err, "checking last ldb sync")
}
if last == 0 {
return nil, fmt.Errorf("no LDB in path (%s)", dirPath)
}
err = reader.switchLDB(dirPath, last)
if err != nil {
return nil, errors.Wrap(err, "switching ldbs")
}
// Then we can defer to the watcher goroutine to swap this
// reader LDB if a newer one appears:
go reader.watchForLDBs(ctx, dirPath, last)
return reader, nil
}
func newLDB(path string) (*sql.DB, error) {
_, err := os.Stat(path)
switch {
case os.IsNotExist(err):
return nil, fmt.Errorf("no LDB found at %s", path)
case err != nil:
return nil, err
}
var db *sql.DB
if ldbVersioning {
db, err = ldb.OpenImmutableLDB(path)
} else {
mode := "ro"
if !globalLDBReadOnly {
mode = "rwc"
}
db, err = ldb.OpenLDB(path, mode)
}
if err != nil {
return nil, err
}
return db, nil
}
// Constructs an LDBReader from a sql.DB. Really only useful for testing.
func NewLDBReaderFromDB(db *sql.DB) *LDBReader {
return &LDBReader{Db: db}
}
// GetLastSequence returns the highest sequence number applied to the DB
func (reader *LDBReader) GetLastSequence(ctx context.Context) (schema.DMLSequence, error) {
ctx = discardContext()
reader.mu.RLock()
defer reader.mu.RUnlock()
return ldb.FetchSeqFromLdb(ctx, reader.Db)
}
// GetLedgerLatency returns the difference between the current time and the timestamp
// from the last DML ledger update processed by the reflector. ErrNoLedgerUpdates will
// be returned if no DML statements have been processed.
func (reader *LDBReader) GetLedgerLatency(ctx context.Context) (time.Duration, error) {
ctx = discardContext()
row := reader.Db.QueryRowContext(ctx, "select timestamp from "+ldb.LDBLastUpdateTableName+" where name=?", ldb.LDBLastLedgerUpdateColumn)
var timestamp time.Time
err := row.Scan(×tamp)
switch {
case err == sql.ErrNoRows:
return 0, ErrNoLedgerUpdates
case err != nil:
return 0, errors.Wrap(err, "get ledger latency")
default:
return time.Now().Sub(timestamp), nil
}
}
// GetRowsByKeyPrefix returns a *Rows iterator that will supply all of the rows in
// the family and table match the supplied primary key prefix.
func (reader *LDBReader) GetRowsByKeyPrefix(ctx context.Context, familyName string, tableName string, key ...interface{}) (*Rows, error) {
ctx = discardContext()
start := time.Now()
defer func() {
globalstats.Observe("get_rows_by_key_prefix", time.Now().Sub(start),
stats.T("family", familyName),
stats.T("table", tableName))
}()
reader.mu.RLock()
defer reader.mu.RUnlock()
famName, err := schema.NewFamilyName(familyName)
if err != nil {
return nil, err
}
tblName, err := schema.NewTableName(tableName)
if err != nil {
return nil, err
}
ldbTable := schema.LDBTableName(famName, tblName)
pk, err := reader.getPrimaryKey(ctx, ldbTable)
if err != nil {
return nil, err
}
if pk.Zero() {
return nil, ErrTableHasNoPrimaryKey
}
if len(key) > len(pk.Fields) {
return nil, errors.New("too many keys supplied for table's primary key")
}
err = convertKeyBeforeQuery(pk, key)
if err != nil {
return nil, err
}
stmt, err := reader.getRowsByKeyPrefixStmt(ctx, pk, ldbTable, len(key))
if err != nil {
return nil, err
}
if len(key) == 0 {
globalstats.Incr("full-table-scans", familyName, tableName)
}
rows, err := stmt.QueryContext(ctx, key...)
switch {
case err == nil:
cols, err := schema.DBColumnMetaFromRows(rows)
if err != nil {
return nil, err
}
res := &Rows{rows: rows, cols: cols}
return res, nil
case err == sql.ErrNoRows:
return &Rows{}, nil
default:
return nil, err
}
}
// GetRowByKey fetches a row from the supplied table by the key parameter,
// filling the data into the out param.
//
// The out param may be one of the following types:
// - pointer to struct
// - map[string]interface{}
//
// The key parameter can support composite keys by passing a slice type.
func (reader *LDBReader) GetRowByKey(
ctx context.Context,
out interface{},
familyName string,
tableName string,
key ...interface{},
) (found bool, err error) {
ctx = discardContext()
start := time.Now()
defer func() {
globalstats.Observe("get_row_by_key", time.Now().Sub(start),
stats.T("family", familyName),
stats.T("table", tableName))
}()
reader.mu.RLock()
defer reader.mu.RUnlock()
famName, err := schema.NewFamilyName(familyName)
if err != nil {
return
}
tblName, err := schema.NewTableName(tableName)
if err != nil {
return
}
ldbTable := schema.LDBTableName(famName, tblName)
// NOTE: A persistent cache is kept on the reader to avoid needing
// to query for PKs on every call. Given that most API consumers will
// very likely use the global singleton reader, this means that we
// must assume that the cache will be shared across the whole process.
// The way that a PK would be changed on a table is that it would need
// to be dropped and re-created. In the mean time, this cache will
// go stale. The way that this is dealt with is to clear the cache if
// the statement encounters any execution errors.
pk, err := reader.getPrimaryKey(ctx, ldbTable) // assumes RLock held
if err != nil {
return
}
if pk.Zero() {
err = ErrTableHasNoPrimaryKey
return
}
if len(pk.Fields) != len(key) {
err = ErrNeedFullKey
return
}
// Stmt & PK cache are separate now to give the option to gracefully
// move back.
stmt, err := reader.getGetRowByKeyStmt(ctx, pk, ldbTable) // assumes RLock held
if err != nil {
return
}
err = convertKeyBeforeQuery(pk, key)
if err != nil {
return
}
rows, err := stmt.QueryContext(ctx, key...)
if err == sql.ErrNoRows {
found = false
err = nil
rows.Close()
return
}
if err != nil {
// See NOTE above about why this cache is getting cleared
reader.invalidatePKCache(ldbTable) // assumes RLock is held
err = errors.Wrap(err, "query target row error")
return
}
defer rows.Close()
cols, err := schema.DBColumnMetaFromRows(rows)
if err != nil {
return
}
scanFunc, err := scanfunc.New(out, cols)
if err != nil {
return
}
if !rows.Next() {
// found is already false by default
err = rows.Err()
return
}
found = true
err = scanFunc(rows)
if err != nil {
err = errors.Wrap(err, "target row scan error")
} else {
err = rows.Err()
}
return
}
func (reader *LDBReader) Close() error {
reader.mu.Lock()
defer reader.mu.Unlock()
if reader.cancelWatcher != nil {
reader.cancelWatcher()
}
return reader.closeDB()
}
// closeDB closes all reader-owned resources associated with the current DB.
// It should only be called when the caller is holding the reader.mu mutex.
func (reader *LDBReader) closeDB() error {
for _, stmt := range reader.getRowByKeyStmtCache {
if err := stmt.Close(); err != nil {
return err
}
}
reader.getRowByKeyStmtCache = map[string]*sql.Stmt{}
for _, stmt := range reader.getRowsByKeyPrefixStmtCache {
if err := stmt.Close(); err != nil {
return err
}
}
reader.getRowsByKeyPrefixStmtCache = map[prefixCacheKey]*sql.Stmt{}
if reader.Db != nil {
return reader.Db.Close()
}
return nil
}
// Ping checks if the LDB is available
func (reader *LDBReader) Ping(ctx context.Context) bool {
ctx = discardContext()
reader.mu.RLock()
defer reader.mu.RUnlock()
qs := "SELECT seq FROM " + ldb.LDBSeqTableName + " WHERE id = ?"
row := reader.Db.QueryRowContext(ctx, qs, ldb.LDBSeqTableID)
var seq sql.NullInt64
err := row.Scan(&seq)
if err != nil || !seq.Valid {
return false
}
return true
}
// ensure that a supplied key is converted appropriately with respect
// to the type of each PK column.
func convertKeyBeforeQuery(pk schema.PrimaryKey, key []interface{}) error {
for i, k := range key {
// sanity check on the length of the pk field type slice
if i >= len(pk.Types) {
return errors.New("insufficient key field type data")
}
pkt := pk.Types[i]
switch k := k.(type) {
case string:
switch pkt {
case schema.FTBinary, schema.FTByteString:
// convert the key from a string -> []byte so that the
// types match, otherwise it won't find the row.
key[i] = []byte(k)
}
}
}
return nil
}
func (reader *LDBReader) lock() {
reader.mu.Lock()
}
func (reader *LDBReader) unlock() {
reader.mu.Unlock()
}
// WARNING: assumes mutex is read locked
func (reader *LDBReader) invalidatePKCache(ldbTable string) {
if reader.pkCache == nil {
// Cache hasn't even been initialized yet, so invalidation would
// do nothing anyways.
return
}
reader.mu.RUnlock()
reader.mu.Lock()
delete(reader.pkCache, ldbTable)
reader.mu.Unlock()
reader.mu.RLock()
}
// WARNING: assumes mutex is read locked
func (reader *LDBReader) getPrimaryKey(ctx context.Context, ldbTable string) (schema.PrimaryKey, error) {
if reader.pkCache == nil {
reader.mu.RUnlock()
reader.mu.Lock()
// double check because there could be a race which would result
// in us wiping out the cache
if reader.pkCache == nil {
reader.pkCache = make(map[string]schema.PrimaryKey)
}
reader.mu.Unlock()
reader.mu.RLock()
}
if _, found := reader.pkCache[ldbTable]; !found {
const qs = "SELECT name,type FROM pragma_table_info(?) WHERE pk > 0 ORDER BY pk ASC"
rows, err := reader.Db.QueryContext(ctx, qs, ldbTable)
if err != nil {
return schema.PrimaryKeyZero, errors.Wrap(err, "query pragma_table_info error")
}
defer rows.Close()
rawFieldNames := []string{}
rawFieldTypes := []string{}
for rows.Next() {
var name string
var ftString string
err = rows.Scan(&name, &ftString)
if err != nil {
return schema.PrimaryKeyZero, errors.WithStack(err)
}
rawFieldNames = append(rawFieldNames, name)
rawFieldTypes = append(rawFieldTypes, ftString)
}
err = rows.Err()
if err != nil {
return schema.PrimaryKeyZero, errors.WithStack(err)
}
pk, err := schema.NewPKFromRawNamesAndTypes(rawFieldNames, rawFieldTypes)
if err != nil {
return schema.PrimaryKeyZero, err
}
if pk.Zero() {
// There's a potential that this is a missing table, so check
// that as well.
qs := sqlgen.SqlSprintf("SELECT * FROM $1 LIMIT 1", ldbTable)
_, err := reader.Db.ExecContext(ctx, qs)
if err != nil {
if strings.Index(err.Error(), "no such table:") == 0 {
return schema.PrimaryKeyZero, errors.New("Table not found")
}
return schema.PrimaryKeyZero, err
}
}
// Hold the lock for a tiny amount of time. That means there is
// a chance for races to cause multiple executions of this block
// of code that wastefully do the same thing. That's worth it
// to avoid per-key caching complexity and to keep the lock holding
// time very short.
reader.mu.RUnlock()
reader.mu.Lock()
reader.pkCache[ldbTable] = pk
reader.mu.Unlock()
reader.mu.RLock()
return pk, nil
}
return reader.pkCache[ldbTable], nil
}
func (reader *LDBReader) getRowsByKeyPrefixStmt(ctx context.Context, pk schema.PrimaryKey, ldbTable string, numKeys int) (*sql.Stmt, error) {
// assumes RLock is held
if reader.getRowsByKeyPrefixStmtCache == nil {
reader.mu.RUnlock()
reader.mu.Lock()
// double check because there could be a race which would result
// in us wiping out the cache
if reader.getRowsByKeyPrefixStmtCache == nil {
reader.getRowsByKeyPrefixStmtCache = make(map[prefixCacheKey]*sql.Stmt)
}
reader.mu.Unlock()
reader.mu.RLock()
}
pck := prefixCacheKey{ldbTableName: ldbTable, numKeys: numKeys}
stmt, found := reader.getRowsByKeyPrefixStmtCache[pck]
if found {
return stmt, nil
}
reader.mu.RUnlock()
defer reader.mu.RLock()
reader.mu.Lock()
defer reader.mu.Unlock()
qsTokens := []string{
"SELECT * FROM",
ldbTable,
}
if numKeys > 0 {
qsTokens = append(qsTokens, "WHERE")
for i := 0; i < numKeys; i++ {
pkField := pk.Fields[i]
if i > 0 {
qsTokens = append(qsTokens, "AND")
}
qsTokens = append(qsTokens,
pkField.Name,
"=",
"?")
}
}
qs := strings.Join(qsTokens, " ")
stmt, err := reader.Db.PrepareContext(ctx, qs)
if err == nil {
reader.getRowsByKeyPrefixStmtCache[pck] = stmt
}
return stmt, err
}
func (reader *LDBReader) getGetRowByKeyStmt(ctx context.Context, pk schema.PrimaryKey, ldbTable string) (*sql.Stmt, error) {
// assumes RLock is held
if reader.getRowByKeyStmtCache == nil {
reader.mu.RUnlock()
reader.mu.Lock()
// double check because there could be a race which would result
// in us wiping out the cache
if reader.getRowByKeyStmtCache == nil {
reader.getRowByKeyStmtCache = make(map[string]*sql.Stmt)
}
reader.mu.Unlock()
reader.mu.RLock()
}
stmt, found := reader.getRowByKeyStmtCache[ldbTable]
if found {
return stmt, nil
}
reader.mu.RUnlock()
defer reader.mu.RLock()
reader.mu.Lock()
defer reader.mu.Unlock()
qsTokens := []string{
"SELECT * FROM",
ldbTable,
"WHERE",
}
for i, pkField := range pk.Fields {
if i > 0 {
qsTokens = append(qsTokens, "AND")
}
qsTokens = append(qsTokens,
pkField.Name,
"=",
"?")
}
qs := strings.Join(qsTokens, " ")
stmt, err := reader.Db.PrepareContext(ctx, qs)
if err == nil {
reader.getRowByKeyStmtCache[ldbTable] = stmt
}
return stmt, err
}
func (reader *LDBReader) watchForLDBs(ctx context.Context, dirPath string, last int64) {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
fsLast, err := lookupLastLDBSync(dirPath)
if err != nil {
events.Log("failed checking for last LDB sync: %{error}+v", err)
errs.Incr("check-last-ldb-sync")
continue
}
// Only swap LDBs if this LDB is newer than our newest LDB.
if fsLast <= last {
continue
}
events.Log("found new LDB (%d > %d), switching...", fsLast, last)
last = fsLast
err = reader.switchLDB(dirPath, last)
if err != nil {
events.Log("failed switching to new LDB: %{error}+v", err)
errs.Incr("switch-ldb")
}
}
}
}
func (reader *LDBReader) switchLDB(dirPath string, timestamp int64) error {
fullPath := filepath.Join(dirPath, fmt.Sprintf("%013d", timestamp), ldb.DefaultLDBFilename)
db, err := newLDB(fullPath)
if err != nil {
return errors.Wrap(err, "new ldb")
}
reader.mu.Lock()
defer reader.mu.Unlock()
if err = reader.closeDB(); err != nil {
return errors.Wrap(err, "closing db")
}
reader.Db = db
return nil
}
func lookupLastLDBSync(dirPath string) (int64, error) {
// Loop through the files in the `dirPath` and look for the ldb.db with the
// highest associated timestamp. Return that.
var lastSync int64
err := filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
// Bail if we hit any errors while visiting files/directories:
if info == nil || err != nil {
return err
}
// Ignore directories:
if info.IsDir() {
return nil
}
// We only care about the timestamps associated with `<path>/<timestamp>/ldb.db` files.
// Therefore, we ignore all other files (ldb.db.wal, etc).
if !strings.HasSuffix(filePath, ldb.DefaultLDBFilename) {
return nil
}
// Ignore `<path>/ldb.db`, which is the standard path for LDBs. We only care
// about versioned LDBs, which are those in a timestamped directory.
if strings.HasPrefix(filePath, filepath.Join(dirPath, ldb.DefaultLDBFilename)) {
return nil
}
// Omit the root path from the file path:
// dirPath + ["<timestamp>", ldb.DefaultLDBFilename]
localPath, err := filepath.Rel(dirPath, filePath)
if err != nil {
return errors.Wrapf(err, "base path (%s)", filePath)
}
fields := strings.Split(localPath, "/")
if len(fields) != 2 || fields[1] != ldb.DefaultLDBFilename {
events.Log("ignoring unexpected file in LDB path (%+v)", fields)
errs.Incr("unexpected-local-file")
return nil
}
timestamp, err := strconv.ParseInt(fields[0], 10, 64)
if err != nil {
events.Log("ignoring file with invalid timestamp in LDB path (%+v)", fields)
errs.Incr("invalid-timestamp-local-file")
return nil
}
if timestamp > lastSync {
lastSync = timestamp
}
return nil
})
if err != nil {
return 0, errors.Wrap(err, "filepath walk")
}
return lastSync, nil
}
// discardContext returns context.Background(). the exported reader API uses the returned
// value instead. This is done because the underlying sqlite CGO code that
// the reader API ultimately calls does not handle interruptions optimally. Additionally
// because the calls read from disk instead of making network calls, context cancellation is
// arguably less important to begin with.
func discardContext() context.Context {
return context.Background()
}