volume_growth_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package topology
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/chrislusf/seaweedfs/weed/sequence"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  10. )
  11. var topologyLayout = `
  12. {
  13. "dc1":{
  14. "rack1":{
  15. "server111":{
  16. "volumes":[
  17. {"id":1, "size":12312},
  18. {"id":2, "size":12312},
  19. {"id":3, "size":12312}
  20. ],
  21. "limit":3
  22. },
  23. "server112":{
  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. "server121":{
  34. "volumes":[
  35. {"id":4, "size":12312},
  36. {"id":5, "size":12312},
  37. {"id":6, "size":12312}
  38. ],
  39. "limit":4
  40. },
  41. "server122":{
  42. "volumes":[],
  43. "limit":4
  44. },
  45. "server123":{
  46. "volumes":[
  47. {"id":2, "size":12312},
  48. {"id":3, "size":12312},
  49. {"id":4, "size":12312}
  50. ],
  51. "limit":5
  52. }
  53. }
  54. },
  55. "dc2":{
  56. },
  57. "dc3":{
  58. "rack2":{
  59. "server321":{
  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. fmt.Println("data:", data)
  78. //need to connect all nodes first before server adding volumes
  79. topo := NewTopology("weedfs", sequence.NewMemorySequencer(), 32*1024, 5, false)
  80. mTopology := data.(map[string]interface{})
  81. for dcKey, dcValue := range mTopology {
  82. dc := NewDataCenter(dcKey)
  83. dcMap := dcValue.(map[string]interface{})
  84. topo.LinkChildNode(dc)
  85. for rackKey, rackValue := range dcMap {
  86. rack := NewRack(rackKey)
  87. rackMap := rackValue.(map[string]interface{})
  88. dc.LinkChildNode(rack)
  89. for serverKey, serverValue := range rackMap {
  90. server := 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{
  96. Id: needle.VolumeId(int64(m["id"].(float64))),
  97. Size: uint64(m["size"].(float64)),
  98. Version: needle.CurrentVersion}
  99. server.AddOrUpdateVolume(vi)
  100. }
  101. server.UpAdjustMaxVolumeCountDelta(int64(serverMap["limit"].(float64)))
  102. }
  103. }
  104. }
  105. return topo
  106. }
  107. func TestFindEmptySlotsForOneVolume(t *testing.T) {
  108. topo := setup(topologyLayout)
  109. vg := NewDefaultVolumeGrowth()
  110. rp, _ := super_block.NewReplicaPlacementFromString("002")
  111. volumeGrowOption := &VolumeGrowOption{
  112. Collection: "",
  113. ReplicaPlacement: rp,
  114. DataCenter: "dc1",
  115. Rack: "",
  116. DataNode: "",
  117. }
  118. servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
  119. if err != nil {
  120. fmt.Println("finding empty slots error :", err)
  121. t.Fail()
  122. }
  123. for _, server := range servers {
  124. fmt.Println("assigned node :", server.Id())
  125. }
  126. }
  127. var topologyLayout2 = `
  128. {
  129. "dc1":{
  130. "rack1":{
  131. "server111":{
  132. "volumes":[
  133. {"id":1, "size":12312},
  134. {"id":2, "size":12312},
  135. {"id":3, "size":12312}
  136. ],
  137. "limit":300
  138. },
  139. "server112":{
  140. "volumes":[
  141. {"id":4, "size":12312},
  142. {"id":5, "size":12312},
  143. {"id":6, "size":12312}
  144. ],
  145. "limit":300
  146. },
  147. "server113":{
  148. "volumes":[],
  149. "limit":300
  150. },
  151. "server114":{
  152. "volumes":[],
  153. "limit":300
  154. },
  155. "server115":{
  156. "volumes":[],
  157. "limit":300
  158. },
  159. "server116":{
  160. "volumes":[],
  161. "limit":300
  162. }
  163. },
  164. "rack2":{
  165. "server121":{
  166. "volumes":[
  167. {"id":4, "size":12312},
  168. {"id":5, "size":12312},
  169. {"id":6, "size":12312}
  170. ],
  171. "limit":300
  172. },
  173. "server122":{
  174. "volumes":[],
  175. "limit":300
  176. },
  177. "server123":{
  178. "volumes":[
  179. {"id":2, "size":12312},
  180. {"id":3, "size":12312},
  181. {"id":4, "size":12312}
  182. ],
  183. "limit":300
  184. },
  185. "server124":{
  186. "volumes":[],
  187. "limit":300
  188. },
  189. "server125":{
  190. "volumes":[],
  191. "limit":300
  192. },
  193. "server126":{
  194. "volumes":[],
  195. "limit":300
  196. }
  197. },
  198. "rack3":{
  199. "server131":{
  200. "volumes":[],
  201. "limit":300
  202. },
  203. "server132":{
  204. "volumes":[],
  205. "limit":300
  206. },
  207. "server133":{
  208. "volumes":[],
  209. "limit":300
  210. },
  211. "server134":{
  212. "volumes":[],
  213. "limit":300
  214. },
  215. "server135":{
  216. "volumes":[],
  217. "limit":300
  218. },
  219. "server136":{
  220. "volumes":[],
  221. "limit":300
  222. }
  223. }
  224. }
  225. }
  226. `
  227. func TestReplication011(t *testing.T) {
  228. topo := setup(topologyLayout2)
  229. vg := NewDefaultVolumeGrowth()
  230. rp, _ := super_block.NewReplicaPlacementFromString("011")
  231. volumeGrowOption := &VolumeGrowOption{
  232. Collection: "MAIL",
  233. ReplicaPlacement: rp,
  234. DataCenter: "dc1",
  235. Rack: "",
  236. DataNode: "",
  237. }
  238. servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
  239. if err != nil {
  240. fmt.Println("finding empty slots error :", err)
  241. t.Fail()
  242. }
  243. for _, server := range servers {
  244. fmt.Println("assigned node :", server.Id())
  245. }
  246. }
  247. var topologyLayout3 = `
  248. {
  249. "dc1":{
  250. "rack1":{
  251. "server111":{
  252. "volumes":[],
  253. "limit":2000
  254. }
  255. }
  256. },
  257. "dc2":{
  258. "rack2":{
  259. "server222":{
  260. "volumes":[],
  261. "limit":2000
  262. }
  263. }
  264. },
  265. "dc3":{
  266. "rack3":{
  267. "server333":{
  268. "volumes":[],
  269. "limit":1000
  270. }
  271. }
  272. },
  273. "dc4":{
  274. "rack4":{
  275. "server444":{
  276. "volumes":[],
  277. "limit":1000
  278. }
  279. }
  280. },
  281. "dc5":{
  282. "rack5":{
  283. "server555":{
  284. "volumes":[],
  285. "limit":500
  286. }
  287. }
  288. },
  289. "dc6":{
  290. "rack6":{
  291. "server666":{
  292. "volumes":[],
  293. "limit":500
  294. }
  295. }
  296. }
  297. }
  298. `
  299. func TestFindEmptySlotsForOneVolumeScheduleByWeight(t *testing.T) {
  300. topo := setup(topologyLayout3)
  301. vg := NewDefaultVolumeGrowth()
  302. rp, _ := super_block.NewReplicaPlacementFromString("100")
  303. volumeGrowOption := &VolumeGrowOption{
  304. Collection: "Weight",
  305. ReplicaPlacement: rp,
  306. DataCenter: "",
  307. Rack: "",
  308. DataNode: "",
  309. }
  310. distribution := map[NodeId]int{}
  311. // assign 1000 volumes
  312. for i := 0; i < 1000; i++ {
  313. servers, err := vg.findEmptySlotsForOneVolume(topo, volumeGrowOption)
  314. if err != nil {
  315. fmt.Println("finding empty slots error :", err)
  316. t.Fail()
  317. }
  318. for _, server := range servers {
  319. // fmt.Println("assigned node :", server.Id())
  320. if _, ok := distribution[server.id]; !ok {
  321. distribution[server.id] = 0
  322. }
  323. distribution[server.id] += 1
  324. }
  325. }
  326. for k, v := range distribution {
  327. fmt.Printf("%s : %d\n", k, v)
  328. }
  329. }