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.

175 lines
5.7 KiB

4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  8. "golang.org/x/exp/slices"
  9. "io"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandVolumeList{})
  13. }
  14. type commandVolumeList struct {
  15. }
  16. func (c *commandVolumeList) Name() string {
  17. return "volume.list"
  18. }
  19. func (c *commandVolumeList) Help() string {
  20. return `list all volumes
  21. This command list all volumes as a tree of dataCenter > rack > dataNode > volume.
  22. `
  23. }
  24. func (c *commandVolumeList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  25. volumeListCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  26. verbosityLevel := volumeListCommand.Int("v", 5, "verbose mode: 0, 1, 2, 3, 4, 5")
  27. if err = volumeListCommand.Parse(args); err != nil {
  28. return nil
  29. }
  30. // collect topology information
  31. topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv, 0)
  32. if err != nil {
  33. return err
  34. }
  35. writeTopologyInfo(writer, topologyInfo, volumeSizeLimitMb, *verbosityLevel)
  36. return nil
  37. }
  38. func diskInfosToString(diskInfos map[string]*master_pb.DiskInfo) string {
  39. var buf bytes.Buffer
  40. for diskType, diskInfo := range diskInfos {
  41. if diskType == "" {
  42. diskType = "hdd"
  43. }
  44. fmt.Fprintf(&buf, " %s(volume:%d/%d active:%d free:%d remote:%d)", diskType, diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  45. }
  46. return buf.String()
  47. }
  48. func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
  49. var buf bytes.Buffer
  50. fmt.Fprintf(&buf, "volume:%d/%d active:%d free:%d remote:%d", diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  51. return buf.String()
  52. }
  53. func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64, verbosityLevel int) statistics {
  54. output(verbosityLevel >= 0, writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
  55. slices.SortFunc(t.DataCenterInfos, func(a, b *master_pb.DataCenterInfo) bool {
  56. return a.Id < b.Id
  57. })
  58. var s statistics
  59. for _, dc := range t.DataCenterInfos {
  60. s = s.plus(writeDataCenterInfo(writer, dc, verbosityLevel))
  61. }
  62. output(verbosityLevel >= 0, writer, "%+v \n", s)
  63. return s
  64. }
  65. func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo, verbosityLevel int) statistics {
  66. output(verbosityLevel >= 1, writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  67. var s statistics
  68. slices.SortFunc(t.RackInfos, func(a, b *master_pb.RackInfo) bool {
  69. return a.Id < b.Id
  70. })
  71. for _, r := range t.RackInfos {
  72. s = s.plus(writeRackInfo(writer, r, verbosityLevel))
  73. }
  74. output(verbosityLevel >= 1, writer, " DataCenter %s %+v \n", t.Id, s)
  75. return s
  76. }
  77. func writeRackInfo(writer io.Writer, t *master_pb.RackInfo, verbosityLevel int) statistics {
  78. output(verbosityLevel >= 2, writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  79. var s statistics
  80. slices.SortFunc(t.DataNodeInfos, func(a, b *master_pb.DataNodeInfo) bool {
  81. return a.Id < b.Id
  82. })
  83. for _, dn := range t.DataNodeInfos {
  84. s = s.plus(writeDataNodeInfo(writer, dn, verbosityLevel))
  85. }
  86. output(verbosityLevel >= 2, writer, " Rack %s %+v \n", t.Id, s)
  87. return s
  88. }
  89. func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo, verbosityLevel int) statistics {
  90. output(verbosityLevel >= 3, writer, " DataNode %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  91. var s statistics
  92. for _, diskInfo := range t.DiskInfos {
  93. s = s.plus(writeDiskInfo(writer, diskInfo, verbosityLevel))
  94. }
  95. output(verbosityLevel >= 3, writer, " DataNode %s %+v \n", t.Id, s)
  96. return s
  97. }
  98. func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo, verbosityLevel int) statistics {
  99. var s statistics
  100. diskType := t.Type
  101. if diskType == "" {
  102. diskType = "hdd"
  103. }
  104. output(verbosityLevel >= 4, writer, " Disk %s(%s)\n", diskType, diskInfoToString(t))
  105. slices.SortFunc(t.VolumeInfos, func(a, b *master_pb.VolumeInformationMessage) bool {
  106. return a.Id < b.Id
  107. })
  108. for _, vi := range t.VolumeInfos {
  109. s = s.plus(writeVolumeInformationMessage(writer, vi, verbosityLevel))
  110. }
  111. for _, ecShardInfo := range t.EcShardInfos {
  112. output(verbosityLevel >= 5, writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
  113. }
  114. output(verbosityLevel >= 4, writer, " Disk %s %+v \n", diskType, s)
  115. return s
  116. }
  117. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage, verbosityLevel int) statistics {
  118. output(verbosityLevel >= 5, writer, " volume %+v \n", t)
  119. return newStatistics(t)
  120. }
  121. func output(condition bool, w io.Writer, format string, a ...interface{}) {
  122. if condition {
  123. fmt.Fprintf(w, format, a...)
  124. }
  125. }
  126. type statistics struct {
  127. Size uint64
  128. FileCount uint64
  129. DeletedFileCount uint64
  130. DeletedBytes uint64
  131. }
  132. func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
  133. return statistics{
  134. Size: t.Size,
  135. FileCount: t.FileCount,
  136. DeletedFileCount: t.DeleteCount,
  137. DeletedBytes: t.DeletedByteCount,
  138. }
  139. }
  140. func (s statistics) plus(t statistics) statistics {
  141. return statistics{
  142. Size: s.Size + t.Size,
  143. FileCount: s.FileCount + t.FileCount,
  144. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  145. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  146. }
  147. }
  148. func (s statistics) String() string {
  149. if s.DeletedFileCount > 0 {
  150. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  151. }
  152. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  153. }