forked from cockroachdb/cockroach
/
testserver.go
408 lines (355 loc) · 12 KB
/
testserver.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// 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.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package server
import (
"fmt"
"net"
"path/filepath"
"time"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/gossip"
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/server/testingshim"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/ts"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/hlc"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/cockroach/util/metric"
"github.com/cockroachdb/cockroach/util/retry"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/pkg/errors"
)
const (
// TestUser is a fixed user used in unittests.
// It has valid embedded client certs.
TestUser = "testuser"
// initialSplitsTimeout is the amount of time to wait for initial splits to
// occur on a freshly started server.
// Note: this needs to be fairly high or tests become flaky.
initialSplitsTimeout = 10 * time.Second
)
// StartTestServerWithContext starts an in-memory test server.
// ctx can be nil, in which case a default context will be created.
func StartTestServerWithContext(t util.Tester, ctx *Context) TestServer {
s := TestServer{Ctx: ctx}
if err := s.Start(); err != nil {
if t != nil {
t.Fatalf("Could not start server: %v", err)
} else {
log.Fatalf("Could not start server: %v", err)
}
}
return s
}
// StartTestServer starts an in-memory test server.
func StartTestServer(t util.Tester) TestServer {
return StartTestServerWithContext(t, nil)
}
// StartTestServerJoining starts an in-memory test server that attempts to join `other`.
func StartTestServerJoining(t util.Tester, other TestServer) TestServer {
ctx := MakeTestContext()
ctx.JoinUsing = other.ServingAddr()
s := TestServer{Ctx: &ctx}
if err := s.Start(); err != nil {
if t != nil {
t.Fatalf("Could not start server: %v", err)
} else {
log.Fatalf("Could not start server: %v", err)
}
}
log.Infof("Node ID: %d", s.Gossip().GetNodeID())
return s
}
// StartInsecureTestServer starts an insecure in-memory test server.
func StartInsecureTestServer(t util.Tester) TestServer {
ctx := MakeTestContext()
ctx.Insecure = true
s := TestServer{Ctx: &ctx}
if err := s.Start(); err != nil {
if t != nil {
t.Fatalf("Could not start server: %v", err)
} else {
log.Fatalf("Could not start server: %v", err)
}
}
return s
}
// MakeTestContext returns a context for testing. It overrides the
// Certs with the test certs directory.
// We need to override the certs loader.
func MakeTestContext() Context {
ctx := MakeContext()
// MaxOffset is the maximum offset for clocks in the cluster.
// This is mostly irrelevant except when testing reads within
// uncertainty intervals.
ctx.MaxOffset = 50 * time.Millisecond
// Test servers start in secure mode by default.
ctx.Insecure = false
// Load test certs. In addition, the tests requiring certs
// need to call security.SetReadFileFn(securitytest.Asset)
// in their init to mock out the file system calls for calls to AssetFS,
// which has the test certs compiled in. Typically this is done
// once per package, in main_test.go.
ctx.SSLCA = filepath.Join(security.EmbeddedCertsDir, security.EmbeddedCACert)
ctx.SSLCert = filepath.Join(security.EmbeddedCertsDir, security.EmbeddedNodeCert)
ctx.SSLCertKey = filepath.Join(security.EmbeddedCertsDir, security.EmbeddedNodeKey)
// Addr defaults to localhost with port set at time of call to
// Start() to an available port.
// Call TestServer.ServingAddr() for the full address (including bound port).
ctx.Addr = "127.0.0.1:0"
ctx.HTTPAddr = "127.0.0.1:0"
// Set standard user for intra-cluster traffic.
ctx.User = security.NodeUser
return ctx
}
// A TestServer encapsulates an in-memory instantiation of a cockroach
// node with a single store. Example usage of a TestServer follows:
//
// s := server.StartTestServer(t)
// defer s.Stop()
//
type TestServer struct {
// Ctx is the context used by this server.
Ctx *Context
// server is the embedded Cockroach server struct.
*Server
StoresPerNode int
}
// Stopper returns the embedded server's Stopper.
func (ts *TestServer) Stopper() *stop.Stopper {
return ts.stopper
}
// Gossip returns the gossip instance used by the TestServer.
func (ts *TestServer) Gossip() *gossip.Gossip {
if ts != nil {
return ts.gossip
}
return nil
}
// Clock returns the clock used by the TestServer.
func (ts *TestServer) Clock() *hlc.Clock {
if ts != nil {
return ts.clock
}
return nil
}
// RPCContext returns the rpc context used by the TestServer.
func (ts *TestServer) RPCContext() *rpc.Context {
if ts != nil {
return ts.rpcContext
}
return nil
}
// TsDB returns the ts.DB instance used by the TestServer.
func (ts *TestServer) TsDB() *ts.DB {
if ts != nil {
return ts.tsDB
}
return nil
}
// DB returns the client.DB instance used by the TestServer.
func (ts *TestServer) DB() *client.DB {
if ts != nil {
return ts.db
}
return nil
}
// Start starts the TestServer by bootstrapping an in-memory store
// (defaults to maximum of 100M). The server is started, launching the
// node RPC server and all HTTP endpoints. Use the value of
// TestServer.ServingAddr() after Start() for client connections. Use Stop()
// to shutdown the server after the test completes.
func (ts *TestServer) Start() error {
return ts.StartWithStopper(nil)
}
// StartWithStopper is the same as Start, but allows passing a stopper
// explicitly.
func (ts *TestServer) StartWithStopper(stopper *stop.Stopper) error {
if ts.Ctx == nil {
ctx := MakeTestContext()
ts.Ctx = &ctx
}
if stopper == nil {
stopper = stop.NewStopper()
}
// Change the replication requirements so we don't get log spam about ranges
// not being replicated enough.
cfg := config.DefaultZoneConfig()
cfg.ReplicaAttrs = []roachpb.Attributes{{}}
fn := config.TestingSetDefaultZoneConfig(cfg)
stopper.AddCloser(stop.CloserFn(fn))
// Needs to be called before NewServer to ensure resolvers are initialized.
if err := ts.Ctx.InitNode(); err != nil {
return err
}
// Ensure we have the correct number of engines. Add in-memory ones where
// needed. There must be at least one store/engine.
if ts.StoresPerNode < 1 {
ts.StoresPerNode = 1
}
for i := len(ts.Ctx.Engines); i < ts.StoresPerNode; i++ {
ts.Ctx.Engines = append(ts.Ctx.Engines, engine.NewInMem(roachpb.Attributes{}, 100<<20, stopper))
}
var err error
ts.Server, err = NewServer(*ts.Ctx, stopper)
if err != nil {
return err
}
// Our context must be shared with our server.
ts.Ctx = &ts.Server.ctx
if err := ts.Server.Start(); err != nil {
return err
}
// If enabled, wait for initial splits to complete before returning control.
// If initial splits do not complete, the server is stopped before
// returning.
if config.TestingTableSplitsDisabled() {
return nil
}
if err := ts.WaitForInitialSplits(); err != nil {
ts.Stop()
return err
}
return nil
}
// ExpectedInitialRangeCount returns the expected number of ranges that should
// be on the server after initial (asynchronous) splits have been completed,
// assuming no additional information is added outside of the normal bootstrap
// process.
func ExpectedInitialRangeCount() int {
return GetBootstrapSchema().DescriptorCount() - sqlbase.NumSystemDescriptors + 1
}
// WaitForInitialSplits waits for the server to complete its expected initial
// splits at startup. If the expected range count is not reached within a
// configured timeout, an error is returned.
func (ts *TestServer) WaitForInitialSplits() error {
return WaitForInitialSplits(ts.DB())
}
// WaitForInitialSplits waits for the expected number of initial ranges to be
// populated in the meta2 table. If the expected range count is not reached
// within a configured timeout, an error is returned.
func WaitForInitialSplits(db *client.DB) error {
expectedRanges := ExpectedInitialRangeCount()
return util.RetryForDuration(initialSplitsTimeout, func() error {
// Scan all keys in the Meta2Prefix; we only need a count.
rows, err := db.Scan(keys.Meta2Prefix, keys.MetaMax, 0)
if err != nil {
return err
}
if a, e := len(rows), expectedRanges; a != e {
return errors.Errorf("had %d ranges at startup, expected %d", a, e)
}
return nil
})
}
// Stores returns the collection of stores from this TestServer's node.
func (ts *TestServer) Stores() *storage.Stores {
return ts.node.stores
}
// ServingAddr returns the server's address. Should be used by clients.
func (ts *TestServer) ServingAddr() string {
return ts.ctx.Addr
}
// ServingHost returns the host portion of the rpc server's address.
func (ts *TestServer) ServingHost() (string, error) {
h, _, err := net.SplitHostPort(ts.ServingAddr())
return h, err
}
// ServingPort returns the port portion of the rpc server's address.
func (ts *TestServer) ServingPort() (string, error) {
_, p, err := net.SplitHostPort(ts.ServingAddr())
return p, err
}
// Stop stops the TestServer.
func (ts *TestServer) Stop() {
if r := recover(); r != nil {
panic(r)
}
ts.Server.Stop()
}
// SetRangeRetryOptions sets the retry options for stores in TestServer.
func (ts *TestServer) SetRangeRetryOptions(ro retry.Options) {
if err := ts.node.stores.VisitStores(func(s *storage.Store) error {
s.SetRangeRetryOptions(ro)
return nil
}); err != nil {
panic(err)
}
}
// WriteSummaries records summaries of time-series data, which is required for any tests
// that query server stats.
func (ts *TestServer) WriteSummaries() error {
return ts.node.writeSummaries()
}
// MustGetSQLCounter returns the value of a counter metric from the server's SQL
// Executor. Runs in O(# of metrics) time, which is fine for test code.
func (ts *TestServer) MustGetSQLCounter(name string) int64 {
var c int64
var found bool
ts.sqlExecutor.Registry().Each(func(n string, v interface{}) {
if name == n {
c = v.(*metric.Counter).Count()
found = true
}
})
if !found {
panic(fmt.Sprintf("couldn't find metric %s", name))
}
return c
}
// MustGetSQLNetworkCounter returns the value of a counter metric from the
// server's SQL server. Runs in O(# of metrics) time, which is fine for test
// code.
func (ts *TestServer) MustGetSQLNetworkCounter(name string) int64 {
var c int64
var found bool
ts.pgServer.Registry().Each(func(n string, v interface{}) {
if name == n {
c = v.(*metric.Counter).Count()
found = true
}
})
if !found {
panic(fmt.Sprintf("couldn't find metric %s", name))
}
return c
}
var _ testingshim.TestServerInterface = &TestServer{}
// KVClient is part of TestServerInterface.
func (ts *TestServer) KVClient() interface{} { return ts.db }
// KVDB is part of TestServerInterface.
func (ts *TestServer) KVDB() interface{} { return ts.kvDB }
// LeaseManager is part of TestServerInterface.
func (ts *TestServer) LeaseManager() interface{} {
return ts.leaseMgr
}
type testServerFactoryImpl struct{}
// TestServerFactory can be passed to testingshim.InitTestServerFactory
var TestServerFactory testingshim.TestServerFactory = testServerFactoryImpl{}
// New is part of TestServerInterface.
func (testServerFactoryImpl) New(params testingshim.TestServerParams) testingshim.TestServerInterface {
ctx := MakeTestContext()
ctx.TestingKnobs = params.Knobs
ctx.JoinUsing = params.JoinAddr
return &TestServer{Ctx: &ctx}
}