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.

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