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.

166 lines
5.1 KiB

6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  8. "io"
  9. "sort"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandVolumeList{})
  13. }
  14. type commandVolumeList struct {
  15. }
  16. func (c *commandVolumeList) Name() string {
  17. return "volume.list"
  18. }
  19. func (c *commandVolumeList) Help() string {
  20. return `list all volumes
  21. This command list all volumes as a tree of dataCenter > rack > dataNode > volume.
  22. `
  23. }
  24. func (c *commandVolumeList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  25. var resp *master_pb.VolumeListResponse
  26. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  27. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  28. return err
  29. })
  30. if err != nil {
  31. return err
  32. }
  33. writeTopologyInfo(writer, resp.TopologyInfo, resp.VolumeSizeLimitMb)
  34. return nil
  35. }
  36. func diskInfosToString(diskInfos map[string]*master_pb.DiskInfo) string {
  37. var buf bytes.Buffer
  38. for diskType, diskInfo := range diskInfos {
  39. if diskType == "" {
  40. diskType = "hdd"
  41. }
  42. fmt.Fprintf(&buf, " %s(volume:%d/%d active:%d free:%d remote:%d)", diskType, diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  43. }
  44. return buf.String()
  45. }
  46. func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
  47. var buf bytes.Buffer
  48. fmt.Fprintf(&buf, "volume:%d/%d active:%d free:%d remote:%d", diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  49. return buf.String()
  50. }
  51. func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64) statistics {
  52. fmt.Fprintf(writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
  53. sort.Slice(t.DataCenterInfos, func(i, j int) bool {
  54. return t.DataCenterInfos[i].Id < t.DataCenterInfos[j].Id
  55. })
  56. var s statistics
  57. for _, dc := range t.DataCenterInfos {
  58. s = s.plus(writeDataCenterInfo(writer, dc))
  59. }
  60. fmt.Fprintf(writer, "%+v \n", s)
  61. return s
  62. }
  63. func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo) statistics {
  64. fmt.Fprintf(writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  65. var s statistics
  66. sort.Slice(t.RackInfos, func(i, j int) bool {
  67. return t.RackInfos[i].Id < t.RackInfos[j].Id
  68. })
  69. for _, r := range t.RackInfos {
  70. s = s.plus(writeRackInfo(writer, r))
  71. }
  72. fmt.Fprintf(writer, " DataCenter %s %+v \n", t.Id, s)
  73. return s
  74. }
  75. func writeRackInfo(writer io.Writer, t *master_pb.RackInfo) statistics {
  76. fmt.Fprintf(writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  77. var s statistics
  78. sort.Slice(t.DataNodeInfos, func(i, j int) bool {
  79. return t.DataNodeInfos[i].Id < t.DataNodeInfos[j].Id
  80. })
  81. for _, dn := range t.DataNodeInfos {
  82. s = s.plus(writeDataNodeInfo(writer, dn))
  83. }
  84. fmt.Fprintf(writer, " Rack %s %+v \n", t.Id, s)
  85. return s
  86. }
  87. func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo) statistics {
  88. fmt.Fprintf(writer, " DataNode %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  89. var s statistics
  90. for _, diskInfo := range t.DiskInfos {
  91. s = s.plus(writeDiskInfo(writer, diskInfo))
  92. }
  93. fmt.Fprintf(writer, " DataNode %s %+v \n", t.Id, s)
  94. return s
  95. }
  96. func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo) statistics {
  97. var s statistics
  98. diskType := t.Type
  99. if diskType == "" {
  100. diskType = "hdd"
  101. }
  102. fmt.Fprintf(writer, " Disk %s(%s)\n", diskType, diskInfoToString(t))
  103. sort.Slice(t.VolumeInfos, func(i, j int) bool {
  104. return t.VolumeInfos[i].Id < t.VolumeInfos[j].Id
  105. })
  106. for _, vi := range t.VolumeInfos {
  107. s = s.plus(writeVolumeInformationMessage(writer, vi))
  108. }
  109. for _, ecShardInfo := range t.EcShardInfos {
  110. fmt.Fprintf(writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
  111. }
  112. fmt.Fprintf(writer, " Disk %s %+v \n", diskType, s)
  113. return s
  114. }
  115. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage) statistics {
  116. fmt.Fprintf(writer, " volume %+v \n", t)
  117. return newStatistics(t)
  118. }
  119. type statistics struct {
  120. Size uint64
  121. FileCount uint64
  122. DeletedFileCount uint64
  123. DeletedBytes uint64
  124. }
  125. func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
  126. return statistics{
  127. Size: t.Size,
  128. FileCount: t.FileCount,
  129. DeletedFileCount: t.DeleteCount,
  130. DeletedBytes: t.DeletedByteCount,
  131. }
  132. }
  133. func (s statistics) plus(t statistics) statistics {
  134. return statistics{
  135. Size: s.Size + t.Size,
  136. FileCount: s.FileCount + t.FileCount,
  137. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  138. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  139. }
  140. }
  141. func (s statistics) String() string {
  142. if s.DeletedFileCount > 0 {
  143. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  144. }
  145. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  146. }