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.

137 lines
2.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/sequence"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "encoding/json"
  6. "fmt"
  7. "math/rand"
  8. "testing"
  9. "time"
  10. )
  11. var topologyLayout = `
  12. {
  13. "dc1":{
  14. "rack1":{
  15. "server1":{
  16. "volumes":[
  17. {"id":1, "size":12312},
  18. {"id":2, "size":12312},
  19. {"id":3, "size":12312}
  20. ],
  21. "limit":3
  22. },
  23. "server2":{
  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. "server1":{
  34. "volumes":[
  35. {"id":4, "size":12312},
  36. {"id":5, "size":12312},
  37. {"id":6, "size":12312}
  38. ],
  39. "limit":4
  40. },
  41. "server2":{
  42. "volumes":[],
  43. "limit":4
  44. },
  45. "server3":{
  46. "volumes":[
  47. {"id":2, "size":12312},
  48. {"id":3, "size":12312},
  49. {"id":4, "size":12312}
  50. ],
  51. "limit":2
  52. }
  53. }
  54. },
  55. "dc2":{
  56. },
  57. "dc3":{
  58. "rack2":{
  59. "server1":{
  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. //need to connect all nodes first before server adding volumes
  78. topo, err := NewTopology("mynetwork", "/etc/weed.conf", sequence.NewMemorySequencer(), 234, 5)
  79. if err != nil {
  80. fmt.Println("error:", err)
  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 TestRemoveDataCenter(t *testing.T) {
  110. topo := setup(topologyLayout)
  111. topo.UnlinkChildNode(NodeId("dc2"))
  112. if topo.GetActiveVolumeCount() != 15 {
  113. t.Fail()
  114. }
  115. topo.UnlinkChildNode(NodeId("dc3"))
  116. if topo.GetActiveVolumeCount() != 12 {
  117. t.Fail()
  118. }
  119. }
  120. func TestReserveOneVolume(t *testing.T) {
  121. topo := setup(topologyLayout)
  122. rand.Seed(time.Now().UnixNano())
  123. rand.Seed(1)
  124. ret, node, vid := topo.RandomlyReserveOneVolume("dc1")
  125. if node.Parent().Parent().Id() != NodeId("dc1") {
  126. t.Fail()
  127. }
  128. fmt.Println("assigned :", ret, ", node :", node, ", volume id:", vid)
  129. }