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.

125 lines
2.8 KiB

  1. package replication
  2. import (
  3. "code.google.com/p/weed-fs/go/storage"
  4. "code.google.com/p/weed-fs/go/topology"
  5. "encoding/json"
  6. "fmt"
  7. "testing"
  8. )
  9. var topologyLayout = `
  10. {
  11. "dc1":{
  12. "rack1":{
  13. "server1":{
  14. "volumes":[
  15. {"id":1, "size":12312},
  16. {"id":2, "size":12312},
  17. {"id":3, "size":12312}
  18. ],
  19. "limit":3
  20. },
  21. "server2":{
  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. "server1":{
  32. "volumes":[
  33. {"id":4, "size":12312},
  34. {"id":5, "size":12312},
  35. {"id":6, "size":12312}
  36. ],
  37. "limit":4
  38. },
  39. "server2":{
  40. "volumes":[],
  41. "limit":4
  42. },
  43. "server3":{
  44. "volumes":[
  45. {"id":2, "size":12312},
  46. {"id":3, "size":12312},
  47. {"id":4, "size":12312}
  48. ],
  49. "limit":2
  50. }
  51. }
  52. },
  53. "dc2":{
  54. },
  55. "dc3":{
  56. "rack2":{
  57. "server1":{
  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.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 := topology.NewTopology("weedfs", "/etc/weedfs/weedfs.conf",
  78. "/tmp", "testing", 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 := topology.NewDataCenter(dcKey)
  85. dcMap := dcValue.(map[string]interface{})
  86. topo.LinkChildNode(dc)
  87. for rackKey, rackValue := range dcMap {
  88. rack := topology.NewRack(rackKey)
  89. rackMap := rackValue.(map[string]interface{})
  90. dc.LinkChildNode(rack)
  91. for serverKey, serverValue := range rackMap {
  92. server := topology.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(topology.NodeId("dc2"))
  112. if topo.GetActiveVolumeCount() != 15 {
  113. t.Fail()
  114. }
  115. topo.UnlinkChildNode(topology.NodeId("dc3"))
  116. if topo.GetActiveVolumeCount() != 12 {
  117. t.Fail()
  118. }
  119. }