command_ec_test.go 5.6 KB

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