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.

112 lines
3.3 KiB

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 `copy the dat file of a volume to a remote tier
  23. ec.encode [-collection=""] [-fullPercent=95] [-quietFor=1h]
  24. ec.encode [-collection=""] [-volumeId=<volume_id>]
  25. This command will:
  26. 1. freeze one volume
  27. 2. copy the dat file of a volume to a remote tier
  28. `
  29. }
  30. func (c *commandVolumeTier) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. volumeId := tierCommand.Int("volumeId", 0, "the volume id")
  33. collection := tierCommand.String("collection", "", "the collection name")
  34. fullPercentage := tierCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
  35. quietPeriod := tierCommand.Duration("quietFor", 24*time.Hour, "select volumes without no writes for this period")
  36. dest := tierCommand.String("destination", "", "the target tier name")
  37. if err = tierCommand.Parse(args); err != nil {
  38. return nil
  39. }
  40. ctx := context.Background()
  41. vid := needle.VolumeId(*volumeId)
  42. // volumeId is provided
  43. if vid != 0 {
  44. return doVolumeTier(ctx, commandEnv, *collection, vid, *dest)
  45. }
  46. // apply to all volumes in the collection
  47. // reusing collectVolumeIdsForEcEncode for now
  48. volumeIds, err := collectVolumeIdsForEcEncode(ctx, commandEnv, *collection, *fullPercentage, *quietPeriod)
  49. if err != nil {
  50. return err
  51. }
  52. fmt.Printf("tiering volumes: %v\n", volumeIds)
  53. for _, vid := range volumeIds {
  54. if err = doVolumeTier(ctx, commandEnv, *collection, vid, *dest); err != nil {
  55. return err
  56. }
  57. }
  58. return nil
  59. }
  60. func doVolumeTier(ctx context.Context, commandEnv *CommandEnv, collection string, vid needle.VolumeId, dest string) (err error) {
  61. // find volume location
  62. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  63. if !found {
  64. return fmt.Errorf("volume %d not found", vid)
  65. }
  66. // mark the volume as readonly
  67. /*
  68. err = markVolumeReadonly(ctx, commandEnv.option.GrpcDialOption, needle.VolumeId(vid), locations)
  69. if err != nil {
  70. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  71. }
  72. */
  73. // copy the .dat file to remote tier
  74. err = copyDatToRemoteTier(ctx, commandEnv.option.GrpcDialOption, needle.VolumeId(vid), collection, locations[0].Url, dest)
  75. if err != nil {
  76. return fmt.Errorf("copy dat file for volume %d on %s to %s: %v", vid, locations[0].Url, dest, err)
  77. }
  78. return nil
  79. }
  80. func copyDatToRemoteTier(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, collection string, sourceVolumeServer string, dest string) error {
  81. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  82. _, copyErr := volumeServerClient.VolumeTierCopyDatToRemote(ctx, &volume_server_pb.VolumeTierCopyDatToRemoteRequest{
  83. VolumeId: uint32(volumeId),
  84. Collection: collection,
  85. })
  86. return copyErr
  87. })
  88. return err
  89. }