command_volume_server_leave.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "google.golang.org/grpc"
  9. "io"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandVolumeServerLeave{})
  13. }
  14. type commandVolumeServerLeave struct {
  15. }
  16. func (c *commandVolumeServerLeave) Name() string {
  17. return "volumeServer.leave"
  18. }
  19. func (c *commandVolumeServerLeave) Help() string {
  20. return `stop a volume server from sending heartbeats to the master
  21. volume.unmount -node <volume server host:port> -force
  22. This command enables gracefully shutting down the volume server.
  23. The volume server will stop sending heartbeats to the master.
  24. After draining the traffic for a few seconds, you can safely shut down the volume server.
  25. This operation is not revocable unless the volume server is restarted.
  26. `
  27. }
  28. func (c *commandVolumeServerLeave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. if err = commandEnv.confirmIsLocked(); err != nil {
  30. return
  31. }
  32. vsLeaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  33. volumeServer := vsLeaveCommand.String("node", "", "<host>:<port> of the volume server")
  34. if err = vsLeaveCommand.Parse(args); err != nil {
  35. return nil
  36. }
  37. if *volumeServer == "" {
  38. return fmt.Errorf("need to specify volume server by -node=<host>:<port>")
  39. }
  40. return volumeServerLeave(commandEnv.option.GrpcDialOption, *volumeServer, writer)
  41. }
  42. func volumeServerLeave(grpcDialOption grpc.DialOption, volumeServer string, writer io.Writer) (err error) {
  43. return operation.WithVolumeServerClient(volumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  44. _, leaveErr := volumeServerClient.VolumeServerLeave(context.Background(), &volume_server_pb.VolumeServerLeaveRequest{})
  45. if leaveErr != nil {
  46. fmt.Fprintf(writer, "ask volume server %s to leave: %v\n", volumeServer, leaveErr)
  47. } else {
  48. fmt.Fprintf(writer, "stopped heartbeat in volume server %s. After a few seconds to drain traffic, it will be safe to stop the volume server.\n", volumeServer)
  49. }
  50. return leaveErr
  51. })
  52. }