-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
status.go
317 lines (285 loc) · 8.84 KB
/
status.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
/*
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 tabletserver
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync/atomic"
"time"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
// This file contains the status web page export for tabletserver
const (
healthyClass = "healthy"
unhealthyClass = "unhealthy"
unhappyClass = "unhappy"
)
const (
// This template is a slight duplicate of the one in go/cmd/vttablet/status.go.
headerTemplate = `
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #999;
padding: 0.5rem;
}
.time {
width: 15%;
}
.healthy {
background-color: LightGreen;
}
.unhealthy {
background-color: Salmon;
}
.unhappy {
background-color: Khaki;
}
</style>
<table width="100%" border="" frame="">
<tr border="">
<td width="25%" border="">
Alias: {{.Alias}}<br>
Keyspace: {{.Target.Keyspace}}<br>
Shard: {{.Target.Shard}}<br>
TabletType: {{.Target.TabletType}}<br>
</td>
<td width="25%" border="">
<a href="{{.Prefix}}/schemaz">Schema</a></br>
<a href="{{.Prefix}}/debug/tablet_plans">Schema Query Plans</a></br>
<a href="{{.Prefix}}/debug/query_stats">Schema Query Stats</a></br>
<a href="{{.Prefix}}/queryz">Query Stats</a></br>
</td>
<td width="25%" border="">
<a href="{{.Prefix}}/debug/consolidations">Consolidations</a></br>
<a href="{{.Prefix}}/querylogz">Current Query Log</a></br>
<a href="{{.Prefix}}/txlogz">Current Transaction Log</a></br>
<a href="{{.Prefix}}/twopcz">In-flight 2PC Transactions</a></br>
</td>
<td width="25%" border="">
<a href="{{.Prefix}}/healthz">Health Check</a></br>
<a href="{{.Prefix}}/debug/health">Query Service Health Check</a></br>
<a href="{{.Prefix}}/livequeryz/">Real-time Queries</a></br>
<a href="{{.Prefix}}/debug/status_details">JSON Status Details</a></br>
<a href="{{.Prefix}}/debug/env">View/Change Environment variables</a></br>
</td>
</tr>
</table>
`
queryserviceStatusTemplate = `
<div style="font-size: x-large">Current status: <span style="padding-left: 0.5em; padding-right: 0.5em; padding-bottom: 0.5ex; padding-top: 0.5ex;" class="{{.Latest.Class}}">{{.Latest.Status}}</span></div>
<h2>Health Details</h2>
<table>
{{range .Details}}
<tr class="{{.Class}}">
<td>{{.Key}}</td>
<td>{{.Value}}</td>
</tr>
{{end}}
</table>
<h2>Health History</h2>
<table>
<tr>
<th>Time</th>
<th>Status</th>
<th>Tablet Type</th>
</tr>
{{range .History}}
<tr class="{{.Class}}">
<td>{{.Time.Format "Jan 2, 2006 at 15:04:05 (MST)"}}</td>
<td>{{.Status}}</td>
<td>{{.TabletType}}</td>
</tr>
{{end}}
</table>
<dl style="font-size: small;">
<dt><span class="healthy">healthy</span></dt>
<dd>serving traffic.</dd>
<dt><span class="unhappy">unhappy</span></dt>
<dd>will serve traffic only if there are no fully healthy tablets.</dd>
<dt><span class="unhealthy">unhealthy</span></dt>
<dd>will not serve traffic.</dd>
</dl>
<!-- The div in the next line will be overwritten by the JavaScript graph. -->
<div id="qps_chart" style="height: 500px; width: 900px">QPS: {{.CurrentQPS}}</div>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
function sampleDate(d, i) {
var copy = new Date(d);
copy.setTime(copy.getTime() - i*60/5*1000);
return copy
}
function drawQPSChart() {
var div = document.getElementById("qps_chart")
var chart = new google.visualization.LineChart(div);
var options = {
title: "QPS",
focusTarget: 'category',
vAxis: {
viewWindow: {min: 0},
}
};
// If we're accessing status through a proxy that requires a URL prefix,
// add the prefix to the vars URL.
var vars_url = '/debug/vars';
var pos = window.location.pathname.lastIndexOf('/debug/status');
if (pos > 0) {
vars_url = window.location.pathname.substring(0, pos) + vars_url;
}
const redraw = () => fetch(vars_url)
.then(async (response) => {
const input_data = await response.json();
var now = new Date();
var qps = input_data.QPS;
var planTypes = Object.keys(qps);
if (planTypes.length === 0) {
planTypes = ["All"];
qps["All"] = [];
}
var data = [["Time"].concat(planTypes)];
// Create data points, starting with the most recent timestamp.
// (On the graph this means going from right to left.)
// Time span: 15 minutes in 5 second intervals.
for (var i = 0; i < 15*60/5; i++) {
var datum = [sampleDate(now, i)];
for (var j = 0; j < planTypes.length; j++) {
if (i < qps[planTypes[j]].length) {
// Rates are ordered from least recent to most recent.
// Therefore, we have to start reading from the end of the array.
var idx = qps[planTypes[j]].length - i - 1;
datum.push(+qps[planTypes[j]][idx].toFixed(2));
} else {
// Assume 0.0 QPS for older, non-existent data points.
datum.push(0);
}
}
data.push(datum)
}
chart.draw(google.visualization.arrayToDataTable(data), options);
})
redraw();
// redraw every 2.5 seconds.
window.setInterval(redraw, 2500);
}
google.setOnLoadCallback(drawQPSChart);
</script>
`
)
type queryserviceStatus struct {
Latest *historyRecord
Details []*kv
History []any
CurrentQPS float64
}
type kv struct {
Key string
Class string
Value string
}
// AddStatusHeader registers a standlone header for the status page.
func (tsv *TabletServer) AddStatusHeader() {
tsv.exporter.AddStatusPart("Tablet Server", headerTemplate, func() any {
return map[string]any{
"Alias": tsv.exporter.Name(),
"Prefix": tsv.exporter.URLPrefix(),
"Target": tsv.sm.Target(),
}
})
}
// AddStatusPart registers the status part for the status page.
func (tsv *TabletServer) AddStatusPart() {
// Save the threshold values for reporting.
degradedThreshold.Store(tsv.config.Healthcheck.DegradedThresholdSeconds.Get().Nanoseconds())
unhealthyThreshold.Store(tsv.config.Healthcheck.UnhealthyThresholdSeconds.Get().Nanoseconds())
tsv.exporter.AddStatusPart("Health", queryserviceStatusTemplate, func() any {
status := queryserviceStatus{
History: tsv.hs.history.Records(),
}
latest := tsv.hs.history.Latest()
if latest != nil {
status.Latest = latest.(*historyRecord)
} else {
status.Latest = &historyRecord{}
}
status.Details = tsv.sm.AppendDetails(nil)
status.Details = tsv.hs.AppendDetails(status.Details)
rates := tsv.stats.QPSRates.Get()
if qps, ok := rates["All"]; ok && len(qps) > 0 {
status.CurrentQPS = qps[0]
}
return status
})
tsv.exporter.HandleFunc("/debug/status_details", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
details := tsv.sm.AppendDetails(nil)
details = tsv.hs.AppendDetails(details)
b, err := json.MarshalIndent(details, "", " ")
if err != nil {
w.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
w.Write(buf.Bytes())
})
}
var degradedThreshold atomic.Int64
var unhealthyThreshold atomic.Int64
type historyRecord struct {
Time time.Time
serving bool
tabletType topodatapb.TabletType
lag time.Duration
err error
}
func (r *historyRecord) Class() string {
if r.serving {
if r.lag > time.Duration(degradedThreshold.Load()) {
return unhappyClass
}
return healthyClass
}
return unhealthyClass
}
func (r *historyRecord) Status() string {
if r.serving {
if r.lag > time.Duration(degradedThreshold.Load()) {
return fmt.Sprintf("replication delayed: %v", r.lag)
}
return "healthy"
}
if r.lag > time.Duration(unhealthyThreshold.Load()) {
return fmt.Sprintf("not serving: replication delay %v", r.lag)
}
if r.err != nil {
return fmt.Sprintf("not serving: %v", r.err)
}
return "not serving"
}
func (r *historyRecord) TabletType() string {
return strings.ToLower(r.tabletType.String())
}
// IsDuplicate implements history.Deduplicable
func (r *historyRecord) IsDuplicate(other any) bool {
rother, ok := other.(*historyRecord)
if !ok {
return false
}
return r.tabletType == rother.tabletType && r.serving == rother.serving && r.err == rother.err
}