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.

129 lines
3.0 KiB

  1. package replication
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "code.google.com/p/weed-fs/weed/storage"
  7. "code.google.com/p/weed-fs/weed/topology"
  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.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 := topology.NewTopology("mynetwork", "/etc/weedfs/weedfs.conf", "/tmp", "testing", 32*1024, 5)
  80. mTopology := data.(map[string]interface{})
  81. for dcKey, dcValue := range mTopology {
  82. dc := topology.NewDataCenter(dcKey)
  83. dcMap := dcValue.(map[string]interface{})
  84. topo.LinkChildNode(dc)
  85. for rackKey, rackValue := range dcMap {
  86. rack := topology.NewRack(rackKey)
  87. rackMap := rackValue.(map[string]interface{})
  88. dc.LinkChildNode(rack)
  89. for serverKey, serverValue := range rackMap {
  90. server := topology.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{Id: storage.VolumeId(int64(m["id"].(float64))), Size: int64(m["size"].(float64)), Version: storage.CurrentVersion}
  96. server.AddOrUpdateVolume(vi)
  97. }
  98. server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
  99. }
  100. }
  101. }
  102. return topo
  103. }
  104. func TestRemoveDataCenter(t *testing.T) {
  105. topo := setup(topologyLayout)
  106. topo.UnlinkChildNode(topology.NodeId("dc2"))
  107. if topo.GetActiveVolumeCount() != 15 {
  108. t.Fail()
  109. }
  110. topo.UnlinkChildNode(topology.NodeId("dc3"))
  111. if topo.GetActiveVolumeCount() != 12 {
  112. t.Fail()
  113. }
  114. }
  115. func TestReserveOneVolume(t *testing.T) {
  116. topo := setup(topologyLayout)
  117. rand.Seed(time.Now().UnixNano())
  118. vg := &VolumeGrowth{copy1factor: 3, copy2factor: 2, copy3factor: 1, copyAll: 4}
  119. if c, e := vg.GrowByCountAndType(1, storage.Copy000, topo); e == nil {
  120. t.Log("reserved", c)
  121. }
  122. }