forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_diff_cmd.go
204 lines (186 loc) · 6.74 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
// Copyright 2013, 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/wrangler"
"golang.org/x/net/context"
)
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>
<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")
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, ",")
}
return NewSplitDiffWorker(wr, wi.cell, keyspace, shard, uint32(*sourceUID), excludeTableArray, *minHealthyRdonlyTablets), 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, 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)
shards, err := wr.TopoServer().GetShardNames(shortCtx, keyspace)
cancel()
if err != nil {
rec.RecordError(fmt.Errorf("failed to get list of shards for keyspace '%v': %v", keyspace, err))
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(fmt.Errorf("failed to get details for shard '%v': %v", topoproto.KeyspaceShardString(keyspace, shard), err))
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, fmt.Errorf("cannot parse form: %s", err)
}
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)
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, fmt.Errorf("cannot parse sourceUID: %s", err)
}
excludeTables := r.FormValue("excludeTables")
var excludeTableArray []string
if excludeTables != "" {
excludeTableArray = strings.Split(excludeTables, ",")
}
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)
}
// start the diff job
wrk := NewSplitDiffWorker(wr, wi.cell, keyspace, shard, uint32(sourceUID), excludeTableArray, int(minHealthyRdonlyTablets))
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"})
}