[#728] writecache: Fix Badger writecache race.
Build / Build Components (1.21) (pull_request) Successful in 1m47s Details
DCO action / DCO (pull_request) Successful in 2m1s Details
Vulncheck / Vulncheck (pull_request) Successful in 3m5s Details
Build / Build Components (1.20) (pull_request) Successful in 3m58s Details
Tests and linters / Tests (1.20) (pull_request) Successful in 5m43s Details
Tests and linters / Staticcheck (pull_request) Successful in 5m49s Details
Tests and linters / Lint (pull_request) Successful in 6m20s Details
Tests and linters / Tests with -race (pull_request) Successful in 6m8s Details
Tests and linters / Tests (1.21) (pull_request) Successful in 3m6s Details

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
pull/765/head
Dmitrii Stepanov 2023-10-30 17:11:04 +03:00
parent d4b6ebe7e7
commit 869518be0a
2 changed files with 24 additions and 13 deletions

View File

@ -85,6 +85,9 @@ func (c *cache) DumpInfo() writecache.Info {
// Open opens and initializes database. Reads object counters from the ObjectCounters instance.
func (c *cache) Open(_ context.Context, readOnly bool) error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
err := c.openStore(readOnly)
if err != nil {
return metaerr.Wrap(err)
@ -94,6 +97,9 @@ func (c *cache) Open(_ context.Context, readOnly bool) error {
// Init runs necessary services.
func (c *cache) Init() error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
c.log.Info(logs.WritecacheBadgerInitExperimental)
c.metrics.SetMode(c.mode)
ctx, cancel := context.WithCancel(context.Background())

View File

@ -21,20 +21,25 @@ func (c *cache) runGCLoop(ctx context.Context) {
case <-ctx.Done():
return
case <-t.C:
// This serves to synchronize the c.db field when changing mode as well.
c.modeMtx.RLock()
ro := c.readOnly()
c.modeMtx.RUnlock()
if ro {
continue
}
// 0.5 is the recommended value so that write amplification of the value log is 2.
// See https://pkg.go.dev/github.com/dgraph-io/badger/v4#DB.RunValueLogGC for more info.
for c.db.RunValueLogGC(0.5) == nil {
c.log.Debug(logs.WritecacheDBValueLogGCRunCompleted)
}
c.runGC()
}
}
}()
}
func (c *cache) runGC() {
// This serves to synchronize the c.db field when changing mode as well.
c.modeMtx.RLock()
defer c.modeMtx.RUnlock()
ro := c.readOnly()
if ro {
return
}
// 0.5 is the recommended value so that write amplification of the value log is 2.
// See https://pkg.go.dev/github.com/dgraph-io/badger/v4#DB.RunValueLogGC for more info.
for c.db.RunValueLogGC(0.5) == nil {
c.log.Debug(logs.WritecacheDBValueLogGCRunCompleted)
}
}