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.

126 lines
5.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  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, &commandVolumeMove{})
  15. }
  16. type commandVolumeMove struct {
  17. }
  18. func (c *commandVolumeMove) Name() string {
  19. return "volume.move"
  20. }
  21. func (c *commandVolumeMove) Help() string {
  22. return `move a live volume from one volume server to another volume server
  23. volume.move <source volume server host:port> <target volume server host:port> <volume id>
  24. This command move a live volume from one volume server to another volume server. Here are the steps:
  25. 1. This command asks the target volume server to copy the source volume from source volume server, remember the last entry's timestamp.
  26. 2. This command asks the target volume server to mount the new volume
  27. Now the master will mark this volume id as readonly.
  28. 3. This command asks the target volume server to tail the source volume for updates after the timestamp, for 1 minutes to drain the requests.
  29. 4. This command asks the source volume server to unmount the source volume
  30. Now the master will mark this volume id as writable.
  31. 5. This command asks the source volume server to delete the source volume
  32. `
  33. }
  34. func (c *commandVolumeMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. if len(args) != 3 {
  36. fmt.Fprintf(writer, "received args: %+v\n", args)
  37. return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <volume id>")
  38. }
  39. sourceVolumeServer, targetVolumeServer, volumeIdString := args[0], args[1], args[2]
  40. volumeId, err := needle.NewVolumeId(volumeIdString)
  41. if err != nil {
  42. return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
  43. }
  44. if sourceVolumeServer == targetVolumeServer {
  45. return fmt.Errorf("source and target volume servers are the same!")
  46. }
  47. ctx := context.Background()
  48. return LiveMoveVolume(ctx, commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, 5*time.Second)
  49. }
  50. // LiveMoveVolume moves one volume from one source volume server to one target volume server, with idleTimeout to drain the incoming requests.
  51. func LiveMoveVolume(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, idleTimeout time.Duration) (err error) {
  52. log.Printf("copying volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  53. lastAppendAtNs, err := copyVolume(ctx, grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer)
  54. if err != nil {
  55. return fmt.Errorf("copy volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  56. }
  57. log.Printf("tailing volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  58. if err = tailVolume(ctx, grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, lastAppendAtNs, idleTimeout); err != nil {
  59. return fmt.Errorf("tail volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  60. }
  61. log.Printf("deleting volume %d from %s", volumeId, sourceVolumeServer)
  62. if err = deleteVolume(ctx, grpcDialOption, volumeId, sourceVolumeServer); err != nil {
  63. return fmt.Errorf("delete volume %d from %s: %v", volumeId, sourceVolumeServer, err)
  64. }
  65. log.Printf("moved volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  66. return nil
  67. }
  68. func copyVolume(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string) (lastAppendAtNs uint64, err error) {
  69. err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  70. resp, replicateErr := volumeServerClient.VolumeCopy(ctx, &volume_server_pb.VolumeCopyRequest{
  71. VolumeId: uint32(volumeId),
  72. SourceDataNode: sourceVolumeServer,
  73. })
  74. if replicateErr == nil {
  75. lastAppendAtNs = resp.LastAppendAtNs
  76. }
  77. return replicateErr
  78. })
  79. return
  80. }
  81. func tailVolume(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
  82. return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  83. _, replicateErr := volumeServerClient.VolumeTailReceiver(ctx, &volume_server_pb.VolumeTailReceiverRequest{
  84. VolumeId: uint32(volumeId),
  85. SinceNs: lastAppendAtNs,
  86. IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
  87. SourceVolumeServer: sourceVolumeServer,
  88. })
  89. return replicateErr
  90. })
  91. }
  92. func deleteVolume(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  93. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  94. _, deleteErr := volumeServerClient.VolumeDelete(ctx, &volume_server_pb.VolumeDeleteRequest{
  95. VolumeId: uint32(volumeId),
  96. })
  97. return deleteErr
  98. })
  99. }