forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_zktopo.go
80 lines (69 loc) · 2.16 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
// 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"
"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."})
}
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
}