-
Notifications
You must be signed in to change notification settings - Fork 759
Expand file tree
/
Copy pathgroup_controller_test.go
More file actions
317 lines (292 loc) · 8.97 KB
/
group_controller_test.go
File metadata and controls
317 lines (292 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2025 TiKV Project Authors.
//
// Licensed 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 controller
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/tikv/pd/client/errs"
)
func createTestGroupCostController(re *require.Assertions) *groupCostController {
group := &rmpb.ResourceGroup{
Name: "test",
Mode: rmpb.GroupMode_RUMode,
Priority: 1,
RUSettings: &rmpb.GroupRequestUnitSettings{
RU: &rmpb.TokenBucket{
Settings: &rmpb.TokenLimitSettings{
FillRate: 1000,
},
},
},
BackgroundSettings: &rmpb.BackgroundSettings{
JobTypes: []string{"lightning", "br"},
},
}
ch1 := make(chan notifyMsg)
ch2 := make(chan *groupCostController)
gc, err := newGroupCostController(group, DefaultRUConfig(), ch1, ch2, nil)
re.NoError(err)
return gc
}
func TestGroupControlBurstable(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
args := tokenBucketReconfigureArgs{
newFillRate: 1000,
newBurst: -1,
}
gc.run.requestUnitTokens.limiter.Reconfigure(time.Now(), args)
gc.updateAvgRequestResourcePerSec()
re.True(gc.burstable.Load())
}
func TestRequestAndResponseConsumption(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
testCases := []struct {
req *TestRequestInfo
resp *TestResponseInfo
}{
// Write request
{
req: &TestRequestInfo{
isWrite: true,
writeBytes: 100,
numReplicas: 3,
accessType: AccessUnknown,
},
resp: &TestResponseInfo{
readBytes: 100,
succeed: true,
},
},
// Write request local AZ
{
req: &TestRequestInfo{
isWrite: true,
writeBytes: 100,
numReplicas: 3,
accessType: AccessLocalZone,
},
resp: &TestResponseInfo{
readBytes: 100,
succeed: true,
},
},
// Write request cross AZ
{
req: &TestRequestInfo{
isWrite: true,
writeBytes: 100,
numReplicas: 3,
accessType: AccessCrossZone,
},
resp: &TestResponseInfo{
readBytes: 100,
succeed: true,
},
},
// Read request
{
req: &TestRequestInfo{
isWrite: false,
writeBytes: 0,
numReplicas: 3,
accessType: AccessLocalZone,
},
resp: &TestResponseInfo{
readBytes: 100,
kvCPU: 100 * time.Millisecond,
succeed: true,
},
},
// Read request cross AZ
{
req: &TestRequestInfo{
isWrite: false,
writeBytes: 0,
numReplicas: 3,
accessType: AccessCrossZone,
},
resp: &TestResponseInfo{
readBytes: 100,
kvCPU: 100 * time.Millisecond,
succeed: true,
},
},
}
kvCalculator := gc.getKVCalculator()
for idx, testCase := range testCases {
caseNum := fmt.Sprintf("case %d", idx)
consumption, _, _, priority, err := gc.onRequestWaitImpl(context.TODO(), testCase.req)
re.NoError(err, caseNum)
re.Equal(priority, gc.meta.Priority)
expectedConsumption := &rmpb.Consumption{}
if testCase.req.IsWrite() {
kvCalculator.calculateWriteCost(expectedConsumption, testCase.req)
re.Equal(expectedConsumption.WRU, consumption.WRU)
if testCase.req.AccessLocationType() != AccessUnknown {
re.Positive(expectedConsumption.WriteCrossAzTrafficBytes, caseNum)
}
}
consumption, err = gc.onResponseImpl(testCase.req, testCase.resp)
re.NoError(err, caseNum)
kvCalculator.calculateReadCost(expectedConsumption, testCase.resp)
kvCalculator.calculateCPUCost(expectedConsumption, testCase.resp)
calculateCrossAZTraffic(expectedConsumption, testCase.req, testCase.resp)
re.Equal(expectedConsumption.RRU, consumption.RRU, caseNum)
re.Equal(expectedConsumption.TotalCpuTimeMs, consumption.TotalCpuTimeMs, caseNum)
if testCase.req.IsWrite() && testCase.req.AccessLocationType() != AccessUnknown {
re.Positive(expectedConsumption.WriteCrossAzTrafficBytes, caseNum)
} else if !testCase.req.IsWrite() && testCase.req.AccessLocationType() == AccessCrossZone {
re.Positive(expectedConsumption.ReadCrossAzTrafficBytes, caseNum)
} else {
re.Equal(expectedConsumption.ReadCrossAzTrafficBytes, uint64(0), caseNum)
re.Equal(expectedConsumption.WriteCrossAzTrafficBytes, uint64(0), caseNum)
}
}
}
func TestOnResponseWaitConsumption(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
req := &TestRequestInfo{
isWrite: false,
}
resp := &TestResponseInfo{
readBytes: 2000 * 64 * 1024, // 2000RU
succeed: true,
}
consumption, waitTIme, err := gc.onResponseWaitImpl(context.TODO(), req, resp)
re.NoError(err)
re.Zero(waitTIme)
verify := func() {
expectedConsumption := &rmpb.Consumption{}
kvCalculator := gc.getKVCalculator()
kvCalculator.calculateReadCost(expectedConsumption, resp)
re.Equal(expectedConsumption.RRU, consumption.RRU)
}
verify()
// modify the counter, then on response should has wait time.
gc.modifyTokenCounter(gc.run.requestUnitTokens, &rmpb.TokenBucket{
Settings: &rmpb.TokenLimitSettings{
FillRate: 1000,
BurstLimit: 1000,
},
},
int64(5*time.Second/time.Millisecond),
)
consumption, waitTIme, err = gc.onResponseWaitImpl(context.TODO(), req, resp)
re.NoError(err)
re.NotZero(waitTIme)
verify()
}
func TestResourceGroupThrottledError(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
req := &TestRequestInfo{
isWrite: true,
writeBytes: 10000000,
}
// The group is throttled
_, _, _, _, err := gc.onRequestWaitImpl(context.TODO(), req)
re.Error(err)
re.True(errs.ErrClientResourceGroupThrottled.Equal(err))
}
func TestAcquireTokensSignalAwareWait(t *testing.T) {
re := require.New(t)
// Build controller with a buffered lowRUNotifyChan so the test can
// observe the notify() call inside reserveN as a synchronization point.
group := &rmpb.ResourceGroup{
Name: "test",
Mode: rmpb.GroupMode_RUMode,
RUSettings: &rmpb.GroupRequestUnitSettings{
RU: &rmpb.TokenBucket{
Settings: &rmpb.TokenLimitSettings{FillRate: 1000},
},
},
}
notifyCh := make(chan notifyMsg, 1)
cfg := DefaultRUConfig()
cfg.WaitRetryInterval = 5 * time.Second
cfg.WaitRetryTimes = 3
gc, err := newGroupCostController(group, cfg, notifyCh, make(chan *groupCostController, 1), nil)
re.NoError(err)
// Set fillRate=0 so reservation always fails with InfDuration,
// which is the exact scenario described in issue #10251.
counter := gc.run.requestUnitTokens
counter.limiter.Reconfigure(time.Now(), tokenBucketReconfigureArgs{
newTokens: 1000,
newFillRate: 0,
newBurst: 0,
})
delta := &rmpb.Consumption{RRU: 5000}
type acquireResult struct {
err error
waitDuration time.Duration
}
resultCh := make(chan acquireResult, 1)
go func() {
var waitDuration time.Duration
_, err := gc.acquireTokens(context.Background(), delta, &waitDuration, false)
resultCh <- acquireResult{err, waitDuration}
}()
// Wait for notify — Reserve has failed and the retry path is entered.
select {
case <-notifyCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for low-RU notification")
}
// Now reconfigure with enough tokens and a real fillRate.
// This closes the reconfiguredCh (waking the select) and provides
// tokens so the next Reserve() succeeds.
counter.limiter.Reconfigure(time.Now(), tokenBucketReconfigureArgs{
newTokens: 100000,
newFillRate: 100000,
newBurst: 0,
})
select {
case r := <-resultCh:
re.NoError(r.err)
re.Less(r.waitDuration, cfg.WaitRetryInterval)
case <-time.After(cfg.WaitRetryInterval):
t.Fatal("acquireTokens was not woken up promptly by Reconfigure signal")
}
}
func TestAcquireTokensFallbackToTimer(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
// Short retry interval so the test runs fast.
gc.mainCfg.WaitRetryInterval = 50 * time.Millisecond
gc.mainCfg.WaitRetryTimes = 3
gc.mainCfg.LTBMaxWaitDuration = 100 * time.Millisecond
// Set fillRate=0 and never reconfigure — no signal will arrive.
counter := gc.run.requestUnitTokens
counter.limiter.Reconfigure(time.Now(), tokenBucketReconfigureArgs{
newTokens: 1000,
newFillRate: 0,
newBurst: 0,
})
delta := &rmpb.Consumption{RRU: 5000}
ctx := context.Background()
var waitDuration time.Duration
_, err := gc.acquireTokens(ctx, delta, &waitDuration, false)
// Without a Reconfigure signal, all retries should exhaust and return an error.
re.Error(err)
re.True(errs.ErrClientResourceGroupThrottled.Equal(err))
// waitDuration should be roughly retryTimes * retryInterval.
re.GreaterOrEqual(waitDuration, gc.mainCfg.WaitRetryInterval*time.Duration(gc.mainCfg.WaitRetryTimes))
}