Skip to content

Commit

Permalink
fix(reserve): reset binIDs as well (#4818)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Oct 1, 2024
1 parent 10b1a4d commit 19481d9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
25 changes: 20 additions & 5 deletions pkg/storer/internal/reserve/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,17 @@ func (r *Reserve) Reset(ctx context.Context) error {

size := r.Size()

// step 1: delete epoch timestamp
err := r.st.Run(ctx, func(s transaction.Store) error { return s.IndexStore().Delete(&EpochItem{}) })
if err != nil {
return err
}

bRitems := make([]*BatchRadiusItem, 0, size)
var eg errgroup.Group
eg.SetLimit(runtime.NumCPU())

// step 2: delete batchRadiusItem, chunkBinItem, and the chunk data
bRitems := make([]*BatchRadiusItem, 0, size)
err = r.st.IndexStore().Iterate(storage.Query{
Factory: func() storage.Item { return &BatchRadiusItem{} },
}, func(res storage.Result) (bool, error) {
Expand All @@ -534,10 +538,6 @@ func (r *Reserve) Reset(ctx context.Context) error {
if err != nil {
return err
}

var eg errgroup.Group
eg.SetLimit(runtime.NumCPU())

for _, item := range bRitems {
item := item
eg.Go(func() error {
Expand All @@ -557,6 +557,7 @@ func (r *Reserve) Reset(ctx context.Context) error {
}
bRitems = nil

// step 3: delete stampindex and chunkstamp
sitems := make([]*stampindex.Item, 0, size)
err = r.st.IndexStore().Iterate(storage.Query{
Factory: func() storage.Item { return &stampindex.Item{} },
Expand Down Expand Up @@ -585,6 +586,20 @@ func (r *Reserve) Reset(ctx context.Context) error {
}
sitems = nil

// step 4: delete binItems
err = r.st.Run(context.Background(), func(s transaction.Store) error {
for i := uint8(0); i < swarm.MaxBins; i++ {
err := s.IndexStore().Delete(&BinItem{Bin: i})
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}

r.size.Store(0)

return nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/storer/internal/reserve/reserve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ func TestReset(t *testing.T) {

checkStore(t, ts.IndexStore(), &reserve.EpochItem{}, false)

ids, _, err := r.LastBinIDs()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, ids[0], uint64(chunksPerBin))

err = r.Reset(context.Background())
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -855,6 +861,11 @@ func TestReset(t *testing.T) {

checkStore(t, ts.IndexStore(), &reserve.EpochItem{}, true)

_, _, err = r.LastBinIDs()
if !errors.Is(err, storage.ErrNotFound) {
t.Fatalf("wanted %v, got %v", storage.ErrNotFound, err)
}

for _, c := range chs {
h, err := c.Stamp().Hash()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/storer/migration/step_06.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func addStampHash(logger log.Logger, st transaction.Storage) (int64, int64, erro
}

if preBatchRadiusCnt != preChunkBinCnt {
return 0, 0, fmt.Errorf("pre-migration check: index counts do not match, %d vs %d. It's recommended that the repair-reserve cmd is run first", preBatchRadiusCnt, preChunkBinCnt)
return 0, 0, fmt.Errorf("pre-migration check: index counts do not match, %d vs %d", preBatchRadiusCnt, preChunkBinCnt)
}

// Delete epoch timestamp
Expand Down

0 comments on commit 19481d9

Please sign in to comment.