-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
322 lines (277 loc) · 7.5 KB
/
server.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 promtable
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"go.uber.org/zap"
"cloud.google.com/go/bigtable"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/uschen/promtable/prompb"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
// Server -
type Server struct {
httpServer *http.Server
cfg *Config
store *Store
mux *http.ServeMux
l net.Listener
// prometheus metric
psrv *http.Server
plistener net.Listener
Logger *zap.Logger
}
// NewServerWithConfig -
func NewServerWithConfig(cfg *Config) (*Server, error) {
s := &Server{
httpServer: &http.Server{},
cfg: cfg,
mux: http.NewServeMux(),
}
s.httpServer.Handler = s.mux
logger, err := zap.NewProduction()
if err != nil {
return nil, err
}
s.Logger = logger
getBTOpts := func(btcfg BT) ([]option.ClientOption, *bigtable.Client, error) {
// bigtable client
btcops := []option.ClientOption{}
if btcfg.Instance == "" {
return nil, nil, errors.New("bigtable.instance is required")
}
if btcfg.KeyPath != "" {
btcops = append(btcops, option.WithServiceAccountFile(btcfg.KeyPath))
}
btc, err := bigtable.NewClient(
context.Background(),
btcfg.ProjectID,
btcfg.Instance,
btcops...)
if err != nil {
return nil, nil, err
}
return btcops, btc, nil
}
// normal bt
btcops, btc, err := getBTOpts(cfg.Bigtable)
if err != nil {
return nil, err
}
var storeOps = []StoreOptionFunc{
StoreWithBigtableClient(btc),
StoreWithTableNamePrefix(cfg.Bigtable.TablePrefix),
StoreWithLogger(s.Logger),
StoreWithHashLabels(cfg.HashMetricName),
StoreWithEnableMetrics(cfg.Metric.Enable),
}
if cfg.EnsureTables {
btac, err := bigtable.NewAdminClient(
context.Background(),
cfg.Bigtable.ProjectID,
cfg.Bigtable.Instance,
btcops...)
if err != nil {
return nil, err
}
storeOps = append(storeOps, StoreWithBigtableAdminClient(btac))
}
if cfg.EnableLongtermStorage {
lbtcops, lbtc, err := getBTOpts(cfg.LongtermBigtable)
if err != nil {
return nil, err
}
storeOps = append(storeOps,
StoreWithEnableLongtermStorage(cfg.EnableLongtermStorage),
StoreWithLongermBigtableClient(lbtc),
StoreWithLongtermTableNamePrefix(cfg.LongtermBigtable.TablePrefix),
// StoreWithLong
)
if cfg.EnsureTables {
lbtac, err := bigtable.NewAdminClient(
context.Background(),
cfg.LongtermBigtable.ProjectID,
cfg.LongtermBigtable.Instance,
lbtcops...)
if err != nil {
return nil, err
}
storeOps = append(storeOps, StoreWithLongtermBigtableAdminClient(lbtac))
}
}
store, err := NewStore(storeOps...)
if err != nil {
return nil, err
}
s.store = store
if cfg.EnsureTables {
if err := s.store.EnsureTables(context.Background()); err != nil {
return nil, err
}
}
s.mux.Handle("/write", http.HandlerFunc(s.HandleWrite))
s.mux.Handle("/read", http.HandlerFunc(s.HandleRead))
lis, err := net.Listen("tcp", cfg.Web.Listen)
if err != nil {
return nil, err
}
s.l = lis
if cfg.Metric.Enable {
prometheusListenStr := cfg.Metric.Listen
if prometheusListenStr == "" {
prometheusListenStr = "127.0.0.1:9100"
}
pLis, err := net.Listen("tcp", prometheusListenStr)
if err != nil {
return nil, err
}
s.plistener = pLis
s.psrv = &http.Server{
Handler: promhttp.Handler(),
}
}
return s, nil
}
// HandleWrite -
func (s *Server) HandleWrite(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
s.Logger.Error("Read error", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
s.Logger.Error("Decode error", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req = new(prompb.WriteRequest)
if err := req.Unmarshal(reqBuf); err != nil {
s.Logger.Error("Unmarshal error", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := s.store.Put(r.Context(), req); err != nil {
s.Logger.Error("Put error", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
// HandleRead -
func (s *Server) HandleRead(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
s.Logger.Error("Read error", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
s.Logger.Error("Decode error", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req = new(prompb.ReadRequest)
if err := req.Unmarshal(reqBuf); err != nil {
s.Logger.Error("Unmarshal error", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
s.Logger.Info("Read", zap.String("req.string", req.String()))
res, err := s.store.Read(r.Context(), req)
if err != nil {
s.Logger.Error("Read error", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := res.Marshal()
if err != nil {
s.Logger.Error("Marshal error", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Content-Encoding", "snappy")
compressed = snappy.Encode(nil, data)
if _, err := w.Write(compressed); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Run -
func (s *Server) Run() error {
s.Logger.Info("Prepare Server.Run()")
var wg sync.WaitGroup
wgCtx, wgCancel := context.WithCancel(context.Background())
defer wgCancel()
// main server, in case we have other servers in the future, like metrics...
wg.Add(1)
go func() {
defer wg.Done()
defer wgCancel()
if err := s.httpServer.Serve(s.l); err != nil {
s.Logger.Error("http server exited with error", zap.Error(err))
}
}()
s.Logger.Info("HTTP Server Start", zap.String("Listen", s.cfg.Web.Listen))
// http prometheus metric server
if s.psrv != nil {
wg.Add(1)
go func() {
defer wg.Done()
defer wgCancel() // signal other servers to close too.
if err := s.psrv.Serve(s.plistener); err != nil {
s.Logger.Error("http prometheus metric server exited with error", zap.Error(err))
}
}()
s.Logger.Info("prometheus metric http server started", zap.String("Listen", s.cfg.Metric.Listen))
}
// wait for signal
stop := make(chan os.Signal)
defer close(stop)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-wgCtx.Done():
s.Logger.Info("some server returned, graceful shutdown all\n")
case sig := <-stop:
s.Logger.Warn("system signal received, will graceful shutdown", zap.String("signal", sig.String()))
}
if err := s.Stop(context.Background()); err != nil {
s.Logger.Warn("close http server error", zap.Error(err))
}
// wait for them to stop
wg.Wait()
s.Logger.Info("Exit")
return nil
}
// Stop -
func (s *Server) Stop(ctx context.Context) error {
var errs []error
if err := s.httpServer.Shutdown(ctx); err != nil && grpc.Code(err) != codes.Canceled {
errs = append(errs, err)
}
if s.psrv != nil {
if err := s.psrv.Shutdown(ctx); err != nil {
s.Logger.Warn("close http prometheus metric server error", zap.Error(err))
}
}
if err := s.store.Close(); err != nil {
errs = append(errs, err)
}
if len(errs) != 0 {
return fmt.Errorf("HTTPServer.Close with errors: %v", errs)
}
return nil
}