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.

266 lines
8.7 KiB

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