Skip to content

Commit

Permalink
cmd/mount: add cache-scan-interval option (#2692)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD authored Sep 8, 2022
1 parent eca9f9b commit 46bf918
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 100 deletions.
5 changes: 5 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func clientFlags() []cli.Flag {
Name: "cache-partial-only",
Usage: "cache only random/small read",
},
&cli.StringFlag{
Name: "cache-scan-interval",
Value: "3600",
Usage: "interval (in seconds) to scan cache-dir to rebuild in-memory index",
},
&cli.StringFlag{
Name: "backup-meta",
Value: "3600",
Expand Down
13 changes: 7 additions & 6 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@ func getChunkConf(c *cli.Context, format *meta.Format) *chunk.Config {
DownloadLimit: c.Int64("download-limit") * 1e6 / 8,
UploadDelay: duration(c.String("upload-delay")),

CacheDir: c.String("cache-dir"),
CacheSize: int64(c.Int("cache-size")),
FreeSpace: float32(c.Float64("free-space-ratio")),
CacheMode: os.FileMode(0600),
CacheFullBlock: !c.Bool("cache-partial-only"),
AutoCreate: true,
CacheDir: c.String("cache-dir"),
CacheSize: int64(c.Int("cache-size")),
FreeSpace: float32(c.Float64("free-space-ratio")),
CacheMode: os.FileMode(0600),
CacheFullBlock: !c.Bool("cache-partial-only"),
CacheScanInterval: duration(c.String("cache-scan-interval")),
AutoCreate: true,
}
if chunkConf.MaxUpload <= 0 {
logger.Warnf("max-uploads should be greater than 0, set it to 1")
Expand Down
43 changes: 22 additions & 21 deletions pkg/chunk/cached_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,27 +518,28 @@ func (s *wSlice) Abort() {

// Config contains options for cachedStore
type Config struct {
CacheDir string
CacheMode os.FileMode
CacheSize int64
FreeSpace float32
AutoCreate bool
Compress string
MaxUpload int
MaxDeletes int
MaxRetries int
UploadLimit int64 // bytes per second
DownloadLimit int64 // bytes per second
Writeback bool
UploadDelay time.Duration
HashPrefix bool
BlockSize int
GetTimeout time.Duration
PutTimeout time.Duration
CacheFullBlock bool
BufferSize int
Readahead int
Prefetch int
CacheDir string
CacheMode os.FileMode
CacheSize int64
CacheScanInterval time.Duration
FreeSpace float32
AutoCreate bool
Compress string
MaxUpload int
MaxDeletes int
MaxRetries int
UploadLimit int64 // bytes per second
DownloadLimit int64 // bytes per second
Writeback bool
UploadDelay time.Duration
HashPrefix bool
BlockSize int
GetTimeout time.Duration
PutTimeout time.Duration
CacheFullBlock bool
BufferSize int
Readahead int
Prefetch int
}

type cachedStore struct {
Expand Down
43 changes: 24 additions & 19 deletions pkg/chunk/disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ type pendingFile struct {
type cacheStore struct {
totalPages int64
sync.Mutex
dir string
mode os.FileMode
capacity int64
freeRatio float32
pending chan pendingFile
pages map[string]*Page
m *cacheManager
dir string
mode os.FileMode
capacity int64
freeRatio float32
scanInterval time.Duration
pending chan pendingFile
pages map[string]*Page
m *cacheManager

used int64
keys map[string]cacheItem
Expand All @@ -76,15 +77,16 @@ func newCacheStore(m *cacheManager, dir string, cacheSize int64, pendingPages in
config.FreeSpace = 0.1 // 10%
}
c := &cacheStore{
m: m,
dir: dir,
mode: config.CacheMode,
capacity: cacheSize,
freeRatio: config.FreeSpace,
keys: make(map[string]cacheItem),
pending: make(chan pendingFile, pendingPages),
pages: make(map[string]*Page),
uploader: uploader,
m: m,
dir: dir,
mode: config.CacheMode,
capacity: cacheSize,
freeRatio: config.FreeSpace,
scanInterval: config.CacheScanInterval,
keys: make(map[string]cacheItem),
pending: make(chan pendingFile, pendingPages),
pages: make(map[string]*Page),
uploader: uploader,
}
c.createDir(c.dir)
br, fr := c.curFreeRatio()
Expand Down Expand Up @@ -129,9 +131,12 @@ func (cache *cacheStore) checkFreeSpace() {
}

func (cache *cacheStore) refreshCacheKeys() {
for {
cache.scanCached()
time.Sleep(time.Minute * 5)
cache.scanCached()
if cache.scanInterval > 0 {
for {
time.Sleep(cache.scanInterval)
cache.scanCached()
}
}
}

Expand Down
110 changes: 56 additions & 54 deletions sdk/java/libjfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,40 +227,41 @@ func freeHandle(fd int) {
}

type javaConf struct {
MetaURL string `json:"meta"`
Bucket string `json:"bucket"`
ReadOnly bool `json:"readOnly"`
NoBGJob bool `json:"noBGJob"`
OpenCache float64 `json:"openCache"`
BackupMeta int64 `json:"backupMeta"`
Heartbeat int `json:"heartbeat"`
CacheDir string `json:"cacheDir"`
CacheSize int64 `json:"cacheSize"`
FreeSpace string `json:"freeSpace"`
AutoCreate bool `json:"autoCreate"`
CacheFullBlock bool `json:"cacheFullBlock"`
Writeback bool `json:"writeback"`
MemorySize int `json:"memorySize"`
Prefetch int `json:"prefetch"`
Readahead int `json:"readahead"`
UploadLimit int `json:"uploadLimit"`
DownloadLimit int `json:"downloadLimit"`
MaxUploads int `json:"maxUploads"`
MaxDeletes int `json:"maxDeletes"`
IORetries int `json:"ioRetries"`
GetTimeout int `json:"getTimeout"`
PutTimeout int `json:"putTimeout"`
FastResolve bool `json:"fastResolve"`
AttrTimeout float64 `json:"attrTimeout"`
EntryTimeout float64 `json:"entryTimeout"`
DirEntryTimeout float64 `json:"dirEntryTimeout"`
Debug bool `json:"debug"`
NoUsageReport bool `json:"noUsageReport"`
AccessLog string `json:"accessLog"`
PushGateway string `json:"pushGateway"`
PushInterval int `json:"pushInterval"`
PushAuth string `json:"pushAuth"`
PushGraphite string `json:"pushGraphite"`
MetaURL string `json:"meta"`
Bucket string `json:"bucket"`
ReadOnly bool `json:"readOnly"`
NoBGJob bool `json:"noBGJob"`
OpenCache float64 `json:"openCache"`
BackupMeta int64 `json:"backupMeta"`
Heartbeat int `json:"heartbeat"`
CacheDir string `json:"cacheDir"`
CacheSize int64 `json:"cacheSize"`
FreeSpace string `json:"freeSpace"`
AutoCreate bool `json:"autoCreate"`
CacheFullBlock bool `json:"cacheFullBlock"`
CacheScanInterval int `json:"cacheScanInterval"`
Writeback bool `json:"writeback"`
MemorySize int `json:"memorySize"`
Prefetch int `json:"prefetch"`
Readahead int `json:"readahead"`
UploadLimit int `json:"uploadLimit"`
DownloadLimit int `json:"downloadLimit"`
MaxUploads int `json:"maxUploads"`
MaxDeletes int `json:"maxDeletes"`
IORetries int `json:"ioRetries"`
GetTimeout int `json:"getTimeout"`
PutTimeout int `json:"putTimeout"`
FastResolve bool `json:"fastResolve"`
AttrTimeout float64 `json:"attrTimeout"`
EntryTimeout float64 `json:"entryTimeout"`
DirEntryTimeout float64 `json:"dirEntryTimeout"`
Debug bool `json:"debug"`
NoUsageReport bool `json:"noUsageReport"`
AccessLog string `json:"accessLog"`
PushGateway string `json:"pushGateway"`
PushInterval int `json:"pushInterval"`
PushAuth string `json:"pushAuth"`
PushGraphite string `json:"pushGraphite"`
}

func getOrCreate(name, user, group, superuser, supergroup string, f func() *fs.FileSystem) uintptr {
Expand Down Expand Up @@ -443,26 +444,27 @@ func jfs_init(cname, jsonConf, user, group, superuser, supergroup *C.char) uintp
freeSpaceRatio, _ = strconv.ParseFloat(jConf.FreeSpace, 64)
}
chunkConf := chunk.Config{
BlockSize: format.BlockSize * 1024,
Compress: format.Compression,
CacheDir: jConf.CacheDir,
CacheMode: 0644, // all user can read cache
CacheSize: jConf.CacheSize,
FreeSpace: float32(freeSpaceRatio),
AutoCreate: jConf.AutoCreate,
CacheFullBlock: jConf.CacheFullBlock,
MaxUpload: jConf.MaxUploads,
MaxDeletes: jConf.MaxDeletes,
MaxRetries: jConf.IORetries,
UploadLimit: int64(jConf.UploadLimit) * 1e6 / 8,
DownloadLimit: int64(jConf.DownloadLimit) * 1e6 / 8,
Prefetch: jConf.Prefetch,
Writeback: jConf.Writeback,
HashPrefix: format.HashPrefix,
GetTimeout: time.Second * time.Duration(jConf.GetTimeout),
PutTimeout: time.Second * time.Duration(jConf.PutTimeout),
BufferSize: jConf.MemorySize << 20,
Readahead: jConf.Readahead << 20,
BlockSize: format.BlockSize * 1024,
Compress: format.Compression,
CacheDir: jConf.CacheDir,
CacheMode: 0644, // all user can read cache
CacheSize: jConf.CacheSize,
FreeSpace: float32(freeSpaceRatio),
AutoCreate: jConf.AutoCreate,
CacheFullBlock: jConf.CacheFullBlock,
CacheScanInterval: time.Second * time.Duration(jConf.CacheScanInterval),
MaxUpload: jConf.MaxUploads,
MaxDeletes: jConf.MaxDeletes,
MaxRetries: jConf.IORetries,
UploadLimit: int64(jConf.UploadLimit) * 1e6 / 8,
DownloadLimit: int64(jConf.DownloadLimit) * 1e6 / 8,
Prefetch: jConf.Prefetch,
Writeback: jConf.Writeback,
HashPrefix: format.HashPrefix,
GetTimeout: time.Second * time.Duration(jConf.GetTimeout),
PutTimeout: time.Second * time.Duration(jConf.PutTimeout),
BufferSize: jConf.MemorySize << 20,
Readahead: jConf.Readahead << 20,
}
if chunkConf.CacheDir != "memory" {
ds := utils.SplitDir(chunkConf.CacheDir)
Expand Down
1 change: 1 addition & 0 deletions sdk/java/src/main/java/io/juicefs/JuiceFileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public void initialize(URI uri, Configuration conf) throws IOException {
obj.put("entryTimeout", Float.valueOf(getConf(conf, "entry-cache", "0.0")));
obj.put("dirEntryTimeout", Float.valueOf(getConf(conf, "dir-entry-cache", "0.0")));
obj.put("cacheFullBlock", Boolean.valueOf(getConf(conf, "cache-full-block", "true")));
obj.put("cacheScanInterval", Integer.valueOf(getConf(conf, "cache-scan-interval", "300")));
obj.put("metacache", Boolean.valueOf(getConf(conf, "metacache", "true")));
obj.put("autoCreate", Boolean.valueOf(getConf(conf, "auto-create-cache-dir", "true")));
obj.put("maxUploads", Integer.valueOf(getConf(conf, "max-uploads", "20")));
Expand Down

0 comments on commit 46bf918

Please sign in to comment.