-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
322 lines (295 loc) · 9.04 KB
/
web.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
package main
import (
"context"
"html/template"
"log"
"net/http"
"time"
"github.com/childoftheuniverse/red-cloud"
"github.com/childoftheuniverse/red-cloud/common"
"go.opencensus.io/trace"
)
var funcMap = map[string]interface{}{
"bytes": common.PrettyBytes,
"since": common.TimeSince,
}
var nodeTemplate = template.Must(
template.New("statuspage").Funcs(funcMap).Parse(`
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Status of {{ .Instance }} node {{ .Node.Address }}</title>
<link rel="stylesheet" type="text/css" href="{{ .BootstrapCSSPath }}"
integrity="{{ .BootstrapCSSHash }}" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h1>Status of red-cloud node {{ .Node.Address }}
<small>{{ .Instance }}</small></h1>
<dl>
<dt>Address</dt>
<dd><a href="http://{{ .Node.StatusAddress }}">{{ .Node.Address }}</a></td>
<dt>Status</dt>
<dd>{{ if .Node.IsHealthy }}Healthy{{ else }}Unhealthy{{ end }}</dd>
<dt>Heap usage</dt>
<dd title="{{ .Node.HeapUsage }}">{{ bytes .Node.HeapUsage }}</dd>
<dt>Last check</dt>
<dd title="{{ .Node.LastCheck }}">{{ since .Node.LastCheck }} ago</dd>
</dl>
<h2>Assigned tablets</h2>
{{ range $table, $tablets := .Tablets }}
<h3><a href="/table?id={{ urlquery $table }}">{{ $table }}</a></h3>
<table class="table table-striped">
<thead>
<tr><th>Start key</th><th>End key</th></tr>
</thead>
<tbody>
{{ range $tablets }}
<tr><td>{{ .StartKey }}</td><td>{{ .EndKey }}</td></tr>
{{ end }}
</tbody>
</table>
{{ end }}
</div>
</body>
</html>
`))
type nodeStatusData struct {
BootstrapCSSPath string
BootstrapCSSHash string
Instance string
Node *DataNode
Tablets map[string][]*common.KeyRange
}
var tableStatusTemplate = template.Must(
template.New("statuspage").Funcs(funcMap).Parse(`
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Tablets of red-cloud://{{ .Instance }}/{{ .Table }}</title>
<link rel="stylesheet" type="text/css" href="{{ .BootstrapCSSPath }}"
integrity="{{ .BootstrapCSSHash }}" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h1>Tablets of red-cloud://{{ .Instance }}/{{ .Table }}
<small>{{ .Instance }}</small></h1>
<table class="table table-striped">
<thead>
<tr><th>Start key</th><th>End key</th><th>Node</th></tr>
</thead>
<tbody>
{{ range .Tablets }}
<tr>
<td>{{ .StartKey }}</td>
<td>{{ .EndKey }}</td>
<td><a href="/node?id={{ urlquery .Host }}:{{ .Port }}">{{ .Host }}:{{ .Port }}</a></td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</body>
</html>
`))
type tableStatusData struct {
BootstrapCSSPath string
BootstrapCSSHash string
Instance string
Table string
Tablets []*redcloud.ServerTabletMetadata
}
var mainStatusTemplate = template.Must(
template.New("statuspage").Funcs(funcMap).Parse(`
<!DOCTYPE html PUBLIC>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Status of {{.Instance}}</title>
<link rel="stylesheet" type="text/css" href="{{ .BootstrapCSSPath }}"
integrity="{{ .BootstrapCSSHash }}" crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h1>Instance Status <small>{{.Instance}}</small></h1>
<div class="row">
<div class="col-md-6">
<h2>Active nodes</h2>
<table class="table table-striped">
<thead>
<tr><th>Node address</th><th>Heap Usage</th><th>Last seen</th></tr>
</thead>
<tbody>
{{range .Alive}}
<tr>
<td><a href="/node?id={{ urlquery .Address }}">{{ .Address }}</a></td>
<td title="{{ .HeapUsage }}">{{ bytes .HeapUsage }}</td>
<td title="{{ .LastCheck }}">{{ since .LastCheck }} ago</td>
</tr>
{{end}}
</tbody>
</table>
</div>
<div class="col-md-6">
<h2>Missing nodes</h2>
<table class="table table-striped">
<thead>
<tr><th>Node address</th><th>Heap Usage</th><th>Last seen</th></tr>
</thead>
<tbody>
{{range .Dead}}
<tr>
<td><a href="/node?id={{ urlquery .Address }}">{{ .Address }}</a></td>
<td title="{{ .HeapUsage }}">{{ bytes .HeapUsage }}</td>
<td title="{{ .LastCheck }}">{{ since .LastCheck }} ago</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
<h2>Known tables</h2>
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<thead>
<tr><th>Table name</th><th>Tablets</th></tr>
</thead>
<tbody>
{{range .Tables}}
<tr>
<td><a href="/table?id={{ urlquery .Name }}">{{ .Name }}</a></td>
<td><a href="/table?id={{ urlquery .Name }}">{{ .Tablet | len }}</a></td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
`))
/*
mainStatusData provides data for the mainStatusTemplate.
*/
type mainStatusData struct {
BootstrapCSSPath string
BootstrapCSSHash string
Instance string
Alive []*DataNode
Dead []*DataNode
Tables []*redcloud.ServerTableMetadata
}
/*
StatusWebService provides a small amount of information about the current
status of the red-cloud caretaker node as a web interface.
*/
type StatusWebService struct {
registry *DataNodeRegistry
bootstrapCSSPath string
bootstrapCSSHash string
}
/*
NewStatusWebService creates a new status web service object which can be
exported via HTTP.
*/
func NewStatusWebService(
bootstrapCSSPath, bootstrapCSSHash string,
registry *DataNodeRegistry) *StatusWebService {
return &StatusWebService{
registry: registry,
bootstrapCSSPath: bootstrapCSSPath,
bootstrapCSSHash: bootstrapCSSHash,
}
}
/*
ServeHTTP serves status information about the state of the caretaker node
as an HTML page over HTTP.
*/
func (s *StatusWebService) ServeHTTP(
rw http.ResponseWriter, req *http.Request) {
var parentCtx context.Context
var ctx context.Context
var cancel context.CancelFunc
var span *trace.Span
parentCtx, cancel = context.WithTimeout(req.Context(), 20*time.Second)
defer cancel()
ctx, span = trace.StartSpan(
parentCtx, "red-cloud.StatusWebService/ServeHTTP")
defer span.End()
span.AddAttributes(
trace.StringAttribute("path", req.URL.Path))
if req.URL.Path == "/node" {
var nodeData = nodeStatusData{
BootstrapCSSPath: s.bootstrapCSSPath,
BootstrapCSSHash: s.bootstrapCSSHash,
Instance: s.registry.Instance(),
}
var err error
span.AddAttributes(
trace.StringAttribute("target-address", req.FormValue("id")))
if nodeData.Node = s.registry.GetNodeByAddress(
ctx, req.FormValue("id")); nodeData.Node == nil {
span.Annotate(nil, "Node not found")
http.Error(rw, "No such node", http.StatusNotFound)
return
}
if nodeData.Tablets, err = s.registry.GetNodeTablets(
ctx, req.FormValue("id")); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error determining tablets held")
log.Print("Error determining tablets held by ",
req.FormValue("id"), ": ", err)
}
if err = nodeTemplate.Execute(rw, nodeData); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error sending node status page")
log.Print("Error sending node status page: ", err)
}
} else if req.URL.Path == "/table" {
var tableData = tableStatusData{
BootstrapCSSPath: s.bootstrapCSSPath,
BootstrapCSSHash: s.bootstrapCSSHash,
Instance: s.registry.Instance(),
Table: req.FormValue("id"),
}
var err error
span.AddAttributes(
trace.StringAttribute("table", req.FormValue("id")))
if tableData.Tablets, err = s.registry.GetTablets(
ctx, req.FormValue("id")); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error requesting table metadata")
log.Print("Error determining the tablets in table ",
req.FormValue("id"), ": ", err)
}
if err = tableStatusTemplate.Execute(rw, tableData); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error sending table status page")
log.Print("Error sending table status page: ", err)
}
} else {
var mainData = mainStatusData{
BootstrapCSSPath: s.bootstrapCSSPath,
BootstrapCSSHash: s.bootstrapCSSHash,
Instance: s.registry.Instance(),
}
var mds map[string]*redcloud.ServerTableMetadata
var md *redcloud.ServerTableMetadata
var err error
mainData.Alive, mainData.Dead = s.registry.GetNodeLists(ctx)
if mds, err = s.registry.GetTableList(ctx); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error fetching table list")
log.Print("Error fetching table list: ", err)
}
for _, md = range mds {
mainData.Tables = append(mainData.Tables, md)
}
if err = mainStatusTemplate.Execute(rw, mainData); err != nil {
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error sending status page")
log.Print("Error sending status page: ", err)
}
}
}