/
http.go
241 lines (213 loc) · 6.89 KB
/
http.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
package ring
import (
"context"
"encoding/json"
"fmt"
"html/template"
"math"
"net/http"
"sort"
"strings"
"time"
"github.com/go-kit/log/level"
)
const pageContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ring Status</title>
</head>
<body>
<h1>Ring Status</h1>
<p>Current time: {{ .Now }}</p>
<p>Storage updated: {{ .StorageLastUpdated }}</p>
<form action="" method="POST">
<input type="hidden" name="csrf_token" value="$__CSRF_TOKEN_PLACEHOLDER__">
<table width="100%" border="1">
<thead>
<tr>
<th>Instance ID</th>
<th>Availability Zone</th>
<th>State</th>
<th>Address</th>
<th>Registered At</th>
<th>Last Heartbeat</th>
<th>Tokens</th>
<th>Ownership</th>
<th>Ownership Diff From Expected</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{ range $i, $ing := .Ingesters }}
{{ if mod $i 2 }}
<tr>
{{ else }}
<tr bgcolor="#BEBEBE">
{{ end }}
<td>{{ .ID }}</td>
<td>{{ .Zone }}</td>
<td>{{ .State }}</td>
<td>{{ .Address }}</td>
<td>{{ .RegisteredTimestamp }}</td>
<td>{{ .HeartbeatTimestamp }}</td>
<td>{{ .NumTokens }}</td>
<td>{{ .Ownership }}%</td>
<td>{{ .DiffOwnership }}%</td>
<td><button name="forget" value="{{ .ID }}" type="submit">Forget</button></td>
</tr>
{{ end }}
</tbody>
</table>
<br>
{{ if .ShowTokens }}
<input type="button" value="Hide Tokens" onclick="window.location.href = '?tokens=false' " />
{{ else }}
<input type="button" value="Show Tokens" onclick="window.location.href = '?tokens=true'" />
{{ end }}
{{ if .ShowTokens }}
{{ range $i, $ing := .Ingesters }}
<h2>Instance: {{ .ID }}</h2>
<p>
Tokens:<br />
{{ range $token := .Tokens }}
{{ $token }}
{{ end }}
</p>
{{ end }}
{{ end }}
</form>
</body>
</html>`
var pageTemplate *template.Template
func init() {
t := template.New("webpage")
t.Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }})
pageTemplate = template.Must(t.Parse(pageContent))
}
func (r *Ring) forget(ctx context.Context, id string) error {
unregister := func(in interface{}) (out interface{}, retry bool, err error) {
if in == nil {
return nil, false, fmt.Errorf("found empty ring when trying to unregister")
}
ringDesc := in.(*Desc)
ringDesc.RemoveIngester(id)
return ringDesc, true, nil
}
return r.KVClient.CAS(ctx, r.key, unregister)
}
type ingesterDesc struct {
ID string `json:"id"`
State string `json:"state"`
Address string `json:"address"`
HeartbeatTimestamp string `json:"timestamp"`
RegisteredTimestamp string `json:"registered_timestamp"`
Zone string `json:"zone"`
Tokens []uint32 `json:"tokens"`
NumTokens int `json:"-"`
Ownership float64 `json:"-"`
DiffOwnership float64 `json:"-"`
}
type httpResponse struct {
Ingesters []ingesterDesc `json:"shards"`
Now time.Time `json:"now"`
StorageLastUpdated time.Time `json:"storageLastUpdated"`
ShowTokens bool `json:"-"`
}
func (r *Ring) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
ingesterID := req.FormValue("forget")
if err := r.forget(req.Context(), ingesterID); err != nil {
level.Error(r.logger).Log("msg", "error forgetting instance", "err", err)
}
// Implement PRG pattern to prevent double-POST and work with CSRF middleware.
// https://en.wikipedia.org/wiki/Post/Redirect/Get
// http.Redirect() would convert our relative URL to absolute, which is not what we want.
// Browser knows how to do that, and it also knows real URL. Furthermore it will also preserve tokens parameter.
// Note that relative Location URLs are explicitly allowed by specification, so we're not doing anything wrong here.
w.Header().Set("Location", "#")
w.WriteHeader(http.StatusFound)
return
}
r.mtx.RLock()
defer r.mtx.RUnlock()
ingesterIDs := []string{}
for id := range r.ringDesc.Ingesters {
ingesterIDs = append(ingesterIDs, id)
}
sort.Strings(ingesterIDs)
storageLastUpdate := r.KVClient.LastUpdateTime(r.key)
var ingesters []ingesterDesc
for _, id := range ingesterIDs {
ing := r.ringDesc.Ingesters[id]
heartbeatTimestamp := time.Unix(ing.Timestamp, 0)
state := ing.State.String()
if !r.IsHealthy(&ing, Reporting, storageLastUpdate) {
state = unhealthy
}
// Format the registered timestamp.
registeredTimestamp := ""
if ing.RegisteredTimestamp != 0 {
registeredTimestamp = ing.GetRegisteredAt().String()
}
var ownership float64
var deltaOwnership float64
if r.cfg.ZoneAwarenessEnabled {
numTokens, ownedByAz := r.countTokensByAz()
ownership = (float64(ownedByAz[id]) / float64(math.MaxUint32+1)) * 100
expectedOwnership := 1 / float64(len(numTokens[ing.Zone])) * 100
deltaOwnership = (1 - expectedOwnership/ownership) * 100
} else {
_, owned := r.countTokens()
ownership = (float64(owned[id]) / float64(math.MaxUint32+1)) * 100
expectedOwnership := 1 / float64(len(owned)) * 100
deltaOwnership = (1 - expectedOwnership/ownership) * 100
}
ingesters = append(ingesters, ingesterDesc{
ID: id,
State: state,
Address: ing.Addr,
HeartbeatTimestamp: heartbeatTimestamp.String(),
RegisteredTimestamp: registeredTimestamp,
Tokens: ing.Tokens,
Zone: ing.Zone,
NumTokens: len(ing.Tokens),
Ownership: ownership,
DiffOwnership: deltaOwnership,
})
}
tokensParam := req.URL.Query().Get("tokens")
renderHTTPResponse(w, httpResponse{
Ingesters: ingesters,
Now: time.Now(),
StorageLastUpdated: storageLastUpdate,
ShowTokens: tokensParam == "true",
}, pageTemplate, req)
}
// RenderHTTPResponse either responds with json or a rendered html page using the passed in template
// by checking the Accepts header
func renderHTTPResponse(w http.ResponseWriter, v httpResponse, t *template.Template, r *http.Request) {
accept := r.Header.Get("Accept")
if strings.Contains(accept, "application/json") {
writeJSONResponse(w, v)
return
}
err := t.Execute(w, v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// WriteJSONResponse writes some JSON as a HTTP response.
func writeJSONResponse(w http.ResponseWriter, v httpResponse) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We ignore errors here, because we cannot do anything about them.
// Write will trigger sending Status code, so we cannot send a different status code afterwards.
// Also this isn't internal error, but error communicating with client.
_, _ = w.Write(data)
}