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.

222 lines
7.3 KiB

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