seaweedfs/go/topology/topo_test.go

138 lines
2.9 KiB
Go
Raw Normal View History

2012-08-29 08:42:24 +00:00
package topology
import (
"code.google.com/p/weed-fs/go/sequence"
2013-02-27 06:54:22 +00:00
"code.google.com/p/weed-fs/go/storage"
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
"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 = `
{
2013-02-27 06:54:22 +00:00
"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
}
}
}
2012-08-31 08:35:11 +00:00
}
`
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, err := NewTopology("mynetwork", "/etc/weed.conf", sequence.NewMemorySequencer(), 234, 5)
2013-02-27 06:54:22 +00:00
if err != nil {
fmt.Println("error:", err)
}
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{})
2013-02-27 06:54:22 +00:00
vi := storage.VolumeInfo{
Id: storage.VolumeId(int64(m["id"].(float64))),
Size: uint64(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)
2013-01-17 08:56:56 +00:00
rand.Seed(time.Now().UnixNano())
rand.Seed(1)
ret, node, vid := topo.RandomlyReserveOneVolume("dc1")
if node.Parent().Parent().Id() != NodeId("dc1") {
t.Fail()
}
2013-01-17 08:56:56 +00:00
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
}