2019-03-23 18:33:34 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-04-19 07:39:34 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
2019-04-19 04:43:36 +00:00
|
|
|
|
2019-04-10 11:41:55 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-03-23 18:33:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage"
|
2019-04-19 04:43:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2019-03-23 18:33:34 +00:00
|
|
|
)
|
|
|
|
|
2019-04-18 05:04:49 +00:00
|
|
|
// VolumeCopy copy the .idx .dat files, and mount the volume
|
|
|
|
func (vs *VolumeServer) VolumeCopy(ctx context.Context, req *volume_server_pb.VolumeCopyRequest) (*volume_server_pb.VolumeCopyResponse, error) {
|
2019-03-23 18:33:34 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
2019-03-23 18:33:34 +00:00
|
|
|
if v != nil {
|
2019-04-20 18:35:20 +00:00
|
|
|
return nil, fmt.Errorf("volume %d already exists", req.VolumeId)
|
2019-03-23 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location := vs.store.FindFreeLocation()
|
|
|
|
if location == nil {
|
|
|
|
return nil, fmt.Errorf("no space left")
|
|
|
|
}
|
|
|
|
|
|
|
|
// the master will not start compaction for read-only volumes, so it is safe to just copy files directly
|
|
|
|
// copy .dat and .idx files
|
|
|
|
// read .idx .dat file size and timestamp
|
|
|
|
// send .idx file
|
|
|
|
// send .dat file
|
|
|
|
// confirm size and timestamp
|
2019-04-10 11:41:55 +00:00
|
|
|
var volFileInfoResp *volume_server_pb.ReadVolumeFileStatusResponse
|
2019-04-20 18:35:20 +00:00
|
|
|
var volumeFileName, idxFileName, datFileName string
|
2019-03-23 18:33:34 +00:00
|
|
|
err := operation.WithVolumeServerClient(req.SourceDataNode, vs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2019-04-10 11:41:55 +00:00
|
|
|
var err error
|
|
|
|
volFileInfoResp, err = client.ReadVolumeFileStatus(ctx,
|
|
|
|
&volume_server_pb.ReadVolumeFileStatusRequest{
|
|
|
|
VolumeId: req.VolumeId,
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
return fmt.Errorf("read volume file status failed, %v", err)
|
|
|
|
}
|
2019-03-23 18:33:34 +00:00
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
volumeFileName = storage.VolumeFileName(volFileInfoResp.Collection, location.Directory, int(req.VolumeId))
|
|
|
|
|
2019-04-19 19:29:49 +00:00
|
|
|
// println("source:", volFileInfoResp.String())
|
|
|
|
|
2019-03-23 18:33:34 +00:00
|
|
|
copyFileClient, err := client.CopyFile(ctx, &volume_server_pb.CopyFileRequest{
|
2019-04-19 19:29:49 +00:00
|
|
|
VolumeId: req.VolumeId,
|
|
|
|
IsIdxFile: true,
|
|
|
|
CompactionRevision: volFileInfoResp.CompactionRevision,
|
|
|
|
StopOffset: volFileInfoResp.IdxFileSize,
|
2019-03-23 18:33:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start copying volume %d idx file: %v", req.VolumeId, err)
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
idxFileName = volumeFileName + ".idx"
|
2019-04-10 11:41:55 +00:00
|
|
|
err = writeToFile(copyFileClient, idxFileName)
|
2019-03-23 18:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to copy volume %d idx file: %v", req.VolumeId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
copyFileClient, err = client.CopyFile(ctx, &volume_server_pb.CopyFileRequest{
|
2019-04-19 19:29:49 +00:00
|
|
|
VolumeId: req.VolumeId,
|
|
|
|
IsDatFile: true,
|
|
|
|
CompactionRevision: volFileInfoResp.CompactionRevision,
|
|
|
|
StopOffset: volFileInfoResp.DatFileSize,
|
2019-03-23 18:33:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start copying volume %d dat file: %v", req.VolumeId, err)
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:35:20 +00:00
|
|
|
datFileName = volumeFileName + ".dat"
|
2019-04-10 11:41:55 +00:00
|
|
|
err = writeToFile(copyFileClient, datFileName)
|
2019-03-23 18:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to copy volume %d dat file: %v", req.VolumeId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2019-04-20 18:35:20 +00:00
|
|
|
if err != nil && volumeFileName != "" {
|
|
|
|
if idxFileName != "" {
|
|
|
|
os.Remove(idxFileName)
|
|
|
|
}
|
|
|
|
if datFileName != "" {
|
|
|
|
os.Remove(datFileName)
|
|
|
|
}
|
2019-03-23 18:33:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-18 07:19:18 +00:00
|
|
|
if err = checkCopyFiles(volFileInfoResp, idxFileName, datFileName); err != nil { // added by panyc16
|
2019-04-10 11:41:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-23 18:33:34 +00:00
|
|
|
|
|
|
|
// mount the volume
|
2019-04-19 04:43:36 +00:00
|
|
|
err = vs.store.MountVolume(needle.VolumeId(req.VolumeId))
|
2019-03-23 18:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to mount volume %d: %v", req.VolumeId, err)
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:39:34 +00:00
|
|
|
return &volume_server_pb.VolumeCopyResponse{
|
2019-04-19 19:29:49 +00:00
|
|
|
LastAppendAtNs: volFileInfoResp.DatFileTimestampSeconds * uint64(time.Second),
|
2019-04-19 07:39:34 +00:00
|
|
|
}, err
|
2019-04-10 11:41:55 +00:00
|
|
|
}
|
2019-03-23 18:33:34 +00:00
|
|
|
|
2019-04-10 11:41:55 +00:00
|
|
|
/**
|
2019-04-18 07:19:18 +00:00
|
|
|
only check the the differ of the file size
|
|
|
|
todo: maybe should check the received count and deleted count of the volume
|
|
|
|
*/
|
2019-04-10 11:41:55 +00:00
|
|
|
func checkCopyFiles(originFileInf *volume_server_pb.ReadVolumeFileStatusResponse, idxFileName, datFileName string) error {
|
|
|
|
stat, err := os.Stat(idxFileName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get idx file info failed, %v", err)
|
|
|
|
}
|
|
|
|
if originFileInf.IdxFileSize != uint64(stat.Size()) {
|
|
|
|
return fmt.Errorf("the idx file size [%v] is not same as origin file size [%v]",
|
|
|
|
stat.Size(), originFileInf.IdxFileSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err = os.Stat(datFileName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get dat file info failed, %v", err)
|
|
|
|
}
|
|
|
|
if originFileInf.DatFileSize != uint64(stat.Size()) {
|
|
|
|
return fmt.Errorf("the dat file size [%v] is not same as origin file size [%v]",
|
|
|
|
stat.Size(), originFileInf.DatFileSize)
|
|
|
|
}
|
|
|
|
return nil
|
2019-03-23 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeToFile(client volume_server_pb.VolumeServer_CopyFileClient, fileName string) error {
|
2019-04-11 01:53:31 +00:00
|
|
|
glog.V(4).Infof("writing to %s", fileName)
|
2019-03-23 18:33:34 +00:00
|
|
|
dst, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, receiveErr := client.Recv()
|
|
|
|
if receiveErr == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if receiveErr != nil {
|
|
|
|
return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
|
|
|
|
}
|
|
|
|
dst.Write(resp.FileContent)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VolumeServer) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
|
|
|
|
resp := &volume_server_pb.ReadVolumeFileStatusResponse{}
|
2019-04-19 04:43:36 +00:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
2019-04-10 11:41:55 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.VolumeId = req.VolumeId
|
2019-04-19 07:39:34 +00:00
|
|
|
datSize, idxSize, modTime := v.FileStat()
|
|
|
|
resp.DatFileSize = datSize
|
|
|
|
resp.IdxFileSize = idxSize
|
|
|
|
resp.DatFileTimestampSeconds = uint64(modTime.Unix())
|
|
|
|
resp.IdxFileTimestampSeconds = uint64(modTime.Unix())
|
2019-04-11 06:39:53 +00:00
|
|
|
resp.FileCount = v.FileCount()
|
2019-04-19 19:29:49 +00:00
|
|
|
resp.CompactionRevision = uint32(v.CompactionRevision)
|
2019-04-20 18:35:20 +00:00
|
|
|
resp.Collection = v.Collection
|
2019-03-23 18:33:34 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:18:40 +00:00
|
|
|
func (vs *VolumeServer) CopyFile(req *volume_server_pb.CopyFileRequest, stream volume_server_pb.VolumeServer_CopyFileServer) error {
|
2019-03-23 18:33:34 +00:00
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
|
2019-03-23 18:33:34 +00:00
|
|
|
if v == nil {
|
|
|
|
return fmt.Errorf("not found volume id %d", req.VolumeId)
|
|
|
|
}
|
|
|
|
|
2019-04-19 19:29:49 +00:00
|
|
|
if uint32(v.CompactionRevision) != req.CompactionRevision {
|
|
|
|
return fmt.Errorf("volume %d is compacted", req.VolumeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesToRead := int64(req.StopOffset)
|
|
|
|
|
|
|
|
const BufferSize = 1024 * 1024 * 2
|
2019-03-23 18:33:34 +00:00
|
|
|
var fileName = v.FileName()
|
|
|
|
if req.IsDatFile {
|
|
|
|
fileName += ".dat"
|
|
|
|
} else if req.IsIdxFile {
|
|
|
|
fileName += ".idx"
|
|
|
|
}
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
buffer := make([]byte, BufferSize)
|
|
|
|
|
2019-04-19 19:29:49 +00:00
|
|
|
for bytesToRead > 0 {
|
2019-03-23 18:33:34 +00:00
|
|
|
bytesread, err := file.Read(buffer)
|
|
|
|
|
2019-04-19 19:29:49 +00:00
|
|
|
// println(fileName, "read", bytesread, "bytes, with target", bytesToRead)
|
|
|
|
|
2019-03-23 18:33:34 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-19 19:29:49 +00:00
|
|
|
// println(fileName, "read", bytesread, "bytes, with target", bytesToRead, "err", err.Error())
|
2019-03-23 18:33:34 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-04-19 19:29:49 +00:00
|
|
|
if int64(bytesread) > bytesToRead {
|
|
|
|
bytesread = int(bytesToRead)
|
|
|
|
}
|
|
|
|
err = stream.Send(&volume_server_pb.CopyFileResponse{
|
2019-03-23 18:33:34 +00:00
|
|
|
FileContent: buffer[:bytesread],
|
|
|
|
})
|
2019-04-19 19:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
// println("sending", bytesread, "bytes err", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesToRead -= int64(bytesread)
|
2019-03-23 18:33:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|