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.

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