command_cluster_raft_ps.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "io"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandRaftClusterPs{})
  11. }
  12. type commandRaftClusterPs struct {
  13. }
  14. func (c *commandRaftClusterPs) Name() string {
  15. return "cluster.raft.ps"
  16. }
  17. func (c *commandRaftClusterPs) Help() string {
  18. return `check current raft cluster status
  19. cluster.raft.ps
  20. `
  21. }
  22. func (c *commandRaftClusterPs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. raftClusterPsCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  24. if err = raftClusterPsCommand.Parse(args); err != nil {
  25. return nil
  26. }
  27. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  28. resp, err := client.RaftListClusterServers(context.Background(), &master_pb.RaftListClusterServersRequest{})
  29. if err != nil {
  30. return fmt.Errorf("raft list cluster: %v", err)
  31. }
  32. fmt.Fprintf(writer, "the raft cluster has %d servers\n", len(resp.ClusterServers))
  33. for _, server := range resp.ClusterServers {
  34. suffrage := server.Suffrage
  35. if server.IsLeader {
  36. suffrage = "Leader"
  37. }
  38. fmt.Fprintf(writer, " * %s %s (%s)\n", server.Id, server.Address, suffrage)
  39. }
  40. return nil
  41. })
  42. return err
  43. }