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.

136 lines
2.8 KiB

13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/storage"
  4. "encoding/json"
  5. "fmt"
  6. "math/rand"
  7. "testing"
  8. "time"
  9. )
  10. var topologyLayout = `
  11. {
  12. "dc1":{
  13. "rack1":{
  14. "server1":{
  15. "volumes":[
  16. {"id":1, "size":12312},
  17. {"id":2, "size":12312},
  18. {"id":3, "size":12312}
  19. ],
  20. "limit":3
  21. },
  22. "server2":{
  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. "server1":{
  33. "volumes":[
  34. {"id":4, "size":12312},
  35. {"id":5, "size":12312},
  36. {"id":6, "size":12312}
  37. ],
  38. "limit":4
  39. },
  40. "server2":{
  41. "volumes":[],
  42. "limit":4
  43. },
  44. "server3":{
  45. "volumes":[
  46. {"id":2, "size":12312},
  47. {"id":3, "size":12312},
  48. {"id":4, "size":12312}
  49. ],
  50. "limit":2
  51. }
  52. }
  53. },
  54. "dc2":{
  55. },
  56. "dc3":{
  57. "rack2":{
  58. "server1":{
  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. //need to connect all nodes first before server adding volumes
  77. topo, err := NewTopology("mynetwork", "/etc/weed.conf", "/tmp", "test", 234, 5)
  78. if err != nil {
  79. fmt.Println("error:", err)
  80. }
  81. mTopology := data.(map[string]interface{})
  82. for dcKey, dcValue := range mTopology {
  83. dc := NewDataCenter(dcKey)
  84. dcMap := dcValue.(map[string]interface{})
  85. topo.LinkChildNode(dc)
  86. for rackKey, rackValue := range dcMap {
  87. rack := NewRack(rackKey)
  88. rackMap := rackValue.(map[string]interface{})
  89. dc.LinkChildNode(rack)
  90. for serverKey, serverValue := range rackMap {
  91. server := NewDataNode(serverKey)
  92. serverMap := serverValue.(map[string]interface{})
  93. rack.LinkChildNode(server)
  94. for _, v := range serverMap["volumes"].([]interface{}) {
  95. m := v.(map[string]interface{})
  96. vi := storage.VolumeInfo{
  97. Id: storage.VolumeId(int64(m["id"].(float64))),
  98. Size: uint64(m["size"].(float64)),
  99. Version: storage.CurrentVersion}
  100. server.AddOrUpdateVolume(vi)
  101. }
  102. server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
  103. }
  104. }
  105. }
  106. return topo
  107. }
  108. func TestRemoveDataCenter(t *testing.T) {
  109. topo := setup(topologyLayout)
  110. topo.UnlinkChildNode(NodeId("dc2"))
  111. if topo.GetActiveVolumeCount() != 15 {
  112. t.Fail()
  113. }
  114. topo.UnlinkChildNode(NodeId("dc3"))
  115. if topo.GetActiveVolumeCount() != 12 {
  116. t.Fail()
  117. }
  118. }
  119. func TestReserveOneVolume(t *testing.T) {
  120. topo := setup(topologyLayout)
  121. rand.Seed(time.Now().UnixNano())
  122. rand.Seed(1)
  123. ret, node, vid := topo.RandomlyReserveOneVolume("dc1")
  124. if node.Parent().Parent().Id() != NodeId("dc1") {
  125. t.Fail()
  126. }
  127. fmt.Println("assigned :", ret, ", node :", node, ", volume id:", vid)
  128. }