-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathlocal_cluster.go
1230 lines (1079 loc) · 33.2 KB
/
local_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
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
/*
* Copyright 2023 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dgraphtest
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
docker "github.com/docker/docker/client"
"github.com/golang-jwt/jwt/v5"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
"github.com/dgraph-io/dgraph/dgraphapi"
"github.com/dgraph-io/dgraph/x"
)
// cluster's network struct
type cnet struct {
id string
name string
}
// LocalCluster is a local dgraph cluster
type LocalCluster struct {
conf ClusterConfig
tempBinDir string
tempSecretsDir string
encKeyPath string
lowerThanV21 bool
customTokenizers string
// resources
dcli *docker.Client
net cnet
zeros []*zero
alphas []*alpha
}
// UpgradeStrategy is an Enum that defines various upgrade strategies
type UpgradeStrategy int
const (
BackupRestore UpgradeStrategy = iota
ExportImport
InPlace
)
func (u UpgradeStrategy) String() string {
switch u {
case BackupRestore:
return "backup-restore"
case InPlace:
return "in-place"
case ExportImport:
return "export-import"
default:
panic("unknown upgrade strategy")
}
}
// NewLocalCluster creates a new local dgraph cluster with given configuration
func NewLocalCluster(conf ClusterConfig) (*LocalCluster, error) {
c := &LocalCluster{conf: conf}
if err := c.init(); err != nil {
c.Cleanup(true)
return nil, err
}
return c, nil
}
// init performs the one time setup and sets up the cluster.
func (c *LocalCluster) init() error {
var err error
c.dcli, err = docker.NewClientWithOpts(docker.FromEnv, docker.WithAPIVersionNegotiation())
if err != nil {
return errors.Wrap(err, "error setting up docker client")
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
if _, err := c.dcli.Ping(ctx); err != nil {
return errors.Wrap(err, "unable to talk to docker daemon")
}
if err := c.createNetwork(); err != nil {
return err
}
c.tempBinDir, err = os.MkdirTemp("", c.conf.prefix)
if err != nil {
return errors.Wrap(err, "error while creating tempBinDir")
}
log.Printf("[INFO] tempBinDir: %v", c.tempBinDir)
c.tempSecretsDir, err = os.MkdirTemp("", c.conf.prefix)
if err != nil {
return errors.Wrap(err, "error while creating tempSecretsDir")
}
log.Printf("[INFO] tempSecretsDir: %v", c.tempSecretsDir)
if err := os.Mkdir(binariesPath, os.ModePerm); err != nil && !os.IsExist(err) {
return errors.Wrap(err, "error while making binariesPath")
}
for _, vol := range c.conf.volumes {
if err := c.createVolume(vol); err != nil {
return err
}
}
c.zeros = c.zeros[:0]
for i := 0; i < c.conf.numZeros; i++ {
zo := &zero{id: i}
zo.containerName = fmt.Sprintf(zeroNameFmt, c.conf.prefix, zo.id)
zo.aliasName = fmt.Sprintf(zeroAliasNameFmt, zo.id)
c.zeros = append(c.zeros, zo)
}
c.alphas = c.alphas[:0]
for i := 0; i < c.conf.numAlphas; i++ {
aa := &alpha{id: i}
aa.containerName = fmt.Sprintf(alphaNameFmt, c.conf.prefix, aa.id)
aa.aliasName = fmt.Sprintf(alphaLNameFmt, aa.id)
c.alphas = append(c.alphas, aa)
}
if err := c.setupSecrets(); err != nil {
return errors.Wrap(err, "error setting up secrets")
}
if err := c.setupBeforeCluster(); err != nil {
return err
}
if err := c.createContainers(); err != nil {
return err
}
return nil
}
func (c *LocalCluster) createNetwork() error {
c.net.name = c.conf.prefix + "-net"
opts := types.NetworkCreate{
Driver: "bridge",
IPAM: &network.IPAM{Driver: "default"},
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
network, err := c.dcli.NetworkCreate(ctx, c.net.name, opts)
if err != nil {
return errors.Wrap(err, "error creating network")
}
c.net.id = network.ID
return nil
}
func (c *LocalCluster) createVolume(name string) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
req := volume.CreateOptions{Driver: "local", Name: name}
if _, err := c.dcli.VolumeCreate(ctx, req); err != nil {
return errors.Wrapf(err, "error creating volume [%v]", name)
}
return nil
}
func (c *LocalCluster) setupBeforeCluster() error {
if err := c.setupBinary(); err != nil {
return errors.Wrapf(err, "error setting up binary")
}
higher, err := IsHigherVersion(c.GetVersion(), "v21.03.0")
if err != nil {
return errors.Wrapf(err, "error checking if version %s is older than v21.03.0", c.GetVersion())
}
c.lowerThanV21 = !higher
return nil
}
func (c *LocalCluster) createContainers() error {
for _, zo := range c.zeros {
cid, err := c.createContainer(zo)
if err != nil {
return err
}
zo.containerID = cid
}
for _, aa := range c.alphas {
cid, err := c.createContainer(aa)
if err != nil {
return err
}
aa.containerID = cid
}
return nil
}
func (c *LocalCluster) createContainer(dc dnode) (string, error) {
cmd := dc.cmd(c)
image := c.dgraphImage()
mts, err := dc.mounts(c)
if err != nil {
return "", err
}
cconf := &container.Config{Cmd: cmd, Image: image, WorkingDir: dc.workingDir(), ExposedPorts: dc.ports()}
hconf := &container.HostConfig{Mounts: mts, PublishAllPorts: true, PortBindings: dc.bindings(c.conf.portOffset)}
networkConfig := &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
c.net.name: {
Aliases: []string{dc.cname(), dc.aname()},
NetworkID: c.net.id,
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
resp, err := c.dcli.ContainerCreate(ctx, cconf, hconf, networkConfig, nil, dc.cname())
if err != nil {
return "", errors.Wrapf(err, "error creating container %v", dc.cname())
}
return resp.ID, nil
}
func (c *LocalCluster) destroyContainers() error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
ro := types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}
for _, zo := range c.zeros {
if err := c.dcli.ContainerRemove(ctx, zo.cid(), ro); err != nil {
return errors.Wrapf(err, "error removing zero [%v]", zo.cname())
}
}
for _, aa := range c.alphas {
if err := c.dcli.ContainerRemove(ctx, aa.cid(), ro); err != nil {
return errors.Wrapf(err, "error removing alpha [%v]", aa.cname())
}
}
return nil
}
// CheckRunningServices checks open ports using lsof and returns the output as a string
func CheckRunningServices() (string, error) {
lsofCmd := exec.Command("lsof", "-i", "-n")
output, err := runCommand(lsofCmd)
if err != nil {
return "", fmt.Errorf("error running lsof command: %v", err)
}
return output, nil
}
// ListRunningContainers lists running Docker containers using the Docker Go client
func (c *LocalCluster) listRunningContainers() (string, error) {
containers, err := c.dcli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return "", fmt.Errorf("error listing Docker containers: %v", err)
}
var result bytes.Buffer
for _, container := range containers {
result.WriteString(fmt.Sprintf("ID: %s, Image: %s, Command: %s, Status: %s\n",
container.ID[:10], container.Image, container.Command, container.Status))
result.WriteString("Port Mappings:\n")
for _, port := range container.Ports {
result.WriteString(fmt.Sprintf(" %s:%d -> %d\n", port.IP, port.PublicPort, port.PrivatePort))
}
result.WriteString("\n")
result.WriteString("Port Mappings:\n")
info, err := c.dcli.ContainerInspect(context.Background(), container.ID)
if err != nil {
return "", errors.Wrap(err, "error inspecting container")
}
for port, bindings := range info.NetworkSettings.Ports {
if len(bindings) == 0 {
continue
}
result.WriteString(fmt.Sprintf(" %s:%s\n", port.Port(), bindings))
}
result.WriteString("\n")
}
return result.String(), nil
}
// runCommand executes a command and returns its output or an error
func runCommand(cmd *exec.Cmd) (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("%v: %v", err, stderr.String())
}
return out.String(), nil
}
func (c *LocalCluster) printNetworkStuff() {
log.Printf("Checking running services and ports using lsof, netstat, and Docker...\n")
// Check running services using lsof
lsofOutput, err := CheckRunningServices()
if err != nil {
fmt.Printf("Error checking running services: %v\n", err)
} else {
log.Printf("Output of lsof -i:")
log.Println(lsofOutput)
}
// List running Docker containers
dockerOutput, err := c.listRunningContainers()
if err != nil {
fmt.Printf("Error listing Docker containers: %v\n", err)
} else {
log.Printf("Running Docker containers:")
log.Println(dockerOutput)
}
}
func (c *LocalCluster) Cleanup(verbose bool) {
if c == nil {
return
}
if verbose {
if err := c.printAllLogs(); err != nil {
log.Printf("[WARNING] error printing container logs: %v", err)
}
if err := c.printInspectContainers(); err != nil {
log.Printf("[WARNING] error printing inspect container output: %v", err)
}
}
log.Printf("[INFO] cleaning up cluster with prefix [%v]", c.conf.prefix)
if err := c.destroyContainers(); err != nil {
log.Printf("[WARNING] error removing container: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
for _, vol := range c.conf.volumes {
if err := c.dcli.VolumeRemove(ctx, vol, true); err != nil {
log.Printf("[WARNING] error removing volume [%v]: %v", vol, err)
}
}
if c.net.id != "" {
if err := c.dcli.NetworkRemove(ctx, c.net.id); err != nil {
log.Printf("[WARNING] error removing network [%v]: %v", c.net.name, err)
}
}
if err := os.RemoveAll(c.tempBinDir); err != nil {
log.Printf("[WARNING] error while removing temp bin dir: %v", err)
}
if err := os.RemoveAll(c.tempSecretsDir); err != nil {
log.Printf("[WARNING] error while removing temp secrets dir: %v", err)
}
}
func (c *LocalCluster) cleanupDocker() error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
// Prune containers
contsReport, err := c.dcli.ContainersPrune(ctx, filters.Args{})
if err != nil {
log.Fatalf("[ERROR] Error pruning containers: %v", err)
}
log.Printf("[INFO] Pruned containers: %+v\n", contsReport)
// Prune networks
netsReport, err := c.dcli.NetworksPrune(ctx, filters.Args{})
if err != nil {
log.Fatalf("[ERROR] Error pruning networks: %v", err)
}
log.Printf("[INFO] Pruned networks: %+v\n", netsReport)
return nil
}
func (c *LocalCluster) Start() error {
log.Printf("[INFO] starting cluster with prefix [%v]", c.conf.prefix)
startAll := func() error {
for i := 0; i < c.conf.numZeros; i++ {
if err := c.StartZero(i); err != nil {
return err
}
}
for i := 0; i < c.conf.numAlphas; i++ {
if err := c.StartAlpha(i); err != nil {
return err
}
}
return c.HealthCheck(false)
}
// sometimes health check doesn't work due to unmapped ports. We dont
// know why this happens, but checking it 3 times before failing the test.
retry := 0
for {
retry++
if err := startAll(); err == nil {
return nil
} else if retry == 3 {
return err
} else {
log.Printf("[WARNING] saw the err, trying again: %v", err)
}
if err1 := c.Stop(); err1 != nil {
log.Printf("[WARNING] error while stopping :%v", err1)
}
c.Cleanup(true)
if err := c.cleanupDocker(); err != nil {
log.Printf("[ERROR] while cleaning old dockers %v", err)
}
c.conf.prefix = fmt.Sprintf("dgraphtest-%d", rand.NewSource(time.Now().UnixNano()).Int63()%1000000)
if err := c.init(); err != nil {
log.Printf("[ERROR] error while init, returning: %v", err)
return err
}
}
}
func (c *LocalCluster) StartZero(id int) error {
if id >= c.conf.numZeros {
return fmt.Errorf("invalid id of zero: %v", id)
}
return c.startContainer(c.zeros[id])
}
func (c *LocalCluster) StartAlpha(id int) error {
if id >= c.conf.numAlphas {
return fmt.Errorf("invalid id of alpha: %v", id)
}
return c.startContainer(c.alphas[id])
}
func (c *LocalCluster) startContainer(dc dnode) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
if err := c.dcli.ContainerStart(ctx, dc.cid(), types.ContainerStartOptions{}); err != nil {
return errors.Wrapf(err, "error starting container [%v]", dc.cname())
}
dc.changeStatus(true)
return nil
}
func (c *LocalCluster) Stop() error {
log.Printf("[INFO] stopping cluster with prefix [%v]", c.conf.prefix)
for i := range c.alphas {
if err := c.StopAlpha(i); err != nil {
return err
}
}
for i := range c.zeros {
if err := c.StopZero(i); err != nil {
return err
}
}
return nil
}
func (c *LocalCluster) StopZero(id int) error {
if id >= c.conf.numZeros {
return fmt.Errorf("invalid id of zero: %v", id)
}
return c.stopContainer(c.zeros[id])
}
func (c *LocalCluster) StopAlpha(id int) error {
if id >= c.conf.numAlphas {
return fmt.Errorf("invalid id of alpha: %v", id)
}
return c.stopContainer(c.alphas[id])
}
func (c *LocalCluster) stopContainer(dc dnode) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
stopTimeout := 30 // in seconds
o := container.StopOptions{Timeout: &stopTimeout}
if err := c.dcli.ContainerStop(ctx, dc.cid(), o); err != nil {
// Force kill the container if timeout exceeded
if strings.Contains(err.Error(), "context deadline exceeded") {
_ = c.dcli.ContainerKill(ctx, dc.cid(), "KILL")
return nil
}
return errors.Wrapf(err, "error stopping container [%v]", dc.cname())
}
dc.changeStatus(false)
return nil
}
func (c *LocalCluster) KillAlpha(id int) error {
if id >= c.conf.numAlphas {
return fmt.Errorf("invalid id of alpha: %v", id)
}
return c.killContainer(c.alphas[id])
}
func (c *LocalCluster) killContainer(dc dnode) error {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
if err := c.dcli.ContainerKill(ctx, dc.cid(), "SIGKILL"); err != nil {
return errors.Wrapf(err, "error killing container [%v]", dc.cname())
}
return nil
}
func (c *LocalCluster) HealthCheck(zeroOnly bool) error {
log.Printf("[INFO] checking health of containers")
for _, zo := range c.zeros {
if !zo.isRunning {
break
}
if err := c.containerHealthCheck(zo.healthURL); err != nil {
return err
}
log.Printf("[INFO] container [%v] passed health check", zo.containerName)
if err := c.checkDgraphVersion(zo.containerName); err != nil {
return err
}
}
if zeroOnly {
return nil
}
for _, aa := range c.alphas {
if !aa.isRunning {
break
}
if err := c.containerHealthCheck(aa.healthURL); err != nil {
return err
}
log.Printf("[INFO] container [%v] passed health check", aa.containerName)
if err := c.checkDgraphVersion(aa.containerName); err != nil {
return err
}
}
return nil
}
func (c *LocalCluster) containerHealthCheck(url func(c *LocalCluster) (string, error)) error {
endpoint, err := url(c)
if err != nil {
return errors.Wrap(err, "error getting health URL")
}
for i := 0; i < 60; i++ {
time.Sleep(waitDurBeforeRetry)
endpoint, err = url(c)
if err != nil {
return errors.Wrap(err, "error getting health URL")
}
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
log.Printf("[WARNING] error building req for endpoint [%v], err: [%v]", endpoint, err)
continue
}
body, err := dgraphapi.DoReq(req)
if err != nil {
log.Printf("[WARNING] error hitting health endpoint [%v], err: [%v]", endpoint, err)
continue
}
resp := string(body)
// zero returns OK in the health check
if resp == "OK" {
return nil
}
// For Alpha, we always run alpha with EE features enabled
if !strings.Contains(resp, `"ee_features"`) {
continue
}
if c.conf.acl && !strings.Contains(resp, `"acl"`) {
continue
}
if err := c.waitUntilLogin(); err != nil {
return err
}
if err := c.waitUntilGraphqlHealthCheck(); err != nil {
return err
}
return nil
}
c.printNetworkStuff()
return fmt.Errorf("health failed, cluster took too long to come up [%v]", endpoint)
}
func (c *LocalCluster) waitUntilLogin() error {
if !c.conf.acl {
return nil
}
client, cleanup, err := c.Client()
if err != nil {
return errors.Wrap(err, "error setting up a client")
}
defer cleanup()
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
for i := 0; i < 10; i++ {
err := client.Login(ctx, dgraphapi.DefaultUser, dgraphapi.DefaultPassword)
if err == nil {
log.Printf("[INFO] login succeeded")
return nil
}
log.Printf("[WARNING] error trying to login: %v", err)
time.Sleep(waitDurBeforeRetry)
}
return errors.New("error during login")
}
func (c *LocalCluster) waitUntilGraphqlHealthCheck() error {
hc, err := c.HTTPClient()
if err != nil {
return errors.Wrap(err, "error creating http client while graphql health check")
}
if c.conf.acl {
if err := hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace); err != nil {
return errors.Wrap(err, "error during login while graphql health check")
}
}
for i := 0; i < 10; i++ {
// we do this because before v21, we used to propose the initial schema to the cluster.
// This results in schema being applied and indexes being built which could delay alpha
// starting to serve graphql schema.
err := hc.DeleteUser("nonexistent")
if err == nil {
log.Printf("[INFO] graphql health check succeeded")
return nil
} else if strings.Contains(err.Error(), "this indicates a resolver or validation bug") {
time.Sleep(waitDurBeforeRetry)
continue
} else {
return errors.Wrapf(err, "error during graphql health check")
}
}
return errors.New("error during graphql health check")
}
// Upgrades the cluster to the provided dgraph version
func (c *LocalCluster) Upgrade(version string, strategy UpgradeStrategy) error {
if version == c.conf.version {
return fmt.Errorf("cannot upgrade to the same version")
}
log.Printf("[INFO] upgrading the cluster from [%v] to [%v] using [%v]", c.conf.version, version, strategy)
switch strategy {
case BackupRestore:
hc, err := c.HTTPClient()
if err != nil {
return err
}
if c.conf.acl {
if err := hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace); err != nil {
return errors.Wrapf(err, "error during login before upgrade")
}
}
if err := hc.Backup(c, true, DefaultBackupDir); err != nil {
return errors.Wrap(err, "error taking backup during upgrade")
}
if err := c.Stop(); err != nil {
return err
}
c.conf.version = version
if err := c.recreateContainers(); err != nil {
return err
}
if err := c.Start(); err != nil {
return err
}
hc, err = c.HTTPClient()
if err != nil {
return errors.Wrapf(err, "error creating HTTP client after upgrade")
}
if c.conf.acl {
if err := hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace); err != nil {
return errors.Wrapf(err, "error during login after upgrade")
}
}
if err := hc.Restore(c, DefaultBackupDir, "", 0, 1); err != nil {
return errors.Wrap(err, "error doing restore during upgrade")
}
if err := dgraphapi.WaitForRestore(c); err != nil {
return errors.Wrap(err, "error waiting for restore to complete")
}
return nil
case ExportImport:
hc, err := c.HTTPClient()
if err != nil {
return err
}
if c.conf.acl {
if err := hc.LoginIntoNamespace(dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace); err != nil {
return errors.Wrapf(err, "error during login before upgrade")
}
}
// using -1 as namespace exports all the namespaces
if err := hc.Export(DefaultExportDir, "rdf", -1); err != nil {
return errors.Wrap(err, "error taking export during upgrade")
}
if err := c.Stop(); err != nil {
return err
}
c.conf.version = version
if err := c.recreateContainers(); err != nil {
return err
}
if err := c.Start(); err != nil {
return err
}
if err := c.LiveLoadFromExport(DefaultExportDir); err != nil {
return errors.Wrap(err, "error doing import using live loader")
}
return nil
case InPlace:
if err := c.Stop(); err != nil {
return err
}
c.conf.version = version
if err := c.setupBeforeCluster(); err != nil {
return err
}
return c.Start()
default:
return errors.New("unknown upgrade strategy")
}
}
func (c *LocalCluster) recreateContainers() error {
if err := c.destroyContainers(); err != nil {
return errors.Wrapf(err, "error while recreaing containers")
}
if err := c.setupBeforeCluster(); err != nil {
return errors.Wrap(err, "error while setupBeforeCluster")
}
if err := c.createContainers(); err != nil {
return errors.Wrapf(err, "error while creating containers")
}
return nil
}
// Client returns a grpc client that can talk to any Alpha in the cluster
func (c *LocalCluster) Client() (*dgraphapi.GrpcClient, func(), error) {
// TODO(aman): can we cache the connections?
var apiClients []api.DgraphClient
var conns []*grpc.ClientConn
for _, aa := range c.alphas {
if !aa.isRunning {
break
}
url, err := aa.alphaURL(c)
if err != nil {
return nil, nil, errors.Wrap(err, "error getting health URL")
}
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, nil, errors.Wrap(err, "error connecting to alpha")
}
conns = append(conns, conn)
apiClients = append(apiClients, api.NewDgraphClient(conn))
}
client := dgo.NewDgraphClient(apiClients...)
cleanup := func() {
for _, conn := range conns {
if err := conn.Close(); err != nil {
log.Printf("[WARNING] error closing connection: %v", err)
}
}
}
return &dgraphapi.GrpcClient{Dgraph: client}, cleanup, nil
}
func (c *LocalCluster) AlphaClient(id int) (*dgraphapi.GrpcClient, func(), error) {
alpha := c.alphas[id]
url, err := alpha.alphaURL(c)
if err != nil {
return nil, nil, errors.Wrap(err, "error getting health URL")
}
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, nil, errors.Wrap(err, "error connecting to alpha")
}
client := dgo.NewDgraphClient(api.NewDgraphClient(conn))
cleanup := func() {
if err := conn.Close(); err != nil {
log.Printf("[WARNING] error closing connection: %v", err)
}
}
return &dgraphapi.GrpcClient{Dgraph: client}, cleanup, nil
}
// HTTPClient creates an HTTP client
func (c *LocalCluster) HTTPClient() (*dgraphapi.HTTPClient, error) {
alphaUrl, err := c.serverURL("alpha", "")
if err != nil {
return nil, err
}
zeroUrl, err := c.serverURL("zero", "")
if err != nil {
return nil, err
}
return dgraphapi.GetHttpClient(alphaUrl, zeroUrl)
}
// serverURL returns url to the 'server' 'endpoint'
func (c *LocalCluster) serverURL(server, endpoint string) (string, error) {
pubPort, err := publicPort(c.dcli, c.alphas[0], alphaHttpPort)
if server == "zero" {
pubPort, err = publicPort(c.dcli, c.zeros[0], zeroHttpPort)
}
if err != nil {
return "", err
}
url := "0.0.0.0:" + pubPort + endpoint
return url, nil
}
// AlphasHealth returns response of health endpoint for all alphas
func (c *LocalCluster) AlphasHealth() ([]string, error) {
if len(c.alphas) == 0 {
return nil, fmt.Errorf("alpha not running")
}
healths := make([]string, 0, c.conf.numAlphas)
for _, a := range c.alphas {
url, err := a.healthURL(c)
if err != nil {
return nil, errors.Wrap(err, "error getting health URL")
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrapf(err, "error building req for endpoint [%v]", url)
}
h, err := dgraphapi.DoReq(req)
if err != nil {
return nil, errors.Wrap(err, "error getting health")
}
healths = append(healths, string(h))
}
return healths, nil
}
// AlphasLogs returns logs of all the alpha containers
func (c *LocalCluster) AlphasLogs() ([]string, error) {
alphasLogs := make([]string, 0, len(c.alphas))
for _, aa := range c.alphas {
alphaLogs, err := c.getLogs(aa.containerID)
if err != nil {
return nil, err
}
alphasLogs = append(alphasLogs, alphaLogs)
}
return alphasLogs, nil
}
// AssignUids talks to zero to assign the given number of uids
func (c *LocalCluster) AssignUids(_ *dgo.Dgraph, num uint64) error {
if len(c.zeros) == 0 {
return errors.New("no zero running")
}
baseURL, err := c.zeros[0].assignURL(c)
if err != nil {
return err
}
url := fmt.Sprintf("%v?what=uids&num=%d", baseURL, num)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return errors.Wrapf(err, "error building req for endpoint [%v]", url)
}
body, err := dgraphapi.DoReq(req)
if err != nil {
return err
}
var data struct {
Errors []struct {
Message string
Code string
}
}
if err := json.Unmarshal(body, &data); err != nil {
return errors.Wrap(err, "error unmarshaling response")
}
if len(data.Errors) > 0 {
return fmt.Errorf("error received from zero: %v", data.Errors[0].Message)
}
return nil
}
// GetVersion returns the version of dgraph the cluster is running
func (c *LocalCluster) GetVersion() string {
return c.conf.version
}
// GetRepoDir returns the repositroty directory of the cluster
func (c *LocalCluster) GetRepoDir() (string, error) {
return c.conf.repoDir, nil
}
// GetEncKeyPath returns the path to the encryption key file when encryption is enabled.
// It returns an empty string otherwise. The path to the encryption file is valid only
// inside the alpha container.
func (c *LocalCluster) GetEncKeyPath() (string, error) {
if c.conf.encryption {
return encKeyMountPath, nil
}
return "", nil
}
func (c *LocalCluster) printAllLogs() error {
log.Printf("[INFO] all logs for cluster with prefix [%v] are below!", c.conf.prefix)
var finalErr error
for _, zo := range c.zeros {
if err := c.printLogs(zo.containerName); err != nil {
finalErr = fmt.Errorf("%v; %v", finalErr, err)
}
}
for _, aa := range c.alphas {
if err := c.printLogs(aa.containerName); err != nil {
finalErr = fmt.Errorf("%v; %v", finalErr, err)
}
}
return finalErr