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.

166 lines
4.4 KiB

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