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.

266 lines
8.4 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
5 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 .vif 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(context.Background(),
  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(client, false, req.Collection, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.IdxFileSize, volumeFileName, ".idx", false, false); err != nil {
  50. return err
  51. }
  52. if err := vs.doCopyFile(client, false, req.Collection, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.DatFileSize, volumeFileName, ".dat", false, true); err != nil {
  53. return err
  54. }
  55. if err := vs.doCopyFile(client, false, req.Collection, req.VolumeId, volFileInfoResp.CompactionRevision, volFileInfoResp.DatFileSize, volumeFileName, ".vif", false, true); err != nil {
  56. return err
  57. }
  58. return nil
  59. })
  60. idxFileName = volumeFileName + ".idx"
  61. datFileName = volumeFileName + ".dat"
  62. if err != nil && volumeFileName != "" {
  63. os.Remove(idxFileName)
  64. os.Remove(datFileName)
  65. os.Remove(volumeFileName + ".vif")
  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(client volume_server_pb.VolumeServerClient, isEcVolume bool, collection string, vid, compactRevision uint32, stopOffset uint64, baseFileName, ext string, isAppend, ignoreSourceFileNotFound bool) error {
  81. copyFileClient, err := client.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  82. VolumeId: vid,
  83. Ext: ext,
  84. CompactionRevision: compactRevision,
  85. StopOffset: stopOffset,
  86. Collection: collection,
  87. IsEcVolume: isEcVolume,
  88. IgnoreSourceFileNotFound: ignoreSourceFileNotFound,
  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. if req.IgnoreSourceFileNotFound {
  187. return nil
  188. }
  189. return fmt.Errorf("CopyFile not found ec volume id %d", req.VolumeId)
  190. }
  191. }
  192. bytesToRead := int64(req.StopOffset)
  193. file, err := os.Open(fileName)
  194. if err != nil {
  195. if req.IgnoreSourceFileNotFound && err == os.ErrNotExist {
  196. return nil
  197. }
  198. return err
  199. }
  200. defer file.Close()
  201. buffer := make([]byte, BufferSizeLimit)
  202. for bytesToRead > 0 {
  203. bytesread, err := file.Read(buffer)
  204. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead)
  205. if err != nil {
  206. if err != io.EOF {
  207. return err
  208. }
  209. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead, "err", err.Error())
  210. break
  211. }
  212. if int64(bytesread) > bytesToRead {
  213. bytesread = int(bytesToRead)
  214. }
  215. err = stream.Send(&volume_server_pb.CopyFileResponse{
  216. FileContent: buffer[:bytesread],
  217. })
  218. if err != nil {
  219. // println("sending", bytesread, "bytes err", err.Error())
  220. return err
  221. }
  222. bytesToRead -= int64(bytesread)
  223. }
  224. return nil
  225. }