From a1210d1e8d923aa2415a2f3aa91643b7e1686091 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 19 Feb 2021 03:56:27 -0800 Subject: [PATCH] Revert "volume: avoid file.stat(), file.seek() if possible during writes" This reverts commit c78409a5983145784d128c86541c6715dad3a937. --- weed/storage/backend/disk_file.go | 23 ++--------------------- weed/storage/needle_map.go | 16 ++++------------ 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/weed/storage/backend/disk_file.go b/weed/storage/backend/disk_file.go index 6a42ad8e7..2b04c8df2 100644 --- a/weed/storage/backend/disk_file.go +++ b/weed/storage/backend/disk_file.go @@ -12,8 +12,6 @@ var ( type DiskFile struct { File *os.File fullFilePath string - fileSize int64 - modTime time.Time } func NewDiskFile(f *os.File) *DiskFile { @@ -28,24 +26,11 @@ func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) { } func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) { - n, err = df.File.WriteAt(p, off) - if err == nil { - waterMark := off + int64(n) - if waterMark > df.fileSize { - df.fileSize = waterMark - df.modTime = time.Now() - } - } - return + return df.File.WriteAt(p, off) } func (df *DiskFile) Truncate(off int64) error { - err := df.File.Truncate(off) - if err == nil { - df.fileSize = off - df.modTime = time.Now() - } - return err + return df.File.Truncate(off) } func (df *DiskFile) Close() error { @@ -53,9 +38,6 @@ func (df *DiskFile) Close() error { } func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) { - if df.fileSize != 0 { - return df.fileSize, df.modTime, nil - } stat, e := df.File.Stat() if e == nil { return stat.Size(), stat.ModTime(), nil @@ -68,6 +50,5 @@ func (df *DiskFile) Name() string { } func (df *DiskFile) Sync() error { - df.fileSize = 0 return df.File.Sync() } diff --git a/weed/storage/needle_map.go b/weed/storage/needle_map.go index f12198c57..5b41286ea 100644 --- a/weed/storage/needle_map.go +++ b/weed/storage/needle_map.go @@ -41,7 +41,6 @@ type baseNeedleMapper struct { indexFile *os.File indexFileAccessLock sync.Mutex - indexFileOffset int64 } func (nm *baseNeedleMapper) IndexFileSize() uint64 { @@ -57,18 +56,11 @@ func (nm *baseNeedleMapper) appendToIndexFile(key NeedleId, offset Offset, size nm.indexFileAccessLock.Lock() defer nm.indexFileAccessLock.Unlock() - if nm.indexFileOffset == 0 { - if fileSize, err := nm.indexFile.Seek(0, 2); err != nil { - return fmt.Errorf("cannot seek end of indexfile %s: %v", - nm.indexFile.Name(), err) - } else { - nm.indexFileOffset = fileSize - } - } - written, err := nm.indexFile.WriteAt(bytes, nm.indexFileOffset) - if err == nil { - nm.indexFileOffset += int64(written) + if _, err := nm.indexFile.Seek(0, 2); err != nil { + return fmt.Errorf("cannot seek end of indexfile %s: %v", + nm.indexFile.Name(), err) } + _, err := nm.indexFile.Write(bytes) return err }