-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dockercluster.go
883 lines (798 loc) · 25.5 KB
/
dockercluster.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cluster
import (
"bytes"
"context"
gosql "database/sql"
"encoding/json"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/logflags"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/oserror"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
)
const (
defaultImage = "docker.io/library/ubuntu:xenial-20170214"
networkPrefix = "cockroachdb_acceptance"
)
// DefaultTCP is the default SQL/RPC port specification.
const DefaultTCP nat.Port = base.DefaultPort + "/tcp"
const defaultHTTP nat.Port = base.DefaultHTTPPort + "/tcp"
// CockroachBinaryInContainer is the container-side path to the CockroachDB
// binary.
const CockroachBinaryInContainer = "/cockroach/cockroach"
var cockroachImage = flag.String("i", defaultImage, "the docker image to run")
var cockroachEntry = flag.String("e", "", "the entry point for the image")
var waitOnStop = flag.Bool("w", false, "wait for the user to interrupt before tearing down the cluster")
var maxRangeBytes = *zonepb.DefaultZoneConfig().RangeMaxBytes
// CockroachBinary is the path to the host-side binary to use.
var CockroachBinary = flag.String("b", func() string {
rootPkg, err := build.Import("github.com/cockroachdb/cockroach", "", build.FindOnly)
if err != nil {
panic(err)
}
// NB: This is the binary produced by our linux-gnu build target. Changes
// to the Makefile must be reflected here.
return filepath.Join(rootPkg.Dir, "cockroach-linux-2.6.32-gnu-amd64")
}(), "the host-side binary to run")
func exists(path string) bool {
if _, err := os.Stat(path); oserror.IsNotExist(err) {
return false
}
return true
}
func nodeStr(l *DockerCluster, i int) string {
return fmt.Sprintf("roach-%s-%d", l.clusterID, i)
}
func storeStr(node, store int) string {
return fmt.Sprintf("data%d.%d", node, store)
}
// The various event types.
const (
eventDie = "die"
eventRestart = "restart"
)
// Event for a node containing a node index and the type of event.
type Event struct {
NodeIndex int
Status string
}
type testStore struct {
index int
dir string
config StoreConfig
}
type testNode struct {
*Container
index int
nodeStr string
config NodeConfig
stores []testStore
}
// DockerCluster manages a local cockroach cluster running on docker. The
// cluster is composed of a "volumes" container which manages the
// persistent volumes used for certs and node data and N cockroach nodes.
type DockerCluster struct {
client client.APIClient
mu syncutil.Mutex // Protects the fields below
vols *Container
config TestConfig
Nodes []*testNode
events chan Event
expectedEvents chan Event
oneshot *Container
stopper *stop.Stopper
monitorCtx context.Context
monitorCtxCancelFunc func()
clusterID string
networkID string
networkName string
// Careful! volumesDir will be emptied during cluster cleanup.
volumesDir string
}
// CreateDocker creates a Docker-based cockroach cluster. The stopper is used to
// gracefully shutdown the channel (e.g. when a signal arrives). The cluster
// must be started before being used and keeps container volumes in the
// specified volumesDir, including logs and cockroach stores. If volumesDir is
// empty, a temporary directory is created.
func CreateDocker(
ctx context.Context, cfg TestConfig, volumesDir string, stopper *stop.Stopper,
) *DockerCluster {
select {
case <-stopper.ShouldQuiesce():
// The stopper was already closed, exit early.
os.Exit(1)
default:
}
if *cockroachImage == defaultImage && !exists(*CockroachBinary) {
log.Fatalf(ctx, "\"%s\": does not exist", *CockroachBinary)
}
cli, err := client.NewClientWithOpts(client.FromEnv)
maybePanic(err)
cli.NegotiateAPIVersion(ctx)
clusterID := uuid.MakeV4()
clusterIDS := clusterID.Short()
if volumesDir == "" {
volumesDir, err = ioutil.TempDir("", fmt.Sprintf("cockroach-acceptance-%s", clusterIDS))
maybePanic(err)
} else {
volumesDir = filepath.Join(volumesDir, clusterIDS)
}
if !filepath.IsAbs(volumesDir) {
pwd, err := os.Getwd()
maybePanic(err)
volumesDir = filepath.Join(pwd, volumesDir)
}
maybePanic(os.MkdirAll(volumesDir, 0755))
log.Infof(ctx, "cluster volume directory: %s", volumesDir)
return &DockerCluster{
clusterID: clusterIDS,
client: resilientDockerClient{APIClient: cli},
config: cfg,
stopper: stopper,
// TODO(tschottdorf): deadlocks will occur if these channels fill up.
events: make(chan Event, 1000),
expectedEvents: make(chan Event, 1000),
volumesDir: volumesDir,
}
}
func (l *DockerCluster) expectEvent(c *Container, msgs ...string) {
for index, ctr := range l.Nodes {
if c.id != ctr.id {
continue
}
for _, status := range msgs {
select {
case l.expectedEvents <- Event{NodeIndex: index, Status: status}:
default:
panic("expectedEvents channel filled up")
}
}
break
}
}
// OneShot runs a container, expecting it to successfully run to completion
// and die, after which it is removed. Not goroutine safe: only one OneShot
// can be running at once.
// Adds the same binds as the cluster containers (certs, binary, etc).
func (l *DockerCluster) OneShot(
ctx context.Context,
ref string,
ipo types.ImagePullOptions,
containerConfig container.Config,
hostConfig container.HostConfig,
name string,
) error {
if err := pullImage(ctx, l, ref, ipo); err != nil {
return err
}
hostConfig.VolumesFrom = []string{l.vols.id}
c, err := createContainer(ctx, l, containerConfig, hostConfig, name)
if err != nil {
return err
}
l.oneshot = c
defer func() {
if err := l.oneshot.Remove(ctx); err != nil {
log.Errorf(ctx, "ContainerRemove: %s", err)
}
l.oneshot = nil
}()
if err := l.oneshot.Start(ctx); err != nil {
return err
}
return l.oneshot.Wait(ctx, container.WaitConditionNotRunning)
}
// stopOnPanic is invoked as a deferred function in Start in order to attempt
// to tear down the cluster if a panic occurs while starting it. If the panic
// was initiated by the stopper being closed (which panicOnStop notices) then
// the process is exited with a failure code.
func (l *DockerCluster) stopOnPanic(ctx context.Context) {
if r := recover(); r != nil {
l.stop(ctx)
if r != l {
panic(r)
}
os.Exit(1)
}
}
// panicOnStop tests whether the stopper has been closed and panics if
// it has. This allows polling for whether to stop and avoids nasty locking
// complications with trying to call Stop at arbitrary points such as in the
// middle of creating a container.
func (l *DockerCluster) panicOnStop() {
if l.stopper == nil {
panic(l)
}
select {
case <-l.stopper.IsStopped():
l.stopper = nil
panic(l)
default:
}
}
func (l *DockerCluster) createNetwork(ctx context.Context) {
l.panicOnStop()
l.networkName = fmt.Sprintf("%s-%s", networkPrefix, l.clusterID)
log.Infof(ctx, "creating docker network with name: %s", l.networkName)
net, err := l.client.NetworkInspect(ctx, l.networkName, types.NetworkInspectOptions{})
if err == nil {
// We need to destroy the network and any running containers inside of it.
for containerID := range net.Containers {
// This call could fail if the container terminated on its own after we call
// NetworkInspect, but the likelihood of this seems low. If this line creates
// a lot of panics we should do more careful error checking.
maybePanic(l.client.ContainerKill(ctx, containerID, "9"))
}
maybePanic(l.client.NetworkRemove(ctx, l.networkName))
} else if !client.IsErrNotFound(err) {
panic(err)
}
resp, err := l.client.NetworkCreate(ctx, l.networkName, types.NetworkCreate{
Driver: "bridge",
// Docker gets very confused if two networks have the same name.
CheckDuplicate: true,
})
maybePanic(err)
if resp.Warning != "" {
log.Warningf(ctx, "creating network: %s", resp.Warning)
}
l.networkID = resp.ID
}
// create the volumes container that keeps all of the volumes used by
// the cluster.
func (l *DockerCluster) initCluster(ctx context.Context) {
configJSON, err := json.Marshal(l.config)
maybePanic(err)
log.Infof(ctx, "Initializing Cluster %s:\n%s", l.config.Name, configJSON)
l.panicOnStop()
pwd, err := os.Getwd()
maybePanic(err)
// Boot2docker's handling of binding local directories into the container is
// very confusing. If the directory being bound has a parent directory that
// exists in the boot2docker VM then that directory is bound into the
// container. In particular, that means that binds of /tmp and /var will be
// problematic.
binds := []string{
filepath.Join(pwd, certsDir) + ":/certs",
filepath.Join(pwd, "..") + ":/go/src/github.com/cockroachdb/cockroach",
filepath.Join(l.volumesDir, "logs") + ":/logs",
}
if *cockroachImage == defaultImage {
path, err := filepath.Abs(*CockroachBinary)
maybePanic(err)
binds = append(binds, path+":"+CockroachBinaryInContainer)
}
l.Nodes = []*testNode{}
// Expand the cluster configuration into nodes and stores per node.
for i, nc := range l.config.Nodes {
newTestNode := &testNode{
config: nc,
index: i,
nodeStr: nodeStr(l, i),
}
for j, sc := range nc.Stores {
hostDir := filepath.Join(l.volumesDir, storeStr(i, j))
containerDir := "/" + storeStr(i, j)
binds = append(binds, hostDir+":"+containerDir)
newTestNode.stores = append(newTestNode.stores,
testStore{
config: sc,
index: j,
dir: containerDir,
})
}
l.Nodes = append(l.Nodes, newTestNode)
}
if *cockroachImage == defaultImage {
maybePanic(pullImage(ctx, l, defaultImage, types.ImagePullOptions{}))
}
c, err := createContainer(
ctx,
l,
container.Config{
Image: *cockroachImage,
Entrypoint: []string{"/bin/true"},
}, container.HostConfig{
Binds: binds,
PublishAllPorts: true,
},
fmt.Sprintf("volumes-%s", l.clusterID),
)
maybePanic(err)
// Make sure this assignment to l.vols is before the calls to Start and Wait.
// Otherwise, if they trigger maybePanic, this container won't get cleaned up
// and it'll get in the way of future runs.
l.vols = c
maybePanic(c.Start(ctx))
maybePanic(c.Wait(ctx, container.WaitConditionNotRunning))
}
// cockroachEntryPoint returns the value to be used as
// container.Config.Entrypoint for a container running the cockroach
// binary under test.
// TODO(bdarnell): refactor this to minimize globals
func cockroachEntrypoint() []string {
var entrypoint []string
if *cockroachImage == defaultImage {
entrypoint = append(entrypoint, CockroachBinaryInContainer)
} else if *cockroachEntry != "" {
entrypoint = append(entrypoint, *cockroachEntry)
}
return entrypoint
}
// createRoach creates the docker container for a testNode. It may be called in
// parallel to start many nodes at once, and thus should remain threadsafe.
func (l *DockerCluster) createRoach(
ctx context.Context, node *testNode, vols *Container, env []string, cmd ...string,
) {
l.panicOnStop()
hostConfig := container.HostConfig{
PublishAllPorts: true,
NetworkMode: container.NetworkMode(l.networkID),
}
if vols != nil {
hostConfig.VolumesFrom = append(hostConfig.VolumesFrom, vols.id)
}
var hostname string
if node.index >= 0 {
hostname = fmt.Sprintf("roach-%s-%d", l.clusterID, node.index)
}
log.Infof(ctx, "creating docker container with name: %s", hostname)
var err error
node.Container, err = createContainer(
ctx,
l,
container.Config{
Hostname: hostname,
Image: *cockroachImage,
ExposedPorts: map[nat.Port]struct{}{
DefaultTCP: {},
defaultHTTP: {},
},
Entrypoint: cockroachEntrypoint(),
Env: env,
Cmd: cmd,
Labels: map[string]string{
// Allow for `docker ps --filter label=Hostname=roach-<id>-0` or `--filter label=Roach`.
"Hostname": hostname,
"Roach": "",
"Acceptance-cluster-id": l.clusterID,
},
},
hostConfig,
node.nodeStr,
)
maybePanic(err)
}
func (l *DockerCluster) createNodeCerts() {
// TODO(mjibson): Including "cockroach" here to mimic
// GenerateCerts. Currently when this function is called it overwrites
// the node.crt and node.key files generated by that function. Until
// this is correctly fixed, make the certs generated here at least the
// same as those.
nodes := []string{"localhost", "cockroach", dockerIP().String()}
for _, node := range l.Nodes {
nodes = append(nodes, node.nodeStr)
}
maybePanic(security.CreateNodePair(
certsDir,
filepath.Join(certsDir, security.EmbeddedCAKey),
keyLen, 48*time.Hour, true /* overwrite */, nodes))
}
// startNode starts a Docker container to run testNode. It may be called in
// parallel to start many nodes at once, and thus should remain threadsafe.
func (l *DockerCluster) startNode(ctx context.Context, node *testNode) {
cmd := []string{
"start",
"--certs-dir=/certs/",
"--listen-addr=" + node.nodeStr,
"--vmodule=*=1",
}
// Forward the vmodule flag to the nodes.
vmoduleFlag := flag.Lookup(logflags.VModuleName)
if vmoduleFlag.Value.String() != "" {
cmd = append(cmd, fmt.Sprintf("--%s=%s", vmoduleFlag.Name, vmoduleFlag.Value.String()))
}
for _, store := range node.stores {
storeSpec := base.StoreSpec{
Path: store.dir,
Size: base.SizeSpec{InBytes: int64(store.config.MaxRanges) * maxRangeBytes},
}
cmd = append(cmd, fmt.Sprintf("--store=%s", storeSpec))
}
// Append --join flag for all nodes.
firstNodeAddr := l.Nodes[0].nodeStr
cmd = append(cmd, "--join="+net.JoinHostPort(firstNodeAddr, base.DefaultPort))
dockerLogDir := "/logs/" + node.nodeStr
localLogDir := filepath.Join(l.volumesDir, "logs", node.nodeStr)
cmd = append(
cmd,
"--logtostderr=ERROR",
"--log-dir="+dockerLogDir)
env := []string{
"COCKROACH_SCAN_MAX_IDLE_TIME=200ms",
"COCKROACH_SKIP_UPDATE_CHECK=1",
"COCKROACH_CRASH_REPORTS=",
}
l.createRoach(ctx, node, l.vols, env, cmd...)
maybePanic(node.Start(ctx))
httpAddr := node.Addr(ctx, defaultHTTP)
log.Infof(ctx, `*** started %[1]s ***
ui: %[2]s
trace: %[2]s/debug/requests
logs: %[3]s/cockroach.INFO
pprof: docker exec -it %[4]s pprof https+insecure://$(hostname):%[5]s/debug/pprof/heap
cockroach: %[6]s
cli-env: COCKROACH_INSECURE=false COCKROACH_CERTS_DIR=%[7]s COCKROACH_HOST=%s:%d`,
node.Name(), "https://"+httpAddr.String(), localLogDir, node.Container.id[:5],
base.DefaultHTTPPort, cmd, certsDir, httpAddr.IP, httpAddr.Port)
}
// RunInitCommand runs the `cockroach init` command. Normally called
// automatically, but exposed for tests that use INIT_NONE. nodeIdx
// may designate any node in the cluster as the target of the command.
func (l *DockerCluster) RunInitCommand(ctx context.Context, nodeIdx int) {
containerConfig := container.Config{
Image: *cockroachImage,
Entrypoint: cockroachEntrypoint(),
Cmd: []string{
"init",
"--certs-dir=/certs/",
"--host=" + l.Nodes[nodeIdx].nodeStr,
"--logtostderr",
},
}
log.Infof(ctx, "trying to initialize via %v", containerConfig.Cmd)
maybePanic(l.OneShot(ctx, defaultImage, types.ImagePullOptions{},
containerConfig, container.HostConfig{}, "init-command"))
log.Info(ctx, "cluster successfully initialized")
}
// returns false is the event
func (l *DockerCluster) processEvent(ctx context.Context, event events.Message) bool {
l.mu.Lock()
defer l.mu.Unlock()
// If there's currently a oneshot container, ignore any die messages from
// it because those are expected.
if l.oneshot != nil && event.ID == l.oneshot.id && event.Status == eventDie {
return true
}
for i, n := range l.Nodes {
if n != nil && n.id == event.ID {
if log.V(1) {
log.Errorf(ctx, "node=%d status=%s", i, event.Status)
}
select {
case l.events <- Event{NodeIndex: i, Status: event.Status}:
default:
panic("events channel filled up")
}
return true
}
}
log.Infof(ctx, "received docker event for unrecognized container: %+v",
event)
// An event on any other container is unexpected. Die.
select {
case <-l.stopper.ShouldQuiesce():
case <-l.monitorCtx.Done():
default:
// There is a very tiny race here: the signal handler might be closing the
// stopper simultaneously.
log.Errorf(ctx, "stopping due to unexpected event: %+v", event)
if rc, err := l.client.ContainerLogs(context.Background(), event.Actor.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
}); err == nil {
defer rc.Close()
if _, err := io.Copy(os.Stderr, rc); err != nil {
log.Infof(ctx, "error listing logs: %s", err)
}
}
}
return false
}
func (l *DockerCluster) monitor(ctx context.Context) {
if log.V(1) {
log.Infof(ctx, "events monitor starts")
defer log.Infof(ctx, "events monitor exits")
}
longPoll := func() bool {
// If our context was canceled, it's time to go home.
if l.monitorCtx.Err() != nil {
return false
}
eventq, errq := l.client.Events(l.monitorCtx, types.EventsOptions{
Filters: filters.NewArgs(
filters.Arg("label", "Acceptance-cluster-id="+l.clusterID),
),
})
for {
select {
case err := <-errq:
log.Infof(ctx, "event stream done, resetting...: %s", err)
// Sometimes we get a random string-wrapped EOF error back.
// Hard to assert on, so we just let this goroutine spin.
return true
case event := <-eventq:
// Currently, the only events generated (and asserted against) are "die"
// and "restart", to maximize compatibility across different versions of
// Docker.
switch event.Status {
case eventDie, eventRestart:
if !l.processEvent(ctx, event) {
return false
}
}
}
}
}
for longPoll() {
}
}
// Start starts the cluster.
func (l *DockerCluster) Start(ctx context.Context) {
defer l.stopOnPanic(ctx)
l.mu.Lock()
defer l.mu.Unlock()
l.createNetwork(ctx)
l.initCluster(ctx)
log.Infof(ctx, "creating node certs (%dbit) in: %s", keyLen, certsDir)
l.createNodeCerts()
l.monitorCtx, l.monitorCtxCancelFunc = context.WithCancel(context.Background())
go l.monitor(ctx)
var wg sync.WaitGroup
wg.Add(len(l.Nodes))
for _, node := range l.Nodes {
go func(node *testNode) {
l.startNode(ctx, node)
wg.Done()
}(node)
}
wg.Wait()
if l.config.InitMode == INIT_COMMAND && len(l.Nodes) > 0 {
l.RunInitCommand(ctx, 0)
}
}
// Assert drains the Events channel and compares the actual events with those
// expected to have been generated by the operations performed on the nodes in
// the cluster (restart, kill, ...). In the event of a mismatch, the passed
// Tester receives a fatal error.
func (l *DockerCluster) Assert(ctx context.Context, t testing.TB) {
const almostZero = 50 * time.Millisecond
filter := func(ch chan Event, wait time.Duration) *Event {
select {
case act := <-ch:
return &act
case <-time.After(wait):
}
return nil
}
var events []Event
for {
exp := filter(l.expectedEvents, almostZero)
if exp == nil {
break
}
act := filter(l.events, 15*time.Second)
if act == nil || *exp != *act {
t.Fatalf("expected event %v, got %v (after %v)", exp, act, events)
}
events = append(events, *exp)
}
if cur := filter(l.events, almostZero); cur != nil {
t.Fatalf("unexpected extra event %v (after %v)", cur, events)
}
if log.V(2) {
log.Infof(ctx, "asserted %v", events)
}
}
// AssertAndStop calls Assert and then stops the cluster. It is safe to stop
// the cluster multiple times.
func (l *DockerCluster) AssertAndStop(ctx context.Context, t testing.TB) {
defer l.stop(ctx)
l.Assert(ctx, t)
}
// stop stops the cluster.
func (l *DockerCluster) stop(ctx context.Context) {
if *waitOnStop {
log.Infof(ctx, "waiting for interrupt")
<-l.stopper.ShouldQuiesce()
}
log.Infof(ctx, "stopping")
l.mu.Lock()
defer l.mu.Unlock()
if l.monitorCtxCancelFunc != nil {
l.monitorCtxCancelFunc()
l.monitorCtxCancelFunc = nil
}
if l.vols != nil {
maybePanic(l.vols.Kill(ctx))
maybePanic(l.vols.Remove(ctx))
l.vols = nil
}
for i, n := range l.Nodes {
if n.Container == nil {
continue
}
ci, err := n.Inspect(ctx)
crashed := err != nil || (!ci.State.Running && ci.State.ExitCode != 0)
maybePanic(n.Kill(ctx))
// TODO(bdarnell): make these filenames more consistent with structured
// logs?
file := filepath.Join(l.volumesDir, "logs", nodeStr(l, i),
fmt.Sprintf("stderr.%s.log", strings.Replace(
timeutil.Now().Format(time.RFC3339), ":", "_", -1)))
maybePanic(os.MkdirAll(filepath.Dir(file), 0755))
w, err := os.Create(file)
maybePanic(err)
defer w.Close()
maybePanic(n.Logs(ctx, w))
log.Infof(ctx, "node %d: stderr at %s", i, file)
if crashed {
log.Infof(ctx, "~~~ node %d CRASHED ~~~~", i)
}
maybePanic(n.Remove(ctx))
}
l.Nodes = nil
if l.networkID != "" {
maybePanic(
l.client.NetworkRemove(ctx, l.networkID))
l.networkID = ""
l.networkName = ""
}
}
// NewDB implements the Cluster interface.
func (l *DockerCluster) NewDB(ctx context.Context, i int) (*gosql.DB, error) {
return gosql.Open("postgres", l.PGUrl(ctx, i))
}
// InternalIP returns the IP address used for inter-node communication.
func (l *DockerCluster) InternalIP(ctx context.Context, i int) net.IP {
c := l.Nodes[i]
containerInfo, err := c.Inspect(ctx)
if err != nil {
return nil
}
return net.ParseIP(containerInfo.NetworkSettings.Networks[l.networkName].IPAddress)
}
// PGUrl returns a URL string for the given node postgres server.
func (l *DockerCluster) PGUrl(ctx context.Context, i int) string {
certUser := security.RootUser
options := url.Values{}
options.Add("sslmode", "verify-full")
options.Add("sslcert", filepath.Join(certsDir, security.EmbeddedRootCert))
options.Add("sslkey", filepath.Join(certsDir, security.EmbeddedRootKey))
options.Add("sslrootcert", filepath.Join(certsDir, security.EmbeddedCACert))
pgURL := url.URL{
Scheme: "postgres",
User: url.User(certUser),
Host: l.Nodes[i].Addr(ctx, DefaultTCP).String(),
RawQuery: options.Encode(),
}
return pgURL.String()
}
// NumNodes returns the number of nodes in the cluster.
func (l *DockerCluster) NumNodes() int {
return len(l.Nodes)
}
// Kill kills the i-th node.
func (l *DockerCluster) Kill(ctx context.Context, i int) error {
if err := l.Nodes[i].Kill(ctx); err != nil {
return errors.Wrapf(err, "failed to kill node %d", i)
}
return nil
}
// Restart restarts the given node. If the node isn't running, this starts it.
func (l *DockerCluster) Restart(ctx context.Context, i int) error {
// The default timeout is 10 seconds.
if err := l.Nodes[i].Restart(ctx, nil); err != nil {
return errors.Wrapf(err, "failed to restart node %d", i)
}
return nil
}
// URL returns the base url.
func (l *DockerCluster) URL(ctx context.Context, i int) string {
return "https://" + l.Nodes[i].Addr(ctx, defaultHTTP).String()
}
// Addr returns the host and port from the node in the format HOST:PORT.
func (l *DockerCluster) Addr(ctx context.Context, i int, port string) string {
return l.Nodes[i].Addr(ctx, nat.Port(port+"/tcp")).String()
}
// Hostname implements the Cluster interface.
func (l *DockerCluster) Hostname(i int) string {
return l.Nodes[i].nodeStr
}
// ExecCLI runs ./cockroach <args> with sane defaults.
func (l *DockerCluster) ExecCLI(ctx context.Context, i int, cmd []string) (string, string, error) {
cmd = append([]string{CockroachBinaryInContainer}, cmd...)
cmd = append(cmd, "--host", l.Hostname(i), "--certs-dir=/certs")
cfg := types.ExecConfig{
Cmd: cmd,
AttachStderr: true,
AttachStdout: true,
}
createResp, err := l.client.ContainerExecCreate(ctx, l.Nodes[i].Container.id, cfg)
if err != nil {
return "", "", err
}
var outputStream, errorStream bytes.Buffer
{
resp, err := l.client.ContainerExecAttach(ctx, createResp.ID, types.ExecStartCheck{})
if err != nil {
return "", "", err
}
defer resp.Close()
ch := make(chan error)
go func() {
_, err := stdcopy.StdCopy(&outputStream, &errorStream, resp.Reader)
ch <- err
}()
if err := <-ch; err != nil {
return "", "", err
}
}
{
resp, err := l.client.ContainerExecInspect(ctx, createResp.ID)
if err != nil {
return "", "", err
}
if resp.Running {
return "", "", errors.Errorf("command still running")
}
if resp.ExitCode != 0 {
o, e := outputStream.String(), errorStream.String()
return o, e, fmt.Errorf("error executing %s:\n%s\n%s",
cmd, o, e)
}
}
return outputStream.String(), errorStream.String(), nil
}
// Cleanup removes the cluster's volumes directory, optionally preserving the
// logs directory.
func (l *DockerCluster) Cleanup(ctx context.Context, preserveLogs bool) {
volumes, err := ioutil.ReadDir(l.volumesDir)
if err != nil {
log.Warningf(ctx, "%v", err)
return
}
for _, v := range volumes {
if preserveLogs && v.Name() == "logs" {
continue
}
if err := os.RemoveAll(filepath.Join(l.volumesDir, v.Name())); err != nil {
log.Warningf(ctx, "%v", err)
}
}
}