Skip to content

Commit d11f535

Browse files
*: Add unit test for sort hot peers (#9276)
ref #4399 Signed-off-by: okJiang <[email protected]> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
1 parent cec4fe0 commit d11f535

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

pkg/schedule/schedulers/hot_region_solver.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (bs *balanceSolver) filterHotPeers(storeLoad *statistics.StoreLoadDetail) [
437437
bs.nthHotPeer[storeID][bs.secondPriority] = secondSort[topnPosition-1]
438438
}
439439
if len(hotPeers) > bs.maxPeerNum {
440-
union := bs.sortHotPeers(firstSort, secondSort)
440+
union := sortHotPeers(firstSort, secondSort, bs.maxPeerNum)
441441
ret = make([]*statistics.HotPeerStat, 0, len(union))
442442
for peer := range union {
443443
appendItem(peer)
@@ -451,10 +451,13 @@ func (bs *balanceSolver) filterHotPeers(storeLoad *statistics.StoreLoadDetail) [
451451
return ret
452452
}
453453

454-
func (bs *balanceSolver) sortHotPeers(firstSort, secondSort []*statistics.HotPeerStat) map[*statistics.HotPeerStat]struct{} {
455-
union := make(map[*statistics.HotPeerStat]struct{}, bs.maxPeerNum)
454+
func sortHotPeers[T any](firstSort, secondSort []*T, maxPeerNum int) map[*T]struct{} {
455+
union := make(map[*T]struct{}, maxPeerNum)
456456
// At most MaxPeerNum peers, to prevent balanceSolver.solve() too slow.
457-
for len(union) < bs.maxPeerNum {
457+
for len(union) < maxPeerNum {
458+
if len(firstSort) == 0 && len(secondSort) == 0 {
459+
break
460+
}
458461
for len(firstSort) > 0 {
459462
peer := firstSort[0]
460463
firstSort = firstSort[1:]
@@ -463,7 +466,7 @@ func (bs *balanceSolver) sortHotPeers(firstSort, secondSort []*statistics.HotPee
463466
break
464467
}
465468
}
466-
for len(union) < bs.maxPeerNum && len(secondSort) > 0 {
469+
for len(union) < maxPeerNum && len(secondSort) > 0 {
467470
peer := secondSort[0]
468471
secondSort = secondSort[1:]
469472
if _, ok := union[peer]; !ok {

pkg/schedule/schedulers/hot_region_solver_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,79 @@ func TestBucketFirstStat(t *testing.T) {
606606
re.Equal(data.expect, bs.bucketFirstStat())
607607
}
608608
}
609+
610+
func TestSortHotPeers(t *testing.T) {
611+
re := require.New(t)
612+
613+
type testPeer struct {
614+
id int
615+
}
616+
617+
peer1 := &testPeer{id: 1}
618+
peer2 := &testPeer{id: 2}
619+
peer3 := &testPeer{id: 3}
620+
peer4 := &testPeer{id: 4}
621+
622+
tests := []struct {
623+
name string
624+
firstSort []*testPeer
625+
secondSort []*testPeer
626+
maxPeerNum int
627+
expected []*testPeer
628+
}{
629+
{
630+
name: "No duplicates, maxPeerNum greater than total peers",
631+
firstSort: []*testPeer{peer1, peer2},
632+
secondSort: []*testPeer{peer3, peer4},
633+
maxPeerNum: 5,
634+
expected: []*testPeer{peer1, peer3, peer2, peer4},
635+
},
636+
{
637+
name: "No duplicates, maxPeerNum less than total peers",
638+
firstSort: []*testPeer{peer1, peer2},
639+
secondSort: []*testPeer{peer3, peer4},
640+
maxPeerNum: 3,
641+
expected: []*testPeer{peer1, peer3, peer2},
642+
},
643+
{
644+
name: "Duplicates in both lists",
645+
firstSort: []*testPeer{peer1, peer2},
646+
secondSort: []*testPeer{peer2, peer3},
647+
maxPeerNum: 3,
648+
expected: []*testPeer{peer1, peer2, peer3},
649+
},
650+
{
651+
name: "Empty firstSort",
652+
firstSort: []*testPeer{},
653+
secondSort: []*testPeer{peer3, peer4},
654+
maxPeerNum: 2,
655+
expected: []*testPeer{peer3, peer4},
656+
},
657+
{
658+
name: "Empty secondSort",
659+
firstSort: []*testPeer{peer1, peer2},
660+
secondSort: []*testPeer{},
661+
maxPeerNum: 2,
662+
expected: []*testPeer{peer1, peer2},
663+
},
664+
{
665+
name: "Both lists empty",
666+
firstSort: []*testPeer{},
667+
secondSort: []*testPeer{},
668+
maxPeerNum: 2,
669+
expected: []*testPeer{},
670+
},
671+
}
672+
673+
for _, tt := range tests {
674+
t.Run(tt.name, func(*testing.T) {
675+
result := sortHotPeers(tt.firstSort, tt.secondSort, tt.maxPeerNum)
676+
re.Len(result, len(tt.expected))
677+
678+
for _, expectedPeer := range tt.expected {
679+
_, exists := result[expectedPeer]
680+
re.True(exists, "Expected peer not found in result")
681+
}
682+
})
683+
}
684+
}

0 commit comments

Comments
 (0)