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.

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