seaweedfs/weed-fs/src/pkg/topology/topo_test.go

128 lines
2.8 KiB
Go
Raw Normal View History

2012-08-29 08:42:24 +00:00
package topology
import (
2012-08-31 08:35:11 +00:00
"encoding/json"
"fmt"
2012-09-01 09:20:59 +00:00
"math/rand"
2012-08-31 08:35:11 +00:00
"pkg/storage"
"testing"
2012-09-01 09:20:59 +00:00
"time"
2012-08-29 08:42:24 +00:00
)
2012-08-31 08:35:11 +00:00
var topologyLayout = `
{
"dc1":{
"rack1":{
"server1":{
"volumes":[
{"id":1, "size":12312},
{"id":2, "size":12312},
{"id":3, "size":12312}
],
"limit":3
},
"server2":{
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":10
}
},
"rack2":{
"server1":{
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":4
},
"server2":{
"volumes":[],
"limit":4
},
"server3":{
"volumes":[
{"id":2, "size":12312},
{"id":3, "size":12312},
{"id":4, "size":12312}
],
"limit":2
}
}
},
"dc2":{
},
"dc3":{
"rack2":{
"server1":{
"volumes":[
{"id":1, "size":12312},
{"id":3, "size":12312},
{"id":5, "size":12312}
],
"limit":4
}
}
}
}
`
func setup(topologyLayout string) *Topology {
2012-08-31 08:35:11 +00:00
var data interface{}
err := json.Unmarshal([]byte(topologyLayout), &data)
if err != nil {
fmt.Println("error:", err)
}
2012-09-01 09:20:59 +00:00
//need to connect all nodes first before server adding volumes
topo := NewTopology("mynetwork","/etc/weed.conf","/tmp","test",234,5)
2012-08-31 08:35:11 +00:00
mTopology := data.(map[string]interface{})
for dcKey, dcValue := range mTopology {
dc := NewDataCenter(dcKey)
2012-08-31 08:35:11 +00:00
dcMap := dcValue.(map[string]interface{})
topo.LinkChildNode(dc)
2012-08-31 08:35:11 +00:00
for rackKey, rackValue := range dcMap {
rack := NewRack(rackKey)
2012-08-31 08:35:11 +00:00
rackMap := rackValue.(map[string]interface{})
dc.LinkChildNode(rack)
2012-08-31 08:35:11 +00:00
for serverKey, serverValue := range rackMap {
2012-09-08 23:25:44 +00:00
server := NewDataNode(serverKey)
2012-08-31 08:35:11 +00:00
serverMap := serverValue.(map[string]interface{})
rack.LinkChildNode(server)
2012-08-31 08:35:11 +00:00
for _, v := range serverMap["volumes"].([]interface{}) {
2012-09-01 09:20:59 +00:00
m := v.(map[string]interface{})
vi := storage.VolumeInfo{Id: storage.VolumeId(int64(m["id"].(float64))), Size: int64(m["size"].(float64)), Version:storage.CurrentVersion}
2012-09-21 08:31:52 +00:00
server.AddOrUpdateVolume(vi)
2012-08-31 08:35:11 +00:00
}
server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
2012-08-31 08:35:11 +00:00
}
}
}
return topo
}
2012-08-29 08:42:24 +00:00
func TestRemoveDataCenter(t *testing.T) {
topo := setup(topologyLayout)
topo.UnlinkChildNode(NodeId("dc2"))
if topo.GetActiveVolumeCount() != 15 {
2012-09-01 09:20:59 +00:00
t.Fail()
}
topo.UnlinkChildNode(NodeId("dc3"))
if topo.GetActiveVolumeCount() != 12 {
2012-09-01 09:20:59 +00:00
t.Fail()
}
}
2012-08-29 08:42:24 +00:00
2012-09-01 09:20:59 +00:00
func TestReserveOneVolume(t *testing.T) {
topo := setup(topologyLayout)
rand.Seed(time.Now().UnixNano())
rand.Seed(1)
2012-09-01 20:17:30 +00:00
ret, node, vid := topo.RandomlyReserveOneVolume()
fmt.Println("assigned :", ret, ", node :", node,", volume id:", vid)
2012-09-03 08:50:04 +00:00
2012-08-29 08:42:24 +00:00
}