2022-09-16 09:56:47 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rootcoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/milvuspb"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/metricsinfo"
|
2022-12-07 18:01:19 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2022-09-16 09:56:47 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/tsoutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type queryCoordMockForQuota struct {
|
|
|
|
mockQueryCoord
|
|
|
|
retErr bool
|
|
|
|
retFailStatus bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type dataCoordMockForQuota struct {
|
|
|
|
mockDataCoord
|
|
|
|
retErr bool
|
|
|
|
retFailStatus bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *queryCoordMockForQuota) GetMetrics(ctx context.Context, request *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
|
|
|
|
if q.retErr {
|
|
|
|
return nil, fmt.Errorf("mock err")
|
|
|
|
}
|
|
|
|
if q.retFailStatus {
|
|
|
|
return &milvuspb.GetMetricsResponse{
|
|
|
|
Status: failStatus(commonpb.ErrorCode_UnexpectedError, "mock failure status"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &milvuspb.GetMetricsResponse{
|
|
|
|
Status: succStatus(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dataCoordMockForQuota) GetMetrics(ctx context.Context, request *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
|
|
|
|
if d.retErr {
|
|
|
|
return nil, fmt.Errorf("mock err")
|
|
|
|
}
|
|
|
|
if d.retFailStatus {
|
|
|
|
return &milvuspb.GetMetricsResponse{
|
|
|
|
Status: failStatus(commonpb.ErrorCode_UnexpectedError, "mock failure status"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &milvuspb.GetMetricsResponse{
|
|
|
|
Status: succStatus(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotaCenter(t *testing.T) {
|
|
|
|
Params.Init()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
core, err := NewCore(ctx, nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
core.tsoAllocator = newMockTsoAllocator()
|
|
|
|
|
|
|
|
pcm := newProxyClientManager(core.proxyCreator)
|
|
|
|
|
|
|
|
t.Run("test QuotaCenter", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
go quotaCenter.run()
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
quotaCenter.stop()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test syncMetrics", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.syncMetrics()
|
|
|
|
assert.Error(t, err) // for empty response
|
|
|
|
|
|
|
|
quotaCenter = NewQuotaCenter(pcm, &queryCoordMockForQuota{retErr: true}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.syncMetrics()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
quotaCenter = NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{retErr: true}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.syncMetrics()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
quotaCenter = NewQuotaCenter(pcm, &queryCoordMockForQuota{retFailStatus: true}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.syncMetrics()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
quotaCenter = NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{retFailStatus: true}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.syncMetrics()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test forceDeny", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaCenter.forceDenyReading(commonpb.ErrorCode_ForceDeny)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DQLQuery])
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DQLQuery])
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaCenter.forceDenyWriting(commonpb.ErrorCode_ForceDeny)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLInsert])
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLDelete])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test calculateRates", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.calculateRates()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
alloc := newMockTsoAllocator()
|
|
|
|
alloc.GenerateTSOF = func(count uint32) (typeutil.Timestamp, error) {
|
|
|
|
return 0, fmt.Errorf("mock err")
|
|
|
|
}
|
|
|
|
quotaCenter.tsoAllocator = alloc
|
|
|
|
err = quotaCenter.calculateRates()
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getTimeTickDelayFactor", func(t *testing.T) {
|
2022-09-16 09:56:47 +08:00
|
|
|
// test MaxTimestamp
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getTimeTickDelayFactor(0)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(1), factor)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.TtProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.MaxTimeTickDelay.Key, "3")
|
2022-09-26 16:48:53 +08:00
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
// test force deny writing
|
|
|
|
alloc := newMockTsoAllocator()
|
|
|
|
alloc.GenerateTSOF = func(count uint32) (typeutil.Timestamp, error) {
|
2022-12-07 18:01:19 +08:00
|
|
|
added := now.Add(Params.QuotaConfig.MaxTimeTickDelay.GetAsDuration(time.Second))
|
2022-09-16 09:56:47 +08:00
|
|
|
ts := tsoutil.ComposeTSByTime(added, 0)
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
quotaCenter.tsoAllocator = alloc
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {Fgm: metricsinfo.FlowGraphMetric{
|
2023-01-16 18:49:43 +08:00
|
|
|
MinFlowGraphTt: tsoutil.ComposeTSByTime(now, 0),
|
|
|
|
NumFlowGraph: 1,
|
|
|
|
MinFlowGraphChannel: "dml",
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
|
|
|
ts, err := quotaCenter.tsoAllocator.GenerateTSO(1)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getTimeTickDelayFactor(ts)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(0), factor)
|
|
|
|
|
|
|
|
// test one-third time tick delay
|
|
|
|
alloc.GenerateTSOF = func(count uint32) (typeutil.Timestamp, error) {
|
2022-12-07 18:01:19 +08:00
|
|
|
oneThirdDelay := Params.QuotaConfig.MaxTimeTickDelay.GetAsDuration(time.Second) / 3
|
2022-09-16 09:56:47 +08:00
|
|
|
added := now.Add(oneThirdDelay)
|
|
|
|
oneThirdTs := tsoutil.ComposeTSByTime(added, 0)
|
|
|
|
return oneThirdTs, nil
|
|
|
|
}
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {Fgm: metricsinfo.FlowGraphMetric{
|
2023-01-16 18:49:43 +08:00
|
|
|
MinFlowGraphTt: tsoutil.ComposeTSByTime(now, 0),
|
|
|
|
NumFlowGraph: 1,
|
|
|
|
MinFlowGraphChannel: "dml",
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
|
|
|
ts, err = quotaCenter.tsoAllocator.GenerateTSO(1)
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.NoError(t, err)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getTimeTickDelayFactor(ts)
|
2022-09-16 09:56:47 +08:00
|
|
|
ok := math.Abs(factor-2.0/3.0) < 0.0001
|
|
|
|
assert.True(t, ok)
|
2022-10-19 15:13:26 +08:00
|
|
|
})
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getTimeTickDelayFactor factors", func(t *testing.T) {
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
type ttCase struct {
|
|
|
|
maxTtDelay time.Duration
|
|
|
|
curTt time.Time
|
|
|
|
fgTt time.Time
|
|
|
|
expectedFactor float64
|
2022-09-16 09:56:47 +08:00
|
|
|
}
|
2022-10-19 15:13:26 +08:00
|
|
|
t0 := time.Now()
|
|
|
|
ttCases := []ttCase{
|
|
|
|
{10 * time.Second, t0, t0.Add(1 * time.Second), 1},
|
|
|
|
{10 * time.Second, t0, t0, 1},
|
|
|
|
{10 * time.Second, t0.Add(1 * time.Second), t0, 0.9},
|
|
|
|
{10 * time.Second, t0.Add(2 * time.Second), t0, 0.8},
|
|
|
|
{10 * time.Second, t0.Add(5 * time.Second), t0, 0.5},
|
|
|
|
{10 * time.Second, t0.Add(7 * time.Second), t0, 0.3},
|
|
|
|
{10 * time.Second, t0.Add(9 * time.Second), t0, 0.1},
|
|
|
|
{10 * time.Second, t0.Add(10 * time.Second), t0, 0},
|
|
|
|
{10 * time.Second, t0.Add(100 * time.Second), t0, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
backup := Params.QuotaConfig.MaxTimeTickDelay
|
|
|
|
|
|
|
|
for i, c := range ttCases {
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.MaxTimeTickDelay.Key, fmt.Sprintf("%f", c.maxTtDelay.Seconds()))
|
2022-10-19 15:13:26 +08:00
|
|
|
fgTs := tsoutil.ComposeTSByTime(c.fgTt, 0)
|
2023-01-16 18:49:43 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {
|
|
|
|
Fgm: metricsinfo.FlowGraphMetric{
|
|
|
|
NumFlowGraph: 1,
|
|
|
|
MinFlowGraphTt: fgTs,
|
|
|
|
MinFlowGraphChannel: "dml",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-10-19 15:13:26 +08:00
|
|
|
curTs := tsoutil.ComposeTSByTime(c.curTt, 0)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getTimeTickDelayFactor(curTs)
|
2022-10-19 15:13:26 +08:00
|
|
|
if math.Abs(factor-c.expectedFactor) > 0.000001 {
|
|
|
|
t.Errorf("case %d failed: curTs[%v], fgTs[%v], expectedFactor: %f, actualFactor: %f",
|
|
|
|
i, c.curTt, c.fgTt, c.expectedFactor, factor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Params.QuotaConfig.MaxTimeTickDelay = backup
|
2022-09-16 09:56:47 +08:00
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getNQInQueryFactor", func(t *testing.T) {
|
2022-09-16 09:56:47 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getNQInQueryFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(1), factor)
|
|
|
|
|
|
|
|
// test cool off
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueueProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.NQInQueueThreshold.Key, "100")
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-12-07 18:01:19 +08:00
|
|
|
UnsolvedQueue: Params.QuotaConfig.NQInQueueThreshold.GetAsInt64(),
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getNQInQueryFactor()
|
2022-12-07 18:01:19 +08:00
|
|
|
assert.Equal(t, Params.QuotaConfig.CoolOffSpeed.GetAsFloat(), factor)
|
2022-09-16 09:56:47 +08:00
|
|
|
|
|
|
|
// test no cool off
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-12-07 18:01:19 +08:00
|
|
|
UnsolvedQueue: Params.QuotaConfig.NQInQueueThreshold.GetAsInt64() - 1,
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getNQInQueryFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, 1.0, factor)
|
|
|
|
//ok := math.Abs(factor-1.0) < 0.0001
|
|
|
|
//assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getQueryLatencyFactor", func(t *testing.T) {
|
2022-09-16 09:56:47 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getQueryLatencyFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(1), factor)
|
|
|
|
|
|
|
|
// test cool off
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueueProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueueLatencyThreshold.Key, "3")
|
2022-09-26 16:48:53 +08:00
|
|
|
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-12-07 18:01:19 +08:00
|
|
|
AvgQueueDuration: Params.QuotaConfig.QueueLatencyThreshold.GetAsDuration(time.Second),
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getQueryLatencyFactor()
|
2022-12-07 18:01:19 +08:00
|
|
|
assert.Equal(t, Params.QuotaConfig.CoolOffSpeed.GetAsFloat(), factor)
|
2022-09-16 09:56:47 +08:00
|
|
|
|
|
|
|
// test no cool off
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-09-16 09:56:47 +08:00
|
|
|
AvgQueueDuration: 1 * time.Second,
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getQueryLatencyFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, 1.0, factor)
|
2022-10-13 14:57:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test checkReadResult", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getReadResultFactor()
|
2022-10-13 14:57:24 +08:00
|
|
|
assert.Equal(t, float64(1), factor)
|
|
|
|
|
|
|
|
// test cool off
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.ResultProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.MaxReadResultRate.Key, fmt.Sprintf("%f", 1.0/1024/1024))
|
2022-10-13 14:57:24 +08:00
|
|
|
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.proxyMetrics = map[UniqueID]*metricsinfo.ProxyQuotaMetrics{
|
|
|
|
1: {Rms: []metricsinfo.RateMetric{
|
2022-10-13 14:57:24 +08:00
|
|
|
{Label: metricsinfo.ReadResultThroughput, Rate: 1.2},
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getReadResultFactor()
|
2022-12-07 18:01:19 +08:00
|
|
|
assert.Equal(t, Params.QuotaConfig.CoolOffSpeed.GetAsFloat(), factor)
|
2022-10-13 14:57:24 +08:00
|
|
|
|
|
|
|
// test no cool off
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.proxyMetrics = map[UniqueID]*metricsinfo.ProxyQuotaMetrics{
|
|
|
|
1: {Rms: []metricsinfo.RateMetric{
|
2022-10-13 14:57:24 +08:00
|
|
|
{Label: metricsinfo.ReadResultThroughput, Rate: 0.8},
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getReadResultFactor()
|
2022-10-13 14:57:24 +08:00
|
|
|
assert.Equal(t, 1.0, factor)
|
2022-09-16 09:56:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test calculateReadRates", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.proxyMetrics = map[UniqueID]*metricsinfo.ProxyQuotaMetrics{
|
|
|
|
1: {Rms: []metricsinfo.RateMetric{
|
2022-09-16 09:56:47 +08:00
|
|
|
{Label: internalpb.RateType_DQLSearch.String(), Rate: 100},
|
|
|
|
{Label: internalpb.RateType_DQLQuery.String(), Rate: 100},
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-09-16 09:56:47 +08:00
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.ForceDenyReading.Key, "false")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueueProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueueLatencyThreshold.Key, "100")
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-12-07 18:01:19 +08:00
|
|
|
AvgQueueDuration: Params.QuotaConfig.QueueLatencyThreshold.GetAsDuration(time.Second),
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-09-16 09:56:47 +08:00
|
|
|
quotaCenter.calculateReadRates()
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLSearch])
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLQuery])
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.NQInQueueThreshold.Key, "100")
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{
|
|
|
|
1: {SearchQueue: metricsinfo.ReadInfoInQueue{
|
2022-12-07 18:01:19 +08:00
|
|
|
UnsolvedQueue: Params.QuotaConfig.NQInQueueThreshold.GetAsInt64(),
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
2022-09-16 09:56:47 +08:00
|
|
|
quotaCenter.calculateReadRates()
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLSearch])
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLQuery])
|
2022-10-13 14:57:24 +08:00
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.ResultProtectionEnabled.Key, "true")
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.MaxReadResultRate.Key, "1")
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.proxyMetrics = map[UniqueID]*metricsinfo.ProxyQuotaMetrics{
|
|
|
|
1: {Rms: []metricsinfo.RateMetric{
|
2022-10-13 14:57:24 +08:00
|
|
|
{Label: internalpb.RateType_DQLSearch.String(), Rate: 100},
|
|
|
|
{Label: internalpb.RateType_DQLQuery.String(), Rate: 100},
|
|
|
|
{Label: metricsinfo.ReadResultThroughput, Rate: 1.2},
|
2022-10-19 15:13:26 +08:00
|
|
|
}}}
|
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{1: {SearchQueue: metricsinfo.ReadInfoInQueue{}}}
|
2022-10-13 14:57:24 +08:00
|
|
|
quotaCenter.calculateReadRates()
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLSearch])
|
|
|
|
assert.Equal(t, Limit(100.0*0.9), quotaCenter.currentRates[internalpb.RateType_DQLQuery])
|
2022-09-16 09:56:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("test calculateWriteRates", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
err = quotaCenter.calculateWriteRates()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-12 10:03:22 +08:00
|
|
|
// DiskQuota exceeded
|
|
|
|
quotaBackup := Params.QuotaConfig.DiskQuota
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.DiskQuota.Key, "99")
|
2022-10-12 10:03:22 +08:00
|
|
|
quotaCenter.dataCoordMetrics = &metricsinfo.DataCoordQuotaMetrics{TotalBinlogSize: 100}
|
|
|
|
err = quotaCenter.calculateWriteRates()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLInsert])
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLDelete])
|
|
|
|
Params.QuotaConfig.DiskQuota = quotaBackup
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
// force deny
|
|
|
|
forceBak := Params.QuotaConfig.ForceDenyWriting
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.ForceDenyWriting.Key, "true")
|
2022-09-16 09:56:47 +08:00
|
|
|
err = quotaCenter.calculateWriteRates()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLInsert])
|
|
|
|
assert.Equal(t, Limit(0), quotaCenter.currentRates[internalpb.RateType_DMLDelete])
|
|
|
|
Params.QuotaConfig.ForceDenyWriting = forceBak
|
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getMemoryFactor basic", func(t *testing.T) {
|
2022-09-16 09:56:47 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getMemoryFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(1), factor)
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.dataNodeMetrics = map[UniqueID]*metricsinfo.DataNodeQuotaMetrics{1: {Hms: metricsinfo.HardwareMetrics{MemoryUsage: 100, Memory: 100}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getMemoryFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(0), factor)
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{1: {Hms: metricsinfo.HardwareMetrics{MemoryUsage: 100, Memory: 100}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor = quotaCenter.getMemoryFactor()
|
2022-09-16 09:56:47 +08:00
|
|
|
assert.Equal(t, float64(0), factor)
|
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test getMemoryFactor factors", func(t *testing.T) {
|
2022-10-14 18:05:24 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
type memCase struct {
|
|
|
|
lowWater float64
|
|
|
|
highWater float64
|
|
|
|
memUsage uint64
|
|
|
|
memTotal uint64
|
|
|
|
expectedFactor float64
|
|
|
|
}
|
|
|
|
memCases := []memCase{
|
2022-10-25 19:39:30 +08:00
|
|
|
{0.8, 0.9, 10, 100, 1},
|
2022-10-14 18:05:24 +08:00
|
|
|
{0.8, 0.9, 80, 100, 1},
|
|
|
|
{0.8, 0.9, 82, 100, 0.8},
|
|
|
|
{0.8, 0.9, 85, 100, 0.5},
|
|
|
|
{0.8, 0.9, 88, 100, 0.2},
|
|
|
|
{0.8, 0.9, 90, 100, 0},
|
|
|
|
|
2022-10-25 19:39:30 +08:00
|
|
|
{0.85, 0.95, 25, 100, 1},
|
2022-10-14 18:05:24 +08:00
|
|
|
{0.85, 0.95, 85, 100, 1},
|
|
|
|
{0.85, 0.95, 87, 100, 0.8},
|
|
|
|
{0.85, 0.95, 90, 100, 0.5},
|
|
|
|
{0.85, 0.95, 93, 100, 0.2},
|
|
|
|
{0.85, 0.95, 95, 100, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range memCases {
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueryNodeMemoryLowWaterLevel.Key, fmt.Sprintf("%f", c.lowWater))
|
|
|
|
paramtable.Get().Save(Params.QuotaConfig.QueryNodeMemoryHighWaterLevel.Key, fmt.Sprintf("%f", c.highWater))
|
2022-10-19 15:13:26 +08:00
|
|
|
quotaCenter.queryNodeMetrics = map[UniqueID]*metricsinfo.QueryNodeQuotaMetrics{1: {Hms: metricsinfo.HardwareMetrics{MemoryUsage: c.memUsage, Memory: c.memTotal}}}
|
2022-10-19 21:43:30 +08:00
|
|
|
factor := quotaCenter.getMemoryFactor()
|
2022-10-14 18:05:24 +08:00
|
|
|
if math.Abs(factor-c.expectedFactor) > 0.000001 {
|
|
|
|
t.Errorf("case %d failed: waterLever[low:%v, high:%v], memMetric[used:%d, total:%d], expectedFactor: %f, actualFactor: %f",
|
|
|
|
i, c.lowWater, c.highWater, c.memUsage, c.memTotal, c.expectedFactor, factor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Reset(Params.QuotaConfig.QueryNodeMemoryLowWaterLevel.Key)
|
|
|
|
paramtable.Get().Reset(Params.QuotaConfig.QueryNodeMemoryHighWaterLevel.Key)
|
2022-10-14 18:05:24 +08:00
|
|
|
})
|
|
|
|
|
2022-10-19 21:43:30 +08:00
|
|
|
t.Run("test ifDiskQuotaExceeded", func(t *testing.T) {
|
2022-10-12 10:03:22 +08:00
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.DiskProtectionEnabled.Key, "false")
|
2022-10-19 21:43:30 +08:00
|
|
|
ok := quotaCenter.ifDiskQuotaExceeded()
|
2022-10-12 10:03:22 +08:00
|
|
|
assert.False(t, ok)
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.DiskProtectionEnabled.Key, "true")
|
2022-10-12 10:03:22 +08:00
|
|
|
|
|
|
|
quotaBackup := Params.QuotaConfig.DiskQuota
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.DiskQuota.Key, fmt.Sprintf("%f", 99.0/1024/1024))
|
2022-10-12 10:03:22 +08:00
|
|
|
quotaCenter.dataCoordMetrics = &metricsinfo.DataCoordQuotaMetrics{TotalBinlogSize: 100}
|
2022-10-19 21:43:30 +08:00
|
|
|
ok = quotaCenter.ifDiskQuotaExceeded()
|
2022-10-12 10:03:22 +08:00
|
|
|
assert.True(t, ok)
|
|
|
|
|
2022-12-07 18:01:19 +08:00
|
|
|
paramtable.Get().Save(Params.QuotaConfig.DiskQuota.Key, fmt.Sprintf("%f", 101.0/1024/1024))
|
2022-10-12 10:03:22 +08:00
|
|
|
quotaCenter.dataCoordMetrics = &metricsinfo.DataCoordQuotaMetrics{TotalBinlogSize: 100}
|
2022-10-19 21:43:30 +08:00
|
|
|
ok = quotaCenter.ifDiskQuotaExceeded()
|
2022-10-12 10:03:22 +08:00
|
|
|
assert.False(t, ok)
|
|
|
|
Params.QuotaConfig.DiskQuota = quotaBackup
|
|
|
|
})
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
t.Run("test setRates", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
quotaCenter.currentRates[internalpb.RateType_DMLInsert] = 100
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaCenter.quotaStates[milvuspb.QuotaState_DenyToWrite] = commonpb.ErrorCode_MemoryQuotaExhausted
|
|
|
|
quotaCenter.quotaStates[milvuspb.QuotaState_DenyToRead] = commonpb.ErrorCode_ForceDeny
|
2022-09-16 09:56:47 +08:00
|
|
|
err = quotaCenter.setRates()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2022-12-30 18:35:32 +08:00
|
|
|
t.Run("test recordMetrics", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
2023-01-06 14:31:37 +08:00
|
|
|
quotaCenter.quotaStates[milvuspb.QuotaState_DenyToWrite] = commonpb.ErrorCode_MemoryQuotaExhausted
|
|
|
|
quotaCenter.quotaStates[milvuspb.QuotaState_DenyToRead] = commonpb.ErrorCode_ForceDeny
|
2022-12-30 18:35:32 +08:00
|
|
|
quotaCenter.recordMetrics()
|
|
|
|
})
|
|
|
|
|
2022-09-16 09:56:47 +08:00
|
|
|
t.Run("test guaranteeMinRate", func(t *testing.T) {
|
|
|
|
quotaCenter := NewQuotaCenter(pcm, &queryCoordMockForQuota{}, &dataCoordMockForQuota{}, core.tsoAllocator)
|
|
|
|
minRate := Limit(100)
|
|
|
|
quotaCenter.currentRates[internalpb.RateType_DQLSearch] = Limit(50)
|
|
|
|
quotaCenter.guaranteeMinRate(float64(minRate), internalpb.RateType_DQLSearch)
|
|
|
|
assert.Equal(t, minRate, quotaCenter.currentRates[internalpb.RateType_DQLSearch])
|
|
|
|
})
|
|
|
|
}
|