forked from vitessio/vitess
/
explorer.go
117 lines (102 loc) · 2.99 KB
/
explorer.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
// Copyright 2014, 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 etcdtopo
import (
"fmt"
"net/http"
"path"
"strings"
"github.com/coreos/go-etcd/etcd"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtctld/explorer"
)
// Explorer is an implementation of vtctld's Explorer interface for etcd.
type Explorer struct {
ts *Server
}
// NewExplorer implements vtctld Explorer.
func NewExplorer(ts *Server) *Explorer {
return &Explorer{ts: ts}
}
// HandlePath implements vtctld Explorer.
func (ex Explorer) HandlePath(rPath string, r *http.Request) *explorer.Result {
result := &explorer.Result{}
// Root is a list of cells.
if rPath == "/" {
cells, err := ex.ts.getCellList()
if err != nil {
result.Error = err.Error()
return result
}
result.Children = append([]string{topo.GlobalCell}, cells...)
return result
}
// Get a client for the requested cell.
var client Client
cell, rPath, err := splitCellPath(rPath)
if err != nil {
result.Error = err.Error()
return result
}
if cell == topo.GlobalCell {
client = ex.ts.getGlobal()
} else {
client, err = ex.ts.getCell(cell)
if err != nil {
result.Error = "Can't get cell: " + err.Error()
return result
}
}
// Get the requested node data.
resp, err := client.Get(rPath, true /* sort */, false /* recursive */)
if err != nil {
result.Error = err.Error()
return result
}
if resp.Node == nil {
result.Error = ErrBadResponse.Error()
return result
}
result.Data = getNodeData(client, resp.Node)
// Populate children.
for _, node := range resp.Node.Nodes {
result.Children = append(result.Children, path.Base(node.Key))
}
return result
}
func getNodeData(client Client, node *etcd.Node) string {
if !node.Dir {
return node.Value
}
// Directories don't have data, but some directories have a special data file.
resp, err := client.Get(path.Join(node.Key, dataFilename), false /* sort */, false /* recursive */)
if err != nil || resp.Node == nil {
return ""
}
return resp.Node.Value
}
// splitCellPath returns the cell name, and the rest of the path.
// For example: "/cell/rest/of/path" -> "cell", "/rest/of/path"
func splitCellPath(p string) (cell, rest string, err error) {
parts := strings.SplitN(p, "/", 3)
if len(parts) < 2 || parts[0] != "" {
return "", "", fmt.Errorf("invalid etcd explorer path: %v", p)
}
if len(parts) < 3 {
return parts[1], "/", nil
}
return parts[1], "/" + parts[2], nil
}
// splitShardDirPath takes a path that matches the path.Match() pattern
// shardDirPath("*", "*") and returns the keyspace and shard.
//
// We assume the path is of the form "/vt/keyspaces/*/*".
// If that ever changes, the unit test for this function will detect it.
func splitShardDirPath(p string) (keyspace, shard string, err error) {
parts := strings.Split(p, "/")
if len(parts) != 5 {
return "", "", fmt.Errorf("invalid shard dir path: %v", p)
}
return parts[3], parts[4], nil
}