Skip to content

Commit ff3a5d2

Browse files
nonsensegbalint
authored andcommitted
swarm/storage: remove redundant increments for dataIdx and entryCnt (#17484)
* swarm/storage: remove redundant increments for dataIdx and entryCnt * swarm/storage: add Delete to LDBStore * swarm/storage: wait for garbage collection
1 parent bfce003 commit ff3a5d2

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

‎swarm/storage/ldbstore.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,10 @@ func NewLDBStore(params *LDBStoreParams) (s *LDBStore, err error) {
147147
}
148148
data, _ := s.db.Get(keyEntryCnt)
149149
s.entryCnt = BytesToU64(data)
150-
s.entryCnt++
151150
data, _ = s.db.Get(keyAccessCnt)
152151
s.accessCnt = BytesToU64(data)
153-
s.accessCnt++
154152
data, _ = s.db.Get(keyDataIdx)
155153
s.dataIdx = BytesToU64(data)
156-
s.dataIdx++
157154

158155
return s, nil
159156
}
@@ -251,6 +248,8 @@ func decodeOldData(data []byte, chunk *Chunk) {
251248
}
252249

253250
func (s *LDBStore) collectGarbage(ratio float32) {
251+
log.Trace("collectGarbage", "ratio", ratio)
252+
254253
metrics.GetOrRegisterCounter("ldbstore.collectgarbage", nil).Inc(1)
255254

256255
it := s.db.NewIterator()
@@ -492,6 +491,18 @@ func (s *LDBStore) ReIndex() {
492491
log.Warn(fmt.Sprintf("Found %v errors out of %v entries", errorsFound, total))
493492
}
494493

494+
func (s *LDBStore) Delete(addr Address) {
495+
s.lock.Lock()
496+
defer s.lock.Unlock()
497+
498+
ikey := getIndexKey(addr)
499+
500+
var indx dpaDBIndex
501+
s.tryAccessIdx(ikey, &indx)
502+
503+
s.delete(indx.Idx, ikey, s.po(addr))
504+
}
505+
495506
func (s *LDBStore) delete(idx uint64, idxKey []byte, po uint8) {
496507
metrics.GetOrRegisterCounter("ldbstore.delete", nil).Inc(1)
497508

@@ -517,8 +528,8 @@ func (s *LDBStore) CurrentBucketStorageIndex(po uint8) uint64 {
517528
}
518529

519530
func (s *LDBStore) Size() uint64 {
520-
s.lock.Lock()
521-
defer s.lock.Unlock()
531+
s.lock.RLock()
532+
defer s.lock.RUnlock()
522533
return s.entryCnt
523534
}
524535

@@ -602,21 +613,23 @@ mainLoop:
602613
}
603614
close(c)
604615
for e > s.capacity {
616+
log.Trace("for >", "e", e, "s.capacity", s.capacity)
605617
// Collect garbage in a separate goroutine
606618
// to be able to interrupt this loop by s.quit.
607619
done := make(chan struct{})
608620
go func() {
609621
s.collectGarbage(gcArrayFreeRatio)
622+
log.Trace("collectGarbage closing done")
610623
close(done)
611624
}()
612625

613-
e = s.entryCnt
614626
select {
615627
case <-s.quit:
616628
s.lock.Unlock()
617629
break mainLoop
618630
case <-done:
619631
}
632+
e = s.entryCnt
620633
}
621634
s.lock.Unlock()
622635
}

‎swarm/storage/ldbstore_test.go‎

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"time"
2828

2929
"github.com/ethereum/go-ethereum/common"
30+
"github.com/ethereum/go-ethereum/log"
3031
"github.com/ethereum/go-ethereum/swarm/chunk"
31-
"github.com/ethereum/go-ethereum/swarm/log"
3232
"github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
3333

