You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.0 KiB

8 years ago
  1. package topology
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/chrislusf/seaweedfs/weed/sequence"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. )
  9. var topologyLayout = `
  10. {
  11. "dc1":{
  12. "rack1":{
  13. "server111":{
  14. "volumes":[
  15. {"id":1, "size":12312},
  16. {"id":2, "size":12312},
  17. {"id":3, "size":12312}
  18. ],
  19. "limit":3
  20. },
  21. "server112":{
  22. "volumes":[
  23. {"id":4, "size":12312},
  24. {"id":5, "size":12312},
  25. {"id":6, "size":12312}
  26. ],
  27. "limit":10
  28. }
  29. },
  30. "rack2":{
  31. "server121":{
  32. "volumes":[
  33. {"id":4, "size":12312},
  34. {"id":5, "size":12312},
  35. {"id":6, "size":12312}
  36. ],
  37. "limit":4
  38. },
  39. "server122":{
  40. "volumes":[],
  41. "limit":4
  42. },
  43. "server123":{
  44. "volumes":[
  45. {"id":2, "size":12312},
  46. {"id":3, "size":12312},
  47. {"id":4, "size":12312}
  48. ],
  49. "limit":5
  50. }
  51. }
  52. },
  53. "dc2":{
  54. },
  55. "dc3":{
  56. "rack2":{
  57. "server321":{
  58. "volumes":[
  59. {"id":1, "size":12312},
  60. {"id":3, "size":12312},
  61. {"id":5, "size":12312}
  62. ],
  63. "limit":4
  64. }
  65. }
  66. }
  67. }
  68. `
  69. func setup(topologyLayout string) *Topology {
  70. var data interface{}
  71. err := json.Unmarshal([]byte(topologyLayout), &data)
  72. if err != nil {
  73. fmt.Println("error:", err)
  74. }
  75. fmt.Println("data:", data)
  76. //need to connect all nodes first before server adding volumes
  77. topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5)
  78. mTopology := data.(map[string]interface{})
  79. for dcKey, dcValue := range mTopology {
  80. dc := NewDataCenter(dcKey)
  81. dcMap := dcValue.(map[string]interface{})
  82. topo.LinkChildNode(dc)
  83. for rackKey, rackValue := range dcMap {
  84. rack := NewRack(rackKey)
  85. rackMap := rackValue.(map[string]interface{})
  86. dc.LinkChildNode(rack)
  87. for serverKey, serverValue := range rackMap {
  88. server := NewDataNode(serverKey)
  89. serverMap := serverValue.(map[string]interface{})
  90. rack.LinkChildNode(server)
  91. for _, v := range serverMap["volumes"].([]interface{}) {
  92. m := v.(map[string]interface{})
  93. vi := storage.VolumeInfo{
  94. Id: storage.VolumeId(int64(m["id"].(float64))),
  95. Size: uint64(m["size"].(float64)),
  96. Version: storage.CurrentVersion}
  97. server.AddOrUpdateVolume(vi)
  98. }
  99. server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
  100. }
  101. }
  102. }
  103. return topo
  104. }
  105. func TestFindEmptySlotsForOneVolume(t *testing.T) {
  106. topo := setup(topologyLayout)
  107. vg := NewDefaultVolumeGrowth()
  108. rp, _ := storage.NewReplicaPlacementFromString("002")
  109. volumeGrowOption := &VolumeGrowOption{
  110. Collection: "",
  111. ReplicaPlacement: rp,
  112. DataCenter: "dc1",
  113. Rack: "",
  114. DataNode: "",
  115. }
  116. servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
  117. if err != nil {
  118. fmt.Println("finding empty slots error :", err)
  119. t.Fail()
  120. }
  121. for _, server := range servers {
  122. fmt.Println("assigned node :", server.Id())
  123. }
  124. }