2023-08-31 08:32:09 +00:00
|
|
|
package blobovniczatree
|
|
|
|
|
|
|
|
import (
|
2024-03-01 11:43:26 +00:00
|
|
|
"context"
|
2023-08-31 08:32:09 +00:00
|
|
|
"sync"
|
2024-03-01 11:43:26 +00:00
|
|
|
"time"
|
2023-08-31 08:32:09 +00:00
|
|
|
|
|
|
|
utilSync "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sync"
|
2024-03-01 11:43:26 +00:00
|
|
|
cache "github.com/go-pkgz/expirable-cache/v3"
|
2023-08-31 08:32:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// dbCache caches sharedDB instances that are NOT open for Put.
|
|
|
|
//
|
|
|
|
// Uses dbManager for opening/closing sharedDB instances.
|
2024-07-01 15:58:37 +00:00
|
|
|
// Stores a reference to a cached sharedDB, so dbManager does not close it.
|
2023-08-31 08:32:09 +00:00
|
|
|
type dbCache struct {
|
2024-03-01 11:43:26 +00:00
|
|
|
cacheGuard *sync.Mutex
|
|
|
|
cache cache.Cache[string, *sharedDB]
|
2023-09-22 10:07:32 +00:00
|
|
|
pathLock *utilSync.KeyLocker[string] // the order of locks is important: pathLock first, cacheGuard second
|
2023-08-31 08:32:09 +00:00
|
|
|
closed bool
|
2023-09-22 10:07:32 +00:00
|
|
|
nonCached map[string]struct{}
|
2024-03-01 11:43:26 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
cancel context.CancelFunc
|
2023-08-31 08:32:09 +00:00
|
|
|
|
|
|
|
dbManager *dbManager
|
|
|
|
}
|
|
|
|
|
2024-03-01 11:43:26 +00:00
|
|
|
func newDBCache(parentCtx context.Context, size int,
|
|
|
|
ttl time.Duration, expInterval time.Duration,
|
|
|
|
dbManager *dbManager,
|
|
|
|
) *dbCache {
|
|
|
|
ch := cache.NewCache[string, *sharedDB]().
|
|
|
|
WithTTL(ttl).WithLRU().WithMaxKeys(size).
|
|
|
|
WithOnEvicted(func(_ string, db *sharedDB) {
|
|
|
|
db.Close()
|
|
|
|
})
|
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
|
|
|
res := &dbCache{
|
|
|
|
cacheGuard: &sync.Mutex{},
|
|
|
|
wg: sync.WaitGroup{},
|
|
|
|
cancel: cancel,
|
|
|
|
cache: ch,
|
2023-08-31 08:32:09 +00:00
|
|
|
dbManager: dbManager,
|
|
|
|
pathLock: utilSync.NewKeyLocker[string](),
|
2023-09-22 10:07:32 +00:00
|
|
|
nonCached: make(map[string]struct{}),
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|
2024-03-01 11:43:26 +00:00
|
|
|
if ttl > 0 {
|
|
|
|
res.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(expInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
res.wg.Done()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
res.cacheGuard.Lock()
|
|
|
|
res.cache.DeleteExpired()
|
|
|
|
res.cacheGuard.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return res
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbCache) Open() {
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
|
|
|
|
|
|
|
c.closed = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbCache) Close() {
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
2024-03-01 11:43:26 +00:00
|
|
|
c.cancel()
|
|
|
|
c.wg.Wait()
|
2023-08-31 08:32:09 +00:00
|
|
|
c.cache.Purge()
|
|
|
|
c.closed = true
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:20:17 +00:00
|
|
|
func (c *dbCache) GetOrCreate(ctx context.Context, path string) *sharedDB {
|
2023-08-31 08:32:09 +00:00
|
|
|
value := c.getExisted(path)
|
|
|
|
if value != nil {
|
|
|
|
return value
|
|
|
|
}
|
2024-10-21 08:20:17 +00:00
|
|
|
return c.create(ctx, path)
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 10:07:32 +00:00
|
|
|
func (c *dbCache) EvictAndMarkNonCached(path string) {
|
|
|
|
c.pathLock.Lock(path)
|
|
|
|
defer c.pathLock.Unlock(path)
|
|
|
|
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
|
|
|
|
|
|
|
c.cache.Remove(path)
|
|
|
|
c.nonCached[path] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbCache) RemoveFromNonCached(path string) {
|
|
|
|
c.pathLock.Lock(path)
|
|
|
|
defer c.pathLock.Unlock(path)
|
|
|
|
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
|
|
|
|
|
|
|
delete(c.nonCached, path)
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:32:09 +00:00
|
|
|
func (c *dbCache) getExisted(path string) *sharedDB {
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
|
|
|
|
|
|
|
if value, ok := c.cache.Get(path); ok {
|
|
|
|
return value
|
2024-03-01 11:43:26 +00:00
|
|
|
} else if value != nil {
|
|
|
|
c.cache.Invalidate(path)
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:20:17 +00:00
|
|
|
func (c *dbCache) create(ctx context.Context, path string) *sharedDB {
|
2023-08-31 08:32:09 +00:00
|
|
|
c.pathLock.Lock(path)
|
|
|
|
defer c.pathLock.Unlock(path)
|
|
|
|
|
|
|
|
value := c.getExisted(path)
|
|
|
|
if value != nil {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
value = c.dbManager.GetByPath(path)
|
|
|
|
|
2024-10-21 08:20:17 +00:00
|
|
|
_, err := value.Open(ctx) // open db to hold reference, closed by evictedDB.Close() or if cache closed
|
2023-08-31 08:32:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
if added := c.put(path, value); !added {
|
|
|
|
value.Close()
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbCache) put(path string, db *sharedDB) bool {
|
|
|
|
c.cacheGuard.Lock()
|
|
|
|
defer c.cacheGuard.Unlock()
|
|
|
|
|
2023-09-22 10:07:32 +00:00
|
|
|
_, isNonCached := c.nonCached[path]
|
|
|
|
|
2024-03-01 11:43:26 +00:00
|
|
|
if isNonCached || c.closed {
|
|
|
|
return false
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|
2024-03-01 11:43:26 +00:00
|
|
|
c.cache.Add(path, db)
|
|
|
|
return true
|
2023-08-31 08:32:09 +00:00
|
|
|
}
|