Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// discardStats keeps track of the amount of data that could be discarded for
// a given logfile.
type discardStats struct {
sync.Mutex
sync.RWMutex

*z.MmapFile
opt Options
Expand Down Expand Up @@ -135,6 +135,8 @@ func (lf *discardStats) Update(fidu uint32, discard int64) int64 {
}

func (lf *discardStats) Iterate(f func(fid, stats uint64)) {
lf.RLock()
defer lf.RUnlock()
for slot := 0; slot < lf.nextEmptySlot; slot++ {
idx := 16 * slot
f(lf.get(idx), lf.get(idx+8))
Expand All @@ -143,8 +145,6 @@ func (lf *discardStats) Iterate(f func(fid, stats uint64)) {

// MaxDiscard returns the file id with maximum discard bytes.
func (lf *discardStats) MaxDiscard() (uint32, int64) {
lf.Lock()
defer lf.Unlock()

var maxFid, maxVal uint64
lf.Iterate(func(fid, val uint64) {
Expand Down
38 changes: 38 additions & 0 deletions discard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package badger

import (
"os"
"sync"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -64,3 +65,40 @@ func TestReloadDiscardStats(t *testing.T) {
require.Zero(t, ds2.Update(uint32(1), 0))
require.Equal(t, 1, int(ds2.Update(uint32(2), 0)))
}


func TestDiscardStats_ConcurrentRace(t *testing.T) {
dir, err := os.MkdirTemp("", "badger-test")
require.NoError(t, err)
defer removeDir(dir)

opt := DefaultOptions(dir)
db, err := Open(opt)
require.NoError(t, err)
defer db.Close()
ds := db.vlog.discardStats

var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(id uint32) {
defer wg.Done()
for j := 0; j < 100; j++ {
ds.Update(id, int64(j*10))
}
}(uint32(i))
}

for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 100; j++ {
ds.Iterate(func(id, val uint64) {})
ds.MaxDiscard()
}
}()
}
wg.Wait()
}

Loading