-
Notifications
You must be signed in to change notification settings - Fork 54
/
runner.go
1189 lines (1055 loc) · 39.3 KB
/
runner.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
// Pick up and run migration jobs.
package runner
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/square/shift/runner/pkg/migration"
"github.com/square/shift/runner/pkg/rest"
"github.com/square/shift/runner/Godeps/_workspace/src/github.com/golang/glog"
"github.com/square/shift/runner/Godeps/_workspace/src/gopkg.in/yaml.v2"
)
const (
// how often pt-osc should print out copy % during copy step
ptOscProgress = "time,5"
SIGKILLSignal = "killed"
LOG_FILE_TYPE = "0"
STATE_FILE_TYPE = "1"
)
var (
statusesToRun = []int{migration.PrepMigrationStatus, migration.RunMigrationStatus,
migration.RenameTablesStatus, migration.CancelStatus, migration.PauseStatus}
// track pt-osc execs, which may need to be canceled/killed
runningMigrations = map[int]int{} // {id: pid}
runningMigMutex = &sync.Mutex{}
ptOscKillSignal = syscall.SIGKILL
canPickupMigrations = false
unstageMigrationMutex = &sync.Mutex{}
fileSyncWaitGroup sync.WaitGroup
unstagedMigrationsWaitGroup sync.WaitGroup
// runner methods
failMigration = (*runner).failMigration
prepMigrationStep = (*runner).prepMigrationStep
runMigrationStep = (*runner).runMigrationStep
runMigrationDirect = (*runner).runMigrationDirect
runMigrationDirectDrop = (*runner).runMigrationDirectDrop
runMigrationPtOsc = (*runner).runMigrationPtOsc
renameTablesStep = (*runner).renameTablesStep
pauseMigrationStep = (*runner).pauseMigrationStep
unstageRunnableMigration = (*runner).unstageRunnableMigration
execPtOsc = (*runner).execPtOsc
killMigration = (*runner).killMigration
killPtOsc = killPtOscProcess
killPtOscById = killPtOscProcessById
// migration methods
SetupDbClient = (*migration.Migration).SetupDbClient
CollectTableStats = (*migration.Migration).CollectTableStats
ValidateFinalInsert = (*migration.Migration).ValidateFinalInsert
DryRunCreatesNew = (*migration.Migration).DryRunCreatesNew
DropTriggers = (*migration.Migration).DropTriggers
DirectDrop = (*migration.Migration).DirectDrop
SwapOscTables = (*migration.Migration).SwapOscTables
MoveToPendingDrops = (*migration.Migration).MoveToPendingDrops
MoveToBlackHole = (*migration.Migration).MoveToBlackHole
CleanUp = (*migration.Migration).CleanUp
RunWriteQuery = (*migration.Migration).RunWriteQuery
// define errors
ErrInvalidMigration = errors.New("runner: invalid migration")
ErrNotRunning = errors.New("runner: can't pause or cancel migration that's not running on this host")
ErrFiles = errors.New("runner: error creating and retrieving files")
ErrPtOscExec = errors.New("runner: error exec-ing pt-osc")
ErrPtOscKill = errors.New("runner: error killing pt-osc")
ErrPtOscCleanUp = errors.New("runner: error cleaning up after pt-osc (the process was killed though)")
ErrGeneral = errors.New("runner: there was an error with the runner")
ErrUnexpectedExit = errors.New("runner: pt-osc died for an unexpected reason")
ErrUnkownStatus = errors.New("runner: unkown status on the migration")
)
type runner struct {
RestClient rest.RestClient
RestApi string `yaml:"rest_api"`
RestCert string `yaml:"rest_cert"`
RestKey string `yaml:"rest_key"`
MysqlUser string `yaml:"mysql_user"`
MysqlPassword string `yaml:"mysql_password"`
MysqlCert string `yaml:"mysql_cert"`
MysqlKey string `yaml:"mysql_key"`
MysqlRootCA string `yaml:"mysql_rootCA"`
MysqlDefaultsFile string `yaml:"mysql_defaults_file"`
LogDir string `yaml:"log_dir"`
PendingDropsDb string `yaml:"pending_drops_db"`
EnableTrash bool `yaml:"enable_trash"`
PtOscPath string `yaml:"pt_osc_path"`
HostOverride string `yaml:"host_override"`
PortOverride int `yaml:"port_override"`
DatabaseOverride string `yaml:"database_override"`
Hostname string
LogSyncInterval int `yaml:"log_sync_interval"`
StateSyncInterval int `yaml:"state_sync_interval"`
StopFilePath string `yaml:"stop_file_path"`
}
type TableStats struct {
tableRows string
tableSize string
indexSize string
}
func intInArray(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// maybeReplaceHostname replaces "%hostname%" in a string with os.Hostname.
// This is primarily used to insert the hostname into file paths that are
// specified in confing files.
func maybeReplaceHostname(a string) string {
hostnameRegex := regexp.MustCompile("%hostname%")
hostname, err := os.Hostname()
if err != nil {
return a
}
return hostnameRegex.ReplaceAllString(a, hostname)
}
func newRunner(configFile string) (*runner, error) {
runner := &runner{}
// load up config file into runner struct
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
data, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &runner)
if err != nil {
return nil, err
}
}
restClient, err := rest.New(runner.RestApi, maybeReplaceHostname(runner.RestCert),
maybeReplaceHostname(runner.RestKey))
if err != nil {
return nil, err
}
runner.Hostname, err = os.Hostname()
if err != nil {
return nil, err
}
runner.RestClient = restClient
return runner, nil
}
// collect and unstage new migrations, and pass them through the job channel.
// loop every 10-20 seconds.
func (runner *runner) sendRunnableMigrationsToProcessor(jobChannel chan *migration.Migration) {
for {
unstageMigrationMutex.Lock()
if canPickupMigrations {
newMigrations := runner.getRunnableMigrations()
for i := range newMigrations {
// Pass each new, unstaged migration through the job channel.
unstagedMigrationsWaitGroup.Add(1)
jobChannel <- newMigrations[i]
}
}
unstageMigrationMutex.Unlock()
// random sleep time to help load balance
time.Sleep(time.Duration(rand.Intn(10)+10) * time.Second)
}
}
// watchForStopFile watches for the existence of a stop file. Will kill and offer up migrations if found
// loop every 1 seconds
func (runner *runner) watchForStopFile() {
for {
if _, err := os.Stat(runner.StopFilePath); err == nil {
glog.Infof("Stop file found (%s), killing...", runner.StopFilePath)
unstageMigrationMutex.Lock()
canPickupMigrations = false
unstagedMigrationsWaitGroup.Wait()
unstageMigrationMutex.Unlock()
runner.killAndOfferMigrations()
} else {
canPickupMigrations = true
}
time.Sleep(1 * time.Second)
}
}
// Kills the ptosc job and sends migration through api to be picked up by other hosts
func (runner *runner) killAndOfferMigrations() {
if len(runningMigrations) > 0 {
killedMigrations := killAllPtOscProcesses()
glog.Infof("Done killing, waiting for file sync gorountines to finish")
fileSyncWaitGroup.Wait() // wait for all file syncing goroutines to finish
glog.Infof("All file syncing gorountines are finished, proceeding...")
for _, migrationId := range killedMigrations {
glog.Infof("Offering migration mig_id=%v", migrationId)
urlParams := map[string]string{"id": strconv.Itoa(migrationId)}
_, err := runner.RestClient.Offer(urlParams)
if err != nil {
glog.Error(err)
}
}
glog.Infof("Offered all killed migrations")
}
}
// getRunnableMigrations queries the shift api to get new/updated migrations.
func (runner *runner) getRunnableMigrations() []*migration.Migration {
newMigrations := []*migration.Migration{}
// Get all of the staged migrations
glog.Info("Getting staged migrations.")
stagedMigrations, err := runner.RestClient.Staged()
if err != nil {
glog.Errorf("Failed to get staged migrations (error: %s)", err.Error())
return newMigrations
}
if len(stagedMigrations) == 0 {
glog.Info("No new staged migrations.")
}
// For each staged migration, try to unstage it.
for i := range stagedMigrations {
newMigration, err := unstageRunnableMigration(runner, stagedMigrations[i])
if err != nil {
glog.Errorf(err.Error())
continue
}
if newMigration != nil {
newMigrations = append(newMigrations, newMigration)
}
}
return newMigrations
}
// unstageRunnableMigration tries to unstage ("claim") migrations that are runnable
func (runner *runner) unstageRunnableMigration(currentMigration rest.RestResponseItem) (*migration.Migration, error) {
migrationStatusField, ok := currentMigration["status"].(float64)
if ok == false {
glog.Errorf("Failed to get migration status for the following migration: %v", currentMigration)
return nil, ErrInvalidMigration
}
migrationStatus := int(migrationStatusField)
// Manually define the statuses to run
if intInArray(migrationStatus, statusesToRun) {
glog.Infof("Status = %d is in the list of statuses to run, so continuing.", migrationStatus)
migrationIdFieldFloat, ok := currentMigration["id"].(float64)
if ok == false {
glog.Errorf("Failed to get migration id for the "+
"following migration: %v", currentMigration)
return nil, ErrInvalidMigration
}
migrationIdField := int(migrationIdFieldFloat)
// check if the migration is already pinned to a particular host
pinnedHostname := currentMigration["run_host"]
if pinnedHostname != nil {
pinnedHostname = pinnedHostname.(string)
if pinnedHostname != runner.Hostname {
glog.Errorf("mig_id=%d: migration already pinned to %s. Skipping.",
migrationIdField, pinnedHostname)
return nil, ErrNotRunning
}
}
customOptions := make(map[string]string)
if val, ok := currentMigration["custom_options"]; ok && val != nil {
json.Unmarshal([]byte(currentMigration["custom_options"].(string)), &customOptions)
}
// log and statefiles are stored in log directory. ex for mig with id 7: /path/to/logs/statefile-id-7.txt
filesDir := runner.LogDir + "id-" + strconv.Itoa(int(migrationIdField)) + "/"
stateFile := filesDir + "statefile.txt"
logFile := filesDir + "ptosc-output.log"
mig := &migration.Migration{
Id: migrationIdField,
Status: migrationStatus,
Host: currentMigration["host"].(string),
Port: int(currentMigration["port"].(float64)),
Database: currentMigration["database"].(string),
Table: currentMigration["table"].(string),
DdlStatement: currentMigration["ddl_statement"].(string),
FinalInsert: currentMigration["final_insert"].(string),
FilesDir: filesDir,
StateFile: stateFile,
LogFile: logFile,
PendingDropsDb: runner.PendingDropsDb,
EnableTrash: runner.EnableTrash,
CustomOptions: customOptions,
}
// some extra fields when we're not killing a migration
if (migrationStatus != migration.CancelStatus) && (migrationStatus != migration.PauseStatus) {
mig.RunType = int(currentMigration["runtype"].(float64))
mig.Mode = int(currentMigration["mode"].(float64))
mig.Action = int(currentMigration["action"].(float64))
}
// allow setting overrides for the host and db from config file.
// this is useful for safe testing in a staging environment
if runner.HostOverride != "" {
mig.Host = runner.HostOverride
}
if runner.PortOverride != 0 {
mig.Port = runner.PortOverride
}
if runner.DatabaseOverride != "" {
mig.Database = runner.DatabaseOverride
}
// if pending_drops database isn't defined, set it to be the same
// database that the migration is on.
if mig.PendingDropsDb == "" {
mig.PendingDropsDb = mig.Database
}
// only claim the migration if we successfully unstage it
urlParams := map[string]string{"id": strconv.Itoa(mig.Id)}
_, err := runner.RestClient.Unstage(urlParams)
if err != nil {
return nil, err
}
glog.Infof("mig_id=%d: Successfully unstaged.", mig.Id)
return mig, nil
}
glog.Infof("Status = %d is not in the list of statuses to run, so skipping.", migrationStatus)
return nil, nil
}
// failMigration reports a failed migration to the shift database/ui.
func (runner *runner) failMigration(migration *migration.Migration, errorMsg string) {
glog.Errorf("mig_id=%d: %s.", migration.Id, errorMsg)
urlParams := map[string]string{
"id": strconv.Itoa(migration.Id),
"error_message": errorMsg,
}
_, err := runner.RestClient.Fail(urlParams)
if err != nil {
glog.Errorf("mig_id=%d: %s.", migration.Id, err)
return
}
glog.Infof("mig_id=%d: Successfully failed migration.", migration.Id)
return
}
// processMigration takes migrations from the job channel and decides
// what to do with them (ex: run them, kill them, etc.).
func (runner *runner) processMigration(currentMigration *migration.Migration) {
glog.Infof("mig_id=%d: Picked up migration from the job channel. Processing.", currentMigration.Id)
if currentMigration.Status != migration.RunMigrationStatus {
// excludes RunMigrationStatus because we want to .Done those after
// we start PtOsc
defer unstagedMigrationsWaitGroup.Done()
}
// setup a database client for the migration
err := SetupDbClient(currentMigration, runner.MysqlUser, runner.MysqlPassword,
maybeReplaceHostname(runner.MysqlCert), maybeReplaceHostname(runner.MysqlKey),
maybeReplaceHostname(runner.MysqlRootCA), currentMigration.Port)
if err != nil {
failMigration(runner, currentMigration, err.Error())
return
}
if currentMigration.DbClient != nil {
defer currentMigration.DbClient.Close()
}
switch currentMigration.Status {
case migration.PrepMigrationStatus:
err = prepMigrationStep(runner, currentMigration)
case migration.RunMigrationStatus:
err = runMigrationStep(runner, currentMigration)
case migration.RenameTablesStatus:
err = renameTablesStep(runner, currentMigration)
case migration.PauseStatus:
err = pauseMigrationStep(runner, currentMigration)
case migration.CancelStatus:
err = killMigration(runner, currentMigration)
default:
err = ErrUnkownStatus
}
if err != nil {
failMigration(runner, currentMigration, err.Error())
}
}
// prepMigrationStep collects the table stats for a migration, sends them
// to the shift api, and moves the migration to the next step.
func (runner *runner) prepMigrationStep(currentMigration *migration.Migration) error {
// validate the final insert statement.
if currentMigration.FinalInsert != "" {
err := ValidateFinalInsert(currentMigration)
if err != nil {
return err
}
}
// only do a dry-run if the ddl should be run with pt-osc
if currentMigration.RunType != migration.SHORT_RUN {
// run a dry-run of pt-osc and make sure there were no errors.
// this will also validate the ddl statement.
var copyPercentChan chan int
_, err := execPtOsc(runner, currentMigration, runner.generatePtOscCommand, copyPercentChan, true)
if err != nil {
return err
}
}
var urlParams map[string]string
migrationId := strconv.Itoa(currentMigration.Id)
// if the migration is creating a new table/view, verify it doesn't
// already exist and then create/drop it to validate the ddl.
// if the migration is not creating a new table/view, just collect
// table stats.
if currentMigration.Action == migration.CREATE_ACTION {
err := DryRunCreatesNew(currentMigration)
if err != nil {
return err
}
} else {
tableStatsStart, err := CollectTableStats(currentMigration)
if err != nil {
return err
}
// send the table stats to the api
urlParams = map[string]string{
"id": migrationId,
"table_rows_start": tableStatsStart.TableRows,
"table_size_start": tableStatsStart.TableSize,
"index_size_start": tableStatsStart.IndexSize,
}
_, err = runner.RestClient.Update(urlParams)
if err != nil {
return err
}
}
// move the migration to the next step
urlParams = map[string]string{"id": migrationId}
_, err := runner.RestClient.NextStep(urlParams)
if err != nil {
return err
}
return nil
}
// runMigrationStep actually runs a migration. Based on the ddl statement, it
// either runs it directly against the database, or with the ptosc tool
func (runner *runner) runMigrationStep(currentMigration *migration.Migration) (err error) {
if currentMigration.RunType == migration.SHORT_RUN {
defer unstagedMigrationsWaitGroup.Done()
if currentMigration.Action == migration.DROP_ACTION {
return runMigrationDirectDrop(runner, currentMigration)
} else {
return runMigrationDirect(runner, currentMigration)
}
} else {
return runMigrationPtOsc(runner, currentMigration)
}
}
// runMigrationDirect runs a migration query directly against the database.
// It then does all the remaining steps to fully complete the migration.
func (runner *runner) runMigrationDirect(currentMigration *migration.Migration) (err error) {
// run the migration directly against the database
err = RunWriteQuery(currentMigration, currentMigration.DdlStatement)
if err != nil {
return
}
// run the final insert
if currentMigration.FinalInsert != "" {
err = RunWriteQuery(currentMigration, currentMigration.FinalInsert)
if err != nil {
return
}
}
// complete the migration
urlParams := map[string]string{"id": strconv.Itoa(currentMigration.Id)}
_, err = runner.RestClient.Complete(urlParams)
if err != nil {
return
}
return
}
// runMigrationDirectDrop runs a DROP or RENAME query directly against the database.
// It then does all the remaining steps to fully complete the migration.
func (runner *runner) runMigrationDirectDrop(currentMigration *migration.Migration) (err error) {
// drop triggers that reference the table
err = DropTriggers(currentMigration, currentMigration.Table)
if err != nil {
return
}
// views can't be renamed to a different database. if the mig is for a view,
// drop it. if the mig is for a table, move it to _pending_drops
if currentMigration.Mode == migration.VIEW_MODE {
err = DirectDrop(currentMigration)
if err != nil {
return
}
} else {
if currentMigration.EnableTrash {
// rename old table to the pending_drops database instead of actually
// dropping it
pdTable := migration.TimestampedTable(currentMigration.Table)
err = MoveToPendingDrops(currentMigration, currentMigration.Table, pdTable)
if err != nil {
return
}
} else {
err = DirectDrop(currentMigration)
if err != nil {
return
}
}
}
// run the final insert
if currentMigration.FinalInsert != "" {
err = RunWriteQuery(currentMigration, currentMigration.FinalInsert)
if err != nil {
return
}
}
// complete the migration
urlParams := map[string]string{"id": strconv.Itoa(currentMigration.Id)}
_, err = runner.RestClient.Complete(urlParams)
if err != nil {
return
}
return
}
// runMigrationPtOsc runs the pt-osc tool for the main copy step and
// moves the migration to the next step if there were no problems.
func (runner *runner) runMigrationPtOsc(currentMigration *migration.Migration) (err error) {
// pin the migration to this host
migrationId := strconv.Itoa(currentMigration.Id)
urlParams := map[string]string{
"id": migrationId,
"run_host": runner.Hostname,
}
_, err = runner.RestClient.Update(urlParams)
if err != nil {
unstagedMigrationsWaitGroup.Done()
return
}
defer runner.RestClient.UnpinRunHost(map[string]string{"id": migrationId})
copyPercentChan := make(chan int)
canceled, err := execPtOsc(runner, currentMigration, runner.generatePtOscCommand, copyPercentChan, false)
if err != nil {
// if pt-osc ran into an error, move the migration to the "error" step instead
// of failing it. from the "error" step we will have the ability to try and
// resume the migration, or clean it up
urlParams := map[string]string{
"id": strconv.Itoa(currentMigration.Id),
"error_message": err.Error(),
}
_, err = runner.RestClient.Error(urlParams)
if err != nil {
return
}
return nil
}
// move it to the next step if it wasn't canceled
if !canceled {
urlParams := map[string]string{"id": strconv.Itoa(currentMigration.Id)}
_, err = runner.RestClient.NextStep(urlParams)
if err != nil {
return
}
}
return
}
// renameTablesStep collects the final table stats for a migration, sends them
// to the shift api, and moves the migration to the next step.
func (runner *runner) renameTablesStep(currentMigration *migration.Migration) error {
// get the lastest state file
err := runner.createAndRetrieveFiles(currentMigration)
if err != nil {
return err
}
// the next few steps swap the tables and move the old table to the pending_drops database,
// where a job will drop the table after a certain amount of time (i.e.,
// we don't have to worry about it).
// do the rename
oldTable, err := SwapOscTables(currentMigration)
if err != nil {
return err
}
// drop the triggers from the old table
err = DropTriggers(currentMigration, oldTable)
if err != nil {
return err
}
if currentMigration.EnableTrash {
// rename old table to pending_drops database.
// if the pending_drops database is the same as the database for the
// migration, skip this step.
if currentMigration.Database != currentMigration.PendingDropsDb {
err = MoveToPendingDrops(currentMigration, oldTable, oldTable)
if err != nil {
return err
}
}
} else {
// trash disabled, just drop oldtable
err = MoveToBlackHole(currentMigration, oldTable)
if err != nil {
return err
}
}
// get the table stats
tableStatsEnd, err := CollectTableStats(currentMigration)
if err != nil {
return err
}
// send the table stats to the api
urlParams := map[string]string{
"id": strconv.Itoa(currentMigration.Id),
"table_rows_end": tableStatsEnd.TableRows,
"table_size_end": tableStatsEnd.TableSize,
"index_size_end": tableStatsEnd.IndexSize,
}
_, err = runner.RestClient.Update(urlParams)
if err != nil {
return err
}
// run the final insert
if currentMigration.FinalInsert != "" {
err = RunWriteQuery(currentMigration, currentMigration.FinalInsert)
if err != nil {
return err
}
}
// complete the migration
urlParams = map[string]string{"id": strconv.Itoa(currentMigration.Id)}
_, err = runner.RestClient.Complete(urlParams)
return err
}
// killPtOscProcess sends a SIGKILL to pt-osc if it is running
func killPtOscProcess(currentMigration *migration.Migration) error {
runningMigMutex.Lock()
defer runningMigMutex.Unlock()
// kill pt-osc if it's running on this host
if _, exists := runningMigrations[currentMigration.Id]; exists {
err := killPtOscProcessById(currentMigration.Id)
if err != nil {
return err
}
delete(runningMigrations, currentMigration.Id)
} else {
glog.Infof("mig_id=%d: can't kill because it's not running.", currentMigration.Id)
}
return nil
}
// killAllPtOscProcesses sends a SIGKILL to all pt-osc proccess associated
// with running migrations, and returns a list of migration ids of killed processes
func killAllPtOscProcesses() []int {
killedMigrations := make([]int, 0)
runningMigMutex.Lock()
defer runningMigMutex.Unlock()
for migrationId, _ := range runningMigrations {
err := killPtOscById(migrationId)
delete(runningMigrations, migrationId)
if err != nil {
glog.Error(err)
continue
}
killedMigrations = append(killedMigrations, migrationId)
}
return killedMigrations
}
// killPtOscProcess sends a SIGKILL to the pt-osc process associated
// with the migrationId, does not lock runningMigMutex
func killPtOscProcessById(migrationId int) error {
pid := runningMigrations[migrationId]
glog.Infof("mig_id=%d: killing (pid = %d).", migrationId, pid)
err := syscall.Kill(pid, ptOscKillSignal)
if err != nil {
glog.Errorf("mig_id=%d: error killing pt-osc (error: %s)", migrationId, err)
return ErrPtOscKill
}
delete(runningMigrations, migrationId)
return nil
}
// pauseMigration kills pt-osc if it's running on this host, and bumps the status
// of the migration
func (runner *runner) pauseMigrationStep(currentMigration *migration.Migration) error {
err := killPtOsc(currentMigration)
if err != nil {
return err
}
runner.postStateFile(currentMigration)
// move the migration to the next step
urlParams := map[string]string{"id": strconv.Itoa(currentMigration.Id)}
_, err = runner.RestClient.NextStep(urlParams)
if err != nil {
return err
}
return nil
}
// killMigration kills pt-osc and cleans up after a migration
func (runner *runner) killMigration(currentMigration *migration.Migration) error {
err := killPtOsc(currentMigration)
if err != nil {
return err
}
err = runner.createAndRetrieveFiles(currentMigration)
if err != nil {
return err
}
// we want to clean up the migration (drop triggers, shadow table, etc.) regardless of
// whether or not the migration is running on this host (or running at all)
glog.Infof("mig_id=%d: cleaning up.", currentMigration.Id)
err = CleanUp(currentMigration)
if err != nil {
glog.Errorf("mig_id=%d: error cleaning up (error: %s)", currentMigration.Id, err)
return ErrPtOscCleanUp
}
return nil
}
// setupLogWriter configures a writer pointed at the pt-osc log file we want
func setupLogWriter(logFilePath string) (*os.File, *bufio.Writer, error) {
var logFile *os.File
if _, err := os.Stat(logFilePath); err == nil {
logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return nil, nil, ErrGeneral
}
} else {
logFile, err = os.Create(logFilePath)
}
return logFile, bufio.NewWriter(logFile), nil
}
// function type for writing to a writer
type writeToWriter func(*bufio.Writer, string, time.Time) error
// writeToPtOscLog takes log lines it receives from a channel and passes them
// to a function that will write them
// Also writes to a buffer which is POSTed to append_to_file when a new log line appears
// after LogSyncInterval seconds
func (runner *runner) writeToPtOscLog(ptOscLogWriter *bufio.Writer, ptOscLogChan chan string, writerFunc writeToWriter, migrationId int, waitGroup *sync.WaitGroup) {
defer fileSyncWaitGroup.Done()
defer waitGroup.Done()
var buffer bytes.Buffer
lastPostTime := time.Unix(0, 0)
for line := range ptOscLogChan {
currentTime := time.Now().Local()
formattedLine := "[" + currentTime.Format("2006-01-02 15:04:05") + "] " + line
err := writerFunc(ptOscLogWriter, formattedLine, currentTime)
if err != nil {
glog.Errorf("Error flushing pt-osc log file (error: %s)", err)
}
_, err = buffer.WriteString(formattedLine + "\n")
if err != nil {
glog.Errorf("Error writing to log buffer (error: %s)", err)
}
if int(currentTime.Sub(lastPostTime)/time.Second) >= runner.LogSyncInterval && buffer.Len() > 0 {
err = runner.postLogToFileApi(&buffer, migrationId)
if err == nil {
lastPostTime = currentTime
}
}
}
glog.Infof("Stopped syncing log file")
runner.postLogToFileApi(&buffer, migrationId)
}
// postLogToFileApi POSTs the lines in buffer to append_to_file as a log file then clears the buffer
func (runner *runner) postLogToFileApi(buffer *bytes.Buffer, migrationId int) error {
if buffer.Len() == 0 {
return nil
}
urlParams := make(map[string]string)
urlParams["migration_id"] = strconv.Itoa(migrationId)
urlParams["file_type"] = LOG_FILE_TYPE
urlParams["contents"] = buffer.String()
_, err := runner.RestClient.AppendToFile(urlParams)
if err != nil {
glog.Errorf("Error POSTing to append_to_file endpoint (error: %s)", err)
return err
}
buffer.Reset()
glog.Infof("Sent log lines to append_to_file endpoint")
return nil
}
// writeLineToPtOscLog writes a single line to a log file
func writeLineToPtOscLog(ptOscLogWriter *bufio.Writer, logMsg string, currentTime time.Time) error {
glog.Infof("pt-osc output: %s", logMsg)
fmt.Fprintln(ptOscLogWriter, logMsg)
return ptOscLogWriter.Flush()
}
// syncStateFile periodically reads a migration's statefile and sends it to the api
func (runner *runner) syncStateFile(migration *migration.Migration, quit chan string, waitGroup *sync.WaitGroup) {
defer fileSyncWaitGroup.Done()
defer waitGroup.Done()
lastPostTime := time.Unix(0, 0)
for {
currentTime := time.Now().Local()
select {
case <-quit:
glog.Infof("Stopped syncing state file")
runner.postStateFile(migration)
return
default:
if int(currentTime.Sub(lastPostTime)/time.Second) >= runner.StateSyncInterval {
lastPostTime = currentTime
runner.postStateFile(migration)
}
time.Sleep(1 * time.Second)
}
}
}
func (runner *runner) postStateFile(migration *migration.Migration) {
data, err := migration.ReadStateFile()
if err != nil {
glog.Warningf("Could not read state file (%s)", err)
} else {
urlParams := make(map[string]string)
urlParams["migration_id"] = strconv.Itoa(migration.Id)
urlParams["file_type"] = STATE_FILE_TYPE
urlParams["contents"] = string(data[:])
_, err = runner.RestClient.WriteFile(urlParams)
if err != nil {
glog.Errorf("Error POSTing to write_file endpoint (error: %s)", err)
} else {
glog.Infof("Sent state file to write_file endpoint")
}
}
}
// function type for generating exec command options
type commandOptionGenerator func(*migration.Migration) (commandOptions []string)
// generatePtOscCommand generates the options for running the pt-osc command.
// there are different options for different steps
func (runner *runner) generatePtOscCommand(currentMigration *migration.Migration) (commandOptions []string) {
// turn "Alter table t1 add column...." into "add column..."
alterPrefixRegex := regexp.MustCompile("^(?i)(ALTER\\s+TABLE\\s+.*?\\s+)")
alterStatement := alterPrefixRegex.ReplaceAllLiteralString(currentMigration.DdlStatement, "")
// set options that don't exist with the defaults
var customOptions map[string]string
defaultOptions := map[string]string{
"max_threads_running": "200",
"max_replication_lag": "1",
"config_path": "",
"recursion_method": "",
}
if currentMigration.CustomOptions == nil {
customOptions = defaultOptions
} else {
customOptions = currentMigration.CustomOptions
for k, v := range defaultOptions {
if _, ok := customOptions[k]; !ok {
customOptions[k] = v
}
}
}
dsn := fmt.Sprintf("D=%s,t=%s", currentMigration.Database, currentMigration.Table)
commandOptions = make([]string, 0)
if currentMigration.Status == migration.PrepMigrationStatus {
commandOptions = append(commandOptions, "--alter", alterStatement, "--dry-run",
"-h", currentMigration.Host, "-P", strconv.Itoa(currentMigration.Port),
"--defaults-file", runner.MysqlDefaultsFile, dsn)
} else if currentMigration.Status == migration.RunMigrationStatus {
// specify config file if it is defined. Options specified via command line will overwrite
// ones specified in the config file
if len(customOptions["config_path"]) > 0 {
commandOptions = append(commandOptions, "--config", customOptions["config_path"])
} else {
commandOptions = append(commandOptions,
"--max-load", "Threads_running=125",
"--critical-load", "Threads_running="+customOptions["max_threads_running"],
"--tries", "create_triggers:200:1,copy_rows:10000:1",
"--max-lag", customOptions["max_replication_lag"],
"--set-vars", "wait_timeout=600,lock_wait_timeout=1")
}
commandOptions = append(commandOptions, "--alter", alterStatement, "--execute",
"-h", currentMigration.Host, "-P", strconv.Itoa(currentMigration.Port),
"--defaults-file", runner.MysqlDefaultsFile, "--progress", ptOscProgress, "--exit-at", "copy",
"--save-state", currentMigration.StateFile)
// if the statefile already exists, we must be resuming the migration. load
// up the previous state
if _, err := os.Stat(currentMigration.StateFile); err == nil {
commandOptions = append(commandOptions, "--load-state", currentMigration.StateFile)
}
if currentMigration.RunType == migration.NOCHECKALTER_RUN {
commandOptions = append(commandOptions, "--nocheck-alter")
}
if len(customOptions["recursion_method"]) > 0 {
commandOptions = append(commandOptions, "--recursion-method", customOptions["recursion_method"])
}
commandOptions = append(commandOptions, dsn)
}
return
}
// createAndRetrieveFiles attempts to create a migration's state file,
// if it does not exist, and retrieve the latest version from the api
func (runner *runner) createAndRetrieveFiles(currentMigration *migration.Migration) error {
if _, err := os.Stat(currentMigration.FilesDir); err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(currentMigration.FilesDir, 0777)
if err != nil {
glog.Errorf("mig_id=%d: error creating pt-osc log directory '%s' (error: %s)", currentMigration.Id, currentMigration.FilesDir, err)
return ErrFiles
}
} else {
glog.Errorf("mig_id=%d: error stat-ing pt-osc log directory '%s' (error: %s)", currentMigration.Id, currentMigration.FilesDir, err)
return ErrFiles
}
}
// try to retrieve a statefile from the api
urlParams := map[string]string{
"migration_id": strconv.Itoa(currentMigration.Id),
"file_type": STATE_FILE_TYPE,
}
stateFile, restErr := runner.RestClient.GetFile(urlParams)
if restErr == nil && stateFile["contents"] != nil && len(stateFile["contents"].(string)) > 0 {
err := currentMigration.WriteStateFile([]byte(stateFile["contents"].(string)))
if err != nil {
glog.Errorf("mig_id=%d: error writing to statefile '%s' (error: %s)", currentMigration.Id, currentMigration.StateFile, err)
return ErrFiles