Skip to content

Commit 2468196

Browse files
authored
Merge pull request #2373 from threefoldtech/add-failover-graphql
Add multiple graphql endpoints
2 parents 172a0a2 + ff49e33 commit 2468196

File tree

5 files changed

+55
-20
lines changed

5 files changed

+55
-20
lines changed

pkg/environment/environment.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Environment struct {
4949
// PLEASE MAKE SURE THAT ANY ENV HAS NO MORE THAN FOUR RELAYS CONFIGURED
5050
RelayURL []string
5151
ActivationURL string
52-
GraphQL string
52+
GraphQL []string
5353

5454
// private vlan to join
5555
// if set, zos will use this as its priv vlan
@@ -116,7 +116,10 @@ var (
116116
ActivationURL: "https://activation.dev.grid.tf/activation/activate",
117117
FlistURL: "redis://hub.grid.tf:9900",
118118
BinRepo: "tf-zos-v3-bins.dev",
119-
GraphQL: "https://graphql.dev.grid.tf/graphql",
119+
GraphQL: []string{
120+
"https://graphql.dev.grid.tf/graphql",
121+
"https://graphql.02.dev.grid.tf/graphql",
122+
},
120123
}
121124

122125
envTest = Environment{
@@ -131,7 +134,10 @@ var (
131134
ActivationURL: "https://activation.test.grid.tf/activation/activate",
132135
FlistURL: "redis://hub.grid.tf:9900",
133136
BinRepo: "tf-zos-v3-bins.test",
134-
GraphQL: "https://graphql.test.grid.tf/graphql",
137+
GraphQL: []string{
138+
"https://graphql.test.grid.tf/graphql",
139+
"https://graphql.02.test.grid.tf/graphql",
140+
},
135141
}
136142

137143
envQA = Environment{
@@ -146,7 +152,10 @@ var (
146152
ActivationURL: "https://activation.qa.grid.tf/activation/activate",
147153
FlistURL: "redis://hub.grid.tf:9900",
148154
BinRepo: "tf-zos-v3-bins.qanet",
149-
GraphQL: "https://graphql.qa.grid.tf/graphql",
155+
GraphQL: []string{
156+
"https://graphql.qa.grid.tf/graphql",
157+
"https://graphql.02.qa.grid.tf/graphql",
158+
},
150159
}
151160

152161
envProd = Environment{
@@ -164,7 +173,10 @@ var (
164173
ActivationURL: "https://activation.grid.tf/activation/activate",
165174
FlistURL: "redis://hub.grid.tf:9900",
166175
BinRepo: "tf-zos-v3-bins",
167-
GraphQL: "https://graphql.grid.tf/graphql",
176+
GraphQL: []string{
177+
"https://graphql.grid.tf/graphql",
178+
"https://graphql.02.grid.tf/graphql",
179+
},
168180
}
169181
)
170182

pkg/perf/graphql/graphql_nodes.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math/rand"
78
"time"
@@ -16,13 +17,15 @@ const (
1617

1718
// GraphQl for tf graphql client
1819
type GraphQl struct {
19-
client *graphql.Client
20+
urls []string
2021
}
2122

2223
// NewGraphQl creates a new tf graphql client
23-
func NewGraphQl(url string) GraphQl {
24-
client := graphql.NewClient(url, nil)
25-
return GraphQl{client: client}
24+
func NewGraphQl(urls ...string) (GraphQl, error) {
25+
if len(urls) == 0 {
26+
return GraphQl{}, errors.New("urls can't be empty")
27+
}
28+
return GraphQl{urls: urls}, nil
2629
}
2730

2831
// Node from graphql
@@ -85,7 +88,7 @@ func (g *GraphQl) GetUpNodes(ctx context.Context, nodesNum int, farmID, excludeF
8588
Nodes []Node
8689
}{}
8790

88-
if err := g.client.Exec(ctx, query, &res, nil); err != nil {
91+
if err := g.exec(ctx, query, &res, nil); err != nil {
8992
return []Node{}, err
9093
}
9194

@@ -101,9 +104,20 @@ func (g *GraphQl) getItemTotalCount(ctx context.Context, itemName string, option
101104
}
102105
}{}
103106

104-
if err := g.client.Exec(ctx, query, &res, nil); err != nil {
107+
if err := g.exec(ctx, query, &res, nil); err != nil {
105108
return 0, err
106109
}
107110

108111
return res.Items.Count, nil
109112
}
113+
114+
// exec is a wrapper around graphql.Client.Exec to retry another endpoints in case some are down.
115+
func (g *GraphQl) exec(ctx context.Context, query string, result interface{}, variables map[string]interface{}, options ...graphql.Option) (err error) {
116+
for _, url := range g.urls {
117+
client := graphql.NewClient(url, nil)
118+
if err = client.Exec(ctx, query, result, variables, options...); err == nil {
119+
return
120+
}
121+
}
122+
return
123+
}

pkg/perf/healthcheck/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const defaultRequestTimeout = 5 * time.Second
1818
func networkCheck(ctx context.Context) []error {
1919
env := environment.MustGet()
2020
servicesUrl := []string{
21-
env.ActivationURL, env.GraphQL, env.FlistURL,
21+
env.ActivationURL, env.FlistURL,
2222
}
2323
servicesUrl = append(append(servicesUrl, env.SubstrateURL...), env.RelayURL...)
24+
servicesUrl = append(servicesUrl, env.GraphQL...)
2425

2526
var errors []error
2627

pkg/perf/iperf/iperf_task.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ func (t *IperfTest) Jitter() uint32 {
7676
// Run runs the tcp test and returns the result
7777
func (t *IperfTest) Run(ctx context.Context) (interface{}, error) {
7878
env := environment.MustGet()
79-
g := graphql.NewGraphQl(env.GraphQL)
79+
g, err := graphql.NewGraphQl(env.GraphQL...)
80+
if err != nil {
81+
return nil, err
82+
}
8083

8184
// get public up nodes
8285
freeFarmNodes, err := g.GetUpNodes(ctx, 0, 1, 0, true, true)

pkg/perf/publicip/publicip_task.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ const (
3434
FetchRealIPFailed = "failed to get real public IP to the node"
3535
)
3636

37-
var errPublicIPLookup = errors.New("failed to reach public ip service")
38-
var errSkippedValidating = errors.New("skipped, there is a node with less ID available")
37+
var (
38+
errPublicIPLookup = errors.New("failed to reach public ip service")
39+
errSkippedValidating = errors.New("skipped, there is a node with less ID available")
40+
)
3941

40-
const testMacvlan = "pub"
41-
const testNamespace = "pubtestns"
42+
const (
43+
testMacvlan = "pub"
44+
testNamespace = "pubtestns"
45+
)
4246

4347
type publicIPValidationTask struct{}
4448

@@ -96,7 +100,6 @@ func (p *publicIPValidationTask) Run(ctx context.Context) (interface{}, error) {
96100
report, err = p.validateIPs(farm.PublicIPs)
97101
return err
98102
})
99-
100103
if err != nil {
101104
return nil, fmt.Errorf("failed to run public IP validation: %w", err)
102105
}
@@ -179,7 +182,10 @@ func (p *publicIPValidationTask) validateIPs(publicIPs []substrate.PublicIP) (ma
179182

180183
func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stubs.SubstrateGatewayStub) (bool, error) {
181184
env := environment.MustGet()
182-
gql := graphql.NewGraphQl(env.GraphQL)
185+
gql, err := graphql.NewGraphQl(env.GraphQL...)
186+
if err != nil {
187+
return false, err
188+
}
183189

184190
nodes, err := gql.GetUpNodes(ctx, 0, farmID, 0, false, false)
185191
if err != nil {
@@ -196,7 +202,6 @@ func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stub
196202
}
197203
return nil
198204
}, backoff.NewConstantBackOff(10*time.Second))
199-
200205
if err != nil {
201206
return false, fmt.Errorf("failed to get node id: %w", err)
202207
}

0 commit comments

Comments
 (0)