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.

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