command_volume_move.go 8.8 KB

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