cache read performance improvements

This commit is contained in:
ston1th 2022-05-19 01:29:23 +02:00
commit f69d3149f3
14 changed files with 140 additions and 71 deletions

View file

@ -15,6 +15,7 @@ import (
type File struct {
log logr.Logger
f provider.File
cache provider.File
md *Metadata
offset int64
offline bool
@ -83,7 +84,7 @@ func (f *File) Preload(ctx context.Context, unlock func()) {
func (f *File) Read(p []byte) (n int, err error) {
log := f.log
if f.hasChunk(len(p)) {
n, err = f.md.ReadAt(p, f.offset)
n, err = f.cache.ReadAt(p, f.offset)
if err == io.EOF {
return
}
@ -172,8 +173,8 @@ func (f *File) Stat() (os.FileInfo, error) {
}
func (f *File) Close() error {
if f.offline {
return nil
if !f.offline && f.cache != nil {
f.cache.Close()
}
return f.f.Close()
}

View file

@ -135,6 +135,7 @@ func (fs *FS) OpenDst(name string) (f http.File, err error) {
f = &File{
log: log.WithName("file"),
f: file,
cache: file,
md: md,
offline: true,
}
@ -198,13 +199,25 @@ func (fs *FS) Open(name string) (f http.File, err error) {
f = &Dir{log: log.WithName("dir"), f: file, dc: fs.dc}
return
}
md := fs.mh.Metadata(name, fi.Size())
var cache provider.File
if offline {
log.V(2).Info("file offline mode")
cache = file
} else {
cache, err = fs.dst.Open(name)
if err != nil {
if !skipLog(name) {
log.Error(err, "error opening cache file")
}
return
}
}
md := fs.mh.Metadata(name, fi.Size())
f = &File{
log: log.WithName("file"),
f: file,
cache: cache,
md: md,
offline: offline,
}
@ -231,7 +244,7 @@ func (fs *FS) openCacheFile(name string, size int64) (df provider.File, err erro
log.Error(err, "error opening cache file")
return
}
if truncate {
if truncate && size > 0 {
err = df.Truncate(size)
if err != nil {
log.Error(err, "error truncating cache file")

View file

@ -389,16 +389,16 @@ func (md *Metadata) AddChunk(off int64, n int) {
md.Chunks.Add(off, n)
}
func (md *Metadata) ReadAt(p []byte, pos int64) (int, error) {
if md.f == nil {
err := md.openCacheFile()
if err != nil {
return 0, err
}
}
atomic.StoreInt64(&md.Atime, now())
return md.f.ReadAt(p, pos)
}
//func (md *Metadata) ReadAt(p []byte, pos int64) (int, error) {
// if md.f == nil {
// err := md.openCacheFile()
// if err != nil {
// return 0, err
// }
// }
// atomic.StoreInt64(&md.Atime, now())
// return md.f.ReadAt(p, pos)
//}
func (md *Metadata) WriteAt(data []byte, pos int64) (int, error) {
if md.f == nil {

View file

@ -22,6 +22,9 @@ func (q *queue) Add(ph *PreloadHandler, name string, prio int) {
for _, p := range *q {
if p.Name == name {
p.Prio += prio
if p.Prio < 0 {
p.Prio = 0
}
q.Sort()
return
}