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.

166 lines
6.5 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 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. // check to see if the volume is already read-only and if its not then we need
  72. // to mark it as read-only and then before we return we need to undo what we
  73. // did
  74. var shouldMarkWritable bool
  75. defer func() {
  76. if !shouldMarkWritable {
  77. return
  78. }
  79. clientErr := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  80. _, writableErr := volumeServerClient.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{
  81. VolumeId: uint32(volumeId),
  82. })
  83. return writableErr
  84. })
  85. if clientErr != nil {
  86. log.Printf("failed to mark volume %d as writable after copy from %s: %v", volumeId, sourceVolumeServer, clientErr)
  87. }
  88. }()
  89. err = operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  90. resp, statusErr := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
  91. VolumeId: uint32(volumeId),
  92. })
  93. if statusErr == nil && !resp.IsReadOnly {
  94. shouldMarkWritable = true
  95. _, readonlyErr := volumeServerClient.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{
  96. VolumeId: uint32(volumeId),
  97. })
  98. return readonlyErr
  99. }
  100. return statusErr
  101. })
  102. if err != nil {
  103. return
  104. }
  105. err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  106. resp, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  107. VolumeId: uint32(volumeId),
  108. SourceDataNode: sourceVolumeServer,
  109. })
  110. if replicateErr == nil {
  111. lastAppendAtNs = resp.LastAppendAtNs
  112. }
  113. return replicateErr
  114. })
  115. return
  116. }
  117. func tailVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
  118. return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  119. _, replicateErr := volumeServerClient.VolumeTailReceiver(context.Background(), &volume_server_pb.VolumeTailReceiverRequest{
  120. VolumeId: uint32(volumeId),
  121. SinceNs: lastAppendAtNs,
  122. IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
  123. SourceVolumeServer: sourceVolumeServer,
  124. })
  125. return replicateErr
  126. })
  127. }
  128. func deleteVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  129. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  130. _, deleteErr := volumeServerClient.VolumeDelete(context.Background(), &volume_server_pb.VolumeDeleteRequest{
  131. VolumeId: uint32(volumeId),
  132. })
  133. return deleteErr
  134. })
  135. }