forked from cockroachdb/cockroach
/
server.go
360 lines (317 loc) · 10.3 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2014 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Andrew Bonventre (andybons@gmail.com)
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package server
import (
"compress/gzip"
"io"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/cockroachdb/c-snappy"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/gossip"
"github.com/cockroachdb/cockroach/gossip/resolver"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/kv"
"github.com/cockroachdb/cockroach/multiraft"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/server/status"
"github.com/cockroachdb/cockroach/sql"
"github.com/cockroachdb/cockroach/sql/driver"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/ts"
"github.com/cockroachdb/cockroach/ui"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/hlc"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/cockroachdb/cockroach/util/tracer"
assetfs "github.com/elazarl/go-bindata-assetfs"
)
var (
// Allocation pool for gzip writers.
gzipWriterPool sync.Pool
// Allocation pool for snappy writers.
snappyWriterPool sync.Pool
)
// Server is the cockroach server node.
type Server struct {
ctx *Context
mux *http.ServeMux
clock *hlc.Clock
rpc *rpc.Server
gossip *gossip.Gossip
db *client.DB
kvDB *kv.DBServer
sqlServer *sql.Server
node *Node
recorder *status.NodeStatusRecorder
admin *adminServer
status *statusServer
tsDB *ts.DB
tsServer *ts.Server
raftTransport multiraft.Transport
stopper *stop.Stopper
}
// NewServer creates a Server from a server.Context.
func NewServer(ctx *Context, stopper *stop.Stopper) (*Server, error) {
if ctx == nil {
return nil, util.Error("ctx must not be null")
}
addr := ctx.Addr
_, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, util.Errorf("unable to resolve RPC address %q: %v", addr, err)
}
if ctx.Insecure {
log.Warning("running in insecure mode, this is strongly discouraged. See --insecure and --certs.")
}
// Try loading the TLS configs before anything else.
if _, err := ctx.GetServerTLSConfig(); err != nil {
return nil, err
}
if _, err := ctx.GetClientTLSConfig(); err != nil {
return nil, err
}
s := &Server{
ctx: ctx,
mux: http.NewServeMux(),
clock: hlc.NewClock(hlc.UnixNano),
stopper: stopper,
}
s.clock.SetMaxOffset(ctx.MaxOffset)
rpcContext := rpc.NewContext(&ctx.Context, s.clock, stopper)
stopper.RunWorker(func() {
rpcContext.RemoteClocks.MonitorRemoteOffsets(stopper)
})
s.rpc = rpc.NewServer(util.MakeUnresolvedAddr("tcp", addr), rpcContext)
s.stopper.AddCloser(s.rpc)
s.gossip = gossip.New(rpcContext, s.ctx.GossipInterval, s.ctx.GossipBootstrapResolvers)
feed := util.NewFeed(stopper)
tracer := tracer.NewTracer(feed, addr)
ds := kv.NewDistSender(&kv.DistSenderContext{Clock: s.clock}, s.gossip)
sender := kv.NewTxnCoordSender(ds, s.clock, ctx.Linearizable, tracer, s.stopper)
if s.db, err = client.Open("//", client.SenderOpt(sender)); err != nil {
return nil, err
}
s.raftTransport, err = newRPCTransport(s.gossip, s.rpc, rpcContext)
if err != nil {
return nil, err
}
s.stopper.AddCloser(s.raftTransport)
s.kvDB = kv.NewDBServer(&s.ctx.Context, sender)
if s.ctx.ExperimentalRPCServer {
if err = s.kvDB.RegisterRPC(s.rpc); err != nil {
return nil, err
}
}
s.sqlServer = sql.NewServer(&s.ctx.Context, s.db)
// TODO(bdarnell): make StoreConfig configurable.
nCtx := storage.StoreContext{
Clock: s.clock,
DB: s.db,
Gossip: s.gossip,
Transport: s.raftTransport,
ScanInterval: s.ctx.ScanInterval,
ScanMaxIdleTime: s.ctx.ScanMaxIdleTime,
EventFeed: feed,
Tracer: tracer,
}
s.node = NewNode(nCtx)
s.admin = newAdminServer(s.db, s.stopper)
s.status = newStatusServer(s.db, s.gossip, ctx)
s.tsDB = ts.NewDB(s.db)
s.tsServer = ts.NewServer(s.tsDB)
return s, nil
}
// Start runs the RPC and HTTP servers, starts the gossip instance (if
// selfBootstrap is true, uses the rpc server's address as the gossip
// bootstrap), and starts the node using the supplied engines slice.
func (s *Server) Start(selfBootstrap bool) error {
if err := s.rpc.Listen(); err != nil {
return util.Errorf("could not listen on %s: %s", s.ctx.Addr, err)
}
// Handle self-bootstrapping case for a single node.
if selfBootstrap {
selfResolver, err := resolver.NewResolver(&s.ctx.Context, s.rpc.Addr().String())
if err != nil {
return err
}
s.gossip.SetResolvers([]resolver.Resolver{selfResolver})
}
s.gossip.Start(s.rpc, s.stopper)
if err := s.node.start(s.rpc, s.ctx.Engines, s.ctx.NodeAttributes, s.stopper); err != nil {
return err
}
// Begin recording runtime statistics.
runtime := status.NewRuntimeStatRecorder(s.node.Descriptor.NodeID, s.clock)
s.tsDB.PollSource(runtime, s.ctx.MetricsFrequency, ts.Resolution10s, s.stopper)
// Begin recording time series data collected by the status monitor.
s.recorder = status.NewNodeStatusRecorder(s.node.status, s.clock)
s.tsDB.PollSource(s.recorder, s.ctx.MetricsFrequency, ts.Resolution10s, s.stopper)
// Begin recording status summaries.
s.startWriteSummaries()
log.Infof("starting %s server at %s", s.ctx.RequestScheme(), s.rpc.Addr())
// TODO(spencer): go1.5 is supposed to allow shutdown of running http server.
s.initHTTP()
s.rpc.Serve(s)
return nil
}
// initHTTP registers http prefixes.
func (s *Server) initHTTP() {
s.mux.Handle("/", http.FileServer(
&assetfs.AssetFS{Asset: ui.Asset, AssetDir: ui.AssetDir}))
// The admin server handles both /debug/ and /_admin/
// TODO(marc): when cookie-based authentication exists,
// apply it for all web endpoints.
s.mux.Handle(adminEndpoint, s.admin)
s.mux.Handle(debugEndpoint, s.admin)
s.mux.Handle(statusPrefix, s.status)
s.mux.Handle(ts.URLPrefix, s.tsServer)
// KV handles its own authentication, verifying user certificates against
// the requested user.
s.mux.Handle(kv.DBPrefix, s.kvDB)
// The SQL endpoints handles its own authentication, verifying user
// credentials against the requested user.
s.mux.Handle(driver.Endpoint, s.sqlServer)
}
// startWriteSummaries begins periodically persisting status summaries for the
// node and its stores.
func (s *Server) startWriteSummaries() {
s.stopper.RunWorker(func() {
ticker := time.NewTicker(s.ctx.MetricsFrequency)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.stopper.RunTask(func() {
if err := s.writeSummaries(); err != nil {
log.Error(err)
}
})
case <-s.stopper.ShouldStop():
return
}
}
})
}
// writeSummaries retrieves status summaries from the supplied
// NodeStatusRecorder and persists them to the cockroach data store.
func (s *Server) writeSummaries() error {
nodeStatus, storeStatuses := s.recorder.GetStatusSummaries()
if nodeStatus != nil {
key := keys.NodeStatusKey(int32(nodeStatus.Desc.NodeID))
if err := s.db.Put(key, nodeStatus); err != nil {
return err
}
if log.V(1) {
log.Infof("recorded status for node %d", nodeStatus.Desc.NodeID)
}
}
for _, ss := range storeStatuses {
key := keys.StoreStatusKey(int32(ss.Desc.StoreID))
if err := s.db.Put(key, &ss); err != nil {
return err
}
}
if log.V(1) {
log.Infof("recorded status for %d stores", len(storeStatuses))
}
return nil
}
// Stop stops the server.
func (s *Server) Stop() {
s.stopper.Stop()
}
// ServeHTTP is necessary to implement the http.Handler interface. It
// will snappy a response if the appropriate request headers are set.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check if we're draining; if so return 503, service unavailable.
if !s.stopper.RunTask(func() {
// Disable caching of responses.
w.Header().Set("Cache-control", "no-cache")
ae := r.Header.Get(util.AcceptEncodingHeader)
switch {
case strings.Contains(ae, util.SnappyEncoding):
w.Header().Set(util.ContentEncodingHeader, util.SnappyEncoding)
s := newSnappyResponseWriter(w)
defer s.Close()
w = s
case strings.Contains(ae, util.GzipEncoding):
w.Header().Set(util.ContentEncodingHeader, util.GzipEncoding)
gzw := newGzipResponseWriter(w)
defer gzw.Close()
w = gzw
}
s.mux.ServeHTTP(w, r)
}) {
http.Error(w, "service is draining", http.StatusServiceUnavailable)
}
}
type gzipResponseWriter struct {
io.WriteCloser
http.ResponseWriter
}
func newGzipResponseWriter(w http.ResponseWriter) *gzipResponseWriter {
var gz *gzip.Writer
if gzI := gzipWriterPool.Get(); gzI == nil {
gz = gzip.NewWriter(w)
} else {
gz = gzI.(*gzip.Writer)
gz.Reset(w)
}
return &gzipResponseWriter{WriteCloser: gz, ResponseWriter: w}
}
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
return w.WriteCloser.Write(b)
}
func (w *gzipResponseWriter) Close() {
if w.WriteCloser != nil {
w.WriteCloser.Close()
gzipWriterPool.Put(w.WriteCloser)
w.WriteCloser = nil
}
}
type snappyResponseWriter struct {
io.Writer
http.ResponseWriter
}
func newSnappyResponseWriter(w http.ResponseWriter) *snappyResponseWriter {
var s *snappy.Writer
if sI := snappyWriterPool.Get(); sI == nil {
// TODO(pmattis): It would be better to use the C++ snappy code
// like rpc/codec is doing. Would have to copy the snappy.Writer
// implementation from snappy-go.
s = snappy.NewWriter(w)
} else {
s = sI.(*snappy.Writer)
s.Reset(w)
}
return &snappyResponseWriter{Writer: s, ResponseWriter: w}
}
func (w *snappyResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w *snappyResponseWriter) Close() {
if w.Writer != nil {
snappyWriterPool.Put(w.Writer)
w.Writer = nil
}
}