You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

2 years ago
2 years ago
2 years ago
6 years ago
3 years ago
2 years ago
2 years ago
2 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "github.com/prometheus/procfs"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "runtime"
  9. )
  10. var numCPU = runtime.NumCPU()
  11. func (vs *VolumeServer) VacuumVolumeCheck(ctx context.Context, req *volume_server_pb.VacuumVolumeCheckRequest) (*volume_server_pb.VacuumVolumeCheckResponse, error) {
  12. resp := &volume_server_pb.VacuumVolumeCheckResponse{}
  13. garbageRatio, err := vs.store.CheckCompactVolume(needle.VolumeId(req.VolumeId))
  14. resp.GarbageRatio = garbageRatio
  15. if err != nil {
  16. glog.V(3).Infof("check volume %d: %v", req.VolumeId, err)
  17. }
  18. return resp, err
  19. }
  20. func (vs *VolumeServer) VacuumVolumeCompact(req *volume_server_pb.VacuumVolumeCompactRequest, stream volume_server_pb.VolumeServer_VacuumVolumeCompactServer) error {
  21. resp := &volume_server_pb.VacuumVolumeCompactResponse{}
  22. reportInterval := int64(1024 * 1024 * 128)
  23. nextReportTarget := reportInterval
  24. fs, fsErr := procfs.NewDefaultFS()
  25. var sendErr error
  26. err := vs.store.CompactVolume(needle.VolumeId(req.VolumeId), req.Preallocate, vs.compactionBytePerSecond, func(processed int64) bool {
  27. if processed > nextReportTarget {
  28. resp.ProcessedBytes = processed
  29. if fsErr == nil && numCPU > 0 {
  30. if fsLa, err := fs.LoadAvg(); err == nil {
  31. resp.LoadAvg_1M = float32(fsLa.Load1 / float64(numCPU))
  32. }
  33. }
  34. if sendErr = stream.Send(resp); sendErr != nil {
  35. return false
  36. }
  37. nextReportTarget = processed + reportInterval
  38. }
  39. return true
  40. })
  41. if err != nil {
  42. glog.Errorf("compact volume %d: %v", req.VolumeId, err)
  43. return err
  44. }
  45. if sendErr != nil {
  46. glog.Errorf("compact volume %d report progress: %v", req.VolumeId, sendErr)
  47. return sendErr
  48. }
  49. glog.V(1).Infof("compact volume %d", req.VolumeId)
  50. return nil
  51. }
  52. func (vs *VolumeServer) VacuumVolumeCommit(ctx context.Context, req *volume_server_pb.VacuumVolumeCommitRequest) (*volume_server_pb.VacuumVolumeCommitResponse, error) {
  53. resp := &volume_server_pb.VacuumVolumeCommitResponse{}
  54. readOnly, err := vs.store.CommitCompactVolume(needle.VolumeId(req.VolumeId))
  55. if err != nil {
  56. glog.Errorf("commit volume %d: %v", req.VolumeId, err)
  57. } else {
  58. glog.V(1).Infof("commit volume %d", req.VolumeId)
  59. }
  60. resp.IsReadOnly = readOnly
  61. return resp, err
  62. }
  63. func (vs *VolumeServer) VacuumVolumeCleanup(ctx context.Context, req *volume_server_pb.VacuumVolumeCleanupRequest) (*volume_server_pb.VacuumVolumeCleanupResponse, error) {
  64. resp := &volume_server_pb.VacuumVolumeCleanupResponse{}
  65. err := vs.store.CommitCleanupVolume(needle.VolumeId(req.VolumeId))
  66. if err != nil {
  67. glog.Errorf("cleanup volume %d: %v", req.VolumeId, err)
  68. } else {
  69. glog.V(1).Infof("cleanup volume %d", req.VolumeId)
  70. }
  71. return resp, err
  72. }