command_volume_tier_download.go 4.6 KB

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