-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest_directory_svr.go
477 lines (434 loc) · 13.3 KB
/
test_directory_svr.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package tenantdirsvr
import (
"bufio"
"container/list"
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/tenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/logtags"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Make sure that TestDirectoryServer implements the DirectoryServer interface.
var _ tenant.DirectoryServer = (*TestDirectoryServer)(nil)
// Process stores information about a running tenant process.
type Process struct {
Stopper *stop.Stopper
Cmd *exec.Cmd
SQL net.Addr
FakeLoad float32
}
// NewSubStopper creates a new stopper that will be stopped when either the
// parent is stopped or its own Stop is called. The code is slightly more
// complicated that simply calling NewStopper followed by AddCloser since there
// is a possibility that between the two calls, the parent stopper completes a
// stop and then the leak detection may find a leaked stopper.
func NewSubStopper(parentStopper *stop.Stopper) *stop.Stopper {
var mu syncutil.Mutex
var subStopper *stop.Stopper
parentStopper.AddCloser(stop.CloserFn(func() {
mu.Lock()
defer mu.Unlock()
if subStopper == nil {
subStopper = stop.NewStopper(stop.WithTracer(parentStopper.Tracer()))
}
subStopper.Stop(context.Background())
}))
mu.Lock()
defer mu.Unlock()
if subStopper == nil {
subStopper = stop.NewStopper(stop.WithTracer(parentStopper.Tracer()))
}
return subStopper
}
// TestDirectoryServer is a directory server implementation that is used for
// testing.
type TestDirectoryServer struct {
args []string
stopper *stop.Stopper
grpcServer *grpc.Server
cockroachExecutable string
// TenantStarterFunc will be used to launch a new tenant process.
TenantStarterFunc func(ctx context.Context, tenantID uint64) (*Process, error)
// When both mutexes need to be held, the locking should always be proc
// first and listen second.
proc struct {
syncutil.RWMutex
processByAddrByTenantID map[uint64]map[net.Addr]*Process
}
listen struct {
syncutil.RWMutex
eventListeners *list.List
}
}
// New will create a new server.
func New(stopper *stop.Stopper, args ...string) (*TestDirectoryServer, error) {
// Determine the path to cockroach executable.
cockroachExecutable, err := os.Executable()
if err != nil {
return nil, err
}
dir := &TestDirectoryServer{
args: args,
grpcServer: grpc.NewServer(),
stopper: stopper,
cockroachExecutable: cockroachExecutable,
}
dir.TenantStarterFunc = dir.startTenantLocked
dir.proc.processByAddrByTenantID = map[uint64]map[net.Addr]*Process{}
dir.listen.eventListeners = list.New()
stopper.AddCloser(stop.CloserFn(dir.grpcServer.GracefulStop))
tenant.RegisterDirectoryServer(dir.grpcServer, dir)
return dir, nil
}
// Stopper returns the stopper that can be used to shut down the test directory
// server.
func (s *TestDirectoryServer) Stopper() *stop.Stopper {
return s.stopper
}
// Get a tenant's list of pods and the process information for each pod.
func (s *TestDirectoryServer) Get(id roachpb.TenantID) (result map[net.Addr]*Process) {
result = make(map[net.Addr]*Process)
s.proc.RLock()
defer s.proc.RUnlock()
processes, ok := s.proc.processByAddrByTenantID[id.ToUint64()]
if ok {
for k, v := range processes {
result[k] = v
}
}
return
}
// serverShutdownErr is returned when TestDirectoryServer is no longer accepting
// new requests.
var serverShutdownErr = status.Error(codes.Unavailable, "server shutting down")
// StartTenant will forcefully start a new tenant pod
// instance. This may be useful to test the behavior when more
// than one tenant is running.
func (s *TestDirectoryServer) StartTenant(ctx context.Context, id roachpb.TenantID) error {
select {
case <-s.stopper.ShouldQuiesce():
return context.Canceled
default:
}
ctx = logtags.AddTag(ctx, "tenant", id)
s.proc.Lock()
defer s.proc.Unlock()
process, err := s.TenantStarterFunc(ctx, id.ToUint64())
if err != nil {
return err
}
s.registerInstanceLocked(id.ToUint64(), process)
process.Stopper.AddCloser(stop.CloserFn(func() {
s.deregisterInstance(id.ToUint64(), process.SQL)
}))
return nil
}
// GetTenant returns tenant metadata for a given ID. Hard coded to return every
// tenant's cluster name as "tenant-cluster"
func (s *TestDirectoryServer) GetTenant(
_ context.Context, _ *tenant.GetTenantRequest,
) (*tenant.GetTenantResponse, error) {
return &tenant.GetTenantResponse{
ClusterName: "tenant-cluster",
}, nil
}
// ListPods returns a list of tenant process pods as well as status of
// the processes.
func (s *TestDirectoryServer) ListPods(
ctx context.Context, req *tenant.ListPodsRequest,
) (*tenant.ListPodsResponse, error) {
ctx = logtags.AddTag(ctx, "tenant", req.TenantID)
s.proc.RLock()
defer s.proc.RUnlock()
return s.listLocked(ctx, req)
}
// WatchPods returns a new stream, that can be used to monitor server
// activity.
func (s *TestDirectoryServer) WatchPods(
_ *tenant.WatchPodsRequest, server tenant.Directory_WatchPodsServer,
) error {
select {
case <-s.stopper.ShouldQuiesce():
return serverShutdownErr
default:
}
// Make the channel with a small buffer to allow for a burst of notifications
// and a slow receiver.
c := make(chan *tenant.WatchPodsResponse, 10)
s.listen.Lock()
elem := s.listen.eventListeners.PushBack(c)
s.listen.Unlock()
err := s.stopper.RunTask(context.Background(), "watch-pods-server",
func(ctx context.Context) {
out:
for {
select {
case e, ok := <-c:
if !ok {
break out
}
if err := server.Send(e); err != nil {
s.listen.Lock()
s.listen.eventListeners.Remove(elem)
close(c)
s.listen.Unlock()
break out
}
case <-s.stopper.ShouldQuiesce():
s.listen.Lock()
s.listen.eventListeners.Remove(elem)
close(c)
s.listen.Unlock()
break out
}
}
})
return err
}
func (s *TestDirectoryServer) WatchTenants(
_ *tenant.WatchTenantsRequest, server tenant.Directory_WatchTenantsServer,
) error {
return nil
}
// Drain sends out DRAINING pod notifications for each process managed by the
// test directory. This causes the proxy to start enforcing short idle
// connection timeouts in order to drain the connections to the pod.
func (s *TestDirectoryServer) Drain() {
s.proc.RLock()
defer s.proc.RUnlock()
for tenantID, processByAddr := range s.proc.processByAddrByTenantID {
for addr := range processByAddr {
s.listen.RLock()
defer s.listen.RUnlock()
s.notifyEventListenersLocked(&tenant.WatchPodsResponse{
Pod: &tenant.Pod{
TenantID: tenantID,
Addr: addr.String(),
State: tenant.DRAINING,
StateTimestamp: timeutil.Now(),
},
})
}
}
}
func (s *TestDirectoryServer) notifyEventListenersLocked(req *tenant.WatchPodsResponse) {
for e := s.listen.eventListeners.Front(); e != nil; {
select {
case e.Value.(chan *tenant.WatchPodsResponse) <- req:
e = e.Next()
default:
// The receiver is unable to consume fast enough. Close the channel and
// remove it from the list.
eToClose := e
e = e.Next()
close(eToClose.Value.(chan *tenant.WatchPodsResponse))
s.listen.eventListeners.Remove(eToClose)
}
}
}
// EnsurePod will ensure that there is either an already active tenant
// process or it will start a new one. It will return an error if starting a new
// tenant process is impossible.
func (s *TestDirectoryServer) EnsurePod(
ctx context.Context, req *tenant.EnsurePodRequest,
) (*tenant.EnsurePodResponse, error) {
select {
case <-s.stopper.ShouldQuiesce():
return nil, context.Canceled
default:
}
ctx = logtags.AddTag(ctx, "tenant", req.TenantID)
s.proc.Lock()
defer s.proc.Unlock()
lst, err := s.listLocked(ctx, &tenant.ListPodsRequest{TenantID: req.TenantID})
if err != nil {
return nil, err
}
if len(lst.Pods) == 0 {
process, err := s.TenantStarterFunc(ctx, req.TenantID)
if err != nil {
return nil, err
}
s.registerInstanceLocked(req.TenantID, process)
process.Stopper.AddCloser(stop.CloserFn(func() {
s.deregisterInstance(req.TenantID, process.SQL)
}))
}
return &tenant.EnsurePodResponse{}, nil
}
// Serve requests on the given listener.
func (s *TestDirectoryServer) Serve(listener net.Listener) error {
return s.grpcServer.Serve(listener)
}
func (s *TestDirectoryServer) listLocked(
_ context.Context, req *tenant.ListPodsRequest,
) (*tenant.ListPodsResponse, error) {
processByAddr, ok := s.proc.processByAddrByTenantID[req.TenantID]
if !ok {
return &tenant.ListPodsResponse{}, nil
}
resp := tenant.ListPodsResponse{}
for addr, proc := range processByAddr {
resp.Pods = append(resp.Pods, &tenant.Pod{
TenantID: req.TenantID,
Addr: addr.String(),
State: tenant.RUNNING,
Load: proc.FakeLoad,
StateTimestamp: timeutil.Now(),
})
}
return &resp, nil
}
func (s *TestDirectoryServer) registerInstanceLocked(tenantID uint64, process *Process) {
processByAddr, ok := s.proc.processByAddrByTenantID[tenantID]
if !ok {
processByAddr = map[net.Addr]*Process{}
s.proc.processByAddrByTenantID[tenantID] = processByAddr
}
processByAddr[process.SQL] = process
s.listen.RLock()
defer s.listen.RUnlock()
s.notifyEventListenersLocked(&tenant.WatchPodsResponse{
Pod: &tenant.Pod{
TenantID: tenantID,
Addr: process.SQL.String(),
State: tenant.RUNNING,
Load: process.FakeLoad,
StateTimestamp: timeutil.Now(),
},
})
}
func (s *TestDirectoryServer) deregisterInstance(tenantID uint64, sql net.Addr) {
s.proc.Lock()
defer s.proc.Unlock()
processByAddr, ok := s.proc.processByAddrByTenantID[tenantID]
if !ok {
return
}
if _, ok = processByAddr[sql]; ok {
delete(processByAddr, sql)
s.listen.RLock()
defer s.listen.RUnlock()
s.notifyEventListenersLocked(&tenant.WatchPodsResponse{
Pod: &tenant.Pod{
TenantID: tenantID,
Addr: sql.String(),
State: tenant.DELETING,
StateTimestamp: timeutil.Now(),
},
})
}
}
type writerFunc func(p []byte) (int, error)
func (wf writerFunc) Write(p []byte) (int, error) { return wf(p) }
// startTenantLocked is the default tenant process startup logic that runs the
// cockroach db executable out of process.
func (s *TestDirectoryServer) startTenantLocked(
ctx context.Context, tenantID uint64,
) (*Process, error) {
// A hackish way to have the sql tenant process listen on known ports.
sqlListener, err := net.Listen("tcp", "")
if err != nil {
return nil, err
}
httpListener, err := net.Listen("tcp", "")
if err != nil {
return nil, err
}
process := &Process{SQL: sqlListener.Addr(), FakeLoad: 0.01}
args := s.args
if len(args) == 0 {
args = append(args,
s.cockroachExecutable, "mt", "start-sql", "--kv-addrs=:26257", "--insecure",
)
}
args = append(args,
fmt.Sprintf("--sql-addr=%s", sqlListener.Addr().String()),
fmt.Sprintf("--http-addr=%s", httpListener.Addr().String()),
fmt.Sprintf("--tenant-id=%d", tenantID),
)
if err = sqlListener.Close(); err != nil {
return nil, err
}
if err = httpListener.Close(); err != nil {
return nil, err
}
c := exec.Command(args[0], args[1:]...)
process.Cmd = c
c.Env = append(os.Environ(), "COCKROACH_TRUST_CLIENT_PROVIDED_SQL_REMOTE_ADDR=true")
var f writerFunc = func(p []byte) (int, error) {
sc := bufio.NewScanner(strings.NewReader(string(p)))
for sc.Scan() {
log.Infof(ctx, "%s", sc.Text())
}
return len(p), nil
}
c.Stdout = f
c.Stderr = f
err = c.Start()
if err != nil {
return nil, err
}
process.Stopper = NewSubStopper(s.stopper)
process.Stopper.AddCloser(stop.CloserFn(func() {
_ = c.Process.Kill()
s.deregisterInstance(tenantID, process.SQL)
}))
err = s.stopper.RunAsyncTask(ctx, "cmd-wait", func(ctx context.Context) {
if err := c.Wait(); err != nil {
log.Infof(ctx, "finished %s with err %s", process.Cmd.Args, err)
return
}
log.Infof(ctx, "finished %s with success", process.Cmd.Args)
process.Stopper.Stop(ctx)
})
if err != nil {
return nil, err
}
// Wait for the tenant to show healthy
start := timeutil.Now()
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transport}
for {
time.Sleep(300 * time.Millisecond)
resp, err := client.Get(fmt.Sprintf("https://%s/health", httpListener.Addr().String()))
waitTime := timeutil.Since(start)
if err == nil {
resp.Body.Close()
log.Infof(ctx, "tenant is healthy")
break
}
if waitTime > 5*time.Second {
log.Infof(ctx, "waited more than 5 sec for the tenant to get healthy and it still isn't")
break
}
log.Infof(ctx, "waiting %s for healthy tenant: %s", waitTime, err)
}
// We currently set the password when we spawn the first tenant so wait
// a second to ensure the password is set.
time.Sleep(time.Second)
return process, nil
}