From 16e7f51033cf5d2162fb9933960ecc2b0fed9715 Mon Sep 17 00:00:00 2001 From: wei liu Date: Fri, 12 Jan 2024 15:10:51 +0800 Subject: [PATCH] fix: Dynamic update rate limit config with wrong value (#29902) pr: #29901 when apply dynamic config changes, we should format the value to proper unit This PR fix update rate limit config with wrong value. Signed-off-by: Wei Liu --- internal/proxy/multi_rate_limiter.go | 2 +- internal/proxy/multi_rate_limiter_test.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/proxy/multi_rate_limiter.go b/internal/proxy/multi_rate_limiter.go index beb694bd75..d1cf2adae2 100644 --- a/internal/proxy/multi_rate_limiter.go +++ b/internal/proxy/multi_rate_limiter.go @@ -336,7 +336,7 @@ func (rl *rateLimiter) registerLimiters(globalLevel bool) { rl.limiters.GetOrInsert(internalpb.RateType(rt), ratelimitutil.NewLimiter(limit, burst)) onEvent := func(rateType internalpb.RateType) func(*config.Event) { return func(event *config.Event) { - f, err := strconv.ParseFloat(event.Value, 64) + f, err := strconv.ParseFloat(r.Formatter(event.Value), 64) if err != nil { log.Info("Error format for rateLimit", zap.String("rateType", rateType.String()), diff --git a/internal/proxy/multi_rate_limiter_test.go b/internal/proxy/multi_rate_limiter_test.go index fee4dc2092..780b99a007 100644 --- a/internal/proxy/multi_rate_limiter_test.go +++ b/internal/proxy/multi_rate_limiter_test.go @@ -299,12 +299,19 @@ func TestRateLimiter(t *testing.T) { Params.EtcdCfg.EtcdTLSCACert.GetValue(), Params.EtcdCfg.EtcdTLSMinVersion.GetValue()) - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() + Params.Save(Params.QuotaConfig.DDLLimitEnabled.Key, "true") + defer Params.Reset(Params.QuotaConfig.DDLLimitEnabled.Key) + Params.Save(Params.QuotaConfig.DMLLimitEnabled.Key, "true") + defer Params.Reset(Params.QuotaConfig.DMLLimitEnabled.Key) + ctx := context.Background() // avoid production precision issues when comparing 0-terminated numbers newRate := fmt.Sprintf("%.2f1", rand.Float64()) + etcdCli.KV.Put(ctx, "by-dev/config/quotaAndLimits/dml/insertRate/collection/max", "8") + defer etcdCli.KV.Delete(ctx, "by-dev/config/quotaAndLimits/dml/insertRate/collection/max") etcdCli.KV.Put(ctx, "by-dev/config/quotaAndLimits/ddl/collectionRate", newRate) + defer etcdCli.KV.Delete(ctx, "by-dev/config/quotaAndLimits/ddl/collectionRate") etcdCli.KV.Put(ctx, "by-dev/config/quotaAndLimits/ddl/partitionRate", "invalid") + defer etcdCli.KV.Delete(ctx, "by-dev/config/quotaAndLimits/ddl/partitionRate") assert.Eventually(t, func() bool { limit, _ := limiter.limiters.Get(internalpb.RateType_DDLCollection) @@ -313,5 +320,8 @@ func TestRateLimiter(t *testing.T) { limit, _ := limiter.limiters.Get(internalpb.RateType_DDLPartition) assert.Equal(t, "+inf", limit.Limit().String()) + + limit, _ = limiter.limiters.Get(internalpb.RateType_DMLInsert) + assert.Equal(t, "8.388608e+06", limit.Limit().String()) }) }