forked from benbjohnson/litestream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompactor_test.go
More file actions
470 lines (395 loc) · 12.7 KB
/
compactor_test.go
File metadata and controls
470 lines (395 loc) · 12.7 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
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
package litestream_test
import (
"bytes"
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/superfly/ltx"
"github.com/benbjohnson/litestream"
"github.com/benbjohnson/litestream/file"
)
func TestCompactor_Compact(t *testing.T) {
t.Run("L0ToL1", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create test L0 files
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
info, err := compactor.Compact(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if info.Level != 1 {
t.Errorf("Level=%d, want 1", info.Level)
}
if info.MinTXID != 1 || info.MaxTXID != 2 {
t.Errorf("TXID range=%d-%d, want 1-2", info.MinTXID, info.MaxTXID)
}
})
t.Run("NoFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
_, err := compactor.Compact(context.Background(), 1)
if err != litestream.ErrNoCompaction {
t.Errorf("err=%v, want ErrNoCompaction", err)
}
})
t.Run("L1ToL2", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create L0 files
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
// Compact to L1
_, err := compactor.Compact(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
// Create more L0 files
createTestLTXFile(t, client, 0, 3, 3)
// Compact to L1 again (should only include TXID 3)
info, err := compactor.Compact(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if info.MinTXID != 3 || info.MaxTXID != 3 {
t.Errorf("TXID range=%d-%d, want 3-3", info.MinTXID, info.MaxTXID)
}
// Now compact L1 to L2 (should include all from 1-3)
info, err = compactor.Compact(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
if info.Level != 2 {
t.Errorf("Level=%d, want 2", info.Level)
}
if info.MinTXID != 1 || info.MaxTXID != 3 {
t.Errorf("TXID range=%d-%d, want 1-3", info.MinTXID, info.MaxTXID)
}
})
}
func TestCompactor_MaxLTXFileInfo(t *testing.T) {
t.Run("WithFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
createTestLTXFile(t, client, 0, 3, 5)
info, err := compactor.MaxLTXFileInfo(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 5 {
t.Errorf("MaxTXID=%d, want 5", info.MaxTXID)
}
})
t.Run("NoFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
info, err := compactor.MaxLTXFileInfo(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 0 {
t.Errorf("MaxTXID=%d, want 0", info.MaxTXID)
}
})
t.Run("WithCache", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Use callbacks for caching
cache := make(map[int]*ltx.FileInfo)
compactor.CacheGetter = func(level int) (*ltx.FileInfo, bool) {
info, ok := cache[level]
return info, ok
}
compactor.CacheSetter = func(level int, info *ltx.FileInfo) {
cache[level] = info
}
createTestLTXFile(t, client, 0, 1, 3)
// First call should populate cache
info, err := compactor.MaxLTXFileInfo(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 3 {
t.Errorf("MaxTXID=%d, want 3", info.MaxTXID)
}
// Second call should use cache
info, err = compactor.MaxLTXFileInfo(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 3 {
t.Errorf("MaxTXID=%d, want 3 (from cache)", info.MaxTXID)
}
})
}
func TestCompactor_EnforceRetentionByTXID(t *testing.T) {
t.Run("DeletesOldFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create files at L1
createTestLTXFile(t, client, 1, 1, 2)
createTestLTXFile(t, client, 1, 3, 5)
createTestLTXFile(t, client, 1, 6, 10)
// Enforce retention - delete files below TXID 5
err := compactor.EnforceRetentionByTXID(context.Background(), 1, 5)
if err != nil {
t.Fatal(err)
}
// Verify only the first file was deleted
info, err := compactor.MaxLTXFileInfo(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 10 {
t.Errorf("MaxTXID=%d, want 10", info.MaxTXID)
}
// Check that files starting from TXID 3 are still present
itr, err := client.LTXFiles(context.Background(), 1, 0, false)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
var count int
for itr.Next() {
count++
}
if count != 2 {
t.Errorf("file count=%d, want 2", count)
}
})
t.Run("KeepsLastFile", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create single file
createTestLTXFile(t, client, 1, 1, 2)
// Try to delete it - should keep at least one
err := compactor.EnforceRetentionByTXID(context.Background(), 1, 100)
if err != nil {
t.Fatal(err)
}
// Verify file still exists
info, err := compactor.MaxLTXFileInfo(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if info.MaxTXID != 2 {
t.Errorf("MaxTXID=%d, want 2 (last file should be kept)", info.MaxTXID)
}
})
}
func TestCompactor_EnforceL0Retention(t *testing.T) {
t.Run("DeletesCompactedFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create L0 files
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
createTestLTXFile(t, client, 0, 3, 3)
// Compact to L1
_, err := compactor.Compact(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
// Enforce L0 retention with 0 duration (delete immediately)
err = compactor.EnforceL0Retention(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
// L0 files compacted into L1 should be deleted (except last)
itr, err := client.LTXFiles(context.Background(), 0, 0, false)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
var count int
for itr.Next() {
count++
}
// At least one file should remain
if count < 1 {
t.Errorf("file count=%d, want at least 1", count)
}
})
t.Run("SkipsIfNoL1", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create L0 files without compacting to L1
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
// Enforce L0 retention - should do nothing since no L1 exists
err := compactor.EnforceL0Retention(context.Background(), 0)
if err != nil {
t.Fatal(err)
}
// All L0 files should still exist
itr, err := client.LTXFiles(context.Background(), 0, 0, false)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
var count int
for itr.Next() {
count++
}
if count != 2 {
t.Errorf("file count=%d, want 2", count)
}
})
}
func TestCompactor_EnforceSnapshotRetention(t *testing.T) {
t.Run("DeletesOldSnapshots", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create snapshot files with different timestamps
createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 5, time.Now().Add(-2*time.Hour))
createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 10, time.Now().Add(-30*time.Minute))
createTestLTXFileWithTimestamp(t, client, litestream.SnapshotLevel, 1, 15, time.Now().Add(-5*time.Minute))
// Enforce retention - keep snapshots from last hour
_, err := compactor.EnforceSnapshotRetention(context.Background(), time.Hour)
if err != nil {
t.Fatal(err)
}
// Count remaining snapshots
itr, err := client.LTXFiles(context.Background(), litestream.SnapshotLevel, 0, false)
if err != nil {
t.Fatal(err)
}
defer itr.Close()
var count int
for itr.Next() {
count++
}
// Should have 2 snapshots (the 30min and 5min old ones)
if count != 2 {
t.Errorf("snapshot count=%d, want 2", count)
}
})
}
func TestCompactor_VerifyLevelConsistency(t *testing.T) {
t.Run("ContiguousFiles", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create contiguous files
createTestLTXFile(t, client, 1, 1, 2)
createTestLTXFile(t, client, 1, 3, 5)
createTestLTXFile(t, client, 1, 6, 10)
// Should pass verification
err := compactor.VerifyLevelConsistency(context.Background(), 1)
if err != nil {
t.Errorf("expected nil error for contiguous files, got: %v", err)
}
})
t.Run("GapDetected", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create files with a gap (missing TXID 3-4)
createTestLTXFile(t, client, 1, 1, 2)
createTestLTXFile(t, client, 1, 5, 7) // gap: expected MinTXID=3, got 5
err := compactor.VerifyLevelConsistency(context.Background(), 1)
if err == nil {
t.Error("expected error for gap in files, got nil")
}
if err != nil && !containsString(err.Error(), "gap") {
t.Errorf("expected gap error, got: %v", err)
}
})
t.Run("OverlapDetected", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create overlapping files
createTestLTXFile(t, client, 1, 1, 5)
createTestLTXFile(t, client, 1, 3, 7) // overlap: expected MinTXID=6, got 3
err := compactor.VerifyLevelConsistency(context.Background(), 1)
if err == nil {
t.Error("expected error for overlapping files, got nil")
}
if err != nil && !containsString(err.Error(), "overlap") {
t.Errorf("expected overlap error, got: %v", err)
}
})
t.Run("SingleFile", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Create single file - should pass
createTestLTXFile(t, client, 1, 1, 5)
err := compactor.VerifyLevelConsistency(context.Background(), 1)
if err != nil {
t.Errorf("expected nil error for single file, got: %v", err)
}
})
t.Run("EmptyLevel", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
// Empty level - should pass
err := compactor.VerifyLevelConsistency(context.Background(), 1)
if err != nil {
t.Errorf("expected nil error for empty level, got: %v", err)
}
})
}
func TestCompactor_CompactWithVerification(t *testing.T) {
t.Run("VerificationEnabled", func(t *testing.T) {
client := file.NewReplicaClient(t.TempDir())
compactor := litestream.NewCompactor(client, slog.Default())
compactor.VerifyCompaction = true
// Create contiguous L0 files
createTestLTXFile(t, client, 0, 1, 1)
createTestLTXFile(t, client, 0, 2, 2)
createTestLTXFile(t, client, 0, 3, 3)
// Compact to L1 - should succeed with verification
info, err := compactor.Compact(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if info.Level != 1 {
t.Errorf("Level=%d, want 1", info.Level)
}
if info.MinTXID != 1 || info.MaxTXID != 3 {
t.Errorf("TXID range=%d-%d, want 1-3", info.MinTXID, info.MaxTXID)
}
})
}
// containsString checks if s contains substr.
func containsString(s, substr string) bool {
return bytes.Contains([]byte(s), []byte(substr))
}
// createTestLTXFile creates a minimal LTX file for testing.
func createTestLTXFile(t testing.TB, client litestream.ReplicaClient, level int, minTXID, maxTXID ltx.TXID) {
t.Helper()
createTestLTXFileWithTimestamp(t, client, level, minTXID, maxTXID, time.Now())
}
// createTestLTXFileWithTimestamp creates a minimal LTX file with a specific timestamp.
func createTestLTXFileWithTimestamp(t testing.TB, client litestream.ReplicaClient, level int, minTXID, maxTXID ltx.TXID, ts time.Time) {
t.Helper()
var buf bytes.Buffer
enc, err := ltx.NewEncoder(&buf)
if err != nil {
t.Fatal(err)
}
if err := enc.EncodeHeader(ltx.Header{
Version: ltx.Version,
Flags: ltx.HeaderFlagNoChecksum,
PageSize: 4096,
Commit: 1,
MinTXID: minTXID,
MaxTXID: maxTXID,
Timestamp: ts.UnixMilli(),
}); err != nil {
t.Fatal(err)
}
// Write a dummy page
if err := enc.EncodePage(ltx.PageHeader{Pgno: 1}, make([]byte, 4096)); err != nil {
t.Fatal(err)
}
if err := enc.Close(); err != nil {
t.Fatal(err)
}
if _, err := client.WriteLTXFile(context.Background(), level, minTXID, maxTXID, io.NopCloser(&buf)); err != nil {
t.Fatal(err)
}
}