command_volume_list.go 7.3 KB

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