@@ -45,7 +45,11 @@ func mockMetaServiceGroups() map[string]string {
4545func (suite * metaServiceGroupTestSuite ) SetupTest () {
4646 suite .ctx , suite .cancel = context .WithCancel (context .Background ())
4747 store := endpoint .NewStorageEndpoint (kv .NewMemoryKV (), nil )
48- suite .manager = NewMetaServiceGroupManager (suite .ctx , store , true , mockMetaServiceGroups ())
48+ cfg := mockConfig {
49+ AutoAssignMetaServiceGroups : true ,
50+ MetaServiceGroups : mockMetaServiceGroups (),
51+ }
52+ suite .manager = NewMetaServiceGroupManager (suite .ctx , store , & cfg )
4953}
5054
5155func (suite * metaServiceGroupTestSuite ) TearDownTest () {
@@ -62,9 +66,10 @@ func (suite *metaServiceGroupTestSuite) TestInitialState() {
6266 re .Zero (status .AssignmentCount )
6367 re .False (status .Enabled )
6468 }
65- // Assign should return error due to no available groups.
66- _ , err = suite .manager .AssignToGroup (1 )
67- re .Error (err )
69+ // Assignment should fallback to PD
70+ group , err := suite .manager .AssignToGroup (1 )
71+ re .NoError (err )
72+ re .Empty (group )
6873}
6974
7075func (suite * metaServiceGroupTestSuite ) TestAssignToGroup () {
@@ -165,7 +170,7 @@ func (suite *metaServiceGroupTestSuite) TestUpdateEndpoints() {
165170 newMap := map [string ]string {
166171 "foo" : "foo.bar.local" ,
167172 }
168- suite .manager .updateConfig (true , newMap )
173+ suite .manager .updateConfig (true , newMap , 0 )
169174 config := map [string ]string {MetaServiceGroupIDKey : "foo" }
170175 suite .manager .AttachEndpoints (config )
171176 re .Equal ("foo.bar.local" , config [MetaServiceGroupAddressesKey ], "should read from updated metaServiceGroups map" )
@@ -191,7 +196,7 @@ func (suite *metaServiceGroupTestSuite) TestUpdateEndpointsAndUpdateAssignment()
191196 // Add a new group "etcd-group-3"
192197 newMap := mockMetaServiceGroups ()
193198 newMap ["etcd-group-3" ] = "etcd-group-3.tidb-serverless.cluster.svc.local"
194- suite .manager .updateConfig (true , newMap )
199+ suite .manager .updateConfig (true , newMap , 0 )
195200
196201 // Move the assignment from the originally assigned group to "etcd-group-3"
197202 err = suite .manager .UpdateAssignment (assigned , "etcd-group-3" )
@@ -212,3 +217,44 @@ func (suite *metaServiceGroupTestSuite) TestUpdateEndpointsAndUpdateAssignment()
212217 re .Equal (0 , statusMap [groupID ].AssignmentCount , "other original groups should remain at 0" )
213218 }
214219}
220+
221+ func (suite * metaServiceGroupTestSuite ) TestFallbackRatio () {
222+ re := suite .Require ()
223+ for groupID := range mockMetaServiceGroups () {
224+ enable := true
225+ re .NoError (suite .manager .PatchStatus (groupID , & MetaServiceGroupStatusPatch {
226+ Enabled : & enable ,
227+ }))
228+ }
229+ // Fallback ratio defaults to 0, all keyspace should be assigned to a group.
230+ for range 10 {
231+ assigned , err := suite .manager .AssignToGroup (1 )
232+ re .NoError (err )
233+ re .NotEmpty (assigned , "expected AssignToGroup to return a non-empty group" )
234+ }
235+ // Fallback ratio set to 0.5, some keyspace should be assigned to a group, some should fallback.
236+ batchSize := 100
237+ expectedFallbackRatio := 0.5
238+ suite .manager .fallbackRatio = expectedFallbackRatio
239+ totalFallback := 0
240+ for range batchSize {
241+ assigned , err := suite .manager .AssignToGroup (1 )
242+ re .NoError (err )
243+ if assigned == "" {
244+ totalFallback ++
245+ }
246+ }
247+ re .InDelta (expectedFallbackRatio , float64 (totalFallback )/ float64 (batchSize ), 0.1 )
248+
249+ // Fallback ratio set to 1, all keyspace should fallback to pd.
250+ suite .manager .fallbackRatio = 1.0
251+ totalFallback = 0
252+ for range batchSize {
253+ assigned , err := suite .manager .AssignToGroup (1 )
254+ re .NoError (err )
255+ if assigned == "" {
256+ totalFallback ++
257+ }
258+ }
259+ re .Equal (batchSize , totalFallback , "all keyspace should fallback to pd when fallback ratio is 1.0" )
260+ }
0 commit comments