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.

259 lines
7.9 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. "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"); err != nil {
  50. return err
  51. }
  52. if err := vs.doCopyFile(ctx, client, false, req.Collection, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.DatFileSize, volumeFileName, ".dat"); 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) 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))
  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) error {
  123. glog.V(4).Infof("writing to %s", fileName)
  124. dst, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  125. if err != nil {
  126. return nil
  127. }
  128. defer dst.Close()
  129. for {
  130. resp, receiveErr := client.Recv()
  131. if receiveErr == io.EOF {
  132. break
  133. }
  134. if receiveErr != nil {
  135. return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
  136. }
  137. dst.Write(resp.FileContent)
  138. wt.MaybeSlowdown(int64(len(resp.FileContent)))
  139. }
  140. return nil
  141. }
  142. func (vs *VolumeServer) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
  143. resp := &volume_server_pb.ReadVolumeFileStatusResponse{}
  144. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  145. if v == nil {
  146. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  147. }
  148. resp.VolumeId = req.VolumeId
  149. datSize, idxSize, modTime := v.FileStat()
  150. resp.DatFileSize = datSize
  151. resp.IdxFileSize = idxSize
  152. resp.DatFileTimestampSeconds = uint64(modTime.Unix())
  153. resp.IdxFileTimestampSeconds = uint64(modTime.Unix())
  154. resp.FileCount = v.FileCount()
  155. resp.CompactionRevision = uint32(v.CompactionRevision)
  156. resp.Collection = v.Collection
  157. return resp, nil
  158. }
  159. // CopyFile client pulls the volume related file from the source server.
  160. // if req.CompactionRevision != math.MaxUint32, it ensures the compact revision is as expected
  161. // The copying still stop at req.StopOffset, but you can set it to math.MaxUint64 in order to read all data.
  162. func (vs *VolumeServer) CopyFile(req *volume_server_pb.CopyFileRequest, stream volume_server_pb.VolumeServer_CopyFileServer) error {
  163. var fileName string
  164. if !req.IsEcVolume {
  165. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  166. if v == nil {
  167. return fmt.Errorf("not found volume id %d", req.VolumeId)
  168. }
  169. if uint32(v.CompactionRevision) != req.CompactionRevision && req.CompactionRevision != math.MaxUint32 {
  170. return fmt.Errorf("volume %d is compacted", req.VolumeId)
  171. }
  172. fileName = v.FileName() + req.Ext
  173. } else {
  174. baseFileName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId)) + req.Ext
  175. for _, location := range vs.store.Locations {
  176. tName := path.Join(location.Directory, baseFileName)
  177. if util.FileExists(tName) {
  178. fileName = tName
  179. }
  180. }
  181. if fileName == "" {
  182. return fmt.Errorf("CopyFile not found ec volume id %d", req.VolumeId)
  183. }
  184. }
  185. bytesToRead := int64(req.StopOffset)
  186. file, err := os.Open(fileName)
  187. if err != nil {
  188. return err
  189. }
  190. defer file.Close()
  191. buffer := make([]byte, BufferSizeLimit)
  192. for bytesToRead > 0 {
  193. bytesread, err := file.Read(buffer)
  194. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead)
  195. if err != nil {
  196. if err != io.EOF {
  197. return err
  198. }
  199. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead, "err", err.Error())
  200. break
  201. }
  202. if int64(bytesread) > bytesToRead {
  203. bytesread = int(bytesToRead)
  204. }
  205. err = stream.Send(&volume_server_pb.CopyFileResponse{
  206. FileContent: buffer[:bytesread],
  207. })
  208. if err != nil {
  209. // println("sending", bytesread, "bytes err", err.Error())
  210. return err
  211. }
  212. bytesToRead -= int64(bytesread)
  213. }
  214. return nil
  215. }
  216. func (vs *VolumeServer) findVolumeOrEcVolumeLocation(volumeId needle.VolumeId) {
  217. }