command_volume_list.go 7.6 KB

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