forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvol_test.go
243 lines (227 loc) · 7.48 KB
/
vol_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package master
import (
"fmt"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/util"
"github.com/chubaofs/chubaofs/util/log"
"testing"
"time"
)
func TestAutoCreateDataPartitions(t *testing.T) {
commonVol, err := server.cluster.getVol(commonVolName)
if err != nil {
t.Error(err)
}
commonVol.Capacity = 300 * util.TB
dpCount := len(commonVol.dataPartitions.partitions)
commonVol.dataPartitions.readableAndWritableCnt = 0
commonVol.dataPartitions.lastAutoCreateTime = time.Now().Add(-time.Minute)
server.cluster.DisableAutoAllocate = false
t.Logf("status[%v],disableAutoAlloc[%v],cap[%v]\n",
commonVol.Status, server.cluster.DisableAutoAllocate, commonVol.Capacity)
commonVol.checkAutoDataPartitionCreation(server.cluster)
newDpCount := len(commonVol.dataPartitions.partitions)
if dpCount == newDpCount {
t.Errorf("autoCreateDataPartitions failed,expand 0 data partitions,oldCount[%v],curCount[%v]", dpCount, newDpCount)
return
}
}
func TestCheckVol(t *testing.T) {
commonVol.checkStatus(server.cluster)
commonVol.checkMetaPartitions(server.cluster)
commonVol.checkDataPartitions(server.cluster)
log.LogFlush()
fmt.Printf("writable data partitions[%v]\n", commonVol.dataPartitions.readableAndWritableCnt)
}
func TestVol(t *testing.T) {
capacity := 300
name := "test1"
createVol(name, t)
//report mp/dp info to master
server.cluster.checkDataNodeHeartbeat()
server.cluster.checkDataNodeHeartbeat()
time.Sleep(5 * time.Second)
//check status
server.cluster.checkMetaPartitions()
server.cluster.checkDataPartitions()
server.cluster.checkLoadMetaPartitions()
server.cluster.doLoadDataPartitions()
vol, err := server.cluster.getVol(name)
if err != nil {
t.Errorf("err is %v", err)
return
}
vol.checkStatus(server.cluster)
getVol(name, t)
updateVol(name, capacity, t)
statVol(name, t)
markDeleteVol(name, t)
getSimpleVol(name, t)
vol.checkStatus(server.cluster)
vol.deleteVolFromStore(server.cluster)
}
func createVol(name string, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v&replicas=3&type=extent&capacity=100&owner=cfs&mpCount=2&zoneName=%v", hostAddr, proto.AdminCreateVol, name, testZone2)
fmt.Println(reqURL)
process(reqURL, t)
vol, err := server.cluster.getVol(name)
if err != nil {
t.Error(err)
return
}
checkDataPartitionsWritableTest(vol, t)
checkMetaPartitionsWritableTest(vol, t)
}
func checkDataPartitionsWritableTest(vol *Vol, t *testing.T) {
if len(vol.dataPartitions.partitions) == 0 {
return
}
partition := vol.dataPartitions.partitions[0]
if partition.Status != proto.ReadWrite {
t.Errorf("expect partition status[%v],real status[%v]\n", proto.ReadWrite, partition.Status)
return
}
//after check data partitions ,the status must be writable
vol.checkDataPartitions(server.cluster)
partition = vol.dataPartitions.partitions[0]
if partition.Status != proto.ReadWrite {
t.Errorf("expect partition status[%v],real status[%v]\n", proto.ReadWrite, partition.Status)
return
}
}
func checkMetaPartitionsWritableTest(vol *Vol, t *testing.T) {
if len(vol.MetaPartitions) == 0 {
t.Error("no meta partition")
return
}
for _, mp := range vol.MetaPartitions {
if mp.Status != proto.ReadWrite {
t.Errorf("expect partition status[%v],real status[%v]\n", proto.ReadWrite, mp.Status)
return
}
}
maxPartitionID := vol.maxPartitionID()
maxMp := vol.MetaPartitions[maxPartitionID]
//after check meta partitions ,the status must be writable
maxMp.checkStatus(server.cluster.Name, false, int(vol.mpReplicaNum), maxPartitionID)
if maxMp.Status != proto.ReadWrite {
t.Errorf("expect partition status[%v],real status[%v]\n", proto.ReadWrite, maxMp.Status)
return
}
}
func getSimpleVol(name string, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v", hostAddr, proto.AdminGetVol, name)
fmt.Println(reqURL)
process(reqURL, t)
}
func getVol(name string, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v&authKey=%v", hostAddr, proto.ClientVol, name, buildAuthKey("cfs"))
fmt.Println(reqURL)
process(reqURL, t)
}
func updateVol(name string, capacity int, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v&capacity=%v&authKey=%v",
hostAddr, proto.AdminUpdateVol, name, capacity, buildAuthKey("cfs"))
fmt.Println(reqURL)
process(reqURL, t)
vol, err := server.cluster.getVol(name)
if err != nil {
t.Error(err)
return
}
if vol.Capacity != uint64(capacity) {
t.Errorf("update vol failed,expect[%v],real[%v]", capacity, vol.Capacity)
return
}
}
func statVol(name string, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v",
hostAddr, proto.ClientVolStat, name)
fmt.Println(reqURL)
process(reqURL, t)
}
func markDeleteVol(name string, t *testing.T) {
reqURL := fmt.Sprintf("%v%v?name=%v&authKey=%v",
hostAddr, proto.AdminDeleteVol, name, buildAuthKey("cfs"))
fmt.Println(reqURL)
process(reqURL, t)
vol, err := server.cluster.getVol(name)
if err != nil {
t.Error(err)
return
}
if vol.Status != markDelete {
t.Errorf("markDeleteVol failed,expect[%v],real[%v]", markDelete, vol.Status)
return
}
}
//func TestVolReduceReplicaNum(t *testing.T) {
// volName := "reduce-replica-num"
// vol, err := server.cluster.createVol(volName, volName, testZone2, 3, 3, util.DefaultDataPartitionSize,
// 100, false, false, false, false)
// if err != nil {
// t.Error(err)
// return
// }
// server.cluster.checkDataNodeHeartbeat()
// time.Sleep(2 * time.Second)
// for _, dp := range vol.dataPartitions.partitionMap {
// t.Logf("dp[%v] replicaNum[%v],hostLen[%v]\n", dp.PartitionID, dp.ReplicaNum, len(dp.Hosts))
// }
// oldReplicaNum := vol.dpReplicaNum
// reqURL := fmt.Sprintf("%v%v?name=%v&capacity=%v&replicaNum=%v&authKey=%v",
// hostAddr, proto.AdminUpdateVol, volName, 100, 2, buildAuthKey(volName))
// fmt.Println(reqURL)
// process(reqURL, t)
// if vol.dpReplicaNum != 2 {
// t.Error("update vol replica Num to [2] failed")
// return
// }
// for i := 0; i < int(oldReplicaNum); i++ {
// t.Logf("before check,needToLowerReplica[%v] \n", vol.NeedToLowerReplica)
// vol.NeedToLowerReplica = true
// t.Logf(" after check,needToLowerReplica[%v]\n", vol.NeedToLowerReplica)
// vol.checkReplicaNum(server.cluster)
// }
// vol.NeedToLowerReplica = true
// //check more once,the replica num of data partition must be equal with vol.dpReplicaNun
// vol.checkReplicaNum(server.cluster)
// for _, dp := range vol.dataPartitions.partitionMap {
// if dp.ReplicaNum != vol.dpReplicaNum || len(dp.Hosts) != int(vol.dpReplicaNum) {
// t.Errorf("dp.replicaNum[%v],hosts[%v],vol.dpReplicaNum[%v]\n", dp.ReplicaNum, len(dp.Hosts), vol.dpReplicaNum)
// return
// }
// }
//}
func TestConcurrentReadWriteDataPartitionMap(t *testing.T) {
name := "TestConcurrentReadWriteDataPartitionMap"
var volID uint64 = 1
var createTime = time.Now().Unix()
vol := newVol(volID, name, name, "", util.DefaultDataPartitionSize, 100, defaultReplicaNum,
defaultReplicaNum, false, false, false,false, createTime, "")
// unavailable mp
mp1 := newMetaPartition(1, 1, defaultMaxMetaPartitionInodeID, 3, name, volID)
vol.addMetaPartition(mp1)
//readonly mp
mp2 := newMetaPartition(2, 1, defaultMaxMetaPartitionInodeID, 3, name, volID)
mp2.Status = proto.ReadOnly
vol.addMetaPartition(mp2)
vol.updateViewCache(server.cluster)
for id := 0; id < 30000; id++ {
dp := newDataPartition(uint64(id), 3, name, volID)
vol.dataPartitions.put(dp)
}
go func() {
var id uint64 = 30000
for {
id++
dp := newDataPartition(id, 3, name, volID)
vol.dataPartitions.put(dp)
time.Sleep(time.Second)
}
}()
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
vol.updateViewCache(server.cluster)
}
}