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.

273 lines
9.0 KiB

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