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.

116 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "io"
  7. )
  8. func init() {
  9. commands = append(commands, &commandVolumeList{})
  10. }
  11. type commandVolumeList struct {
  12. }
  13. func (c *commandVolumeList) Name() string {
  14. return "volume.list"
  15. }
  16. func (c *commandVolumeList) Help() string {
  17. return `list all volumes
  18. This command list all volumes as a tree of dataCenter > rack > dataNode > volume.
  19. `
  20. }
  21. func (c *commandVolumeList) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  22. var resp *master_pb.VolumeListResponse
  23. ctx := context.Background()
  24. err = commandEnv.masterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  25. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  26. return err
  27. })
  28. if err != nil {
  29. return err
  30. }
  31. writeTopologyInfo(writer, resp.TopologyInfo)
  32. return nil
  33. }
  34. func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo) statistics {
  35. fmt.Fprintf(writer, "Topology volume:%d/%d active:%d free:%d\n", t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount)
  36. var s statistics
  37. for _, dc := range t.DataCenterInfos {
  38. s = s.plus(writeDataCenterInfo(writer, dc))
  39. }
  40. fmt.Fprintf(writer, "%+v \n", s)
  41. return s
  42. }
  43. func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo) statistics {
  44. fmt.Fprintf(writer, " DataCenter %s volume:%d/%d active:%d free:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount)
  45. var s statistics
  46. for _, r := range t.RackInfos {
  47. s = s.plus(writeRackInfo(writer, r))
  48. }
  49. fmt.Fprintf(writer, " DataCenter %s %+v \n", t.Id, s)
  50. return s
  51. }
  52. func writeRackInfo(writer io.Writer, t *master_pb.RackInfo) statistics {
  53. fmt.Fprintf(writer, " Rack %s volume:%d/%d active:%d free:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount)
  54. var s statistics
  55. for _, dn := range t.DataNodeInfos {
  56. s = s.plus(writeDataNodeInfo(writer, dn))
  57. }
  58. fmt.Fprintf(writer, " Rack %s %+v \n", t.Id, s)
  59. return s
  60. }
  61. func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo) statistics {
  62. fmt.Fprintf(writer, " DataNode %s volume:%d/%d active:%d free:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount)
  63. var s statistics
  64. for _, vi := range t.VolumeInfos {
  65. s = s.plus(writeVolumeInformationMessage(writer, vi))
  66. }
  67. fmt.Fprintf(writer, " DataNode %s %+v \n", t.Id, s)
  68. return s
  69. }
  70. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage) statistics {
  71. fmt.Fprintf(writer, " volume %+v \n", t)
  72. return newStatiscis(t)
  73. }
  74. type statistics struct {
  75. Size uint64
  76. FileCount uint64
  77. DeletedFileCount uint64
  78. DeletedBytes uint64
  79. }
  80. func newStatiscis(t *master_pb.VolumeInformationMessage) statistics {
  81. return statistics{
  82. Size: t.Size,
  83. FileCount: t.FileCount,
  84. DeletedFileCount: t.DeleteCount,
  85. DeletedBytes: t.DeletedByteCount,
  86. }
  87. }
  88. func (s statistics) plus(t statistics) statistics {
  89. return statistics{
  90. Size: s.Size + t.Size,
  91. FileCount: s.FileCount + t.FileCount,
  92. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  93. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  94. }
  95. }
  96. func (s statistics) String() string {
  97. if s.DeletedFileCount > 0 {
  98. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  99. }
  100. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  101. }