-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathreplica_internal_test.go
More file actions
362 lines (309 loc) · 10.6 KB
/
replica_internal_test.go
File metadata and controls
362 lines (309 loc) · 10.6 KB
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
package litestream
import (
"bytes"
"context"
"database/sql"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/superfly/ltx"
_ "modernc.org/sqlite"
)
func TestReplica_ApplyNewLTXFiles_FillGapWithOverlappingCompactedFile(t *testing.T) {
const pageSize = 4096
compactedInfo := <x.FileInfo{Level: 1, MinTXID: 100, MaxTXID: 200}
l0Info := <x.FileInfo{Level: 0, MinTXID: 201, MaxTXID: 201}
fixtures := map[string][]byte{
ltxFixtureKey(compactedInfo.Level, compactedInfo.MinTXID, compactedInfo.MaxTXID): mustBuildIncrementalLTX(t, compactedInfo.MinTXID, compactedInfo.MaxTXID, pageSize, 1, 0xA1),
ltxFixtureKey(l0Info.Level, l0Info.MinTXID, l0Info.MaxTXID): mustBuildIncrementalLTX(t, l0Info.MinTXID, l0Info.MaxTXID, pageSize, 1, 0xB2),
}
client := &followTestReplicaClient{}
client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) {
var all []*ltx.FileInfo
switch level {
case 0:
all = []*ltx.FileInfo{l0Info}
case 1:
all = []*ltx.FileInfo{compactedInfo}
default:
all = nil
}
infos := make([]*ltx.FileInfo, 0, len(all))
for _, info := range all {
if info.MinTXID >= seek {
infos = append(infos, info)
}
}
return ltx.NewFileInfoSliceIterator(infos), nil
}
client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) {
key := ltxFixtureKey(level, minTXID, maxTXID)
data, ok := fixtures[key]
if !ok {
return nil, os.ErrNotExist
}
return io.NopCloser(bytes.NewReader(data)), nil
}
r := NewReplicaWithClient(nil, client)
f := mustCreateWritableDBFile(t)
defer func() { _ = f.Close() }()
got, err := r.applyNewLTXFiles(context.Background(), f, 150, pageSize)
if err != nil {
t.Fatalf("apply new ltx files: %v", err)
}
if got != 201 {
t.Fatalf("txid=%s, want %s", got, ltx.TXID(201))
}
}
func TestReplica_ApplyNewLTXFiles_LevelZeroEmptyFallsBackToCompaction(t *testing.T) {
const pageSize = 4096
compactedInfo := <x.FileInfo{Level: 1, MinTXID: 11, MaxTXID: 12}
fixtures := map[string][]byte{
ltxFixtureKey(compactedInfo.Level, compactedInfo.MinTXID, compactedInfo.MaxTXID): mustBuildIncrementalLTX(t, compactedInfo.MinTXID, compactedInfo.MaxTXID, pageSize, 1, 0xC3),
}
client := &followTestReplicaClient{}
client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) {
switch level {
case 0:
return ltx.NewFileInfoSliceIterator(nil), nil
case 1:
if compactedInfo.MinTXID < seek {
return ltx.NewFileInfoSliceIterator(nil), nil
}
return ltx.NewFileInfoSliceIterator([]*ltx.FileInfo{compactedInfo}), nil
default:
return ltx.NewFileInfoSliceIterator(nil), nil
}
}
client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) {
key := ltxFixtureKey(level, minTXID, maxTXID)
data, ok := fixtures[key]
if !ok {
return nil, os.ErrNotExist
}
return io.NopCloser(bytes.NewReader(data)), nil
}
r := NewReplicaWithClient(nil, client)
f := mustCreateWritableDBFile(t)
defer func() { _ = f.Close() }()
got, err := r.applyNewLTXFiles(context.Background(), f, 10, pageSize)
if err != nil {
t.Fatalf("apply new ltx files: %v", err)
}
if got != 12 {
t.Fatalf("txid=%s, want %s", got, ltx.TXID(12))
}
}
func TestReplica_ApplyNewLTXFiles_IteratorCloseError(t *testing.T) {
client := &followTestReplicaClient{}
client.LTXFilesFunc = func(_ context.Context, level int, seek ltx.TXID, _ bool) (ltx.FileIterator, error) {
if level == 0 {
return &errorFileIterator{closeErr: fmt.Errorf("level 0 listing failed")}, nil
}
return ltx.NewFileInfoSliceIterator(nil), nil
}
client.OpenLTXFileFunc = func(_ context.Context, _ int, _, _ ltx.TXID, _, _ int64) (io.ReadCloser, error) {
return nil, fmt.Errorf("unexpected open")
}
r := NewReplicaWithClient(nil, client)
f := mustCreateWritableDBFile(t)
defer func() { _ = f.Close() }()
_, err := r.applyNewLTXFiles(context.Background(), f, 10, 4096)
if err == nil {
t.Fatal("expected error")
}
if got, want := err.Error(), "level 0 listing failed"; !bytes.Contains([]byte(got), []byte(want)) {
t.Fatalf("error=%q, want substring %q", got, want)
}
}
func TestReplica_ApplyLTXFile_VerifiesChecksumOnClose(t *testing.T) {
const pageSize = 4096
info := <x.FileInfo{Level: 0, MinTXID: 20, MaxTXID: 20}
data := mustBuildIncrementalLTX(t, info.MinTXID, info.MaxTXID, pageSize, 1, 0xD4)
data[len(data)-1] ^= 0xFF
client := &followTestReplicaClient{}
client.OpenLTXFileFunc = func(_ context.Context, level int, minTXID, maxTXID ltx.TXID, _, _ int64) (io.ReadCloser, error) {
if level != info.Level || minTXID != info.MinTXID || maxTXID != info.MaxTXID {
return nil, os.ErrNotExist
}
return io.NopCloser(bytes.NewReader(data)), nil
}
r := NewReplicaWithClient(nil, client)
f := mustCreateWritableDBFile(t)
defer func() { _ = f.Close() }()
err := r.applyLTXFile(context.Background(), f, info, pageSize)
if err == nil {
t.Fatal("expected checksum validation error")
}
}
type errorFileIterator struct {
closeErr error
}
type followTestReplicaClient struct {
LTXFilesFunc func(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error)
OpenLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error)
WriteLTXFileFunc func(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error)
DeleteLTXFilesFunc func(ctx context.Context, a []*ltx.FileInfo) error
DeleteAllFunc func(ctx context.Context) error
}
func (*followTestReplicaClient) Type() string { return "test" }
func (*followTestReplicaClient) Init(context.Context) error { return nil }
func (c *followTestReplicaClient) LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error) {
if c.LTXFilesFunc != nil {
return c.LTXFilesFunc(ctx, level, seek, useMetadata)
}
return ltx.NewFileInfoSliceIterator(nil), nil
}
func (c *followTestReplicaClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) {
if c.OpenLTXFileFunc != nil {
return c.OpenLTXFileFunc(ctx, level, minTXID, maxTXID, offset, size)
}
return nil, os.ErrNotExist
}
func (c *followTestReplicaClient) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) {
if c.WriteLTXFileFunc != nil {
return c.WriteLTXFileFunc(ctx, level, minTXID, maxTXID, r)
}
return nil, fmt.Errorf("not implemented")
}
func (c *followTestReplicaClient) DeleteLTXFiles(ctx context.Context, a []*ltx.FileInfo) error {
if c.DeleteLTXFilesFunc != nil {
return c.DeleteLTXFilesFunc(ctx, a)
}
return nil
}
func (c *followTestReplicaClient) DeleteAll(ctx context.Context) error {
if c.DeleteAllFunc != nil {
return c.DeleteAllFunc(ctx)
}
return nil
}
func (itr *errorFileIterator) Close() error {
return itr.closeErr
}
func (itr *errorFileIterator) Next() bool {
return false
}
func (itr *errorFileIterator) Err() error {
return itr.closeErr
}
func (itr *errorFileIterator) Item() *ltx.FileInfo {
return nil
}
func mustBuildIncrementalLTX(tb testing.TB, minTXID, maxTXID ltx.TXID, pageSize, pgno uint32, fill byte) []byte {
tb.Helper()
var buf bytes.Buffer
enc, err := ltx.NewEncoder(&buf)
if err != nil {
tb.Fatal(err)
}
hdr := ltx.Header{
Version: ltx.Version,
Flags: ltx.HeaderFlagNoChecksum,
PageSize: pageSize,
Commit: pgno,
MinTXID: minTXID,
MaxTXID: maxTXID,
Timestamp: time.Now().UnixMilli(),
}
if err := enc.EncodeHeader(hdr); err != nil {
tb.Fatal(err)
}
page := bytes.Repeat([]byte{fill}, int(pageSize))
if err := enc.EncodePage(ltx.PageHeader{Pgno: pgno}, page); err != nil {
tb.Fatal(err)
}
if err := enc.Close(); err != nil {
tb.Fatal(err)
}
return buf.Bytes()
}
func mustCreateWritableDBFile(tb testing.TB) *os.File {
tb.Helper()
path := filepath.Join(tb.TempDir(), "follower.db")
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o600)
if err != nil {
tb.Fatal(err)
}
if err := f.Truncate(128 * 1024); err != nil {
_ = f.Close()
tb.Fatal(err)
}
return f
}
func ltxFixtureKey(level int, minTXID, maxTXID ltx.TXID) string {
return fmt.Sprintf("%d:%s:%s", level, minTXID, maxTXID)
}
func mustCreateValidSQLiteDB(tb testing.TB) string {
tb.Helper()
dbPath := filepath.Join(tb.TempDir(), "test.db")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
tb.Fatal(err)
}
defer func() { _ = db.Close() }()
if _, err := db.Exec("CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)"); err != nil {
tb.Fatal(err)
}
if _, err := db.Exec("INSERT INTO t (name) VALUES ('a'), ('b'), ('c')"); err != nil {
tb.Fatal(err)
}
if _, err := db.Exec("CREATE INDEX idx_t_name ON t(name)"); err != nil {
tb.Fatal(err)
}
return dbPath
}
func TestCheckIntegrity_Quick_ValidDB(t *testing.T) {
dbPath := mustCreateValidSQLiteDB(t)
if err := checkIntegrity(dbPath, IntegrityCheckQuick); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestCheckIntegrity_Full_ValidDB(t *testing.T) {
dbPath := mustCreateValidSQLiteDB(t)
if err := checkIntegrity(dbPath, IntegrityCheckFull); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestCheckIntegrity_None_Skips(t *testing.T) {
if err := checkIntegrity("/nonexistent/path.db", IntegrityCheckNone); err != nil {
t.Fatalf("expected nil for IntegrityCheckNone, got: %v", err)
}
}
func TestCheckIntegrity_CorruptDB(t *testing.T) {
dbPath := mustCreateValidSQLiteDB(t)
// Remove any WAL/SHM files so we have a clean single-file database.
_ = os.Remove(dbPath + "-wal")
_ = os.Remove(dbPath + "-shm")
// Read the page size from the database header (bytes 16-17, big-endian).
f, err := os.OpenFile(dbPath, os.O_RDWR, 0o600)
if err != nil {
t.Fatal(err)
}
// Corrupt page 2 onwards. Page 1 is the header/schema page. Corrupting
// pages that contain table/index data triggers integrity check failures.
// We overwrite from byte offset 4096 (start of page 2 for 4096-byte pages,
// which is the default) with garbage data.
info, err := f.Stat()
if err != nil {
_ = f.Close()
t.Fatal(err)
}
// Overwrite everything after the first page with garbage to ensure corruption.
pageSize := int64(4096)
if info.Size() > pageSize {
garbage := bytes.Repeat([]byte{0xDE}, int(info.Size()-pageSize))
if _, err := f.WriteAt(garbage, pageSize); err != nil {
_ = f.Close()
t.Fatal(err)
}
}
_ = f.Close()
err = checkIntegrity(dbPath, IntegrityCheckFull)
if err == nil {
t.Fatal("expected integrity check to fail on corrupt database")
}
}