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.

128 lines
3.0 KiB

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