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.

151 lines
4.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "time"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  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, &commandVolumeTierUpload{})
  15. }
  16. type commandVolumeTierUpload struct {
  17. }
  18. func (c *commandVolumeTierUpload) Name() string {
  19. return "volume.tier.upload"
  20. }
  21. func (c *commandVolumeTierUpload) Help() string {
  22. return `upload the dat file of a volume to a remote tier
  23. volume.tier.upload [-collection=""] [-fullPercent=95] [-quietFor=1h]
  24. volume.tier.upload [-collection=""] -volumeId=<volume_id> -dest=<storage_backend> [-keepLocalDatFile]
  25. e.g.:
  26. volume.tier.upload -volumeId=7 -dest=s3
  27. volume.tier.upload -volumeId=7 -dest=s3.default
  28. The <storage_backend> is defined in master.toml.
  29. For example, "s3.default" in [storage.backend.s3.default]
  30. This command will move the dat file of a volume to a remote tier.
  31. SeaweedFS enables scalable and fast local access to lots of files,
  32. and the cloud storage is slower by cost efficient. How to combine them together?
  33. Usually the data follows 80/20 rule: only 20% of data is frequently accessed.
  34. We can offload the old volumes to the cloud.
  35. With this, SeaweedFS can be both fast and scalable, and infinite storage space.
  36. Just add more local SeaweedFS volume servers to increase the throughput.
  37. The index file is still local, and the same O(1) disk read is applied to the remote file.
  38. `
  39. }
  40. func (c *commandVolumeTierUpload) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  41. if err = commandEnv.confirmIsLocked(); err != nil {
  42. return
  43. }
  44. tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  45. volumeId := tierCommand.Int("volumeId", 0, "the volume id")
  46. collection := tierCommand.String("collection", "", "the collection name")
  47. fullPercentage := tierCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
  48. quietPeriod := tierCommand.Duration("quietFor", 24*time.Hour, "select volumes without no writes for this period")
  49. dest := tierCommand.String("dest", "", "the target tier name")
  50. keepLocalDatFile := tierCommand.Bool("keepLocalDatFile", false, "whether keep local dat file")
  51. if err = tierCommand.Parse(args); err != nil {
  52. return nil
  53. }
  54. vid := needle.VolumeId(*volumeId)
  55. // volumeId is provided
  56. if vid != 0 {
  57. return doVolumeTierUpload(commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile)
  58. }
  59. // apply to all volumes in the collection
  60. // reusing collectVolumeIdsForEcEncode for now
  61. volumeIds, err := collectVolumeIdsForEcEncode(commandEnv, *collection, *fullPercentage, *quietPeriod)
  62. if err != nil {
  63. return err
  64. }
  65. fmt.Printf("tier upload volumes: %v\n", volumeIds)
  66. for _, vid := range volumeIds {
  67. if err = doVolumeTierUpload(commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile); err != nil {
  68. return err
  69. }
  70. }
  71. return nil
  72. }
  73. func doVolumeTierUpload(commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId, dest string, keepLocalDatFile bool) (err error) {
  74. // find volume location
  75. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  76. if !found {
  77. return fmt.Errorf("volume %d not found", vid)
  78. }
  79. err = markVolumeReadonly(commandEnv.option.GrpcDialOption, needle.VolumeId(vid), locations)
  80. if err != nil {
  81. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  82. }
  83. // copy the .dat file to remote tier
  84. err = uploadDatToRemoteTier(commandEnv.option.GrpcDialOption, writer, needle.VolumeId(vid), collection, locations[0].Url, dest, keepLocalDatFile)
  85. if err != nil {
  86. return fmt.Errorf("copy dat file for volume %d on %s to %s: %v", vid, locations[0].Url, dest, err)
  87. }
  88. return nil
  89. }
  90. func uploadDatToRemoteTier(grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, collection string, sourceVolumeServer string, dest string, keepLocalDatFile bool) error {
  91. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  92. stream, copyErr := volumeServerClient.VolumeTierMoveDatToRemote(context.Background(), &volume_server_pb.VolumeTierMoveDatToRemoteRequest{
  93. VolumeId: uint32(volumeId),
  94. Collection: collection,
  95. DestinationBackendName: dest,
  96. KeepLocalDatFile: keepLocalDatFile,
  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, "copied %.2f%%, %d bytes, %.2fMB/s\n", resp.ProcessedPercentage, resp.Processed, processingSpeed)
  110. lastProcessed = resp.Processed
  111. }
  112. return copyErr
  113. })
  114. return err
  115. }