@@ -12,12 +12,13 @@ import (
1212 "github.com/pingcap/tiproxy/lib/util/logger"
1313 "github.com/pingcap/tiproxy/pkg/balance/policy"
1414 "github.com/stretchr/testify/require"
15+ "go.uber.org/zap"
1516)
1617
1718func TestRouteWithOneFactor (t * testing.T ) {
1819 lg , _ := logger .CreateLoggerForTest (t )
1920 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
20- factor := & mockFactor {bitNum : 8 , balanceCount : 1 , threshold : 1 }
21+ factor := & mockFactor {bitNum : 8 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
2122 fm .factors = []Factor {factor }
2223 require .NoError (t , fm .updateBitNum ())
2324
@@ -71,8 +72,8 @@ func TestRouteWithOneFactor(t *testing.T) {
7172func TestRouteWith2Factors (t * testing.T ) {
7273 lg , _ := logger .CreateLoggerForTest (t )
7374 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
74- factor1 := & mockFactor {bitNum : 4 , balanceCount : 1 , threshold : 1 }
75- factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , threshold : 1 }
75+ factor1 := & mockFactor {bitNum : 4 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
76+ factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
7677 fm .factors = []Factor {factor1 , factor2 }
7778 require .NoError (t , fm .updateBitNum ())
7879
@@ -143,7 +144,7 @@ func TestRouteWith2Factors(t *testing.T) {
143144func TestBalanceWithOneFactor (t * testing.T ) {
144145 lg , _ := logger .CreateLoggerForTest (t )
145146 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
146- factor := & mockFactor {bitNum : 2 , balanceCount : 1 }
147+ factor := & mockFactor {bitNum : 2 , balanceCount : 1 , canBeRouted : true }
147148 fm .factors = []Factor {factor }
148149 require .NoError (t , fm .updateBitNum ())
149150
@@ -193,8 +194,8 @@ func TestBalanceWithOneFactor(t *testing.T) {
193194func TestBalanceWith2Factors (t * testing.T ) {
194195 lg , _ := logger .CreateLoggerForTest (t )
195196 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
196- factor1 := & mockFactor {bitNum : 2 , balanceCount : 2 , threshold : 1 }
197- factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 }
197+ factor1 := & mockFactor {bitNum : 2 , balanceCount : 2 , threshold : 1 , canBeRouted : true }
198+ factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , canBeRouted : true }
198199 fm .factors = []Factor {factor1 , factor2 }
199200 require .NoError (t , fm .updateBitNum ())
200201
@@ -280,7 +281,7 @@ func TestBalanceWith2Factors(t *testing.T) {
280281func TestBalanceWith3Factors (t * testing.T ) {
281282 lg , _ := logger .CreateLoggerForTest (t )
282283 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
283- factors := []* mockFactor {{bitNum : 1 }, {bitNum : 2 }, {bitNum : 2 }}
284+ factors := []* mockFactor {{bitNum : 1 , canBeRouted : true }, {bitNum : 2 , canBeRouted : true }, {bitNum : 2 , canBeRouted : true }}
284285 fm .factors = []Factor {factors [0 ], factors [1 ], factors [2 ]}
285286 require .NoError (t , fm .updateBitNum ())
286287
@@ -333,7 +334,7 @@ func TestBalanceWith3Factors(t *testing.T) {
333334func TestScoreAlwaysUpdated (t * testing.T ) {
334335 lg , _ := logger .CreateLoggerForTest (t )
335336 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
336- factors := []* mockFactor {{bitNum : 1 }, {bitNum : 2 }}
337+ factors := []* mockFactor {{bitNum : 1 , canBeRouted : true }, {bitNum : 2 , canBeRouted : true }}
337338 fm .factors = []Factor {factors [0 ], factors [1 ]}
338339 factors [0 ].updateScore = func (backends []scoredBackend ) {
339340 for i := 0 ; i < len (backends ); i ++ {
@@ -375,14 +376,14 @@ func TestSetFactors(t *testing.T) {
375376 setFunc : func (balance * config.Balance ) {
376377 balance .LabelName = "group"
377378 },
378- expectedNames : []string {"status " , "label " , "health" , "memory" , "cpu" , "location" , "conn" },
379+ expectedNames : []string {"label " , "status " , "health" , "memory" , "cpu" , "location" , "conn" },
379380 },
380381 {
381382 setFunc : func (balance * config.Balance ) {
382383 balance .Policy = config .BalancePolicyLocation
383384 balance .LabelName = "group"
384385 },
385- expectedNames : []string {"status " , "label " , "location" , "health" , "memory" , "cpu" , "conn" },
386+ expectedNames : []string {"label " , "status " , "location" , "health" , "memory" , "cpu" , "conn" },
386387 },
387388 {
388389 setFunc : func (balance * config.Balance ) {
@@ -395,7 +396,7 @@ func TestSetFactors(t *testing.T) {
395396 balance .Policy = config .BalancePolicyConnection
396397 balance .LabelName = "group"
397398 },
398- expectedNames : []string {"status " , "label " , "conn" },
399+ expectedNames : []string {"label " , "status " , "conn" },
399400 },
400401 }
401402
@@ -417,3 +418,56 @@ func TestSetFactors(t *testing.T) {
417418 }
418419 fm .Close ()
419420}
421+
422+ func TestCanBeRouted (t * testing.T ) {
423+ fm := NewFactorBasedBalance (zap .NewNop (), newMockMetricsReader ())
424+ factor1 := & mockFactor {bitNum : 2 , balanceCount : 1 , canBeRouted : false }
425+ factor2 := & mockFactor {bitNum : 2 , balanceCount : 1 , canBeRouted : true }
426+ fm .factors = []Factor {factor1 , factor2 }
427+ require .NoError (t , fm .updateBitNum ())
428+
429+ tests := []struct {
430+ scores1 []int
431+ scores2 []int
432+ routed bool
433+ }{
434+ {
435+ scores1 : []int {1 , 0 },
436+ scores2 : []int {0 , 0 },
437+ routed : true ,
438+ },
439+ {
440+ scores1 : []int {1 , 1 },
441+ scores2 : []int {1 , 0 },
442+ routed : false ,
443+ },
444+ {
445+ scores1 : []int {0 , 0 },
446+ scores2 : []int {2 , 1 },
447+ routed : true ,
448+ },
449+ {
450+ scores1 : []int {2 , 1 },
451+ scores2 : []int {0 , 0 },
452+ routed : false ,
453+ },
454+ }
455+ for tIdx , test := range tests {
456+ factor1 .updateScore = func (backends []scoredBackend ) {
457+ for i := 0 ; i < len (backends ); i ++ {
458+ backends [i ].addScore (test .scores1 [i ], factor1 .bitNum )
459+ }
460+ }
461+ factor2 .updateScore = func (backends []scoredBackend ) {
462+ for i := 0 ; i < len (backends ); i ++ {
463+ backends [i ].addScore (test .scores2 [i ], factor2 .bitNum )
464+ }
465+ }
466+ backends := createBackends (len (test .scores1 ))
467+ _ , _ , count , _ , _ := fm .BackendsToBalance (backends )
468+ require .Equal (t , test .routed , count > 0 , "test index %d" , tIdx )
469+ backends = createBackends (len (test .scores1 ))
470+ b := fm .BackendToRoute (backends )
471+ require .Equal (t , test .routed , b != nil , "test index %d" , tIdx )
472+ }
473+ }
0 commit comments