-
Notifications
You must be signed in to change notification settings - Fork 712
/
option.go
128 lines (110 loc) · 4.09 KB
/
option.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
// Copyright 2021 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 pd
import (
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
)
const (
defaultPDTimeout = 3 * time.Second
maxInitClusterRetries = 100
defaultMaxTSOBatchWaitInterval time.Duration = 0
defaultEnableTSOFollowerProxy = false
defaultEnableFollowerHandle = false
)
// DynamicOption is used to distinguish the dynamic option type.
type DynamicOption int
const (
// MaxTSOBatchWaitInterval is the max TSO batch wait interval option.
// It is stored as time.Duration and should be between 0 and 10ms.
MaxTSOBatchWaitInterval DynamicOption = iota
// EnableTSOFollowerProxy is the TSO Follower Proxy option.
// It is stored as bool.
EnableTSOFollowerProxy
// EnableFollowerHandle is the follower handle option.
EnableFollowerHandle
dynamicOptionCount
)
// option is the configurable option for the PD client.
// It provides the ability to change some PD client's options online from the outside.
type option struct {
// Static options.
gRPCDialOptions []grpc.DialOption
timeout time.Duration
maxRetryTimes int
enableForwarding bool
metricsLabels prometheus.Labels
initMetrics bool
// Dynamic options.
dynamicOptions [dynamicOptionCount]atomic.Value
enableTSOFollowerProxyCh chan struct{}
}
// newOption creates a new PD client option with the default values set.
func newOption() *option {
co := &option{
timeout: defaultPDTimeout,
maxRetryTimes: maxInitClusterRetries,
enableTSOFollowerProxyCh: make(chan struct{}, 1),
initMetrics: true,
}
co.dynamicOptions[MaxTSOBatchWaitInterval].Store(defaultMaxTSOBatchWaitInterval)
co.dynamicOptions[EnableTSOFollowerProxy].Store(defaultEnableTSOFollowerProxy)
co.dynamicOptions[EnableFollowerHandle].Store(defaultEnableFollowerHandle)
return co
}
// setMaxTSOBatchWaitInterval sets the max TSO batch wait interval option.
// It only accepts the interval value between 0 and 10ms.
func (o *option) setMaxTSOBatchWaitInterval(interval time.Duration) error {
if interval < 0 || interval > 10*time.Millisecond {
return errors.New("[pd] invalid max TSO batch wait interval, should be between 0 and 10ms")
}
old := o.getMaxTSOBatchWaitInterval()
if interval != old {
o.dynamicOptions[MaxTSOBatchWaitInterval].Store(interval)
}
return nil
}
// setEnableFollowerHandle set the Follower Handle option.
func (o *option) setEnableFollowerHandle(enable bool) {
old := o.getEnableFollowerHandle()
if enable != old {
o.dynamicOptions[EnableFollowerHandle].Store(enable)
}
}
// getMaxTSOBatchWaitInterval gets the Follower Handle enable option.
func (o *option) getEnableFollowerHandle() bool {
return o.dynamicOptions[EnableFollowerHandle].Load().(bool)
}
// getMaxTSOBatchWaitInterval gets the max TSO batch wait interval option.
func (o *option) getMaxTSOBatchWaitInterval() time.Duration {
return o.dynamicOptions[MaxTSOBatchWaitInterval].Load().(time.Duration)
}
// setEnableTSOFollowerProxy sets the TSO Follower Proxy option.
func (o *option) setEnableTSOFollowerProxy(enable bool) {
old := o.getEnableTSOFollowerProxy()
if enable != old {
o.dynamicOptions[EnableTSOFollowerProxy].Store(enable)
select {
case o.enableTSOFollowerProxyCh <- struct{}{}:
default:
}
}
}
// getEnableTSOFollowerProxy gets the TSO Follower Proxy option.
func (o *option) getEnableTSOFollowerProxy() bool {
return o.dynamicOptions[EnableTSOFollowerProxy].Load().(bool)
}