This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
cluster.go
815 lines (745 loc) · 25.1 KB
/
cluster.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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package test
import (
"context"
"fmt"
"math"
"sort"
"strings"
"sync"
"testing"
"time"
"unicode"
pilosa "github.com/featurebasedb/featurebase/v3"
"github.com/featurebasedb/featurebase/v3/api/client"
"github.com/featurebasedb/featurebase/v3/disco"
"github.com/featurebasedb/featurebase/v3/etcd"
"github.com/featurebasedb/featurebase/v3/logger"
"github.com/featurebasedb/featurebase/v3/proto"
"github.com/featurebasedb/featurebase/v3/server"
"github.com/featurebasedb/featurebase/v3/storage"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// Cluster represents a per-test wrapper of a "real" cluster.
// Individual tests which request a cluster can get one of these
// back, wrapped with their test name.
type Cluster struct {
*ShareableCluster
tb testing.TB
indexName string
indexBytes []byte
}
// Idx produces an index name suitable for this test's
// cluster, using the name itself, or the concatenation
// of that name and any provided strings, joined by underscores.
func (c *Cluster) Idx(of ...string) string {
if len(of) == 0 {
return c.indexName
}
for i := range of {
of[i] = indexName(of[i])
}
return c.indexName + strings.Join(of, "_")
}
// Format implements Formatter, allowing us to use clusters in
// format strings to handle the extremely common problem of "I
// want to embed the index name in this here string". Because we
// are horrible criminals, we append the rune, so you can use
// %i and %j to get index-name-plus-i and index-name-plus-j,
// respectively. We do assume that the rune works as a plain byte,
// though.
func (c *Cluster) Format(state fmt.State, r rune) {
c.indexBytes = append(c.indexBytes[:0], c.indexName...)
c.indexBytes = append(c.indexBytes, byte(r))
_, _ = state.Write(c.indexBytes)
}
func (c *Cluster) Close() error {
if c.shared {
// We're not going to actually close the cluster, BUT, we do want to
// delete and close all the indexes we might have just made.
h := c.GetHolder(0)
indexes := h.Indexes()
var lastErr error
api := c.Nodes[0].API
for _, idx := range indexes {
name := idx.Name()
if strings.HasPrefix(name, c.indexName) {
err := api.DeleteIndex(context.Background(), name)
if err != nil {
lastErr = err
}
}
}
return lastErr
}
if !c.started {
return errors.New("cluster not started yet")
}
return c.ShareableCluster.Close()
}
func (c *Cluster) Start() error {
if c.started {
return errors.New("cluster already started")
}
c.started = true
return c.ShareableCluster.Start()
}
// ShareableCluster represents a featurebase cluster (multiple Command instances)
// without test-specific overhead.
type ShareableCluster struct {
Nodes []*Command
started bool
shared bool
}
// Query executes an API.Query through one of the cluster's node's API. It fails
// the test if there is an error.
func (c *ShareableCluster) Query(t testing.TB, index, query string) pilosa.QueryResponse {
t.Helper()
if len(c.Nodes) == 0 {
t.Fatal("must have at least one node in cluster to query")
}
return c.GetPrimary().QueryAPI(t, &pilosa.QueryRequest{Index: index, Query: query})
}
// QueryHTTP executes a PQL query through the HTTP endpoint. It fails
// the test for explicit errors, but returns an error which has the
// response body if the HTTP call returns a non-OK status.
func (c *ShareableCluster) QueryHTTP(t testing.TB, index, query string) (string, error) {
t.Helper()
if len(c.Nodes) == 0 {
t.Fatal("must have at least one node in cluster to query")
}
return c.GetPrimary().Query(t, index, "", query)
}
// QueryGRPC executes a PQL query through the GRPC endpoint. It fails the
// test if there is an error.
func (c *ShareableCluster) QueryGRPC(t testing.TB, index, query string) *proto.TableResponse {
t.Helper()
if len(c.Nodes) == 0 {
t.Fatal("must have at least one node in cluster to query")
}
grpcClient, err := client.NewGRPCClient([]string{fmt.Sprintf("%s:%d", c.GetPrimary().Server.GRPCURI().Host, c.GetPrimary().Server.GRPCURI().Port)}, nil, logger.NopLogger)
if err != nil {
t.Fatalf("getting GRPC client: %v", err)
}
defer grpcClient.Close()
tableResp, err := grpcClient.QueryUnary(context.Background(), index, query)
if err != nil {
t.Fatalf("querying unary: %v", err)
}
return tableResp
}
// GetIdleNode gets the node at the given index. This method is used (instead of
// `GetNode()`) when the cluster has yet to be started. In that case, etcd has
// not assigned each node an ID, and therefore the nodes are not in their final,
// sorted order. In other words, this method can only be used to retrieve a node
// when order doesn't matter. An example is if you need to do something like
// this:
//
// c.GetNode(0).Config.Cluster.ReplicaN = 2
// c.GetNode(1).Config.Cluster.ReplicaN = 2
//
// In this example, the test needs the replication factor to be set to 2 before
// starting; it's ok to reference each node by its index in the pre-sorted node
// list. It's also safe to use this method after `MustRunCluster()` if the
// cluster contains only one node.
func (c *ShareableCluster) GetIdleNode(n int) *Command {
return c.Nodes[n]
}
// GetNode gets the node at the given index; this method assumes the cluster has
// already been started. Because the node IDs are assigned randomly, they can be
// in an order that does not align with the test's expectations. For example, a
// test might create a 3-node cluster and retrieve them using `GetNode(0)`,
// `GetNode(1)`, and `GetNode(2)` respectively. But if the node IDs are `456`,
// `123`, `789`, then we actually want `GetNode(0)` to return `c.Nodes[1]`, and
// `GetNode(1)` to return `c.Nodes[0]`. This method looks at all the node IDs,
// sorts them, and then returns the node that the test expects.
func (c *ShareableCluster) GetNode(n int) *Command {
// Put all the node IDs into a list to be sorted.
ids := make([]nodePlace, len(c.Nodes))
for i := range c.Nodes {
ids[i].id = c.Nodes[i].ID()
ids[i].idx = i
}
// Sort the list.
sort.SliceStable(ids, func(i, j int) bool {
return ids[i].id < ids[j].id
})
// Return the node which is at the given position in the sorted list.
return c.Nodes[ids[n].idx]
}
// GetPrimary gets the node which has been determined to be the primary.
// This used to be node0 in tests, but since implementing etcd, the primary
// can be any node in the cluster, so we have to use this method in tests which
// need to act on the primary.
func (c *ShareableCluster) GetPrimary() *Command {
for _, n := range c.Nodes {
if n.IsPrimary() {
return n
}
}
return nil
}
// GetNonPrimary gets first first non-primary node in the list of nodes.
func (c *ShareableCluster) GetNonPrimary() *Command {
for _, n := range c.Nodes {
if !n.IsPrimary() {
return n
}
}
return nil
}
// GetNonPrimaries gets all nodes except the primary.
func (c *ShareableCluster) GetNonPrimaries() []*Command {
rtn := make([]*Command, 0)
for _, n := range c.Nodes {
if !n.IsPrimary() {
rtn = append(rtn, n)
}
}
return rtn
}
// nodePlace represents a node's ID and its index into the c.Nodes slice.
type nodePlace struct {
id string
idx int
}
func (c *ShareableCluster) GetHolder(n int) *Holder {
return &Holder{Holder: c.GetNode(n).Server.Holder()}
}
func (c *ShareableCluster) Len() int {
return len(c.Nodes)
}
func (c *ShareableCluster) ImportBitsWithTimestamp(t testing.TB, index, field string, rowcols [][2]uint64, timestamps []int64) {
t.Helper()
byShard := make(map[uint64][][2]uint64)
byShardTs := make(map[uint64][]int64)
for i, rowcol := range rowcols {
shard := rowcol[1] / pilosa.ShardWidth
byShard[shard] = append(byShard[shard], rowcol)
if len(timestamps) > 0 {
byShardTs[shard] = append(byShardTs[shard], timestamps[i])
}
}
for shard, bits := range byShard {
rowIDs := make([]uint64, len(bits))
colIDs := make([]uint64, len(bits))
for i, bit := range bits {
rowIDs[i] = bit[0]
colIDs[i] = bit[1]
}
nodes, err := c.GetPrimary().API.ShardNodes(context.Background(), index, shard)
if err != nil {
t.Fatalf("getting shard nodes: %v", err)
}
// TODO won't be necessary to do all nodes once that works hits
// (travis) this TODO is not clear to me, but I think it's
// suggesting that elsewhere we would support importing to a
// single node, regardless of where the data ends up.
for _, node := range nodes {
for _, com := range c.Nodes {
if com.API.Node().ID != node.ID {
continue
}
func() {
qcx := com.API.Txf().NewQcx()
defer qcx.Abort()
if len(timestamps) == 0 {
err := com.API.Import(context.Background(), qcx, &pilosa.ImportRequest{
Index: index,
Field: field,
Shard: shard,
RowIDs: rowIDs,
ColumnIDs: colIDs,
})
if err != nil {
t.Fatalf("importing data: %v", err)
}
} else {
ts := byShardTs[shard]
err := com.API.Import(context.Background(), qcx, &pilosa.ImportRequest{
Index: index,
Field: field,
Shard: shard,
RowIDs: rowIDs,
ColumnIDs: colIDs,
Timestamps: ts,
})
if err != nil {
t.Fatalf("importing data: %v", err)
}
}
}()
}
}
}
}
func (c *ShareableCluster) ImportBits(t testing.TB, index, field string, rowcols [][2]uint64) {
var noTime []int64
c.ImportBitsWithTimestamp(t, index, field, rowcols, noTime)
}
// ImportKeyKey imports data into an index where both the index and
// the field are using string keys.
func (c *ShareableCluster) ImportKeyKey(t testing.TB, index, field string, valAndRecKeys [][2]string) {
t.Helper()
importRequest := &pilosa.ImportRequest{
Index: index,
Field: field,
RowKeys: make([]string, len(valAndRecKeys)),
ColumnKeys: make([]string, len(valAndRecKeys)),
}
for i, vk := range valAndRecKeys {
importRequest.RowKeys[i] = vk[0]
importRequest.ColumnKeys[i] = vk[1]
}
qcx := c.GetPrimary().API.Txf().NewQcx()
defer qcx.Abort()
err := c.GetPrimary().API.Import(context.Background(), qcx, importRequest)
if err != nil {
t.Fatalf("importing keykey data: %v", err)
}
}
// TimeQuantumKey is a string key and a string+key value
type TimeQuantumKey struct {
RowKey string
ColKey string
Ts int64
}
// ImportTimeQuantumKey imports data into an index where the index is keyd
// and the field is a time-quantum
func (c *ShareableCluster) ImportTimeQuantumKey(t testing.TB, index, field string, entries []TimeQuantumKey) {
t.Helper()
importRequest := &pilosa.ImportRequest{
Index: index,
Field: field,
RowKeys: make([]string, len(entries)),
ColumnKeys: make([]string, len(entries)),
Timestamps: make([]int64, len(entries)),
}
for i, entry := range entries {
importRequest.ColumnKeys[i] = entry.ColKey
importRequest.RowKeys[i] = entry.RowKey
importRequest.Timestamps[i] = entry.Ts
}
qcx := c.GetPrimary().API.Txf().NewQcx()
defer qcx.Abort()
err := c.GetPrimary().API.Import(context.Background(), qcx, importRequest)
if err != nil {
t.Fatalf("importing keykey data: %v", err)
}
}
// IntKey is a string key and a signed integer value.
type IntKey struct {
Val int64
Key string
}
// ImportIntKey imports int data into an index which uses string keys.
func (c *ShareableCluster) ImportIntKey(t testing.TB, index, field string, pairs []IntKey) {
t.Helper()
importRequest := &pilosa.ImportValueRequest{
Index: index,
Field: field,
Shard: math.MaxUint64,
ColumnKeys: make([]string, len(pairs)),
Values: make([]int64, len(pairs)),
}
for i, pair := range pairs {
importRequest.Values[i] = pair.Val
importRequest.ColumnKeys[i] = pair.Key
}
qcx := c.GetPrimary().API.Txf().NewQcx()
defer qcx.Abort()
if err := c.GetPrimary().API.ImportValue(context.Background(), qcx, importRequest); err != nil {
t.Fatalf("importing IntKey data: %v", err)
}
}
type IntID struct {
Val int64
ID uint64
}
// ImportIntID imports data into an int field in an unkeyed index.
func (c *ShareableCluster) ImportIntID(t testing.TB, index, field string, pairs []IntID) {
t.Helper()
importRequest := &pilosa.ImportValueRequest{
Index: index,
Field: field,
Shard: math.MaxUint64,
ColumnIDs: make([]uint64, len(pairs)),
Values: make([]int64, len(pairs)),
}
for i, pair := range pairs {
importRequest.Values[i] = pair.Val
importRequest.ColumnIDs[i] = pair.ID
}
qcx := c.GetPrimary().API.Txf().NewQcx()
defer qcx.Abort()
if err := c.GetPrimary().API.ImportValue(context.Background(), qcx, importRequest); err != nil {
t.Fatalf("importing IntID data: %v", err)
}
}
// KeyID represents a key and an ID for importing data into an index
// and field where one uses string keys and the other does not.
type KeyID struct {
Key string
ID uint64
}
// ImportIDKey imports data into an unkeyed set field in a keyed index.
func (c *ShareableCluster) ImportIDKey(t testing.TB, index, field string, pairs []KeyID) {
t.Helper()
importRequest := &pilosa.ImportRequest{
Index: index,
Field: field,
RowIDs: make([]uint64, len(pairs)),
ColumnKeys: make([]string, len(pairs)),
}
for i, pair := range pairs {
importRequest.RowIDs[i] = pair.ID
importRequest.ColumnKeys[i] = pair.Key
}
qcx := c.GetPrimary().API.Txf().NewQcx()
defer qcx.Abort()
err := c.GetPrimary().API.Import(context.Background(), qcx, importRequest)
if err != nil {
t.Fatalf("importing IDKey data: %v", err)
}
}
// CreateField creates the index (if necessary) and field specified.
func (c *ShareableCluster) CreateField(t testing.TB, index string, iopts pilosa.IndexOptions, field string, fopts ...pilosa.FieldOption) *pilosa.Field {
t.Helper()
idx, err := c.GetPrimary().API.CreateIndex(context.Background(), index, iopts)
if err != nil && !strings.Contains(err.Error(), "index already exists") {
t.Fatalf("creating index: %v", err)
} else if err != nil { // index exists
idx, err = c.GetPrimary().API.Index(context.Background(), index)
if err != nil {
t.Fatalf("getting index: %v", err)
}
}
if idx.Options() != iopts {
t.Logf("existing index options:\n%v\ndon't match given opts:\n%v\n in pilosa/test.Cluster.CreateField", idx.Options(), iopts)
}
f, err := c.GetPrimary().API.CreateField(context.Background(), index, field, fopts...)
// we'll assume the field doesn't exist because checking if the options
// match seems painful.
if err != nil {
t.Fatalf("creating field: %v", err)
}
return f
}
// Start runs a Cluster
func (c *ShareableCluster) Start() error {
var eg errgroup.Group
for _, cc := range c.Nodes {
cc := cc
eg.Go(func() error {
return cc.Start()
})
}
err := eg.Wait()
if err != nil {
return errors.Wrap(err, "starting cluster")
}
return c.AwaitState(disco.ClusterStateNormal, 30*time.Second)
}
// Close stops a Cluster
func (c *ShareableCluster) Close() error {
for i, cc := range c.Nodes {
if err := cc.Close(); err != nil {
return errors.Wrapf(err, "stopping server %d", i)
}
}
return nil
}
func (c *ShareableCluster) CloseAndRemoveNonPrimary() error {
if c.shared {
return errors.New("can't close-and-remove in shared cluster")
}
for i, n := range c.Nodes {
if !n.IsPrimary() {
return c.CloseAndRemove(i)
}
}
return errors.New("could not find non-primary node")
}
func (c *ShareableCluster) CloseAndRemove(n int) error {
if c.shared {
return errors.New("can't close-and-remove in shared cluster")
}
if n < 0 || n >= len(c.Nodes) {
return fmt.Errorf("close/remove from cluster: index %d out of range (len %d)", n, len(c.Nodes))
}
err := c.Nodes[n].Close()
copy(c.Nodes[n:], c.Nodes[n+1:])
c.Nodes = c.Nodes[:len(c.Nodes)-1]
return err
}
// AwaitPrimaryState waits for the cluster primary to reach a specified cluster state.
// When this happens, we know etcd reached a combination of node states that
// would imply this cluster state, but some nodes may not have caught up yet;
// we just test that the coordinator thought the cluster was in the given state.
func (c *ShareableCluster) AwaitPrimaryState(expectedState disco.ClusterState, timeout time.Duration) error {
if len(c.Nodes) < 1 {
return errors.New("can't await coordinator state on an empty cluster")
}
primary := c.GetPrimary()
if primary == nil {
startTime := time.Now()
var elapsed time.Duration
for elapsed = 0; elapsed <= timeout; elapsed = time.Since(startTime) {
time.Sleep(50 * time.Millisecond)
primary = c.GetPrimary()
if primary != nil {
break
}
}
if primary == nil {
return errors.New("timed out waiting for cluster to have valid topology")
}
// we used up some of our timeout waiting for this
timeout -= elapsed
}
onlyCoordinator := &ShareableCluster{Nodes: []*Command{primary}}
return onlyCoordinator.AwaitState(expectedState, timeout)
}
// ExceptionalState returns an error if any node in the cluster is not
// in the expected state.
func (c *ShareableCluster) ExceptionalState(expectedState disco.ClusterState) error {
for _, node := range c.Nodes {
state, err := node.API.State()
if err != nil || state != expectedState {
return fmt.Errorf("node %q: state %s: err %v", node.ID(), state, err)
}
}
return nil
}
// AwaitState waits for the whole cluster to reach a specified state.
func (c *ShareableCluster) AwaitState(expectedState disco.ClusterState, timeout time.Duration) (err error) {
if len(c.Nodes) < 1 {
return errors.New("can't await state of an empty cluster")
}
startTime := time.Now()
var elapsed time.Duration
for elapsed = 0; elapsed <= timeout; elapsed = time.Since(startTime) {
// Counterintuitive: We're returning if the err *is* nil,
// meaning we've reached the expected state.
if err = c.ExceptionalState(expectedState); err == nil {
return err
}
time.Sleep(50 * time.Millisecond)
}
return fmt.Errorf("waited %v for cluster to reach state %q: %v",
elapsed, expectedState, err)
}
// MustNewCluster creates a new cluster or returns an existing one. It never shares
// a cluster with non-empty opts. If opts contains only one
// slice of command options, those options are used with every node.
// If it is empty, default options are used. Otherwise, it must contain size
// slices of command options, which are used with corresponding nodes.
func MustNewCluster(tb testing.TB, size int, opts ...[]server.CommandOption) *Cluster {
if size > 1 && !etcd.AllowCluster() {
tb.Skip("Testing PLG which does not allow clustering")
}
tb.Helper()
shareable := len(opts) == 0
// We want tests to default to using the in-memory translate store, so we
// prepend opts with that functional option. If a different translate store
// has been specified, it will override this one.
opts = prependOpts(opts, size)
c, err := newCluster(tb, size, shareable, opts...)
if err != nil {
tb.Fatalf("new cluster: %v", err)
}
return c
}
// MustUnsharedCluster creates a new cluster. If opts contains only one
// slice of command options, those options are used with every node.
// If it is empty, default options are used. Otherwise, it must contain size
// slices of command options, which are used with corresponding nodes. The
// new cluster is always unshared.
func MustUnsharedCluster(tb testing.TB, size int, opts ...[]server.CommandOption) *Cluster {
if size > 1 && !etcd.AllowCluster() {
tb.Skip("Testing PLG which does not allow clustering")
}
tb.Helper()
// We want tests to default to using the in-memory translate store, so we
// prepend opts with that functional option. If a different translate store
// has been specified, it will override this one.
opts = prependOpts(opts, size)
c, err := newCluster(tb, size, false, opts...)
if err != nil {
tb.Fatalf("new cluster: %v", err)
}
return c
}
// MustRunUnsharedCluster creates a new cluster. If opts contains only one
// slice of command options, those options are used with every node.
// If it is empty, default options are used. Otherwise, it must contain size
// slices of command options, which are used with corresponding nodes. The
// new cluster is always unshared. The new cluster is started automatically,
// or the test is failed.
func MustRunUnsharedCluster(tb testing.TB, size int, opts ...[]server.CommandOption) *Cluster {
if size > 1 && !etcd.AllowCluster() {
tb.Skip("Testing PLG which does not allow clustering")
}
tb.Helper()
// We want tests to default to using the in-memory translate store, so we
// prepend opts with that functional option. If a different translate store
// has been specified, it will override this one.
opts = prependOpts(opts, size)
c, err := newCluster(tb, size, false, opts...)
if err != nil {
tb.Fatalf("new cluster: %v", err)
}
err = c.Start()
if err != nil {
tb.Fatalf("starting cluster: %v", err)
}
return c
}
type clusterCache struct {
mu sync.Mutex
clusters map[int]*ShareableCluster
}
// CleanupClusters calls the close functions on any shared clusters that are
// still open.
func (c *clusterCache) CleanupClusters() {
for k, v := range c.clusters {
_ = v.Close()
delete(c.clusters, k)
}
}
func (c *clusterCache) newCluster(tb testing.TB, size int) (*ShareableCluster, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c := c.clusters[size]; c != nil {
return c, nil
}
// Make a whole-test wrapper so that the cluster we create will use the provided tb
// for nearly everything, but the call to TempDir inside NewCommand will pick up a
// persistent directory which outlives the provided TB.
newTB := NewWholeTestRun(tb)
if c.clusters == nil {
c.clusters = make(map[int]*ShareableCluster)
// tb should always be a wholeTestRun for clusterCache, and we need to
// register with that, so our cleanup happens *before* the deletion of
// the directories, otherwise etcd can fail to flush WAL files on
// exit, causing tests to fail.
newTB.Cleanup(c.CleanupClusters)
}
cluster, err := underlyingNewCluster(newTB, size)
if err != nil {
return nil, err
}
cluster.shared = true
c.clusters[size] = cluster
return cluster, nil
}
func underlyingNewCluster(tb DirCleaner, size int, opts ...[]server.CommandOption) (*ShareableCluster, error) {
cluster := &ShareableCluster{Nodes: make([]*Command, size)}
for i := 0; i < size; i++ {
var commandOpts []server.CommandOption
if len(opts) > 0 {
commandOpts = opts[i%len(opts)]
}
m := NewCommandNode(tb, commandOpts...)
m.Config.ImportWorkerPoolSize = 2
cluster.Nodes[i] = m
}
// The GetPorts... stuff calls things elsewhere that want a plain testing.TB,
// and doesn't produce permanent directories, I think.
err := GetPortsGenConfigs(tb, cluster.Nodes)
if err != nil {
return nil, errors.Wrap(err, "configuring cluster ports")
}
return cluster, nil
}
var globalClusterCache clusterCache
// newCluster creates a new cluster, using the shared cluster cache if no opts are
// specified.
func newCluster(tb testing.TB, size int, shareable bool, opts ...[]server.CommandOption) (*Cluster, error) {
if size == 0 {
return nil, errors.New("cluster must contain at least one node")
}
if len(opts) != size && len(opts) != 0 && len(opts) != 1 {
return nil, errors.New("Slice of CommandOptions must be of length 0, 1, or equal to the number of cluster nodes")
}
var shared *ShareableCluster
var err error
if !shareable {
shared, err = underlyingNewCluster(tb, size, opts...)
} else {
shared, err = globalClusterCache.newCluster(tb, size)
}
if err != nil {
return nil, err
}
return &Cluster{ShareableCluster: shared, tb: tb, indexName: indexName(tb.Name())}, nil
}
// MustRunCluster creates and starts a new cluster. The opts parameter
// is slightly magical; see MustNewCluster.
func MustRunCluster(tb testing.TB, size int, opts ...[]server.CommandOption) *Cluster {
cluster := MustNewCluster(tb, size, opts...)
if !cluster.started {
err := cluster.Start()
if err != nil {
tb.Fatalf("run cluster: %v", err)
}
cluster.started = true
}
return cluster
}
// prependOpts applies prependTestServerOpts to each of the ops (one per
// node, or one for the entire cluser).
func prependOpts(opts [][]server.CommandOption, size int) [][]server.CommandOption {
if len(opts) == 0 {
opts = make([][]server.CommandOption, size)
for i := 0; i < size; i++ {
opts[i] = prependTestServerOpts([]server.CommandOption{})
}
} else if len(opts) == 1 {
opts2 := make([][]server.CommandOption, size)
for i := 0; i < size; i++ {
opts2[i] = prependTestServerOpts(opts[0])
}
return opts2
} else {
for i := range opts {
opts[i] = prependTestServerOpts(opts[i])
}
}
return opts
}
// prependTestServerOpts prepends opts with the OpenInMemTranslateStore,
// tweaks to initial startup delay, and storage config to disable fsync and
// specify a smaller RBF size.
func prependTestServerOpts(opts []server.CommandOption) []server.CommandOption {
cfg := pilosa.TestHolderConfig()
defaultOpts := []server.CommandOption{
server.OptCommandServerOptions(
pilosa.OptServerOpenTranslateStore(pilosa.OpenInMemTranslateStore),
pilosa.OptServerNodeDownRetries(5, 100*time.Millisecond),
pilosa.OptServerStorageConfig(&storage.Config{
Backend: storage.DefaultBackend,
FsyncEnabled: false,
}),
pilosa.OptServerRBFConfig(cfg.RBFConfig),
),
}
return append(defaultOpts, opts...)
}
func indexName(in string) string {
return strings.Map(func(r rune) rune {
if r < 127 {
switch {
case unicode.IsLetter(r):
return unicode.ToLower(r)
case unicode.IsNumber(r):
return r
case r == '/':
return '_'
}
}
return -1
}, in)
}