aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/ldbstore_test.go
blob: 48af8c57c01627a47f33a8bb9b386a2e8c3dcd2b (plain) (blame)
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
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package storage

import (
    "bytes"
    "context"
    "fmt"
    "io/ioutil"
    "os"
    "strconv"
    "strings"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    ch "github.com/ethereum/go-ethereum/swarm/chunk"
    "github.com/ethereum/go-ethereum/swarm/log"
    "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"

    ldberrors "github.com/syndtr/goleveldb/leveldb/errors"
)

type testDbStore struct {
    *LDBStore
    dir string
}

func newTestDbStore(mock bool, trusted bool) (*testDbStore, func(), error) {
    dir, err := ioutil.TempDir("", "bzz-storage-test")
    if err != nil {
        return nil, func() {}, err
    }

    var db *LDBStore
    storeparams := NewDefaultStoreParams()
    params := NewLDBStoreParams(storeparams, dir)
    params.Po = testPoFunc

    if mock {
        globalStore := mem.NewGlobalStore()
        addr := common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
        mockStore := globalStore.NewNodeStore(addr)

        db, err = NewMockDbStore(params, mockStore)
    } else {
        db, err = NewLDBStore(params)
    }

    cleanup := func() {
        if db != nil {
            db.Close()
        }
        err = os.RemoveAll(dir)
        if err != nil {
            panic(fmt.Sprintf("db cleanup failed: %v", err))
        }
    }

    return &testDbStore{db, dir}, cleanup, err
}

func testPoFunc(k Address) (ret uint8) {
    basekey := make([]byte, 32)
    return uint8(Proximity(basekey, k[:]))
}

func (db *testDbStore) close() {
    db.Close()
    err := os.RemoveAll(db.dir)
    if err != nil {
        panic(err)
    }
}

func testDbStoreRandom(n int, chunksize int64, mock bool, t *testing.T) {
    db, cleanup, err := newTestDbStore(mock, true)
    defer cleanup()
    if err != nil {
        t.Fatalf("init dbStore failed: %v", err)
    }
    testStoreRandom(db, n, chunksize, t)
}

func testDbStoreCorrect(n int, chunksize int64, mock bool, t *testing.T) {
    db, cleanup, err := newTestDbStore(mock, false)
    defer cleanup()
    if err != nil {
        t.Fatalf("init dbStore failed: %v", err)
    }
    testStoreCorrect(db, n, chunksize, t)
}

func TestDbStoreRandom_1(t *testing.T) {
    testDbStoreRandom(1, 0, false, t)
}

func TestDbStoreCorrect_1(t *testing.T) {
    testDbStoreCorrect(1, 4096, false, t)
}

func TestDbStoreRandom_1k(t *testing.T) {
    testDbStoreRandom(1000, 0, false, t)
}

func TestDbStoreCorrect_1k(t *testing.T) {
    testDbStoreCorrect(1000, 4096, false, t)
}

func TestMockDbStoreRandom_1(t *testing.T) {
    testDbStoreRandom(1, 0, true, t)
}

func TestMockDbStoreCorrect_1(t *testing.T) {
    testDbStoreCorrect(1, 4096, true, t)
}

func TestMockDbStoreRandom_1k(t *testing.T) {
    testDbStoreRandom(1000, 0, true, t)
}

func TestMockDbStoreCorrect_1k(t *testing.T) {
    testDbStoreCorrect(1000, 4096, true, t)
}

func testDbStoreNotFound(t *testing.T, mock bool) {
    db, cleanup, err := newTestDbStore(mock, false)
    defer cleanup()
    if err != nil {
        t.Fatalf("init dbStore failed: %v", err)
    }

    _, err = db.Get(context.TODO(), ZeroAddr)
    if err != ErrChunkNotFound {
        t.Errorf("Expected ErrChunkNotFound, got %v", err)
    }
}

func TestDbStoreNotFound(t *testing.T) {
    testDbStoreNotFound(t, false)
}
func TestMockDbStoreNotFound(t *testing.T) {
    testDbStoreNotFound(t, true)
}

