-
Notifications
You must be signed in to change notification settings - Fork 96
/
backup_client_test.go
221 lines (195 loc) · 8.63 KB
/
backup_client_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
package litefs_test
import (
"bytes"
"context"
"errors"
"io"
"reflect"
"testing"
"github.com/superfly/litefs"
"github.com/superfly/ltx"
)
func TestFileBackupClient_URL(t *testing.T) {
c := litefs.NewFileBackupClient("/path/to/data")
if got, want := c.URL(), `file:///path/to/data`; got != want {
t.Fatalf("URL=%s, want %s", got, want)
}
}
func TestFileBackupClient_WriteTx(t *testing.T) {
t.Run("OK", func(t *testing.T) {
c := newOpenFileBackupClient(t)
// Write several transaction files to the client.
if hwm, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{1}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0xe4e4aaa102377eee},
})); err != nil {
t.Fatal(err)
} else if got, want := hwm, ltx.TXID(1); got != want {
t.Fatalf("hwm=%s, want %s", got, want)
}
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 2, MinTXID: 2, MaxTXID: 2, PreApplyChecksum: 0xe4e4aaa102377eee},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 2}, Data: bytes.Repeat([]byte{2}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0x99b1d11ab98cc555},
})); err != nil {
t.Fatal(err)
}
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 2, MinTXID: 3, MaxTXID: 4, PreApplyChecksum: 0x99b1d11ab98cc555},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{3}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0x8b87423eeeeeeeee},
})); err != nil {
t.Fatal(err)
}
// Write to a different database.
if _, err := c.WriteTx(context.Background(), "db2", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{5}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0x99b1d11ab98cc555},
})); err != nil {
t.Fatal(err)
}
// Read snapshot from backup service.
var other ltx.FileSpec
if rc, err := c.FetchSnapshot(context.Background(), "db"); err != nil {
t.Fatal(err)
} else if _, err := other.ReadFrom(rc); err != nil {
t.Fatal(err)
} else if err := rc.Close(); err != nil {
t.Fatal(err)
}
// Verify contents of the snapshot.
if got, want := &other, (<x.FileSpec{
Header: ltx.Header{Version: 1, Flags: ltx.HeaderFlagCompressLZ4, PageSize: 512, Commit: 2, MinTXID: 1, MaxTXID: 4},
Pages: []ltx.PageSpec{
{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{3}, 512)},
{Header: ltx.PageHeader{Pgno: 2}, Data: bytes.Repeat([]byte{2}, 512)},
},
Trailer: ltx.Trailer{
PostApplyChecksum: 0x8b87423eeeeeeeee,
FileChecksum: 0xb8e6a652b0ec8453,
},
}); !reflect.DeepEqual(got, want) {
t.Fatalf("spec mismatch:\ngot: %#v\nwant: %#v", got, want)
}
})
t.Run("ErrPosMismatch/TXID", func(t *testing.T) {
c := newOpenFileBackupClient(t)
// Write the initial transaction.
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{1}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0xe4e4aaa102377eee},
})); err != nil {
t.Fatal(err)
}
// Write a transaction that doesn't line up with the TXID.
var pmErr *ltx.PosMismatchError
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 2, MinTXID: 3, MaxTXID: 3, PreApplyChecksum: 0xe4e4aaa102377eee},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 2}, Data: bytes.Repeat([]byte{2}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: ltx.ChecksumFlag | 2000},
})); !errors.As(err, &pmErr) {
t.Fatalf("unexpected error: %s", err)
} else if got, want := pmErr.Pos, (ltx.Pos{TXID: 1, PostApplyChecksum: 0xe4e4aaa102377eee}); !reflect.DeepEqual(got, want) {
t.Fatalf("pos=%s, want %s", got, want)
}
})
t.Run("ErrPosMismatch/PostApplyChecksum", func(t *testing.T) {
c := newOpenFileBackupClient(t)
// Write the initial transaction.
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{1}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0xe4e4aaa102377eee},
})); err != nil {
t.Fatal(err)
}
// Write a transaction that doesn't line up with the TXID.
var pmErr *ltx.PosMismatchError
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 2, MinTXID: 2, MaxTXID: 2, PreApplyChecksum: ltx.ChecksumFlag | 2000},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 2}, Data: bytes.Repeat([]byte{2}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: ltx.ChecksumFlag | 2000},
})); !errors.As(err, &pmErr) {
t.Fatalf("unexpected error: %s", err)
} else if got, want := pmErr.Pos, (ltx.Pos{TXID: 1, PostApplyChecksum: 0xe4e4aaa102377eee}); !reflect.DeepEqual(got, want) {
t.Fatalf("pos=%s, want %s", got, want)
}
})
t.Run("ErrPosMismatch/FirstTx", func(t *testing.T) {
c := newOpenFileBackupClient(t)
var pmErr *ltx.PosMismatchError
if _, err := c.WriteTx(context.Background(), "db", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 2, MaxTXID: 2, PreApplyChecksum: ltx.ChecksumFlag | 2000},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{1}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: ltx.ChecksumFlag | 1000},
})); !errors.As(err, &pmErr) {
t.Fatalf("unexpected error: %s", err)
} else if got, want := pmErr.Pos, (ltx.Pos{}); !reflect.DeepEqual(got, want) {
t.Fatalf("pos=%s, want %s", got, want)
}
})
}
func TestFileBackupClient_PosMap(t *testing.T) {
t.Run("OK", func(t *testing.T) {
c := newOpenFileBackupClient(t)
if _, err := c.WriteTx(context.Background(), "db1", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{1}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0xe4e4aaa102377eee},
})); err != nil {
t.Fatal(err)
}
if _, err := c.WriteTx(context.Background(), "db1", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 2, MinTXID: 2, MaxTXID: 2, PreApplyChecksum: 0xe4e4aaa102377eee},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 2}, Data: bytes.Repeat([]byte{2}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: ltx.ChecksumFlag | 2000},
})); err != nil {
t.Fatal(err)
}
// Write to a different database.
if _, err := c.WriteTx(context.Background(), "db2", ltxFileSpecReader(t, <x.FileSpec{
Header: ltx.Header{Version: 1, PageSize: 512, Commit: 1, MinTXID: 1, MaxTXID: 1},
Pages: []ltx.PageSpec{{Header: ltx.PageHeader{Pgno: 1}, Data: bytes.Repeat([]byte{5}, 512)}},
Trailer: ltx.Trailer{PostApplyChecksum: 0x99b1d11ab98cc555},
})); err != nil {
t.Fatal(err)
}
// Read snapshot from backup service.
if m, err := c.PosMap(context.Background()); err != nil {
t.Fatal(err)
} else if got, want := m, map[string]ltx.Pos{
"db1": {TXID: 0x2, PostApplyChecksum: 0x80000000000007d0},
"db2": {TXID: 0x1, PostApplyChecksum: 0x99b1d11ab98cc555},
}; !reflect.DeepEqual(got, want) {
t.Fatalf("map=%#v, want %#v", got, want)
}
})
t.Run("NoDatabases", func(t *testing.T) {
c := newOpenFileBackupClient(t)
if m, err := c.PosMap(context.Background()); err != nil {
t.Fatal(err)
} else if got, want := m, map[string]ltx.Pos{}; !reflect.DeepEqual(got, want) {
t.Fatalf("map=%#v, want %#v", got, want)
}
})
}
func newOpenFileBackupClient(tb testing.TB) *litefs.FileBackupClient {
tb.Helper()
c := litefs.NewFileBackupClient(tb.TempDir())
if err := c.Open(); err != nil {
tb.Fatal(err)
}
return c
}
// ltxFileSpecReader returns a spec as an io.Reader of its serialized bytes.
func ltxFileSpecReader(tb testing.TB, spec *ltx.FileSpec) io.Reader {
tb.Helper()
var buf bytes.Buffer
if _, err := spec.WriteTo(&buf); err != nil {
tb.Fatal(err)
}
return bytes.NewReader(buf.Bytes())
}