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.

135 lines
3.1 KiB

  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, err := NewTopology("weedfs", "/etc/weedfs/weedfs.conf",
  78. sequence.NewMemorySequencer(), 32*1024, 5)
  79. if err != nil {
  80. panic("error: " + err.Error())
  81. }
  82. mTopology := data.(map[string]interface{})
  83. for dcKey, dcValue := range mTopology {
  84. dc := NewDataCenter(dcKey)
  85. dcMap := dcValue.(map[string]interface{})
  86. topo.LinkChildNode(dc)
  87. for rackKey, rackValue := range dcMap {
  88. rack := NewRack(rackKey)
  89. rackMap := rackValue.(map[string]interface{})
  90. dc.LinkChildNode(rack)
  91. for serverKey, serverValue := range rackMap {
  92. server := NewDataNode(serverKey)
  93. serverMap := serverValue.(map[string]interface{})
  94. rack.LinkChildNode(server)
  95. for _, v := range serverMap["volumes"].([]interface{}) {
  96. m := v.(map[string]interface{})
  97. vi := storage.VolumeInfo{
  98. Id: storage.VolumeId(int64(m["id"].(float64))),
  99. Size: uint64(m["size"].(float64)),
  100. Version: storage.CurrentVersion}
  101. server.AddOrUpdateVolume(vi)
  102. }
  103. server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
  104. }
  105. }
  106. }
  107. return topo
  108. }
  109. func TestFindEmptySlotsForOneVolume(t *testing.T) {
  110. topo := setup(topologyLayout)
  111. vg := NewDefaultVolumeGrowth()
  112. rp, _ := storage.NewReplicaPlacementFromString("002")
  113. volumeGrowOption := &VolumeGrowOption{
  114. Collection: "",
  115. ReplicaPlacement: rp,
  116. DataCenter: "dc1",
  117. Rack: "",
  118. DataNode: "",
  119. }
  120. servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
  121. if err != nil {
  122. fmt.Println("finding empty slots error :", err)
  123. t.Fail()
  124. }
  125. for _, server := range servers {
  126. fmt.Println("assigned node :", server.Id())
  127. }
  128. }