-
Notifications
You must be signed in to change notification settings - Fork 2k
/
executor.go
1266 lines (1139 loc) · 40.6 KB
/
executor.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 The Vitess Authors.
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 onlineddl
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/timer"
"vitess.io/vitess/go/vt/dbconnpool"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/vexec"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
var (
// ErrExecutorNotWritableTablet is generated when executor is asked to run gh-ost on a read-only server
ErrExecutorNotWritableTablet = errors.New("Cannot run gh-ost migration on non-writable tablet")
// ErrExecutorMigrationAlreadyRunning is generated when an attempt is made to run an operation that conflicts with a running migration
ErrExecutorMigrationAlreadyRunning = errors.New("Cannot run gh-ost migration since a migration is already running")
// ErrMigrationNotFound is returned by readMigration when given UUI cannot be found
ErrMigrationNotFound = errors.New("Migration not found")
)
var vexecUpdateTemplates = []string{
`update _vt.schema_migrations set migration_status='val' where migration_uuid='val' and mysql_schema='val'`,
`update _vt.schema_migrations set migration_status='val' where migration_uuid='val' and mysql_schema='val' and shard='val'`,
}
var vexecInsertTemplates = []string{
`INSERT IGNORE INTO _vt.schema_migrations (
migration_uuid,
keyspace,
shard,
mysql_schema,
mysql_table,
migration_statement,
strategy,
options,
requested_timestamp,
migration_status
) VALUES (
'val', 'val', 'val', 'val', 'val', 'val', 'val', 'val', FROM_UNIXTIME(0), 'val'
)`,
}
var emptyResult = &sqltypes.Result{
RowsAffected: 0,
}
var ghostOverridePath = flag.String("gh-ost-path", "", "override default gh-ost binary full path")
var ptOSCOverridePath = flag.String("pt-osc-path", "", "override default pt-online-schema-change binary full path")
const (
maxPasswordLength = 32 // MySQL's *replication* password may not exceed 32 characters
staleMigrationMinutes = 10
)
var (
migrationLogFileName = "migration.log"
onlineDDLUser = "vt-online-ddl-internal"
onlineDDLGrant = fmt.Sprintf("'%s'@'%s'", onlineDDLUser, "%")
)
// Executor wraps and manages the execution of a gh-ost migration.
type Executor struct {
env tabletenv.Env
pool *connpool.Pool
tabletTypeFunc func() topodatapb.TabletType
ts *topo.Server
keyspace string
shard string
dbName string
initMutex sync.Mutex
migrationMutex sync.Mutex
migrationRunning int64
lastMigrationUUID string
ticks *timer.Timer
isOpen bool
}
var (
migrationCheckInterval = time.Second * 10
)
// GhostBinaryFileName returns the full path+name of the gh-ost binary
func GhostBinaryFileName() (fileName string, isOverride bool) {
if *ghostOverridePath != "" {
return *ghostOverridePath, true
}
return path.Join(os.TempDir(), "vt-gh-ost"), false
}
// PTOSCFileName returns the full path+name of the pt-online-schema-change binary
// Note that vttablet does not include pt-online-schema-change
func PTOSCFileName() (fileName string, isOverride bool) {
if *ptOSCOverridePath != "" {
return *ptOSCOverridePath, true
}
return "/usr/bin/pt-online-schema-change", false
}
// NewExecutor creates a new gh-ost executor.
func NewExecutor(env tabletenv.Env, ts *topo.Server, tabletTypeFunc func() topodatapb.TabletType) *Executor {
return &Executor{
env: env,
pool: connpool.NewPool(env, "ExecutorPool", tabletenv.ConnPoolConfig{
Size: 1,
IdleTimeoutSeconds: env.Config().OltpReadPool.IdleTimeoutSeconds,
}),
tabletTypeFunc: tabletTypeFunc,
ts: ts,
ticks: timer.NewTimer(migrationCheckInterval),
}
}
func (e *Executor) execQuery(ctx context.Context, query string) (result *sqltypes.Result, err error) {
defer e.env.LogError()
conn, err := e.pool.Get(ctx)
if err != nil {
return result, err
}
defer conn.Recycle()
return conn.Exec(ctx, query, math.MaxInt32, true)
}
func (e *Executor) initSchema(ctx context.Context) error {
defer e.env.LogError()
conn, err := e.pool.Get(ctx)
if err != nil {
return err
}
defer conn.Recycle()
parsed := sqlparser.BuildParsedQuery(sqlValidationQuery, "_vt")
_, err = withDDL.Exec(ctx, parsed.Query, conn.Exec)
return err
}
// InitDBConfig initializes keysapce
func (e *Executor) InitDBConfig(keyspace, shard, dbName string) {
e.keyspace = keyspace
e.shard = shard
e.dbName = dbName
}
// Open opens database pool and initializes the schema
func (e *Executor) Open() error {
e.initMutex.Lock()
defer e.initMutex.Unlock()
if e.isOpen {
return nil
}
e.pool.Open(e.env.Config().DB.AppWithDB(), e.env.Config().DB.DbaWithDB(), e.env.Config().DB.AppDebugWithDB())
e.initSchema(context.Background())
e.ticks.Start(e.onMigrationCheckTick)
if _, err := sqlparser.QueryMatchesTemplates("select 1 from dual", vexecUpdateTemplates); err != nil {
// this validates vexecUpdateTemplates
return err
}
e.isOpen = true
return nil
}
// Close frees resources
func (e *Executor) Close() {
e.initMutex.Lock()
defer e.initMutex.Unlock()
if !e.isOpen {
return
}
e.ticks.Stop()
e.pool.Close()
e.isOpen = false
}
func (e *Executor) ghostPanicFlagFileName(uuid string) string {
return path.Join(os.TempDir(), fmt.Sprintf("ghost.%s.panic.flag", uuid))
}
func (e *Executor) createGhostPanicFlagFile(uuid string) error {
_, err := os.Create(e.ghostPanicFlagFileName(uuid))
return err
}
func (e *Executor) deleteGhostPanicFlagFile(uuid string) error {
// We use RemoveAll because if the file does not exist that's fine. Remove will return an error
// if file does not exist; RemoveAll does not.
return os.RemoveAll(e.ghostPanicFlagFileName(uuid))
}
func (e *Executor) ptPidFileName(uuid string) string {
return path.Join(os.TempDir(), fmt.Sprintf("pt-online-schema-change.%s.pid", uuid))
}
// readMySQLVariables contacts the backend MySQL server to read some of its configuration
func (e *Executor) readMySQLVariables(ctx context.Context) (host string, port int, readOnly bool, err error) {
conn, err := e.pool.Get(ctx)
if err != nil {
return host, port, readOnly, err
}
defer conn.Recycle()
tm, err := conn.Exec(ctx, "select @@global.hostname as hostname, @@global.port as port, @@global.read_only as read_only from dual", 1, true)
if err != nil {
return host, port, readOnly, vterrors.Errorf(vtrpcpb.Code_UNKNOWN, "could not read MySQL variables: %v", err)
}
row := tm.Named().Row()
if row == nil {
return host, port, readOnly, vterrors.Errorf(vtrpcpb.Code_UNKNOWN, "unexpected result for MySQL variables: %+v", tm.Rows)
}
host = row["hostname"].ToString()
p, err := row.ToInt64("port")
if err != nil {
return host, port, readOnly, vterrors.Errorf(vtrpcpb.Code_UNKNOWN, "could not parse @@global.port %v: %v", tm, err)
}
port = int(p)
if readOnly, err = row.ToBool("read_only"); err != nil {
return host, port, readOnly, vterrors.Errorf(vtrpcpb.Code_UNKNOWN, "could not parse @@global.read_only %v: %v", tm, err)
}
return host, port, readOnly, nil
}
// createOnlineDDLUser creates a gh-ost user account with all neccessary privileges and with a random password
func (e *Executor) createOnlineDDLUser(ctx context.Context) (password string, err error) {
conn, err := dbconnpool.NewDBConnection(ctx, e.env.Config().DB.DbaConnector())
if err != nil {
return password, err
}
defer conn.Close()
password = RandomHash()[0:maxPasswordLength]
for _, query := range sqlCreateOnlineDDLUser {
parsed := sqlparser.BuildParsedQuery(query, onlineDDLGrant, password)
if _, err := conn.ExecuteFetch(parsed.Query, 0, false); err != nil {
return password, err
}
}
for _, query := range sqlGrantOnlineDDLUser {
parsed := sqlparser.BuildParsedQuery(query, onlineDDLGrant)
if _, err := conn.ExecuteFetch(parsed.Query, 0, false); err != nil {
return password, err
}
}
return password, err
}
// dropOnlineDDLUser drops the given ddl user account at the end of migration
func (e *Executor) dropOnlineDDLUser(ctx context.Context) error {
conn, err := dbconnpool.NewDBConnection(ctx, e.env.Config().DB.DbaConnector())
if err != nil {
return err
}
defer conn.Close()
parsed := sqlparser.BuildParsedQuery(sqlDropOnlineDDLUser, onlineDDLGrant)
_, err = conn.ExecuteFetch(parsed.Query, 0, false)
return err
}
// ExecuteWithGhost validates and runs a gh-ost process.
// Validation included testing the backend MySQL server and the gh-ost binary itself
// Execution runs first a dry run, then an actual migration
func (e *Executor) ExecuteWithGhost(ctx context.Context, onlineDDL *schema.OnlineDDL) error {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()
if atomic.LoadInt64(&e.migrationRunning) > 0 {
return ErrExecutorMigrationAlreadyRunning
}
if e.tabletTypeFunc() != topodatapb.TabletType_MASTER {
return ErrExecutorNotWritableTablet
}
mysqlHost, mysqlPort, readOnly, err := e.readMySQLVariables(ctx)
if err != nil {
log.Errorf("Error before running gh-ost: %+v", err)
return err
}
if readOnly {
err := fmt.Errorf("Error before running gh-ost: MySQL server is read_only")
log.Errorf(err.Error())
return err
}
onlineDDLPassword, err := e.createOnlineDDLUser(ctx)
if err != nil {
err := fmt.Errorf("Error creating gh-ost user: %+v", err)
log.Errorf(err.Error())
return err
}
tempDir, err := createTempDir(onlineDDL.UUID)
if err != nil {
log.Errorf("Error creating temporary directory: %+v", err)
return err
}
binaryFileName, _ := GhostBinaryFileName()
credentialsConfigFileContent := fmt.Sprintf(`[client]
user=%s
password=${ONLINE_DDL_PASSWORD}
`, onlineDDLUser)
credentialsConfigFileName, err := createTempScript(tempDir, "gh-ost-conf.cfg", credentialsConfigFileContent)
if err != nil {
log.Errorf("Error creating config file: %+v", err)
return err
}
wrapperScriptContent := fmt.Sprintf(`#!/bin/bash
ghost_log_path="%s"
ghost_log_file="%s"
mkdir -p "$ghost_log_path"
export ONLINE_DDL_PASSWORD
%s "$@" > "$ghost_log_path/$ghost_log_file" 2>&1
`, tempDir, migrationLogFileName, binaryFileName,
)
wrapperScriptFileName, err := createTempScript(tempDir, "gh-ost-wrapper.sh", wrapperScriptContent)
if err != nil {
log.Errorf("Error creating wrapper script: %+v", err)
return err
}
onHookContent := func(status schema.OnlineDDLStatus) string {
return fmt.Sprintf(`#!/bin/bash
curl -s 'http://localhost:%d/schema-migration/report-status?uuid=%s&status=%s&dryrun='"$GH_OST_DRY_RUN"
`, *servenv.Port, onlineDDL.UUID, string(status))
}
if _, err := createTempScript(tempDir, "gh-ost-on-startup", onHookContent(schema.OnlineDDLStatusRunning)); err != nil {
log.Errorf("Error creating script: %+v", err)
return err
}
if _, err := createTempScript(tempDir, "gh-ost-on-status", onHookContent(schema.OnlineDDLStatusRunning)); err != nil {
log.Errorf("Error creating script: %+v", err)
return err
}
if _, err := createTempScript(tempDir, "gh-ost-on-success", onHookContent(schema.OnlineDDLStatusComplete)); err != nil {
log.Errorf("Error creating script: %+v", err)
return err
}
if _, err := createTempScript(tempDir, "gh-ost-on-failure", onHookContent(schema.OnlineDDLStatusFailed)); err != nil {
log.Errorf("Error creating script: %+v", err)
return err
}
serveSocketFile := path.Join(tempDir, "serve.sock")
if err := e.deleteGhostPanicFlagFile(onlineDDL.UUID); err != nil {
log.Errorf("Error removing gh-ost panic flag file %s: %+v", e.ghostPanicFlagFileName(onlineDDL.UUID), err)
return err
}
// Validate gh-ost binary:
log.Infof("Will now validate gh-ost binary")
_, err = execCmd(
"bash",
[]string{
wrapperScriptFileName,
"--version",
},
os.Environ(),
"/tmp",
nil,
nil,
)
if err != nil {
log.Errorf("Error testing gh-ost binary: %+v", err)
return err
}
log.Infof("+ OK")
if err := e.updateMigrationLogPath(ctx, onlineDDL.UUID, mysqlHost, tempDir); err != nil {
return err
}
runGhost := func(execute bool) error {
// Temporary hack (2020-08-11)
// Because sqlparser does not do full blown ALTER TABLE parsing,
// and because we don't want gh-ost to know about WITH_GHOST and WITH_PT syntax,
// we resort to regexp-based parsing of the query.
// TODO(shlomi): generate _alter options_ via sqlparser when it full supports ALTER TABLE syntax.
_, _, alterOptions := schema.ParseAlterTableOptions(onlineDDL.SQL)
forceTableNames := fmt.Sprintf("%s_%s", onlineDDL.UUID, ReadableTimestamp())
if err := e.updateArtifacts(ctx, onlineDDL.UUID,
fmt.Sprintf("_%s_gho", forceTableNames),
fmt.Sprintf("_%s_ghc", forceTableNames),
fmt.Sprintf("_%s_del", forceTableNames),
); err != nil {
return err
}
os.Setenv("ONLINE_DDL_PASSWORD", onlineDDLPassword)
args := []string{
wrapperScriptFileName,
fmt.Sprintf(`--host=%s`, mysqlHost),
fmt.Sprintf(`--port=%d`, mysqlPort),
fmt.Sprintf(`--conf=%s`, credentialsConfigFileName), // user & password found here
`--allow-on-master`,
`--max-load=Threads_running=900`,
`--critical-load=Threads_running=1000`,
`--critical-load-hibernate-seconds=60`,
`--approve-renamed-columns`,
`--debug`,
`--exact-rowcount`,
`--default-retries=120`,
fmt.Sprintf("--force-table-names=%s", forceTableNames),
fmt.Sprintf("--serve-socket-file=%s", serveSocketFile),
fmt.Sprintf("--hooks-path=%s", tempDir),
fmt.Sprintf(`--hooks-hint-token=%s`, onlineDDL.UUID),
fmt.Sprintf(`--throttle-http=http://localhost:%d/throttler/check`, *servenv.Port),
fmt.Sprintf(`--database=%s`, e.dbName),
fmt.Sprintf(`--table=%s`, onlineDDL.Table),
fmt.Sprintf(`--alter=%s`, alterOptions),
fmt.Sprintf(`--panic-flag-file=%s`, e.ghostPanicFlagFileName(onlineDDL.UUID)),
fmt.Sprintf(`--execute=%t`, execute),
}
args = append(args, strings.Fields(onlineDDL.Options)...)
_, err := execCmd("bash", args, os.Environ(), "/tmp", nil, nil)
return err
}
atomic.StoreInt64(&e.migrationRunning, 1)
e.lastMigrationUUID = onlineDDL.UUID
go func() error {
defer atomic.StoreInt64(&e.migrationRunning, 0)
defer e.dropOnlineDDLUser(ctx)
log.Infof("Will now dry-run gh-ost on: %s:%d", mysqlHost, mysqlPort)
if err := runGhost(false); err != nil {
// perhaps gh-ost was interrupted midway and didn't have the chance to send a "failes" status
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
log.Errorf("Error executing gh-ost dry run: %+v", err)
return err
}
log.Infof("+ OK")
log.Infof("Will now run gh-ost on: %s:%d", mysqlHost, mysqlPort)
startedMigrations.Add(1)
if err := runGhost(true); err != nil {
// perhaps gh-ost was interrupted midway and didn't have the chance to send a "failes" status
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
failedMigrations.Add(1)
log.Errorf("Error running gh-ost: %+v", err)
return err
}
// Migration successful!
os.RemoveAll(tempDir)
successfulMigrations.Add(1)
log.Infof("+ OK")
return nil
}()
return nil
}
// ExecuteWithPTOSC validates and runs a pt-online-schema-change process.
// Validation included testing the backend MySQL server and the pt-online-schema-change binary itself
// Execution runs first a dry run, then an actual migration
func (e *Executor) ExecuteWithPTOSC(ctx context.Context, onlineDDL *schema.OnlineDDL) error {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()
if atomic.LoadInt64(&e.migrationRunning) > 0 {
return ErrExecutorMigrationAlreadyRunning
}
if e.tabletTypeFunc() != topodatapb.TabletType_MASTER {
return ErrExecutorNotWritableTablet
}
mysqlHost, mysqlPort, readOnly, err := e.readMySQLVariables(ctx)
if err != nil {
log.Errorf("Error before running pt-online-schema-change: %+v", err)
return err
}
if readOnly {
err := fmt.Errorf("Error before running pt-online-schema-change: MySQL server is read_only")
log.Errorf(err.Error())
return err
}
onlineDDLPassword, err := e.createOnlineDDLUser(ctx)
if err != nil {
err := fmt.Errorf("Error creating pt-online-schema-change user: %+v", err)
log.Errorf(err.Error())
return err
}
tempDir, err := createTempDir(onlineDDL.UUID)
if err != nil {
log.Errorf("Error creating temporary directory: %+v", err)
return err
}
binaryFileName, _ := PTOSCFileName()
wrapperScriptContent := fmt.Sprintf(`#!/bin/bash
pt_log_path="%s"
pt_log_file="%s"
mkdir -p "$pt_log_path"
export MYSQL_PWD
%s "$@" > "$pt_log_path/$pt_log_file" 2>&1
`, tempDir, migrationLogFileName, binaryFileName,
)
wrapperScriptFileName, err := createTempScript(tempDir, "pt-online-schema-change-wrapper.sh", wrapperScriptContent)
if err != nil {
log.Errorf("Error creating wrapper script: %+v", err)
return err
}
pluginCode := `
package pt_online_schema_change_plugin;
use strict;
use LWP::Simple;
sub new {
my($class, % args) = @_;
my $self = { %args };
return bless $self, $class;
}
sub init {
my($self, % args) = @_;
}
sub before_create_new_table {
my($self, % args) = @_;
get("http://localhost:{{VTTABLET_PORT}}/schema-migration/report-status?uuid={{MIGRATION_UUID}}&status={{OnlineDDLStatusRunning}}&dryrun={{DRYRUN}}");
}
sub before_exit {
my($self, % args) = @_;
my $exit_status = $args{exit_status};
if ($exit_status == 0) {
get("http://localhost:{{VTTABLET_PORT}}/schema-migration/report-status?uuid={{MIGRATION_UUID}}&status={{OnlineDDLStatusComplete}}&dryrun={{DRYRUN}}");
} else {
get("http://localhost:{{VTTABLET_PORT}}/schema-migration/report-status?uuid={{MIGRATION_UUID}}&status={{OnlineDDLStatusFailed}}&dryrun={{DRYRUN}}");
}
}
sub get_slave_lag {
my ($self, %args) = @_;
return sub {
if (head("http://localhost:{{VTTABLET_PORT}}/throttler/check")) {
# Got HTTP 200 OK, means throttler is happy
return 0;
} else {
# Throttler requests to hold back
return 2147483647; # maxint, report *very* high lag
}
};
}
1;
`
pluginCode = strings.ReplaceAll(pluginCode, "{{VTTABLET_PORT}}", fmt.Sprintf("%d", *servenv.Port))
pluginCode = strings.ReplaceAll(pluginCode, "{{MIGRATION_UUID}}", onlineDDL.UUID)
pluginCode = strings.ReplaceAll(pluginCode, "{{OnlineDDLStatusRunning}}", string(schema.OnlineDDLStatusRunning))
pluginCode = strings.ReplaceAll(pluginCode, "{{OnlineDDLStatusComplete}}", string(schema.OnlineDDLStatusComplete))
pluginCode = strings.ReplaceAll(pluginCode, "{{OnlineDDLStatusFailed}}", string(schema.OnlineDDLStatusFailed))
// Validate pt-online-schema-change binary:
log.Infof("Will now validate pt-online-schema-change binary")
_, err = execCmd(
"bash",
[]string{
wrapperScriptFileName,
"--version",
},
os.Environ(),
"/tmp",
nil,
nil,
)
if err != nil {
log.Errorf("Error testing pt-online-schema-change binary: %+v", err)
return err
}
log.Infof("+ OK")
if err := e.updateMigrationLogPath(ctx, onlineDDL.UUID, mysqlHost, tempDir); err != nil {
return err
}
// Temporary hack (2020-08-11)
// Because sqlparser does not do full blown ALTER TABLE parsing,
// and because pt-online-schema-change requires only the table options part of the ALTER TABLE statement,
// we resort to regexp-based parsing of the query.
// TODO(shlomi): generate _alter options_ via sqlparser when it full supports ALTER TABLE syntax.
_, _, alterOptions := schema.ParseAlterTableOptions(onlineDDL.SQL)
// The following sleep() is temporary and artificial. Because we create a new user for this
// migration, and because we throttle by replicas, we need to wait for the replicas to be
// caught up with the new user creation. Otherwise, the OSC tools will fail connecting to the replicas...
// Once we have a built in throttling service , we will no longe rneed to have the OSC tools probe the
// replicas. Instead, they will consult with our throttling service.
// TODO(shlomi): replace/remove this when we have a proper throttling solution
time.Sleep(time.Second)
runPTOSC := func(execute bool) error {
os.Setenv("MYSQL_PWD", onlineDDLPassword)
newTableName := fmt.Sprintf("_%s_%s_new", onlineDDL.UUID, ReadableTimestamp())
if err := e.updateArtifacts(ctx, onlineDDL.UUID,
fmt.Sprintf("_%s_del", newTableName),
); err != nil {
return err
}
executeFlag := "--dry-run"
if execute {
executeFlag = "--execute"
}
finalPluginCode := strings.ReplaceAll(pluginCode, "{{DRYRUN}}", fmt.Sprintf("%t", !execute))
pluginFile, err := createTempScript(tempDir, "pt-online-schema-change-plugin", finalPluginCode)
if err != nil {
log.Errorf("Error creating script: %+v", err)
return err
}
args := []string{
wrapperScriptFileName,
`--pid`,
e.ptPidFileName(onlineDDL.UUID),
`--plugin`,
pluginFile,
`--new-table-name`,
newTableName,
`--alter`,
alterOptions,
`--check-slave-lag`, // We use primary's identity so that pt-online-schema-change calls our lag plugin for exactly 1 server
fmt.Sprintf(`h=%s,P=%d,D=%s,t=%s,u=%s`, mysqlHost, mysqlPort, e.dbName, onlineDDL.Table, onlineDDLUser),
executeFlag,
fmt.Sprintf(`h=%s,P=%d,D=%s,t=%s,u=%s`, mysqlHost, mysqlPort, e.dbName, onlineDDL.Table, onlineDDLUser),
}
if execute {
args = append(args,
`--no-drop-new-table`,
`--no-drop-old-table`,
)
}
args = append(args, strings.Fields(onlineDDL.Options)...)
_, err = execCmd("bash", args, os.Environ(), "/tmp", nil, nil)
return err
}
atomic.StoreInt64(&e.migrationRunning, 1)
e.lastMigrationUUID = onlineDDL.UUID
go func() error {
defer atomic.StoreInt64(&e.migrationRunning, 0)
defer e.dropOnlineDDLUser(ctx)
log.Infof("Will now dry-run pt-online-schema-change on: %s:%d", mysqlHost, mysqlPort)
if err := runPTOSC(false); err != nil {
// perhaps pt-osc was interrupted midway and didn't have the chance to send a "failes" status
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
_ = e.updateMigrationTimestamp(ctx, "completed_timestamp", onlineDDL.UUID)
log.Errorf("Error executing pt-online-schema-change dry run: %+v", err)
return err
}
log.Infof("+ OK")
log.Infof("Will now run pt-online-schema-change on: %s:%d", mysqlHost, mysqlPort)
startedMigrations.Add(1)
if err := runPTOSC(true); err != nil {
// perhaps pt-osc was interrupted midway and didn't have the chance to send a "failes" status
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
_ = e.updateMigrationTimestamp(ctx, "completed_timestamp", onlineDDL.UUID)
_ = e.dropPTOSCMigrationTriggers(ctx, onlineDDL)
failedMigrations.Add(1)
log.Errorf("Error running pt-online-schema-change: %+v", err)
return err
}
// Migration successful!
os.RemoveAll(tempDir)
successfulMigrations.Add(1)
log.Infof("+ OK")
return nil
}()
return nil
}
func (e *Executor) readMigration(ctx context.Context, uuid string) (onlineDDL *schema.OnlineDDL, err error) {
parsed := sqlparser.BuildParsedQuery(sqlSelectMigration, "_vt", ":migration_uuid")
bindVars := map[string]*querypb.BindVariable{
"migration_uuid": sqltypes.StringBindVariable(uuid),
}
bound, err := parsed.GenerateQuery(bindVars, nil)
if err != nil {
return onlineDDL, err
}
r, err := e.execQuery(ctx, bound)
if err != nil {
return onlineDDL, err
}
row := r.Named().Row()
if row == nil {
// No results
return nil, ErrMigrationNotFound
}
onlineDDL = &schema.OnlineDDL{
Keyspace: row["keyspace"].ToString(),
Table: row["mysql_table"].ToString(),
Schema: row["mysql_schema"].ToString(),
SQL: row["migration_statement"].ToString(),
UUID: row["migration_uuid"].ToString(),
Strategy: sqlparser.DDLStrategy(row["strategy"].ToString()),
Options: row["options"].ToString(),
Status: schema.OnlineDDLStatus(row["migration_status"].ToString()),
}
return onlineDDL, nil
}
// terminateMigration attempts to interrupt and hard-stop a running migration
func (e *Executor) terminateMigration(ctx context.Context, onlineDDL *schema.OnlineDDL, lastMigrationUUID string) (foundRunning bool, err error) {
if atomic.LoadInt64(&e.migrationRunning) > 0 {
// double check: is the running migration the very same one we wish to cancel?
if onlineDDL.UUID == lastMigrationUUID {
// assuming all goes well in next steps, we can already report that there has indeed been a migration
foundRunning = true
}
}
switch onlineDDL.Strategy {
case schema.DDLStrategyPTOSC:
// see if pt-osc is running (could have been executed by this vttablet or one that crashed in the past)
if running, pid, _ := e.isPTOSCMigrationRunning(ctx, onlineDDL.UUID); running {
foundRunning = true
// Because pt-osc doesn't offer much control, we take a brute force approach to killing it,
// revoking its privileges, and cleaning up its triggers.
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
return foundRunning, nil
}
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
return foundRunning, nil
}
if err := e.dropOnlineDDLUser(ctx); err != nil {
return foundRunning, nil
}
if err := e.dropPTOSCMigrationTriggers(ctx, onlineDDL); err != nil {
return foundRunning, nil
}
}
case schema.DDLStrategyGhost:
// gh-ost migrations are easy to kill: just touch their specific panic flag files. We trust
// gh-ost to terminate. No need to KILL it. And there's no trigger cleanup.
if err := e.createGhostPanicFlagFile(onlineDDL.UUID); err != nil {
return foundRunning, fmt.Errorf("Error cancelling migration, flag file error: %+v", err)
}
}
return foundRunning, nil
}
// cancelMigration attempts to abort a scheduled or a running migration
func (e *Executor) cancelMigration(ctx context.Context, uuid string, terminateRunningMigration bool) (result *sqltypes.Result, err error) {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()
var rowsAffected uint64
onlineDDL, err := e.readMigration(ctx, uuid)
if err != nil {
return nil, err
}
switch onlineDDL.Status {
case schema.OnlineDDLStatusQueued, schema.OnlineDDLStatusReady:
if err := e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusCancelled); err != nil {
return nil, err
}
rowsAffected = 1
}
if terminateRunningMigration {
migrationFound, err := e.terminateMigration(ctx, onlineDDL, e.lastMigrationUUID)
if migrationFound {
rowsAffected = 1
}
if err != nil {
return result, err
}
}
result = &sqltypes.Result{
RowsAffected: rowsAffected,
}
return result, nil
}
// scheduleNextMigration attemps to schedule a single migration to run next.
// possibly there's no migrations to run. Possibly there's a migration running right now,
// in which cases nothing happens.
func (e *Executor) scheduleNextMigration(ctx context.Context) error {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()
if atomic.LoadInt64(&e.migrationRunning) > 0 {
return ErrExecutorMigrationAlreadyRunning
}
{
parsed := sqlparser.BuildParsedQuery(sqlSelectCountReadyMigrations, "_vt")
r, err := e.execQuery(ctx, parsed.Query)
if err != nil {
return err
}
row := r.Named().Row()
countReady, err := row.ToInt64("count_ready")
if err != nil {
return err
}
if countReady > 0 {
// seems like there's already one migration that's good to go
return nil
}
} // Cool, seems like no migration is ready. Let's try and make a single 'queued' migration 'ready'
parsed := sqlparser.BuildParsedQuery(sqlScheduleSingleMigration, "_vt")
_, err := e.execQuery(ctx, parsed.Query)
return err
}
func (e *Executor) runNextMigration(ctx context.Context) error {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()
if atomic.LoadInt64(&e.migrationRunning) > 0 {
return ErrExecutorMigrationAlreadyRunning
}
parsed := sqlparser.BuildParsedQuery(sqlSelectReadyMigration, "_vt")
r, err := e.execQuery(ctx, parsed.Query)
if err != nil {
return err
}
named := r.Named()
for i, row := range named.Rows {
onlineDDL := &schema.OnlineDDL{
Keyspace: row["keyspace"].ToString(),
Table: row["mysql_table"].ToString(),
Schema: row["mysql_schema"].ToString(),
SQL: row["migration_statement"].ToString(),
UUID: row["migration_uuid"].ToString(),
Strategy: sqlparser.DDLStrategy(row["strategy"].ToString()),
Options: row["options"].ToString(),
Status: schema.OnlineDDLStatus(row["migration_status"].ToString()),
}
switch onlineDDL.Strategy {
case schema.DDLStrategyGhost:
go func() {
if err := e.ExecuteWithGhost(ctx, onlineDDL); err != nil {
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
}
}()
case schema.DDLStrategyPTOSC:
go func() {
if err := e.ExecuteWithPTOSC(ctx, onlineDDL); err != nil {
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
}
}()
default:
{
_ = e.updateMigrationStatus(ctx, onlineDDL.UUID, schema.OnlineDDLStatusFailed)
return fmt.Errorf("Unsupported strategy: %+v", onlineDDL.Strategy)
}
}
// the query should only ever return a single row at the most
// but let's make it also explicit here that we only run a single migration
if i == 0 {
break
}
}
return nil
}
// isPTOSCMigrationRunning sees if pt-online-schema-change is running a specific migration,
// by examining its PID file
func (e *Executor) isPTOSCMigrationRunning(ctx context.Context, uuid string) (isRunning bool, pid int, err error) {
// Try and read its PID file:
content, err := ioutil.ReadFile(e.ptPidFileName(uuid))
if err != nil {
// file probably does not exist (migration not running)
// or any other issue --> we can't confirm that the migration is actually running
return false, pid, err
}
contentString := strings.TrimSpace(string(content))
//
pid, err = strconv.Atoi(contentString)
if err != nil {
// can't get the PID right. Can't confirm migration is running.
return false, pid, err
}
p, err := os.FindProcess(pid)
if err != nil {
// can't find the process. Can't confirm migration is running.
return false, pid, err
}
err = p.Signal(syscall.Signal(0))
if err != nil {
// can't verify process is running. Can't confirm migration is running.
return false, pid, err
}
// AHA! We are able to confirm this pt-osc migration is actually running!
return true, pid, nil
}
// dropOnlineDDLUser drops the given ddl user account at the end of migration
func (e *Executor) dropPTOSCMigrationTriggers(ctx context.Context, onlineDDL *schema.OnlineDDL) error {
conn, err := dbconnpool.NewDBConnection(ctx, e.env.Config().DB.DbaConnector())
if err != nil {
return err
}
defer conn.Close()
parsed := sqlparser.BuildParsedQuery(sqlSelectPTOSCMigrationTriggers, ":mysql_schema", ":mysql_table")
bindVars := map[string]*querypb.BindVariable{
"mysql_schema": sqltypes.StringBindVariable(onlineDDL.Schema),
"mysql_table": sqltypes.StringBindVariable(onlineDDL.Table),
}
bound, err := parsed.GenerateQuery(bindVars, nil)
if err != nil {
return err
}
r, err := e.execQuery(ctx, bound)
if err != nil {
return err
}
for _, row := range r.Named().Rows {
// iterate pt-osc triggers and drop them
triggerSchema := row.AsString("trigger_schema", "")
triggerName := row.AsString("trigger_name", "")
dropParsed := sqlparser.BuildParsedQuery(sqlDropTrigger, triggerSchema, triggerName)
if _, err := conn.ExecuteFetch(dropParsed.Query, 0, false); err != nil {
return err
}
}
return err
}
// reviewRunningMigrations iterates migrations in 'running' state (there really should just be one that is
// actually running).
func (e *Executor) reviewRunningMigrations(ctx context.Context) (countRunnning int, err error) {
parsed := sqlparser.BuildParsedQuery(sqlSelectRunningMigrations, "_vt", ":strategy")
bindVars := map[string]*querypb.BindVariable{
"strategy": sqltypes.StringBindVariable(string(schema.DDLStrategyPTOSC)),
}
bound, err := parsed.GenerateQuery(bindVars, nil)
if err != nil {
return countRunnning, err
}
r, err := e.execQuery(ctx, bound)
if err != nil {