-
Notifications
You must be signed in to change notification settings - Fork 1
/
vbmap.go
353 lines (292 loc) · 8.7 KB
/
vbmap.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/couchbaselabs/go-couchbase"
)
type vbmap map[string][]uint16
func maybefatal(err error, f string, args ...interface{}) {
if err != nil {
panic(fmt.Sprintf(f, args...))
}
}
func getVbMapsMC(bucket *couchbase.Bucket, commonSuffixMC string) map[string]vbmap {
allstats := bucket.GetStats("vbucket")
rv := map[string]vbmap{}
for fullname, m := range allstats {
sn := couchbase.CleanupHost(fullname, commonSuffixMC)
rv[sn] = vbmap{}
for k, v := range m {
vb, err := strconv.ParseInt(k[3:], 10, 16)
maybefatal(err, "Error parsing vbucket: %v/%v: %v",
k, v, err)
rv[sn][v] = append(rv[sn][v], uint16(vb))
}
}
return rv
}
func getServerStates(bucket *couchbase.Bucket, commonSuffixMC string) map[string]string {
rv := make(map[string]string)
for _, node := range bucket.Nodes {
rv[couchbase.CleanupHost(node.Hostname, commonSuffixMC)] = node.Status
}
return rv
}
func getShortServerList(bucket *couchbase.Bucket, commonSuffixMC string) []string {
rv := []string{}
for _, node := range bucket.VBucketServerMap.ServerList {
rv = append(rv, couchbase.CleanupHost(node, commonSuffixMC))
}
return rv
}
func getBucket(req *http.Request) *couchbase.Bucket {
req.ParseForm()
clusterurl := req.Form.Get("cluster")
if clusterurl == "" {
clusterurl = flag.Arg(0)
}
bucketName := req.Form.Get("bucket")
client, err := couchbase.Connect(clusterurl)
maybefatal(err, "Error connecting to cluster: %v", err)
pool, err := client.GetPool("default")
maybefatal(err, "Error getting pool: %v", err)
var bucket *couchbase.Bucket
if bucketName == "" {
for n, b := range pool.BucketMap {
if bucket != nil {
err = errors.New("Too many buckets found.")
}
bucketName = n
bucket = &b
}
} else {
bucket, err = pool.GetBucket(bucketName)
}
log.Printf("Got bucket %v from %v", bucketName, clusterurl)
return bucket
}
func displayMap(w http.ResponseWriter, req *http.Request, bucket *couchbase.Bucket) {
commonSuffix := bucket.CommonAddressSuffix()
commonSuffixMC := couchbase.FindCommonSuffix(bucket.VBucketServerMap.ServerList)
req.ParseForm()
var_name := req.FormValue("name")
rv := map[string]interface{}{}
// rv["mc_vbmap"] = getVbMapsMC(bucket, commonSuffixMC)
rv["server_list"] = getShortServerList(bucket, commonSuffixMC)
rv["repmap"] = bucket.VBucketServerMap.VBucketMap
rv["server_states"] = getServerStates(bucket, commonSuffix)
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
json.NewEncoder(w).Encode(rv)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
func mapHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
bucket := getBucket(req)
defer bucket.Close()
}
type vbstats map[string]map[string]interface{}
func processVBDetails(vbd map[string]map[string]string,
commonSuffixMC string) map[string]vbstats {
rv := map[string]vbstats{}
for fullname, m := range vbd {
sn := couchbase.CleanupHost(fullname, commonSuffixMC)
rv[sn] = vbstats{}
for k, v := range m {
var parts = strings.Split(k[3:], ":")
vbbig, err := strconv.ParseInt(parts[0], 10, 16)
maybefatal(err, "Error parsing vbucket: %v/%v: %v",
k, v, err)
vb := fmt.Sprintf("%d", vbbig)
label := "state"
if len(parts) == 2 {
label = parts[1]
}
d, ok := rv[sn][vb]
if !ok {
d = make(map[string]interface{})
rv[sn][vb] = d
}
rv[sn][vb][label] = v
}
}
return rv
}
func getVbStats(bucket *couchbase.Bucket, commonSuffixMC string) map[string]vbstats {
return processVBDetails(bucket.GetStats("vbucket-details"), commonSuffixMC)
}
func processGenericStats(in map[string]map[string]string, commonSuffixMC string) map[string]map[string]string {
out := map[string]map[string]string{}
for k, v := range in {
out[couchbase.CleanupHost(k, commonSuffixMC)] = v
}
return out
}
func getStats(bucket *couchbase.Bucket, commonSuffixMC string) map[string]map[string]string {
return processGenericStats(bucket.GetStats(""), commonSuffixMC)
}
func statsHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
bucket := getBucket(req)
defer bucket.Close()
commonSuffixMC := couchbase.FindCommonSuffix(bucket.VBucketServerMap.ServerList)
rv := getStats(bucket, commonSuffixMC)
req.ParseForm()
var_name := req.FormValue("name")
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
err := json.NewEncoder(w).Encode(rv)
maybefatal(err, "Error encoding output: %v", err)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
func vbHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
bucket := getBucket(req)
defer bucket.Close()
commonSuffixMC := couchbase.FindCommonSuffix(bucket.VBucketServerMap.ServerList)
rv := getVbStats(bucket, commonSuffixMC)
req.ParseForm()
var_name := req.FormValue("name")
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
err := json.NewEncoder(w).Encode(rv)
maybefatal(err, "Error encoding output: %v", err)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
type handler func(http.ResponseWriter, *http.Request)
func files(contentType string, paths ...string) handler {
ch := make(chan string)
go func() {
for {
for _, p := range paths {
ch <- p
}
}
}()
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", contentType)
// If there are multiple paths, we start returning
// 304s for all requests very quickly.
if len(paths) > 1 {
req.Header.Del("If-Modified-Since")
}
// Ugly hack for variable definition type call.
req.ParseForm()
var_name := req.FormValue("name")
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
http.ServeFile(w, req, <-ch)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
}
func replaymapHandler(w http.ResponseWriter, req *http.Request) {
re := currentState.current()
displayMap(w, req, &re.Bucket)
}
func replayvbHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
re := currentState.current()
conv := map[string]map[string]string{}
for s, m := range re.VBDetails {
out, ok := conv[s]
if !ok {
out = make(map[string]string)
conv[s] = out
}
for k, v := range m {
out[k] = fmt.Sprintf("%v", v)
}
}
vbd := processVBDetails(conv, "")
req.ParseForm()
var_name := req.FormValue("name")
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
err := json.NewEncoder(w).Encode(vbd)
maybefatal(err, "Error encoding output: %v", err)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
func replaystatsHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
re := currentState.current()
conv := map[string]map[string]string{}
for s, m := range re.All {
out, ok := conv[s]
if !ok {
out = make(map[string]string)
conv[s] = out
}
for k, v := range m {
out[k] = fmt.Sprintf("%v", v)
}
}
req.ParseForm()
var_name := req.FormValue("name")
if var_name != "" {
fmt.Fprintf(w, "var "+var_name+" = ")
}
err := json.NewEncoder(w).Encode(conv)
maybefatal(err, "Error encoding output: %v", err)
if var_name != "" {
fmt.Fprintf(w, ";")
}
}
func main() {
staticPath := flag.Bool("static", false,
"Interpret URL as a static path (for testing)")
replayPath := flag.Bool("replay", false,
"Provide a replay json.gz for /map and /vb")
replaySpeed := flag.Float64("replaySpeed", 1.0,
"Realtime multiplier for replay")
flag.Parse()
http.HandleFunc("/", files("text/html", "root.html"))
http.HandleFunc("/custom", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/custom.html", http.StatusMovedPermanently)
})
http.Handle("/static/", http.FileServer(http.Dir(".")))
if *staticPath && *replayPath {
log.Fatalf("Static and replay paths are mutually exclusive.")
}
switch {
default:
http.HandleFunc("/map", mapHandler)
http.HandleFunc("/vb", vbHandler)
http.HandleFunc("/stats", statsHandler)
case *staticPath:
http.HandleFunc("/map", files("application/json", flag.Args()...))
http.HandleFunc("/vb", files("application/json", flag.Args()...))
http.HandleFunc("/stats", files("application/json", flag.Args()...))
case *replayPath:
go startReplay(*replaySpeed, flag.Arg(0))
http.HandleFunc("/map", replaymapHandler)
http.HandleFunc("/vb", replayvbHandler)
http.HandleFunc("/stats", replaystatsHandler)
}
log.Fatal(http.ListenAndServe(":4444", nil))
}