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.

283 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. idxFileName = volumeFileName + ".idx"
  69. datFileName = volumeFileName + ".dat"
  70. defer func() {
  71. if err != nil && volumeFileName != "" {
  72. os.Remove(idxFileName)
  73. os.Remove(datFileName)
  74. os.Remove(volumeFileName + ".vif")
  75. }
  76. }()
  77. if err != nil && volumeFileName != "" {
  78. return nil, err
  79. }
  80. if err = checkCopyFiles(volFileInfoResp, idxFileName, datFileName); err != nil { // added by panyc16
  81. return nil, err
  82. }
  83. // mount the volume
  84. err = vs.store.MountVolume(needle.VolumeId(req.VolumeId))
  85. if err != nil {
  86. return nil, fmt.Errorf("failed to mount volume %d: %v", req.VolumeId, err)
  87. }
  88. return &volume_server_pb.VolumeCopyResponse{
  89. LastAppendAtNs: volFileInfoResp.DatFileTimestampSeconds * uint64(time.Second),
  90. }, err
  91. }
  92. 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 {
  93. copyFileClient, err := client.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
  94. VolumeId: vid,
  95. Ext: ext,
  96. CompactionRevision: compactRevision,
  97. StopOffset: stopOffset,
  98. Collection: collection,
  99. IsEcVolume: isEcVolume,
  100. IgnoreSourceFileNotFound: ignoreSourceFileNotFound,
  101. })
  102. if err != nil {
  103. return fmt.Errorf("failed to start copying volume %d %s file: %v", vid, ext, err)
  104. }
  105. err = writeToFile(copyFileClient, baseFileName+ext, util.NewWriteThrottler(vs.compactionBytePerSecond), isAppend)
  106. if err != nil {
  107. return fmt.Errorf("failed to copy %s file: %v", baseFileName+ext, err)
  108. }
  109. return nil
  110. }
  111. /**
  112. only check the the differ of the file size
  113. todo: maybe should check the received count and deleted count of the volume
  114. */
  115. func checkCopyFiles(originFileInf *volume_server_pb.ReadVolumeFileStatusResponse, idxFileName, datFileName string) error {
  116. stat, err := os.Stat(idxFileName)
  117. if err != nil {
  118. return fmt.Errorf("stat idx file %s failed, %v", idxFileName, err)
  119. }
  120. if originFileInf.IdxFileSize != uint64(stat.Size()) {
  121. return fmt.Errorf("idx file %s size [%v] is not same as origin file size [%v]",
  122. idxFileName, stat.Size(), originFileInf.IdxFileSize)
  123. }
  124. stat, err = os.Stat(datFileName)
  125. if err != nil {
  126. return fmt.Errorf("get dat file info failed, %v", err)
  127. }
  128. if originFileInf.DatFileSize != uint64(stat.Size()) {
  129. return fmt.Errorf("the dat file size [%v] is not same as origin file size [%v]",
  130. stat.Size(), originFileInf.DatFileSize)
  131. }
  132. return nil
  133. }
  134. func writeToFile(client volume_server_pb.VolumeServer_CopyFileClient, fileName string, wt *util.WriteThrottler, isAppend bool) error {
  135. glog.V(4).Infof("writing to %s", fileName)
  136. flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  137. if isAppend {
  138. flags = os.O_WRONLY | os.O_CREATE
  139. }
  140. dst, err := os.OpenFile(fileName, flags, 0644)
  141. if err != nil {
  142. return nil
  143. }
  144. defer dst.Close()
  145. for {
  146. resp, receiveErr := client.Recv()
  147. if receiveErr == io.EOF {
  148. break
  149. }
  150. if receiveErr != nil {
  151. return fmt.Errorf("receiving %s: %v", fileName, receiveErr)
  152. }
  153. dst.Write(resp.FileContent)
  154. wt.MaybeSlowdown(int64(len(resp.FileContent)))
  155. }
  156. return nil
  157. }
  158. func (vs *VolumeServer) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
  159. resp := &volume_server_pb.ReadVolumeFileStatusResponse{}
  160. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  161. if v == nil {
  162. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  163. }
  164. resp.VolumeId = req.VolumeId
  165. datSize, idxSize, modTime := v.FileStat()
  166. resp.DatFileSize = datSize
  167. resp.IdxFileSize = idxSize
  168. resp.DatFileTimestampSeconds = uint64(modTime.Unix())
  169. resp.IdxFileTimestampSeconds = uint64(modTime.Unix())
  170. resp.FileCount = v.FileCount()
  171. resp.CompactionRevision = uint32(v.CompactionRevision)
  172. resp.Collection = v.Collection
  173. return resp, nil
  174. }
  175. // CopyFile client pulls the volume related file from the source server.
  176. // if req.CompactionRevision != math.MaxUint32, it ensures the compact revision is as expected
  177. // The copying still stop at req.StopOffset, but you can set it to math.MaxUint64 in order to read all data.
  178. func (vs *VolumeServer) CopyFile(req *volume_server_pb.CopyFileRequest, stream volume_server_pb.VolumeServer_CopyFileServer) error {
  179. var fileName string
  180. if !req.IsEcVolume {
  181. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  182. if v == nil {
  183. return fmt.Errorf("not found volume id %d", req.VolumeId)
  184. }
  185. if uint32(v.CompactionRevision) != req.CompactionRevision && req.CompactionRevision != math.MaxUint32 {
  186. return fmt.Errorf("volume %d is compacted", req.VolumeId)
  187. }
  188. fileName = v.FileName() + req.Ext
  189. } else {
  190. baseFileName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId)) + req.Ext
  191. for _, location := range vs.store.Locations {
  192. tName := util.Join(location.Directory, baseFileName)
  193. if util.FileExists(tName) {
  194. fileName = tName
  195. }
  196. }
  197. if fileName == "" {
  198. if req.IgnoreSourceFileNotFound {
  199. return nil
  200. }
  201. return fmt.Errorf("CopyFile not found ec volume id %d", req.VolumeId)
  202. }
  203. }
  204. bytesToRead := int64(req.StopOffset)
  205. file, err := os.Open(fileName)
  206. if err != nil {
  207. if req.IgnoreSourceFileNotFound && err == os.ErrNotExist {
  208. return nil
  209. }
  210. return err
  211. }
  212. defer file.Close()
  213. buffer := make([]byte, BufferSizeLimit)
  214. for bytesToRead > 0 {
  215. bytesread, err := file.Read(buffer)
  216. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead)
  217. if err != nil {
  218. if err != io.EOF {
  219. return err
  220. }
  221. // println(fileName, "read", bytesread, "bytes, with target", bytesToRead, "err", err.Error())
  222. break
  223. }
  224. if int64(bytesread) > bytesToRead {
  225. bytesread = int(bytesToRead)
  226. }
  227. err = stream.Send(&volume_server_pb.CopyFileResponse{
  228. FileContent: buffer[:bytesread],
  229. })
  230. if err != nil {
  231. // println("sending", bytesread, "bytes err", err.Error())
  232. return err
  233. }
  234. bytesToRead -= int64(bytesread)
  235. }
  236. return nil
  237. }