2014-04-13 16:29:52 +08:00
|
|
|
package topology
|
2012-09-03 16:50:04 +08:00
|
|
|
|
|
|
|
import (
|
2014-09-25 16:57:22 +08:00
|
|
|
"github.com/chrislusf/weed-fs/go/sequence"
|
|
|
|
"github.com/chrislusf/weed-fs/go/storage"
|
2012-09-03 16:50:04 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var topologyLayout = `
|
|
|
|
{
|
|
|
|
"dc1":{
|
|
|
|
"rack1":{
|
2014-03-03 14:16:54 +08:00
|
|
|
"server111":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[
|
|
|
|
{"id":1, "size":12312},
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":3
|
|
|
|
},
|
2014-03-03 14:16:54 +08:00
|
|
|
"server112":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[
|
|
|
|
{"id":4, "size":12312},
|
|
|
|
{"id":5, "size":12312},
|
|
|
|
{"id":6, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":10
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"rack2":{
|
2014-03-03 14:16:54 +08:00
|
|
|
"server121":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[
|
|
|
|
{"id":4, "size":12312},
|
|
|
|
{"id":5, "size":12312},
|
|
|
|
{"id":6, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":4
|
|
|
|
},
|
2014-03-03 14:16:54 +08:00
|
|
|
"server122":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[],
|
|
|
|
"limit":4
|
|
|
|
},
|
2014-03-03 14:16:54 +08:00
|
|
|
"server123":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312},
|
|
|
|
{"id":4, "size":12312}
|
|
|
|
],
|
2014-03-03 14:16:54 +08:00
|
|
|
"limit":5
|
2012-09-03 16:50:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc2":{
|
|
|
|
},
|
|
|
|
"dc3":{
|
|
|
|
"rack2":{
|
2014-03-03 14:16:54 +08:00
|
|
|
"server321":{
|
2012-09-03 16:50:04 +08:00
|
|
|
"volumes":[
|
|
|
|
{"id":1, "size":12312},
|
|
|
|
{"id":3, "size":12312},
|
|
|
|
{"id":5, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-04-13 16:29:52 +08:00
|
|
|
func setup(topologyLayout string) *Topology {
|
2012-09-03 16:50:04 +08:00
|
|
|
var data interface{}
|
|
|
|
err := json.Unmarshal([]byte(topologyLayout), &data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
fmt.Println("data:", data)
|
|
|
|
|
|
|
|
//need to connect all nodes first before server adding volumes
|
2014-04-13 16:29:52 +08:00
|
|
|
topo, err := NewTopology("weedfs", "/etc/weedfs/weedfs.conf",
|
2013-11-10 17:31:50 +08:00
|
|
|
sequence.NewMemorySequencer(), 32*1024, 5)
|
2013-04-15 10:30:26 +08:00
|
|
|
if err != nil {
|
|
|
|
panic("error: " + err.Error())
|
|
|
|
}
|
2012-09-03 16:50:04 +08:00
|
|
|
mTopology := data.(map[string]interface{})
|
|
|
|
for dcKey, dcValue := range mTopology {
|
2014-04-13 16:29:52 +08:00
|
|
|
dc := NewDataCenter(dcKey)
|
2012-09-03 16:50:04 +08:00
|
|
|
dcMap := dcValue.(map[string]interface{})
|
|
|
|
topo.LinkChildNode(dc)
|
|
|
|
for rackKey, rackValue := range dcMap {
|
2014-04-13 16:29:52 +08:00
|
|
|
rack := NewRack(rackKey)
|
2012-09-03 16:50:04 +08:00
|
|
|
rackMap := rackValue.(map[string]interface{})
|
|
|
|
dc.LinkChildNode(rack)
|
|
|
|
for serverKey, serverValue := range rackMap {
|
2014-04-13 16:29:52 +08:00
|
|
|
server := NewDataNode(serverKey)
|
2012-09-03 16:50:04 +08:00
|
|
|
serverMap := serverValue.(map[string]interface{})
|
|
|
|
rack.LinkChildNode(server)
|
|
|
|
for _, v := range serverMap["volumes"].([]interface{}) {
|
|
|
|
m := v.(map[string]interface{})
|
2013-02-27 14:54:22 +08:00
|
|
|
vi := storage.VolumeInfo{
|
|
|
|
Id: storage.VolumeId(int64(m["id"].(float64))),
|
|
|
|
Size: uint64(m["size"].(float64)),
|
|
|
|
Version: storage.CurrentVersion}
|
2012-09-17 08:31:15 +08:00
|
|
|
server.AddOrUpdateVolume(vi)
|
2012-09-03 16:50:04 +08:00
|
|
|
}
|
|
|
|
server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return topo
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:16:54 +08:00
|
|
|
func TestFindEmptySlotsForOneVolume(t *testing.T) {
|
2012-09-03 16:50:04 +08:00
|
|
|
topo := setup(topologyLayout)
|
2014-03-03 14:16:54 +08:00
|
|
|
vg := NewDefaultVolumeGrowth()
|
|
|
|
rp, _ := storage.NewReplicaPlacementFromString("002")
|
2014-07-21 14:41:25 +08:00
|
|
|
volumeGrowOption := &VolumeGrowOption{
|
|
|
|
Collection: "",
|
|
|
|
ReplicaPlacement: rp,
|
|
|
|
DataCenter: "dc1",
|
|
|
|
Rack: "",
|
|
|
|
DataNode: "",
|
|
|
|
}
|
|
|
|
servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
|
2014-03-03 14:16:54 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("finding empty slots error :", err)
|
2012-09-03 16:50:04 +08:00
|
|
|
t.Fail()
|
|
|
|
}
|
2014-03-03 14:16:54 +08:00
|
|
|
for _, server := range servers {
|
|
|
|
fmt.Println("assigned node :", server.Id())
|
2012-09-03 16:50:04 +08:00
|
|
|
}
|
|
|
|
}
|