-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
topocustomrule.go
216 lines (176 loc) · 5.17 KB
/
topocustomrule.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
/*
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 topocustomrule implements a topo service backed listener for query rules.
One usage is to allow fast propagation of table denylists.
*/
package topocustomrule
import (
"context"
"fmt"
"reflect"
"sync"
"time"
"github.com/spf13/pflag"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tabletserver/rules"
)
var (
// Commandline flag to specify rule cell and path.
ruleCell = "global"
rulePath string
)
func registerFlags(fs *pflag.FlagSet) {
fs.StringVar(&ruleCell, "topocustomrule_cell", ruleCell, "topo cell for customrules file.")
fs.StringVar(&rulePath, "topocustomrule_path", rulePath, "path for customrules file. Disabled if empty.")
}
func init() {
servenv.OnParseFor("vttablet", registerFlags)
}
// topoCustomRuleSource is topo based custom rule source name
const topoCustomRuleSource string = "TOPO_CUSTOM_RULE"
// sleepDuringTopoFailure is how long to sleep before retrying in case of error.
// (it's a var not a const so the test can change the value).
var sleepDuringTopoFailure = 30 * time.Second
// topoCustomRule is the topo backed implementation.
type topoCustomRule struct {
// qsc is set at construction time.
qsc tabletserver.Controller
// conn is the topo connection. Set at construction time.
conn topo.Conn
// filePath is the file to read from.
filePath string
// qrs is the current rule set that we read.
qrs *rules.Rules
// mu protects the following variables.
mu sync.Mutex
// cancel is the function to call to cancel the current watch, if any.
cancel func()
// stopped is set when stop() is called. It is a protection for race conditions.
stopped bool
}
func newTopoCustomRule(qsc tabletserver.Controller, cell, filePath string) (*topoCustomRule, error) {
conn, err := qsc.TopoServer().ConnForCell(context.Background(), cell)
if err != nil {
return nil, err
}
return &topoCustomRule{
qsc: qsc,
conn: conn,
filePath: filePath,
}, nil
}
func (cr *topoCustomRule) start() {
go func() {
for {
if err := cr.oneWatch(); err != nil {
log.Warningf("Background watch of topo custom rule failed: %v", err)
}
cr.mu.Lock()
stopped := cr.stopped
cr.mu.Unlock()
if stopped {
log.Warningf("Topo custom rule was terminated")
return
}
log.Warningf("Sleeping for %v before trying again", sleepDuringTopoFailure)
time.Sleep(sleepDuringTopoFailure)
}
}()
}
func (cr *topoCustomRule) stop() {
cr.mu.Lock()
if cr.cancel != nil {
cr.cancel()
}
cr.stopped = true
cr.mu.Unlock()
}
func (cr *topoCustomRule) apply(wd *topo.WatchData) error {
qrs := rules.New()
if err := qrs.UnmarshalJSON(wd.Contents); err != nil {
return fmt.Errorf("error unmarshaling query rules: %v, original data '%s' version %v", err, wd.Contents, wd.Version)
}
if !reflect.DeepEqual(cr.qrs, qrs) {
cr.qrs = qrs.Copy()
cr.qsc.SetQueryRules(topoCustomRuleSource, qrs)
log.Infof("Custom rule version %v fetched from topo and applied to vttablet", wd.Version)
}
return nil
}
func (cr *topoCustomRule) oneWatch() error {
defer func() {
// Whatever happens, cancel() won't be valid after this function exits.
cr.mu.Lock()
cr.cancel = nil
cr.mu.Unlock()
}()
ctx, cancel := context.WithCancel(context.Background())
current, wdChannel, err := cr.conn.Watch(ctx, cr.filePath)
if err != nil {
cancel()
return err
}
cr.mu.Lock()
if cr.stopped {
// We're not interested in the result any more.
cr.mu.Unlock()
cancel()
for range wdChannel {
}
return topo.NewError(topo.Interrupted, "watch")
}
cr.cancel = cancel
cr.mu.Unlock()
if err := cr.apply(current); err != nil {
// Cancel the watch, drain channel.
cancel()
for range wdChannel {
}
return err
}
for wd := range wdChannel {
if wd.Err != nil {
// Last error value, we're done.
// wdChannel will be closed right after
// this, no need to do anything.
return wd.Err
}
if err := cr.apply(wd); err != nil {
// Cancel the watch, drain channel.
cancel()
for range wdChannel {
}
return err
}
}
return fmt.Errorf("watch terminated with no error")
}
// activateTopoCustomRules activates topo dynamic custom rule mechanism.
func activateTopoCustomRules(qsc tabletserver.Controller) {
if rulePath != "" {
qsc.RegisterQueryRuleSource(topoCustomRuleSource)
cr, err := newTopoCustomRule(qsc, ruleCell, rulePath)
if err != nil {
log.Fatalf("cannot start TopoCustomRule: %v", err)
}
cr.start()
servenv.OnTerm(cr.stop)
}
}
func init() {
tabletserver.RegisterFunctions = append(tabletserver.RegisterFunctions, activateTopoCustomRules)
}