@@ -1864,3 +1864,52 @@ func TestingRaftStatusFn(desc *roachpb.RangeDescriptor, storeID roachpb.StoreID)
1864
1864
}
1865
1865
return status
1866
1866
}
1867
+
1868
+ // TestReplicateQueueMaxSize tests the max size of the replicate queue and
1869
+ // verifies that replicas are dropped when the max size is exceeded. It also
1870
+ // checks that the metric ReplicateQueueDroppedDueToSize is updated correctly.
1871
+ func TestReplicateQueueMaxSize (t * testing.T ) {
1872
+ defer leaktest .AfterTest (t )()
1873
+ defer log .Scope (t ).Close (t )
1874
+ tc := testContext {}
1875
+ stopper := stop .NewStopper ()
1876
+ ctx := context .Background ()
1877
+ defer stopper .Stop (ctx )
1878
+ tc .Start (ctx , t , stopper )
1879
+
1880
+ r , err := tc .store .GetReplica (1 )
1881
+ require .NoError (t , err )
1882
+
1883
+ stopper , _ , _ , a , _ := allocatorimpl .CreateTestAllocator (ctx , 10 , true /* deterministic */ )
1884
+ defer stopper .Stop (ctx )
1885
+ ReplicateQueueMaxSize .Override (ctx , & tc .store .cfg .Settings .SV , 1 )
1886
+ replicateQueue := newReplicateQueue (tc .store , a )
1887
+
1888
+ // Function to add a replica and verify queue state
1889
+ addReplicaAndVerify := func (rangeID roachpb.RangeID , expectedLength int , expectedDropped int64 ) {
1890
+ r .Desc ().RangeID = rangeID
1891
+ enqueued , err := replicateQueue .testingAdd (context .Background (), r , 0.0 )
1892
+ require .NoError (t , err )
1893
+ require .True (t , enqueued )
1894
+ require .Equal (t , expectedLength , replicateQueue .Length ())
1895
+ require .Equal (t , expectedDropped , replicateQueue .droppedDueToSize .Count ())
1896
+ require .Equal (t , expectedDropped , tc .store .metrics .ReplicateQueueDroppedDueToSize .Count ())
1897
+ }
1898
+
1899
+ // First replica should be added.
1900
+ addReplicaAndVerify (1 /* rangeID */ , 1 /* expectedLength */ , 0 /* expectedDropped */ )
1901
+ // Second replica should be dropped.
1902
+ addReplicaAndVerify (2 /* rangeID */ , 1 /* expectedLength */ , 1 /* expectedDropped */ )
1903
+ // Third replica should be dropped.
1904
+ addReplicaAndVerify (3 /* rangeID */ , 1 /* expectedLength */ , 2 /* expectedDropped */ )
1905
+
1906
+ // Increase the max size to 100 and add more replicas
1907
+ ReplicateQueueMaxSize .Override (ctx , & tc .store .cfg .Settings .SV , 100 )
1908
+ for i := 2 ; i <= 100 ; i ++ {
1909
+ // Should be added.
1910
+ addReplicaAndVerify (roachpb .RangeID (i + 1 /* rangeID */ ), i /* expectedLength */ , 2 /* expectedDropped */ )
1911
+ }
1912
+
1913
+ // Add one more to exceed the max size. Should be dropped.
1914
+ addReplicaAndVerify (102 /* rangeID */ , 100 /* expectedLength */ , 3 /* expectedDropped */ )
1915
+ }
0 commit comments