-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswarm_test.go
More file actions
1300 lines (1149 loc) · 36 KB
/
Copy pathswarm_test.go
File metadata and controls
1300 lines (1149 loc) · 36 KB
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
p2pgrpc "github.com/birros/go-libp2p-grpc"
"github.com/dolthub/dolt/go/libraries/utils/concurrentmap"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/nustiueudinastea/doltswarm"
swarmproto "github.com/nustiueudinastea/doltswarm/proto"
"github.com/nustiueudinastea/doltswarmdemo/p2p"
p2pproto "github.com/nustiueudinastea/doltswarmdemo/p2p/proto"
"github.com/segmentio/ksuid"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
gocmd "gopkg.in/ryankurte/go-async-cmd.v1"
)
//
// Implemented tests:
// - TestIntegration: basic integration test with convergence verification
// - TestSequentialWritesPropagation: commits from one peer propagate to all others
// - TestConcurrentWrites: concurrent writes converge deterministically
// - TestCommitOrderConsistency: interleaved writes maintain consistent ordering
//
// TODO:
// - test conflict resolution using custom merge function
// - test denial of commit
// - test that peer is not allowed to commit to specific table
// - test custom table merge function
//
const (
localInit = "localInit"
peerInit = "peerInit"
server = "server"
startPort = 10500
// Convergence test timeouts
defaultConvergenceTimeout = 120 * time.Second
defaultPollInterval = 500 * time.Millisecond
logProgressInterval = 10 * time.Second
grpcCallTimeout = 10 * time.Second
)
//
// ConvergenceResult holds the result of convergence verification
//
type ConvergenceResult struct {
Converged bool
AllHeads map[string]string // peerID -> head commit
AllCommitLists map[string][]string // peerID -> ordered commit list
UnresponsivePeers map[string]string // peerID -> last error message
Duration time.Duration
}
// commitListsEqual compares two commit lists for equality (same commits in same order)
func commitListsEqual(list1, list2 []string) bool {
if len(list1) != len(list2) {
return false
}
for i, commit := range list1 {
if commit != list2[i] {
return false
}
}
return true
}
// waitForHeadConvergence waits until all peers have the same HEAD commit
func waitForHeadConvergence(
ctx context.Context,
clients []*p2p.P2PClient,
timeout time.Duration,
pollInterval time.Duration,
) (*ConvergenceResult, error) {
startTime := time.Now()
lastLogTime := startTime
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
result := &ConvergenceResult{
AllHeads: make(map[string]string),
UnresponsivePeers: make(map[string]string),
}
iteration := 0
for {
iteration++
select {
case <-timeoutCtx.Done():
result.Duration = time.Since(startTime)
logger.Warnf("Head convergence timeout after %v (%d iterations)", result.Duration, iteration)
logConvergenceStatus(result, clients)
return result, nil
default:
}
// Fetch heads from all clients in parallel with per-call timeout
var wg sync.WaitGroup
headsChan := make(chan struct {
peerID string
head string
err error
}, len(clients))
for _, client := range clients {
wg.Add(1)
go func(c *p2p.P2PClient) {
defer wg.Done()
callCtx, callCancel := context.WithTimeout(ctx, grpcCallTimeout)
defer callCancel()
resp, err := c.GetHead(callCtx, &p2pproto.GetHeadRequest{})
if err != nil {
headsChan <- struct {
peerID string
head string
err error
}{c.GetID(), "", err}
return
}
headsChan <- struct {
peerID string
head string
err error
}{c.GetID(), resp.Commit, nil}
}(client)
}
wg.Wait()
close(headsChan)
// Collect results and track unresponsive peers
heads := make(map[string]string)
unresponsive := make(map[string]string)
for res := range headsChan {
if res.err != nil {
unresponsive[res.peerID] = res.err.Error()
continue
}
heads[res.peerID] = res.head
}
result.AllHeads = heads
result.UnresponsivePeers = unresponsive
// Check if all heads are the same
var firstHead string
allSame := true
headCounts := make(map[string]int)
for _, head := range heads {
headCounts[head]++
if firstHead == "" {
firstHead = head
} else if head != firstHead {
allSame = false
}
}
// Periodic logging
if time.Since(lastLogTime) >= logProgressInterval {
lastLogTime = time.Now()
elapsed := time.Since(startTime)
logger.Infof("[%v] Head convergence: %d/%d responsive, %d unresponsive, %d unique heads",
elapsed.Round(time.Second), len(heads), len(clients), len(unresponsive), len(headCounts))
if len(unresponsive) > 0 {
for peerID, errMsg := range unresponsive {
logger.Warnf(" Unresponsive peer %s: %s", peerID[:12], errMsg)
}
}
}
if allSame && len(heads) == len(clients) && len(unresponsive) == 0 {
result.Converged = true
result.Duration = time.Since(startTime)
return result, nil
}
time.Sleep(pollInterval)
}
}
// logConvergenceStatus logs detailed status when convergence fails
func logConvergenceStatus(result *ConvergenceResult, clients []*p2p.P2PClient) {
// Count heads
headCounts := make(map[string][]string) // head -> list of peer IDs
for peerID, head := range result.AllHeads {
headCounts[head] = append(headCounts[head], peerID)
}
logger.Warnf("Convergence status: %d responsive, %d unresponsive",
len(result.AllHeads), len(result.UnresponsivePeers))
if len(headCounts) > 1 {
logger.Warnf(" Found %d different heads:", len(headCounts))
for head, peers := range headCounts {
logger.Warnf(" Head %s: %d peers", head[:12], len(peers))
}
}
if len(result.UnresponsivePeers) > 0 {
logger.Warnf(" Unresponsive peers:")
for peerID, errMsg := range result.UnresponsivePeers {
shortID := peerID
if len(peerID) > 12 {
shortID = peerID[:12]
}
logger.Warnf(" %s: %s", shortID, errMsg)
}
}
}
// waitForCommitHistoryConvergence waits until all peers have identical commit histories
func waitForCommitHistoryConvergence(
ctx context.Context,
clients []*p2p.P2PClient,
timeout time.Duration,
pollInterval time.Duration,
) (*ConvergenceResult, error) {
startTime := time.Now()
lastLogTime := startTime
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
result := &ConvergenceResult{
AllCommitLists: make(map[string][]string),
UnresponsivePeers: make(map[string]string),
}
iteration := 0
for {
iteration++
select {
case <-timeoutCtx.Done():
result.Duration = time.Since(startTime)
logger.Warnf("Commit history convergence timeout after %v (%d iterations)", result.Duration, iteration)
logHistoryConvergenceStatus(result, clients)
return result, nil
default:
}
// Fetch commit lists from all clients in parallel with per-call timeout
var wg sync.WaitGroup
commitsChan := make(chan struct {
peerID string
commits []string
err error
}, len(clients))
for _, client := range clients {
wg.Add(1)
go func(c *p2p.P2PClient) {
defer wg.Done()
callCtx, callCancel := context.WithTimeout(ctx, grpcCallTimeout)
defer callCancel()
resp, err := c.GetAllCommits(callCtx, &p2pproto.GetAllCommitsRequest{})
if err != nil {
commitsChan <- struct {
peerID string
commits []string
err error
}{c.GetID(), nil, err}
return
}
commitsChan <- struct {
peerID string
commits []string
err error
}{c.GetID(), resp.Commits, nil}
}(client)
}
wg.Wait()
close(commitsChan)
// Collect results and track unresponsive peers
commitLists := make(map[string][]string)
unresponsive := make(map[string]string)
for res := range commitsChan {
if res.err != nil {
unresponsive[res.peerID] = res.err.Error()
continue
}
commitLists[res.peerID] = res.commits
}
result.AllCommitLists = commitLists
result.UnresponsivePeers = unresponsive
// Check if all commit lists are identical
var firstList []string
allSame := true
commitCounts := make(map[int]int) // commit count -> number of peers with that count
for _, commits := range commitLists {
commitCounts[len(commits)]++
if firstList == nil {
firstList = commits
} else if !commitListsEqual(firstList, commits) {
allSame = false
}
}
// Periodic logging
if time.Since(lastLogTime) >= logProgressInterval {
lastLogTime = time.Now()
elapsed := time.Since(startTime)
logger.Infof("[%v] History convergence: %d/%d responsive, %d unresponsive",
elapsed.Round(time.Second), len(commitLists), len(clients), len(unresponsive))
if len(commitCounts) > 1 {
logger.Infof(" Commit count distribution: %v", commitCounts)
}
if len(unresponsive) > 0 {
for peerID, errMsg := range unresponsive {
logger.Warnf(" Unresponsive peer %s: %s", peerID[:12], errMsg)
}
}
}
if allSame && len(commitLists) == len(clients) && len(unresponsive) == 0 {
result.Converged = true
result.Duration = time.Since(startTime)
return result, nil
}
time.Sleep(pollInterval)
}
}
// logHistoryConvergenceStatus logs detailed status when history convergence fails
func logHistoryConvergenceStatus(result *ConvergenceResult, clients []*p2p.P2PClient) {
logger.Warnf("History convergence status: %d responsive, %d unresponsive",
len(result.AllCommitLists), len(result.UnresponsivePeers))
// Group by commit count
commitCounts := make(map[int][]string) // commit count -> list of peer IDs
for peerID, commits := range result.AllCommitLists {
count := len(commits)
commitCounts[count] = append(commitCounts[count], peerID)
}
if len(commitCounts) > 1 {
logger.Warnf(" Peers have different commit counts:")
for count, peers := range commitCounts {
logger.Warnf(" %d commits: %d peers", count, len(peers))
}
}
if len(result.UnresponsivePeers) > 0 {
logger.Warnf(" Unresponsive peers:")
for peerID, errMsg := range result.UnresponsivePeers {
shortID := peerID
if len(peerID) > 12 {
shortID = peerID[:12]
}
logger.Warnf(" %s: %s", shortID, errMsg)
}
}
}
// waitForCommitOnAllPeers waits for a specific commit to appear on all peers
func waitForCommitOnAllPeers(
ctx context.Context,
clients []*p2p.P2PClient,
commitHash string,
timeout time.Duration,
pollInterval time.Duration,
) (map[string]bool, error) {
startTime := time.Now()
lastLogTime := startTime
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
peerStatus := make(map[string]bool)
peerErrors := make(map[string]string)
for _, c := range clients {
peerStatus[c.GetID()] = false
}
shortCommit := commitHash
if len(commitHash) > 12 {
shortCommit = commitHash[:12]
}
for {
select {
case <-timeoutCtx.Done():
// Log which peers don't have the commit
missingPeers := []string{}
for peerID, hasCommit := range peerStatus {
if !hasCommit {
shortID := peerID
if len(peerID) > 12 {
shortID = peerID[:12]
}
missingPeers = append(missingPeers, shortID)
}
}
logger.Warnf("Timeout waiting for commit %s: %d/%d peers have it, missing: %v",
shortCommit, countTrue(peerStatus), len(clients), missingPeers)
return peerStatus, nil
default:
}
allHaveCommit := true
for _, client := range clients {
if peerStatus[client.GetID()] {
continue // Already found on this peer
}
callCtx, callCancel := context.WithTimeout(ctx, grpcCallTimeout)
resp, err := client.GetAllCommits(callCtx, &p2pproto.GetAllCommitsRequest{})
callCancel()
if err != nil {
peerErrors[client.GetID()] = err.Error()
allHaveCommit = false
continue
}
delete(peerErrors, client.GetID())
for _, c := range resp.Commits {
if c == commitHash {
peerStatus[client.GetID()] = true
break
}
}
if !peerStatus[client.GetID()] {
allHaveCommit = false
}
}
// Periodic logging
if time.Since(lastLogTime) >= logProgressInterval {
lastLogTime = time.Now()
elapsed := time.Since(startTime)
logger.Infof("[%v] Waiting for commit %s: %d/%d peers have it",
elapsed.Round(time.Second), shortCommit, countTrue(peerStatus), len(clients))
if len(peerErrors) > 0 {
for peerID, errMsg := range peerErrors {
logger.Warnf(" Peer %s error: %s", peerID[:12], errMsg)
}
}
}
if allHaveCommit {
return peerStatus, nil
}
time.Sleep(pollInterval)
}
}
// countTrue counts the number of true values in a map
func countTrue(m map[string]bool) int {
count := 0
for _, v := range m {
if v {
count++
}
}
return count
}
var nrOfInstances = 5
var enableInitProcessOutput = false
var enableProcessOutput = false
var keepTestDir = false
var logger = logrus.New()
var p2pStopper func() error
func init() {
if os.Getenv("ENABLE_INIT_PROCESS_OUTPUT") == "true" {
enableInitProcessOutput = true
}
if os.Getenv("ENABLE_PROCESS_OUTPUT") == "true" {
enableProcessOutput = true
}
if os.Getenv("KEEP_TEST_DIR") == "true" {
keepTestDir = true
}
if os.Getenv("NR_INSTANCES") != "" {
nr, err := strconv.Atoi(os.Getenv("NR_INSTANCES"))
if err != nil {
logger.Fatal(err)
}
nrOfInstances = nr
}
}
//
// testDB is a mock database
//
type testDB struct{}
func (pr *testDB) AddPeer(peerID string, conn *grpc.ClientConn) error {
return nil
}
func (pr *testDB) RemovePeer(peerID string) error {
return nil
}
func (pr *testDB) GetAllCommits() ([]doltswarm.Commit, error) {
return []doltswarm.Commit{}, nil
}
func (pr *testDB) ExecAndCommit(execFunc doltswarm.ExecFunc, commitMsg string) (string, error) {
return "", nil
}
func (pr *testDB) InitFromPeer(peerID string) error {
return nil
}
func (pr *testDB) GetLastCommit(branch string) (doltswarm.Commit, error) {
return doltswarm.Commit{}, nil
}
func (pr *testDB) EnableGRPCServers(server *grpc.Server) error {
return nil
}
func (pr *testDB) Initialized() bool {
return false
}
//
// ServerSyncer is a mock syncer
//
type ServerSyncer struct {
peerCommits *concurrentmap.Map[string, []string]
}
func (s *ServerSyncer) AdvertiseHead(ctx context.Context, req *swarmproto.AdvertiseHeadRequest) (*swarmproto.AdvertiseHeadResponse, error) {
peer, ok := p2pgrpc.RemotePeerFromContext(ctx)
if !ok {
return nil, errors.New("no AuthInfo in context")
}
if commits, found := s.peerCommits.Get(peer.String()); found {
fmt.Println(peer.String(), "appending commit", req.Head)
s.peerCommits.Set(peer.String(), append(commits, req.Head))
} else {
fmt.Println(peer.String(), "first commit", req.Head)
s.peerCommits.Set(peer.String(), []string{req.Head})
}
return &swarmproto.AdvertiseHeadResponse{}, nil
}
func (s *ServerSyncer) RequestHead(ctx context.Context, req *swarmproto.RequestHeadRequest) (*swarmproto.RequestHeadResponse, error) {
return nil, fmt.Errorf("not implemented")
}
func (s *ServerSyncer) peerHasCommit(peer string, commit string) bool {
if commits, found := s.peerCommits.Get(peer); found {
for _, c := range commits {
if commit == c {
return true
}
}
}
return false
}
//
// controller is a wrapper around a process
//
type controller struct {
quitChan chan bool
exitErr chan error
hostID chan string
name string
}
func (c *controller) Name() string {
return c.name
}
func runInstance(testDir string, nr int, mode string, initPeer string, printOutput bool) *controller {
ctrl := &controller{
quitChan: make(chan bool, 100),
exitErr: make(chan error, 100),
hostID: make(chan string, 100),
name: fmt.Sprintf("dsw%d", nr),
}
timeOutSeconds := 5
port := 10500 + nr
waitOutput := ""
commands := []string{"--port", strconv.Itoa(port), "--db", testDir + "/" + ctrl.name}
switch mode {
case localInit:
commands = append(commands, "init", "--local")
waitOutput = "p2p setup done"
timeOutSeconds = 10
case peerInit:
commands = append(commands, "init", "--peer", initPeer)
waitOutput = "Successfully cloned db"
timeOutSeconds = 30
case server:
commands = append(commands, "--no-gui", "--no-commits", "server")
timeOutSeconds = 6000
}
go func() {
timeout := time.After(time.Duration(timeOutSeconds) * time.Second)
logger.Infof("starting proc '%s'(%s)", ctrl.name, mode)
c := gocmd.Command("./ddolt", commands...)
c.OutputChan = make(chan string, 1024)
err := c.Start()
if err != nil {
ctrl.exitErr <- fmt.Errorf("Error starting proc '%s': %s\n", ctrl.name, err.Error())
return
}
hostID := "unknown"
for {
select {
case line := <-c.OutputChan:
if printOutput {
fmt.Printf("stdout '%s'(%s): %s", ctrl.name, hostID, line)
}
if waitOutput != "" && strings.Contains(line, waitOutput) {
if mode == peerInit {
logger.Infof("%s: finished p2p clone", ctrl.name)
} else {
logger.Infof("%s: exiting", ctrl.name)
}
}
if strings.Contains(line, "Shutdown completed") {
err := c.Wait()
if err != nil {
ctrl.exitErr <- fmt.Errorf("Error for proc '%s' when exiting: %s\n", ctrl.name, err.Error())
return
}
logger.Infof("%s: terminated successfully", ctrl.name)
ctrl.exitErr <- nil
return
}
if strings.Contains(line, "server using id") {
tokens := strings.Split(line, " ")
keyToken := tokens[7]
hostid := keyToken[:len(keyToken)-2]
ctrl.hostID <- hostid
hostID = hostid
}
case <-ctrl.quitChan:
logger.Infof("%s: sending interrupt", ctrl.name)
c.Interrupt()
case <-timeout:
err := c.Exit()
if err != nil {
ctrl.exitErr <- fmt.Errorf("Timeout waiting for output '%s': %s", waitOutput, err.Error())
} else {
ctrl.exitErr <- fmt.Errorf("Timeout waiting for output '%s'", waitOutput)
}
return
}
}
}()
return ctrl
}
func doInit(testDir string, nrOfInstances int) error {
fmt.Println("==== Initialising test ====")
ctrl1 := runInstance(testDir, 1, localInit, "", enableInitProcessOutput)
err := <-ctrl1.exitErr
if err != nil {
return fmt.Errorf("failed to local init instance: %s", err.Error())
}
ctrl1 = runInstance(testDir, 1, server, "", enableInitProcessOutput)
ctrl1Host := <-ctrl1.hostID
defer func() {
ctrl1.quitChan <- true
err = <-ctrl1.exitErr
if err != nil {
logger.Error("failed to stop init instance: ", err)
}
}()
clientInstances := make([]*controller, nrOfInstances-1)
for i := 0; i < nrOfInstances-1; i++ {
clientInstances[i] = runInstance(testDir, i+2, peerInit, ctrl1Host, enableInitProcessOutput)
time.Sleep(500 * time.Millisecond)
}
// print host ID for each instance
logger.Infof("Instance '%s' has ID '%s'", ctrl1.Name(), ctrl1Host)
for _, instance := range clientInstances {
hostID := <-instance.hostID
logger.Infof("Instance '%s' has ID '%s'", instance.Name(), hostID)
}
// wait for all instances to finish
for _, instance := range clientInstances {
instanceErr := <-instance.exitErr
if instanceErr != nil {
err = errors.Join(err, fmt.Errorf("instance '%s': %s", instance.Name(), instanceErr.Error()))
}
}
if err != nil {
return err
}
return nil
}
func stopAllInstances(instances []*controller, t *testing.T) {
logger.Info("Stopping p2p instances")
var err error
for _, instance := range instances {
instance.quitChan <- true
}
for _, instance := range instances {
instanceErr := <-instance.exitErr
if instanceErr != nil {
err = errors.Join(err, fmt.Errorf("instance '%s': %s", instance.Name(), instanceErr.Error()))
}
}
if err != nil {
t.Fatal(err)
}
if p2pStopper != nil {
err = p2pStopper()
if err != nil {
t.Fatal(err)
}
}
}
func TestIntegration(t *testing.T) {
testDir, err := os.MkdirTemp("temp", "tst")
if err != nil {
t.Fatal(err)
}
if !keepTestDir {
defer os.RemoveAll(testDir)
}
err = doInit(testDir, nrOfInstances)
if err != nil {
t.Fatal(err)
}
fmt.Printf("==== Starting test %s ====\n", testDir)
instances := make([]*controller, nrOfInstances)
for i := 1; i <= nrOfInstances; i++ {
instances[i-1] = runInstance(testDir, i, server, "", enableProcessOutput)
time.Sleep(500 * time.Millisecond)
}
defer stopAllInstances(instances, t)
instanceIDs := make(map[string]string, nrOfInstances)
// wait for all host ids
for _, instance := range instances {
hostID := <-instance.hostID
instanceIDs[hostID] = instance.Name()
}
logger.Infof("Sleeping for 5 seconds")
time.Sleep(5 * time.Second)
peerListChan := make(chan peer.IDSlice, 100)
tDB := &testDB{}
p2pkey, err := p2p.NewKey(testDir + "/testp2p")
if err != nil {
t.Fatal(err)
}
p2pmgr, err = p2p.NewManager(p2pkey, startPort, peerListChan, logger, tDB)
if err != nil {
t.Fatal(err)
}
logger.Infof("TEST ID: %s", p2pmgr.GetID())
grpcServer := p2pmgr.GetGRPCServer()
srvSyncer := &ServerSyncer{peerCommits: concurrentmap.New[string, []string]()}
swarmproto.RegisterDBSyncerServer(grpcServer, srvSyncer)
p2pStopper, err = p2pmgr.StartServer()
if err != nil {
t.Fatal(err)
}
// Wait for all clients to connect with timeout
clientConnectTimeout := time.Duration(nrOfInstances*10) * time.Second // 10 seconds per instance
if clientConnectTimeout < 60*time.Second {
clientConnectTimeout = 60 * time.Second
}
startWait := time.Now()
lastLog := startWait
for len(p2pmgr.GetClients()) != nrOfInstances {
if time.Since(startWait) > clientConnectTimeout {
connectedClients := p2pmgr.GetClients()
connectedIDs := make([]string, 0, len(connectedClients))
for _, c := range connectedClients {
connectedIDs = append(connectedIDs, c.GetID()[:12])
}
t.Fatalf("Timeout waiting for clients to connect: got %d/%d after %v. Connected: %v",
len(connectedClients), nrOfInstances, clientConnectTimeout, connectedIDs)
}
if time.Since(lastLog) >= logProgressInterval {
lastLog = time.Now()
logger.Infof("[%v] Waiting for clients: %d/%d connected",
time.Since(startWait).Round(time.Second), len(p2pmgr.GetClients()), nrOfInstances)
}
time.Sleep(2 * time.Second)
}
logger.Infof("All %d clients connected in %v", nrOfInstances, time.Since(startWait).Round(time.Second))
clients := p2pmgr.GetClients()
for _, client := range clients {
pingCtx, pingCancel := context.WithTimeout(context.Background(), grpcCallTimeout)
_, err = client.Ping(pingCtx, &p2pproto.PingRequest{
Ping: "pong",
})
pingCancel()
if err != nil {
t.Fatalf("failure to ping client '%s': %s", client.GetID(), err.Error())
}
}
logger.Infof("test p2p ID: %s", p2pmgr.GetID())
//
// Check that all clients have the same head
//
allHeads := make(map[string]string)
for _, client := range clients {
resp, instanceErr := client.GetHead(context.Background(), &p2pproto.GetHeadRequest{})
if instanceErr != nil {
err = errors.Join(err, fmt.Errorf("failure while calling GetHead on instance '%s'(%s): %s", instanceIDs[client.GetID()], client.GetID(), instanceErr.Error()))
continue
}
allHeads[client.GetID()] = resp.Commit
}
if err != nil {
t.Fatalf("%s", err.Error())
}
if len(allHeads) != nrOfInstances {
t.Error("not all instances have a head. Expected: ", nrOfInstances, " Got: ", len(allHeads))
}
for _, head := range allHeads {
if head != allHeads[clients[0].GetID()] {
t.Errorf("heads are not the same: %v", allHeads)
}
}
logger.Infof("Sleeping for 5 seconds")
time.Sleep(10 * time.Second)
//
// Insert and make sure commit is propagated
//
for nr, client := range clients {
uid, err := ksuid.NewRandom()
if err != nil {
t.Fatal(err)
}
queryString := fmt.Sprintf("INSERT INTO %s (id, name) VALUES ('%s', 'propagation test client %d');", tableName, uid.String(), nr)
resp, err := client.ExecSQL(context.Background(), &p2pproto.ExecSQLRequest{Statement: queryString, Msg: fmt.Sprintf("commit propagation test %d", nr)})
if err != nil {
t.Fatal(err)
}
logger.Infof("Waiting for commit %s to be propagated", resp.Commit)
}
//
// Wait for head convergence after all inserts
//
logger.Info("Waiting for head convergence after inserts")
ctx := context.Background()
headResult, err := waitForHeadConvergence(ctx, clients, defaultConvergenceTimeout, defaultPollInterval)
if err != nil {
t.Fatalf("error while waiting for head convergence: %v", err)
}
if !headResult.Converged {
t.Errorf("peers did not converge to same head after %v", headResult.Duration)
t.Logf("Responsive peers (%d):", len(headResult.AllHeads))
for peerID, head := range headResult.AllHeads {
t.Logf(" %s (%s): head=%s", instanceIDs[peerID], peerID[:12], head[:12])
}
if len(headResult.UnresponsivePeers) > 0 {
t.Logf("Unresponsive peers (%d):", len(headResult.UnresponsivePeers))
for peerID, errMsg := range headResult.UnresponsivePeers {
t.Logf(" %s: %s", peerID[:12], errMsg)
}
}
} else {
logger.Infof("Head convergence achieved in %v", headResult.Duration)
}
//
// Verify all peers have identical commit history
//
logger.Info("Verifying commit history convergence")
historyResult, err := waitForCommitHistoryConvergence(ctx, clients, defaultConvergenceTimeout, defaultPollInterval)
if err != nil {
t.Fatalf("error while waiting for history convergence: %v", err)
}
if !historyResult.Converged {
t.Error("commit histories do not match across peers")
t.Logf("Responsive peers (%d):", len(historyResult.AllCommitLists))
for peerID, commits := range historyResult.AllCommitLists {
t.Logf(" %s (%s): %d commits", instanceIDs[peerID], peerID[:12], len(commits))
}
if len(historyResult.UnresponsivePeers) > 0 {
t.Logf("Unresponsive peers (%d):", len(historyResult.UnresponsivePeers))
for peerID, errMsg := range historyResult.UnresponsivePeers {
t.Logf(" %s: %s", peerID[:12], errMsg)
}
}
} else {
logger.Infof("Commit history convergence achieved in %v", historyResult.Duration)
// Log the final commit count
for _, commits := range historyResult.AllCommitLists {
logger.Infof("All peers have %d commits", len(commits))
break
}
}
}
//
// testSetup contains the common test setup
//
type testSetup struct {
testDir string
instances []*controller
instanceIDs map[string]string
clients []*p2p.P2PClient
p2pMgr *p2p.P2P
cleanup func()
}
// setupTestEnvironment creates the test environment with initialized instances
func setupTestEnvironment(t *testing.T, numInstances int) *testSetup {
testDir, err := os.MkdirTemp("temp", "tst")
if err != nil {
t.Fatal(err)
}
err = doInit(testDir, numInstances)
if err != nil {
os.RemoveAll(testDir)
t.Fatal(err)
}
instances := make([]*controller, numInstances)
for i := 1; i <= numInstances; i++ {
instances[i-1] = runInstance(testDir, i, server, "", enableProcessOutput)
time.Sleep(500 * time.Millisecond)
}
instanceIDs := make(map[string]string, numInstances)
for _, instance := range instances {
hostID := <-instance.hostID
instanceIDs[hostID] = instance.Name()
}
time.Sleep(5 * time.Second)
peerListChan := make(chan peer.IDSlice, 100)
tDB := &testDB{}
p2pkey, err := p2p.NewKey(testDir + "/testp2p")
if err != nil {
stopAllInstances(instances, t)
os.RemoveAll(testDir)
t.Fatal(err)
}
p2pMgr, err := p2p.NewManager(p2pkey, startPort, peerListChan, logger, tDB)
if err != nil {
stopAllInstances(instances, t)
os.RemoveAll(testDir)
t.Fatal(err)