forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 2
/
split_clone_cmd.go
289 lines (267 loc) · 13.3 KB
/
split_clone_cmd.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
/*
Copyright 2017 Google 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 worker
import (
"flag"
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"sync"
"github.com/xsec-lab/go/vt/vterrors"
"github.com/xsec-lab/go/vt/concurrency"
"github.com/xsec-lab/go/vt/topo/topoproto"
"github.com/xsec-lab/go/vt/topotools"
"github.com/xsec-lab/go/vt/wrangler"
"golang.org/x/net/context"
)
const splitCloneHTML = `
<!DOCTYPE html>
<head>
<title>Split Clone Action</title>
</head>
<body>
<h1>Split Clone Action</h1>
{{if .Error}}
<b>Error:</b> {{.Error}}</br>
{{else}}
<p>Choose the destination keyspace for this action.</p>
<ul>
{{range $i, $si := .Choices}}
<li><a href="/Clones/SplitClone?keyspace={{$si.Keyspace}}&shard={{$si.Shard}}">{{$si.Keyspace}}/{{$si.Shard}}</a></li>
{{end}}
</ul>
{{end}}
</body>
`
const splitCloneHTML2 = `
<!DOCTYPE html>
<head>
<title>Split Clone Action</title>
</head>
<body>
<p>Shard involved: {{.Keyspace}}/{{.Shard}}</p>
<h1>Split Clone Action</h1>
<form action="/Clones/SplitClone" method="post">
<LABEL for="online">Do Online Copy: (optional approximate copy, source and destination tablets will not be put out of serving, minimizes downtime during offline copy)</LABEL>
<INPUT type="checkbox" id="online" name="online" value="true"{{if .DefaultOnline}} checked{{end}}></BR>
<LABEL for="offline">Do Offline Copy: (exact copy at a specific GTID, required before shard migration, source and destination tablets will be put out of serving during copy)</LABEL>
<INPUT type="checkbox" id="offline" name="offline" value="true"{{if .DefaultOnline}} checked{{end}}></BR>
<LABEL for="excludeTables">Exclude Tables: </LABEL>
<INPUT type="text" id="excludeTables" name="excludeTables" value="/ignored/"></BR>
<LABEL for="chunkCount">Chunk Count: </LABEL>
<INPUT type="text" id="chunkCount" name="chunkCount" value="{{.DefaultChunkCount}}"></BR>
<LABEL for="minRowsPerChunk">Minimun Number of Rows per Chunk (may reduce the Chunk Count): </LABEL>
<INPUT type="text" id="minRowsPerChunk" name="minRowsPerChunk" value="{{.DefaultMinRowsPerChunk}}"></BR>
<LABEL for="sourceReaderCount">Source Reader Count: </LABEL>
<INPUT type="text" id="sourceReaderCount" name="sourceReaderCount" value="{{.DefaultSourceReaderCount}}"></BR>
<LABEL for="writeQueryMaxRows">Maximum Number of Rows per Write Query: </LABEL>
<INPUT type="text" id="writeQueryMaxRows" name="writeQueryMaxRows" value="{{.DefaultWriteQueryMaxRows}}"></BR>
<LABEL for="writeQueryMaxSize">Maximum Size (in bytes) per Write Query: </LABEL>
<INPUT type="text" id="writeQueryMaxSize" name="writeQueryMaxSize" value="{{.DefaultWriteQueryMaxSize}}"></BR>
<LABEL for="destinationWriterCount">Destination Writer Count: </LABEL>
<INPUT type="text" id="destinationWriterCount" name="destinationWriterCount" value="{{.DefaultDestinationWriterCount}}"></BR>
<LABEL for="minHealthyRdonlyTablets">Minimum Number of required healthy RDONLY tablets in the source and destination shard at start: </LABEL>
<INPUT type="text" id="minHealthyRdonlyTablets" name="minHealthyRdonlyTablets" value="{{.DefaultMinHealthyRdonlyTablets}}"></BR>
<LABEL for="maxTPS">Maximum Write Transactions/second (If non-zero, writes on the destination will be throttled. Unlimited by default.): </LABEL>
<INPUT type="text" id="maxTPS" name="maxTPS" value="{{.DefaultMaxTPS}}"></BR>
<LABEL for="maxReplicationLag">Maximum Replication Lag Seconds (enables the adapative throttler. Disabled by default.): </LABEL>
<INPUT type="text" id="maxReplicationLag" name="maxReplicationLag" value="{{.DefaultMaxReplicationLag}}"></BR>
<INPUT type="hidden" name="keyspace" value="{{.Keyspace}}"/>
<INPUT type="hidden" name="shard" value="{{.Shard}}"/>
<INPUT type="submit" value="Clone"/>
</form>
</body>
`
var splitCloneTemplate = mustParseTemplate("splitClone", splitCloneHTML)
var splitCloneTemplate2 = mustParseTemplate("splitClone2", splitCloneHTML2)
func commandSplitClone(wi *Instance, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) (Worker, error) {
online := subFlags.Bool("online", defaultOnline, "do online copy (optional approximate copy, source and destination tablets will not be put out of serving, minimizes downtime during offline copy)")
offline := subFlags.Bool("offline", defaultOffline, "do offline copy (exact copy at a specific GTID, required before shard migration, source and destination tablets will be put out of serving during copy)")
excludeTables := subFlags.String("exclude_tables", "", "comma separated list of tables to exclude. Each is either an exact match, or a regular expression of the form /regexp/")
chunkCount := subFlags.Int("chunk_count", defaultChunkCount, "number of chunks per table")
minRowsPerChunk := subFlags.Int("min_rows_per_chunk", defaultMinRowsPerChunk, "minimum number of rows per chunk (may reduce --chunk_count)")
sourceReaderCount := subFlags.Int("source_reader_count", defaultSourceReaderCount, "number of concurrent streaming queries to use on the source")
writeQueryMaxRows := subFlags.Int("write_query_max_rows", defaultWriteQueryMaxRows, "maximum number of rows per write query")
writeQueryMaxSize := subFlags.Int("write_query_max_size", defaultWriteQueryMaxSize, "maximum size (in bytes) per write query")
destinationWriterCount := subFlags.Int("destination_writer_count", defaultDestinationWriterCount, "number of concurrent RPCs to execute on the destination")
minHealthyRdonlyTablets := subFlags.Int("min_healthy_rdonly_tablets", defaultMinHealthyRdonlyTablets, "minimum number of healthy RDONLY tablets in the source and destination shard at start")
maxTPS := subFlags.Int64("max_tps", defaultMaxTPS, "rate limit of maximum number of (write) transactions/second on the destination (unlimited by default)")
maxReplicationLag := subFlags.Int64("max_replication_lag", defaultMaxReplicationLag, "if set, the adapative throttler will be enabled and automatically adjust the write rate to keep the lag below the set value in seconds (disabled by default)")
if err := subFlags.Parse(args); err != nil {
return nil, err
}
if subFlags.NArg() != 1 {
subFlags.Usage()
return nil, fmt.Errorf("command SplitClone requires <keyspace/shard>")
}
keyspace, shard, err := topoproto.ParseKeyspaceShard(subFlags.Arg(0))
if err != nil {
return nil, err
}
var excludeTableArray []string
if *excludeTables != "" {
excludeTableArray = strings.Split(*excludeTables, ",")
}
worker, err := newSplitCloneWorker(wr, wi.cell, keyspace, shard, *online, *offline, excludeTableArray, *chunkCount, *minRowsPerChunk, *sourceReaderCount, *writeQueryMaxRows, *writeQueryMaxSize, *destinationWriterCount, *minHealthyRdonlyTablets, *maxTPS, *maxReplicationLag)
if err != nil {
return nil, vterrors.Wrap(err, "cannot create split clone worker")
}
return worker, nil
}
func keyspacesWithOverlappingShards(ctx context.Context, wr *wrangler.Wrangler) ([]map[string]string, error) {
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
keyspaces, err := wr.TopoServer().GetKeyspaces(shortCtx)
cancel()
if err != nil {
return nil, vterrors.Wrap(err, "failed to get list of keyspaces")
}
wg := sync.WaitGroup{}
mu := sync.Mutex{} // protects result
result := make([]map[string]string, 0, len(keyspaces))
rec := concurrency.AllErrorRecorder{}
for _, keyspace := range keyspaces {
wg.Add(1)
go func(keyspace string) {
defer wg.Done()
shortCtx, cancel = context.WithTimeout(ctx, *remoteActionsTimeout)
osList, err := topotools.FindOverlappingShards(shortCtx, wr.TopoServer(), keyspace)
cancel()
if err != nil {
rec.RecordError(err)
return
}
mu.Lock()
for _, os := range osList {
result = append(result, map[string]string{
"Keyspace": os.Left[0].Keyspace(),
"Shard": os.Left[0].ShardName(),
})
}
mu.Unlock()
}(keyspace)
}
wg.Wait()
if rec.HasErrors() {
return nil, rec.Error()
}
if len(result) == 0 {
return nil, fmt.Errorf("There are no keyspaces with overlapping shards")
}
return result, nil
}
func interactiveSplitClone(ctx context.Context, wi *Instance, wr *wrangler.Wrangler, w http.ResponseWriter, r *http.Request) (Worker, *template.Template, map[string]interface{}, error) {
if err := r.ParseForm(); err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse form")
}
keyspace := r.FormValue("keyspace")
shard := r.FormValue("shard")
if keyspace == "" || shard == "" {
// display the list of possible splits to choose from
// (just find all the overlapping guys)
result := make(map[string]interface{})
choices, err := keyspacesWithOverlappingShards(ctx, wr)
if err != nil {
result["Error"] = err.Error()
} else {
result["Choices"] = choices
}
return nil, splitCloneTemplate, result, nil
}
sourceReaderCountStr := r.FormValue("sourceReaderCount")
if sourceReaderCountStr == "" {
// display the input form
result := make(map[string]interface{})
result["Keyspace"] = keyspace
result["Shard"] = shard
result["DefaultOnline"] = defaultOnline
result["DefaultOffline"] = defaultOffline
result["DefaultChunkCount"] = fmt.Sprintf("%v", defaultChunkCount)
result["DefaultMinRowsPerChunk"] = fmt.Sprintf("%v", defaultMinRowsPerChunk)
result["DefaultSourceReaderCount"] = fmt.Sprintf("%v", defaultSourceReaderCount)
result["DefaultWriteQueryMaxRows"] = fmt.Sprintf("%v", defaultWriteQueryMaxRows)
result["DefaultWriteQueryMaxSize"] = fmt.Sprintf("%v", defaultWriteQueryMaxSize)
result["DefaultDestinationWriterCount"] = fmt.Sprintf("%v", defaultDestinationWriterCount)
result["DefaultMinHealthyRdonlyTablets"] = fmt.Sprintf("%v", defaultMinHealthyRdonlyTablets)
result["DefaultMaxTPS"] = fmt.Sprintf("%v", defaultMaxTPS)
result["DefaultMaxReplicationLag"] = fmt.Sprintf("%v", defaultMaxReplicationLag)
return nil, splitCloneTemplate2, result, nil
}
// get other parameters
onlineStr := r.FormValue("online")
online := onlineStr == "true"
offlineStr := r.FormValue("offline")
offline := offlineStr == "true"
excludeTables := r.FormValue("excludeTables")
var excludeTableArray []string
if excludeTables != "" {
excludeTableArray = strings.Split(excludeTables, ",")
}
chunkCountStr := r.FormValue("chunkCount")
chunkCount, err := strconv.ParseInt(chunkCountStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse chunkCount")
}
minRowsPerChunkStr := r.FormValue("minRowsPerChunk")
minRowsPerChunk, err := strconv.ParseInt(minRowsPerChunkStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse minRowsPerChunk")
}
sourceReaderCount, err := strconv.ParseInt(sourceReaderCountStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse sourceReaderCount")
}
writeQueryMaxRowsStr := r.FormValue("writeQueryMaxRows")
writeQueryMaxRows, err := strconv.ParseInt(writeQueryMaxRowsStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse writeQueryMaxRows")
}
writeQueryMaxSizeStr := r.FormValue("writeQueryMaxSize")
writeQueryMaxSize, err := strconv.ParseInt(writeQueryMaxSizeStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse writeQueryMaxSize")
}
destinationWriterCountStr := r.FormValue("destinationWriterCount")
destinationWriterCount, err := strconv.ParseInt(destinationWriterCountStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse destinationWriterCount")
}
minHealthyRdonlyTabletsStr := r.FormValue("minHealthyRdonlyTablets")
minHealthyRdonlyTablets, err := strconv.ParseInt(minHealthyRdonlyTabletsStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse minHealthyRdonlyTablets")
}
maxTPSStr := r.FormValue("maxTPS")
maxTPS, err := strconv.ParseInt(maxTPSStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse maxTPS")
}
maxReplicationLagStr := r.FormValue("maxReplicationLag")
maxReplicationLag, err := strconv.ParseInt(maxReplicationLagStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse maxReplicationLag")
}
// start the clone job
wrk, err := newSplitCloneWorker(wr, wi.cell, keyspace, shard, online, offline, excludeTableArray, int(chunkCount), int(minRowsPerChunk), int(sourceReaderCount), int(writeQueryMaxRows), int(writeQueryMaxSize), int(destinationWriterCount), int(minHealthyRdonlyTablets), maxTPS, maxReplicationLag)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot create worker")
}
return wrk, nil, nil, nil
}
func init() {
AddCommand("Clones", Command{"SplitClone",
commandSplitClone, interactiveSplitClone,
"[--online=false] [--offline=false] [--exclude_tables=''] <keyspace/shard>",
"Replicates the data and creates configuration for a horizontal split."})
}