Skip to content

Commit 8430395

Browse files
authored
Merge pull request #22 from threefoldtech/add-failover-graphql
add all available endpoints to zos environment
2 parents 85a7d94 + 4143820 commit 8430395

File tree

10 files changed

+92
-42
lines changed

10 files changed

+92
-42
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/shirou/gopsutil v3.21.11+incompatible
4646
github.com/stretchr/testify v1.9.0
4747
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56
48-
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240416134707-eb36aa90df2d
48+
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974
4949
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.15.2
5050
github.com/threefoldtech/zbus v1.0.1
5151
github.com/tyler-smith/go-bip39 v1.1.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ github.com/stripe/safesql v0.2.0/go.mod h1:q7b2n0JmzM1mVGfcYpanfVb2j23cXZeWFxcIL
511511
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
512512
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56 h1:uWd8JfE8N3IM6Zw/LMr0+sRJl+G0YgqQmNDP8xXR0vw=
513513
github.com/threefoldtech/0-fs v1.3.1-0.20240424140157-b488dfedcc56/go.mod h1:lZjR32SiNo3dP70inVFxaLMyZjmKX1ucS+5O31dbPNM=
514-
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240416134707-eb36aa90df2d h1:vWHR33VfphOahBlo3aGCE/eKPlfb4BCInhKidGkdp5s=
515-
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240416134707-eb36aa90df2d/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
514+
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974 h1:95Ox9WYNiwm3mVJeVF/lp7iCpVGA28AkU1atN893eDo=
515+
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
516516
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.15.2 h1:6NsugITjq90ErPqa7+JGTYpaduh+eInyIxfy+eBvbIs=
517517
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.15.2/go.mod h1:/Jf4SDtYO2cov3ZZQhyQqh+4o8UYYUrs+NDHWe/xUwY=
518518
github.com/threefoldtech/zbus v1.0.1 h1:3KaEpyOiDYAw+lrAyoQUGIvY9BcjVRXlQ1beBRqhRNk=

