command_volume_tier_download.go 5.2 KB

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