-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
throttler.go
311 lines (266 loc) · 10.9 KB
/
throttler.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
/*
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 vtctl
import (
"flag"
"fmt"
"strconv"
"strings"
"time"
"google.golang.org/protobuf/encoding/prototext"
"context"
"github.com/olekukonko/tablewriter"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/throttler"
"vitess.io/vitess/go/vt/throttler/throttlerclient"
"vitess.io/vitess/go/vt/wrangler"
throttlerdatapb "vitess.io/vitess/go/vt/proto/throttlerdata"
)
const (
throttlerGroupName = "Resharding Throttler"
shortTimeout = 15 * time.Second
)
// This file contains the commands to control the throttler which is used during
// resharding (vtworker) and by filtered replication (vttablet).
func init() {
addCommandGroup(throttlerGroupName)
addCommand(throttlerGroupName, command{
name: "ThrottlerMaxRates",
method: commandThrottlerMaxRates,
params: "-server <vtworker or vttablet>",
help: "Returns the current max rate of all active resharding throttlers on the server.",
})
addCommand(throttlerGroupName, command{
name: "ThrottlerSetMaxRate",
method: commandThrottlerSetMaxRate,
params: "-server <vtworker or vttablet> <rate>",
help: "Sets the max rate for all active resharding throttlers on the server.",
})
addCommand(throttlerGroupName, command{
name: "GetThrottlerConfiguration",
method: commandGetThrottlerConfiguration,
params: "-server <vtworker or vttablet> [<throttler name>]",
help: "Returns the current configuration of the MaxReplicationLag module. If no throttler name is specified, the configuration of all throttlers will be returned.",
})
addCommand(throttlerGroupName, command{
name: "UpdateThrottlerConfiguration",
method: commandUpdateThrottlerConfiguration,
// Note: <configuration protobuf text> is put in quotes to tell the user
// that the value must be quoted such that it's one argument only.
params: `-server <vtworker or vttablet> [-copy_zero_values] "<configuration protobuf text>" [<throttler name>]`,
help: "Updates the configuration of the MaxReplicationLag module. The configuration must be specified as protobuf text. If a field is omitted or has a zero value, it will be ignored unless -copy_zero_values is specified. If no throttler name is specified, all throttlers will be updated.",
})
addCommand(throttlerGroupName, command{
name: "ResetThrottlerConfiguration",
method: commandResetThrottlerConfiguration,
params: "-server <vtworker or vttablet> [<throttler name>]",
help: "Resets the current configuration of the MaxReplicationLag module. If no throttler name is specified, the configuration of all throttlers will be reset.",
})
}
func commandThrottlerMaxRates(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
server := subFlags.String("server", "", "vtworker or vttablet to connect to")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 0 {
return fmt.Errorf("the ThrottlerSetMaxRate command does not accept any positional parameters")
}
// Connect to the server.
ctx, cancel := context.WithTimeout(ctx, shortTimeout)
defer cancel()
client, err := throttlerclient.New(*server)
if err != nil {
return fmt.Errorf("error creating a throttler client for server '%v': %v", *server, err)
}
defer client.Close()
rates, err := client.MaxRates(ctx)
if err != nil {
return fmt.Errorf("failed to get the throttler rate from server '%v': %v", *server, err)
}
if len(rates) == 0 {
wr.Logger().Printf("There are no active throttlers on server '%v'.\n", *server)
return nil
}
table := tablewriter.NewWriter(loggerWriter{wr.Logger()})
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Name", "Rate"})
for name, rate := range rates {
rateText := strconv.FormatInt(rate, 10)
if rate == throttler.MaxRateModuleDisabled {
rateText = "unlimited"
}
table.Append([]string{name, rateText})
}
table.Render()
wr.Logger().Printf("%d active throttler(s) on server '%v'.\n", len(rates), *server)
return nil
}
func commandThrottlerSetMaxRate(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
server := subFlags.String("server", "", "vtworker or vttablet to connect to")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 1 {
return fmt.Errorf("the <rate> argument is required for the ThrottlerSetMaxRate command")
}
var rate int64
if strings.ToLower(subFlags.Arg(0)) == "unlimited" {
rate = throttler.MaxRateModuleDisabled
} else {
var err error
rate, err = strconv.ParseInt(subFlags.Arg(0), 0, 64)
if err != nil {
return fmt.Errorf("failed to parse rate '%v' as integer value: %v", subFlags.Arg(0), err)
}
}
// Connect to the server.
ctx, cancel := context.WithTimeout(ctx, shortTimeout)
defer cancel()
client, err := throttlerclient.New(*server)
if err != nil {
return fmt.Errorf("error creating a throttler client for server '%v': %v", *server, err)
}
defer client.Close()
names, err := client.SetMaxRate(ctx, rate)
if err != nil {
return fmt.Errorf("failed to set the throttler rate on server '%v': %v", *server, err)
}
if len(names) == 0 {
wr.Logger().Printf("ThrottlerSetMaxRate did nothing because server '%v' has no active throttlers.\n", *server)
return nil
}
printUpdatedThrottlers(wr.Logger(), *server, names)
return nil
}
func commandGetThrottlerConfiguration(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
server := subFlags.String("server", "", "vtworker or vttablet to connect to")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() > 1 {
return fmt.Errorf("the GetThrottlerConfiguration command accepts only <throttler name> as optional positional parameter")
}
var throttlerName string
if subFlags.NArg() == 1 {
throttlerName = subFlags.Arg(0)
}
// Connect to the server.
ctx, cancel := context.WithTimeout(ctx, shortTimeout)
defer cancel()
client, err := throttlerclient.New(*server)
if err != nil {
return fmt.Errorf("error creating a throttler client for server '%v': %v", *server, err)
}
defer client.Close()
configurations, err := client.GetConfiguration(ctx, throttlerName)
if err != nil {
return fmt.Errorf("failed to get the throttler configuration from server '%v': %v", *server, err)
}
if len(configurations) == 0 {
wr.Logger().Printf("There are no active throttlers on server '%v'.\n", *server)
return nil
}
table := tablewriter.NewWriter(loggerWriter{wr.Logger()})
table.SetAutoFormatHeaders(false)
// The full protobuf text will span more than one terminal line. Do not wrap
// it to make it easy to copy and paste it.
table.SetAutoWrapText(false)
table.SetHeader([]string{"Name", "Configuration (protobuf text, fields with a zero value are omitted)"})
for name, c := range configurations {
pcfg, _ := prototext.Marshal(c)
table.Append([]string{name, string(pcfg)})
}
table.Render()
wr.Logger().Printf("%d active throttler(s) on server '%v'.\n", len(configurations), *server)
return nil
}
func commandUpdateThrottlerConfiguration(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
server := subFlags.String("server", "", "vtworker or vttablet to connect to")
copyZeroValues := subFlags.Bool("copy_zero_values", false, "If true, fields with zero values will be copied as well")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() > 2 {
return fmt.Errorf(`the "<configuration protobuf text>" argument is required for the UpdateThrottlerConfiguration command. The <throttler name> is an optional positional parameter`)
}
var throttlerName string
if subFlags.NArg() == 2 {
throttlerName = subFlags.Arg(1)
}
protoText := subFlags.Arg(0)
configuration := &throttlerdatapb.Configuration{}
if err := prototext.Unmarshal([]byte(protoText), configuration); err != nil {
return fmt.Errorf("failed to unmarshal the configuration protobuf text (%v) into a protobuf instance: %v", protoText, err)
}
// Connect to the server.
ctx, cancel := context.WithTimeout(ctx, shortTimeout)
defer cancel()
client, err := throttlerclient.New(*server)
if err != nil {
return fmt.Errorf("error creating a throttler client for server '%v': %v", *server, err)
}
defer client.Close()
names, err := client.UpdateConfiguration(ctx, throttlerName, configuration, *copyZeroValues)
if err != nil {
return fmt.Errorf("failed to update the throttler configuration on server '%v': %v", *server, err)
}
if len(names) == 0 {
wr.Logger().Printf("UpdateThrottlerConfiguration did nothing because server '%v' has no active throttlers.\n", *server)
return nil
}
printUpdatedThrottlers(wr.Logger(), *server, names)
wr.Logger().Printf("The new configuration will become effective with the next recalculation event.\n")
return nil
}
func commandResetThrottlerConfiguration(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
server := subFlags.String("server", "", "vtworker or vttablet to connect to")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() > 1 {
return fmt.Errorf("the ResetThrottlerConfiguration command accepts only <throttler name> as optional positional parameter")
}
var throttlerName string
if subFlags.NArg() == 1 {
throttlerName = subFlags.Arg(0)
}
// Connect to the server.
ctx, cancel := context.WithTimeout(ctx, shortTimeout)
defer cancel()
client, err := throttlerclient.New(*server)
if err != nil {
return fmt.Errorf("error creating a throttler client for server '%v': %v", *server, err)
}
defer client.Close()
names, err := client.ResetConfiguration(ctx, throttlerName)
if err != nil {
return fmt.Errorf("failed to get the throttler configuration from server '%v': %v", *server, err)
}
if len(names) == 0 {
wr.Logger().Printf("ResetThrottlerConfiguration did nothing because server '%v' has no active throttlers.\n", *server)
return nil
}
printUpdatedThrottlers(wr.Logger(), *server, names)
wr.Logger().Printf("The reset initial configuration will become effective with the next recalculation event.\n")
return nil
}
func printUpdatedThrottlers(logger logutil.Logger, server string, names []string) {
table := tablewriter.NewWriter(loggerWriter{logger})
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Name"})
for _, name := range names {
table.Append([]string{name})
}
table.Render()
logger.Printf("%d active throttler(s) on server '%v' were updated.\n", len(names), server)
}