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.

118 lines
3.2 KiB

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