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.

159 lines
5.0 KiB

6 years ago
6 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. fmt.Fprintf(&buf, " %s(volume:%d/%d active:%d free:%d remote:%d)", diskType, diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  40. }
  41. return buf.String()
  42. }
  43. func diskInfoToString(diskInfo *master_pb.DiskInfo) string {
  44. var buf bytes.Buffer
  45. fmt.Fprintf(&buf, "volume:%d/%d active:%d free:%d remote:%d", diskInfo.VolumeCount, diskInfo.MaxVolumeCount, diskInfo.ActiveVolumeCount, diskInfo.FreeVolumeCount, diskInfo.RemoteVolumeCount)
  46. return buf.String()
  47. }
  48. func writeTopologyInfo(writer io.Writer, t *master_pb.TopologyInfo, volumeSizeLimitMb uint64) statistics {
  49. fmt.Fprintf(writer, "Topology volumeSizeLimit:%d MB%s\n", volumeSizeLimitMb, diskInfosToString(t.DiskInfos))
  50. sort.Slice(t.DataCenterInfos, func(i, j int) bool {
  51. return t.DataCenterInfos[i].Id < t.DataCenterInfos[j].Id
  52. })
  53. var s statistics
  54. for _, dc := range t.DataCenterInfos {
  55. s = s.plus(writeDataCenterInfo(writer, dc))
  56. }
  57. fmt.Fprintf(writer, "%+v \n", s)
  58. return s
  59. }
  60. func writeDataCenterInfo(writer io.Writer, t *master_pb.DataCenterInfo) statistics {
  61. fmt.Fprintf(writer, " DataCenter %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  62. var s statistics
  63. sort.Slice(t.RackInfos, func(i, j int) bool {
  64. return t.RackInfos[i].Id < t.RackInfos[j].Id
  65. })
  66. for _, r := range t.RackInfos {
  67. s = s.plus(writeRackInfo(writer, r))
  68. }
  69. fmt.Fprintf(writer, " DataCenter %s %+v \n", t.Id, s)
  70. return s
  71. }
  72. func writeRackInfo(writer io.Writer, t *master_pb.RackInfo) statistics {
  73. fmt.Fprintf(writer, " Rack %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  74. var s statistics
  75. sort.Slice(t.DataNodeInfos, func(i, j int) bool {
  76. return t.DataNodeInfos[i].Id < t.DataNodeInfos[j].Id
  77. })
  78. for _, dn := range t.DataNodeInfos {
  79. s = s.plus(writeDataNodeInfo(writer, dn))
  80. }
  81. fmt.Fprintf(writer, " Rack %s %+v \n", t.Id, s)
  82. return s
  83. }
  84. func writeDataNodeInfo(writer io.Writer, t *master_pb.DataNodeInfo) statistics {
  85. fmt.Fprintf(writer, " DataNode %s%s\n", t.Id, diskInfosToString(t.DiskInfos))
  86. var s statistics
  87. for _, diskInfo := range t.DiskInfos {
  88. s = s.plus(writeDiskInfo(writer, diskInfo))
  89. }
  90. fmt.Fprintf(writer, " DataNode %s %+v \n", t.Id, s)
  91. return s
  92. }
  93. func writeDiskInfo(writer io.Writer, t *master_pb.DiskInfo) statistics {
  94. var s statistics
  95. fmt.Fprintf(writer, " Disk %s(%s)\n", t.Type, diskInfoToString(t))
  96. sort.Slice(t.VolumeInfos, func(i, j int) bool {
  97. return t.VolumeInfos[i].Id < t.VolumeInfos[j].Id
  98. })
  99. for _, vi := range t.VolumeInfos {
  100. s = s.plus(writeVolumeInformationMessage(writer, vi))
  101. }
  102. for _, ecShardInfo := range t.EcShardInfos {
  103. fmt.Fprintf(writer, " ec volume id:%v collection:%v shards:%v\n", ecShardInfo.Id, ecShardInfo.Collection, erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds())
  104. }
  105. fmt.Fprintf(writer, " Disk %s %+v \n", t.Type, s)
  106. return s
  107. }
  108. func writeVolumeInformationMessage(writer io.Writer, t *master_pb.VolumeInformationMessage) statistics {
  109. fmt.Fprintf(writer, " volume %+v \n", t)
  110. return newStatistics(t)
  111. }
  112. type statistics struct {
  113. Size uint64
  114. FileCount uint64
  115. DeletedFileCount uint64
  116. DeletedBytes uint64
  117. }
  118. func newStatistics(t *master_pb.VolumeInformationMessage) statistics {
  119. return statistics{
  120. Size: t.Size,
  121. FileCount: t.FileCount,
  122. DeletedFileCount: t.DeleteCount,
  123. DeletedBytes: t.DeletedByteCount,
  124. }
  125. }
  126. func (s statistics) plus(t statistics) statistics {
  127. return statistics{
  128. Size: s.Size + t.Size,
  129. FileCount: s.FileCount + t.FileCount,
  130. DeletedFileCount: s.DeletedFileCount + t.DeletedFileCount,
  131. DeletedBytes: s.DeletedBytes + t.DeletedBytes,
  132. }
  133. }
  134. func (s statistics) String() string {
  135. if s.DeletedFileCount > 0 {
  136. return fmt.Sprintf("total size:%d file_count:%d deleted_file:%d deleted_bytes:%d", s.Size, s.FileCount, s.DeletedFileCount, s.DeletedBytes)
  137. }
  138. return fmt.Sprintf("total size:%d file_count:%d", s.Size, s.FileCount)
  139. }