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.

101 lines
2.8 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. )
  10. func (vs *VolumeServer) VolumeSyncStatus(ctx context.Context, req *volume_server_pb.VolumeSyncStatusRequest) (*volume_server_pb.VolumeSyncStatusResponse, error) {
  11. v := vs.store.GetVolume(storage.VolumeId(req.VolumdId))
  12. if v == nil {
  13. return nil, fmt.Errorf("Not Found Volume Id %d", req.VolumdId)
  14. }
  15. resp := v.GetVolumeSyncStatus()
  16. glog.V(2).Infof("volume sync status %d", req.VolumdId)
  17. return resp, nil
  18. }
  19. func (vs *VolumeServer) VolumeSyncIndex(req *volume_server_pb.VolumeSyncIndexRequest, stream volume_server_pb.VolumeServer_VolumeSyncIndexServer) error {
  20. v := vs.store.GetVolume(storage.VolumeId(req.VolumdId))
  21. if v == nil {
  22. return fmt.Errorf("Not Found Volume Id %d", req.VolumdId)
  23. }
  24. content, err := v.IndexFileContent()
  25. if err != nil {
  26. glog.Errorf("sync volume %d index: %v", req.VolumdId, err)
  27. } else {
  28. glog.V(2).Infof("sync volume %d index", req.VolumdId)
  29. }
  30. const blockSizeLimit = 1024 * 1024 * 2
  31. for i := 0; i < len(content); i += blockSizeLimit {
  32. blockSize := len(content) - i
  33. if blockSize > blockSizeLimit {
  34. blockSize = blockSizeLimit
  35. }
  36. resp := &volume_server_pb.VolumeSyncIndexResponse{}
  37. resp.IndexFileContent = content[i : i+blockSize]
  38. stream.Send(resp)
  39. }
  40. return nil
  41. }
  42. func (vs *VolumeServer) VolumeSyncData(req *volume_server_pb.VolumeSyncDataRequest, stream volume_server_pb.VolumeServer_VolumeSyncDataServer) error {
  43. v := vs.store.GetVolume(storage.VolumeId(req.VolumdId))
  44. if v == nil {
  45. return fmt.Errorf("Not Found Volume Id %d", req.VolumdId)
  46. }
  47. if uint32(v.SuperBlock.CompactRevision) != req.Revision {
  48. return fmt.Errorf("Requested Volume Revision is %d, but current revision is %d", req.Revision, v.SuperBlock.CompactRevision)
  49. }
  50. content, err := storage.ReadNeedleBlob(v.DataFile(), int64(req.Offset)*types.NeedlePaddingSize, req.Size, v.Version())
  51. if err != nil {
  52. return fmt.Errorf("read offset:%d size:%d", req.Offset, req.Size)
  53. }
  54. id, err := types.ParseNeedleId(req.NeedleId)
  55. if err != nil {
  56. return fmt.Errorf("parsing needle id %s: %v", req.NeedleId, err)
  57. }
  58. n := new(storage.Needle)
  59. n.ParseNeedleHeader(content)
  60. if id != n.Id {
  61. return fmt.Errorf("Expected file entry id %d, but found %d", id, n.Id)
  62. }
  63. if err != nil {
  64. glog.Errorf("sync volume %d data: %v", req.VolumdId, err)
  65. }
  66. const blockSizeLimit = 1024 * 1024 * 2
  67. for i := 0; i < len(content); i += blockSizeLimit {
  68. blockSize := len(content) - i
  69. if blockSize > blockSizeLimit {
  70. blockSize = blockSizeLimit
  71. }
  72. resp := &volume_server_pb.VolumeSyncDataResponse{}
  73. resp.FileContent = content[i : i+blockSize]
  74. stream.Send(resp)
  75. }
  76. return nil
  77. }