|
|
@@ -728,15 +728,14 @@ type CacheFileHandle struct { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
log := logger.WithField("F", "CacheFileHandle.ReadAt") |
|
|
|
|
|
|
|
|
log := logger.WithField("F", "CacheFileHandle.ReadAt"). |
|
|
|
|
|
WithField("Path", h.file.pathComps) |
|
|
log.Tracef("buf: %v, off: %v", len(buf), off) |
|
|
log.Tracef("buf: %v, off: %v", len(buf), off) |
|
|
|
|
|
|
|
|
if !h.readable { |
|
|
if !h.readable { |
|
|
return 0, fuse.ErrPermission |
|
|
return 0, fuse.ErrPermission |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.Tracef("CacheFileReadWriter.ReadAt: %v, %v, %v", h.file.pathComps, off, len(buf)) |
|
|
|
|
|
|
|
|
|
|
|
// 读取数据必须读满整个buf,否则就会被认为是文件已经结束了 |
|
|
// 读取数据必须读满整个buf,否则就会被认为是文件已经结束了 |
|
|
totalReadLen := 0 |
|
|
totalReadLen := 0 |
|
|
for totalReadLen < len(buf) { |
|
|
for totalReadLen < len(buf) { |
|
|
@@ -758,8 +757,8 @@ func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
totalReadLen += realReadLen |
|
|
totalReadLen += realReadLen |
|
|
h.file.rwLock.RUnlock() |
|
|
h.file.rwLock.RUnlock() |
|
|
|
|
|
|
|
|
logger.Tracef("read from local cache, n: %v, err: %v", realReadLen, err) |
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
log.Tracef("read from local cache: %v", err) |
|
|
return totalReadLen, err |
|
|
return totalReadLen, err |
|
|
} |
|
|
} |
|
|
continue |
|
|
continue |
|
|
@@ -777,6 +776,7 @@ func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
/// 2. 开始从远端下载数据 |
|
|
/// 2. 开始从远端下载数据 |
|
|
|
|
|
|
|
|
if h.remote == nil { |
|
|
if h.remote == nil { |
|
|
|
|
|
log.Warnf("no remote file") |
|
|
return totalReadLen, fmt.Errorf("no remote file") |
|
|
return totalReadLen, fmt.Errorf("no remote file") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -790,7 +790,7 @@ func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
return totalReadLen, err |
|
|
return totalReadLen, err |
|
|
} |
|
|
} |
|
|
h.remoteLock.Unlock() |
|
|
h.remoteLock.Unlock() |
|
|
logger.Tracef("load from remote: %v", realLoadLen) |
|
|
|
|
|
|
|
|
log.Tracef("load from remote: %v", realLoadLen) |
|
|
|
|
|
|
|
|
/// 3. 数据加载完毕,写入到本地文件 |
|
|
/// 3. 数据加载完毕,写入到本地文件 |
|
|
|
|
|
|
|
|
@@ -814,11 +814,11 @@ func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
h.file.writeLock.Unlock() |
|
|
h.file.writeLock.Unlock() |
|
|
|
|
|
|
|
|
logger.Tracef("save to local file: %v", err) |
|
|
|
|
|
return totalReadLen, fmt.Errorf("save to local file: %w", err) |
|
|
|
|
|
|
|
|
log.Warnf("save to local file: %v", err) |
|
|
|
|
|
return totalReadLen, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.Tracef("save to local: %v", loadRng.Length) |
|
|
|
|
|
|
|
|
log.Tracef("save to local: %v", loadRng.Length) |
|
|
|
|
|
|
|
|
h.file.writeLock.Unlock() |
|
|
h.file.writeLock.Unlock() |
|
|
|
|
|
|
|
|
@@ -833,15 +833,13 @@ func (h *CacheFileHandle) ReadAt(buf []byte, off int64) (int, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (h *CacheFileHandle) WriteAt(buf []byte, off int64) (int, error) { |
|
|
func (h *CacheFileHandle) WriteAt(buf []byte, off int64) (int, error) { |
|
|
log := logger.WithField("F", "CacheFileHandle.WriteAt") |
|
|
|
|
|
|
|
|
log := logger.WithField("F", "CacheFileHandle.WriteAt").WithField("Path", h.file.pathComps) |
|
|
log.Tracef("buf: %v, off: %v", len(buf), off) |
|
|
log.Tracef("buf: %v, off: %v", len(buf), off) |
|
|
|
|
|
|
|
|
if !h.writeable { |
|
|
if !h.writeable { |
|
|
return 0, fuse.ErrPermission |
|
|
return 0, fuse.ErrPermission |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logger.Tracef("CacheFileReadWriter.WriteAt: %v, %v, %v", h.file.pathComps, off, len(buf)) |
|
|
|
|
|
|
|
|
|
|
|
// 允许多线程并行写入,但在数据加载期间不能写入 |
|
|
// 允许多线程并行写入,但在数据加载期间不能写入 |
|
|
h.file.writeLock.RLock() |
|
|
h.file.writeLock.RLock() |
|
|
defer h.file.writeLock.RUnlock() |
|
|
defer h.file.writeLock.RUnlock() |
|
|
@@ -849,7 +847,8 @@ func (h *CacheFileHandle) WriteAt(buf []byte, off int64) (int, error) { |
|
|
// 写入到本地缓存文件 |
|
|
// 写入到本地缓存文件 |
|
|
writeLen, err := h.file.dataFile.WriteAt(buf, off) |
|
|
writeLen, err := h.file.dataFile.WriteAt(buf, off) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return writeLen, fmt.Errorf("save to local file: %w", err) |
|
|
|
|
|
|
|
|
log.Tracef("save to local file: %v", err) |
|
|
|
|
|
return writeLen, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 提交到段列表里 |
|
|
// 提交到段列表里 |
|
|
@@ -866,7 +865,15 @@ func (h *CacheFileHandle) WriteAt(buf []byte, off int64) (int, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (f *CacheFileHandle) Sync() error { |
|
|
func (f *CacheFileHandle) Sync() error { |
|
|
return f.file.dataFile.Sync() |
|
|
|
|
|
|
|
|
log := logger.WithField("Path", f.file.pathComps) |
|
|
|
|
|
|
|
|
|
|
|
err := f.file.dataFile.Sync() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Tracef("sync local file: %v", err) |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (f *CacheFileHandle) Close() error { |
|
|
func (f *CacheFileHandle) Close() error { |
|
|
|