forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_clone_cmd.go
248 lines (229 loc) · 10.6 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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package worker
import (
"flag"
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"sync"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"github.com/youtube/vitess/go/vt/topotools"
"github.com/youtube/vitess/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="excludeTables">Exclude Tables: </LABEL>
<INPUT type="text" id="excludeTables" name="excludeTables" value="moving.*"></BR>
<LABEL for="strategy">Strategy: </LABEL>
<INPUT type="text" id="strategy" name="strategy" value=""></BR>
<LABEL for="sourceReaderCount">Source Reader Count: </LABEL>
<INPUT type="text" id="sourceReaderCount" name="sourceReaderCount" value="{{.DefaultSourceReaderCount}}"></BR>
<LABEL for="destinationPackCount">Destination Pack Count: </LABEL>
<INPUT type="text" id="destinationPackCount" name="destinationPackCount" value="{{.DefaultDestinationPackCount}}"></BR>
<LABEL for="minTableSizeForSplit">Minimun Table Size For Split: </LABEL>
<INPUT type="text" id="minTableSizeForSplit" name="minTableSizeForSplit" value="{{.DefaultMinTableSizeForSplit}}"></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: </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>
<INPUT type="hidden" name="keyspace" value="{{.Keyspace}}"/>
<INPUT type="hidden" name="shard" value="{{.Shard}}"/>
<INPUT type="submit" value="Clone"/>
</form>
<h1>Help</h1>
<p>Strategy can have the following values, comma separated:</p>
<ul>
<li><b>skipPopulateBlpCheckpoint</b>: skips creating (if necessary) and populating the blp_checkpoint table in the destination. Not skipped by default because it's required for filtered replication to start.</li>
<li><b>dontStartBinlogPlayer</b>: (requires skipPopulateBlpCheckpoint to be false) will setup, but not start binlog replication on the destination. The flag has to be manually cleared from the _vt.blp_checkpoint table.</li>
<li><b>skipSetSourceShards</b>: we won't set SourceShards on the destination shards, disabling filtered replication. Useful for worker tests.</li>
</ul>
</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) {
excludeTables := subFlags.String("exclude_tables", "", "comma separated list of tables to exclude")
strategy := subFlags.String("strategy", "", "which strategy to use for restore, use 'vtworker SplitClone --strategy=-help k/s' for more info")
sourceReaderCount := subFlags.Int("source_reader_count", defaultSourceReaderCount, "number of concurrent streaming queries to use on the source")
destinationPackCount := subFlags.Int("destination_pack_count", defaultDestinationPackCount, "number of packets to pack in one destination insert")
minTableSizeForSplit := subFlags.Int("min_table_size_for_split", defaultMinTableSizeForSplit, "tables bigger than this size on disk in bytes will be split into source_reader_count chunks if possible")
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 before taking out one")
maxTPS := subFlags.Int64("max_tps", defaultMaxTPS, "if non-zero, limit copy to maximum number of (write) transactions/second on the destination (unlimited 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, excludeTableArray, *strategy, *sourceReaderCount, *destinationPackCount, uint64(*minTableSizeForSplit), *destinationWriterCount, *minHealthyRdonlyTablets, *maxTPS)
if err != nil {
return nil, fmt.Errorf("cannot create split clone worker: %v", err)
}
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, fmt.Errorf("failed to get list of keyspaces: %v", err)
}
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, fmt.Errorf("cannot parse form: %s", err)
}
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["DefaultSourceReaderCount"] = fmt.Sprintf("%v", defaultSourceReaderCount)
result["DefaultDestinationPackCount"] = fmt.Sprintf("%v", defaultDestinationPackCount)
result["DefaultMinTableSizeForSplit"] = fmt.Sprintf("%v", defaultMinTableSizeForSplit)
result["DefaultDestinationWriterCount"] = fmt.Sprintf("%v", defaultDestinationWriterCount)
result["DefaultMinHealthyRdonlyTablets"] = fmt.Sprintf("%v", defaultMinHealthyRdonlyTablets)
result["DefaultMaxTPS"] = fmt.Sprintf("%v", defaultMaxTPS)
return nil, splitCloneTemplate2, result, nil
}
// get other parameters
destinationPackCountStr := r.FormValue("destinationPackCount")
excludeTables := r.FormValue("excludeTables")
var excludeTableArray []string
if excludeTables != "" {
excludeTableArray = strings.Split(excludeTables, ",")
}
strategy := r.FormValue("strategy")
sourceReaderCount, err := strconv.ParseInt(sourceReaderCountStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse sourceReaderCount: %s", err)
}
destinationPackCount, err := strconv.ParseInt(destinationPackCountStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse destinationPackCount: %s", err)
}
minTableSizeForSplitStr := r.FormValue("minTableSizeForSplit")
minTableSizeForSplit, err := strconv.ParseInt(minTableSizeForSplitStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse minTableSizeForSplit: %s", err)
}
destinationWriterCountStr := r.FormValue("destinationWriterCount")
destinationWriterCount, err := strconv.ParseInt(destinationWriterCountStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse destinationWriterCount: %s", err)
}
minHealthyRdonlyTabletsStr := r.FormValue("minHealthyRdonlyTablets")
minHealthyRdonlyTablets, err := strconv.ParseInt(minHealthyRdonlyTabletsStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse minHealthyRdonlyTablets: %s", err)
}
maxTPSStr := r.FormValue("maxTPS")
maxTPS, err := strconv.ParseInt(maxTPSStr, 0, 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot parse maxTPS: %s", err)
}
// start the clone job
wrk, err := NewSplitCloneWorker(wr, wi.cell, keyspace, shard, excludeTableArray, strategy, int(sourceReaderCount), int(destinationPackCount), uint64(minTableSizeForSplit), int(destinationWriterCount), int(minHealthyRdonlyTablets), maxTPS)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot create worker: %v", err)
}
return wrk, nil, nil, nil
}
func init() {
AddCommand("Clones", Command{"SplitClone",
commandSplitClone, interactiveSplitClone,
"[--exclude_tables=''] [--strategy=''] <keyspace/shard>",
"Replicates the data and creates configuration for a horizontal split."})
}