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.

140 lines
5.9 KiB

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