-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathvarsutil.go
492 lines (437 loc) · 13.3 KB
/
varsutil.go
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2016 PingCAP, Inc.
//
// 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 variable
import (
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/timeutil"
"github.com/tikv/client-go/v2/oracle"
)
// secondsPerYear represents seconds in a normal year. Leap year is not considered here.
const secondsPerYear = 60 * 60 * 24 * 365
// SetDDLReorgWorkerCounter sets ddlReorgWorkerCounter count.
// Max worker count is maxDDLReorgWorkerCount.
func SetDDLReorgWorkerCounter(cnt int32) {
if cnt > maxDDLReorgWorkerCount {
cnt = maxDDLReorgWorkerCount
}
atomic.StoreInt32(&ddlReorgWorkerCounter, cnt)
}
// GetDDLReorgWorkerCounter gets ddlReorgWorkerCounter.
func GetDDLReorgWorkerCounter() int32 {
return atomic.LoadInt32(&ddlReorgWorkerCounter)
}
// SetDDLReorgBatchSize sets ddlReorgBatchSize size.
// Max batch size is MaxDDLReorgBatchSize.
func SetDDLReorgBatchSize(cnt int32) {
if cnt > MaxDDLReorgBatchSize {
cnt = MaxDDLReorgBatchSize
}
if cnt < MinDDLReorgBatchSize {
cnt = MinDDLReorgBatchSize
}
atomic.StoreInt32(&ddlReorgBatchSize, cnt)
}
// GetDDLReorgBatchSize gets ddlReorgBatchSize.
func GetDDLReorgBatchSize() int32 {
return atomic.LoadInt32(&ddlReorgBatchSize)
}
// SetDDLErrorCountLimit sets ddlErrorCountlimit size.
func SetDDLErrorCountLimit(cnt int64) {
atomic.StoreInt64(&ddlErrorCountlimit, cnt)
}
// GetDDLErrorCountLimit gets ddlErrorCountlimit size.
func GetDDLErrorCountLimit() int64 {
return atomic.LoadInt64(&ddlErrorCountlimit)
}
// SetDDLReorgRowFormat sets ddlReorgRowFormat version.
func SetDDLReorgRowFormat(format int64) {
atomic.StoreInt64(&ddlReorgRowFormat, format)
}
// GetDDLReorgRowFormat gets ddlReorgRowFormat version.
func GetDDLReorgRowFormat() int64 {
return atomic.LoadInt64(&ddlReorgRowFormat)
}
// SetMaxDeltaSchemaCount sets maxDeltaSchemaCount size.
func SetMaxDeltaSchemaCount(cnt int64) {
atomic.StoreInt64(&maxDeltaSchemaCount, cnt)
}
// GetMaxDeltaSchemaCount gets maxDeltaSchemaCount size.
func GetMaxDeltaSchemaCount() int64 {
return atomic.LoadInt64(&maxDeltaSchemaCount)
}
// BoolToOnOff returns the string representation of a bool, i.e. "ON/OFF"
func BoolToOnOff(b bool) string {
if b {
return On
}
return Off
}
func int32ToBoolStr(i int32) string {
if i == 1 {
return On
}
return Off
}
func checkCollation(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
coll, err := collate.GetCollationByName(normalizedValue)
if err != nil {
return normalizedValue, errors.Trace(err)
}
return coll.Name, nil
}
func checkCharacterSet(normalizedValue string, argName string) (string, error) {
if normalizedValue == "" {
return normalizedValue, errors.Trace(ErrWrongValueForVar.GenWithStackByArgs(argName, "NULL"))
}
cs, err := charset.GetCharsetInfo(normalizedValue)
if err != nil {
return normalizedValue, errors.Trace(err)
}
return cs.Name, nil
}
// checkReadOnly requires TiDBEnableNoopFuncs=1 for the same scope otherwise an error will be returned.
func checkReadOnly(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag, offlineMode bool) (string, error) {
errMsg := ErrFunctionsNoopImpl.GenWithStackByArgs("READ ONLY")
if offlineMode {
errMsg = ErrFunctionsNoopImpl.GenWithStackByArgs("OFFLINE MODE")
}
if TiDBOptOn(normalizedValue) {
if scope == ScopeSession && vars.NoopFuncsMode != OnInt {
if vars.NoopFuncsMode == OffInt {
return Off, errMsg
}
vars.StmtCtx.AppendWarning(errMsg)
}
if scope == ScopeGlobal {
val, err := vars.GlobalVarsAccessor.GetGlobalSysVar(TiDBEnableNoopFuncs)
if err != nil {
return originalValue, errUnknownSystemVariable.GenWithStackByArgs(TiDBEnableNoopFuncs)
}
if val == Off {
return Off, errMsg
}
if val == Warn {
vars.StmtCtx.AppendWarning(errMsg)
}
}
}
return normalizedValue, nil
}
func checkIsolationLevel(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
if normalizedValue == "SERIALIZABLE" || normalizedValue == "READ-UNCOMMITTED" {
returnErr := ErrUnsupportedIsolationLevel.GenWithStackByArgs(normalizedValue)
if !TiDBOptOn(vars.systems[TiDBSkipIsolationLevelCheck]) {
return normalizedValue, ErrUnsupportedIsolationLevel.GenWithStackByArgs(normalizedValue)
}
vars.StmtCtx.AppendWarning(returnErr)
}
return normalizedValue, nil
}
// GetSessionOrGlobalSystemVar gets a system variable.
// If it is a session only variable, use the default value defined in code.
// Returns error if there is no such variable.
func GetSessionOrGlobalSystemVar(s *SessionVars, name string) (string, error) {
sv := GetSysVar(name)
if sv == nil {
return "", ErrUnknownSystemVar.GenWithStackByArgs(name)
}
if sv.HasNoneScope() {
return sv.Value, nil
}
if sv.HasSessionScope() {
// Populate the value to s.systems if it is not there already.
// in future should be already loaded on session init
if sv.GetSession != nil {
// shortcut to the getter, we won't use the value
return sv.GetSessionFromHook(s)
}
if _, ok := s.systems[sv.Name]; !ok {
if sv.HasGlobalScope() {
if val, err := s.GlobalVarsAccessor.GetGlobalSysVar(sv.Name); err == nil {
s.systems[sv.Name] = val
}
} else {
s.systems[sv.Name] = sv.Value // no global scope, use default
}
}
return sv.GetSessionFromHook(s)
}
return sv.GetGlobalFromHook(s)
}
// GetGlobalSystemVar gets a global system variable.
func GetGlobalSystemVar(s *SessionVars, name string) (string, error) {
sv := GetSysVar(name)
if sv == nil {
return "", ErrUnknownSystemVar.GenWithStackByArgs(name)
}
return sv.GetGlobalFromHook(s)
}
// SetSessionSystemVar sets system variable and updates SessionVars states.
func SetSessionSystemVar(vars *SessionVars, name string, value string) error {
sysVar := GetSysVar(name)
if sysVar == nil {
return ErrUnknownSystemVar.GenWithStackByArgs(name)
}
sVal, err := sysVar.Validate(vars, value, ScopeSession)
if err != nil {
return err
}
return vars.SetSystemVar(name, sVal)
}
// SetStmtVar sets system variable and updates SessionVars states.
func SetStmtVar(vars *SessionVars, name string, value string) error {
name = strings.ToLower(name)
sysVar := GetSysVar(name)
if sysVar == nil {
return ErrUnknownSystemVar.GenWithStackByArgs(name)
}
sVal, err := sysVar.Validate(vars, value, ScopeSession)
if err != nil {
return err
}
return vars.SetStmtVar(name, sVal)
}
func getTiDBTableValue(vars *SessionVars, name, defaultVal string) (string, error) {
val, err := vars.GlobalVarsAccessor.GetTiDBTableValue(name)
if err != nil { // handle empty result or other errors
return defaultVal, nil
}
return trueFalseToOnOff(val), nil
}
func setTiDBTableValue(vars *SessionVars, name, value, comment string) error {
value = onOffToTrueFalse(value)
return vars.GlobalVarsAccessor.SetTiDBTableValue(name, value, comment)
}
// In mysql.tidb the convention has been to store the string value "true"/"false",
// but sysvars use the convention ON/OFF.
func trueFalseToOnOff(str string) string {
if strings.EqualFold("true", str) {
return On
} else if strings.EqualFold("false", str) {
return Off
}
return str
}
// In mysql.tidb the convention has been to store the string value "true"/"false",
// but sysvars use the convention ON/OFF.
func onOffToTrueFalse(str string) string {
if strings.EqualFold("ON", str) {
return "true"
} else if strings.EqualFold("OFF", str) {
return "false"
}
return str
}
const (
// initChunkSizeUpperBound indicates upper bound value of tidb_init_chunk_size.
initChunkSizeUpperBound = 32
// maxChunkSizeLowerBound indicates lower bound value of tidb_max_chunk_size.
maxChunkSizeLowerBound = 32
)
// appendDeprecationWarning adds a warning that the item is deprecated.
func appendDeprecationWarning(s *SessionVars, name, replacement string) {
s.StmtCtx.AppendWarning(errWarnDeprecatedSyntax.FastGenByArgs(name, replacement))
}
// TiDBOptOn could be used for all tidb session variable options, we use "ON"/1 to turn on those options.
func TiDBOptOn(opt string) bool {
return strings.EqualFold(opt, "ON") || opt == "1"
}
const (
// OffInt is used by TiDBOptOnOffWarn
OffInt = 0
// OnInt is used TiDBOptOnOffWarn
OnInt = 1
// WarnInt is used by TiDBOptOnOffWarn
WarnInt = 2
)
// TiDBOptOnOffWarn converts On/Off/Warn to an int.
// It is used for MultiStmtMode and NoopFunctionsMode
func TiDBOptOnOffWarn(opt string) int {
switch opt {
case Warn:
return WarnInt
case On:
return OnInt
}
return OffInt
}
// ClusteredIndexDefMode controls the default clustered property for primary key.
type ClusteredIndexDefMode int
const (
// ClusteredIndexDefModeIntOnly indicates only single int primary key will default be clustered.
ClusteredIndexDefModeIntOnly ClusteredIndexDefMode = 0
// ClusteredIndexDefModeOn indicates primary key will default be clustered.
ClusteredIndexDefModeOn ClusteredIndexDefMode = 1
// ClusteredIndexDefModeOff indicates primary key will default be non-clustered.
ClusteredIndexDefModeOff ClusteredIndexDefMode = 2
)
// TiDBOptEnableClustered converts enable clustered options to ClusteredIndexDefMode.
func TiDBOptEnableClustered(opt string) ClusteredIndexDefMode {
switch opt {
case On:
return ClusteredIndexDefModeOn
case Off:
return ClusteredIndexDefModeOff
default:
return ClusteredIndexDefModeIntOnly
}
}
func tidbOptPositiveInt32(opt string, defaultVal int) int {
val, err := strconv.Atoi(opt)
if err != nil || val <= 0 {
return defaultVal
}
return val
}
func tidbOptInt(opt string, defaultVal int) int {
val, err := strconv.Atoi(opt)
if err != nil {
return defaultVal
}
return val
}
func tidbOptInt64(opt string, defaultVal int64) int64 {
val, err := strconv.ParseInt(opt, 10, 64)
if err != nil {
return defaultVal
}
return val
}
func tidbOptFloat64(opt string, defaultVal float64) float64 {
val, err := strconv.ParseFloat(opt, 64)
if err != nil {
return defaultVal
}
return val
}
func parseTimeZone(s string) (*time.Location, error) {
if strings.EqualFold(s, "SYSTEM") {
return timeutil.SystemLocation(), nil
}
loc, err := time.LoadLocation(s)
if err == nil {
return loc, nil
}
// The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'.
// The time zone's value should in [-12:59,+14:00].
if strings.HasPrefix(s, "+") || strings.HasPrefix(s, "-") {
d, err := types.ParseDuration(nil, s[1:], 0)
if err == nil {
if s[0] == '-' {
if d.Duration > 12*time.Hour+59*time.Minute {
return nil, ErrUnknownTimeZone.GenWithStackByArgs(s)
}
} else {
if d.Duration > 14*time.Hour {
return nil, ErrUnknownTimeZone.GenWithStackByArgs(s)
}
}
ofst := int(d.Duration / time.Second)
if s[0] == '-' {
ofst = -ofst
}
return time.FixedZone("", ofst), nil
}
}
return nil, ErrUnknownTimeZone.GenWithStackByArgs(s)
}
func setSnapshotTS(s *SessionVars, sVal string) error {
if sVal == "" {
s.SnapshotTS = 0
s.SnapshotInfoschema = nil
return nil
}
if tso, err := strconv.ParseUint(sVal, 10, 64); err == nil {
s.SnapshotTS = tso
return nil
}
t, err := types.ParseTime(s.StmtCtx, sVal, mysql.TypeTimestamp, types.MaxFsp)
if err != nil {
return err
}
t1, err := t.GoTime(s.Location())
s.SnapshotTS = oracle.GoTimeToTS(t1)
// tx_read_ts should be mutual exclusive with tidb_snapshot
s.TxnReadTS = NewTxnReadTS(0)
return err
}
func setTxnReadTS(s *SessionVars, sVal string) error {
if sVal == "" {
s.TxnReadTS = NewTxnReadTS(0)
return nil
}
t, err := types.ParseTime(s.StmtCtx, sVal, mysql.TypeTimestamp, types.MaxFsp)
if err != nil {
return err
}
t1, err := t.GoTime(s.Location())
if err != nil {
return err
}
s.TxnReadTS = NewTxnReadTS(oracle.GoTimeToTS(t1))
// tx_read_ts should be mutual exclusive with tidb_snapshot
s.SnapshotTS = 0
s.SnapshotInfoschema = nil
return err
}
func setReadStaleness(s *SessionVars, sVal string) error {
if sVal == "" || sVal == "0" {
s.ReadStaleness = 0
return nil
}
sValue, err := strconv.ParseInt(sVal, 10, 32)
if err != nil {
return err
}
if sValue > 0 {
return fmt.Errorf("%s's value should be less than 0", TiDBReadStaleness)
}
s.ReadStaleness = time.Duration(sValue) * time.Second
return nil
}
// serverGlobalVariable is used to handle variables that acts in server and global scope.
type serverGlobalVariable struct {
sync.Mutex
serverVal string
globalVal string
}
// Set sets the value according to variable scope.
func (v *serverGlobalVariable) Set(val string, isServer bool) {
v.Lock()
if isServer {
v.serverVal = val
} else {
v.globalVal = val
}
v.Unlock()
}
// GetVal gets the value.
func (v *serverGlobalVariable) GetVal() string {
v.Lock()
defer v.Unlock()
if v.serverVal != "" {
return v.serverVal
}
return v.globalVal
}