seaweedfs/weed/topology/volume_growth_test.go

134 lines
3.1 KiB
Go
Raw Permalink Normal View History

package topology
2012-09-03 08:50:04 +00:00
import (
"encoding/json"
"fmt"
"testing"
"github.com/chrislusf/seaweedfs/weed/sequence"
"github.com/chrislusf/seaweedfs/weed/storage"
2019-04-19 04:43:36 +00:00
"github.com/chrislusf/seaweedfs/weed/storage/needle"
2019-12-23 20:48:20 +00:00
"github.com/chrislusf/seaweedfs/weed/storage/super_block"
2012-09-03 08:50:04 +00:00
)
var topologyLayout = `
{
"dc1":{
"rack1":{
"server111":{
2012-09-03 08:50:04 +00:00
"volumes":[
{"id":1, "size":12312},
{"id":2, "size":12312},
{"id":3, "size":12312}
],
"limit":3
},
"server112":{
2012-09-03 08:50:04 +00:00
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":10
}
},
"rack2":{
"server121":{
2012-09-03 08:50:04 +00:00
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":4
},
"server122":{
2012-09-03 08:50:04 +00:00
"volumes":[],
"limit":4
},
"server123":{
2012-09-03 08:50:04 +00:00
"volumes":[
{"id":2, "size":12312},
{"id":3, "size":12312},
{"id":4, "size":12312}
],
"limit":5
2012-09-03 08:50:04 +00:00
}
}
},
"dc2":{
},
"dc3":{
"rack2":{
"server321":{
2012-09-03 08:50:04 +00:00
"volumes":[
{"id":1, "size":12312},
{"id":3, "size":12312},
{"id":5, "size":12312}
],
"limit":4
}
}
}
}
`
func setup(topologyLayout string) *Topology {
2012-09-03 08:50:04 +00: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
2017-02-13 06:18:48 +00:00
topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5)
2012-09-03 08:50:04 +00:00
mTopology := data.(map[string]interface{})
for dcKey, dcValue := range mTopology {
dc := NewDataCenter(dcKey)
2012-09-03 08:50:04 +00:00
dcMap := dcValue.(map[string]interface{})
topo.LinkChildNode(dc)
for rackKey, rackValue := range dcMap {
rack := NewRack(rackKey)
2012-09-03 08:50:04 +00:00
rackMap := rackValue.(map[string]interface{})
dc.LinkChildNode(rack)
for serverKey, serverValue := range rackMap {
server := NewDataNode(serverKey)
2012-09-03 08:50:04 +00:00
serverMap := serverValue.(map[string]interface{})
rack.LinkChildNode(server)
for _, v := range serverMap["volumes"].([]interface{}) {
m := v.(map[string]interface{})
2013-02-27 06:54:22 +00:00
vi := storage.VolumeInfo{
2019-04-19 04:43:36 +00:00
Id: needle.VolumeId(int64(m["id"].(float64))),
2013-02-27 06:54:22 +00:00
Size: uint64(m["size"].(float64)),
2019-04-19 04:43:36 +00:00
Version: needle.CurrentVersion}
server.AddOrUpdateVolume(vi)
2012-09-03 08:50:04 +00:00
}
2019-04-05 03:11:59 +00:00
server.UpAdjustMaxVolumeCountDelta(int64(serverMap["limit"].(float64)))
2012-09-03 08:50:04 +00:00
}
}
}
return topo
}
func TestFindEmptySlotsForOneVolume(t *testing.T) {
2012-09-03 08:50:04 +00:00
topo := setup(topologyLayout)
vg := NewDefaultVolumeGrowth()
2019-12-23 20:48:20 +00:00
rp, _ := super_block.NewReplicaPlacementFromString("002")
volumeGrowOption := &VolumeGrowOption{
Collection: "",
ReplicaPlacement: rp,
DataCenter: "dc1",
Rack: "",
DataNode: "",
}
servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
if err != nil {
fmt.Println("finding empty slots error :", err)
2012-09-03 08:50:04 +00:00
t.Fail()
}
for _, server := range servers {
fmt.Println("assigned node :", server.Id())
2012-09-03 08:50:04 +00:00
}
}