2014-04-13 08:29:52 +00:00
|
|
|
package topology
|
2012-09-03 08:50:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"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":{
|
2014-03-03 06:16:54 +00:00
|
|
|
"server111":{
|
2012-09-03 08:50:04 +00:00
|
|
|
"volumes":[
|
|
|
|
{"id":1, "size":12312},
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":3
|
|
|
|
},
|
2014-03-03 06:16:54 +00:00
|
|
|
"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":{
|
2014-03-03 06:16:54 +00:00
|
|
|
"server121":{
|
2012-09-03 08:50:04 +00:00
|
|
|
"volumes":[
|
|
|
|
{"id":4, "size":12312},
|
|
|
|
{"id":5, "size":12312},
|
|
|
|
{"id":6, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":4
|
|
|
|
},
|
2014-03-03 06:16:54 +00:00
|
|
|
"server122":{
|
2012-09-03 08:50:04 +00:00
|
|
|
"volumes":[],
|
|
|
|
"limit":4
|
|
|
|
},
|
2014-03-03 06:16:54 +00:00
|
|
|
"server123":{
|
2012-09-03 08:50:04 +00:00
|
|
|
"volumes":[
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312},
|
|
|
|
{"id":4, "size":12312}
|
|
|
|
],
|
2014-03-03 06:16:54 +00:00
|
|
|
"limit":5
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc2":{
|
|
|
|
},
|
|
|
|
"dc3":{
|
|
|
|
"rack2":{
|
2014-03-03 06:16:54 +00:00
|
|
|
"server321":{
|
2012-09-03 08:50:04 +00:00
|
|
|
"volumes":[
|
|
|
|
{"id":1, "size":12312},
|
|
|
|
{"id":3, "size":12312},
|
|
|
|
{"id":5, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-04-13 08:29:52 +00:00
|
|
|
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
|
2020-04-01 19:18:40 +00:00
|
|
|
topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5, false)
|
2012-09-03 08:50:04 +00:00
|
|
|
mTopology := data.(map[string]interface{})
|
|
|
|
for dcKey, dcValue := range mTopology {
|
2014-04-13 08:29:52 +00:00
|
|
|
dc := NewDataCenter(dcKey)
|
2012-09-03 08:50:04 +00:00
|
|
|
dcMap := dcValue.(map[string]interface{})
|
|
|
|
topo.LinkChildNode(dc)
|
|
|
|
for rackKey, rackValue := range dcMap {
|
2014-04-13 08:29:52 +00:00
|
|
|
rack := NewRack(rackKey)
|
2012-09-03 08:50:04 +00:00
|
|
|
rackMap := rackValue.(map[string]interface{})
|
|
|
|
dc.LinkChildNode(rack)
|
|
|
|
for serverKey, serverValue := range rackMap {
|
2014-04-13 08:29:52 +00:00
|
|
|
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}
|
2012-09-17 00:31:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-03 06:16:54 +00:00
|
|
|
func TestFindEmptySlotsForOneVolume(t *testing.T) {
|
2012-09-03 08:50:04 +00:00
|
|
|
topo := setup(topologyLayout)
|
2014-03-03 06:16:54 +00:00
|
|
|
vg := NewDefaultVolumeGrowth()
|
2019-12-23 20:48:20 +00:00
|
|
|
rp, _ := super_block.NewReplicaPlacementFromString("002")
|
2014-07-21 06:41:25 +00:00
|
|
|
volumeGrowOption := &VolumeGrowOption{
|
|
|
|
Collection: "",
|
|
|
|
ReplicaPlacement: rp,
|
|
|
|
DataCenter: "dc1",
|
|
|
|
Rack: "",
|
|
|
|
DataNode: "",
|
|
|
|
}
|
|
|
|
servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
|
2014-03-03 06:16:54 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("finding empty slots error :", err)
|
2012-09-03 08:50:04 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2014-03-03 06:16:54 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
fmt.Println("assigned node :", server.Id())
|
2012-09-03 08:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 09:21:11 +00:00
|
|
|
|
|
|
|
var topologyLayout2 = `
|
|
|
|
{
|
|
|
|
"dc1":{
|
|
|
|
"rack1":{
|
|
|
|
"server111":{
|
|
|
|
"volumes":[
|
|
|
|
{"id":1, "size":12312},
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server112":{
|
|
|
|
"volumes":[
|
|
|
|
{"id":4, "size":12312},
|
|
|
|
{"id":5, "size":12312},
|
|
|
|
{"id":6, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server113":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server114":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server115":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server116":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"rack2":{
|
|
|
|
"server121":{
|
|
|
|
"volumes":[
|
|
|
|
{"id":4, "size":12312},
|
|
|
|
{"id":5, "size":12312},
|
|
|
|
{"id":6, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server122":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server123":{
|
|
|
|
"volumes":[
|
|
|
|
{"id":2, "size":12312},
|
|
|
|
{"id":3, "size":12312},
|
|
|
|
{"id":4, "size":12312}
|
|
|
|
],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server124":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server125":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server126":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"rack3":{
|
|
|
|
"server131":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server132":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server133":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server134":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server135":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
},
|
|
|
|
"server136":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":300
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestReplication011(t *testing.T) {
|
|
|
|
topo := setup(topologyLayout2)
|
|
|
|
vg := NewDefaultVolumeGrowth()
|
|
|
|
rp, _ := super_block.NewReplicaPlacementFromString("011")
|
|
|
|
volumeGrowOption := &VolumeGrowOption{
|
|
|
|
Collection: "MAIL",
|
|
|
|
ReplicaPlacement: rp,
|
|
|
|
DataCenter: "dc1",
|
|
|
|
Rack: "",
|
|
|
|
DataNode: "",
|
|
|
|
}
|
|
|
|
servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("finding empty slots error :", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
for _, server := range servers {
|
|
|
|
fmt.Println("assigned node :", server.Id())
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 05:41:52 +00:00
|
|
|
|
|
|
|
var topologyLayout3 = `
|
|
|
|
{
|
|
|
|
"dc1":{
|
|
|
|
"rack1":{
|
|
|
|
"server111":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":2000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc2":{
|
|
|
|
"rack2":{
|
|
|
|
"server222":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":2000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc3":{
|
|
|
|
"rack3":{
|
|
|
|
"server333":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":1000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc4":{
|
|
|
|
"rack4":{
|
|
|
|
"server444":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":1000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc5":{
|
|
|
|
"rack5":{
|
|
|
|
"server555":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"dc6":{
|
|
|
|
"rack6":{
|
|
|
|
"server666":{
|
|
|
|
"volumes":[],
|
|
|
|
"limit":500
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestFindEmptySlotsForOneVolumeScheduleByWeight(t *testing.T) {
|
|
|
|
topo := setup(topologyLayout3)
|
|
|
|
vg := NewDefaultVolumeGrowth()
|
|
|
|
rp, _ := super_block.NewReplicaPlacementFromString("100")
|
|
|
|
volumeGrowOption := &VolumeGrowOption{
|
|
|
|
Collection: "Weight",
|
|
|
|
ReplicaPlacement: rp,
|
|
|
|
DataCenter: "",
|
|
|
|
Rack: "",
|
|
|
|
DataNode: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
distribution := map[NodeId]int{}
|
|
|
|
// assign 1000 volumes
|
2020-03-07 14:12:57 +00:00
|
|
|
for i := 0; i < 1000; i++ {
|
2020-03-05 05:41:52 +00:00
|
|
|
servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("finding empty slots error :", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
for _, server := range servers {
|
2020-03-07 14:12:20 +00:00
|
|
|
// fmt.Println("assigned node :", server.Id())
|
2020-03-05 05:41:52 +00:00
|
|
|
if _, ok := distribution[server.id]; !ok {
|
|
|
|
distribution[server.id] = 0
|
|
|
|
}
|
|
|
|
distribution[server.id] += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range distribution {
|
2020-03-07 14:12:20 +00:00
|
|
|
fmt.Printf("%s : %d\n", k, v)
|
2020-03-05 05:41:52 +00:00
|
|
|
}
|
2020-03-07 14:12:57 +00:00
|
|
|
}
|