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.

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