@@ -612,7 +612,7 @@ func TestAffinityCheckerMergePath(t *testing.T) {
612612 defer cancel ()
613613
614614 opt := mockconfig .NewTestOptions ()
615- opt .SetMaxAffinityMergeRegionSize (20 )
615+ opt .SetMaxAffinityMergeRegionSize (60 ) // Larger than default MaxMergeRegionSize
616616 tc := mockcluster .NewCluster (ctx , opt )
617617 tc .AddRegionStore (1 , 100 )
618618 tc .AddRegionStore (2 , 100 )
@@ -659,7 +659,7 @@ func TestAffinityMergeCheckNoTarget(t *testing.T) {
659659 defer cancel ()
660660
661661 opt := mockconfig .NewTestOptions ()
662- opt .SetMaxAffinityMergeRegionSize (20 )
662+ opt .SetMaxAffinityMergeRegionSize (60 ) // Larger than default MaxMergeRegionSize
663663 tc := mockcluster .NewCluster (ctx , opt )
664664 tc .AddRegionStore (1 , 100 )
665665 tc .AddRegionStore (2 , 100 )
@@ -696,7 +696,7 @@ func TestAffinityMergeCheckDifferentGroups(t *testing.T) {
696696 defer cancel ()
697697
698698 opt := mockconfig .NewTestOptions ()
699- opt .SetMaxAffinityMergeRegionSize (20 )
699+ opt .SetMaxAffinityMergeRegionSize (60 ) // Larger than default MaxMergeRegionSize
700700 tc := mockcluster .NewCluster (ctx , opt )
701701 tc .AddRegionStore (1 , 100 )
702702 tc .AddRegionStore (2 , 100 )
@@ -757,7 +757,7 @@ func TestAffinityMergeCheckRegionTooLarge(t *testing.T) {
757757 defer cancel ()
758758
759759 opt := mockconfig .NewTestOptions ()
760- opt .SetMaxAffinityMergeRegionSize (20 )
760+ opt .SetMaxAffinityMergeRegionSize (60 ) // Larger than default MaxMergeRegionSize
761761 tc := mockcluster .NewCluster (ctx , opt )
762762 tc .AddRegionStore (1 , 100 )
763763 tc .AddRegionStore (2 , 100 )
@@ -770,8 +770,8 @@ func TestAffinityMergeCheckRegionTooLarge(t *testing.T) {
770770 region2 := tc .GetRegion (2 )
771771
772772 region1 = region1 .Clone (
773- core .SetApproximateSize (30 ), // Too large to merge
774- core .SetApproximateKeys (30000 ),
773+ core .SetApproximateSize (70 ), // Too large to merge under 60 MB limit
774+ core .SetApproximateKeys (700000 ),
775775 core .WithStartKey ([]byte ("a" )),
776776 core .WithEndKey ([]byte ("b" )),
777777 )
@@ -1012,7 +1012,7 @@ func TestAffinityMergeCheckTargetTooBig(t *testing.T) {
10121012 defer cancel ()
10131013
10141014 opt := mockconfig .NewTestOptions ()
1015- opt .SetMaxAffinityMergeRegionSize (20 ) // Max size 20, Max keys 200000
1015+ opt .SetMaxAffinityMergeRegionSize (60 ) // Larger than default MaxMergeRegionSize
10161016 tc := mockcluster .NewCluster (ctx , opt )
10171017 tc .AddRegionStore (1 , 100 )
10181018 tc .AddRegionStore (2 , 100 )
@@ -1022,13 +1022,63 @@ func TestAffinityMergeCheckTargetTooBig(t *testing.T) {
10221022 tc .AddLeaderRegion (1 , 1 , 2 , 3 )
10231023 tc .AddLeaderRegion (2 , 1 , 2 , 3 )
10241024 region1 := tc .GetRegion (1 ).Clone (
1025- core .SetApproximateSize (15 ), // 15 size (Source)
1025+ core .SetApproximateSize (45 ), // Source fits under 60 MB limit
1026+ core .SetApproximateKeys (450000 ),
1027+ core .WithStartKey ([]byte ("a" )),
1028+ core .WithEndKey ([]byte ("b" )),
1029+ )
1030+ region2 := tc .GetRegion (2 ).Clone (
1031+ core .SetApproximateSize (25 ), // Total: 70 > 60
1032+ core .SetApproximateKeys (250000 ),
1033+ core .WithStartKey ([]byte ("b" )),
1034+ core .WithEndKey ([]byte ("c" )),
1035+ )
1036+
1037+ tc .PutRegion (region1 )
1038+ tc .PutRegion (region2 )
1039+
1040+ affinityManager := tc .GetAffinityManager ()
1041+ checker := NewAffinityChecker (tc , opt )
1042+
1043+ group := & affinity.Group {
1044+ ID : "test_group" ,
1045+ LeaderStoreID : 1 ,
1046+ VoterStoreIDs : []uint64 {1 , 2 , 3 },
1047+ }
1048+ err := createAffinityGroupForTest (affinityManager , group , []byte ("" ), []byte ("" ))
1049+ re .NoError (err )
1050+
1051+ // MergeCheck should return nil because the combined size (70) exceeds the limit (60)
1052+ groupState , _ := affinityManager .GetRegionAffinityGroupState (region1 )
1053+ re .NotNil (groupState )
1054+ ops := checker .MergeCheck (region1 , groupState )
1055+ re .Nil (ops , "Merged size exceeds MaxAffinityMergeRegionSize" )
1056+ }
1057+
1058+ // TestAffinityMergeCheckTargetRespectsLargerGlobalLimit verifies that the merge
1059+ // limit follows the larger value between MaxMergeRegionSize and MaxAffinityMergeRegionSize.
1060+ func TestAffinityMergeCheckTargetRespectsLargerGlobalLimit (t * testing.T ) {
1061+ re := require .New (t )
1062+ ctx , cancel := context .WithCancel (context .Background ())
1063+ defer cancel ()
1064+
1065+ opt := mockconfig .NewTestOptions ()
1066+ opt .SetMaxAffinityMergeRegionSize (20 ) // Smaller than the default MaxMergeRegionSize (54)
1067+ tc := mockcluster .NewCluster (ctx , opt )
1068+ tc .AddRegionStore (1 , 100 )
1069+ tc .AddRegionStore (2 , 100 )
1070+ tc .AddRegionStore (3 , 100 )
1071+
1072+ tc .AddLeaderRegion (1 , 1 , 2 , 3 )
1073+ tc .AddLeaderRegion (2 , 1 , 2 , 3 )
1074+ region1 := tc .GetRegion (1 ).Clone (
1075+ core .SetApproximateSize (15 ),
10261076 core .SetApproximateKeys (150000 ),
10271077 core .WithStartKey ([]byte ("a" )),
10281078 core .WithEndKey ([]byte ("b" )),
10291079 )
10301080 region2 := tc .GetRegion (2 ).Clone (
1031- core .SetApproximateSize (6 ), // 6 size (Target). Total : 21 > 20
1081+ core .SetApproximateSize (6 ), // Total size: 21, allowed by global limit 54
10321082 core .SetApproximateKeys (60000 ),
10331083 core .WithStartKey ([]byte ("b" )),
10341084 core .WithEndKey ([]byte ("c" )),
@@ -1048,11 +1098,10 @@ func TestAffinityMergeCheckTargetTooBig(t *testing.T) {
10481098 err := createAffinityGroupForTest (affinityManager , group , []byte ("" ), []byte ("" ))
10491099 re .NoError (err )
10501100
1051- // MergeCheck should return nil because the combined size (21) exceeds the limit (20)
10521101 groupState , _ := affinityManager .GetRegionAffinityGroupState (region1 )
10531102 re .NotNil (groupState )
10541103 ops := checker .MergeCheck (region1 , groupState )
1055- re .Nil (ops , "Merged size exceeds MaxAffinityMergeRegionSize " )
1104+ re .NotNil (ops , "Merge should be allowed because the effective limit follows the larger global max " )
10561105}
10571106
10581107// TestAffinityMergeCheckAdjacentUnhealthy tests that merging is blocked if the adjacent region is unhealthy.
0 commit comments