command_volume_list.go 4.3 KB

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