forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconfig_test.go
More file actions
321 lines (275 loc) · 10.5 KB
/
config_test.go
File metadata and controls
321 lines (275 loc) · 10.5 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
318
319
320
321
/*
Copyright 2019 The Vitess 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 endtoend
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/endtoend/framework"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
func TestPoolSize(t *testing.T) {
revert := changeVar(t, "PoolSize", "1")
defer revert()
vstart := framework.DebugVars()
verifyIntValue(t, vstart, "ConnPoolCapacity", 1)
var wg sync.WaitGroup
wg.Add(2)
go func() {
framework.NewClient().Execute("select sleep(0.5) from dual", nil)
wg.Done()
}()
// The queries have to be different so consolidator doesn't kick in.
go func() {
framework.NewClient().Execute("select sleep(0.49) from dual", nil)
wg.Done()
}()
wg.Wait()
// Parallel plan building can cause multiple conn pool waits.
// Check that the wait count was at least incremented once so
// we know it's working.
tag := "ConnPoolWaitCount"
got := framework.FetchInt(framework.DebugVars(), tag)
want := framework.FetchInt(vstart, tag)
assert.LessOrEqual(t, want, got)
}
func TestStreamPoolSize(t *testing.T) {
revert := changeVar(t, "StreamPoolSize", "1")
defer revert()
vstart := framework.DebugVars()
verifyIntValue(t, vstart, "StreamConnPoolCapacity", 1)
}
func TestDisableConsolidator(t *testing.T) {
totalConsolidationsTag := "Waits/Histograms/Consolidations/Count"
initial := framework.FetchInt(framework.DebugVars(), totalConsolidationsTag)
var wg sync.WaitGroup
wg.Add(2)
go func() {
framework.NewClient().Execute("select sleep(0.5) from dual", nil)
wg.Done()
}()
go func() {
framework.NewClient().Execute("select sleep(0.5) from dual", nil)
wg.Done()
}()
wg.Wait()
afterOne := framework.FetchInt(framework.DebugVars(), totalConsolidationsTag)
assert.Equal(t, initial+1, afterOne, "expected one consolidation")
revert := changeVar(t, "Consolidator", tabletenv.Disable)
defer revert()
var wg2 sync.WaitGroup
wg2.Add(2)
go func() {
framework.NewClient().Execute("select sleep(0.5) from dual", nil)
wg2.Done()
}()
go func() {
framework.NewClient().Execute("select sleep(0.5) from dual", nil)
wg2.Done()
}()
wg2.Wait()
noNewConsolidations := framework.FetchInt(framework.DebugVars(), totalConsolidationsTag)
assert.Equal(t, afterOne, noNewConsolidations, "expected no new consolidations")
}
func TestConsolidatorReplicasOnly(t *testing.T) {
type executeFn func(
query string, bindvars map[string]*querypb.BindVariable,
) (*sqltypes.Result, error)
testCases := []struct {
name string
getExecuteFn func(qc *framework.QueryClient) executeFn
totalConsolidationsTag string
}{
{
name: "Execute",
getExecuteFn: func(qc *framework.QueryClient) executeFn { return qc.Execute },
totalConsolidationsTag: "Waits/Histograms/Consolidations/Count",
},
{
name: "StreamExecute",
getExecuteFn: func(qc *framework.QueryClient) executeFn { return qc.StreamExecute },
totalConsolidationsTag: "Waits/Histograms/StreamConsolidations/Count",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
initial := framework.FetchInt(framework.DebugVars(), testCase.totalConsolidationsTag)
var wg sync.WaitGroup
wg.Add(2)
go func() {
testCase.getExecuteFn(framework.NewClient())("select sleep(0.5) from dual", nil)
wg.Done()
}()
go func() {
testCase.getExecuteFn(framework.NewClient())("select sleep(0.5) from dual", nil)
wg.Done()
}()
wg.Wait()
afterOne := framework.FetchInt(framework.DebugVars(), testCase.totalConsolidationsTag)
assert.Equal(t, initial+1, afterOne, "expected one consolidation")
revert := changeVar(t, "Consolidator", tabletenv.NotOnPrimary)
defer revert()
// primary should not do query consolidation
var wg2 sync.WaitGroup
wg2.Add(2)
go func() {
testCase.getExecuteFn(framework.NewClient())("select sleep(0.5) from dual", nil)
wg2.Done()
}()
go func() {
testCase.getExecuteFn(framework.NewClient())("select sleep(0.5) from dual", nil)
wg2.Done()
}()
wg2.Wait()
noNewConsolidations := framework.FetchInt(framework.DebugVars(), testCase.totalConsolidationsTag)
assert.Equal(t, afterOne, noNewConsolidations, "expected no new consolidations")
// become a replica, where query consolidation should happen
client := framework.NewClientWithTabletType(topodatapb.TabletType_REPLICA)
err := client.SetServingType(topodatapb.TabletType_REPLICA)
require.NoError(t, err)
defer func() {
err = client.SetServingType(topodatapb.TabletType_PRIMARY)
require.NoError(t, err)
}()
initial = framework.FetchInt(framework.DebugVars(), testCase.totalConsolidationsTag)
var wg3 sync.WaitGroup
wg3.Add(2)
go func() {
testCase.getExecuteFn(client)("select sleep(0.5) from dual", nil)
wg3.Done()
}()
go func() {
testCase.getExecuteFn(client)("select sleep(0.5) from dual", nil)
wg3.Done()
}()
wg3.Wait()
afterOne = framework.FetchInt(framework.DebugVars(), testCase.totalConsolidationsTag)
assert.Equal(t, initial+1, afterOne, "expected another consolidation")
})
}
}
func TestQueryPlanCacheSize(t *testing.T) {
var cachedPlanSize = int((&tabletserver.TabletPlan{}).CachedSize(true))
// sleep to avoid race between SchemaChanged event clearing out the plans cache which breaks this test
framework.Server.WaitForSchemaReset(2 * time.Second)
bindVars := map[string]*querypb.BindVariable{
"ival1": sqltypes.Int64BindVariable(1),
"ival2": sqltypes.Int64BindVariable(1),
}
framework.Server.ClearQueryPlanCache()
client := framework.NewClient()
_, _ = client.Execute("select * from vitess_test where intval=:ival1", bindVars)
_, _ = client.Execute("select * from vitess_test where intval=:ival1", bindVars)
assert.Equal(t, 1, framework.Server.QueryPlanCacheLen())
vend := framework.DebugVars()
assert.GreaterOrEqual(t, framework.FetchInt(vend, "QueryPlanCacheSize"), cachedPlanSize)
_, _ = client.Execute("select * from vitess_test where intval=:ival2", bindVars)
require.Equal(t, 2, framework.Server.QueryPlanCacheLen())
vend = framework.DebugVars()
assert.GreaterOrEqual(t, framework.FetchInt(vend, "QueryPlanCacheSize"), 2*cachedPlanSize)
_, _ = client.Execute("select * from vitess_test where intval=1", bindVars)
require.Equal(t, 3, framework.Server.QueryPlanCacheLen())
vend = framework.DebugVars()
assert.GreaterOrEqual(t, framework.FetchInt(vend, "QueryPlanCacheSize"), 3*cachedPlanSize)
}
func TestMaxResultSize(t *testing.T) {
revert := changeVar(t, "MaxResultSize", "2")
defer revert()
client := framework.NewClient()
query := "select * from vitess_test"
_, err := client.Execute(query, nil)
assert.Error(t, err)
want := "Row count exceeded"
assert.Contains(t, err.Error(), want, "Error: %v, must start with %s", err, want)
verifyIntValue(t, framework.DebugVars(), "MaxResultSize", 2)
framework.Server.SetMaxResultSize(10)
_, err = client.Execute(query, nil)
require.NoError(t, err)
}
func TestWarnResultSize(t *testing.T) {
revert := changeVar(t, "WarnResultSize", "2")
defer revert()
client := framework.NewClient()
originalWarningsResultsExceededCount := framework.FetchInt(framework.DebugVars(), "Warnings/ResultsExceeded")
query := "select * from vitess_test"
_, _ = client.Execute(query, nil)
newWarningsResultsExceededCount := framework.FetchInt(framework.DebugVars(), "Warnings/ResultsExceeded")
exceededCountDiff := newWarningsResultsExceededCount - originalWarningsResultsExceededCount
assert.Equal(t, 1, exceededCountDiff, "Warnings.ResultsExceeded counter should have increased by 1")
verifyIntValue(t, framework.DebugVars(), "WarnResultSize", 2)
framework.Server.SetWarnResultSize(10)
_, _ = client.Execute(query, nil)
newerWarningsResultsExceededCount := framework.FetchInt(framework.DebugVars(), "Warnings/ResultsExceeded")
exceededCountDiff = newerWarningsResultsExceededCount - newWarningsResultsExceededCount
assert.Equal(t, 0, exceededCountDiff, "Warnings.ResultsExceeded counter should not have increased")
}
func TestQueryTimeout(t *testing.T) {
vstart := framework.DebugVars()
defer framework.Server.QueryTimeout.Store(framework.Server.QueryTimeout.Load())
framework.Server.QueryTimeout.Store(100 * time.Millisecond.Nanoseconds())
client := framework.NewClient()
err := client.Begin(false)
require.NoError(t, err)
_, err = client.Execute("select sleep(1) from vitess_test", nil)
assert.Equal(t, vtrpcpb.Code_CANCELED, vterrors.Code(err))
_, err = client.Execute("select 1 from dual", nil)
assert.Equal(t, vtrpcpb.Code_ABORTED, vterrors.Code(err))
vend := framework.DebugVars()
verifyIntValue(t, vend, "QueryTimeout", int(100*time.Millisecond))
compareIntDiff(t, vend, "Kills/Connections", vstart, 1)
}
func changeVar(t *testing.T, name, value string) (revert func()) {
t.Helper()
vals := framework.FetchJSON("/debug/env?format=json")
initial, ok := vals[name]
if !ok {
t.Fatalf("%s not found in: %v", name, vals)
}
vals = framework.PostJSON("/debug/env?format=json", map[string]string{
"varname": name,
"value": value,
})
verifyMapValue(t, vals, name, value)
return func() {
vals = framework.PostJSON("/debug/env?format=json", map[string]string{
"varname": name,
"value": fmt.Sprintf("%v", initial),
})
verifyMapValue(t, vals, name, initial)
}
}
func verifyMapValue(t *testing.T, values map[string]any, tag string, want any) {
t.Helper()
val, ok := values[tag]
if !ok {
t.Fatalf("%s not found in: %v", tag, values)
}
assert.Equal(t, want, val)
}
func compareIntDiff(t *testing.T, end map[string]any, tag string, start map[string]any, diff int) {
t.Helper()
verifyIntValue(t, end, tag, framework.FetchInt(start, tag)+diff)
}
func verifyIntValue(t *testing.T, values map[string]any, tag string, want int) {
t.Helper()
require.Equal(t, want, framework.FetchInt(values, tag), tag)
}