2019-11-27 19:09:42 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-12-03 07:08:28 +08:00
|
|
|
"time"
|
2019-11-27 19:09:42 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2019-11-27 19:09:42 +08:00
|
|
|
)
|
|
|
|
|
2019-12-26 01:53:13 +08:00
|
|
|
// VolumeTierMoveDatToRemote copy dat file to a remote tier
|
|
|
|
func (vs *VolumeServer) VolumeTierMoveDatToRemote(req *volume_server_pb.VolumeTierMoveDatToRemoteRequest, stream volume_server_pb.VolumeServer_VolumeTierMoveDatToRemoteServer) error {
|
2019-11-27 19:09:42 +08:00
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// find existing volume
|
2019-11-27 19:09:42 +08:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
|
|
|
if v == nil {
|
2019-12-03 07:08:28 +08:00
|
|
|
return fmt.Errorf("volume %d not found", req.VolumeId)
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// verify the collection
|
2019-11-27 19:09:42 +08:00
|
|
|
if v.Collection != req.Collection {
|
2019-12-03 07:08:28 +08:00
|
|
|
return fmt.Errorf("existing collection:%v unexpected input: %v", v.Collection, req.Collection)
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// locate the disk file
|
2019-11-27 19:09:42 +08:00
|
|
|
diskFile, ok := v.DataBackend.(*backend.DiskFile)
|
|
|
|
if !ok {
|
2022-05-01 11:03:04 +08:00
|
|
|
return nil // already copied to remove. fmt.Errorf("volume %d is not on local disk", req.VolumeId)
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// check valid storage backend type
|
|
|
|
backendStorage, found := backend.BackendStorages[req.DestinationBackendName]
|
|
|
|
if !found {
|
|
|
|
var keys []string
|
|
|
|
for key := range backend.BackendStorages {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("destination %s not found, suppported: %v", req.DestinationBackendName, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether the existing backend storage is the same as requested
|
|
|
|
// if same, skip
|
|
|
|
backendType, backendId := backend.BackendNameToTypeId(req.DestinationBackendName)
|
2019-12-29 03:21:49 +08:00
|
|
|
for _, remoteFile := range v.GetVolumeInfo().GetFiles() {
|
2019-12-03 07:08:28 +08:00
|
|
|
if remoteFile.BackendType == backendType && remoteFile.BackendId == backendId {
|
|
|
|
return fmt.Errorf("destination %s already exists", req.DestinationBackendName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
fn := func(progressed int64, percentage float32) error {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(startTime) < time.Second {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
startTime = now
|
2019-12-26 01:53:13 +08:00
|
|
|
return stream.Send(&volume_server_pb.VolumeTierMoveDatToRemoteResponse{
|
2019-12-03 07:08:28 +08:00
|
|
|
Processed: progressed,
|
|
|
|
ProcessedPercentage: percentage,
|
|
|
|
})
|
|
|
|
}
|
2019-12-26 13:37:21 +08:00
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// copy the data file
|
2021-10-30 03:39:19 +08:00
|
|
|
key, size, err := backendStorage.CopyFile(diskFile.File, fn)
|
2019-12-03 07:08:28 +08:00
|
|
|
if err != nil {
|
2019-12-09 11:44:16 +08:00
|
|
|
return fmt.Errorf("backend %s copy file %s: %v", req.DestinationBackendName, diskFile.Name(), err)
|
2019-12-03 07:08:28 +08:00
|
|
|
}
|
2019-11-27 19:09:42 +08:00
|
|
|
|
2019-12-03 07:08:28 +08:00
|
|
|
// save the remote file to volume tier info
|
2019-12-29 03:21:49 +08:00
|
|
|
v.GetVolumeInfo().Files = append(v.GetVolumeInfo().GetFiles(), &volume_server_pb.RemoteFile{
|
2019-12-03 07:08:28 +08:00
|
|
|
BackendType: backendType,
|
|
|
|
BackendId: backendId,
|
|
|
|
Key: key,
|
|
|
|
Offset: 0,
|
|
|
|
FileSize: uint64(size),
|
|
|
|
ModifiedTime: uint64(time.Now().Unix()),
|
2019-12-29 13:13:10 +08:00
|
|
|
Extension: ".dat",
|
2019-12-03 07:08:28 +08:00
|
|
|
})
|
|
|
|
|
2019-12-29 03:21:49 +08:00
|
|
|
if err := v.SaveVolumeInfo(); err != nil {
|
2019-12-03 07:08:28 +08:00
|
|
|
return fmt.Errorf("volume %d fail to save remote file info: %v", v.Id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.LoadRemoteFile(); err != nil {
|
|
|
|
return fmt.Errorf("volume %d fail to load remote file: %v", v.Id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.KeepLocalDatFile {
|
2020-11-27 19:17:10 +08:00
|
|
|
os.Remove(v.FileName(".dat"))
|
2019-12-03 07:08:28 +08:00
|
|
|
}
|
2019-11-27 19:09:42 +08:00
|
|
|
|
|
|
|
return nil
|
2019-11-28 04:34:57 +08:00
|
|
|
}
|