command_volume_tier_download.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandVolumeTierDownload{})
  15. }
  16. type commandVolumeTierDownload struct {
  17. }
  18. func (c *commandVolumeTierDownload) Name() string {
  19. return "volume.tier.download"
  20. }
  21. func (c *commandVolumeTierDownload) Help() string {
  22. return `download the dat file of a volume from a remote tier
  23. volume.tier.download [-collection=""]
  24. volume.tier.download [-collection=""] -volumeId=<volume_id>
  25. e.g.:
  26. volume.tier.download -volumeId=7
  27. volume.tier.download -volumeId=7
  28. This command will download the dat file of a volume from a remote tier to a volume server in local cluster.
  29. `
  30. }
  31. func (c *commandVolumeTierDownload) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  32. tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  33. volumeId := tierCommand.Int("volumeId", 0, "the volume id")
  34. collection := tierCommand.String("collection", "", "the collection name")
  35. if err = tierCommand.Parse(args); err != nil {
  36. return nil
  37. }
  38. ctx := context.Background()
  39. vid := needle.VolumeId(*volumeId)
  40. // collect topology information
  41. topologyInfo, err := collectTopologyInfo(ctx, commandEnv)
  42. if err != nil {
  43. return err
  44. }
  45. // volumeId is provided
  46. if vid != 0 {
  47. return doVolumeTierDownload(ctx, commandEnv, writer, *collection, vid)
  48. }
  49. // apply to all volumes in the collection
  50. // reusing collectVolumeIdsForEcEncode for now
  51. volumeIds := collectRemoteVolumes(topologyInfo, *collection)
  52. if err != nil {
  53. return err
  54. }
  55. fmt.Printf("tier download volumes: %v\n", volumeIds)
  56. for _, vid := range volumeIds {
  57. if err = doVolumeTierDownload(ctx, commandEnv, writer, *collection, vid); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. func collectRemoteVolumes(topoInfo *master_pb.TopologyInfo, selectedCollection string) (vids []needle.VolumeId) {
  64. vidMap := make(map[uint32]bool)
  65. eachDataNode(topoInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  66. for _, v := range dn.VolumeInfos {
  67. if v.Collection == selectedCollection && v.RemoteStorageKey != "" && v.RemoteStorageName != "" {
  68. vidMap[v.Id] = true
  69. }
  70. }
  71. })
  72. for vid := range vidMap {
  73. vids = append(vids, needle.VolumeId(vid))
  74. }
  75. return
  76. }
  77. func doVolumeTierDownload(ctx context.Context, commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId) (err error) {
  78. // find volume location
  79. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  80. if !found {
  81. return fmt.Errorf("volume %d not found", vid)
  82. }
  83. // TODO parallelize this
  84. for _, loc := range locations {
  85. // copy the .dat file from remote tier to local
  86. err = downloadDatFromRemoteTier(ctx, commandEnv.option.GrpcDialOption, writer, needle.VolumeId(vid), collection, loc.Url)
  87. if err != nil {
  88. return fmt.Errorf("download dat file for volume %d to %s: %v", vid, loc.Url, err)
  89. }
  90. }
  91. return nil
  92. }
  93. func downloadDatFromRemoteTier(ctx context.Context, grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, collection string, targetVolumeServer string) error {
  94. err := operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(ctx context.Context, volumeServerClient volume_server_pb.VolumeServerClient) error {
  95. stream, downloadErr := volumeServerClient.VolumeTierMoveDatFromRemote(ctx, &volume_server_pb.VolumeTierMoveDatFromRemoteRequest{
  96. VolumeId: uint32(volumeId),
  97. Collection: collection,
  98. })
  99. var lastProcessed int64
  100. for {
  101. resp, recvErr := stream.Recv()
  102. if recvErr != nil {
  103. if recvErr == io.EOF {
  104. break
  105. } else {
  106. return recvErr
  107. }
  108. }
  109. processingSpeed := float64(resp.Processed-lastProcessed) / 1024.0 / 1024.0
  110. fmt.Fprintf(writer, "downloaded %.2f%%, %d bytes, %.2fMB/s\n", resp.ProcessedPercentage, resp.Processed, processingSpeed)
  111. lastProcessed = resp.Processed
  112. }
  113. if downloadErr != nil {
  114. return downloadErr
  115. }
  116. _, unmountErr := volumeServerClient.VolumeUnmount(ctx, &volume_server_pb.VolumeUnmountRequest{
  117. VolumeId: uint32(volumeId),
  118. })
  119. if unmountErr != nil {
  120. return unmountErr
  121. }
  122. _, mountErr := volumeServerClient.VolumeMount(ctx, &volume_server_pb.VolumeMountRequest{
  123. VolumeId: uint32(volumeId),
  124. })
  125. if mountErr != nil {
  126. return mountErr
  127. }
  128. return nil
  129. })
  130. return err
  131. }