-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
topo.go
305 lines (264 loc) · 8.44 KB
/
topo.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
/*
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"
"encoding/json"
"flag"
"fmt"
"os"
"path"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/wrangler"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
)
// This file contains the topo command group for vtctl.
const topoGroupName = "Topo"
func init() {
addCommandGroup(topoGroupName)
addCommand(topoGroupName, command{
name: "TopoCat",
method: commandTopoCat,
params: "[-cell <cell>] [-decode_proto] [-decode_proto_json] [-long] <path> [<path>...]",
help: "Retrieves the file(s) at <path> from the topo service, and displays it. It can resolve wildcards, and decode the proto-encoded data.",
})
addCommand(topoGroupName, command{
name: "TopoCp",
method: commandTopoCp,
params: "[-cell <cell>] [-to_topo] <src> <dst>",
help: "Copies a file from topo to local file structure, or the other way around",
})
}
// DecodeContent uses the filename to imply a type, and proto-decodes
// the right object, then echoes it as a string.
func DecodeContent(filename string, data []byte, json bool) (string, error) {
name := path.Base(filename)
dir := path.Dir(filename)
var p proto.Message
switch name {
case topo.CellInfoFile:
p = new(topodatapb.CellInfo)
case topo.KeyspaceFile:
p = new(topodatapb.Keyspace)
case topo.ShardFile:
p = new(topodatapb.Shard)
case topo.VSchemaFile:
p = new(vschemapb.Keyspace)
case topo.ShardReplicationFile:
p = new(topodatapb.ShardReplication)
case topo.TabletFile:
p = new(topodatapb.Tablet)
case topo.SrvVSchemaFile:
p = new(vschemapb.SrvVSchema)
case topo.SrvKeyspaceFile:
p = new(topodatapb.SrvKeyspace)
case topo.RoutingRulesFile:
p = new(vschemapb.RoutingRules)
default:
switch dir {
case "/" + topo.GetExternalVitessClusterDir():
p = new(topodatapb.ExternalVitessCluster)
default:
}
if p == nil {
if json {
return "", fmt.Errorf("unknown topo protobuf type for %v", name)
}
return string(data), nil
}
}
if err := proto.Unmarshal(data, p); err != nil {
return string(data), err
}
var marshalled []byte
var err error
if json {
marshalled, err = protojson.Marshal(p)
} else {
marshalled, err = prototext.Marshal(p)
}
return string(marshalled), err
}
func commandTopoCat(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
cell := subFlags.String("cell", topo.GlobalCell, "topology cell to cat the file from. Defaults to global cell.")
long := subFlags.Bool("long", false, "long listing.")
decodeProtoJSON := subFlags.Bool("decode_proto_json", false, "decode proto files and display them as json")
decodeProto := subFlags.Bool("decode_proto", false, "decode proto files and display them as text")
subFlags.Parse(args)
if subFlags.NArg() == 0 {
return fmt.Errorf("TopoCat: no path specified")
}
resolved, err := wr.TopoServer().ResolveWildcards(ctx, *cell, subFlags.Args())
if err != nil {
return fmt.Errorf("TopoCat: invalid wildcards: %v", err)
}
if len(resolved) == 0 {
// The wildcards didn't result in anything, we're done.
return nil
}
conn, err := wr.TopoServer().ConnForCell(ctx, *cell)
if err != nil {
return err
}
var topologyDecoder TopologyDecoder
switch {
case *decodeProtoJSON:
topologyDecoder = JSONTopologyDecoder{}
case *decodeProto:
topologyDecoder = ProtoTopologyDecoder{}
default:
topologyDecoder = PlainTopologyDecoder{}
}
return topologyDecoder.decode(ctx, resolved, conn, wr, *long)
}
func commandTopoCp(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
cell := subFlags.String("cell", topo.GlobalCell, "topology cell to use for the copy. Defaults to global cell.")
toTopo := subFlags.Bool("to_topo", false, "copies from local server to topo instead (reverse direction).")
subFlags.Parse(args)
if subFlags.NArg() != 2 {
return fmt.Errorf("TopoCp: need source and destination")
}
from := subFlags.Arg(0)
to := subFlags.Arg(1)
if *toTopo {
return copyFileToTopo(ctx, wr.TopoServer(), *cell, from, to)
}
return copyFileFromTopo(ctx, wr.TopoServer(), *cell, from, to)
}
func copyFileFromTopo(ctx context.Context, ts *topo.Server, cell, from, to string) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
data, _, err := conn.Get(ctx, from)
if err != nil {
return err
}
return os.WriteFile(to, data, 0644)
}
func copyFileToTopo(ctx context.Context, ts *topo.Server, cell, from, to string) error {
conn, err := ts.ConnForCell(ctx, cell)
if err != nil {
return err
}
data, err := os.ReadFile(from)
if err != nil {
return err
}
_, err = conn.Update(ctx, to, data, nil)
return err
}
// TopologyDecoder interface for exporting out a leaf node in a readable form
type TopologyDecoder interface {
decode(context.Context, []string, topo.Conn, *wrangler.Wrangler, bool) error
}
// ProtoTopologyDecoder exports topo node as a proto
type ProtoTopologyDecoder struct{}
// PlainTopologyDecoder exports topo node as plain text
type PlainTopologyDecoder struct{}
// JSONTopologyDecoder exports topo node as JSON
type JSONTopologyDecoder struct{}
func (d ProtoTopologyDecoder) decode(ctx context.Context, topoPaths []string, conn topo.Conn, wr *wrangler.Wrangler, long bool) error {
hasError := false
for _, topoPath := range topoPaths {
data, version, err := conn.Get(ctx, topoPath)
if err != nil {
hasError = true
wr.Logger().Printf("TopoCat: Get(%v) failed: %v\n", topoPath, err)
continue
}
if long {
wr.Logger().Printf("path=%v version=%v\n", topoPath, version)
}
decoded, err := DecodeContent(topoPath, data, false)
if err != nil {
wr.Logger().Warningf("TopoCat: cannot proto decode %v: %v", topoPath, err)
decoded = string(data)
}
wr.Logger().Printf(decoded)
if len(decoded) > 0 && decoded[len(decoded)-1] != '\n' && long {
wr.Logger().Printf("\n")
}
}
if hasError {
return fmt.Errorf("TopoCat: some paths had errors")
}
return nil
}
func (d PlainTopologyDecoder) decode(ctx context.Context, topoPaths []string, conn topo.Conn, wr *wrangler.Wrangler, long bool) error {
hasError := false
for _, topoPath := range topoPaths {
data, version, err := conn.Get(ctx, topoPath)
if err != nil {
hasError = true
wr.Logger().Printf("TopoCat: Get(%v) failed: %v\n", topoPath, err)
continue
}
if long {
wr.Logger().Printf("path=%v version=%v\n", topoPath, version)
}
decoded := string(data)
wr.Logger().Printf(decoded)
if len(decoded) > 0 && decoded[len(decoded)-1] != '\n' && long {
wr.Logger().Printf("\n")
}
}
if hasError {
return fmt.Errorf("TopoCat: some paths had errors")
}
return nil
}
func (d JSONTopologyDecoder) decode(ctx context.Context, topoPaths []string, conn topo.Conn, wr *wrangler.Wrangler, long bool) error {
hasError := false
var jsonData []interface{}
for _, topoPath := range topoPaths {
data, version, err := conn.Get(ctx, topoPath)
if err != nil {
hasError = true
wr.Logger().Printf("TopoCat: Get(%v) failed: %v\n", topoPath, err)
continue
}
decoded, err := DecodeContent(topoPath, data, true)
if err != nil {
hasError = true
wr.Logger().Printf("TopoCat: cannot proto decode %v: %v", topoPath, err)
continue
}
var jsonDatum map[string]interface{}
if err = json.Unmarshal([]byte(decoded), &jsonDatum); err != nil {
hasError = true
wr.Logger().Printf("TopoCat: cannot json Unmarshal %v: %v", topoPath, err)
continue
}
if long {
jsonDatum["__path"] = topoPath
jsonDatum["__version"] = version.String()
}
jsonData = append(jsonData, jsonDatum)
}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
hasError = true
wr.Logger().Printf("TopoCat: cannot json Marshal: %v", err)
} else {
wr.Logger().Printf(string(jsonBytes) + "\n")
}
if hasError {
return fmt.Errorf("TopoCat: some paths had errors")
}
return nil
}