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.

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