diff --git a/weed/storage/volume.go b/weed/storage/volume.go index 3b03b7c83..17917cea8 100644 --- a/weed/storage/volume.go +++ b/weed/storage/volume.go @@ -57,6 +57,13 @@ func (v *Volume) Version() Version { } func (v *Volume) Size() int64 { + v.dataFileAccessLock.Lock() + defer v.dataFileAccessLock.Unlock() + + if v.dataFile == nil { + return 0 + } + stat, e := v.dataFile.Stat() if e == nil { return stat.Size() @@ -69,8 +76,14 @@ func (v *Volume) Size() int64 { func (v *Volume) Close() { v.dataFileAccessLock.Lock() defer v.dataFileAccessLock.Unlock() - v.nm.Close() - _ = v.dataFile.Close() + if v.nm != nil { + v.nm.Close() + v.nm = nil + } + if v.dataFile != nil { + _ = v.dataFile.Close() + v.dataFile = nil + } } func (v *Volume) NeedToReplicate() bool { diff --git a/weed/storage/volume_read_write.go b/weed/storage/volume_read_write.go index 41f049674..54b0649b4 100644 --- a/weed/storage/volume_read_write.go +++ b/weed/storage/volume_read_write.go @@ -193,6 +193,7 @@ func ScanVolumeFile(dirname string, collection string, id VolumeId, if err = visitSuperBlock(v.SuperBlock); err != nil { return fmt.Errorf("Failed to process volume %d super block: %v", id, err) } + defer v.Close() version := v.Version() diff --git a/weed/storage/volume_vacuum.go b/weed/storage/volume_vacuum.go index 88df76251..fda568126 100644 --- a/weed/storage/volume_vacuum.go +++ b/weed/storage/volume_vacuum.go @@ -44,7 +44,10 @@ func (v *Volume) commitCompact() error { defer v.dataFileAccessLock.Unlock() glog.V(3).Infof("Got volume %d committing lock...", v.Id) v.nm.Close() - _ = v.dataFile.Close() + if err := v.dataFile.Close(); err != nil { + glog.V(0).Infof("fail to close volume %d", v.Id) + } + v.dataFile = nil var e error if e = v.makeupDiff(v.FileName()+".cpd", v.FileName()+".cpx", v.FileName()+".dat", v.FileName()+".idx"); e != nil { @@ -60,10 +63,10 @@ func (v *Volume) commitCompact() error { } else { var e error if e = os.Rename(v.FileName()+".cpd", v.FileName()+".dat"); e != nil { - return e + return fmt.Errorf("rename %s: %v", v.FileName()+".cpd", e) } if e = os.Rename(v.FileName()+".cpx", v.FileName()+".idx"); e != nil { - return e + return fmt.Errorf("rename %s: %v", v.FileName()+".cpx", e) } }