From dfe5ede8b20a1c4038e5be017763ed8a020b95b7 Mon Sep 17 00:00:00 2001 From: husharp Date: Wed, 19 Jul 2023 09:43:53 +0800 Subject: [PATCH] add fallback Signed-off-by: husharp --- .../resource_group/controller/controller.go | 27 +++++++++++++++---- .../resourcemanager/resource_manager_test.go | 22 +++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/client/resource_group/controller/controller.go b/client/resource_group/controller/controller.go index 3415a0d6b8b4..aa585f85bff4 100755 --- a/client/resource_group/controller/controller.go +++ b/client/resource_group/controller/controller.go @@ -486,14 +486,31 @@ func (c *ResourceGroupsController) IsBackgroundRequest(ctx context.Context, gc.metaLock.RLock() defer gc.metaLock.RUnlock() - if bg := gc.meta.BackgroundSettings; bg != nil { - if len(requestResource) == 0 || len(bg.JobTypes) == 0 { + log.Info("[resource group controller] checkBackgroundSettings resource group", zap.String("resource group", resourceGroupName)) + return c.checkBackgroundSettings(ctx, gc.meta.BackgroundSettings, requestResource) +} + +func (c *ResourceGroupsController) checkBackgroundSettings(ctx context.Context, bg *rmpb.BackgroundSettings, requestResource string) bool { + // fallback to default resource group. + if bg == nil { + resourceGroupName := "default" + gc, err := c.tryGetResourceGroup(ctx, resourceGroupName) + if err != nil { + log.Info("[resource group controller] fallback to default resource group failed", zap.Any("resource group", gc)) + failedRequestCounter.WithLabelValues(resourceGroupName).Inc() return false } - if idx := strings.LastIndex(requestResource, "_"); idx != -1 { - return slices.Contains(bg.JobTypes, requestResource[idx+1:]) - } + bg = gc.meta.BackgroundSettings + } + + if bg == nil || len(requestResource) == 0 || len(bg.JobTypes) == 0 { + return false } + + if idx := strings.LastIndex(requestResource, "_"); idx != -1 { + return slices.Contains(bg.JobTypes, requestResource[idx+1:]) + } + return false } diff --git a/tests/integrations/mcs/resourcemanager/resource_manager_test.go b/tests/integrations/mcs/resourcemanager/resource_manager_test.go index b429ff4f442f..f720d46894af 100644 --- a/tests/integrations/mcs/resourcemanager/resource_manager_test.go +++ b/tests/integrations/mcs/resourcemanager/resource_manager_test.go @@ -1189,6 +1189,23 @@ func (suite *resourceManagerClientTestSuite) TestSkipConsumptionForBackgroundJob re.Contains(resp, "Success!") } + resp, err := cli.ModifyResourceGroup(suite.ctx, &rmpb.ResourceGroup{ + Name: "default", + Mode: rmpb.GroupMode_RUMode, + RUSettings: &rmpb.GroupRequestUnitSettings{ + RU: &rmpb.TokenBucket{ + Settings: &rmpb.TokenLimitSettings{ + FillRate: 1, + BurstLimit: -1, + }, + Tokens: 1, + }, + }, + BackgroundSettings: &rmpb.BackgroundSettings{JobTypes: []string{"lightning", "ddl"}}, + }) + re.NoError(err) + re.Contains(resp, "Success!") + cfg := &controller.RequestUnitConfig{ ReadBaseCost: 1, ReadCostPerByte: 1, @@ -1201,10 +1218,15 @@ func (suite *resourceManagerClientTestSuite) TestSkipConsumptionForBackgroundJob resourceGroupName := suite.initGroups[1].Name re.False(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_default")) + // test fallback. + re.True(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_lightning")) + re.True(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_ddl")) resourceGroupName = "background_job" re.True(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_br")) re.True(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_lightning")) + // test fallback. + re.False(c.IsBackgroundRequest(suite.ctx, resourceGroupName, "internal_ddl")) c.Stop() }