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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2012-09-02 21:33:48 +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-03 08:50:04 +00:00
|
|
|
//fmt.Println("data:", data)
|
|
|
|
//printMap(data)
|
2012-08-31 08:35:11 +00:00
|
|
|
|
2012-09-01 09:20:59 +00:00
|
|
|
//need to connect all nodes first before server adding volumes
|
2012-09-02 21:33:48 +00:00
|
|
|
topo := NewTopology("mynetwork")
|
2012-08-31 08:35:11 +00:00
|
|
|
mTopology := data.(map[string]interface{})
|
|
|
|
for dcKey, dcValue := range mTopology {
|
2012-09-02 21:33:48 +00:00
|
|
|
dc := NewDataCenter(dcKey)
|
2012-08-31 08:35:11 +00:00
|
|
|
dcMap := dcValue.(map[string]interface{})
|
2012-09-02 21:33:48 +00:00
|
|
|
topo.LinkChildNode(dc)
|
2012-08-31 08:35:11 +00:00
|
|
|
for rackKey, rackValue := range dcMap {
|
2012-09-02 21:33:48 +00:00
|
|
|
rack := NewRack(rackKey)
|
2012-08-31 08:35:11 +00:00
|
|
|
rackMap := rackValue.(map[string]interface{})
|
2012-09-02 21:33:48 +00:00
|
|
|
dc.LinkChildNode(rack)
|
2012-08-31 08:35:11 +00:00
|
|
|
for serverKey, serverValue := range rackMap {
|
2012-09-02 21:33:48 +00:00
|
|
|
server := NewServer(serverKey)
|
2012-08-31 08:35:11 +00:00
|
|
|
serverMap := serverValue.(map[string]interface{})
|
2012-09-02 21:33:48 +00:00
|
|
|
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{})
|
2012-08-31 08:35:11 +00:00
|
|
|
vi := &storage.VolumeInfo{Id: storage.VolumeId(int64(m["id"].(float64))), Size: int64(m["size"].(float64))}
|
|
|
|
server.AddVolume(vi)
|
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
|
2012-08-31 08:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("topology:", *topo)
|
|
|
|
|
2012-09-02 21:33:48 +00:00
|
|
|
bytes, err := json.Marshal(topo.children)
|
2012-09-01 09:20:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("json error:", err)
|
|
|
|
}
|
|
|
|
fmt.Println("json topo:", string(bytes))
|
2012-08-31 08:35:11 +00:00
|
|
|
|
|
|
|
return topo
|
|
|
|
}
|
2012-08-29 08:42:24 +00:00
|
|
|
|
2012-08-31 08:35:11 +00:00
|
|
|
func printMap(mm interface{}) {
|
|
|
|
m := mm.(map[string]interface{})
|
|
|
|
for k, v := range m {
|
|
|
|
switch vv := v.(type) {
|
|
|
|
case string:
|
|
|
|
fmt.Println(k, "\"", vv, "\"")
|
|
|
|
case int, float64:
|
|
|
|
fmt.Println(k, ":", vv)
|
|
|
|
case []interface{}:
|
|
|
|
fmt.Println(k, ":[")
|
|
|
|
for _, u := range vv {
|
|
|
|
fmt.Println(u)
|
|
|
|
fmt.Println(",")
|
|
|
|
}
|
|
|
|
fmt.Println("]")
|
|
|
|
default:
|
|
|
|
fmt.Println(k, ":")
|
|
|
|
printMap(vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-29 08:42:24 +00:00
|
|
|
func TestRemoveDataCenter(t *testing.T) {
|
2012-09-02 21:33:48 +00:00
|
|
|
topo := setup(topologyLayout)
|
|
|
|
topo.UnlinkChildNode(NodeId("dc2"))
|
|
|
|
if topo.GetActiveVolumeCount() != 15 {
|
2012-09-01 09:20:59 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2012-09-02 21:33:48 +00:00
|
|
|
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) {
|
2012-09-02 21:33:48 +00:00
|
|
|
topo := setup(topologyLayout)
|
2012-09-01 09:20:59 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2012-09-02 21:33:48 +00:00
|
|
|
rand.Seed(1)
|
2012-09-01 20:17:30 +00:00
|
|
|
ret, node, vid := topo.RandomlyReserveOneVolume()
|
2012-09-02 21:33:48 +00:00
|
|
|
fmt.Println("topology:", topo)
|
|
|
|
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
|
|
|
}
|