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.

192 lines
7.8 KiB

6 years ago
6 years ago
3 years ago
6 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "log"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/operation"
  10. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "google.golang.org/grpc"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandVolumeMove{})
  16. }
  17. type commandVolumeMove struct {
  18. }
  19. func (c *commandVolumeMove) Name() string {
  20. return "volume.move"
  21. }
  22. func (c *commandVolumeMove) Help() string {
  23. return `move a live volume from one volume server to another volume server
  24. volume.move -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id>
  25. volume.move -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id> -disk [hdd|ssd|<tag>]
  26. This command move a live volume from one volume server to another volume server. Here are the steps:
  27. 1. This command asks the target volume server to copy the source volume from source volume server, remember the last entry's timestamp.
  28. 2. This command asks the target volume server to mount the new volume
  29. Now the master will mark this volume id as readonly.
  30. 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.
  31. 4. This command asks the source volume server to unmount the source volume
  32. Now the master will mark this volume id as writable.
  33. 5. This command asks the source volume server to delete the source volume
  34. The option "-disk [hdd|ssd|<tag>]" can be used to change the volume disk type.
  35. `
  36. }
  37. func (c *commandVolumeMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  38. if err = commandEnv.confirmIsLocked(); err != nil {
  39. return
  40. }
  41. volMoveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  42. volumeIdInt := volMoveCommand.Int("volumeId", 0, "the volume id")
  43. sourceNodeStr := volMoveCommand.String("source", "", "the source volume server <host>:<port>")
  44. targetNodeStr := volMoveCommand.String("target", "", "the target volume server <host>:<port>")
  45. diskTypeStr := volMoveCommand.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  46. if err = volMoveCommand.Parse(args); err != nil {
  47. return nil
  48. }
  49. sourceVolumeServer, targetVolumeServer := *sourceNodeStr, *targetNodeStr
  50. volumeId := needle.VolumeId(*volumeIdInt)
  51. if sourceVolumeServer == targetVolumeServer {
  52. return fmt.Errorf("source and target volume servers are the same!")
  53. }
  54. return LiveMoveVolume(commandEnv.option.GrpcDialOption, writer, volumeId, sourceVolumeServer, targetVolumeServer, 5*time.Second, *diskTypeStr, false)
  55. }
  56. // LiveMoveVolume moves one volume from one source volume server to one target volume server, with idleTimeout to drain the incoming requests.
  57. func LiveMoveVolume(grpcDialOption grpc.DialOption, writer io.Writer, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, idleTimeout time.Duration, diskType string, skipTailError bool) (err error) {
  58. log.Printf("copying volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  59. lastAppendAtNs, err := copyVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, diskType)
  60. if err != nil {
  61. return fmt.Errorf("copy volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  62. }
  63. log.Printf("tailing volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  64. if err = tailVolume(grpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer, lastAppendAtNs, idleTimeout); err != nil {
  65. if skipTailError {
  66. fmt.Fprintf(writer, "tail volume %d from %s to %s: %v\n", volumeId, sourceVolumeServer, targetVolumeServer, err)
  67. } else {
  68. return fmt.Errorf("tail volume %d from %s to %s: %v", volumeId, sourceVolumeServer, targetVolumeServer, err)
  69. }
  70. }
  71. log.Printf("deleting volume %d from %s", volumeId, sourceVolumeServer)
  72. if err = deleteVolume(grpcDialOption, volumeId, sourceVolumeServer); err != nil {
  73. return fmt.Errorf("delete volume %d from %s: %v", volumeId, sourceVolumeServer, err)
  74. }
  75. log.Printf("moved volume %d from %s to %s", volumeId, sourceVolumeServer, targetVolumeServer)
  76. return nil
  77. }
  78. func copyVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, diskType string) (lastAppendAtNs uint64, err error) {
  79. // check to see if the volume is already read-only and if its not then we need
  80. // to mark it as read-only and then before we return we need to undo what we
  81. // did
  82. var shouldMarkWritable bool
  83. defer func() {
  84. if !shouldMarkWritable {
  85. return
  86. }
  87. clientErr := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  88. _, writableErr := volumeServerClient.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{
  89. VolumeId: uint32(volumeId),
  90. })
  91. return writableErr
  92. })
  93. if clientErr != nil {
  94. log.Printf("failed to mark volume %d as writable after copy from %s: %v", volumeId, sourceVolumeServer, clientErr)
  95. }
  96. }()
  97. err = operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  98. resp, statusErr := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
  99. VolumeId: uint32(volumeId),
  100. })
  101. if statusErr == nil && !resp.IsReadOnly {
  102. shouldMarkWritable = true
  103. _, readonlyErr := volumeServerClient.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{
  104. VolumeId: uint32(volumeId),
  105. })
  106. return readonlyErr
  107. }
  108. return statusErr
  109. })
  110. if err != nil {
  111. return
  112. }
  113. err = operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  114. resp, replicateErr := volumeServerClient.VolumeCopy(context.Background(), &volume_server_pb.VolumeCopyRequest{
  115. VolumeId: uint32(volumeId),
  116. SourceDataNode: sourceVolumeServer,
  117. DiskType: diskType,
  118. })
  119. if replicateErr == nil {
  120. lastAppendAtNs = resp.LastAppendAtNs
  121. }
  122. return replicateErr
  123. })
  124. return
  125. }
  126. func tailVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer, targetVolumeServer string, lastAppendAtNs uint64, idleTimeout time.Duration) (err error) {
  127. return operation.WithVolumeServerClient(targetVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  128. _, replicateErr := volumeServerClient.VolumeTailReceiver(context.Background(), &volume_server_pb.VolumeTailReceiverRequest{
  129. VolumeId: uint32(volumeId),
  130. SinceNs: lastAppendAtNs,
  131. IdleTimeoutSeconds: uint32(idleTimeout.Seconds()),
  132. SourceVolumeServer: sourceVolumeServer,
  133. })
  134. return replicateErr
  135. })
  136. }
  137. func deleteVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  138. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  139. _, deleteErr := volumeServerClient.VolumeDelete(context.Background(), &volume_server_pb.VolumeDeleteRequest{
  140. VolumeId: uint32(volumeId),
  141. })
  142. return deleteErr
  143. })
  144. }
  145. func markVolumeWritable(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string, writable bool) (err error) {
  146. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  147. if writable {
  148. _, err = volumeServerClient.VolumeMarkWritable(context.Background(), &volume_server_pb.VolumeMarkWritableRequest{
  149. VolumeId: uint32(volumeId),
  150. })
  151. } else {
  152. _, err = volumeServerClient.VolumeMarkReadonly(context.Background(), &volume_server_pb.VolumeMarkReadonlyRequest{
  153. VolumeId: uint32(volumeId),
  154. })
  155. }
  156. return err
  157. })
  158. }