command_volume_move.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 err = commandEnv.confirmIsLocked(); err != nil {
  36. return
  37. }
  38. if len(args) != 3 {
  39. fmt.Fprintf(writer, "received args: %+v\n", args)
  40. return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <volume id>")
  41. }
  42. sourceVolumeServer, targetVolumeServer, volumeIdString := args[0], args[1], args[2]
  43. volumeId, err := needle.NewVolumeId(volumeIdString)
  44. if err != nil {
  45. return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
  46. }
  47. if sourceVolumeServer == targetVolumeServer {
  48. return fmt.Errorf("source and target volume servers are the same!")
  49. }
  50. return LiveMoveVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, 5*time.Second)
  51. }
  52. // LiveMoveVolume moves one volume from one source volume server to one target volume server, with idleTimeout to drain the incoming requests.
  53. func LiveMoveVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, idleTimeout time.Duration) (err error) {
  54. log.Printf("copying volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  55. lastAppendAtNs, err := copyVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer)
  56. if err != nil {
  57. return fmt.Errorf("copy volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  58. }
  59. log.Printf("tailing volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  60. if err = tailVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, lastAppendAtNs, idleTimeout); err != nil {
  61. return fmt.Errorf("tail volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  62. }
  63. log.Printf("deleting volume %d from %s", volumeId, sourceVolumeServer)
  64. if err = deleteVolume(grpcDialOption, volumeId, sourceVolumeServer); err != nil {
  65. return fmt.Errorf("delete volume %d from %s: %v", volumeId, sourceVolumeServer, err)
  66. }
  67. log.Printf("moved volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  68. return nil
  69. }
  70. func copyVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string) (lastAppendAtNs uint64, err error) {
  71. err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  72. resp, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  73. VolumeId: uint32(volumeId),
  74. SourceDataNode: sourceVolumeServer,
  75. })
  76. if replicateErr == nil {
  77. lastAppendAtNs = resp.LastAppendAtNs
  78. }
  79. return replicateErr
  80. })
  81. return
  82. }
  83. func tailVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
  84. return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  85. _, replicateErr := volumeServerClient.VolumeTailReceiver(context.Background(), &volume_server_pb.VolumeTailReceiverRequest{
  86. VolumeId: uint32(volumeId),
  87. SinceNs: lastAppendAtNs,
  88. IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
  89. SourceVolumeServer: sourceVolumeServer,
  90. })
  91. return replicateErr
  92. })
  93. }
  94. func deleteVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  95. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  96. _, deleteErr := volumeServerClient.VolumeDelete(context.Background(), &volume_server_pb.VolumeDeleteRequest{
  97. VolumeId: uint32(volumeId),
  98. })
  99. return deleteErr
  100. })
  101. }