3434
ldberrors "github.com/syndtr/goleveldb/leveldb/errors"
@@ -209,7 +209,7 @@ func testIterator(t *testing.T, mock bool) {
209209
for poc = 0; poc <= 255; poc++ {
210210
err := db.SyncIterator(0, uint64(chunkkeys.Len()), uint8(poc), func(k Address, n uint64) bool {
211211
log.Trace(fmt.Sprintf("Got key %v number %d poc %d", k, n, uint8(poc)))
212-
chunkkeys_results[n-1] = k
212+
chunkkeys_results[n] = k
213213
i++
214214
return true
215215
})
@@ -324,12 +324,12 @@ func TestLDBStoreWithoutCollectGarbage(t *testing.T) {
324324
log.Info("got back chunk", "chunk", ret)
325325
}
326326

327-
if ldb.entryCnt != uint64(n+1) {
328-
t.Fatalf("expected entryCnt to be equal to %v, but got %v", n+1, ldb.entryCnt)
327+
if ldb.entryCnt != uint64(n) {
328+
t.Fatalf("expected entryCnt to be equal to %v, but got %v", n, ldb.entryCnt)
329329
}
330330

331-
if ldb.accessCnt != uint64(2*n+1) {
332-
t.Fatalf("expected accessCnt to be equal to %v, but got %v", n+1, ldb.accessCnt)
331+
if ldb.accessCnt != uint64(2*n) {
332+
t.Fatalf("expected accessCnt to be equal to %v, but got %v", 2*n, ldb.accessCnt)
333333
}
334334
}
335335

@@ -362,7 +362,7 @@ func TestLDBStoreCollectGarbage(t *testing.T) {
362362
log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
363363

364364
// wait for garbage collection to kick in on the responsible actor
365-
time.Sleep(5 * time.Second)
365+
time.Sleep(1 * time.Second)
366366

367367
var missing int
368368
for i := 0; i < n; i++ {
@@ -416,14 +416,7 @@ func TestLDBStoreAddRemove(t *testing.T) {
416416
for i := 0; i < n; i++ {
417417
// delete all even index chunks
418418
if i%2 == 0 {
419-
420-
key := chunks[i].Addr
421-
ikey := getIndexKey(key)
422-
423-
var indx dpaDBIndex
424-
ldb.tryAccessIdx(ikey, &indx)
425-
426-
ldb.delete(indx.Idx, ikey, ldb.po(key))
419+
ldb.Delete(chunks[i].Addr)
427420
}
428421
}
429422

@@ -452,12 +445,12 @@ func TestLDBStoreAddRemove(t *testing.T) {
452445

453446
// TestLDBStoreRemoveThenCollectGarbage tests that we can delete chunks and that we can trigger garbage collection
454447
func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
455-
capacity := 10
448+
capacity := 11
456449

457450
ldb, cleanup := newLDBStore(t)
458451
ldb.setCapacity(uint64(capacity))
459452

460-
n := 7
453+
n := 11
461454

462455
chunks := []*Chunk{}
463456
for i := 0; i < capacity; i++ {
@@ -477,23 +470,18 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
477470

478471
// delete all chunks
479472
for i := 0; i < n; i++ {
480-
key := chunks[i].Addr
481-
ikey := getIndexKey(key)
482-
483-
var indx dpaDBIndex
484-
ldb.tryAccessIdx(ikey, &indx)
485-
486-
ldb.delete(indx.Idx, ikey, ldb.po(key))
473+
ldb.Delete(chunks[i].Addr)
487474
}
488475

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

491478
cleanup()
492479

493480
ldb, cleanup = newLDBStore(t)
481+
capacity = 10
494482
ldb.setCapacity(uint64(capacity))
495483

496-
n = 10
484+
n = 11
497485

498486
for i := 0; i < n; i++ {
499487
ldb.Put(context.TODO(), chunks[i])
@@ -504,6 +492,9 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
504492
<-chunks[i].dbStoredC
505493
}
506494

495+
// wait for garbage collection
496+
time.Sleep(1 * time.Second)
497+
507498
// expect for first chunk to be missing, because it has the smallest access value
508499
idx := 0
509500
ret, err := ldb.Get(context.TODO(), chunks[idx].Addr)

0 commit comments

Comments
 (0)