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.

150 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
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. "google.golang.org/grpc"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandVolumeTier{})
  15. }
  16. type commandVolumeTier struct {
  17. }
  18. func (c *commandVolumeTier) Name() string {
  19. return "volume.tier"
  20. }
  21. func (c *commandVolumeTier) Help() string {
  22. return `move the dat file of a volume to a remote tier
  23. volume.tier [-collection=""] [-fullPercent=95] [-quietFor=1h]
  24. volume.tier [-collection=""] -volumeId=<volume_id> -dest=<storage_backend> [-keepLocalDatFile]
  25. e.g.:
  26. volume.tier -volumeId=7 -dest=s3
  27. volume.tier -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 *commandVolumeTier) 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 doVolumeTier(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("tiering volumes: %v\n", volumeIds)
  64. for _, vid := range volumeIds {
  65. if err = doVolumeTier(ctx, commandEnv, writer, *collection, vid, *dest, *keepLocalDatFile); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func doVolumeTier(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. // mark the volume as readonly
  78. /*
  79. err = markVolumeReadonly(ctx, 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. */
  84. // copy the .dat file to remote tier
  85. err = copyDatToRemoteTier(ctx, commandEnv.option.GrpcDialOption, writer, needle.VolumeId(vid), collection, locations[0].Url, dest, keepLocalDatFile)
  86. if err != nil {
  87. return fmt.Errorf("copy dat file for volume %d on %s to %s: %v", vid, locations[0].Url, dest, err)
  88. }
  89. return nil
  90. }
  91. func copyDatToRemoteTier(ctx context.Context, grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, collection string, sourceVolumeServer string, dest string, keepLocalDatFile bool) error {
  92. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  93. stream, copyErr := volumeServerClient.VolumeTierCopyDatToRemote(ctx, &volume_server_pb.VolumeTierCopyDatToRemoteRequest{
  94. VolumeId: uint32(volumeId),
  95. Collection: collection,
  96. DestinationBackendName: dest,
  97. KeepLocalDatFile: keepLocalDatFile,
  98. })
  99. var lastProcessed int64
  100. for {
  101. resp, recvErr := stream.Recv()
  102. if recvErr != nil {
  103. if recvErr == io.EOF {
  104. break
  105. } else {
  106. return recvErr
  107. }
  108. }
  109. processingSpeed := float64(resp.Processed-lastProcessed) / 1024.0 / 1024.0
  110. fmt.Fprintf(writer, "copied %.2f%%, %d bytes, %.2fMB/s\n", resp.ProcessedPercentage, resp.Processed, processingSpeed)
  111. lastProcessed = resp.Processed
  112. }
  113. return copyErr
  114. })
  115. return err
  116. }