command_volume_list.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  26. resp, err = client.VolumeList(context.Background(), &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 remote:%d volumeSizeLimit:%d MB\n", t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount, t.RemoteVolumeCount, 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 remote:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount, t.RemoteVolumeCount)
  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 remote:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount, t.RemoteVolumeCount)
  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 remote:%d\n", t.Id, t.VolumeCount, t.MaxVolumeCount, t.ActiveVolumeCount, t.FreeVolumeCount, t.RemoteVolumeCount)
  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. for _, ecShardInfo := range t.EcShardInfos {
  81. fmt.Fprintf(writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
  82. }
  83. fmt.Fprintf(writer, " DataNode %s %+v \n", t.Id, s)
  84. return s
  85. }
  86. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage) statistics {
  87. fmt.Fprintf(writer, " volume %+v \n", t)
  88. return newStatistics(t)
  89. }
  90. type statistics struct {
  91. Size uint64
  92. FileCount uint64
  93. DeletedFileCount uint64
  94. DeletedBytes uint64
  95. }
  96. func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
  97. return statistics{
  98. Size: t.Size,
  99. FileCount: t.FileCount,
  100. DeletedFileCount: t.DeleteCount,
  101. DeletedBytes: t.DeletedByteCount,
  102. }
  103. }
  104. func (s statistics) plus(t statistics) statistics {
  105. return statistics{
  106. Size: s.Size + t.Size,
  107. FileCount: s.FileCount + t.FileCount,
  108. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  109. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  110. }
  111. }
  112. func (s statistics) String() string {
  113. if s.DeletedFileCount > 0 {
  114. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  115. }
  116. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  117. }