-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm_test.go
458 lines (392 loc) · 11.7 KB
/
swarm_test.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
package main
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"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"
)
//
// TODO:
// - test that commits are propagated
// - ability to wait for commits to be propagated (eventually with a timeout)
// - test conflic 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
)
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(query string, commitMsg string) (string, error) {
return "", nil
}
func (pr *testDB) GetLastCommit(branch string) (doltswarm.Commit, error) {
return doltswarm.Commit{}, nil
}
//
// 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)
}
for len(p2pmgr.GetClients()) != nrOfInstances {
logger.Infof("Waiting for %d clients. Currently have %d", nrOfInstances, len(p2pmgr.GetClients()))
time.Sleep(2 * time.Second)
}
clients := p2pmgr.GetClients()
for _, client := range clients {
_, err = client.Ping(context.Background(), &p2pproto.PingRequest{
Ping: "pong",
})
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(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)
}
// peersWithCommits := map[string]bool{}
// timeStart := time.Now()
// for i := 0; i < 500; i++ {
// if len(peersWithCommits) == len(clients) {
// break
// }
// for _, client := range clients {
// if !srvSyncer.peerHasCommit(client.GetID(), resp.Commit) {
// continue
// } else {
// peersWithCommits[client.GetID()] = true
// }
// }
// time.Sleep(50 * time.Millisecond)
// }
// for k, v := range srvSyncer.peerCommits.Snapshot() {
// logger.Infof("Commits for %s: %v", k, v)
// }
// if len(peersWithCommits) != len(clients) {
// t.Fatalf("commit not propagated to all peers. Only the following peers had all the commits: %v", peersWithCommits)
// } else {
// logger.Infof("It took %f seconds to propagate to %d clients", time.Since(timeStart).Seconds(), len(clients))
// }
logger.Info("Waiting for all peers to finish communication")
time.Sleep(30 * time.Second)
}