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.

125 lines
5.1 KiB

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. return LiveMoveVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, 5*time.Second)
  48. }
  49. // LiveMoveVolume moves one volume from one source volume server to one target volume server, with idleTimeout to drain the incoming requests.
  50. func LiveMoveVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, idleTimeout time.Duration) (err error) {
  51. log.Printf("copying volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  52. lastAppendAtNs, err := copyVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer)
  53. if err != nil {
  54. return fmt.Errorf("copy volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  55. }
  56. log.Printf("tailing volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  57. if err = tailVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, lastAppendAtNs, idleTimeout); err != nil {
  58. return fmt.Errorf("tail volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  59. }
  60. log.Printf("deleting volume %d from %s", volumeId, sourceVolumeServer)
  61. if err = deleteVolume(grpcDialOption, volumeId, sourceVolumeServer); err != nil {
  62. return fmt.Errorf("delete volume %d from %s: %v", volumeId, sourceVolumeServer, err)
  63. }
  64. log.Printf("moved volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  65. return nil
  66. }
  67. func copyVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string) (lastAppendAtNs uint64, err error) {
  68. err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  69. resp, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  70. VolumeId: uint32(volumeId),
  71. SourceDataNode: sourceVolumeServer,
  72. })
  73. if replicateErr == nil {
  74. lastAppendAtNs = resp.LastAppendAtNs
  75. }
  76. return replicateErr
  77. })
  78. return
  79. }
  80. func tailVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
  81. return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  82. _, replicateErr := volumeServerClient.VolumeTailReceiver(context.Background(), &volume_server_pb.VolumeTailReceiverRequest{
  83. VolumeId: uint32(volumeId),
  84. SinceNs: lastAppendAtNs,
  85. IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
  86. SourceVolumeServer: sourceVolumeServer,
  87. })
  88. return replicateErr
  89. })
  90. }
  91. func deleteVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  92. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  93. _, deleteErr := volumeServerClient.VolumeDelete(context.Background(), &volume_server_pb.VolumeDeleteRequest{
  94. VolumeId: uint32(volumeId),
  95. })
  96. return deleteErr
  97. })
  98. }