forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology_test.go
139 lines (133 loc) · 3.9 KB
/
topology_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package master
import (
"fmt"
"github.com/chubaofs/chubaofs/util"
"testing"
"time"
)
func createDataNodeForTopo(addr, zoneName string, ns *nodeSet) (dn *DataNode) {
dn = newDataNode(addr, zoneName, "test")
dn.ZoneName = zoneName
dn.Total = 1024 * util.GB
dn.Used = 10 * util.GB
dn.AvailableSpace = 1024 * util.GB
dn.Carry = 0.9
dn.ReportTime = time.Now()
dn.isActive = true
dn.NodeSetID = ns.ID
return
}
func TestSingleZone(t *testing.T) {
topo := newTopology()
zoneName := "test"
zone := newZone(zoneName)
topo.putZone(zone)
nodeSet := newNodeSet(1, 6, zoneName)
zone.putNodeSet(nodeSet)
topo.putDataNode(createDataNodeForTopo(mds1Addr, zoneName, nodeSet))
topo.putDataNode(createDataNodeForTopo(mds2Addr, zoneName, nodeSet))
topo.putDataNode(createDataNodeForTopo(mds3Addr, zoneName, nodeSet))
topo.putDataNode(createDataNodeForTopo(mds4Addr, zoneName, nodeSet))
topo.putDataNode(createDataNodeForTopo(mds5Addr, zoneName, nodeSet))
if !topo.isSingleZone() {
zones := topo.getAllZones()
t.Errorf("topo should be single zone,zone num [%v]", len(zones))
return
}
replicaNum := 2
//single zone exclude,if it is a single zone excludeZones don't take effect
excludeZones := make([]string, 0)
excludeZones = append(excludeZones, zoneName)
zones, err := topo.allocZonesForDataNode(replicaNum, replicaNum, excludeZones)
if err != nil {
t.Error(err)
return
}
if len(zones) != 1 {
t.Errorf("expect zone num [%v],len(zones) is %v", 0, len(zones))
return
}
//single zone normal
zones, err = topo.allocZonesForDataNode(replicaNum, replicaNum, nil)
if err != nil {
t.Error(err)
return
}
newHosts, _, err := zones[0].getAvailDataNodeHosts(nil, nil, replicaNum)
if err != nil {
t.Error(err)
return
}
fmt.Println(newHosts)
topo.deleteDataNode(createDataNodeForTopo(mds1Addr, zoneName, nodeSet))
}
func TestAllocZones(t *testing.T) {
topo := newTopology()
zoneCount := 3
//add three zones
zoneName1 := "zone1"
zone1 := newZone(zoneName1)
nodeSet1 := newNodeSet(1, 6, zoneName1)
zone1.putNodeSet(nodeSet1)
topo.putZone(zone1)
topo.putDataNode(createDataNodeForTopo(mds1Addr, zoneName1, nodeSet1))
topo.putDataNode(createDataNodeForTopo(mds2Addr, zoneName1, nodeSet1))
zoneName2 := "zone2"
zone2 := newZone(zoneName2)
nodeSet2 := newNodeSet(2, 6, zoneName2)
zone2.putNodeSet(nodeSet2)
topo.putZone(zone2)
topo.putDataNode(createDataNodeForTopo(mds3Addr, zoneName2, nodeSet2))
topo.putDataNode(createDataNodeForTopo(mds4Addr, zoneName2, nodeSet2))
zoneName3 := "zone3"
zone3 := newZone(zoneName3)
nodeSet3 := newNodeSet(3, 6, zoneName3)
zone3.putNodeSet(nodeSet3)
topo.putZone(zone3)
topo.putDataNode(createDataNodeForTopo(mds5Addr, zoneName3, nodeSet3))
zones := topo.getAllZones()
if len(zones) != zoneCount {
t.Errorf("expect zones num[%v],len(zones) is %v", zoneCount, len(zones))
return
}
//only pass replica num
replicaNum := 2
zones, err := topo.allocZonesForDataNode(replicaNum, replicaNum, nil)
if err != nil {
t.Error(err)
return
}
if len(zones) != replicaNum {
t.Errorf("expect zones num[%v],len(zones) is %v", replicaNum, len(zones))
return
}
cluster := new(Cluster)
cluster.t = topo
cluster.cfg = newClusterConfig()
//don't cross zone
hosts, _, err := cluster.chooseTargetDataNodes("", nil, nil, replicaNum, 1, "")
if err != nil {
t.Error(err)
return
}
//cross zone
hosts, _, err = cluster.chooseTargetDataNodes("", nil, nil, replicaNum, 2, "")
if err != nil {
t.Error(err)
return
}
t.Logf("ChooseTargetDataHosts in multi zones,hosts[%v]", hosts)
// after excluding zone3, alloc zones will be success
excludeZones := make([]string, 0)
excludeZones = append(excludeZones, zoneName3)
zones, err = topo.allocZonesForDataNode(2, replicaNum, excludeZones)
if err != nil {
t.Logf("allocZonesForDataNode failed,err[%v]", err)
}
for _, zone := range zones {
if zone.name == zoneName3 {
t.Errorf("zone [%v] should be exclued", zoneName3)
return
}
}
}