-
Notifications
You must be signed in to change notification settings - Fork 13
/
vtrepairtable.go
385 lines (337 loc) · 11.1 KB
/
vtrepairtable.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"fmt"
"context"
"flag"
"os"
"strings"
"time"
log "github.com/golang/glog"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/sqlescape"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
_ "vitess.io/vitess/go/vt/topo/zk2topo"
"vitess.io/vitess/go/vt/vtgate/vindexes"
_ "vitess.io/vitess/go/vt/vttablet/grpctabletconn"
"vitess.io/vitess/go/vt/vttablet/queryservice"
"vitess.io/vitess/go/vt/vttablet/tabletconn"
)
var (
cell = flag.String("cell", "", "which cell to search for the MASTER tablet in")
timeout = flag.Duration("deadline", 5*time.Minute, "timeout for the copy process")
dry = flag.Bool("dry", true, "dry run")
ignoreDuplicateEntryConstraint = flag.String("ignore_duplicate_entry", "", "ignore the named DuplicateEntry constraint when inserting (NOTE: Will still delete the row in the source shard so may cause data loss unless used with care)")
readFromTabletType = flag.String("read_from_tablet_type", "REPLICA", "which type of tablet to read data from")
committed = false
)
var doc = `
Moves rows from one shard to another. Works non-transactionally like this:
select all the rows from the source
for each row:
insert row with autocommit
delete row with autocommit
If there is a failure it will stop working but will not roll back any of the work so far.
`
func main() {
defer exit.Recover()
defer logutil.Flush()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%v <from shard> <to shard> <table>\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, doc)
}
flag.Parse()
args := flag.Args()
if len(args) != 3 {
flag.Usage()
exit.Return(1)
}
ctx, cancelFunc := context.WithTimeout(context.Background(), *timeout)
defer cancelFunc()
sourceTarget, err := parseTarget(args[0])
if err != nil {
log.Fatalln(err)
}
// TODO ideally we'd just figure out which shard it belongs to and move it there
destTarget, err := parseTarget(args[1])
if err != nil {
log.Fatalln(err)
}
if sourceTarget.Keyspace != destTarget.Keyspace {
log.Fatalln("can only move between shards in the same keyspace")
}
tableName := args[2]
ts := topo.Open()
vschema, err := loadVSchema(ctx, ts, sourceTarget.Keyspace)
if err != nil {
log.Fatalln(err)
}
sourceQs, _, err := Connect(ctx, ts, sourceTarget)
if err != nil {
log.Fatalln(err)
}
sourceReplicaTarget := *sourceTarget
sourceReplicaTarget.TabletType, err = topoproto.ParseTabletType(*readFromTabletType)
if err != nil {
log.Fatalln(err)
}
sourceReplicaQs, _, err := Connect(ctx, ts, &sourceReplicaTarget)
if err != nil {
log.Fatalln(err)
}
destShard, err := ts.GetShard(ctx, destTarget.Keyspace, destTarget.Shard)
if err != nil {
log.Fatalln(err)
}
destQs, _, err := Connect(ctx, ts, destTarget)
if err != nil {
log.Fatalln(err)
}
selectSql := fmt.Sprintf("SELECT * FROM %v", tableName)
log.Infoln(selectSql)
fieldResult, err := sourceQs.Execute(ctx, sourceTarget, selectSql+" WHERE 1=0", make(map[string]*query.BindVariable), 0, nil)
if err != nil {
log.Fatalln(err)
}
var fieldVars = make([]string, len(fieldResult.Fields))
for i, field := range fieldResult.Fields {
if field.Name == "updated_at" {
fieldVars[i] = "now()"
} else {
fieldVars[i] = ":" + field.Name
}
}
insertSql := "INSERT INTO " + sqlescape.EscapeID(tableName) +
" (" + strings.Join(escapeAll(fieldResult.Fields), ", ") + ") VALUES (" +
strings.Join(fieldVars, ",") + ")"
// TODO handle primary keys properly
deleteSql := "DELETE FROM " + sqlescape.EscapeID(tableName) + " WHERE id = :id"
table := vschema.Tables[tableName]
primaryVindex := table.ColumnVindexes[0]
if len(primaryVindex.Columns) > 1 {
log.Fatalln("can only handle vindex on single column")
}
uniqueVindex, ok := primaryVindex.Vindex.(vindexes.Unique)
if !ok {
log.Fatalln("primary vindex have to be unqiue")
}
// TODO handle primary keys properly
idColumnIndex := -1
primaryVindexedColumn := primaryVindex.Columns[0].Lowered()
primaryVindexColumIndex := -1
for i, field := range fieldResult.Fields {
if field.Name == "id" {
idColumnIndex = i
}
if field.Name == primaryVindexedColumn {
primaryVindexColumIndex = i
}
}
if idColumnIndex == -1 {
log.Fatalf("no column named 'id' in: %#v", fieldResult.Fields)
}
if primaryVindexColumIndex == -1 {
log.Fatalf("could not find primary vindexed column '%v' in: %#v", primaryVindexedColumn, fieldResult.Fields)
}
var rowsToMove [][]sqltypes.Value
var rowCount = 0
// This disables rowlimit and query timeout so we can process the entire table
executeOptions := query.ExecuteOptions{Workload: query.ExecuteOptions_OLAP}
err = sourceReplicaQs.StreamExecute(ctx, &sourceReplicaTarget, selectSql, make(map[string]*query.BindVariable), &executeOptions, func(result *sqltypes.Result) error {
if len(result.Rows) == 0 {
return nil
}
rowCount += len(result.Rows)
for _, row := range result.Rows {
// Check that the row actually belongs to the destination shard
primaryVindexVal := row[primaryVindexColumIndex]
ksids, err := uniqueVindex.Map(nil, []sqltypes.Value{primaryVindexVal})
if err != nil {
return err
}
if ksids[0].Range != nil || ksids[0].ID == nil {
return fmt.Errorf("could not map %v to a keyspace id", primaryVindexVal)
}
if !key.KeyRangeContains(destShard.KeyRange, ksids[0].ID) {
continue
}
idVal := row[idColumnIndex]
log.Infof("row %v belongs to target shard. queueing up for move", idVal)
rowsToMove = append(rowsToMove, row)
}
log.Infof("processed %v rows", rowCount)
return nil
})
if err != nil {
log.Fatal(err)
}
if len(rowsToMove) > 0 {
log.Infof("moving %v rows", len(rowsToMove))
sourceTx, err := sourceQs.Begin(ctx, sourceTarget, nil)
if err != nil {
log.Fatal(err)
}
defer maybeRollback(sourceQs, sourceTarget, sourceTx)
destTx, err := destQs.Begin(ctx, destTarget, nil)
if err != nil {
log.Fatalln(err)
}
defer maybeRollback(destQs, destTarget, destTx)
var insertCount uint64 = 0
var deleteCount uint64 = 0
for _, row := range rowsToMove {
// Insert the row in the source
insertVars := make(map[string]*query.BindVariable)
for i, field := range fieldResult.Fields {
if field.Name != "updated_at" {
insertVars[field.Name] = sqltypes.ValueBindVariable(row[i])
}
}
log.Infoln(insertSql)
insertResult, err := destQs.Execute(ctx, destTarget, insertSql, insertVars, destTx, nil)
if err != nil {
if *ignoreDuplicateEntryConstraint == "" {
log.Fatalln(err)
} else {
// Handle the Duplicate entry constraint
errMessage := fmt.Sprintf("%v", err)
if !strings.Contains(errMessage, "code = AlreadyExists") {
log.Fatalln(err)
}
if !strings.Contains(errMessage, *ignoreDuplicateEntryConstraint) {
log.Fatalln(err)
}
log.Warningf("ignoring constraint violation: %v", err)
}
} else {
if insertResult.RowsAffected != 1 {
log.Fatalln("row was not inserted. something is wrong. aborting and rolling back")
}
insertCount += insertResult.RowsAffected
}
// Delete the row in the source
idVal := row[idColumnIndex]
deleteVars := map[string]*query.BindVariable{"id": sqltypes.ValueBindVariable(idVal)}
log.Infoln(deleteSql)
deleteResult, err := sourceQs.Execute(ctx, sourceTarget, deleteSql, deleteVars, sourceTx, nil)
if err != nil {
log.Fatalln(err)
}
if deleteResult.RowsAffected != 1 {
log.Fatalln("row was not inserted. something is wrong. aborting and rolling back")
}
deleteCount += deleteResult.RowsAffected
}
log.Infof("inserted %v rows", insertCount)
log.Infof("deleted %v rows", deleteCount)
if *dry {
log.Infof("dry run. rolling back")
destQs.Rollback(ctx, destTarget, destTx)
sourceQs.Rollback(ctx, sourceTarget, sourceTx)
} else {
log.Infof("committing")
// Commit the destination first so we don't "drop" the rows
destQs.Commit(ctx, destTarget, destTx)
sourceQs.Commit(ctx, sourceTarget, sourceTx)
}
committed = true
} else {
log.Infoln("nothing to move. done")
}
}
func maybeRollback(qs queryservice.QueryService, target *query.Target, tx int64) {
if !committed {
// TODO handle failures properly
log.Infof("rolling back %v", target)
qs.Rollback(context.Background(), target, tx)
}
}
func Connect(ctx context.Context, ts *topo.Server, target *query.Target) (queryservice.QueryService, *topodata.Tablet, error) {
tabletInfo, err := GetTablet(ctx, ts, target)
if err != nil {
return nil, nil, err
}
queryService, err := tabletconn.GetDialer()(tabletInfo.Tablet, true)
if err != nil {
return nil, nil, err
}
return queryService, tabletInfo.Tablet, nil
}
func GetTablet(ctx context.Context, ts *topo.Server, target *query.Target) (*topo.TabletInfo, error) {
var tabletInfos map[string]*topo.TabletInfo
var err error
if *cell != "" {
tabletInfos, err = ts.GetTabletMapForShardByCell(ctx, target.Keyspace, target.Shard, []string{*cell})
if err != nil {
return nil, err
}
} else {
tabletInfos, err = ts.GetTabletMapForShard(ctx, target.Keyspace, target.Shard)
if err != nil {
return nil, err
}
}
for _, tabletInfo := range tabletInfos {
if tabletInfo.Tablet.Type == target.TabletType {
return tabletInfo, nil
}
}
return nil, fmt.Errorf("could not find tablet matching: %v", target)
}
func escapeAll(identifiers []*query.Field) []string {
result := make([]string, len(identifiers))
for i := range identifiers {
result[i] = sqlescape.EscapeID(identifiers[i].Name)
}
return result
}
func parseTarget(targetString string) (*query.Target, error) {
// Default tablet type is master.
target := &query.Target{
TabletType: topodata.TabletType_MASTER,
}
last := strings.LastIndexAny(targetString, "@")
if last != -1 {
// No need to check the error. UNKNOWN will be returned on
// error and it will fail downstream.
tabletType, err := topoproto.ParseTabletType(targetString[last+1:])
if err != nil {
return target, err
}
target.TabletType = tabletType
targetString = targetString[:last]
}
last = strings.LastIndexAny(targetString, "/:")
if last != -1 {
target.Shard = targetString[last+1:]
targetString = targetString[:last]
}
target.Keyspace = targetString
if target.Keyspace == "" {
return target, fmt.Errorf("no keyspace in: %v", targetString)
}
if target.Shard == "" {
return target, fmt.Errorf("no shard in: %v", targetString)
}
return target, nil
}
func loadVSchema(ctx context.Context, ts *topo.Server, keyspace string) (*vindexes.KeyspaceSchema, error) {
kschema, err := ts.GetVSchema(ctx, keyspace)
if err != nil {
return nil, fmt.Errorf("cannot load VSchema for keyspace %v: %v", keyspace, err)
}
if kschema == nil {
return nil, fmt.Errorf("no VSchema for keyspace %v", keyspace)
}
keyspaceSchema, err := vindexes.BuildKeyspaceSchema(kschema, keyspace)
if err != nil {
return nil, fmt.Errorf("cannot build vschema for keyspace %v: %v", keyspace, err)
}
return keyspaceSchema, nil
}