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.

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