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.

203 lines
8.2 KiB

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