Skip to content

Commit

Permalink
check if a cache file exists to avoid opening and closing it
Browse files Browse the repository at this point in the history
  • Loading branch information
daimashusheng committed Dec 23, 2024
1 parent f5fcaae commit e993062
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
8 changes: 2 additions & 6 deletions pkg/chunk/cached_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,7 @@ func (store *cachedStore) FillCache(id uint64, length uint32) error {
keys := r.keys()
var err error
for _, k := range keys {
f, e := store.bcache.load(k)
if e == nil { // already cached
_ = f.Close()
if store.bcache.exist(k) { // already cached
continue
}
size := parseObjOrigSize(k)
Expand Down Expand Up @@ -1134,9 +1132,7 @@ func (store *cachedStore) CheckCache(id uint64, length uint32) (uint64, error) {
keys := r.keys()
missBytes := uint64(0)
for i, k := range keys {
tmpReader, err := store.bcache.load(k)
if err == nil {
_ = tmpReader.Close()
if store.bcache.exist(k) {
continue
}
missBytes += uint64(r.blockSize(i))
Expand Down
49 changes: 49 additions & 0 deletions pkg/chunk/disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,39 @@ func (cache *cacheStore) load(key string) (ReadCloser, error) {
return f, err
}

func (cache *cacheStore) exist(key string) (bool, error) {
cache.Lock()
defer cache.Unlock()
if _, ok := cache.pages[key]; ok {
return true, nil
}
k := cache.getCacheKey(key)
if cache.scanned && cache.keys[k].atime == 0 {
return false, errNotCached
}
cache.Unlock()
var err error
err = cache.checkErr(func() error {
_, err = os.Stat(cache.cachePath(key))
return err
})

cache.Lock()
if err == nil {
if it, ok := cache.keys[k]; ok {
// update atime
cache.keys[k] = cacheItem{it.size, uint32(time.Now().Unix())}
}
return true, nil
} else if it, ok := cache.keys[k]; ok {
if it.size > 0 {
cache.used -= int64(it.size + 4096)
}
delete(cache.keys, k)
}
return false, err
}

func (cache *cacheStore) cachePath(key string) string {
return filepath.Join(cache.dir, cacheDir, key)
}
Expand Down Expand Up @@ -991,6 +1024,7 @@ type CacheManager interface {
cache(key string, p *Page, force, dropCache bool)
remove(key string, staging bool)
load(key string) (ReadCloser, error)
exist(key string) bool
uploaded(key string, size int)
stage(key string, data []byte, keepCache bool) (string, error)
removeStage(key string) error
Expand Down Expand Up @@ -1170,6 +1204,21 @@ func (m *cacheManager) load(key string) (ReadCloser, error) {
return r, err
}

func (m *cacheManager) exist(key string) bool {
store := m.getStore(key)
if store == nil {
return false
}
exited, err := m.getStore(key).exist(key)
if err == errNotCached {
legacy := m.getStoreLegacy(key)
if legacy != store && legacy != nil {
exited, _ = legacy.exist(key)
}
}
return exited
}

func (m *cacheManager) remove(key string, staging bool) {
store := m.getStore(key)
if store != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/chunk/disk_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ func TestCacheManager(t *testing.T) {

rc, _ := m.load(k1)
require.Nil(t, rc)
exist := m.exist(k1)
require.False(t, exist)

s2 := m.getStore(k1)
require.NotNil(t, s2)
Expand Down
13 changes: 13 additions & 0 deletions pkg/chunk/mem_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ func (c *memcache) load(key string) (ReadCloser, error) {
return nil, errors.New("not found")
}

func (c *memcache) exist(key string) bool {
if c.capacity == 0 {
return false
}
c.Lock()
defer c.Unlock()
if item, ok := c.pages[key]; ok {
c.pages[key] = memItem{time.Now(), item.page}
return true
}
return false
}

// locked
func (c *memcache) cleanup() {
var cnt int
Expand Down

0 comments on commit e993062

Please sign in to comment.