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.

173 lines
4.6 KiB

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