Skip to content

Commit

Permalink
Allow setting cache-item=0 to disable the restriction
Browse files Browse the repository at this point in the history
Signed-off-by: Changxin Miao <[email protected]>
  • Loading branch information
polyrabbit committed Jan 2, 2025
1 parent c211ca4 commit a62e17a
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ func dataCacheFlags() []cli.Flag {
},
&cli.Int64Flag{
Name: "cache-items",
Value: 1e9, // should be enough for most disks
Usage: "max number of cached items",
Value: 0,
Usage: "max number of cached items (0 for unlimited)",
},
&cli.Float64Flag{
Name: "free-space-ratio",
Expand Down
4 changes: 2 additions & 2 deletions pkg/chunk/cached_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ type Config struct {
func (c *Config) SelfCheck(uuid string) {
if !c.CacheEnabled() {
if c.Writeback || c.Prefetch > 0 {
logger.Warnf("cache-size or cache-items is 0, writeback and prefetch will be disabled")
logger.Warnf("cache-size is 0, writeback and prefetch will be disabled")
c.Writeback = false
c.Prefetch = 0
}
Expand Down Expand Up @@ -652,7 +652,7 @@ func (c *Config) parseHours() (start, end int, err error) {
}

func (c *Config) CacheEnabled() bool {
return c.CacheSize > 0 && c.CacheItems > 0
return c.CacheSize > 0
}

type cachedStore struct {
Expand Down
1 change: 0 additions & 1 deletion pkg/chunk/cached_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ var defaultConf = Config{
CacheDir: filepath.Join(os.TempDir(), "diskCache"),
CacheMode: 0600,
CacheSize: 10 << 20,
CacheItems: 1e9,
CacheChecksum: CsNone,
CacheScanInterval: time.Second * 300,
MaxUpload: 1,
Expand Down
12 changes: 8 additions & 4 deletions pkg/chunk/disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func (c *cacheStore) available() bool {
}

func (c *cacheStore) enabled() bool {
return c.capacity > 0 && c.maxItems > 0
return c.capacity > 0
}

func (c *cacheStore) full() bool {
return c.used > c.capacity || (c.maxItems != 0 && int64(len(c.pages)) > c.maxItems)
}

func (cache *cacheStore) checkErr(f func() error) error {
Expand Down Expand Up @@ -700,7 +704,7 @@ func (cache *cacheStore) add(key string, size int32, atime uint32) {
cache.used += int64(size + 4096)
}

if (cache.used > cache.capacity || int64(len(cache.keys)) > cache.maxItems) && cache.eviction != "none" {
if cache.full() && cache.eviction != "none" {
logger.Debugf("Cleanup cache when add new data (%s): %d blocks (%d MB)", cache.dir, len(cache.keys), cache.used>>20)
cache.cleanupFull()
}
Expand Down Expand Up @@ -746,7 +750,7 @@ func (cache *cacheStore) cleanupFull() {

goal := cache.capacity * 95 / 100
num := int64(len(cache.keys)) * 99 / 100
if num > cache.maxItems*99/100 {
if cache.maxItems != 0 && num > cache.maxItems * 99 / 100 {
num = cache.maxItems * 99 / 100
}
// make sure we have enough free space after cleanup
Expand All @@ -766,7 +770,7 @@ func (cache *cacheStore) cleanupFull() {
if toFree > len(cache.keys) {
num = 0
} else {
num = int64(len(cache.keys)-toFree) * 99 / 100
num = int64(len(cache.keys) - toFree) * 99 / 100
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/chunk/mem_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ func (c *memcache) cleanup() {
}

func (c *memcache) enabled() bool {
return c.capacity > 0 && c.maxItems > 0
return c.capacity > 0
}

func (c *memcache) full() bool {
return c.used > c.capacity || int64(len(c.pages)) > c.maxItems
return c.used > c.capacity || (c.maxItems != 0 && int64(len(c.pages)) > c.maxItems)
}

func (c *memcache) cleanupExpire() {
Expand Down
1 change: 0 additions & 1 deletion pkg/fuse/fuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func mount(url, mp string) {
MaxUpload: 20,
BufferSize: 300 << 20,
CacheSize: 1024,
CacheItems: 1e9,
CacheDir: "memory",
}

Expand Down
1 change: 0 additions & 1 deletion pkg/vfs/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestCompact(t *testing.T) {
MaxUpload: 2,
BufferSize: 30 << 20,
CacheSize: 10 << 20,
CacheItems: 1e9,
CacheDir: "memory",
}
blob, _ := object.CreateStorage("mem", "", "", "", "")
Expand Down
1 change: 0 additions & 1 deletion pkg/vfs/vfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func createTestVFS(applyMetaConfOption func(metaConfig *meta.Config), metaUri st
MaxUpload: 2,
BufferSize: 30 << 20,
CacheSize: 10 << 20,
CacheItems: 1e9,
CacheDir: "memory",
},
FuseOpts: &FuseOptions{},
Expand Down
2 changes: 1 addition & 1 deletion sdk/java/src/main/java/io/juicefs/JuiceFileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void initialize(URI uri, Configuration conf) throws IOException {
obj.put("noBGJob", Boolean.valueOf(getConf(conf, "no-bgjob", "false")));
obj.put("cacheDir", getConf(conf, "cache-dir", "memory"));
obj.put("cacheSize", getConf(conf, "cache-size", "100"));
obj.put("cacheItems", Integer.valueOf(getConf(conf, "cache-items", "1000000000")));
obj.put("cacheItems", Integer.valueOf(getConf(conf, "cache-items", "0")));
obj.put("openCache", getConf(conf, "open-cache", "0.0"));
obj.put("backupMeta", getConf(conf, "backup-meta", "3600"));
obj.put("backupSkipTrash", Boolean.valueOf(getConf(conf, "backup-skip-trash", "false")));
Expand Down

0 comments on commit a62e17a

Please sign in to comment.