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.

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