command_volume_list.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package shell
  2. import (
  3. "bytes"
  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. // collect topology information
  25. topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv)
  26. if err != nil {
  27. return err
  28. }
  29. writeTopologyInfo(writer, topologyInfo, volumeSizeLimitMb)
  30. return nil
  31. }
  32. func diskInfosToString(diskInfos map[string]*master_pb.DiskInfo) string {
  33. var buf bytes.Buffer
  34. for diskType, diskInfo := range diskInfos {
  35. if diskType == "" {
  36. diskType = "hdd"
  37. }
  38. fmt.Fprintf(&buf, " %s(volume:%d/%d active:%d free:%d remote:%d)", diskType, diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  39. }
  40. return buf.String()
  41. }
  42. func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
  43. var buf bytes.Buffer
  44. fmt.Fprintf(&buf, "volume:%d/%d active:%d free:%d remote:%d", diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  45. return buf.String()
  46. }
  47. func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64) statistics {
  48. fmt.Fprintf(writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
  49. sort.Slice(t.DataCenterInfos, func(i, j int) bool {
  50. return t.DataCenterInfos[i].Id < t.DataCenterInfos[j].Id
  51. })
  52. var s statistics
  53. for _, dc := range t.DataCenterInfos {
  54. s = s.plus(writeDataCenterInfo(writer, dc))
  55. }
  56. fmt.Fprintf(writer, "%+v \n", s)
  57. return s
  58. }
  59. func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo) statistics {
  60. fmt.Fprintf(writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  61. var s statistics
  62. sort.Slice(t.RackInfos, func(i, j int) bool {
  63. return t.RackInfos[i].Id < t.RackInfos[j].Id
  64. })
  65. for _, r := range t.RackInfos {
  66. s = s.plus(writeRackInfo(writer, r))
  67. }
  68. fmt.Fprintf(writer, " DataCenter %s %+v \n", t.Id, s)
  69. return s
  70. }
  71. func writeRackInfo(writer io.Writer, t *master_pb.RackInfo) statistics {
  72. fmt.Fprintf(writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  73. var s statistics
  74. sort.Slice(t.DataNodeInfos, func(i, j int) bool {
  75. return t.DataNodeInfos[i].Id < t.DataNodeInfos[j].Id
  76. })
  77. for _, dn := range t.DataNodeInfos {
  78. s = s.plus(writeDataNodeInfo(writer, dn))
  79. }
  80. fmt.Fprintf(writer, " Rack %s %+v \n", t.Id, s)
  81. return s
  82. }
  83. func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo) statistics {
  84. fmt.Fprintf(writer, " DataNode %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  85. var s statistics
  86. for _, diskInfo := range t.DiskInfos {
  87. s = s.plus(writeDiskInfo(writer, diskInfo))
  88. }
  89. fmt.Fprintf(writer, " DataNode %s %+v \n", t.Id, s)
  90. return s
  91. }
  92. func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo) statistics {
  93. var s statistics
  94. diskType := t.Type
  95. if diskType == "" {
  96. diskType = "hdd"
  97. }
  98. fmt.Fprintf(writer, " Disk %s(%s)\n", diskType, diskInfoToString(t))
  99. sort.Slice(t.VolumeInfos, func(i, j int) bool {
  100. return t.VolumeInfos[i].Id < t.VolumeInfos[j].Id
  101. })
  102. for _, vi := range t.VolumeInfos {
  103. s = s.plus(writeVolumeInformationMessage(writer, vi))
  104. }
  105. for _, ecShardInfo := range t.EcShardInfos {
  106. fmt.Fprintf(writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
  107. }
  108. fmt.Fprintf(writer, " Disk %s %+v \n", diskType, s)
  109. return s
  110. }
  111. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage) statistics {
  112. fmt.Fprintf(writer, " volume %+v \n", t)
  113. return newStatistics(t)
  114. }
  115. type statistics struct {
  116. Size uint64
  117. FileCount uint64
  118. DeletedFileCount uint64
  119. DeletedBytes uint64
  120. }
  121. func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
  122. return statistics{
  123. Size: t.Size,
  124. FileCount: t.FileCount,
  125. DeletedFileCount: t.DeleteCount,
  126. DeletedBytes: t.DeletedByteCount,
  127. }
  128. }
  129. func (s statistics) plus(t statistics) statistics {
  130. return statistics{
  131. Size: s.Size + t.Size,
  132. FileCount: s.FileCount + t.FileCount,
  133. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  134. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  135. }
  136. }
  137. func (s statistics) String() string {
  138. if s.DeletedFileCount > 0 {
  139. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  140. }
  141. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  142. }