func testIterator(t *testing.T, mock bool) {
    var chunkcount int = 32
    var i int
    var poc uint
    chunkkeys := NewAddressCollection(chunkcount)
    chunkkeys_results := NewAddressCollection(chunkcount)

    db, cleanup, err := newTestDbStore(mock, false)
    defer cleanup()
    if err != nil {
        t.Fatalf("init dbStore failed: %v", err)
    }

    chunks := GenerateRandomChunks(ch.DefaultSize, chunkcount)

    for i = 0; i < len(chunks); i++ {
        chunkkeys[i] = chunks[i].Address()
        err := db.Put(context.TODO(), chunks[i])
        if err != nil {
            t.Fatalf("dbStore.Put failed: %v", err)
        }
    }

    for i = 0; i < len(chunkkeys); i++ {
        log.Trace(fmt.Sprintf("Chunk array pos %d/%d: '%v'", i, chunkcount, chunkkeys[i]))
    }
    i = 0
    for poc = 0; poc <= 255; poc++ {
        err := db.SyncIterator(0, uint64(chunkkeys.Len()), uint8(poc), func(k Address, n uint64) bool {
            log.Trace(fmt.Sprintf("Got key %v number %d poc %d", k, n, uint8(poc)))
            chunkkeys_results[n] = k
            i++
            return true
        })
        if err != nil {
            t.Fatalf("Iterator call failed: %v", err)
        }
    }

    for i = 0; i < chunkcount; i++ {
        if !bytes.Equal(chunkkeys[i], chunkkeys_results[i]) {
            t.Fatalf("Chunk put #%d key '%v' does not match iterator's key '%v'", i, chunkkeys[i], chunkkeys_results[i])
        }
    }

}

func TestIterator(t *testing.T) {
    testIterator(t, false)
}
func TestMockIterator(t *testing.T) {
    testIterator(t, true)
}

func benchmarkDbStorePut(n int, processors int, chunksize int64, mock bool, b *testing.B) {
    db, cleanup, err := newTestDbStore(mock, true)
    defer cleanup()
    if err != nil {
        b.Fatalf("init dbStore failed: %v", err)
    }
    benchmarkStorePut(db, n, chunksize, b)
}

func benchmarkDbStoreGet(n int, processors int, chunksize int64, mock bool, b *testing.B) {
    db, cleanup, err := newTestDbStore(mock, true)
    defer cleanup()
    if err != nil {
        b.Fatalf("init dbStore failed: %v", err)
    }
    benchmarkStoreGet(db, n, chunksize, b)
}

func BenchmarkDbStorePut_1_500(b *testing.B) {
    benchmarkDbStorePut(500, 1, 4096, false, b)
}

func BenchmarkDbStorePut_8_500(b *testing.B) {
    benchmarkDbStorePut(500, 8, 4096, false, b)
}

func BenchmarkDbStoreGet_1_500(b *testing.B) {
    benchmarkDbStoreGet(500, 1, 4096, false, b)
}

func BenchmarkDbStoreGet_8_500(b *testing.B) {
    benchmarkDbStoreGet(500, 8, 4096, false, b)
}

func BenchmarkMockDbStorePut_1_500(b *testing.B) {
    benchmarkDbStorePut(500, 1, 4096, true, b)
}

func BenchmarkMockDbStorePut_8_500(b *testing.B) {
    benchmarkDbStorePut(500, 8, 4096, true, b)
}

func BenchmarkMockDbStoreGet_1_500(b *testing.B) {
    benchmarkDbStoreGet(500, 1, 4096, true, b)
}

func BenchmarkMockDbStoreGet_8_500(b *testing.B) {
    benchmarkDbStoreGet(500, 8, 4096, true, b)
}

// TestLDBStoreWithoutCollectGarbage tests that we can put a number of random chunks in the LevelDB store, and
// retrieve them, provided we don't hit the garbage collection
func TestLDBStoreWithoutCollectGarbage(t *testing.T) {
    capacity := 50
    n := 10

    ldb, cleanup := newLDBStore(t)
    ldb.setCapacity(uint64(capacity))
    defer cleanup()

    chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
    if err != nil {
        t.Fatal(err.Error())
    }

    log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)

    for _, ch := range chunks {
        ret, err := ldb.Get(context.TODO(), ch.Address())
        if err != nil {
            t.Fatal(err)
        }

        if !bytes.Equal(ret.Data(), ch.Data()) {
            t.Fatal("expected to get the same data back, but got smth else")
        }
    }

    if ldb.entryCnt != uint64(n) {
        t.Fatalf("expected entryCnt to be equal to %v, but got %v", n, ldb.entryCnt)
    }

    if ldb.accessCnt != uint64(2*n) {
        t.Fatalf("expected accessCnt to be equal to %v, but got %v", 2*n, ldb.accessCnt)
    }
}

