Fix Badger writecache race #765

Merged
fyrchik merged 1 commits from dstepanov-yadro/frostfs-node:fix/wc_badger_race into master 2023-10-31 10:01:28 +00:00
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. // Open opens and initializes database. Reads object counters from the ObjectCounters instance.
func (c *cache) Open(_ context.Context, readOnly bool) error { func (c *cache) Open(_ context.Context, readOnly bool) error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
err := c.openStore(readOnly) err := c.openStore(readOnly)
if err != nil { if err != nil {
return metaerr.Wrap(err) return metaerr.Wrap(err)
@ -94,6 +97,9 @@ func (c *cache) Open(_ context.Context, readOnly bool) error {
// Init runs necessary services. // Init runs necessary services.
func (c *cache) Init() error { func (c *cache) Init() error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
c.log.Info(logs.WritecacheBadgerInitExperimental) c.log.Info(logs.WritecacheBadgerInitExperimental)
c.metrics.SetMode(c.mode) c.metrics.SetMode(c.mode)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())

View File

@ -21,12 +21,20 @@ func (c *cache) runGCLoop(ctx context.Context) {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-t.C: case <-t.C:
c.runGC()
}
}
}()
}
func (c *cache) runGC() {
// This serves to synchronize the c.db field when changing mode as well. // This serves to synchronize the c.db field when changing mode as well.
c.modeMtx.RLock() c.modeMtx.RLock()
defer c.modeMtx.RUnlock()
ro := c.readOnly() ro := c.readOnly()
c.modeMtx.RUnlock()
if ro { if ro {
continue return
} }
// 0.5 is the recommended value so that write amplification of the value log is 2. // 0.5 is the recommended value so that write amplification of the value log is 2.
@ -35,6 +43,3 @@ func (c *cache) runGCLoop(ctx context.Context) {
c.log.Debug(logs.WritecacheDBValueLogGCRunCompleted) c.log.Debug(logs.WritecacheDBValueLogGCRunCompleted)
} }
} }
}
}()
}