2021-03-15 11:23:19 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2022-02-08 17:14:27 +08:00
|
|
|
_ "embed"
|
2022-10-10 10:21:23 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2021-03-15 11:23:19 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-03-17 16:00:43 +08:00
|
|
|
//"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-03-15 11:23:19 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2021-03-15 11:23:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParsing(t *testing.T) {
|
|
|
|
topo := parseOutput(topoData)
|
|
|
|
|
|
|
|
assert.Equal(t, 5, len(topo.DataCenterInfos))
|
|
|
|
|
2022-10-10 13:47:55 +08:00
|
|
|
topo = parseOutput(topoData2)
|
|
|
|
|
|
|
|
dataNodes := topo.DataCenterInfos[0].RackInfos[0].DataNodeInfos
|
|
|
|
assert.Equal(t, 14, len(dataNodes))
|
|
|
|
diskInfo := dataNodes[0].DiskInfos[""]
|
|
|
|
assert.Equal(t, 1559, len(diskInfo.VolumeInfos))
|
|
|
|
assert.Equal(t, 6740, len(diskInfo.EcShardInfos))
|
|
|
|
|
2021-03-15 11:23:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseOutput(output string) *master_pb.TopologyInfo {
|
|
|
|
lines := strings.Split(output, "\n")
|
|
|
|
var topo *master_pb.TopologyInfo
|
|
|
|
var dc *master_pb.DataCenterInfo
|
|
|
|
var rack *master_pb.RackInfo
|
|
|
|
var dn *master_pb.DataNodeInfo
|
|
|
|
var disk *master_pb.DiskInfo
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
parts := strings.Split(line, " ")
|
|
|
|
switch parts[0] {
|
|
|
|
case "Topology":
|
|
|
|
if topo == nil {
|
|
|
|
topo = &master_pb.TopologyInfo{}
|
|
|
|
}
|
|
|
|
case "DataCenter":
|
|
|
|
if dc == nil {
|
|
|
|
dc = &master_pb.DataCenterInfo{
|
|
|
|
Id: parts[1],
|
|
|
|
}
|
|
|
|
topo.DataCenterInfos = append(topo.DataCenterInfos, dc)
|
|
|
|
} else {
|
|
|
|
dc = nil
|
|
|
|
}
|
|
|
|
case "Rack":
|
|
|
|
if rack == nil {
|
|
|
|
rack = &master_pb.RackInfo{
|
|
|
|
Id: parts[1],
|
|
|
|
}
|
|
|
|
dc.RackInfos = append(dc.RackInfos, rack)
|
|
|
|
} else {
|
|
|
|
rack = nil
|
|
|
|
}
|
|
|
|
case "DataNode":
|
|
|
|
if dn == nil {
|
|
|
|
dn = &master_pb.DataNodeInfo{
|
|
|
|
Id: parts[1],
|
|
|
|
DiskInfos: make(map[string]*master_pb.DiskInfo),
|
|
|
|
}
|
|
|
|
rack.DataNodeInfos = append(rack.DataNodeInfos, dn)
|
|
|
|
} else {
|
|
|
|
dn = nil
|
|
|
|
}
|
|
|
|
case "Disk":
|
|
|
|
if disk == nil {
|
|
|
|
diskType := parts[1][:strings.Index(parts[1], "(")]
|
2022-02-08 17:50:05 +08:00
|
|
|
volumeCountStr := parts[1][strings.Index(parts[1], ":")+1 : strings.Index(parts[1], "/")]
|
2021-03-15 11:23:19 +08:00
|
|
|
maxVolumeCountStr := parts[1][strings.Index(parts[1], "/")+1:]
|
|
|
|
maxVolumeCount, _ := strconv.Atoi(maxVolumeCountStr)
|
2022-02-08 17:50:05 +08:00
|
|
|
volumeCount, _ := strconv.Atoi(volumeCountStr)
|
2021-03-15 11:23:19 +08:00
|
|
|
disk = &master_pb.DiskInfo{
|
2021-03-19 16:31:56 +08:00
|
|
|
Type: diskType,
|
2021-08-09 08:56:26 +08:00
|
|
|
MaxVolumeCount: int64(maxVolumeCount),
|
2022-02-08 17:50:05 +08:00
|
|
|
VolumeCount: int64(volumeCount),
|
2021-03-15 11:23:19 +08:00
|
|
|
}
|
2021-03-15 11:50:14 +08:00
|
|
|
dn.DiskInfos[types.ToDiskType(diskType).String()] = disk
|
2021-03-15 11:23:19 +08:00
|
|
|
} else {
|
|
|
|
disk = nil
|
|
|
|
}
|
|
|
|
case "volume":
|
|
|
|
volumeLine := line[len("volume "):]
|
|
|
|
volume := &master_pb.VolumeInformationMessage{}
|
2024-03-17 16:00:43 +08:00
|
|
|
proto.UnmarshalText(volumeLine, volume)
|
2021-03-15 11:23:19 +08:00
|
|
|
disk.VolumeInfos = append(disk.VolumeInfos, volume)
|
2022-10-10 10:21:23 +08:00
|
|
|
case "ec":
|
|
|
|
ecVolumeLine := line[len("ec volume "):]
|
|
|
|
ecShard := &master_pb.VolumeEcShardInformationMessage{}
|
|
|
|
for _, part := range strings.Split(ecVolumeLine, " ") {
|
|
|
|
if strings.HasPrefix(part, "id:") {
|
|
|
|
id, _ := strconv.ParseInt(part[len("id:"):], 10, 64)
|
|
|
|
ecShard.Id = uint32(id)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(part, "collection:") {
|
|
|
|
ecShard.Collection = part[len("collection:"):]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(part, "shards:") {
|
|
|
|
shards := part[len("shards:["):]
|
|
|
|
shards = strings.TrimRight(shards, "]")
|
|
|
|
shardBits := erasure_coding.ShardBits(0)
|
|
|
|
for _, shardId := range strings.Split(shards, ",") {
|
|
|
|
sid, _ := strconv.Atoi(shardId)
|
|
|
|
shardBits = shardBits.AddShardId(erasure_coding.ShardId(sid))
|
|
|
|
}
|
|
|
|
ecShard.EcIndexBits = uint32(shardBits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disk.EcShardInfos = append(disk.EcShardInfos, ecShard)
|
2021-03-15 11:23:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return topo
|
|
|
|
}
|
|
|
|
|
2022-09-09 03:06:03 +08:00
|
|
|
//go:embed volume.list.txt
|
2022-02-08 17:14:27 +08:00
|
|
|
var topoData string
|
2022-10-10 13:47:55 +08:00
|
|
|
|
|
|
|
//go:embed volume.list2.txt
|
|
|
|
var topoData2 string
|