// TestLDBStoreCollectGarbage tests that we can put more chunks than LevelDB's capacity, and
// retrieve only some of them, because garbage collection must have partially cleared the store
// Also tests that we can delete chunks and that we can trigger garbage collection
func TestLDBStoreCollectGarbage(t *testing.T) {

    // below max ronud
    cap := defaultMaxGCRound / 2
    t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
    t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)

    // at max round
    cap = defaultMaxGCRound
    t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
    t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)

    // more than max around, not on threshold
    cap = defaultMaxGCRound * 1.1
    t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
    t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)

}

func testLDBStoreCollectGarbage(t *testing.T) {
    params := strings.Split(t.Name(), "/")
    capacity, err := strconv.Atoi(params[2])
    if err != nil {
        t.Fatal(err)
    }
    n, err := strconv.Atoi(params[3])
    if err != nil {
        t.Fatal(err)
    }

    ldb, cleanup := newLDBStore(t)
    ldb.setCapacity(uint64(capacity))
    defer cleanup()

    // retrieve the gc round target count for the db capacity
    ldb.startGC(capacity)
    roundTarget := ldb.gc.target

    // split put counts to gc target count threshold, and wait for gc to finish in between
    var allChunks []Chunk
    remaining := n
    for remaining > 0 {
        var putCount int
        if remaining < roundTarget {
            putCount = remaining
        } else {
            putCount = roundTarget
        }
        remaining -= putCount
        chunks, err := mputRandomChunks(ldb, putCount, int64(ch.DefaultSize))
        if err != nil {
            t.Fatal(err.Error())
        }
        allChunks = append(allChunks, chunks...)
        log.Debug("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt, "cap", capacity, "n", n)

        ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
        defer cancel()
        waitGc(ctx, ldb)
    }

    // attempt gets on all put chunks
    var missing int
    for _, ch := range allChunks {
        ret, err := ldb.Get(context.TODO(), ch.Address())
        if err == ErrChunkNotFound || err == ldberrors.ErrNotFound {
            missing++
            continue
        }
        if err != nil {
            t.Fatal(err)
        }

        if !bytes.Equal(ret.Data(), ch.Data()) {
            t.Fatal("expected to get the same data back, but got smth else")
        }

        log.Trace("got back chunk", "chunk", ret)
    }

    // all surplus chunks should be missing
    expectMissing := roundTarget + (((n - capacity) / roundTarget) * roundTarget)
    if missing != expectMissing {
        t.Fatalf("gc failure: expected to miss %v chunks, but only %v are actually missing", expectMissing, missing)
    }

    log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
}

// TestLDBStoreAddRemove tests that we can put and then delete a given chunk
func TestLDBStoreAddRemove(t *testing.T) {
    ldb, cleanup := newLDBStore(t)
    ldb.setCapacity(200)
    defer cleanup()

    n := 100
    chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
    if err != nil {
        t.Fatalf(err.Error())
    }

    for i := 0; i < n; i++ {
        // delete all even index chunks
        if i%2 == 0 {
            ldb.Delete(chunks[i].Address())
        }
    }

    log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)

    for i := 0; i < n; i++ {
        ret, err := ldb.Get(nil, chunks[i].Address())

        if i%2 == 0 {
            // expect even chunks to be missing
            if err == nil {
                t.Fatal("expected chunk to be missing, but got no error")
            }
        } else {
            // expect odd chunks to be retrieved successfully
            if err != nil {
                t.Fatalf("expected no error, but got %s", err)
            }

            if !bytes.Equal(ret.Data(), chunks[i].Data()) {
                t.Fatal("expected to get the same data back, but got smth else")
            }
        }
    }
}

