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.

129 lines
3.5 KiB

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