frostfs-node/pkg/local_object_storage/writecache/writecachebadger/gc.go
Dmitrii Stepanov 869518be0a
All checks were successful
Build / Build Components (1.21) (pull_request) Successful in 1m47s
DCO action / DCO (pull_request) Successful in 2m1s
Vulncheck / Vulncheck (pull_request) Successful in 3m5s
Build / Build Components (1.20) (pull_request) Successful in 3m58s
Tests and linters / Tests (1.20) (pull_request) Successful in 5m43s
Tests and linters / Staticcheck (pull_request) Successful in 5m49s
Tests and linters / Lint (pull_request) Successful in 6m20s
Tests and linters / Tests with -race (pull_request) Successful in 6m8s
Tests and linters / Tests (1.21) (pull_request) Successful in 3m6s
[#728] writecache: Fix Badger writecache race.
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-10-30 18:36:41 +03:00

45 lines
829 B
Go

package writecachebadger
import (
"context"
"time"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
)
func (c *cache) runGCLoop(ctx context.Context) {
c.wg.Add(1)
go func() {
defer c.wg.Done()
t := time.NewTicker(c.gcInterval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
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)
}
}