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.

132 lines
5.6 KiB

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