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() *Topology {
|
|
|
|
var data interface{}
|
|
|
|
err := json.Unmarshal([]byte(topologyLayout), &data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
fmt.Println("data:", data)
|
|
|
|
printMap(data)
|
|
|
|
|
2012-09-01 09:20:59 +00:00
|
|
|
//need to connect all nodes first before server adding volumes
|
2012-08-31 08:35:11 +00:00
|
|
|
topo := NewTopology(NodeId("mynetwork"))
|
|
|
|
mTopology := data.(map[string]interface{})
|
|
|
|
for dcKey, dcValue := range mTopology {
|
|
|
|
dc := NewDataCenter(NodeId(dcKey))
|
|
|
|
dc.Node.parent = &topo.Node
|
|
|
|
dcMap := dcValue.(map[string]interface{})
|
2012-09-01 09:20:59 +00:00
|
|
|
topo.Node.AddNode(&dc.Node)
|
2012-08-31 08:35:11 +00:00
|
|
|
for rackKey, rackValue := range dcMap {
|
|
|
|
rack := NewRack(NodeId(rackKey))
|
2012-09-01 09:20:59 +00:00
|
|
|
rack.Node.parent = &dc.Node
|
2012-08-31 08:35:11 +00:00
|
|
|
rackMap := rackValue.(map[string]interface{})
|
2012-09-01 09:20:59 +00:00
|
|
|
dc.Node.AddNode(&rack.Node)
|
2012-08-31 08:35:11 +00:00
|
|
|
for serverKey, serverValue := range rackMap {
|
|
|
|
server := NewServer(NodeId(serverKey))
|
2012-09-01 09:20:59 +00:00
|
|
|
server.Node.parent = &rack.Node
|
2012-08-31 08:35:11 +00:00
|
|
|
serverMap := serverValue.(map[string]interface{})
|
2012-09-01 09:20:59 +00:00
|
|
|
rack.Node.AddNode(&server.Node)
|
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-01 09:20:59 +00:00
|
|
|
server.Node.AddMaxVolumeCount(int(serverMap["limit"].(float64)))
|
2012-08-31 08:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("topology:", *topo)
|
|
|
|
|
2012-09-01 09:20:59 +00:00
|
|
|
bytes, err := json.Marshal(topo.Node.children)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddVolume(t *testing.T) {
|
|
|
|
topo := setup()
|
|
|
|
v := &storage.VolumeInfo{}
|
|
|
|
topo.AddVolume(v)
|
2012-08-29 08:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDataCenter(t *testing.T) {
|
2012-09-01 09:20:59 +00:00
|
|
|
topo := setup()
|
|
|
|
topo.RemoveNode(NodeId("dc2"))
|
|
|
|
if topo.activeVolumeCount != 15 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
topo.RemoveNode(NodeId("dc3"))
|
|
|
|
if topo.activeVolumeCount != 12 {
|
|
|
|
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()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
ret, vid := topo.RandomlyReserveOneVolume()
|
|
|
|
fmt.Println("topology:", topo.Node)
|
|
|
|
fmt.Println("assigned :", ret)
|
|
|
|
fmt.Println("assigned volume id:", vid)
|
|
|
|
if topo.reservedVolumeCount != 1 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2012-08-29 08:42:24 +00:00
|
|
|
}
|