2018-10-15 14:12:43 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-13 15:51:20 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-10-15 14:12:43 +08:00
|
|
|
|
2023-06-06 01:17:21 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
|
2022-07-29 14:59:33 +08:00
|
|
|
"github.com/prometheus/procfs"
|
2022-08-01 19:36:32 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2018-10-15 14:12:43 +08:00
|
|
|
)
|
|
|
|
|
2022-07-29 14:59:33 +08:00
|
|
|
var numCPU = runtime.NumCPU()
|
|
|
|
|
2018-10-15 14:12:43 +08:00
|
|
|
func (vs *VolumeServer) VacuumVolumeCheck(ctx context.Context, req *volume_server_pb.VacuumVolumeCheckRequest) (*volume_server_pb.VacuumVolumeCheckResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VacuumVolumeCheckResponse{}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
garbageRatio, err := vs.store.CheckCompactVolume(needle.VolumeId(req.VolumeId))
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
resp.GarbageRatio = garbageRatio
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-24 02:33:34 +08:00
|
|
|
glog.V(3).Infof("check volume %d: %v", req.VolumeId, err)
|
2018-10-15 14:12:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:55:34 +08:00
|
|
|
func (vs *VolumeServer) VacuumVolumeCompact(req *volume_server_pb.VacuumVolumeCompactRequest, stream volume_server_pb.VolumeServer_VacuumVolumeCompactServer) error {
|
2022-10-13 15:51:20 +08:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
stats.VolumeServerVacuumingHistogram.WithLabelValues("compact").Observe(time.Since(start).Seconds())
|
|
|
|
}(start)
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
resp := &volume_server_pb.VacuumVolumeCompactResponse{}
|
2021-11-03 14:39:16 +08:00
|
|
|
reportInterval := int64(1024 * 1024 * 128)
|
2021-10-24 16:55:34 +08:00
|
|
|
nextReportTarget := reportInterval
|
2022-07-29 14:59:33 +08:00
|
|
|
fs, fsErr := procfs.NewDefaultFS()
|
2021-10-24 16:55:34 +08:00
|
|
|
var sendErr error
|
|
|
|
err := vs.store.CompactVolume(needle.VolumeId(req.VolumeId), req.Preallocate, vs.compactionBytePerSecond, func(processed int64) bool {
|
|
|
|
if processed > nextReportTarget {
|
|
|
|
resp.ProcessedBytes = processed
|
2022-07-29 14:59:33 +08:00
|
|
|
if fsErr == nil && numCPU > 0 {
|
|
|
|
if fsLa, err := fs.LoadAvg(); err == nil {
|
2022-08-02 00:32:21 +08:00
|
|
|
resp.LoadAvg_1M = float32(fsLa.Load1 / float64(numCPU))
|
2022-07-29 14:59:33 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 16:55:34 +08:00
|
|
|
if sendErr = stream.Send(resp); sendErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
nextReportTarget = processed + reportInterval
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2018-10-15 14:12:43 +08:00
|
|
|
|
2022-10-13 15:51:20 +08:00
|
|
|
stats.VolumeServerVacuumingCompactCounter.WithLabelValues(strconv.FormatBool(err == nil && sendErr == nil)).Inc()
|
2018-10-15 14:12:43 +08:00
|
|
|
if err != nil {
|
2022-10-13 15:51:20 +08:00
|
|
|
glog.Errorf("failed compact volume %d: %v", req.VolumeId, err)
|
2021-10-24 16:55:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sendErr != nil {
|
2022-10-13 15:51:20 +08:00
|
|
|
glog.Errorf("failed compact volume %d report progress: %v", req.VolumeId, sendErr)
|
2021-10-24 16:55:34 +08:00
|
|
|
return sendErr
|
2018-10-15 14:12:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:55:34 +08:00
|
|
|
glog.V(1).Infof("compact volume %d", req.VolumeId)
|
|
|
|
return nil
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) VacuumVolumeCommit(ctx context.Context, req *volume_server_pb.VacuumVolumeCommitRequest) (*volume_server_pb.VacuumVolumeCommitResponse, error) {
|
2022-10-13 15:51:20 +08:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
stats.VolumeServerVacuumingHistogram.WithLabelValues("commit").Observe(time.Since(start).Seconds())
|
|
|
|
}(start)
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
resp := &volume_server_pb.VacuumVolumeCommitResponse{}
|
|
|
|
|
2023-06-06 01:17:21 +08:00
|
|
|
readOnly, volumeSize, err := vs.store.CommitCompactVolume(needle.VolumeId(req.VolumeId))
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2022-10-13 15:51:20 +08:00
|
|
|
glog.Errorf("failed commit volume %d: %v", req.VolumeId, err)
|
2018-10-15 14:12:43 +08:00
|
|
|
} else {
|
2019-03-24 02:33:34 +08:00
|
|
|
glog.V(1).Infof("commit volume %d", req.VolumeId)
|
2018-10-15 14:12:43 +08:00
|
|
|
}
|
2022-10-13 15:51:20 +08:00
|
|
|
stats.VolumeServerVacuumingCommitCounter.WithLabelValues(strconv.FormatBool(err == nil)).Inc()
|
2021-05-05 17:54:50 +08:00
|
|
|
resp.IsReadOnly = readOnly
|
2023-06-06 01:17:21 +08:00
|
|
|
resp.VolumeSize = uint64(volumeSize)
|
2018-10-15 14:12:43 +08:00
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) VacuumVolumeCleanup(ctx context.Context, req *volume_server_pb.VacuumVolumeCleanupRequest) (*volume_server_pb.VacuumVolumeCleanupResponse, error) {
|
|
|
|
|
|
|
|
resp := &volume_server_pb.VacuumVolumeCleanupResponse{}
|
|
|
|
|
2019-04-19 12:43:36 +08:00
|
|
|
err := vs.store.CommitCleanupVolume(needle.VolumeId(req.VolumeId))
|
2018-10-15 14:12:43 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2022-10-13 15:51:20 +08:00
|
|
|
glog.Errorf("failed cleanup volume %d: %v", req.VolumeId, err)
|
2018-10-15 14:12:43 +08:00
|
|
|
} else {
|
2019-03-24 02:33:34 +08:00
|
|
|
glog.V(1).Infof("cleanup volume %d", req.VolumeId)
|
2018-10-15 14:12:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
|
|
}
|