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.

232 lines
7.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. "os"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. // VolumeCopy copy the .idx .dat files, and mount the volume
  17. func (vs *VolumeServer) VolumeCopy(ctx context.Context, req *volume_server_pb.VolumeCopyRequest) (*volume_server_pb.VolumeCopyResponse, error) {
  18. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  19. if v != nil {
  20. return nil, fmt.Errorf("volume %d already exists", req.VolumeId)
  21. }
  22. location := vs.store.FindFreeLocation()
  23. if location == nil {
  24. return nil, fmt.Errorf("no space left")
  25. }
  26. // the master will not start compaction for read-only volumes, so it is safe to just copy files directly
  27. // copy .dat and .idx files
  28. // read .idx .dat file size and timestamp
  29. // send .idx file
  30. // send .dat file
  31. // confirm size and timestamp
  32. var volFileInfoResp *volume_server_pb.ReadVolumeFileStatusResponse
  33. var volumeFileName, idxFileName, datFileName string
  34. err := operation.WithVolumeServerClient(req.SourceDataNode, vs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  35. var err error
  36. volFileInfoResp, err = client.ReadVolumeFileStatus(ctx,
  37. &volume_server_pb.ReadVolumeFileStatusRequest{
  38. VolumeId: req.VolumeId,
  39. })
  40. if nil != err {
  41. return fmt.Errorf("read volume file status failed, %v", err)
  42. }
  43. volumeFileName = storage.VolumeFileName(volFileInfoResp.Collection, location.Directory, int(req.VolumeId))
  44. // println("source:", volFileInfoResp.String())
  45. // copy ecx file
  46. if err:=vs.doCopyFile(ctx, client, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.IdxFileSize, volumeFileName, ".idx"); err!=nil{
  47. return err
  48. }
  49. if err:=vs.doCopyFile(ctx, client, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.DatFileSize, volumeFileName, ".dat"); err!=nil{
  50. return err
  51. }
  52. return nil
  53. })
  54. if err != nil && volumeFileName != "" {
  55. if idxFileName != "" {
  56. os.Remove(idxFileName)
  57. }
  58. if datFileName != "" {
  59. os.Remove(datFileName)
  60. }
  61. return nil, err
  62. }
  63. if err = checkCopyFiles(volFileInfoResp, idxFileName, datFileName); err != nil { // added by panyc16
  64. return nil, err
  65. }
  66. // mount the volume
  67. err = vs.store.MountVolume(needle.VolumeId(req.VolumeId))
  68. if err != nil {
  69. return nil, fmt.Errorf("failed to mount volume %d: %v", req.VolumeId, err)
  70. }
  71. return &volume_server_pb.VolumeCopyResponse{
  72. LastAppendAtNs: volFileInfoResp.DatFileTimestampSeconds * uint64(time.Second),
  73. }, err
  74. }
  75. func (vs *VolumeServer) doCopyFile(ctx context.Context, client volume_server_pb.VolumeServerClient, vid uint32,
  76. compactRevision uint32, stopOffset uint64, baseFileName, ext string) error {
  77. copyFileClient, err := client.CopyFile(ctx, &volume_server_pb.CopyFileRequest{
  78. VolumeId: vid,
  79. Ext: ext,
  80. CompactionRevision: compactRevision,
  81. StopOffset: stopOffset,
  82. })
  83. if err != nil {
  84. return fmt.Errorf("failed to start copying volume %d %s file: %v", vid, ext, err)
  85. }
  86. err = writeToFile(copyFileClient, baseFileName+ext, util.NewWriteThrottler(vs.compactionBytePerSecond))
  87. if err != nil {
  88. return fmt.Errorf("failed to copy volume %d %s file: %v", vid, ext, err)
  89. }
  90. return nil
  91. }
  92. /**
  93. only check the the differ of the file size
  94. todo: maybe should check the received count and deleted count of the volume
  95. */
  96. func checkCopyFiles(originFileInf *volume_server_pb.ReadVolumeFileStatusResponse, idxFileName, datFileName string) error {
  97. stat, err := os.Stat(idxFileName)
  98. if err != nil {
  99. return fmt.Errorf("get idx file info failed, %v", err)
  100. }
  101. if originFileInf.IdxFileSize != uint64(stat.Size()) {
  102. return fmt.Errorf("the idx file size [%v] is not same as origin file size [%v]",
  103. stat.Size(), originFileInf.IdxFileSize)
  104. }
  105. stat, err = os.Stat(datFileName)
  106. if err != nil {
  107. return fmt.Errorf("get dat file info failed, %v", err)
  108. }
  109. if originFileInf.DatFileSize != uint64(stat.Size()) {
  110. return fmt.Errorf("the dat file size [%v] is not same as origin file size [%v]",
  111. stat.Size(), originFileInf.DatFileSize)
  112. }
  113. return nil
  114. }
  115. func writeToFile(client volume_server_pb.VolumeServer_CopyFileClient, fileName string, wt *util.WriteThrottler) error {
  116. glog.V(4).Infof("writing to %s", fileName)
  117. dst, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  118. if err != nil {
  119. return nil
  120. }
  121. defer dst.Close()
  122. for {
  123. resp, receiveErr := client.Recv()
  124. if receiveErr == io.EOF {
  125. break
  126. }
  127. if receiveErr != nil {
  128. return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
  129. }
  130. dst.Write(resp.FileContent)
  131. wt.MaybeSlowdown(int64(len(resp.FileContent)))
  132. }
  133. return nil
  134. }
  135. func (vs *VolumeServer) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
  136. resp := &volume_server_pb.ReadVolumeFileStatusResponse{}
  137. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  138. if v == nil {
  139. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  140. }
  141. resp.VolumeId = req.VolumeId
  142. datSize, idxSize, modTime := v.FileStat()
  143. resp.DatFileSize = datSize
  144. resp.IdxFileSize = idxSize
  145. resp.DatFileTimestampSeconds = uint64(modTime.Unix())
  146. resp.IdxFileTimestampSeconds = uint64(modTime.Unix())
  147. resp.FileCount = v.FileCount()
  148. resp.CompactionRevision = uint32(v.CompactionRevision)
  149. resp.Collection = v.Collection
  150. return resp, nil
  151. }
  152. // CopyFile client pulls the volume related file from the source server.
  153. // if req.CompactionRevision != math.MaxUint32, it ensures the compact revision is as expected
  154. // The copying still stop at req.StopOffset, but you can set it to math.MaxUint64 in order to read all data.
  155. func (vs *VolumeServer) CopyFile(req *volume_server_pb.CopyFileRequest, stream volume_server_pb.VolumeServer_CopyFileServer) error {
  156. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  157. if v == nil {
  158. return fmt.Errorf("not found volume id %d", req.VolumeId)
  159. }
  160. if uint32(v.CompactionRevision) != req.CompactionRevision && req.CompactionRevision != math.MaxUint32 {
  161. return fmt.Errorf("volume %d is compacted", req.VolumeId)
  162. }
  163. bytesToRead := int64(req.StopOffset)
  164. const BufferSize = 1024 * 1024 * 2
  165. var fileName = v.FileName() + req.Ext
  166. file, err := os.Open(fileName)
  167. if err != nil {
  168. return err
  169. }
  170. defer file.Close()
  171. buffer := make([]byte, BufferSize)
  172. for bytesToRead > 0 {
  173. bytesread, err := file.Read(buffer)
  174. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead)
  175. if err != nil {
  176. if err != io.EOF {
  177. return err
  178. }
  179. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead, "err", err.Error())
  180. break
  181. }
  182. if int64(bytesread) > bytesToRead {
  183. bytesread = int(bytesToRead)
  184. }
  185. err = stream.Send(&volume_server_pb.CopyFileResponse{
  186. FileContent: buffer[:bytesread],
  187. })
  188. if err != nil {
  189. // println("sending", bytesread, "bytes err", err.Error())
  190. return err
  191. }
  192. bytesToRead -= int64(bytesread)
  193. }
  194. return nil
  195. }