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.

326 lines
10 KiB

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