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.

148 lines
4.9 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
  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. ctx := context.Background()
  52. vid := needle.VolumeId(*volumeId)
  53. // volumeId is provided
  54. if vid != 0 {
  55. return doVolumeTierUpload(ctx, commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile)
  56. }
  57. // apply to all volumes in the collection
  58. // reusing collectVolumeIdsForEcEncode for now
  59. volumeIds, err := collectVolumeIdsForEcEncode(ctx, commandEnv, *collection, *fullPercentage, *quietPeriod)
  60. if err != nil {
  61. return err
  62. }
  63. fmt.Printf("tier upload volumes: %v\n", volumeIds)
  64. for _, vid := range volumeIds {
  65. if err = doVolumeTierUpload(ctx, commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func doVolumeTierUpload(ctx context.Context, commandEnv *CommandEnv, writer io.Writer, collection string, vid needle.VolumeId, dest string, keepLocalDatFile bool) (err error) {
  72. // find volume location
  73. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  74. if !found {
  75. return fmt.Errorf("volume %d not found", vid)
  76. }
  77. err = markVolumeReadonly(ctx, commandEnv.option.GrpcDialOption, needle.VolumeId(vid), locations)
  78. if err != nil {
  79. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  80. }
  81. // copy the .dat file to remote tier
  82. err = uploadDatToRemoteTier(ctx, commandEnv.option.GrpcDialOption, writer, needle.VolumeId(vid), collection, locations[0].Url, dest, keepLocalDatFile)
  83. if err != nil {
  84. return fmt.Errorf("copy dat file for volume %d on %s to %s: %v", vid, locations[0].Url, dest, err)
  85. }
  86. return nil
  87. }
  88. func uploadDatToRemoteTier(ctx context.Context, grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, collection string, sourceVolumeServer string, dest string, keepLocalDatFile bool) error {
  89. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(ctx context.Context, volumeServerClient volume_server_pb.VolumeServerClient) error {
  90. stream, copyErr := volumeServerClient.VolumeTierMoveDatToRemote(ctx, &volume_server_pb.VolumeTierMoveDatToRemoteRequest{
  91. VolumeId: uint32(volumeId),
  92. Collection: collection,
  93. DestinationBackendName: dest,
  94. KeepLocalDatFile: keepLocalDatFile,
  95. })
  96. var lastProcessed int64
  97. for {
  98. resp, recvErr := stream.Recv()
  99. if recvErr != nil {
  100. if recvErr == io.EOF {
  101. break
  102. } else {
  103. return recvErr
  104. }
  105. }
  106. processingSpeed := float64(resp.Processed-lastProcessed) / 1024.0 / 1024.0
  107. fmt.Fprintf(writer, "copied %.2f%%, %d bytes, %.2fMB/s\n", resp.ProcessedPercentage, resp.Processed, processingSpeed)
  108. lastProcessed = resp.Processed
  109. }
  110. return copyErr
  111. })
  112. return err
  113. }