forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.go
160 lines (141 loc) · 5.59 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
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
package main
import (
"errors"
"fmt"
"net/http"
"path"
"strings"
"sync"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/cmd/vtctld/proto"
"github.com/youtube/vitess/go/vt/topo/topoproto"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// Explorer allows exploring a topology server.
type Explorer interface {
// HandlePath returns a result (suitable to be passed to a
// template) appropriate for url, using actionRepo to populate
// the actions in result.
HandlePath(actionRepo proto.ActionRepository, url string, r *http.Request) interface{}
// GetKeyspacePath returns an explorer path that will contain
// information about the named keyspace.
GetKeyspacePath(keyspace string) string
// GetShardPath returns an explorer path that will contain
// information about the named shard in the named keyspace.
GetShardPath(keyspace, shard string) string
// GetSrvKeyspacePath returns an explorer path that will
// contain information about the named keyspace in the serving
// graph for cell.
GetSrvKeyspacePath(cell, keyspace string) string
// GetShardPath returns an explorer path that will contain
// information about the named shard in the named keyspace in
// the serving graph for cell.
GetSrvShardPath(cell, keyspace, shard string) string
// GetShardTypePath returns an explorer path that will contain
// information about the named tablet type in the named shard
// in the named keyspace in the serving graph for cell.
GetSrvTypePath(cell, keyspace, shard string, tabletType pb.TabletType) string
// GetTabletPath returns an explorer path that will contain
// information about the tablet named by alias.
GetTabletPath(alias *pb.TabletAlias) string
// GetReplicationSlaves returns an explorer path that contains
// replication slaves for the named cell, keyspace, and shard.
GetReplicationSlaves(cell, keyspace, shard string) string
}
var (
// explorerMutex protects against concurrent registration attempts (e.g. OnRun).
// Other than registration, the explorer should never change.
explorerMutex sync.Mutex
explorerName string
explorer Explorer
)
// HandleExplorer registers the Explorer under url, using the given template.
// It should be called by a plugin either from init() or from servenv.OnRun().
// Only one Explorer can be registered in a given instance of vtctld.
func HandleExplorer(name, url, templateName string, exp Explorer) {
explorerMutex.Lock()
defer explorerMutex.Unlock()
if explorer != nil {
panic(fmt.Sprintf("Only one Explorer can be registered in vtctld. Trying to register %q, but %q was already registered.", name, explorerName))
}
// Topo explorer API for client-side vtctld app.
handleCollection("topodata", func(r *http.Request) (interface{}, error) {
return exp.HandlePath(actionRepo, path.Clean(url+getItemPath(r.URL.Path)), r), nil
})
// Old server-side explorer.
explorer = exp
explorerName = name
indexContent.ToplevelLinks[name+" Explorer"] = url
http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpErrorf(w, r, "cannot parse form: %s", err)
return
}
topoPath := r.URL.Path[strings.Index(r.URL.Path, url):]
if cleanPath := path.Clean(topoPath); topoPath != cleanPath && topoPath != cleanPath+"/" {
log.Infof("redirecting to %v", cleanPath)
http.Redirect(w, r, cleanPath, http.StatusTemporaryRedirect)
return
}
if strings.HasSuffix(topoPath, "/") {
topoPath = topoPath[:len(topoPath)-1]
}
result := explorer.HandlePath(actionRepo, topoPath, r)
templateLoader.ServeTemplate(templateName, result, w, r)
})
}
// handleExplorerRedirect returns the redirect target URL.
func handleExplorerRedirect(r *http.Request) (string, error) {
keyspace := r.FormValue("keyspace")
shard := r.FormValue("shard")
cell := r.FormValue("cell")
switch r.FormValue("type") {
case "keyspace":
if keyspace == "" {
return "", errors.New("keyspace is required for this redirect")
}
return explorer.GetKeyspacePath(keyspace), nil
case "shard":
if keyspace == "" || shard == "" {
return "", errors.New("keyspace and shard are required for this redirect")
}
return explorer.GetShardPath(keyspace, shard), nil
case "srv_keyspace":
if keyspace == "" || cell == "" {
return "", errors.New("keyspace and cell are required for this redirect")
}
return explorer.GetSrvKeyspacePath(cell, keyspace), nil
case "srv_shard":
if keyspace == "" || shard == "" || cell == "" {
return "", errors.New("keyspace, shard, and cell are required for this redirect")
}
return explorer.GetSrvShardPath(cell, keyspace, shard), nil
case "srv_type":
tabletType := r.FormValue("tablet_type")
if keyspace == "" || shard == "" || cell == "" || tabletType == "" {
return "", errors.New("keyspace, shard, cell, and tablet_type are required for this redirect")
}
tt, err := topoproto.ParseTabletType(tabletType)
if err != nil {
return "", fmt.Errorf("cannot parse tablet type %v: %v", tabletType, err)
}
return explorer.GetSrvTypePath(cell, keyspace, shard, tt), nil
case "tablet":
alias := r.FormValue("alias")
if alias == "" {
return "", errors.New("alias is required for this redirect")
}
tabletAlias, err := topoproto.ParseTabletAlias(alias)
if err != nil {
return "", fmt.Errorf("bad tablet alias %q: %v", alias, err)
}
return explorer.GetTabletPath(tabletAlias), nil
case "replication":
if keyspace == "" || shard == "" || cell == "" {
return "", errors.New("keyspace, shard, and cell are required for this redirect")
}
return explorer.GetReplicationSlaves(cell, keyspace, shard), nil
default:
return "", errors.New("bad redirect type")
}
}