-
Notifications
You must be signed in to change notification settings - Fork 17
/
ldb_reader_test.go
600 lines (536 loc) · 16.2 KB
/
ldb_reader_test.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
package ctlstore
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/segmentio/ctlstore/pkg/ldb"
"github.com/segmentio/ctlstore/pkg/ldbwriter"
"github.com/segmentio/ctlstore/pkg/schema"
"github.com/segmentio/ctlstore/pkg/utils"
"github.com/stretchr/testify/require"
)
type testKVStruct struct {
Key string `ctlstore:"key"`
Val string `ctlstore:"value"`
}
func benchmarkGetRowByKey(b *testing.B, target interface{}) {
ctx := context.Background()
db, teardown := ldb.LDBForTest(b)
defer teardown()
_, err := db.Exec(initSQLForReadKeyByRow)
if err != nil {
b.Errorf("Unexpected error encountered when bootstrapping test database: %v", err)
}
reader := LDBReader{Db: db}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
found, err := reader.GetRowByKey(
ctx,
target,
"foo",
"bar",
utils.InterfaceSlice([]string{"foo"})...,
)
if !found {
b.Fatal("should have been found")
}
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkStructGetRowByKey(b *testing.B) {
benchmarkGetRowByKey(b, &testKVStruct{})
}
func BenchmarkMapGetRowByKey(b *testing.B) {
benchmarkGetRowByKey(b, map[string]interface{}{})
}
func TestGetLedgerLatency(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
db, teardown := ldb.LDBForTest(t)
defer teardown()
reader := &LDBReader{Db: db}
latency, err := reader.GetLedgerLatency(ctx)
require.Equal(t, ErrNoLedgerUpdates, err)
writer := ldbwriter.SqlLdbWriter{Db: db}
err = writer.ApplyDMLStatement(ctx, schema.NewTestDMLStatement("create table foo (var VARCHAR)"))
require.NoError(t, err)
require.NoError(t, err)
latency, err = reader.GetLedgerLatency(ctx)
require.NoError(t, err)
require.True(t, latency < time.Second, "weird latency: %v", latency)
}
func TestLDBMustExistForReader(t *testing.T) {
oldGlobal := globalLDBDirPath
defer func() { globalLDBDirPath = oldGlobal }()
var err error
globalLDBDirPath, err = ioutil.TempDir("", "ldb_must_exist")
require.NoError(t, err)
r, err := Reader()
require.EqualError(t, err, fmt.Sprintf("no LDB found at %s/%s", globalLDBDirPath, ldb.DefaultLDBFilename))
require.Nil(t, r)
r, err = ReaderForPath("/does/not/exist/ldb.db")
require.EqualError(t, err, "no LDB found at /does/not/exist/ldb.db")
require.Nil(t, r)
}
func TestGetRowsByKeyPrefix(t *testing.T) {
type mrStruct struct {
K1 string `ctlstore:"k1"`
K2 string `ctlstore:"k2"`
Val int64 `ctlstore:"val"`
}
for _, test := range []struct {
desc string
family string
table string
key interface{}
targetFunc func() interface{}
err error
expected []interface{}
}{
{
desc: "single key [map]",
family: "foo",
table: "multirow",
key: []interface{}{"a"},
targetFunc: func() interface{} { return map[string]interface{}{} },
expected: []interface{}{
map[string]interface{}{"k1": "a", "k2": "A", "val": int64(42)},
map[string]interface{}{"k1": "a", "k2": "B", "val": int64(43)},
},
err: nil,
},
{
desc: "single key [struct]",
family: "foo",
table: "multirow",
key: []interface{}{"a"},
targetFunc: func() interface{} { return &mrStruct{} },
expected: []interface{}{
&mrStruct{K1: "a", K2: "A", Val: 42},
&mrStruct{K1: "a", K2: "B", Val: 43},
},
err: nil,
},
{
desc: "all keys [map]",
family: "foo",
table: "multirow",
key: []interface{}{"a", "A"},
targetFunc: func() interface{} { return map[string]interface{}{} },
expected: []interface{}{
map[string]interface{}{"k1": "a", "k2": "A", "val": int64(42)},
},
err: nil,
},
{
desc: "all keys [struct]",
family: "foo",
table: "multirow",
key: []interface{}{"a", "A"},
targetFunc: func() interface{} { return &mrStruct{} },
expected: []interface{}{
&mrStruct{K1: "a", K2: "A", Val: 42},
},
err: nil,
},
{
desc: "no keys [map]",
family: "foo",
table: "multirow",
key: []interface{}{},
targetFunc: func() interface{} { return map[string]interface{}{} },
expected: []interface{}{
map[string]interface{}{"k1": "a", "k2": "A", "val": int64(42)},
map[string]interface{}{"k1": "a", "k2": "B", "val": int64(43)},
map[string]interface{}{"k1": "b", "k2": "B", "val": int64(44)},
},
err: nil,
},
{
desc: "no keys [struct]",
family: "foo",
table: "multirow",
key: []interface{}{},
targetFunc: func() interface{} { return &mrStruct{} },
expected: []interface{}{
&mrStruct{K1: "a", K2: "A", Val: 42},
&mrStruct{K1: "a", K2: "B", Val: 43},
&mrStruct{K1: "b", K2: "B", Val: 44},
},
err: nil,
},
{
desc: "too many keys supplied",
family: "foo",
table: "multirow",
key: []interface{}{"a", "b", "c", "d"},
targetFunc: func() interface{} { return map[string]interface{}{} },
expected: nil,
err: errors.New("too many keys supplied for table's primary key"),
},
{
desc: "no rows found",
family: "foo",
table: "multirow",
key: []interface{}{"lol nothing here"},
targetFunc: func() interface{} { return map[string]interface{}{} },
expected: nil,
err: nil,
},
} {
t.Run(test.desc, func(t *testing.T) {
ctx := context.Background()
db, teardown := ldb.LDBForTest(t)
defer teardown()
_, err := db.Exec(initSQLForReadKeyByRow)
if err != nil {
t.Errorf("Unexpected error encountered when bootstrapping test database: %v", err)
}
reader := LDBReader{Db: db}
rows, err := reader.GetRowsByKeyPrefix(
ctx,
test.family,
test.table,
utils.InterfaceSlice(test.key)...,
)
switch {
case err != nil && test.err == nil:
t.Fatal(err)
case err == nil && test.err != nil:
t.Fatal("Expected err: " + test.err.Error())
case err != nil:
require.EqualValues(t, err.Error(), test.err.Error())
}
if err != nil {
// don't bother with the rest of the test if we hit an error
return
}
defer func() {
err = rows.Close()
require.NoError(t, err)
}()
var res []interface{}
for rows.Next() {
target := test.targetFunc()
err := rows.Scan(target)
require.NoError(t, err)
res = append(res, target)
}
require.EqualValues(t, test.expected, res)
})
}
}
func TestGetRowByKey(t *testing.T) {
suite := []struct {
desc string
familyName string
tableName string
key interface{}
gotOut interface{}
expectOut interface{}
expectFound bool
expectErr error
}{
{
desc: "map single-key found",
familyName: "foo",
tableName: "bar",
key: []string{"foo"},
gotOut: map[string]interface{}{},
expectOut: map[string]interface{}{"key": "foo", "value": "bar"},
expectFound: true,
expectErr: nil,
},
{
desc: "struct single-key found",
familyName: "foo",
tableName: "bar",
key: []string{"foo"},
gotOut: &testKVStruct{},
expectOut: &testKVStruct{"foo", "bar"},
expectFound: true,
expectErr: nil,
},
{
desc: "map single-key not found",
familyName: "foo",
tableName: "bar",
key: []string{"non-existant"},
gotOut: map[string]interface{}{},
expectOut: map[string]interface{}{},
expectFound: false,
expectErr: nil,
},
{
desc: "struct single-key not found",
familyName: "foo",
tableName: "bar",
key: []string{"non-existant"},
gotOut: &testKVStruct{},
expectOut: &testKVStruct{},
expectFound: false,
expectErr: nil,
},
{
desc: "map composite key found",
familyName: "foo",
tableName: "composite",
key: []string{"foo", "bar"},
gotOut: map[string]interface{}{},
expectOut: map[string]interface{}{"key": "foo", "value": "bar"},
expectFound: true,
expectErr: nil,
},
{
desc: "struct composite key found",
familyName: "foo",
tableName: "composite",
key: []string{"foo", "bar"},
gotOut: &testKVStruct{},
expectOut: &testKVStruct{"foo", "bar"},
expectFound: true,
expectErr: nil,
},
{
desc: "struct varbinary key query by binary",
familyName: "foo",
tableName: "varbinarykey",
key: []interface{}{[]byte("beef")}, // use []byte key
gotOut: &testKVStruct{},
expectOut: &testKVStruct{"beef", "moo"},
expectFound: true,
expectErr: nil,
},
{
desc: "map varbinary key query by binary",
familyName: "foo",
tableName: "varbinarykey",
key: []interface{}{[]byte("beef")}, // use []byte key
gotOut: map[string]interface{}{},
expectOut: map[string]interface{}{"key": []byte("beef"), "value": "moo"},
expectFound: true,
expectErr: nil,
},
{
desc: "struct varbinary key query by string",
familyName: "foo",
tableName: "varbinarykey",
key: []interface{}{"beef"}, // use string key
gotOut: &testKVStruct{},
expectOut: &testKVStruct{"beef", "moo"},
expectFound: true,
expectErr: nil,
},
{
desc: "map varbinary key query by string",
familyName: "foo",
tableName: "varbinarykey",
key: []interface{}{"beef"}, // use string key
gotOut: map[string]interface{}{},
expectOut: map[string]interface{}{"key": []byte("beef"), "value": "moo"},
expectFound: true,
expectErr: nil,
},
}
for _, testCase := range suite {
t.Run(testCase.desc, func(t *testing.T) {
ctx := context.Background()
db, teardown := ldb.LDBForTest(t)
defer teardown()
_, err := db.Exec(initSQLForReadKeyByRow)
if err != nil {
t.Errorf("Unexpected error encountered when bootstrapping test database: %v", err)
}
reader := LDBReader{Db: db}
gotFound, gotErr := reader.GetRowByKey(
ctx,
testCase.gotOut,
testCase.familyName,
testCase.tableName,
utils.InterfaceSlice(testCase.key)...,
)
require.EqualValues(t, testCase.expectErr, gotErr)
require.EqualValues(t, testCase.expectFound, gotFound)
if diff := cmp.Diff(testCase.gotOut, testCase.expectOut); diff != "" {
t.Errorf("Out mismatch\n%s", diff)
}
})
}
}
func TestLDBReaderEmptyFileHandling(t *testing.T) {
ctx := context.Background()
dbPath, teardown := ldb.NewLDBTmpPath(t)
defer teardown()
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Couldn't open SQLite db, error %v", err)
}
reader := LDBReader{Db: db}
var foo struct{}
gotFound, gotErr := reader.GetRowByKey(ctx, &foo, "foo", "bar", "foo")
if want, got := false, gotFound; want != got {
t.Errorf("Expected %v, got %v", want, got)
}
if want, got := "Table not found", gotErr; gotErr == nil || want != got.Error() {
t.Errorf("Expected %v, got %v", want, got)
}
db2, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Couldn't open SQLite db, error %v", err)
}
err = ldb.EnsureLdbInitialized(context.Background(), db2)
if err != nil {
t.Fatalf("Couldn't initialize SQLite db, error %v", err)
}
initSQL := `
CREATE TABLE foo___bar (
key VARCHAR PRIMARY KEY,
value VARCHAR
);
INSERT INTO foo___bar VALUES('foo', 'bar');
`
_, err = db2.Exec(initSQL)
if err != nil {
t.Errorf("Unexpected error encountered when bootstrapping test database: %v", err)
}
gotFound, gotErr = reader.GetRowByKey(ctx, &foo, "foo", "bar", "foo")
if want, got := true, gotFound; want != got {
t.Errorf("Expected %v, got %v", want, got)
}
if want, got := "", gotErr; gotErr != nil && want != got.Error() {
t.Errorf("Expected %v, got %v", want, got)
}
}
func TestLDBReaderPing(t *testing.T) {
ctx := context.Background()
dbPath, teardown := ldb.NewLDBTmpPath(t)
defer teardown()
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Couldn't open SQLite db, error %v", err)
}
reader := LDBReader{Db: db}
if want, got := false, reader.Ping(ctx); want != got {
t.Errorf("Expected %v, got %v", want, got)
}
err = ldb.EnsureLdbInitialized(context.Background(), db)
_, err = db.Exec(
fmt.Sprintf("REPLACE INTO %s (id, seq) VALUES(?, ?)", ldb.LDBSeqTableName),
ldb.LDBSeqTableID, 1)
if err != nil {
t.Fatalf("Unexpected error: %+v", err)
}
if want, got := true, reader.Ping(ctx); want != got {
t.Errorf("Expected %v, got %v", want, got)
}
}
func TestLDBVersioning(t *testing.T) {
require := require.New(t)
globalLDBReadOnly = false
// Initialize a new temporary directory, which we'll use
// as the path for storing timestamp-versioned LDBs.
path, err := ioutil.TempDir("", "ldb-versioning-tmp-path")
require.NoError(err)
defer os.RemoveAll(path)
// Initialize an LDBVersioned reader pointing at this directory
// which currently does not contain any LDBs. It should error.
reader, err := newVersionedLDBReader(path)
require.Error(err, "expected new reader to fail when directory contains no LDBs")
require.Nil(reader)
// So now we initialize a new LDB in this path, which should
// allow us to create a new LDB without error.
generateVersionedLDB(t, path, int64(1500000000000))
// Create a new reader, which should not fail this time, since an LDB exists
// in the associated folder.
reader, err = newVersionedLDBReader(path)
require.NoError(err)
defer reader.Close()
require.True(reader.Ping(context.Background()))
// Verify that this reader is consuming from the LDB we created above, based on
// what timestamp was written into the LDB on creation.
require.Equal(int64(1500000000000), getLDBTimestamp(t, reader))
// Now, create a newer LDB with a higher timestamp. The reader should pick this
// up and return the new timestamp.
generateVersionedLDB(t, path, int64(1500000000001))
time.Sleep(2 * time.Second) // Wait for the reader to pick up the new LDB.
require.Equal(int64(1500000000001), getLDBTimestamp(t, reader))
// As a safeguard, should a new LDB be generated with a _lower_ timestamp
// make sure that the reader does not consume it.
generateVersionedLDB(t, path, int64(1400000000000))
time.Sleep(2 * time.Second) // Wait for the reader to pick up the new LDB.
require.Equal(int64(1500000000001), getLDBTimestamp(t, reader))
}
func generateVersionedLDB(t *testing.T, path string, timestamp int64) {
require := require.New(t)
dirPath := filepath.Join(path, fmt.Sprintf("%013d", timestamp))
require.NoError(os.Mkdir(dirPath, 0755))
dbPath := filepath.Join(dirPath, "ldb.db")
db, err := sql.Open("sqlite3", dbPath)
require.NoError(err)
// Initialize the ctlstore LDB tables:
require.NoError(ldb.EnsureLdbInitialized(context.Background(), db))
_, err = db.Exec(
fmt.Sprintf("REPLACE INTO %s (id, seq) VALUES(?, ?)", ldb.LDBSeqTableName),
ldb.LDBSeqTableID, 1)
require.NoError(err)
// Initialize a fake ctlstore table which will store the timestamp
// of this LDB.
_, err = db.Exec(`
CREATE TABLE versioning___curr_ts (
key VARCHAR PRIMARY KEY,
timestamp BIGINT
);
INSERT INTO versioning___curr_ts VALUES('now', ?);
`, timestamp)
require.NoError(err)
}
func getLDBTimestamp(t *testing.T, reader *LDBReader) int64 {
require := require.New(t)
var out struct {
timestamp int64 `ctlstore:"timestamp"`
}
ok, err := reader.GetRowByKey(context.Background(), &out, "versioning", "curr_ts", "now")
require.True(ok)
require.NoError(err)
return out.timestamp
}
// sql that is used to initialize the tests and benchmarks that
// exercise ReadKeyByRow()
const initSQLForReadKeyByRow = `
CREATE TABLE foo___bar (
key VARCHAR PRIMARY KEY,
value VARCHAR
);
INSERT INTO foo___bar VALUES('foo', 'bar');
CREATE TABLE foo___composite (
key VARCHAR,
value VARCHAR,
PRIMARY KEY(key, value)
);
INSERT INTO foo___composite VALUES('foo', 'bar');
CREATE TABLE foo___varbinarykey (
key VARBINARY,
value VARCHAR,
PRIMARY KEY(key)
);
INSERT INTO foo___varbinarykey (key, value) VALUES (x'62656566', "moo"); /* key is "beef" */
CREATE TABLE foo___multirow (
k1 varchar,
k2 varchar,
val int ,
primary key (k1, k2)
);
INSERT INTO foo___multirow (k1,k2,val) VALUES ('a', 'A', 42);
INSERT INTO foo___multirow (k1,k2,val) VALUES ('a', 'B', 43);
INSERT INTO foo___multirow (k1,k2,val) VALUES ('b', 'B', 44);
`