/
cli.go
1493 lines (1446 loc) · 54.7 KB
/
cli.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 2014 Outbrain 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 app
import (
"fmt"
"net"
"os"
"os/user"
"regexp"
"sort"
"strings"
"time"
"vitess.io/vitess/go/vt/orchestrator/config"
"vitess.io/vitess/go/vt/orchestrator/external/golib/log"
"vitess.io/vitess/go/vt/orchestrator/external/golib/util"
"vitess.io/vitess/go/vt/orchestrator/inst"
"vitess.io/vitess/go/vt/orchestrator/kv"
"vitess.io/vitess/go/vt/orchestrator/logic"
"vitess.io/vitess/go/vt/orchestrator/process"
)
var thisInstanceKey *inst.InstanceKey
var knownCommands []CliCommand
type CliCommand struct {
Command string
Section string
Description string
}
type stringSlice []string
func (a stringSlice) Len() int { return len(a) }
func (a stringSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a stringSlice) Less(i, j int) bool { return a[i] < a[j] }
var commandSynonyms = map[string]string{
"stop-slave": "stop-replica",
"start-slave": "start-replica",
"restart-slave": "restart-replica",
"reset-slave": "reset-replica",
"restart-slave-statements": "restart-replica-statements",
"relocate-slaves": "relocate-replicas",
"regroup-slaves": "regroup-replicas",
"move-up-slaves": "move-up-replicas",
"repoint-slaves": "repoint-replicas",
"enslave-siblings": "take-siblings",
"enslave-master": "take-master",
"get-candidate-slave": "get-candidate-replica",
"move-slaves-gtid": "move-replicas-gtid",
"regroup-slaves-gtid": "regroup-replicas-gtid",
"which-cluster-osc-slaves": "which-cluster-osc-replicas",
"which-cluster-gh-ost-slaves": "which-cluster-gh-ost-replicas",
"which-slaves": "which-replicas",
"detach-slave": "detach-replica-master-host",
"detach-replica": "detach-replica-master-host",
"detach-slave-master-host": "detach-replica-master-host",
"reattach-slave": "reattach-replica-master-host",
"reattach-replica": "reattach-replica-master-host",
"reattach-slave-master-host": "reattach-replica-master-host",
}
func registerCliCommand(command string, section string, description string) string {
if synonym, ok := commandSynonyms[command]; ok {
command = synonym
}
knownCommands = append(knownCommands, CliCommand{Command: command, Section: section, Description: description})
return command
}
func commandsListing() string {
listing := []string{}
lastSection := ""
for _, cliCommand := range knownCommands {
if lastSection != cliCommand.Section {
lastSection = cliCommand.Section
listing = append(listing, fmt.Sprintf("%s:", cliCommand.Section))
}
commandListing := fmt.Sprintf("\t%-40s%s", cliCommand.Command, cliCommand.Description)
listing = append(listing, commandListing)
}
return strings.Join(listing, "\n")
}
func availableCommandsUsage() string {
return fmt.Sprintf(`Available commands (-c):
%+v
Run 'orchestrator help <command>' for detailed help on given command, e.g. 'orchestrator help relocate'
Usage for most commands:
orchestrator -c <command> [-i <instance.fqdn>[,<instance.fqdn>]* ] [-d <destination.fqdn>] [--verbose|--debug]
`, commandsListing())
}
// getClusterName will make a best effort to deduce a cluster name using either a given alias
// or an instanceKey. First attempt is at alias, and if that doesn't work, we try instanceKey.
func getClusterName(clusterAlias string, instanceKey *inst.InstanceKey) (clusterName string) {
clusterName, _ = inst.FigureClusterName(clusterAlias, instanceKey, thisInstanceKey)
return clusterName
}
func validateInstanceIsFound(instanceKey *inst.InstanceKey) (instance *inst.Instance) {
instance, _, err := inst.ReadInstance(instanceKey)
if err != nil {
log.Fatale(err)
}
if instance == nil {
log.Fatalf("Instance not found: %+v", *instanceKey)
}
return instance
}
// CliWrapper is called from main and allows for the instance parameter
// to take multiple instance names separated by a comma or whitespace.
func CliWrapper(command string, strict bool, instances string, destination string, owner string, reason string, duration string, pattern string, clusterAlias string, pool string, hostnameFlag string) {
if config.Config.RaftEnabled && !*config.RuntimeCLIFlags.IgnoreRaftSetup {
log.Fatalf(`Orchestrator configured to run raft ("RaftEnabled": true). All access must go through the web API of the active raft node. You may use the orchestrator-client script which has a similar interface to the command line invocation. You may override this with --ignore-raft-setup`)
}
r := regexp.MustCompile(`[ ,\r\n\t]+`)
tokens := r.Split(instances, -1)
switch command {
case "submit-pool-instances":
{
// These commands unsplit the tokens (they expect a comma delimited list of instances)
tokens = []string{instances}
}
}
for _, instance := range tokens {
if instance != "" || len(tokens) == 1 {
Cli(command, strict, instance, destination, owner, reason, duration, pattern, clusterAlias, pool, hostnameFlag)
}
}
}
// Cli initiates a command line interface, executing requested command.
func Cli(command string, strict bool, instance string, destination string, owner string, reason string, duration string, pattern string, clusterAlias string, pool string, hostnameFlag string) {
if synonym, ok := commandSynonyms[command]; ok {
command = synonym
}
skipDatabaseCommands := false
switch command {
case "redeploy-internal-db":
skipDatabaseCommands = true
case "help":
skipDatabaseCommands = true
case "dump-config":
skipDatabaseCommands = true
}
instanceKey, err := inst.ParseResolveInstanceKey(instance)
if err != nil {
instanceKey = nil
}
rawInstanceKey, err := inst.ParseRawInstanceKey(instance)
if err != nil {
rawInstanceKey = nil
}
if destination != "" && !strings.Contains(destination, ":") {
destination = fmt.Sprintf("%s:%d", destination, config.Config.DefaultInstancePort)
}
destinationKey, err := inst.ParseResolveInstanceKey(destination)
if err != nil {
destinationKey = nil
}
if !skipDatabaseCommands {
destinationKey = inst.ReadFuzzyInstanceKeyIfPossible(destinationKey)
}
if hostname, err := os.Hostname(); err == nil {
thisInstanceKey = &inst.InstanceKey{Hostname: hostname, Port: int(config.Config.DefaultInstancePort)}
}
postponedFunctionsContainer := inst.NewPostponedFunctionsContainer()
if len(owner) == 0 {
// get os username as owner
usr, err := user.Current()
if err != nil {
log.Fatale(err)
}
owner = usr.Username
}
inst.SetMaintenanceOwner(owner)
if !skipDatabaseCommands && !*config.RuntimeCLIFlags.SkipContinuousRegistration {
process.ContinuousRegistration(string(process.OrchestratorExecutionCliMode), command)
}
kv.InitKVStores()
// begin commands
switch command {
// smart mode
case registerCliCommand("relocate", "Smart relocation", `Relocate a replica beneath another instance`), registerCliCommand("relocate-below", "Smart relocation", `Synonym to 'relocate', will be deprecated`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce destination:", destination)
}
_, err := inst.RelocateBelow(instanceKey, destinationKey)
if err != nil {
log.Fatale(err)
}
fmt.Printf("%s<%s\n", instanceKey.DisplayString(), destinationKey.DisplayString())
}
case registerCliCommand("relocate-replicas", "Smart relocation", `Relocates all or part of the replicas of a given instance under another instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce destination:", destination)
}
replicas, _, err, errs := inst.RelocateReplicas(instanceKey, destinationKey, pattern)
if err != nil {
log.Fatale(err)
} else {
for _, e := range errs {
log.Errore(e)
}
for _, replica := range replicas {
fmt.Println(replica.Key.DisplayString())
}
}
}
case registerCliCommand("take-siblings", "Smart relocation", `Turn all siblings of a replica into its sub-replicas.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
_, _, err := inst.TakeSiblings(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("regroup-replicas", "Smart relocation", `Given an instance, pick one of its replicas and make it local master of its siblings`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
validateInstanceIsFound(instanceKey)
lostReplicas, equalReplicas, aheadReplicas, cannotReplicateReplicas, promotedReplica, err := inst.RegroupReplicas(instanceKey, false, func(candidateReplica *inst.Instance) { fmt.Println(candidateReplica.Key.DisplayString()) }, postponedFunctionsContainer)
lostReplicas = append(lostReplicas, cannotReplicateReplicas...)
postponedFunctionsContainer.Wait()
if promotedReplica == nil {
log.Fatalf("Could not regroup replicas of %+v; error: %+v", *instanceKey, err)
}
fmt.Printf("%s lost: %d, trivial: %d, pseudo-gtid: %d\n",
promotedReplica.Key.DisplayString(), len(lostReplicas), len(equalReplicas), len(aheadReplicas)) //nolint
if err != nil {
log.Fatale(err)
}
}
// General replication commands
// move, binlog file:pos
case registerCliCommand("move-up", "Classic file:pos relocation", `Move a replica one level up the topology`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
instance, err := inst.MoveUp(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Printf("%s<%s\n", instanceKey.DisplayString(), instance.MasterKey.DisplayString())
}
case registerCliCommand("move-up-replicas", "Classic file:pos relocation", `Moves replicas of the given instance one level up the topology`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
movedReplicas, _, err, errs := inst.MoveUpReplicas(instanceKey, pattern)
if err != nil {
log.Fatale(err)
} else {
for _, e := range errs {
log.Errore(e)
}
for _, replica := range movedReplicas {
fmt.Println(replica.Key.DisplayString())
}
}
}
case registerCliCommand("move-below", "Classic file:pos relocation", `Moves a replica beneath its sibling. Both replicas must be actively replicating from same master.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce destination/sibling:", destination)
}
_, err := inst.MoveBelow(instanceKey, destinationKey)
if err != nil {
log.Fatale(err)
}
fmt.Printf("%s<%s\n", instanceKey.DisplayString(), destinationKey.DisplayString())
}
case registerCliCommand("repoint", "Classic file:pos relocation", `Make the given instance replicate from another instance without changing the binglog coordinates. Use with care`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
// destinationKey can be null, in which case the instance repoints to its existing master
instance, err := inst.Repoint(instanceKey, destinationKey, inst.GTIDHintNeutral)
if err != nil {
log.Fatale(err)
}
fmt.Printf("%s<%s\n", instanceKey.DisplayString(), instance.MasterKey.DisplayString())
}
case registerCliCommand("repoint-replicas", "Classic file:pos relocation", `Repoint all replicas of given instance to replicate back from the instance. Use with care`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
repointedReplicas, err, errs := inst.RepointReplicasTo(instanceKey, pattern, destinationKey)
if err != nil {
log.Fatale(err)
} else {
for _, e := range errs {
log.Errore(e)
}
for _, replica := range repointedReplicas {
fmt.Printf("%s<%s\n", replica.Key.DisplayString(), instanceKey.DisplayString())
}
}
}
case registerCliCommand("take-master", "Classic file:pos relocation", `Turn an instance into a master of its own master; essentially switch the two.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
_, err := inst.TakeMaster(instanceKey, false)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("make-co-master", "Classic file:pos relocation", `Create a master-master replication. Given instance is a replica which replicates directly from a master.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.MakeCoMaster(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("get-candidate-replica", "Classic file:pos relocation", `Information command suggesting the most up-to-date replica of a given instance that is good for promotion`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
instance, _, _, _, _, err := inst.GetCandidateReplica(instanceKey, false)
if err != nil {
log.Fatale(err)
} else {
fmt.Println(instance.Key.DisplayString())
}
}
case registerCliCommand("regroup-replicas-bls", "Binlog server relocation", `Regroup Binlog Server replicas of a given instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
validateInstanceIsFound(instanceKey)
_, promotedBinlogServer, err := inst.RegroupReplicasBinlogServers(instanceKey, false)
if promotedBinlogServer == nil {
log.Fatalf("Could not regroup binlog server replicas of %+v; error: %+v", *instanceKey, err)
}
fmt.Println(promotedBinlogServer.Key.DisplayString()) //nolint
if err != nil {
log.Fatale(err)
}
}
// move, GTID
case registerCliCommand("move-gtid", "GTID relocation", `Move a replica beneath another instance.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce destination:", destination)
}
_, err := inst.MoveBelowGTID(instanceKey, destinationKey)
if err != nil {
log.Fatale(err)
}
fmt.Printf("%s<%s\n", instanceKey.DisplayString(), destinationKey.DisplayString())
}
case registerCliCommand("move-replicas-gtid", "GTID relocation", `Moves all replicas of a given instance under another (destination) instance using GTID`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce destination:", destination)
}
movedReplicas, _, err, errs := inst.MoveReplicasGTID(instanceKey, destinationKey, pattern)
if err != nil {
log.Fatale(err)
} else {
for _, e := range errs {
log.Errore(e)
}
for _, replica := range movedReplicas {
fmt.Println(replica.Key.DisplayString())
}
}
}
case registerCliCommand("regroup-replicas-gtid", "GTID relocation", `Given an instance, pick one of its replica and make it local master of its siblings, using GTID.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
validateInstanceIsFound(instanceKey)
lostReplicas, movedReplicas, cannotReplicateReplicas, promotedReplica, err := inst.RegroupReplicasGTID(instanceKey, false, func(candidateReplica *inst.Instance) { fmt.Println(candidateReplica.Key.DisplayString()) }, postponedFunctionsContainer, nil)
lostReplicas = append(lostReplicas, cannotReplicateReplicas...)
if promotedReplica == nil {
log.Fatalf("Could not regroup replicas of %+v; error: %+v", *instanceKey, err)
}
fmt.Printf("%s lost: %d, moved: %d\n", promotedReplica.Key.DisplayString(), len(lostReplicas), len(movedReplicas)) //nolint
if err != nil {
log.Fatale(err)
}
}
// General replication commands
case registerCliCommand("enable-gtid", "Replication, general", `If possible, turn on GTID replication`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.EnableGTID(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("disable-gtid", "Replication, general", `Turn off GTID replication, back to file:pos replication`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.DisableGTID(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("which-gtid-errant", "Replication, general", `Get errant GTID set (empty results if no errant GTID)`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
instance, err := inst.ReadTopologyInstance(instanceKey)
if err != nil {
log.Fatale(err)
}
if instance == nil {
log.Fatalf("Instance not found: %+v", *instanceKey)
}
fmt.Println(instance.GtidErrant) //nolint
}
case registerCliCommand("gtid-errant-reset-master", "Replication, general", `Reset master on instance, remove GTID errant transactions`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.ErrantGTIDResetMaster(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("skip-query", "Replication, general", `Skip a single statement on a replica; either when running with GTID or without`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.SkipQuery(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("stop-slave", "Replication, general", `Issue a STOP SLAVE on an instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.StopReplication(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("start-slave", "Replication, general", `Issue a START SLAVE on an instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.StartReplication(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("restart-slave", "Replication, general", `STOP and START SLAVE on an instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.RestartReplication(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("reset-slave", "Replication, general", `Issues a RESET SLAVE command; use with care`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.ResetReplicationOperation(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("detach-replica-master-host", "Replication, general", `Stops replication and modifies Master_Host into an impossible, yet reversible, value.`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
_, err := inst.DetachReplicaMasterHost(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("reattach-replica-master-host", "Replication, general", `Undo a detach-replica-master-host operation`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatal("Cannot deduce instance:", instance)
}
_, err := inst.ReattachReplicaMasterHost(instanceKey)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("master-pos-wait", "Replication, general", `Wait until replica reaches given replication coordinates (--binlog=file:pos)`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
instance, err := inst.ReadTopologyInstance(instanceKey)
if err != nil {
log.Fatale(err)
}
if instance == nil {
log.Fatalf("Instance not found: %+v", *instanceKey)
}
var binlogCoordinates *inst.BinlogCoordinates
if binlogCoordinates, err = inst.ParseBinlogCoordinates(*config.RuntimeCLIFlags.BinlogFile); err != nil {
log.Fatalf("Expecing --binlog argument as file:pos")
}
_, err = inst.MasterPosWait(instanceKey, binlogCoordinates)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("restart-slave-statements", "Replication, general", `Get a list of statements to execute to stop then restore replica to same execution state. Provide --statement for injected statement`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
statements, err := inst.GetReplicationRestartPreserveStatements(instanceKey, *config.RuntimeCLIFlags.Statement)
if err != nil {
log.Fatale(err)
}
for _, statement := range statements {
fmt.Println(statement)
}
}
// Replication, information
case registerCliCommand("can-replicate-from", "Replication information", `Can an instance (-i) replicate from another (-d) according to replication rules? Prints 'true|false'`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
if destinationKey == nil {
log.Fatal("Cannot deduce target instance:", destination)
}
otherInstance := validateInstanceIsFound(destinationKey)
if canReplicate, _ := instance.CanReplicateFrom(otherInstance); canReplicate {
fmt.Println(destinationKey.DisplayString())
}
}
case registerCliCommand("is-replicating", "Replication information", `Is an instance (-i) actively replicating right now`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
if instance.ReplicaRunning() {
fmt.Println(instance.Key.DisplayString())
}
}
case registerCliCommand("is-replication-stopped", "Replication information", `Is an instance (-i) a replica with both replication threads stopped`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
if instance.ReplicationThreadsStopped() {
fmt.Println(instance.Key.DisplayString())
}
}
// Instance
case registerCliCommand("set-read-only", "Instance", `Turn an instance read-only, via SET GLOBAL read_only := 1`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.SetReadOnly(instanceKey, true)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("set-writeable", "Instance", `Turn an instance writeable, via SET GLOBAL read_only := 0`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
_, err := inst.SetReadOnly(instanceKey, false)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
// Binary log operations
case registerCliCommand("flush-binary-logs", "Binary logs", `Flush binary logs on an instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
var err error
if *config.RuntimeCLIFlags.BinlogFile == "" {
_, err = inst.FlushBinaryLogs(instanceKey, 1)
} else {
_, err = inst.FlushBinaryLogsTo(instanceKey, *config.RuntimeCLIFlags.BinlogFile)
}
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("purge-binary-logs", "Binary logs", `Purge binary logs of an instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
var err error
if *config.RuntimeCLIFlags.BinlogFile == "" {
log.Fatal("expecting --binlog value")
}
_, err = inst.PurgeBinaryLogsTo(instanceKey, *config.RuntimeCLIFlags.BinlogFile, false)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("locate-gtid-errant", "Binary logs", `List binary logs containing errant GTIDs`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unresolved instance")
}
errantBinlogs, err := inst.LocateErrantGTID(instanceKey)
if err != nil {
log.Fatale(err)
}
for _, binlog := range errantBinlogs {
fmt.Println(binlog)
}
}
// Pool
case registerCliCommand("submit-pool-instances", "Pools", `Submit a pool name with a list of instances in that pool`):
{
if pool == "" {
log.Fatal("Please submit --pool")
}
err := inst.ApplyPoolInstances(inst.NewPoolInstancesSubmission(pool, instance))
if err != nil {
log.Fatale(err)
}
}
case registerCliCommand("cluster-pool-instances", "Pools", `List all pools and their associated instances`):
{
clusterPoolInstances, err := inst.ReadAllClusterPoolInstances()
if err != nil {
log.Fatale(err)
}
for _, clusterPoolInstance := range clusterPoolInstances {
fmt.Printf("%s\t%s\t%s\t%s:%d\n", clusterPoolInstance.ClusterName, clusterPoolInstance.ClusterAlias, clusterPoolInstance.Pool, clusterPoolInstance.Hostname, clusterPoolInstance.Port)
}
}
case registerCliCommand("which-heuristic-cluster-pool-instances", "Pools", `List instances of a given cluster which are in either any pool or in a specific pool`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instances, err := inst.GetHeuristicClusterPoolInstances(clusterName, pool)
if err != nil {
log.Fatale(err)
} else {
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
}
// Information
case registerCliCommand("find", "Information", `Find instances whose hostname matches given regex pattern`):
{
if pattern == "" {
log.Fatal("No pattern given")
}
instances, err := inst.FindInstances(pattern)
if err != nil {
log.Fatale(err)
} else {
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
}
case registerCliCommand("search", "Information", `Search instances by name, version, version comment, port`):
{
if pattern == "" {
log.Fatal("No pattern given")
}
instances, err := inst.SearchInstances(pattern)
if err != nil {
log.Fatale(err)
} else {
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
}
case registerCliCommand("clusters", "Information", `List all clusters known to orchestrator`):
{
clusters, err := inst.ReadClusters()
if err != nil {
log.Fatale(err)
}
fmt.Println(strings.Join(clusters, "\n"))
}
case registerCliCommand("clusters-alias", "Information", `List all clusters known to orchestrator`):
{
clusters, err := inst.ReadClustersInfo("")
if err != nil {
log.Fatale(err)
}
for _, cluster := range clusters {
fmt.Printf("%s\t%s\n", cluster.ClusterName, cluster.ClusterAlias)
}
}
case registerCliCommand("all-clusters-masters", "Information", `List of writeable masters, one per cluster`):
{
instances, err := inst.ReadWriteableClustersMasters()
if err != nil {
log.Fatale(err)
} else {
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
}
case registerCliCommand("topology", "Information", `Show an ascii-graph of a replication topology, given a member of that topology`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
output, err := inst.ASCIITopology(clusterName, pattern, false, false)
if err != nil {
log.Fatale(err)
}
fmt.Println(output)
}
case registerCliCommand("topology-tabulated", "Information", `Show an ascii-graph of a replication topology, given a member of that topology`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
output, err := inst.ASCIITopology(clusterName, pattern, true, false)
if err != nil {
log.Fatale(err)
}
fmt.Println(output)
}
case registerCliCommand("topology-tags", "Information", `Show an ascii-graph of a replication topology and instance tags, given a member of that topology`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
output, err := inst.ASCIITopology(clusterName, pattern, false, true)
if err != nil {
log.Fatale(err)
}
fmt.Println(output)
}
case registerCliCommand("all-instances", "Information", `The complete list of known instances`):
{
instances, err := inst.SearchInstances("")
if err != nil {
log.Fatale(err)
} else {
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
}
case registerCliCommand("which-instance", "Information", `Output the fully-qualified hostname:port representation of the given instance, or error if unknown`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unable to get master: unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
fmt.Println(instance.Key.DisplayString())
}
case registerCliCommand("which-cluster", "Information", `Output the name of the cluster an instance belongs to, or error if unknown to orchestrator`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
fmt.Println(clusterName)
}
case registerCliCommand("which-cluster-alias", "Information", `Output the alias of the cluster an instance belongs to, or error if unknown to orchestrator`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
clusterInfo, err := inst.ReadClusterInfo(clusterName)
if err != nil {
log.Fatale(err)
}
fmt.Println(clusterInfo.ClusterAlias)
}
case registerCliCommand("which-cluster-domain", "Information", `Output the domain name of the cluster an instance belongs to, or error if unknown to orchestrator`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
clusterInfo, err := inst.ReadClusterInfo(clusterName)
if err != nil {
log.Fatale(err)
}
fmt.Println(clusterInfo.ClusterDomain)
}
case registerCliCommand("which-heuristic-domain-instance", "Information", `Returns the instance associated as the cluster's writer with a cluster's domain name.`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instanceKey, err := inst.GetHeuristicClusterDomainInstanceAttribute(clusterName)
if err != nil {
log.Fatale(err)
}
fmt.Println(instanceKey.DisplayString())
}
case registerCliCommand("which-cluster-master", "Information", `Output the name of the master in a given cluster`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
masters, err := inst.ReadClusterMaster(clusterName)
if err != nil {
log.Fatale(err)
}
if len(masters) == 0 {
log.Fatalf("No writeable masters found for cluster %+v", clusterName)
}
fmt.Println(masters[0].Key.DisplayString())
}
case registerCliCommand("which-cluster-instances", "Information", `Output the list of instances participating in same cluster as given instance`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instances, err := inst.ReadClusterInstances(clusterName)
if err != nil {
log.Fatale(err)
}
for _, clusterInstance := range instances {
fmt.Println(clusterInstance.Key.DisplayString())
}
}
case registerCliCommand("which-cluster-osc-replicas", "Information", `Output a list of replicas in a cluster, that could serve as a pt-online-schema-change operation control replicas`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instances, err := inst.GetClusterOSCReplicas(clusterName)
if err != nil {
log.Fatale(err)
}
for _, clusterInstance := range instances {
fmt.Println(clusterInstance.Key.DisplayString())
}
}
case registerCliCommand("which-cluster-gh-ost-replicas", "Information", `Output a list of replicas in a cluster, that could serve as a gh-ost working server`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instances, err := inst.GetClusterGhostReplicas(clusterName)
if err != nil {
log.Fatale(err)
}
for _, clusterInstance := range instances {
fmt.Println(clusterInstance.Key.DisplayString())
}
}
case registerCliCommand("which-master", "Information", `Output the fully-qualified hostname:port representation of a given instance's master`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unable to get master: unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
if instance.MasterKey.IsValid() {
fmt.Println(instance.MasterKey.DisplayString())
}
}
case registerCliCommand("which-downtimed-instances", "Information", `List instances currently downtimed, potentially filtered by cluster`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
instances, err := inst.ReadDowntimedInstances(clusterName)
if err != nil {
log.Fatale(err)
}
for _, clusterInstance := range instances {
fmt.Println(clusterInstance.Key.DisplayString())
}
}
case registerCliCommand("which-replicas", "Information", `Output the fully-qualified hostname:port list of replicas of a given instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unable to get replicas: unresolved instance")
}
replicas, err := inst.ReadReplicaInstances(instanceKey)
if err != nil {
log.Fatale(err)
}
for _, replica := range replicas {
fmt.Println(replica.Key.DisplayString())
}
}
case registerCliCommand("which-lost-in-recovery", "Information", `List instances marked as downtimed for being lost in a recovery process`):
{
instances, err := inst.ReadLostInRecoveryInstances("")
if err != nil {
log.Fatale(err)
}
for _, instance := range instances {
fmt.Println(instance.Key.DisplayString())
}
}
case registerCliCommand("instance-status", "Information", `Output short status on a given instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
if instanceKey == nil {
log.Fatalf("Unable to get status: unresolved instance")
}
instance := validateInstanceIsFound(instanceKey)
fmt.Println(instance.HumanReadableDescription())
}
case registerCliCommand("get-cluster-heuristic-lag", "Information", `For a given cluster (indicated by an instance or alias), output a heuristic "representative" lag of that cluster`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
lag, err := inst.GetClusterHeuristicLag(clusterName)
if err != nil {
log.Fatale(err)
}
fmt.Println(lag)
}
case registerCliCommand("submit-masters-to-kv-stores", "Key-value", `Submit master of a specific cluster, or all masters of all clusters to key-value stores`):
{
clusterName := getClusterName(clusterAlias, instanceKey)
log.Debugf("cluster name is <%s>", clusterName)
kvPairs, _, err := logic.SubmitMastersToKvStores(clusterName, true)
if err != nil {
log.Fatale(err)
}
for _, kvPair := range kvPairs {
fmt.Printf("%s:%s\n", kvPair.Key, kvPair.Value)
}
}
case registerCliCommand("tags", "tags", `List tags for a given instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
tags, err := inst.ReadInstanceTags(instanceKey)
if err != nil {
log.Fatale(err)
}
for _, tag := range tags {
fmt.Println(tag.String())
}
}
case registerCliCommand("tag-value", "tags", `Get tag value for a specific instance`):
{
instanceKey, _ = inst.FigureInstanceKey(instanceKey, thisInstanceKey)
tag, err := inst.ParseTag(*config.RuntimeCLIFlags.Tag)