forked from filecoin-project/go-bs-lmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockstore_test.go
400 lines (324 loc) · 9.3 KB
/
blockstore_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
package lmdbbs
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"sync"
"testing"
"time"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
ipfsUtil "github.com/ipfs/go-ipfs-util"
logger "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multihash"
bstest "github.com/raulk/go-bs-tests"
"github.com/stretchr/testify/require"
)
func init() {
logger.SetupLogging(logger.Config{Stdout: true})
}
func TestLMDBBlockstore(t *testing.T) {
sync := Options{NoSync: false}
s := &bstest.Suite{
NewBlockstore: newBlockstore(sync),
OpenBlockstore: openBlockstore(sync),
}
s.RunTests(t, "sync")
nosync := Options{NoSync: true}
s = &bstest.Suite{
NewBlockstore: newBlockstore(nosync),
OpenBlockstore: openBlockstore(nosync),
}
s.RunTests(t, "nosync")
}
func newBlockstore(opts Options) func(tb testing.TB) (bstest.Blockstore, string) {
return func(tb testing.TB) (bstest.Blockstore, string) {
tb.Helper()
path, err := ioutil.TempDir("", "")
if err != nil {
tb.Fatal(err)
}
opts.Path = path
db, err := Open(&opts)
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
_ = os.RemoveAll(path)
})
return db, path
}
}
func openBlockstore(opts Options) func(tb testing.TB, path string) (bstest.Blockstore, error) {
return func(tb testing.TB, path string) (bstest.Blockstore, error) {
opts.Path = path
return Open(&opts)
}
}
func checkBlocksBs(t *testing.T, bs blockstore.Viewer, blocks []blocks.Block) {
for _, b := range blocks {
require.NoError(t, bs.View(b.Cid(), func(data []byte) error {
if !bytes.Equal(data, b.RawData()) {
return fmt.Errorf("unexpected value for a key")
}
return nil
}))
}
}
func TestMmapExpansionAndParallelContext(t *testing.T) {
opts := Options{InitialMmapSize: 1 << 20, NoLock: true} // 1MiB.
bs, path := newBlockstore(opts)(t)
info, err := bs.(*Blockstore).env.Info()
require.NoError(t, err)
prev := info.MapSize
blocks1 := putEntries(t, bs, 16*1024, 1*1024)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
current := info.MapSize
require.Greater(t, current, prev)
opts.ReadOnly = true
// reopen the database with the original initial mmap size.
bs2, err := openBlockstore(opts)(t, path)
require.NoError(t, err)
info, err = bs2.(*Blockstore).env.Info()
require.NoError(t, err)
reopened := info.MapSize
require.Greater(t, int(reopened), 1<<23)
checkBlocksBs(t, bs2, blocks1)
// verify that we can add more entries, and that we grow again.
blocks2 := putEntries(t, bs, 16*1024, 1*1024)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
final := info.MapSize
require.Greater(t, final, reopened)
checkBlocksBs(t, bs2, blocks2)
}
func TestMmapExpansionSucceedsReopen(t *testing.T) {
opts := Options{InitialMmapSize: 1 << 20} // 1MiB.
bs, path := newBlockstore(opts)(t)
info, err := bs.(*Blockstore).env.Info()
require.NoError(t, err)
prev := info.MapSize
blocks1 := putEntries(t, bs, 16*1024, 1*1024)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
current := info.MapSize
require.Greater(t, current, prev)
// close the db.
require.NoError(t, bs.(io.Closer).Close())
// reopen the database with the original initial mmap size.
bs, err = openBlockstore(opts)(t, path)
require.NoError(t, err)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
reopened := info.MapSize
require.Greater(t, int(reopened), 1<<23)
checkBlocksBs(t, bs, blocks1)
// verify that we can add more entries, and that we grow again.
putEntries(t, bs, 16*1024, 1*1024)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
final := info.MapSize
require.Greater(t, final, reopened)
}
func TestNoMmapExpansion(t *testing.T) {
opts := Options{InitialMmapSize: 64 << 20} // 64MiB, a large enough mmap size.
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
info, err := bs.(*Blockstore).env.Info()
require.NoError(t, err)
prev := info.MapSize
putEntries(t, bs, 16*1024, 1*1024)
info, err = bs.(*Blockstore).env.Info()
require.NoError(t, err)
current := info.MapSize
require.EqualValues(t, prev, current)
}
func TestMmapExpansionWithCursors(t *testing.T) {
opts := Options{InitialMmapSize: 64 << 20} // 64MiB, a large enough mmap size.
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
putEntries(t, bs, 1*1024, 1*1024)
// cursor 1.
ctx, cancel := context.WithCancel(context.Background())
ch1, err := bs.AllKeysChan(ctx)
require.NoError(t, err)
<-ch1 // consume one entry
// cursor 2.
ch2, err := bs.AllKeysChan(ctx)
require.NoError(t, err)
<-ch2 // consume one entry
// cursor 3.
ch3, err := bs.AllKeysChan(ctx)
require.NoError(t, err)
<-ch3 // consume one entry
// add more entries to force the mmap to grow.
putEntries(t, bs, 4*1024, 1*1024)
var i int
for range ch1 {
i++ // verify that the cursor continues running and eventually finishes.
}
require.Greater(t, i, 1*1024) // we see entries from the second insertion batch.
i = 0
for range ch2 {
i++ // verify that the cursor continues running and eventually finishes.
}
require.Greater(t, i, 1*1024) // we see entries from the second insertion batch.
i = 0
for range ch3 {
i++ // verify that the cursor continues running and eventually finishes.
}
require.Greater(t, i, 1*1024) // we see entries from the second insertion batch.
cancel()
}
func TestGrowUnderConcurrency(t *testing.T) {
opts := Options{ // set a really aggressive policy that makes the mmap grow very frequently.
InitialMmapSize: 1 << 10,
MmapGrowthStepFactor: 1.5,
MmapGrowthStepMax: 2 << 10,
}
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
var wg sync.WaitGroup
for i := 0; i < 20; i++ { // 20 writers.
wg.Add(1)
go func() {
defer wg.Done()
putEntries(t, bs, 1*1024, 1*1024)
}()
}
for i := 0; i < 20; i++ { // 20 queriers for random CIDs.
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 1024; i++ {
_, _ = bs.Get(randomCID())
}
}()
}
for i := 0; i < 20; i++ { // 20 deleters of random CIDs.
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 1024; i++ {
_ = bs.DeleteBlock(randomCID())
}
}()
}
for i := 0; i < 20; i++ { // 20 cursors.
wg.Add(1)
go func() {
defer wg.Done()
ch, _ := bs.AllKeysChan(context.Background())
for range ch {
}
}()
}
wg.Wait()
}
func TestRetryWhenReadersFull(t *testing.T) {
opts := Options{
MaxReaders: 1, // single reader to induce contention.
}
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
blocks := putEntries(t, bs, 1*1024, 1*1024)
// this context cancels in 2 seconds.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
ch, err := bs.AllKeysChan(ctx)
require.NoError(t, err)
k1 := <-ch // consume one element, then leave it hanging.
keyExists := false
for _, block := range blocks {
keyExists = keyExists || k1.Equals(block.Cid())
}
require.True(t, keyExists, "Key received from the channel is unknown")
// this get will block until the cursor has finished.
start := time.Now()
_, err = bs.Get(randomCID())
require.Equal(t, blockstore.ErrNotFound, err)
require.GreaterOrEqual(t, time.Since(start).Nanoseconds(), 1*time.Second.Nanoseconds())
}
// TestMmapExpansionPutMany tests that a PutMany operation yields when it
// encounters a MDB_MAP_FULL error, and that it retries once the grow finishes.
func TestMmapExpansionPutMany(t *testing.T) {
opts := Options{
InitialMmapSize: 1 << 10,
MmapGrowthStepFactor: 1.5,
MmapGrowthStepMax: 2 << 10,
}
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
var blks []blocks.Block
for i := 0; i < 1024; i++ {
b := make([]byte, 1024)
rand.Read(b)
c := cid.NewCidV1(cid.Raw, ipfsUtil.Hash(b)) // makes a copy of k
blk, err := blocks.NewBlockWithCid(b, c)
require.NoError(t, err)
blks = append(blks, blk)
}
err := bs.PutMany(blks)
require.NoError(t, err)
}
func putEntries(t *testing.T, bs bstest.Blockstore, count int, size int) []blocks.Block {
res := make([]blocks.Block, 0, count)
for i := 0; i < count; i++ {
b := make([]byte, size)
rand.Read(b)
c := cid.NewCidV1(cid.Raw, ipfsUtil.Hash(b)) // makes a copy of k
blk, err := blocks.NewBlockWithCid(b, c)
require.NoError(t, err)
require.NoError(t, bs.Put(blk))
res = append(res, blk)
}
return res
}
func randomCID() cid.Cid {
b := make([]byte, 32)
rand.Read(b)
mh, _ := multihash.Encode(b, multihash.SHA2_256)
return cid.NewCidV1(cid.Raw, mh)
}
type deleteManyer interface {
DeleteMany([]cid.Cid) error
}
func TestDeleteMany(t *testing.T) {
opts := Options{
InitialMmapSize: 1 << 10,
MmapGrowthStepFactor: 1.5,
MmapGrowthStepMax: 2 << 10,
}
bs, _ := newBlockstore(opts)(t)
defer bs.(io.Closer).Close()
blocks := putEntries(t, bs, 500, 1024)
todelete := make([]cid.Cid, 100)
for i := range todelete {
todelete[i] = blocks[i].Cid()
}
require.NoError(t, bs.DeleteBlock(todelete[5]))
require.NoError(t, bs.DeleteBlock(todelete[5]))
dm := bs.(deleteManyer)
if err := dm.DeleteMany(todelete); err != nil {
t.Fatal(err)
}
deleted := make(map[cid.Cid]bool)
for _, d := range todelete {
deleted[d] = true
}
ch, err := bs.AllKeysChan(context.TODO())
if err != nil {
t.Fatal(err)
}
for c := range ch {
if deleted[c] {
t.Fatal("found cid in blockstore we deleted")
}
}
}