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.

145 lines
5.9 KiB

4 years ago
4 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. )
  8. func TestCommandEcDistribution(t *testing.T) {
  9. allEcNodes := []*EcNode{
  10. newEcNode("dc1", "rack1", "dn1", 100),
  11. newEcNode("dc1", "rack2", "dn2", 100),
  12. }
  13. allocated := balancedEcDistribution(allEcNodes)
  14. fmt.Printf("allocated: %+v", allocated)
  15. }
  16. func TestCommandEcBalanceSmall(t *testing.T) {
  17. ecb := &ecBalancer{
  18. ecNodes: []*EcNode{
  19. newEcNode("dc1", "rack1", "dn1", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}),
  20. newEcNode("dc1", "rack2", "dn2", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}),
  21. },
  22. applyBalancing: false,
  23. }
  24. ecb.balanceEcVolumes("c1")
  25. }
  26. func TestCommandEcBalanceNothingToMove(t *testing.T) {
  27. ecb := &ecBalancer{
  28. ecNodes: []*EcNode{
  29. newEcNode("dc1", "rack1", "dn1", 100).
  30. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  31. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  32. newEcNode("dc1", "rack1", "dn2", 100).
  33. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  34. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  35. },
  36. applyBalancing: false,
  37. }
  38. ecb.balanceEcVolumes("c1")
  39. }
  40. func TestCommandEcBalanceAddNewServers(t *testing.T) {
  41. ecb := &ecBalancer{
  42. ecNodes: []*EcNode{
  43. newEcNode("dc1", "rack1", "dn1", 100).
  44. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  45. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  46. newEcNode("dc1", "rack1", "dn2", 100).
  47. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  48. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  49. newEcNode("dc1", "rack1", "dn3", 100),
  50. newEcNode("dc1", "rack1", "dn4", 100),
  51. },
  52. applyBalancing: false,
  53. }
  54. ecb.balanceEcVolumes("c1")
  55. }
  56. func TestCommandEcBalanceAddNewRacks(t *testing.T) {
  57. ecb := &ecBalancer{
  58. ecNodes: []*EcNode{
  59. newEcNode("dc1", "rack1", "dn1", 100).
  60. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  61. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  62. newEcNode("dc1", "rack1", "dn2", 100).
  63. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  64. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  65. newEcNode("dc1", "rack2", "dn3", 100),
  66. newEcNode("dc1", "rack2", "dn4", 100),
  67. },
  68. applyBalancing: false,
  69. }
  70. ecb.balanceEcVolumes("c1")
  71. }
  72. func TestCommandEcBalanceVolumeEvenButRackUneven(t *testing.T) {
  73. ecb := ecBalancer{
  74. ecNodes: []*EcNode{
  75. newEcNode("dc1", "rack1", "dn_shared", 100).
  76. addEcVolumeAndShardsForTest(1, "c1", []uint32{0}).
  77. addEcVolumeAndShardsForTest(2, "c1", []uint32{0}),
  78. newEcNode("dc1", "rack1", "dn_a1", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{1}),
  79. newEcNode("dc1", "rack1", "dn_a2", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{2}),
  80. newEcNode("dc1", "rack1", "dn_a3", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{3}),
  81. newEcNode("dc1", "rack1", "dn_a4", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{4}),
  82. newEcNode("dc1", "rack1", "dn_a5", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{5}),
  83. newEcNode("dc1", "rack1", "dn_a6", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{6}),
  84. newEcNode("dc1", "rack1", "dn_a7", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{7}),
  85. newEcNode("dc1", "rack1", "dn_a8", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{8}),
  86. newEcNode("dc1", "rack1", "dn_a9", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{9}),
  87. newEcNode("dc1", "rack1", "dn_a10", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{10}),
  88. newEcNode("dc1", "rack1", "dn_a11", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{11}),
  89. newEcNode("dc1", "rack1", "dn_a12", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{12}),
  90. newEcNode("dc1", "rack1", "dn_a13", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{13}),
  91. newEcNode("dc1", "rack1", "dn_b1", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{1}),
  92. newEcNode("dc1", "rack1", "dn_b2", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{2}),
  93. newEcNode("dc1", "rack1", "dn_b3", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{3}),
  94. newEcNode("dc1", "rack1", "dn_b4", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{4}),
  95. newEcNode("dc1", "rack1", "dn_b5", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{5}),
  96. newEcNode("dc1", "rack1", "dn_b6", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{6}),
  97. newEcNode("dc1", "rack1", "dn_b7", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{7}),
  98. newEcNode("dc1", "rack1", "dn_b8", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{8}),
  99. newEcNode("dc1", "rack1", "dn_b9", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{9}),
  100. newEcNode("dc1", "rack1", "dn_b10", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{10}),
  101. newEcNode("dc1", "rack1", "dn_b11", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{11}),
  102. newEcNode("dc1", "rack1", "dn_b12", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{12}),
  103. newEcNode("dc1", "rack1", "dn_b13", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{13}),
  104. newEcNode("dc1", "rack1", "dn3", 100),
  105. },
  106. applyBalancing: false,
  107. }
  108. ecb.balanceEcVolumes("c1")
  109. ecb.balanceEcRacks()
  110. }
  111. func newEcNode(dc string, rack string, dataNodeId string, freeEcSlot int) *EcNode {
  112. return &EcNode{
  113. info: &master_pb.DataNodeInfo{
  114. Id: dataNodeId,
  115. DiskInfos: make(map[string]*master_pb.DiskInfo),
  116. },
  117. dc: DataCenterId(dc),
  118. rack: RackId(rack),
  119. freeEcSlot: freeEcSlot,
  120. }
  121. }
  122. func (ecNode *EcNode) addEcVolumeAndShardsForTest(vid uint32, collection string, shardIds []uint32) *EcNode {
  123. return ecNode.addEcVolumeShards(needle.VolumeId(vid), collection, shardIds)
  124. }