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.

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