2012-08-29 08:42:24 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
2013-11-10 09:31:50 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
`
|
|
|
|
|
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-01 09:20:59 +00:00
|
|
|
//need to connect all nodes first before server adding volumes
|
2013-11-10 09:31:50 +00:00
|
|
|
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 {
|
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-08 23:25:44 +00:00
|
|
|
server := NewDataNode(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{})
|
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
|
|
|
}
|
2012-09-02 21:33:48 +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) {
|
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)
|
2013-01-17 08:56:56 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
rand.Seed(1)
|
2013-06-20 01:10:38 +00:00
|
|
|
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
|
|
|
}
|