func testLDBStoreRemoveThenCollectGarbage(t *testing.T) {

    params := strings.Split(t.Name(), "/")
    capacity, err := strconv.Atoi(params[2])
    if err != nil {
        t.Fatal(err)
    }
    n, err := strconv.Atoi(params[3])
    if err != nil {
        t.Fatal(err)
    }

    ldb, cleanup := newLDBStore(t)
    defer cleanup()
    ldb.setCapacity(uint64(capacity))

    // put capacity count number of chunks
    chunks := make([]Chunk, n)
    for i := 0; i < n; i++ {
        c := GenerateRandomChunk(ch.DefaultSize)
        chunks[i] = c
        log.Trace("generate random chunk", "idx", i, "chunk", c)
    }

    for i := 0; i < n; i++ {
        err := ldb.Put(context.TODO(), chunks[i])
        if err != nil {
            t.Fatal(err)
        }
    }

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    defer cancel()
    waitGc(ctx, ldb)

    // delete all chunks
    // (only count the ones actually deleted, the rest will have been gc'd)
    deletes := 0
    for i := 0; i < n; i++ {
        if ldb.Delete(chunks[i].Address()) == nil {
            deletes++
        }
    }

    log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)

    if ldb.entryCnt != 0 {
        t.Fatalf("ldb.entrCnt expected 0 got %v", ldb.entryCnt)
    }

    // the manual deletes will have increased accesscnt, so we need to add this when we verify the current count
    expAccessCnt := uint64(n)
    if ldb.accessCnt != expAccessCnt {
        t.Fatalf("ldb.accessCnt expected %v got %v", expAccessCnt, ldb.accessCnt)
    }

    // retrieve the gc round target count for the db capacity
    ldb.startGC(capacity)
    roundTarget := ldb.gc.target

    remaining := n
    var puts int
    for remaining > 0 {
        var putCount int
        if remaining < roundTarget {
            putCount = remaining
        } else {
            putCount = roundTarget
        }
        remaining -= putCount
        for putCount > 0 {
            ldb.Put(context.TODO(), chunks[puts])
            log.Debug("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt, "cap", capacity, "n", n, "puts", puts, "remaining", remaining, "roundtarget", roundTarget)
            puts++
            putCount--
        }

        ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
        defer cancel()
        waitGc(ctx, ldb)
    }

    // expect first surplus chunks to be missing, because they have the smallest access value
    expectMissing := roundTarget + (((n - capacity) / roundTarget) * roundTarget)
    for i := 0; i < expectMissing; i++ {
        _, err := ldb.Get(context.TODO(), chunks[i].Address())
        if err == nil {
            t.Fatalf("expected surplus chunk %d to be missing, but got no error", i)
        }
    }

    // expect last chunks to be present, as they have the largest access value
    for i := expectMissing; i < n; i++ {
        ret, err := ldb.Get(context.TODO(), chunks[i].Address())
        if err != nil {
            t.Fatalf("chunk %v: expected no error, but got %s", i, err)
        }
        if !bytes.Equal(ret.Data(), chunks[i].Data()) {
            t.Fatal("expected to get the same data back, but got smth else")
        }
    }
}

// TestLDBStoreCollectGarbageAccessUnlikeIndex tests garbage collection where accesscount differs from indexcount
func TestLDBStoreCollectGarbageAccessUnlikeIndex(t *testing.T) {

    capacity := defaultMaxGCRound * 2
    n := capacity - 1

    ldb, cleanup := newLDBStore(t)
    ldb.setCapacity(uint64(capacity))
    defer cleanup()

    chunks, err := mputRandomChunks(ldb, n, int64(ch.DefaultSize))
    if err != nil {
        t.Fatal(err.Error())
    }
    log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)

    // set first added capacity/2 chunks to highest accesscount
    for i := 0; i < capacity/2; i++ {
        _, err := ldb.Get(context.TODO(), chunks[i].Address())
        if err != nil {
            t.Fatalf("fail add chunk #%d - %s: %v", i, chunks[i].Address(), err)
        }
    }
    _, err = mputRandomChunks(ldb, 2, int64(ch.DefaultSize))
    if err != nil {
        t.Fatal(err.Error())
    }

    // wait for garbage collection to kick in on the responsible actor
    ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    defer cancel()
    waitGc(ctx, ldb)

    var missing int
    for i, ch := range chunks[2 : capacity/2] {
        ret, err := ldb.Get(context.TODO(), ch.Address())
        if err == ErrChunkNotFound || err == ldberrors.ErrNotFound {
            t.Fatalf("fail find chunk #%d - %s: %v", i, ch.Address(), err)
        }

        if !bytes.Equal(ret.Data(), ch.Data()) {
            t.Fatal("expected to get the same data back, but got smth else")
        }
        log.Trace("got back chunk", "chunk", ret)
    }

    log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
}

func waitGc(ctx context.Context, ldb *LDBStore) {
    <-ldb.gc.runC
    ldb.gc.runC <- struct{}{}
}