forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 2
/
split_diff_cmd.go
234 lines (209 loc) · 8.26 KB
/
split_diff_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
/*
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/wrangler"
"golang.org/x/net/context"
topodatapb "github.com/xsec-lab/go/vt/proto/topodata"
)
const splitDiffHTML = `
<!DOCTYPE html>
<head>
<title>Split Diff Action</title>
</head>
<body>
<h1>Split Diff Action</h1>
{{if .Error}}
<b>Error:</b> {{.Error}}</br>
{{else}}
{{range $i, $si := .Shards}}
<li><a href="/Diffs/SplitDiff?keyspace={{$si.Keyspace}}&shard={{$si.Shard}}">{{$si.Keyspace}}/{{$si.Shard}}</a></li>
{{end}}
{{end}}
</body>
`
const splitDiffHTML2 = `
<!DOCTYPE html>
<head>
<title>Split Diff Action</title>
</head>
<body>
<p>Shard involved: {{.Keyspace}}/{{.Shard}}</p>
<h1>Split Diff Action</h1>
<form action="/Diffs/SplitDiff" method="post">
<LABEL for="sourceUID">Source shard UID: </LABEL>
<INPUT type="text" id="sourceUID" name="sourceUID" value="{{.DefaultSourceUID}}"></BR>
<LABEL for="excludeTables">Exclude Tables: </LABEL>
<INPUT type="text" id="excludeTables" name="excludeTables" value=""></BR>
<LABEL for="minHealthyRdonlyTablets">Minimum Number of required healthy RDONLY tablets: </LABEL>
<INPUT type="text" id="minHealthyRdonlyTablets" name="minHealthyRdonlyTablets" value="{{.DefaultMinHealthyRdonlyTablets}}"></BR>
<LABEL for="parallelDiffsCount">Number of tables to diff in parallel: </LABEL>
<INPUT type="text" id="parallelDiffsCount" name="parallelDiffsCount" value="{{.DefaultParallelDiffsCount}}"></BR>
<INPUT type="hidden" name="keyspace" value="{{.Keyspace}}"/>
<INPUT type="hidden" name="shard" value="{{.Shard}}"/>
<INPUT type="submit" name="submit" value="Split Diff"/>
</form>
</body>
`
var splitDiffTemplate = mustParseTemplate("splitDiff", splitDiffHTML)
var splitDiffTemplate2 = mustParseTemplate("splitDiff2", splitDiffHTML2)
func commandSplitDiff(wi *Instance, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) (Worker, error) {
sourceUID := subFlags.Int("source_uid", 0, "uid of the source shard to run the diff against")
excludeTables := subFlags.String("exclude_tables", "", "comma separated list of tables to exclude")
minHealthyRdonlyTablets := subFlags.Int("min_healthy_rdonly_tablets", defaultMinHealthyRdonlyTablets, "minimum number of healthy RDONLY tablets before taking out one")
destTabletTypeStr := subFlags.String("dest_tablet_type", defaultDestTabletType, "destination tablet type (RDONLY or REPLICA) that will be used to compare the shards")
parallelDiffsCount := subFlags.Int("parallel_diffs_count", defaultParallelDiffsCount, "number of tables to diff in parallel")
if err := subFlags.Parse(args); err != nil {
return nil, err
}
if subFlags.NArg() != 1 {
subFlags.Usage()
return nil, fmt.Errorf("command SplitDiff 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, ",")
}
destTabletType, ok := topodatapb.TabletType_value[*destTabletTypeStr]
if !ok {
return nil, fmt.Errorf("command SplitDiff invalid dest_tablet_type: %v", destTabletType)
}
return NewSplitDiffWorker(wr, wi.cell, keyspace, shard, uint32(*sourceUID), excludeTableArray, *minHealthyRdonlyTablets, *parallelDiffsCount, topodatapb.TabletType(destTabletType)), nil
}
// shardsWithSources returns all the shards that have SourceShards set
// with no Tables list.
func shardsWithSources(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)
shards, err := wr.TopoServer().GetShardNames(shortCtx, keyspace)
cancel()
if err != nil {
rec.RecordError(vterrors.Wrapf(err, "failed to get list of shards for keyspace '%v'", keyspace))
return
}
for _, shard := range shards {
wg.Add(1)
go func(keyspace, shard string) {
defer wg.Done()
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
si, err := wr.TopoServer().GetShard(shortCtx, keyspace, shard)
cancel()
if err != nil {
rec.RecordError(vterrors.Wrapf(err, "failed to get details for shard '%v'", topoproto.KeyspaceShardString(keyspace, shard)))
return
}
if len(si.SourceShards) > 0 && len(si.SourceShards[0].Tables) == 0 {
mu.Lock()
result = append(result, map[string]string{
"Keyspace": keyspace,
"Shard": shard,
})
mu.Unlock()
}
}(keyspace, shard)
}
}(keyspace)
}
wg.Wait()
if rec.HasErrors() {
return nil, rec.Error()
}
if len(result) == 0 {
return nil, fmt.Errorf("there are no shards with SourceShards")
}
return result, nil
}
func interactiveSplitDiff(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 shards to chose from
result := make(map[string]interface{})
shards, err := shardsWithSources(ctx, wr)
if err != nil {
result["Error"] = err.Error()
} else {
result["Shards"] = shards
}
return nil, splitDiffTemplate, result, nil
}
submitButtonValue := r.FormValue("submit")
if submitButtonValue == "" {
// display the input form
result := make(map[string]interface{})
result["Keyspace"] = keyspace
result["Shard"] = shard
result["DefaultSourceUID"] = "0"
result["DefaultMinHealthyRdonlyTablets"] = fmt.Sprintf("%v", defaultMinHealthyRdonlyTablets)
result["DefaultParallelDiffsCount"] = fmt.Sprintf("%v", defaultParallelDiffsCount)
return nil, splitDiffTemplate2, result, nil
}
// Process input form.
sourceUIDStr := r.FormValue("sourceUID")
sourceUID, err := strconv.ParseInt(sourceUIDStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse sourceUID")
}
excludeTables := r.FormValue("excludeTables")
var excludeTableArray []string
if excludeTables != "" {
excludeTableArray = strings.Split(excludeTables, ",")
}
minHealthyRdonlyTabletsStr := r.FormValue("minHealthyRdonlyTablets")
parallelDiffsCountStr := r.FormValue("parallelDiffsCount")
minHealthyRdonlyTablets, err := strconv.ParseInt(minHealthyRdonlyTabletsStr, 0, 64)
parallelDiffsCount, err := strconv.ParseInt(parallelDiffsCountStr, 0, 64)
if err != nil {
return nil, nil, nil, vterrors.Wrap(err, "cannot parse minHealthyRdonlyTablets")
}
// start the diff job
// TODO: @rafael - Add option to set destination tablet type in UI form.
wrk := NewSplitDiffWorker(wr, wi.cell, keyspace, shard, uint32(sourceUID), excludeTableArray, int(minHealthyRdonlyTablets), int(parallelDiffsCount), topodatapb.TabletType_RDONLY)
return wrk, nil, nil, nil
}
func init() {
AddCommand("Diffs", Command{"SplitDiff",
commandSplitDiff, interactiveSplitDiff,
"[--exclude_tables=''] <keyspace/shard>",
"Diffs a rdonly destination shard against its SourceShards"})
}