[#642] writecache: Remove usage of close channel in badger

Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
Anton Nikiforov 2023-09-19 08:43:13 +03:00 committed by Evgenii Stratonikov
parent b0cf100427
commit c0b86f2d93
3 changed files with 30 additions and 34 deletions

View file

@ -26,12 +26,12 @@ type cache struct {
// helps to avoid multiple flushing of one object
scheduled4Flush map[oid.Address]struct{}
scheduled4FlushMtx sync.RWMutex
// closeCh is close channel, protected by modeMtx.
closeCh chan struct{}
// wg is a wait group for flush workers.
wg sync.WaitGroup
// store contains underlying database.
store
// cancel is cancel function, protected by modeMtx in Close.
cancel func()
}
// wcStorageType is used for write-cache operations logging.
@ -89,11 +89,6 @@ func (c *cache) Open(_ context.Context, readOnly bool) error {
if err != nil {
return metaerr.Wrap(err)
}
// Opening after Close is done during maintenance mode,
// thus we need to create a channel here.
c.closeCh = make(chan struct{})
return metaerr.Wrap(c.initCounters())
}
@ -101,8 +96,10 @@ func (c *cache) Open(_ context.Context, readOnly bool) error {
func (c *cache) Init() error {
c.log.Info(logs.WritecacheBadgerInitExperimental)
c.metrics.SetMode(c.mode)
c.runFlushLoop()
c.runGCLoop()
ctx, cancel := context.WithCancel(context.Background())
c.cancel = cancel
c.runFlushLoop(ctx)
c.runGCLoop(ctx)
return nil
}
@ -111,8 +108,9 @@ func (c *cache) Close() error {
// We cannot lock mutex for the whole operation duration
// because it is taken by some background workers, so `wg.Wait()` is done without modeMtx.
c.modeMtx.Lock()
if c.closeCh != nil {
close(c.closeCh)
if c.cancel != nil {
c.cancel()
c.cancel = nil
}
c.mode = mode.DegradedReadOnly // prevent new operations from being processed
c.modeMtx.Unlock()
@ -122,7 +120,6 @@ func (c *cache) Close() error {
c.modeMtx.Lock()
defer c.modeMtx.Unlock()
c.closeCh = nil
var err error
if c.db != nil {
err = c.db.Close()