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.

129 lines
4.0 KiB

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