forked from jehiah/sortdb
/
http.go
208 lines (187 loc) · 5.69 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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
httpprof "net/http/pprof"
"os"
"strconv"
"sync/atomic"
"time"
"github.com/bitly/timer_metrics"
)
type httpServer struct {
ctx *Context
Requests uint64
GetRequests uint64
GetHits uint64
GetMisses uint64
MgetRequests uint64
MgetHits uint64
MgetMisses uint64
GetMetrics *timer_metrics.TimerMetrics
MgetMetrics *timer_metrics.TimerMetrics
}
func NewHTTPServer(ctx *Context, logging bool) http.Handler {
h := &httpServer{
ctx: ctx,
GetMetrics: timer_metrics.NewTimerMetrics(1500, "/get"),
MgetMetrics: timer_metrics.NewTimerMetrics(1500, "/mget"),
}
if logging {
return LoggingHandler(os.Stdout, h)
}
return h
}
func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/ping":
s.pingHandler(w, req)
case "/get":
s.getHandler(w, req)
case "/mget":
s.mgetHandler(w, req)
// case "/fwmatch":
// s.fwmatchHandler(w, req)
case "/stats":
s.statsHandler(w, req)
case "/reload":
s.reloadHandler(w, req)
// case "/exit":
// s.exitHandler(w, req)
case "/debug/pprof":
httpprof.Index(w, req)
case "/debug/pprof/cmdline":
httpprof.Cmdline(w, req)
case "/debug/pprof/symbol":
httpprof.Symbol(w, req)
case "/debug/pprof/heap":
httpprof.Handler("heap").ServeHTTP(w, req)
case "/debug/pprof/goroutine":
httpprof.Handler("goroutine").ServeHTTP(w, req)
case "/debug/pprof/profile":
httpprof.Profile(w, req)
case "/debug/pprof/block":
httpprof.Handler("block").ServeHTTP(w, req)
case "/debug/pprof/threadcreate":
httpprof.Handler("threadcreate").ServeHTTP(w, req)
default:
log.Printf("ERROR: 404 %s", req.URL.Path)
http.NotFound(w, req)
}
}
func (s *httpServer) pingHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Length", "2")
io.WriteString(w, "OK")
}
func (s *httpServer) getHandler(w http.ResponseWriter, req *http.Request) {
key := req.FormValue("key")
if key == "" {
http.Error(w, "MISSING_ARG_KEY", 400)
return
}
startTime := time.Now()
atomic.AddUint64(&s.Requests, 1)
atomic.AddUint64(&s.GetRequests, 1)
needle := append([]byte(key), s.ctx.db.RecordSeparator)
line := s.ctx.db.Search(needle)
if len(line) == 0 {
atomic.AddUint64(&s.GetMisses, 1)
http.Error(w, "NOT_FOUND", 404)
} else {
// we only output the 'value'
line = line[len(needle):]
atomic.AddUint64(&s.GetHits, 1)
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Length", strconv.Itoa(len(line)+1))
w.Write(line)
w.Write([]byte{s.ctx.db.LineEnding})
}
s.GetMetrics.Status(startTime)
}
func (s *httpServer) mgetHandler(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
if len(req.Form["key"]) == 0 {
http.Error(w, "MISSING_ARG_KEY", 400)
return
}
startTime := time.Now()
atomic.AddUint64(&s.Requests, 1)
atomic.AddUint64(&s.MgetRequests, 1)
w.Header().Set("Content-Type", "text/plain")
var numFound int
for _, key := range req.Form["key"] {
needle := append([]byte(key), s.ctx.db.RecordSeparator)
line := s.ctx.db.Search(needle)
if len(line) != 0 {
numFound += 1
w.Write(line)
w.Write([]byte{s.ctx.db.LineEnding})
}
}
if numFound == 0 {
atomic.AddUint64(&s.MgetMisses, 1)
w.WriteHeader(200)
} else {
atomic.AddUint64(&s.MgetHits, 1)
}
s.MgetMetrics.Status(startTime)
}
func (s *httpServer) reloadHandler(w http.ResponseWriter, req *http.Request) {
s.ctx.reloadChan <- 1
w.Header().Set("Content-Length", "2")
io.WriteString(w, "OK")
}
type statsResponse struct {
Requests uint64 `json:"total_requests"`
SeekCount uint64 `json:"total_seeks"`
GetRequests uint64 `json:"get_requests"`
GetHits uint64 `json:"get_hits"`
GetMisses uint64 `json:"get_misses"`
GetAvg time.Duration `json:"get_average_request"` // Microsecond
Get95 time.Duration `json:"get_95"` // Microsecond
Get99 time.Duration `json:"get_99"` // Microsecond
MgetRequests uint64 `json:"mget_requests"`
MgetHits uint64 `json:"mget_hits"`
MgetMisses uint64 `json:"mget_misses"`
MgetAvg time.Duration `json:"mget_average_request"` // Microsecond
Mget95 time.Duration `json:"mget_95"` // Microsecond
Mget99 time.Duration `json:"mget_99"` // Microsecond
DBSize int64 `json:"db_size"`
DBMtime int64 `json:"db_mtime"`
}
func (s *httpServer) statsHandler(w http.ResponseWriter, req *http.Request) {
getStats := s.GetMetrics.Stats()
mgetStats := s.MgetMetrics.Stats()
size, mtime := s.ctx.db.Info()
stats := statsResponse{
Requests: atomic.LoadUint64(&s.Requests),
SeekCount: s.ctx.db.SeekCount(),
GetRequests: atomic.LoadUint64(&s.GetRequests),
GetHits: atomic.LoadUint64(&s.GetHits),
GetMisses: atomic.LoadUint64(&s.GetMisses),
GetAvg: getStats.Avg / time.Microsecond,
Get95: getStats.P95 / time.Microsecond,
Get99: getStats.P99 / time.Microsecond,
MgetRequests: atomic.LoadUint64(&s.MgetRequests),
MgetHits: atomic.LoadUint64(&s.MgetHits),
MgetMisses: atomic.LoadUint64(&s.MgetMisses),
MgetAvg: mgetStats.Avg / time.Microsecond,
Mget95: mgetStats.P95 / time.Microsecond,
Mget99: mgetStats.P99 / time.Microsecond,
DBSize: int64(size),
DBMtime: mtime.Unix(),
}
// evbuffer_add_printf(evb, "\"total_seeks\": %"PRIu64",", total_seeks);
response, err := json.Marshal(stats)
if err != nil {
log.Printf("%s", err)
http.Error(w, "INTERNAL_ERROR", 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.WriteHeader(200)
w.Write(response)
}