store_vacuum.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/stats"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. )
  8. func (s *Store) CheckCompactVolume(volumeId needle.VolumeId) (float64, error) {
  9. if v := s.findVolume(volumeId); v != nil {
  10. glog.V(3).Infof("volume %d garbage level: %f", volumeId, v.garbageLevel())
  11. return v.garbageLevel(), nil
  12. }
  13. return 0, fmt.Errorf("volume id %d is not found during check compact", volumeId)
  14. }
  15. func (s *Store) CompactVolume(vid needle.VolumeId, preallocate int64, compactionBytePerSecond int64, progressFn ProgressFunc) error {
  16. if v := s.findVolume(vid); v != nil {
  17. s := stats.NewDiskStatus(v.dir)
  18. if int64(s.Free) < preallocate {
  19. return fmt.Errorf("free space: %d bytes, not enough for %d bytes", s.Free, preallocate)
  20. }
  21. return v.Compact2(preallocate, compactionBytePerSecond, progressFn)
  22. }
  23. return fmt.Errorf("volume id %d is not found during compact", vid)
  24. }
  25. func (s *Store) CommitCompactVolume(vid needle.VolumeId) (bool, int64, error) {
  26. if s.isStopping {
  27. return false, 0, fmt.Errorf("volume id %d skips compact because volume is stopping", vid)
  28. }
  29. if v := s.findVolume(vid); v != nil {
  30. isReadOnly := v.IsReadOnly()
  31. err := v.CommitCompact()
  32. var volumeSize int64 = 0
  33. if err == nil && v.DataBackend != nil {
  34. volumeSize, _, _ = v.DataBackend.GetStat()
  35. }
  36. return isReadOnly, volumeSize, err
  37. }
  38. return false, 0, fmt.Errorf("volume id %d is not found during commit compact", vid)
  39. }
  40. func (s *Store) CommitCleanupVolume(vid needle.VolumeId) error {
  41. if v := s.findVolume(vid); v != nil {
  42. return v.cleanupCompact()
  43. }
  44. return fmt.Errorf("volume id %d is not found during cleaning up", vid)
  45. }