command_ec_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. allEcNodes := []*EcNode{
  18. newEcNode("dc1", "rack1", "dn1", 100).addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}),
  19. newEcNode("dc1", "rack2", "dn2", 100).addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}),
  20. }
  21. racks := collectRacks(allEcNodes)
  22. balanceEcVolumes(nil, "c1", allEcNodes, racks, false)
  23. }
  24. func TestCommandEcBalanceNothingToMove(t *testing.T) {
  25. allEcNodes := []*EcNode{
  26. newEcNode("dc1", "rack1", "dn1", 100).
  27. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  28. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  29. newEcNode("dc1", "rack1", "dn2", 100).
  30. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  31. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  32. }
  33. racks := collectRacks(allEcNodes)
  34. balanceEcVolumes(nil, "c1", allEcNodes, racks, false)
  35. }
  36. func TestCommandEcBalanceAddNewServers(t *testing.T) {
  37. allEcNodes := []*EcNode{
  38. newEcNode("dc1", "rack1", "dn1", 100).
  39. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  40. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  41. newEcNode("dc1", "rack1", "dn2", 100).
  42. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  43. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  44. newEcNode("dc1", "rack1", "dn3", 100),
  45. newEcNode("dc1", "rack1", "dn4", 100),
  46. }
  47. racks := collectRacks(allEcNodes)
  48. balanceEcVolumes(nil, "c1", allEcNodes, racks, false)
  49. }
  50. func TestCommandEcBalanceAddNewRacks(t *testing.T) {
  51. allEcNodes := []*EcNode{
  52. newEcNode("dc1", "rack1", "dn1", 100).
  53. addEcVolumeAndShardsForTest(1, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}).
  54. addEcVolumeAndShardsForTest(2, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}),
  55. newEcNode("dc1", "rack1", "dn2", 100).
  56. addEcVolumeAndShardsForTest(1, "c1", []uint32{7, 8, 9, 10, 11, 12, 13}).
  57. addEcVolumeAndShardsForTest(2, "c1", []uint32{0, 1, 2, 3, 4, 5, 6}),
  58. newEcNode("dc1", "rack2", "dn3", 100),
  59. newEcNode("dc1", "rack2", "dn4", 100),
  60. }
  61. racks := collectRacks(allEcNodes)
  62. balanceEcVolumes(nil, "c1", allEcNodes, racks, false)
  63. }
  64. func TestCommandEcBalanceVolumeEvenButRackUneven(t *testing.T) {
  65. allEcNodes := []*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. racks := collectRacks(allEcNodes)
  98. balanceEcVolumes(nil, "c1", allEcNodes, racks, false)
  99. balanceEcRacks(nil, racks, false)
  100. }
  101. func newEcNode(dc string, rack string, dataNodeId string, freeEcSlot int) *EcNode {
  102. return &EcNode{
  103. info: &master_pb.DataNodeInfo{
  104. Id: dataNodeId,
  105. DiskInfos: make(map[string]*master_pb.DiskInfo),
  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. }