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.

147 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
  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. tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  42. volumeId := tierCommand.Int("volumeId", 0, "the volume id")
  43. collection := tierCommand.String("collection", "", "the collection name")
  44. fullPercentage := tierCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
  45. quietPeriod := tierCommand.Duration("quietFor", 24*time.Hour, "select volumes without no writes for this period")
  46. dest := tierCommand.String("dest", "", "the target tier name")
  47. keepLocalDatFile := tierCommand.Bool("keepLocalDatFile", false, "whether keep local dat file")
  48. if err = tierCommand.Parse(args); err != nil {
  49. return nil
  50. }
  51. vid := needle.VolumeId(*volumeId)
  52. // volumeId is provided
  53. if vid != 0 {
  54. return doVolumeTierUpload(commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile)
  55. }
  56. // apply to all volumes in the collection
  57. // reusing collectVolumeIdsForEcEncode for now
  58. volumeIds, err := collectVolumeIdsForEcEncode(commandEnv, *collection, *fullPercentage, *quietPeriod)
  59. if err != nil {
  60. return err
  61. }
  62. fmt.Printf("tier upload volumes: %v\n", volumeIds)
  63. for _, vid := range volumeIds {
  64. if err = doVolumeTierUpload(commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile); err != nil {
  65. return err
  66. }
  67. }
  68. return nil
  69. }
  70. func doVolumeTierUpload(commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId, dest string, keepLocalDatFile bool) (err error) {
  71. // find volume location
  72. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  73. if !found {
  74. return fmt.Errorf("volume %d not found", vid)
  75. }
  76. err = markVolumeReadonly(commandEnv.option.GrpcDialOption, needle.VolumeId(vid), locations)
  77. if err != nil {
  78. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  79. }
  80. // copy the .dat file to remote tier
  81. err = uploadDatToRemoteTier(commandEnv.option.GrpcDialOption, writer, needle.VolumeId(vid), collection, locations[0].Url, dest, keepLocalDatFile)
  82. if err != nil {
  83. return fmt.Errorf("copy dat file for volume %d on %s to %s: %v", vid, locations[0].Url, dest, err)
  84. }
  85. return nil
  86. }
  87. func uploadDatToRemoteTier(grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, collection string, sourceVolumeServer string, dest string, keepLocalDatFile bool) error {
  88. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  89. stream, copyErr := volumeServerClient.VolumeTierMoveDatToRemote(context.Background(), &volume_server_pb.VolumeTierMoveDatToRemoteRequest{
  90. VolumeId: uint32(volumeId),
  91. Collection: collection,
  92. DestinationBackendName: dest,
  93. KeepLocalDatFile: keepLocalDatFile,
  94. })
  95. var lastProcessed int64
  96. for {
  97. resp, recvErr := stream.Recv()
  98. if recvErr != nil {
  99. if recvErr == io.EOF {
  100. break
  101. } else {
  102. return recvErr
  103. }
  104. }
  105. processingSpeed := float64(resp.Processed-lastProcessed) / 1024.0 / 1024.0
  106. fmt.Fprintf(writer, "copied %.2f%%, %d bytes, %.2fMB/s\n", resp.ProcessedPercentage, resp.Processed, processingSpeed)
  107. lastProcessed = resp.Processed
  108. }
  109. return copyErr
  110. })
  111. return err
  112. }