-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
reparent.go
209 lines (186 loc) · 8.9 KB
/
reparent.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
/*
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 vtctl
import (
"context"
"fmt"
"github.com/spf13/pflag"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/wrangler"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
func init() {
addCommand("Tablets", command{
name: "ReparentTablet",
method: commandReparentTablet,
params: "<tablet alias>",
help: "Reparent a tablet to the current primary in the shard. This only works if the current replication position matches the last known reparent action.",
})
addCommand("Shards", command{
name: "InitShardPrimary",
method: commandInitShardPrimary,
params: "[--force] [--wait_replicas_timeout=<duration>] <keyspace/shard> <tablet alias>",
help: "Sets the initial primary for a shard. Will make all other tablets in the shard replicas of the provided tablet. WARNING: this could cause data loss on an already replicating shard. PlannedReparentShard or EmergencyReparentShard should be used instead.",
deprecated: true,
deprecatedBy: "PlannedReparentShard",
})
addCommand("Shards", command{
name: "PlannedReparentShard",
method: commandPlannedReparentShard,
params: "--keyspace_shard=<keyspace/shard> [--new_primary=<tablet alias>] [--avoid_tablet=<tablet alias>] [--wait_replicas_timeout=<duration>]",
help: "Reparents the shard to the new primary, or away from old primary. Both old and new primary need to be up and running.",
})
addCommand("Shards", command{
name: "EmergencyReparentShard",
method: commandEmergencyReparentShard,
params: "--keyspace_shard=<keyspace/shard> [--new_primary=<tablet alias>] [--wait_replicas_timeout=<duration>] [--ignore_replicas=<tablet alias list>] [--prevent_cross_cell_promotion=<true/false>]",
help: "Reparents the shard to the new primary. Assumes the old primary is dead and not responding.",
})
addCommand("Shards", command{
name: "TabletExternallyReparented",
method: commandTabletExternallyReparented,
params: "<tablet alias>",
help: "Changes metadata in the topology server to acknowledge a shard primary change performed by an external tool. See the Reparenting guide for more information:" +
"https://vitess.io/docs/user-guides/reparenting/#external-reparenting",
})
}
func commandReparentTablet(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
if mysqlctl.DisableActiveReparents {
return fmt.Errorf("active reparent commands disabled (unset the --disable_active_reparents flag to enable)")
}
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 1 {
return fmt.Errorf("action ReparentTablet requires <tablet alias>")
}
tabletAlias, err := topoproto.ParseTabletAlias(subFlags.Arg(0))
if err != nil {
return err
}
return wr.ReparentTablet(ctx, tabletAlias)
}
func commandInitShardPrimary(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
if mysqlctl.DisableActiveReparents {
return fmt.Errorf("active reparent commands disabled (unset the --disable_active_reparents flag to enable)")
}
force := subFlags.Bool("force", false, "will force the reparent even if the provided tablet is not writable or the shard primary")
waitReplicasTimeout := subFlags.Duration("wait_replicas_timeout", topo.RemoteOperationTimeout, "time to wait for replicas to catch up in reparenting")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 2 {
return fmt.Errorf("action InitShardPrimary requires <keyspace/shard> <tablet alias>")
}
keyspace, shard, err := topoproto.ParseKeyspaceShard(subFlags.Arg(0))
if err != nil {
return err
}
tabletAlias, err := topoproto.ParseTabletAlias(subFlags.Arg(1))
if err != nil {
return err
}
return wr.InitShardPrimary(ctx, keyspace, shard, tabletAlias, *force, *waitReplicasTimeout)
}
func commandPlannedReparentShard(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
if mysqlctl.DisableActiveReparents {
return fmt.Errorf("active reparent commands disabled (unset the --disable_active_reparents flag to enable)")
}
waitReplicasTimeout := subFlags.Duration("wait_replicas_timeout", topo.RemoteOperationTimeout, "time to wait for replicas to catch up on replication before and after reparenting")
keyspaceShard := subFlags.String("keyspace_shard", "", "keyspace/shard of the shard that needs to be reparented")
newPrimary := subFlags.String("new_primary", "", "alias of a tablet that should be the new primary")
avoidTablet := subFlags.String("avoid_tablet", "", "alias of a tablet that should not be the primary, i.e. reparent to any other tablet if this one is the primary")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() == 2 {
// Legacy syntax: "<keyspace/shard> <tablet alias>".
if *keyspaceShard != "" || *newPrimary != "" {
return fmt.Errorf("cannot use legacy syntax and flags --keyspace_shard and --new_primary for action PlannedReparentShard at the same time")
}
*keyspaceShard = subFlags.Arg(0)
*newPrimary = subFlags.Arg(1)
} else if subFlags.NArg() != 0 {
return fmt.Errorf("action PlannedReparentShard requires --keyspace_shard=<keyspace/shard> [--new_primary=<tablet alias>] [--avoid_tablet=<tablet alias>]")
}
keyspace, shard, err := topoproto.ParseKeyspaceShard(*keyspaceShard)
if err != nil {
return err
}
var newPrimaryAlias, avoidTabletAlias *topodatapb.TabletAlias
if *newPrimary != "" {
newPrimaryAlias, err = topoproto.ParseTabletAlias(*newPrimary)
if err != nil {
return err
}
}
if *avoidTablet != "" {
avoidTabletAlias, err = topoproto.ParseTabletAlias(*avoidTablet)
if err != nil {
return err
}
}
return wr.PlannedReparentShard(ctx, keyspace, shard, newPrimaryAlias, avoidTabletAlias, *waitReplicasTimeout)
}
func commandEmergencyReparentShard(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
if mysqlctl.DisableActiveReparents {
return fmt.Errorf("active reparent commands disabled (unset the --disable_active_reparents flag to enable)")
}
waitReplicasTimeout := subFlags.Duration("wait_replicas_timeout", topo.RemoteOperationTimeout, "time to wait for replicas to catch up in reparenting")
keyspaceShard := subFlags.String("keyspace_shard", "", "keyspace/shard of the shard that needs to be reparented")
newPrimary := subFlags.String("new_primary", "", "optional alias of a tablet that should be the new primary. If not specified, Vitess will select the best candidate")
preventCrossCellPromotion := subFlags.Bool("prevent_cross_cell_promotion", false, "only promotes a new primary from the same cell as the previous primary")
ignoreReplicasList := subFlags.String("ignore_replicas", "", "comma-separated list of replica tablet aliases to ignore during emergency reparent")
waitForAllTablets := subFlags.Bool("wait_for_all_tablets", false, "should ERS wait for all the tablets to respond. Useful when all the tablets are reachable")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() == 2 {
// Legacy syntax: "<keyspace/shard> <tablet alias>".
if *newPrimary != "" {
return fmt.Errorf("cannot use legacy syntax and flag --new_primary for action EmergencyReparentShard at the same time")
}
*keyspaceShard = subFlags.Arg(0)
*newPrimary = subFlags.Arg(1)
} else if subFlags.NArg() != 0 {
return fmt.Errorf("action EmergencyReparentShard requires --keyspace_shard=<keyspace/shard>")
}
keyspace, shard, err := topoproto.ParseKeyspaceShard(*keyspaceShard)
if err != nil {
return err
}
var tabletAlias *topodatapb.TabletAlias
if *newPrimary != "" {
tabletAlias, err = topoproto.ParseTabletAlias(*newPrimary)
if err != nil {
return err
}
}
unreachableReplicas := topoproto.ParseTabletSet(*ignoreReplicasList)
return wr.EmergencyReparentShard(ctx, keyspace, shard, tabletAlias, *waitReplicasTimeout, unreachableReplicas, *preventCrossCellPromotion, *waitForAllTablets)
}
func commandTabletExternallyReparented(ctx context.Context, wr *wrangler.Wrangler, subFlags *pflag.FlagSet, args []string) error {
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 1 {
return fmt.Errorf("action TabletExternallyReparented requires <tablet alias>")
}
tabletAlias, err := topoproto.ParseTabletAlias(subFlags.Arg(0))
if err != nil {
return err
}
return wr.TabletExternallyReparented(ctx, tabletAlias)
}