command_cluster_ps.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/cluster"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "io"
  13. "time"
  14. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandClusterPs{})
  18. }
  19. type commandClusterPs struct {
  20. }
  21. func (c *commandClusterPs) Name() string {
  22. return "cluster.ps"
  23. }
  24. func (c *commandClusterPs) Help() string {
  25. return `check current cluster process status
  26. cluster.ps
  27. `
  28. }
  29. func (c *commandClusterPs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  30. clusterPsCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  31. if err = clusterPsCommand.Parse(args); err != nil {
  32. return nil
  33. }
  34. var filerNodes []*master_pb.ListClusterNodesResponse_ClusterNode
  35. var mqBrokerNodes []*master_pb.ListClusterNodesResponse_ClusterNode
  36. // get the list of filers
  37. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  38. resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
  39. ClientType: cluster.FilerType,
  40. FilerGroup: *commandEnv.option.FilerGroup,
  41. })
  42. if err != nil {
  43. return err
  44. }
  45. filerNodes = resp.ClusterNodes
  46. return err
  47. })
  48. if err != nil {
  49. return
  50. }
  51. // get the list of message queue brokers
  52. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  53. resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
  54. ClientType: cluster.BrokerType,
  55. FilerGroup: *commandEnv.option.FilerGroup,
  56. })
  57. if err != nil {
  58. return err
  59. }
  60. mqBrokerNodes = resp.ClusterNodes
  61. return err
  62. })
  63. if err != nil {
  64. return
  65. }
  66. if len(mqBrokerNodes) > 0 {
  67. fmt.Fprintf(writer, "* message queue brokers %d\n", len(mqBrokerNodes))
  68. for _, node := range mqBrokerNodes {
  69. fmt.Fprintf(writer, " * %s (%v)\n", node.Address, node.Version)
  70. if node.DataCenter != "" {
  71. fmt.Fprintf(writer, " DataCenter: %v\n", node.DataCenter)
  72. }
  73. if node.Rack != "" {
  74. fmt.Fprintf(writer, " Rack: %v\n", node.Rack)
  75. }
  76. }
  77. }
  78. filerSignatures := make(map[*master_pb.ListClusterNodesResponse_ClusterNode]int32)
  79. fmt.Fprintf(writer, "* filers %d\n", len(filerNodes))
  80. for _, node := range filerNodes {
  81. fmt.Fprintf(writer, " * %s (%v) %v\n", node.Address, node.Version, time.Unix(0, node.CreatedAtNs).UTC())
  82. if node.DataCenter != "" {
  83. fmt.Fprintf(writer, " DataCenter: %v\n", node.DataCenter)
  84. }
  85. if node.Rack != "" {
  86. fmt.Fprintf(writer, " Rack: %v\n", node.Rack)
  87. }
  88. pb.WithFilerClient(false, 0, pb.ServerAddress(node.Address), commandEnv.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  89. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  90. if err == nil {
  91. if resp.FilerGroup != "" {
  92. fmt.Fprintf(writer, " filer group: %s\n", resp.FilerGroup)
  93. }
  94. fmt.Fprintf(writer, " signature: %d\n", resp.Signature)
  95. filerSignatures[node] = resp.Signature
  96. } else {
  97. fmt.Fprintf(writer, " failed to connect: %v\n", err)
  98. }
  99. return err
  100. })
  101. }
  102. for _, node := range filerNodes {
  103. pb.WithFilerClient(false, 0, pb.ServerAddress(node.Address), commandEnv.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  104. fmt.Fprintf(writer, "* filer %s metadata sync time\n", node.Address)
  105. selfSignature := filerSignatures[node]
  106. for peer, peerSignature := range filerSignatures {
  107. if selfSignature == peerSignature {
  108. continue
  109. }
  110. if resp, err := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: filer.GetPeerMetaOffsetKey(peerSignature)}); err == nil && len(resp.Value) == 8 {
  111. lastTsNs := int64(util.BytesToUint64(resp.Value))
  112. fmt.Fprintf(writer, " %s: %v\n", peer.Address, time.Unix(0, lastTsNs).UTC())
  113. }
  114. }
  115. return nil
  116. })
  117. }
  118. // collect volume servers
  119. var volumeServers []pb.ServerAddress
  120. t, _, err := collectTopologyInfo(commandEnv, 0)
  121. if err != nil {
  122. return err
  123. }
  124. for _, dc := range t.DataCenterInfos {
  125. for _, r := range dc.RackInfos {
  126. for _, dn := range r.DataNodeInfos {
  127. volumeServers = append(volumeServers, pb.NewServerAddressFromDataNode(dn))
  128. }
  129. }
  130. }
  131. fmt.Fprintf(writer, "* volume servers %d\n", len(volumeServers))
  132. for _, dc := range t.DataCenterInfos {
  133. fmt.Fprintf(writer, " * data center: %s\n", dc.Id)
  134. for _, r := range dc.RackInfos {
  135. fmt.Fprintf(writer, " * rack: %s\n", r.Id)
  136. for _, dn := range r.DataNodeInfos {
  137. pb.WithVolumeServerClient(false, pb.NewServerAddressFromDataNode(dn), commandEnv.option.GrpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  138. resp, err := client.VolumeServerStatus(context.Background(), &volume_server_pb.VolumeServerStatusRequest{})
  139. if err == nil {
  140. fmt.Fprintf(writer, " * %s (%v)\n", dn.Id, resp.Version)
  141. }
  142. return err
  143. })
  144. }
  145. }
  146. }
  147. return nil
  148. }