forked from vitessio/vitess
/
reparent.go
84 lines (76 loc) · 2.66 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
// Copyright 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vtctl
import (
"flag"
"fmt"
_ "github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/wrangler"
)
func init() {
addCommand("Tablets", command{
"DemoteMaster",
commandDemoteMaster,
"<tablet alias|zk tablet path>",
"Demotes a master tablet."})
addCommand("Tablets", command{
"ReparentTablet",
commandReparentTablet,
"<tablet alias|zk tablet path>",
"Reparent a tablet to the current master in the shard. This only works if the current slave position matches the last known reparent action."})
addCommand("Shards", command{
"ReparentShard",
commandReparentShard,
"[-force] [-leave-master-read-only] <keyspace/shard|zk shard path> <tablet alias|zk tablet path>",
"Specify which shard to reparent and which tablet should be the new master."})
}
func commandDemoteMaster(wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 1 {
return fmt.Errorf("action DemoteMaster requires <tablet alias|zk tablet path>")
}
tabletAlias, err := tabletParamToTabletAlias(subFlags.Arg(0))
if err != nil {
return err
}
tabletInfo, err := wr.TopoServer().GetTablet(tabletAlias)
if err != nil {
return err
}
return wr.TabletManagerClient().DemoteMaster(tabletInfo, wr.ActionTimeout())
}
func commandReparentTablet(wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 1 {
return fmt.Errorf("action ReparentTablet requires <tablet alias|zk tablet path>")
}
tabletAlias, err := tabletParamToTabletAlias(subFlags.Arg(0))
if err != nil {
return err
}
return wr.ReparentTablet(tabletAlias)
}
func commandReparentShard(wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
leaveMasterReadOnly := subFlags.Bool("leave-master-read-only", false, "leaves the master read-only after reparenting")
force := subFlags.Bool("force", false, "will force the reparent even if the master is already correct")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() != 2 {
return fmt.Errorf("action ReparentShard requires <keyspace/shard|zk shard path> <tablet alias|zk tablet path>")
}
keyspace, shard, err := shardParamToKeyspaceShard(subFlags.Arg(0))
if err != nil {
return err
}
tabletAlias, err := tabletParamToTabletAlias(subFlags.Arg(1))
if err != nil {
return err
}
return wr.ReparentShard(keyspace, shard, tabletAlias, *leaveMasterReadOnly, *force)
}