preallocate disk space during compaction also, add cleanup for failed compaction
This commit is contained in:
@@ -22,13 +22,13 @@ func (s *Store) CheckCompactVolume(volumeIdString string, garbageThresholdString
|
||||
}
|
||||
return fmt.Errorf("volume id %d is not found during check compact", vid), false
|
||||
}
|
||||
func (s *Store) CompactVolume(volumeIdString string) error {
|
||||
func (s *Store) CompactVolume(volumeIdString string, preallocate int64) error {
|
||||
vid, err := NewVolumeId(volumeIdString)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Volume Id %s is not a valid unsigned integer", volumeIdString)
|
||||
}
|
||||
if v := s.findVolume(vid); v != nil {
|
||||
return v.Compact()
|
||||
return v.Compact(preallocate)
|
||||
}
|
||||
return fmt.Errorf("volume id %d is not found during compact", vid)
|
||||
}
|
||||
@@ -42,3 +42,13 @@ func (s *Store) CommitCompactVolume(volumeIdString string) error {
|
||||
}
|
||||
return fmt.Errorf("volume id %d is not found during commit compact", vid)
|
||||
}
|
||||
func (s *Store) CommitCleanupVolume(volumeIdString string) error {
|
||||
vid, err := NewVolumeId(volumeIdString)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Volume Id %s is not a valid unsigned integer", volumeIdString)
|
||||
}
|
||||
if v := s.findVolume(vid); v != nil {
|
||||
return v.cleanupCompact()
|
||||
}
|
||||
return fmt.Errorf("volume id %d is not found during cleaning up", vid)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (v *Volume) Synchronize(volumeServer string) (err error) {
|
||||
return fmt.Errorf("Failed to sync volume %d entries with %s: %v", v.Id, volumeServer, err)
|
||||
}
|
||||
if lastCompactRevision != compactRevision && lastCompactRevision != 0 {
|
||||
if err = v.Compact(); err != nil {
|
||||
if err = v.Compact(0); err != nil {
|
||||
return fmt.Errorf("Compact Volume before synchronizing %v", err)
|
||||
}
|
||||
if err = v.commitCompact(); err != nil {
|
||||
|
||||
@@ -13,7 +13,7 @@ func (v *Volume) garbageLevel() float64 {
|
||||
return float64(v.nm.DeletedSize()) / float64(v.ContentSize())
|
||||
}
|
||||
|
||||
func (v *Volume) Compact() error {
|
||||
func (v *Volume) Compact(preallocate int64) error {
|
||||
glog.V(3).Infof("Compacting ...")
|
||||
//no need to lock for copy on write
|
||||
//v.accessLock.Lock()
|
||||
@@ -24,7 +24,7 @@ func (v *Volume) Compact() error {
|
||||
v.lastCompactIndexOffset = v.nm.IndexFileSize()
|
||||
v.lastCompactRevision = v.SuperBlock.CompactRevision
|
||||
glog.V(3).Infof("creating copies for volume %d ,last offset %d...", v.Id, v.lastCompactIndexOffset)
|
||||
return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx", 0)
|
||||
return v.copyDataAndGenerateIndexFile(filePath+".cpd", filePath+".cpx", preallocate)
|
||||
}
|
||||
|
||||
func (v *Volume) Compact2() error {
|
||||
@@ -72,6 +72,20 @@ func (v *Volume) commitCompact() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Volume) cleanupCompact() error {
|
||||
glog.V(0).Infof("Cleaning up vacuuming...")
|
||||
|
||||
e1 := os.Remove(v.FileName() + ".cpd")
|
||||
e2 := os.Remove(v.FileName() + ".cpx")
|
||||
if e1 != nil {
|
||||
return e1
|
||||
}
|
||||
if e2 != nil {
|
||||
return e2
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchCompactRevisionFromDatFile(file *os.File) (compactRevision uint16, err error) {
|
||||
if _, err = file.Seek(0, 0); err != nil {
|
||||
return 0, fmt.Errorf("cannot seek to the beginning of %s: %v", file.Name(), err)
|
||||
|
||||
Reference in New Issue
Block a user