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.

68 lines
2.2 KiB

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