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.

132 lines
3.1 KiB

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