pkg/api_gateway.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type SubstrateGateway interface {
1111
CreateNode(node substrate.Node) (uint32, error)
1212
CreateTwin(relay string, pk []byte) (uint32, error)
13-
EnsureAccount(activationURL string, termsAndConditionsLink string, termsAndConditionsHash string) (info substrate.AccountInfo, err error)
13+
EnsureAccount(activationURL []string, termsAndConditionsLink string, termsAndConditionsHash string) (info substrate.AccountInfo, err error)
1414
GetContract(id uint64) (substrate.Contract, SubstrateError)
1515
GetContractIDByNameRegistration(name string) (uint64, SubstrateError)
1616
GetFarm(id uint32) (substrate.Farm, error)

pkg/environment/environment.go

+51-23
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type Environment struct {
4747
// SINCE RELAYS FOR A NODE IS STORED ON THE CHAIN IN A LIMITED SPACE
4848
// PLEASE MAKE SURE THAT ANY ENV HAS NO MORE THAN FOUR RELAYS CONFIGURED
4949
RelayURL []string
50-
ActivationURL string
51-
GraphQL string
50+
ActivationURL []string
51+
GraphQL []string
5252
KycURL string
5353

5454
// private vlan to join
@@ -108,32 +108,46 @@ var (
108108
RunningMode: RunningDev,
109109
SubstrateURL: []string{
110110
"wss://tfchain.dev.grid.tf/",
111+
"wss://tfchain.02.dev.grid.tf",
111112
},
112113
RelayURL: []string{
113114
"wss://relay.dev.grid.tf",
114115
"wss://relay.02.dev.grid.tf",
115116
},
116-
ActivationURL: "https://activation.dev.grid.tf/activation/activate",
117-
FlistURL: "redis://hub.grid.tf:9900",
118-
BinRepo: "tf-zos-v3-bins.dev",
119-
GraphQL: "https://graphql.dev.grid.tf/graphql",
120-
KycURL: "https://kyc.dev.grid.tf",
117+
ActivationURL: []string{
118+
"https://activation.dev.grid.tf/activation/activate",
119+
"https://activation.02.dev.grid.tf/activation/activate",
120+
},
121+
FlistURL: "redis://hub.grid.tf:9900",
122+
BinRepo: "tf-zos-v3-bins.dev",
123+
GraphQL: []string{
124+
"https://graphql.dev.grid.tf/graphql",
125+
"https://graphql.02.dev.grid.tf/graphql",
126+
},
127+
KycURL: "https://kyc.dev.grid.tf",
121128
}
122129

123130
envTest = Environment{
124131
RunningMode: RunningTest,
125132
SubstrateURL: []string{
126133
"wss://tfchain.test.grid.tf/",
134+
"wss://tfchain.02.test.grid.tf",
127135
},
128136
RelayURL: []string{
129137
"wss://relay.test.grid.tf",
130138
"wss://relay.02.test.grid.tf",
131139
},
132-
ActivationURL: "https://activation.test.grid.tf/activation/activate",
133-
FlistURL: "redis://hub.grid.tf:9900",
134-
BinRepo: "tf-zos-v3-bins.test",
135-
GraphQL: "https://graphql.test.grid.tf/graphql",
136-
KycURL: "https://kyc.test.grid.tf",
140+
ActivationURL: []string{
141+
"https://activation.test.grid.tf/activation/activate",
142+
"https://activation.02.test.grid.tf/activation/activate",
143+
},
144+
FlistURL: "redis://hub.grid.tf:9900",
145+
BinRepo: "tf-zos-v3-bins.test",
146+
GraphQL: []string{
147+
"https://graphql.test.grid.tf/graphql",
148+
"https://graphql.02.test.grid.tf/graphql",
149+
},
150+
KycURL: "https://kyc.test.grid.tf",
137151
}
138152

139153
envQA = Environment{
@@ -144,18 +158,26 @@ var (
144158
},
145159
RelayURL: []string{
146160
"wss://relay.qa.grid.tf",
161+
"wss://relay.02.qa.grid.tf",
147162
},
148-
ActivationURL: "https://activation.qa.grid.tf/activation/activate",
149-
FlistURL: "redis://hub.grid.tf:9900",
150-
BinRepo: "tf-zos-v3-bins.qanet",
151-
GraphQL: "https://graphql.qa.grid.tf/graphql",
152-
KycURL: "https://kyc.qa.grid.tf",
163+
ActivationURL: []string{
164+
"https://activation.qa.grid.tf/activation/activate",
165+
"https://activation.02.qa.grid.tf/activation/activate",
166+
},
167+
FlistURL: "redis://hub.grid.tf:9900",
168+
BinRepo: "tf-zos-v3-bins.qanet",
169+
GraphQL: []string{
170+
"https://graphql.qa.grid.tf/graphql",
171+
"https://graphql.02.qa.grid.tf/graphql",
172+
},
173+
KycURL: "https://kyc.qa.grid.tf",
153174
}
154175

155176
envProd = Environment{
156177
RunningMode: RunningMain,
157178
SubstrateURL: []string{
158179
"wss://tfchain.grid.tf/",
180+
"wss://tfchain.02.grid.tf",
159181
"wss://02.tfchain.grid.tf/",
160182
"wss://03.tfchain.grid.tf/",
161183
"wss://04.tfchain.grid.tf/",
@@ -164,11 +186,17 @@ var (
164186
"wss://relay.grid.tf",
165187
"wss://relay.02.grid.tf",
166188
},
167-
ActivationURL: "https://activation.grid.tf/activation/activate",
168-
FlistURL: "redis://hub.grid.tf:9900",
169-
BinRepo: "tf-zos-v3-bins",
170-
GraphQL: "https://graphql.grid.tf/graphql",
171-
KycURL: "https://kyc.grid.tf",
189+
ActivationURL: []string{
190+
"https://activation.grid.tf/activation/activate",
191+
"https://activation.02.grid.tf/activation/activate",
192+
},
193+
FlistURL: "redis://hub.grid.tf:9900",
194+
BinRepo: "tf-zos-v3-bins",
195+
GraphQL: []string{
196+
"https://graphql.grid.tf/graphql",
197+
"https://graphql.02.grid.tf/graphql",
198+
},
199+
KycURL: "https://kyc.grid.tf",
172200
}
173201
)
174202

@@ -245,7 +273,7 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
245273

246274
if activation, ok := params.Get("activation"); ok {
247275
if len(activation) > 0 {
248-
env.ActivationURL = activation[len(activation)-1]
276+
env.ActivationURL = activation
249277
}
250278
}
251279

pkg/perf/graphql/graphql_nodes.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const (
1616

1717
// GraphQl for tf graphql client
1818
type GraphQl struct {
19-
client *graphql.Client
19+
urls []string
2020
}
2121

2222
// NewGraphQl creates a new tf graphql client
23-
func NewGraphQl(url string) GraphQl {
24-
client := graphql.NewClient(url, nil)
25-
return GraphQl{client: client}
23+
func NewGraphQl(urls ...string) GraphQl {
24+
if len(urls) == 0 {
25+
panic("urls can't be empty")
26+
}
27+
return GraphQl{urls: urls}
2628
}
2729

2830
// Node from graphql
@@ -85,7 +87,7 @@ func (g *GraphQl) GetUpNodes(ctx context.Context, nodesNum int, farmID, excludeF
8587
Nodes []Node
8688
}{}
8789

88-
if err := g.client.Exec(ctx, query, &res, nil); err != nil {
90+
if err := g.exec(ctx, query, &res, nil); err != nil {
8991
return []Node{}, err
9092
}
9193

@@ -101,9 +103,21 @@ func (g *GraphQl) getItemTotalCount(ctx context.Context, itemName string, option
101103
}
102104
}{}
103105

104-
if err := g.client.Exec(ctx, query, &res, nil); err != nil {
106+
if err := g.exec(ctx, query, &res, nil); err != nil {
105107
return 0, err
106108
}
107109

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

pkg/perf/healthcheck/network.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const defaultRequestTimeout = 5 * time.Second
1717

1818
func networkCheck(ctx context.Context) []error {
1919
env := environment.MustGet()
20-
servicesUrl := []string{
21-
env.ActivationURL, env.GraphQL, env.FlistURL,
22-
}
20+
servicesUrl := []string{env.FlistURL}
2321
servicesUrl = append(append(servicesUrl, env.SubstrateURL...), env.RelayURL...)
22+
servicesUrl = append(append(servicesUrl, env.ActivationURL...), env.GraphQL...)
2423

2524
var errors []error
2625

pkg/perf/iperf/iperf_task.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ 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 := graphql.NewGraphQl(env.GraphQL...)
8080

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

pkg/perf/publicip/publicip_task.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (p *publicIPValidationTask) validateIPs(publicIPs []substrate.PublicIP) (ma
179179

180180
func isLeastValidNode(ctx context.Context, farmID uint32, substrateGateway *stubs.SubstrateGatewayStub) (bool, error) {
181181
env := environment.MustGet()
182-
gql := graphql.NewGraphQl(env.GraphQL)
182+
gql := graphql.NewGraphQl(env.GraphQL...)
183183

184184
nodes, err := gql.GetUpNodes(ctx, 0, farmID, 0, false, false)
185185
if err != nil {

pkg/stubs/api_gateway_stub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *SubstrateGatewayStub) CreateTwin(ctx context.Context, arg0 string, arg1
6464
return
6565
}
6666

67-
func (s *SubstrateGatewayStub) EnsureAccount(ctx context.Context, arg0 string, arg1 string, arg2 string) (ret0 tfchainclientgo.AccountInfo, ret1 error) {
67+
func (s *SubstrateGatewayStub) EnsureAccount(ctx context.Context, arg0 []string, arg1 string, arg2 string) (ret0 tfchainclientgo.AccountInfo, ret1 error) {
6868
args := []interface{}{arg0, arg1, arg2}
6969
result, err := s.client.RequestContext(ctx, s.module, s.object, "EnsureAccount", args...)
7070
if err != nil {

pkg/substrate_gateway/substrate_gateway.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,25 @@ func (g *substrateGateway) CreateTwin(relay string, pk []byte) (uint32, error) {
4848
return g.sub.CreateTwin(g.identity, relay, pk)
4949
}
5050

51-
func (g *substrateGateway) EnsureAccount(activationURL string, termsAndConditionsLink string, termsAndConditionsHash string) (info substrate.AccountInfo, err error) {
51+
func (g *substrateGateway) EnsureAccount(activationURL []string, termsAndConditionsLink string, termsAndConditionsHash string) (info substrate.AccountInfo, err error) {
5252
log.Debug().
5353
Str("method", "EnsureAccount").
54-
Str("activation url", activationURL).
54+
Strs("activation url", activationURL).
5555
Str("terms and conditions link", termsAndConditionsLink).
5656
Str("terms and conditions hash", termsAndConditionsHash).
5757
Msg("method called")
5858
g.mu.Lock()
5959
defer g.mu.Unlock()
60-
return g.sub.EnsureAccount(g.identity, activationURL, termsAndConditionsLink, termsAndConditionsHash)
60+
61+
for _, url := range activationURL {
62+
info, err = g.sub.EnsureAccount(g.identity, url, termsAndConditionsLink, termsAndConditionsHash)
63+
// check other activationURL only if EnsureAccount failed with ActivationServiceError
64+
if err == nil || !errors.As(err, &substrate.ActivationServiceError{}) {
65+
return
66+
}
67+
log.Debug().Str("activation url", url).Err(err).Msg("failed to EnsureAccount with ActivationServiceError")
68+
}
69+
return
6170
}
6271

6372
func (g *substrateGateway) GetContract(id uint64) (result substrate.Contract, serr pkg.SubstrateError) {

0 commit comments

Comments
 (0)