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.

130 lines
3.6 KiB

3 years ago
4 years ago
4 years ago
4 years ago
2 years ago
3 years ago
  1. package shell
  2. import (
  3. _ "embed"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  6. "github.com/stretchr/testify/assert"
  7. // "google.golang.org/protobuf/proto"
  8. //"github.com/golang/protobuf/proto"
  9. jsonpb "google.golang.org/protobuf/encoding/protojson"
  10. "strconv"
  11. "strings"
  12. "testing"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  14. )
  15. func TestParsing(t *testing.T) {
  16. topo := parseOutput(topoData)
  17. assert.Equal(t, 5, len(topo.DataCenterInfos))
  18. topo = parseOutput(topoData2)
  19. dataNodes := topo.DataCenterInfos[0].RackInfos[0].DataNodeInfos
  20. assert.Equal(t, 14, len(dataNodes))
  21. diskInfo := dataNodes[0].DiskInfos[""]
  22. assert.Equal(t, 1559, len(diskInfo.VolumeInfos))
  23. assert.Equal(t, 6740, len(diskInfo.EcShardInfos))
  24. }
  25. func parseOutput(output string) *master_pb.TopologyInfo {
  26. lines := strings.Split(output, "\n")
  27. var topo *master_pb.TopologyInfo
  28. var dc *master_pb.DataCenterInfo
  29. var rack *master_pb.RackInfo
  30. var dn *master_pb.DataNodeInfo
  31. var disk *master_pb.DiskInfo
  32. for _, line := range lines {
  33. line = strings.TrimSpace(line)
  34. parts := strings.Split(line, " ")
  35. switch parts[0] {
  36. case "Topology":
  37. if topo == nil {
  38. topo = &master_pb.TopologyInfo{}
  39. }
  40. case "DataCenter":
  41. if dc == nil {
  42. dc = &master_pb.DataCenterInfo{
  43. Id: parts[1],
  44. }
  45. topo.DataCenterInfos = append(topo.DataCenterInfos, dc)
  46. } else {
  47. dc = nil
  48. }
  49. case "Rack":
  50. if rack == nil {
  51. rack = &master_pb.RackInfo{
  52. Id: parts[1],
  53. }
  54. dc.RackInfos = append(dc.RackInfos, rack)
  55. } else {
  56. rack = nil
  57. }
  58. case "DataNode":
  59. if dn == nil {
  60. dn = &master_pb.DataNodeInfo{
  61. Id: parts[1],
  62. DiskInfos: make(map[string]*master_pb.DiskInfo),
  63. }
  64. rack.DataNodeInfos = append(rack.DataNodeInfos, dn)
  65. } else {
  66. dn = nil
  67. }
  68. case "Disk":
  69. if disk == nil {
  70. diskType := parts[1][:strings.Index(parts[1], "(")]
  71. volumeCountStr := parts[1][strings.Index(parts[1], ":")+1 : strings.Index(parts[1], "/")]
  72. maxVolumeCountStr := parts[1][strings.Index(parts[1], "/")+1:]
  73. maxVolumeCount, _ := strconv.Atoi(maxVolumeCountStr)
  74. volumeCount, _ := strconv.Atoi(volumeCountStr)
  75. disk = &master_pb.DiskInfo{
  76. Type: diskType,
  77. MaxVolumeCount: int64(maxVolumeCount),
  78. VolumeCount: int64(volumeCount),
  79. }
  80. dn.DiskInfos[types.ToDiskType(diskType).String()] = disk
  81. } else {
  82. disk = nil
  83. }
  84. case "volume":
  85. volumeLine := line[len("volume "):]
  86. volume := &master_pb.VolumeInformationMessage{}
  87. jsonpb.Unmarshal([]byte(volumeLine), volume)
  88. disk.VolumeInfos = append(disk.VolumeInfos, volume)
  89. case "ec":
  90. ecVolumeLine := line[len("ec volume "):]
  91. ecShard := &master_pb.VolumeEcShardInformationMessage{}
  92. for _, part := range strings.Split(ecVolumeLine, " ") {
  93. if strings.HasPrefix(part, "id:") {
  94. id, _ := strconv.ParseInt(part[len("id:"):], 10, 64)
  95. ecShard.Id = uint32(id)
  96. }
  97. if strings.HasPrefix(part, "collection:") {
  98. ecShard.Collection = part[len("collection:"):]
  99. }
  100. if strings.HasPrefix(part, "shards:") {
  101. shards := part[len("shards:["):]
  102. shards = strings.TrimRight(shards, "]")
  103. shardBits := erasure_coding.ShardBits(0)
  104. for _, shardId := range strings.Split(shards, ",") {
  105. sid, _ := strconv.Atoi(shardId)
  106. shardBits = shardBits.AddShardId(erasure_coding.ShardId(sid))
  107. }
  108. ecShard.EcIndexBits = uint32(shardBits)
  109. }
  110. }
  111. disk.EcShardInfos = append(disk.EcShardInfos, ecShard)
  112. }
  113. }
  114. return topo
  115. }
  116. //go:embed volume.list.txt
  117. var topoData string
  118. //go:embed volume.list2.txt
  119. var topoData2 string