forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_zktopo.go
145 lines (130 loc) · 4.27 KB
/
plugin_zktopo.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
// 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
// Imports and register the Zookeeper topo.Server
// Adds the Zookeeper specific commands
import (
"flag"
"fmt"
"strings"
"sync"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/wrangler"
"github.com/youtube/vitess/go/vt/zktopo"
"github.com/youtube/vitess/go/zk"
"golang.org/x/net/context"
)
func init() {
addCommand("Generic", command{
"PruneActionLogs",
commandPruneActionLogs,
"[-keep-count=<count to keep>] <zk actionlog path> ...",
"(requires zktopo.Server)\n" +
"e.g. PruneActionLogs -keep-count=10 /zk/global/vt/keyspaces/my_keyspace/shards/0/actionlog\n" +
"Removes older actionlog entries until at most <count to keep> are left."})
addCommand("Generic", command{
"ExportZkns",
commandExportZkns,
"<cell name|zk local vt path>",
"(requires zktopo.Server)\n" +
"Export the serving graph entries to the zkns format."})
addCommand("Generic", command{
"ExportZknsForKeyspace",
commandExportZknsForKeyspace,
"<keyspace|zk global keyspace path>",
"(requires zktopo.Server)\n" +
"Export the serving graph entries to the zkns format."})
}
func zkResolveWildcards(wr *wrangler.Wrangler, args []string) ([]string, error) {
zkts, ok := wr.TopoServer().Impl.(*zktopo.Server)
if !ok {
return args, nil
}
return zk.ResolveWildcards(zkts.GetZConn(), args)
}
func commandPruneActionLogs(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
keepCount := subFlags.Int("keep-count", 10, "count to keep")
if err := subFlags.Parse(args); err != nil {
return err
}
if subFlags.NArg() == 0 {
return fmt.Errorf("action PruneActionLogs requires <zk action log path> [...]")
}
paths, err := zkResolveWildcards(wr, subFlags.Args())
if err != nil {
return err
}
zkts, ok := wr.TopoServer().Impl.(*zktopo.Server)
if !ok {
return fmt.Errorf("PruneActionLogs requires a zktopo.Server")
}
var errCount sync2.AtomicInt32
wg := sync.WaitGroup{}
for _, zkActionLogPath := range paths {
wg.Add(1)
go func(zkActionLogPath string) {
defer wg.Done()
purgedCount, err := zkts.PruneActionLogs(zkActionLogPath, *keepCount)
if err == nil {
wr.Logger().Infof("%v pruned %v", zkActionLogPath, purgedCount)
} else {
wr.Logger().Errorf("%v pruning failed: %v", zkActionLogPath, err)
errCount.Add(1)
}
}(zkActionLogPath)
}
wg.Wait()
if errCount.Get() > 0 {
return fmt.Errorf("some errors occurred, check the log")
}
return nil
}
func commandExportZkns(ctx context.Context, 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 ExportZkns requires <cell name|zk vt root path>")
}
cell, err := zkVtPathToCell(subFlags.Arg(0))
if err != nil {
return err
}
return wr.ExportZkns(ctx, cell)
}
func commandExportZknsForKeyspace(ctx context.Context, 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 ExportZknsForKeyspace requires <keyspace|zk global keyspace path>")
}
keyspace, err := zkKeyspaceParamToKeyspace(subFlags.Arg(0))
if err != nil {
return err
}
return wr.ExportZknsForKeyspace(ctx, keyspace)
}
func zkVtPathToCell(param string) (string, error) {
if param[0] == '/' {
// old zookeeper replication path like /zk/<cell>/vt
zkPathParts := strings.Split(param, "/")
if len(zkPathParts) != 4 || zkPathParts[0] != "" || zkPathParts[1] != "zk" || zkPathParts[3] != "vt" {
return "", fmt.Errorf("Invalid vt path: %v", param)
}
return zkPathParts[2], nil
}
return param, nil
}
func zkKeyspaceParamToKeyspace(param string) (string, error) {
if param[0] == '/' {
// old zookeeper path, convert to new-style string keyspace
zkPathParts := strings.Split(param, "/")
if len(zkPathParts) != 6 || zkPathParts[0] != "" || zkPathParts[1] != "zk" || zkPathParts[2] != "global" || zkPathParts[3] != "vt" || zkPathParts[4] != "keyspaces" {
return "", fmt.Errorf("Invalid keyspace path: %v", param)
}
return zkPathParts[5], nil
}
return param, nil
}