forked from cockroachdb/cockroach
/
server.go
290 lines (253 loc) · 8.18 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
// 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"
"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/kv"
"github.com/cockroachdb/cockroach/multiraft"
"github.com/cockroachdb/cockroach/resource"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/server/status"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/structured"
"github.com/cockroachdb/cockroach/ts"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/hlc"
"github.com/cockroachdb/cockroach/util/log"
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
kv *client.KV
kvDB *kv.DBServer
kvREST *kv.RESTServer
node *Node
admin *adminServer
status *statusServer
structuredDB structured.DB
structuredREST *structured.RESTServer
tsDB *ts.DB
tsServer *ts.Server
raftTransport multiraft.Transport
stopper *util.Stopper
}
// NewServer creates a Server from a server.Context.
func NewServer(ctx *Context, stopper *util.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.")
}
tlsConfig, err := ctx.GetServerTLSConfig()
if 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(s.clock, tlsConfig, stopper)
go rpcContext.RemoteClocks.MonitorRemoteOffsets()
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)
ds := kv.NewDistSender(&kv.DistSenderContext{Clock: s.clock}, s.gossip)
sender := kv.NewTxnCoordSender(ds, s.clock, ctx.Linearizable, s.stopper)
s.kv = client.NewKV(nil, sender)
s.kv.User = storage.UserRoot
s.raftTransport, err = newRPCTransport(s.gossip, s.rpc, rpcContext)
if err != nil {
return nil, err
}
s.stopper.AddCloser(s.raftTransport)
s.kvDB = kv.NewDBServer(sender)
if s.ctx.ExperimentalRPCServer {
if err = s.kvDB.RegisterRPC(s.rpc); err != nil {
return nil, err
}
}
s.kvREST = kv.NewRESTServer(s.kv)
// TODO(bdarnell): make StoreConfig configurable.
nCtx := storage.StoreContext{
Clock: s.clock,
DB: s.kv,
Gossip: s.gossip,
Transport: s.raftTransport,
ScanInterval: s.ctx.ScanInterval,
ScanMaxIdleTime: s.ctx.ScanMaxIdleTime,
EventFeed: &util.Feed{},
}
s.node = NewNode(nCtx)
s.admin = newAdminServer(s.kv, s.stopper)
s.status = newStatusServer(s.kv, s.gossip)
s.structuredDB = structured.NewDB(s.kv)
s.structuredREST = structured.NewRESTServer(s.structuredDB)
s.tsDB = ts.NewDB(s.kv)
s.tsServer = ts.NewServer(s.tsDB)
s.stopper.AddCloser(nCtx.EventFeed)
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 time series data collected by the status monitor.
recorder := status.NewNodeStatusRecorder(s.node.status, s.clock)
s.tsDB.PollSource(recorder, s.ctx.MetricsFrequency, ts.Resolution10s, s.stopper)
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
}
func (s *Server) initHTTP() {
s.mux.Handle("/", http.FileServer(
&assetfs.AssetFS{Asset: resource.Asset, AssetDir: resource.AssetDir, Prefix: "./ui/"}))
// Admin handlers.
s.admin.registerHandlers(s.mux)
// Status endpoints:
s.mux.Handle(statusKeyPrefix, s.status)
s.mux.Handle(kv.RESTPrefix, s.kvREST)
s.mux.Handle(kv.DBPrefix, s.kvDB)
s.mux.Handle(structured.StructuredKeyPrefix, s.structuredREST)
s.mux.Handle(ts.URLPrefix, s.tsServer)
}
// 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.StartTask() {
http.Error(w, "service is draining", http.StatusServiceUnavailable)
return
}
defer s.stopper.FinishTask()
// Disable caching of responses.
w.Header().Set("Cache-control", "no-cache")
ae := r.Header.Get("Accept-Encoding")
switch {
case strings.Contains(ae, "snappy"):
w.Header().Set("Content-Encoding", "snappy")
s := newSnappyResponseWriter(w)
defer s.Close()
w = s
case strings.Contains(ae, "gzip"):
w.Header().Set("Content-Encoding", "gzip")
gzw := newGzipResponseWriter(w)
defer gzw.Close()
w = gzw
}
s.mux.ServeHTTP(w, r)
}
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
}
}