Skip to content

Commit 010b3cf

Browse files
committed
move the location update to the register pkg
1 parent 9eeb8a5 commit 010b3cf

File tree

5 files changed

+76
-138
lines changed

5 files changed

+76
-138
lines changed

cmds/modules/noded/main.go

-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/threefoldtech/zos/pkg/perf/publicip"
2424
"github.com/threefoldtech/zos/pkg/registrar"
2525
"github.com/threefoldtech/zos/pkg/stubs"
26-
"github.com/threefoldtech/zos/pkg/updater"
2726
"github.com/threefoldtech/zos/pkg/utils"
2827

2928
"github.com/rs/zerolog/log"
@@ -158,14 +157,6 @@ func action(cli *cli.Context) error {
158157

159158
go registerationServer(ctx, msgBrokerCon, env, info)
160159

161-
log.Info().Msg("start node updater")
162-
nodeUpdater := updater.NewUpdater(redis)
163-
if err != nil {
164-
return errors.Wrap(err, "failed to create new node updater")
165-
}
166-
167-
go nodeUpdater.Start(ctx)
168-
169160
log.Info().Msg("start perf scheduler")
170161

171162
perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon)

pkg/registrar.go

-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@ package pkg
44

55
//go:generate zbusc -module registrar -version 0.0.1 -name registrar -package stubs github.com/threefoldtech/zos4/pkg+Registrar stubs/registrar_stub.go
66

7-
type RegistrationState string
8-
9-
type State struct {
10-
NodeID uint32
11-
TwinID uint32
12-
State RegistrationState
13-
Msg string
14-
}
15-
167
type Registrar interface {
178
NodeID() (uint32, error)
189
TwinID() (uint32, error)
19-
GetState() State
2010
}

pkg/registrar/registrar.go

+76-18
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,67 @@ package registrar
22

33
import (
44
"context"
5+
"fmt"
56
"os"
7+
"reflect"
68
"sync"
79
"time"
810

911
"github.com/cenkalti/backoff/v3"
1012
"github.com/pkg/errors"
1113
"github.com/rs/zerolog/log"
14+
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1215
"github.com/threefoldtech/zbus"
13-
"github.com/threefoldtech/zos/pkg"
1416
"github.com/threefoldtech/zos/pkg/app"
1517
"github.com/threefoldtech/zos/pkg/environment"
18+
"github.com/threefoldtech/zos/pkg/geoip"
1619
"github.com/threefoldtech/zos/pkg/stubs"
1720
)
1821

1922
// should any of this be moved to pkg?
23+
type RegistrationState string
2024

2125
const (
22-
Failed pkg.RegistrationState = "Failed"
23-
InProgress pkg.RegistrationState = "InProgress"
24-
Done pkg.RegistrationState = "Done"
26+
Failed RegistrationState = "Failed"
27+
InProgress RegistrationState = "InProgress"
28+
Done RegistrationState = "Done"
2529

2630
monitorAccountEvery = 30 * time.Minute
27-
updateNodeInfoInterval = 24 * time.Hour
31+
updateLocationInterval = 24 * time.Hour
2832
)
2933

3034
var (
3135
ErrInProgress = errors.New("registration is still in progress")
3236
ErrFailed = errors.New("registration failed")
3337
)
3438

35-
func FailedState(err error) pkg.State {
36-
return pkg.State{
39+
type State struct {
40+
NodeID uint32
41+
TwinID uint32
42+
State RegistrationState
43+
Msg string
44+
}
45+
46+
func FailedState(err error) State {
47+
return State{
3748
0,
3849
0,
3950
Failed,
4051
err.Error(),
4152
}
4253
}
4354

44-
func InProgressState() pkg.State {
45-
return pkg.State{
55+
func InProgressState() State {
56+
return State{
4657
0,
4758
0,
4859
InProgress,
4960
"",
5061
}
5162
}
5263

53-
func DoneState(nodeID uint32, twinID uint32) pkg.State {
54-
return pkg.State{
64+
func DoneState(nodeID uint32, twinID uint32) State {
65+
return State{
5566
nodeID,
5667
twinID,
5768
Done,
@@ -60,32 +71,32 @@ func DoneState(nodeID uint32, twinID uint32) pkg.State {
6071
}
6172

6273
type Registrar struct {
63-
state pkg.State
74+
state State
6475
mutex sync.RWMutex
6576
}
6677

6778
func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar {
6879
r := Registrar{
69-
pkg.State{
80+
state: State{
7081
0,
7182
0,
7283
InProgress,
7384
"",
7485
},
75-
sync.RWMutex{},
86+
mutex: sync.RWMutex{},
7687
}
7788

7889
go r.register(ctx, cl, env, info)
7990
return &r
8091
}
8192

82-
func (r *Registrar) setState(s pkg.State) {
93+
func (r *Registrar) setState(s State) {
8394
r.mutex.Lock()
8495
defer r.mutex.Unlock()
8596
r.state = s
8697
}
8798

88-
func (r *Registrar) GetState() pkg.State {
99+
func (r *Registrar) getState() State {
89100
r.mutex.RLock()
90101
defer r.mutex.RUnlock()
91102
return r.state
@@ -141,9 +152,16 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen
141152
if err := r.reActivate(ctx, cl, env); err != nil {
142153
log.Error().Err(err).Msg("failed to reactivate account")
143154
}
155+
<<<<<<< HEAD
144156
case <-time.After(updateNodeInfoInterval):
145157
log.Info().Msg("update interval passed, re-register")
146158
register()
159+
=======
160+
case <-time.After(updateLocationInterval):
161+
if err := r.updateLocation(ctx, cl); err != nil {
162+
log.Error().Err(err).Msg("updating location failed")
163+
}
164+
>>>>>>> 3b559b8b (move the location update to the register pkg)
147165
case <-addressesUpdate:
148166
log.Info().Msg("zos address has changed, re-register")
149167
register()
@@ -159,12 +177,52 @@ func (r *Registrar) reActivate(ctx context.Context, cl zbus.Client, env environm
159177
return err
160178
}
161179

180+
// updateLocation validates the node location on chain against the geoip
181+
// service and update it if needed.
182+
func (r *Registrar) updateLocation(ctx context.Context, cl zbus.Client) error {
183+
nodeId, err := r.NodeID()
184+
if err != nil {
185+
return fmt.Errorf("failed to get node id: %w", err)
186+
}
187+
188+
substrateGw := stubs.NewSubstrateGatewayStub(cl)
189+
node, err := substrateGw.GetNode(ctx, nodeId)
190+
if err != nil {
191+
return fmt.Errorf("failed to get node from chain: %w", err)
192+
}
193+
194+
loc, err := geoip.Fetch()
195+
if err != nil {
196+
return fmt.Errorf("failed to fetch location info: %w", err)
197+
}
198+
199+
newLoc := substrate.Location{
200+
City: loc.City,
201+
Country: loc.Country,
202+
Latitude: fmt.Sprintf("%f", loc.Latitude),
203+
Longitude: fmt.Sprintf("%f", loc.Longitude),
204+
}
205+
206+
if reflect.DeepEqual(newLoc, node.Location) {
207+
// no need to update
208+
return nil
209+
}
210+
211+
node.Location = newLoc
212+
if _, err := substrateGw.UpdateNode(ctx, node); err != nil {
213+
return fmt.Errorf("failed to update node on chain: %w", err)
214+
}
215+
216+
log.Info().Msg("node location updated")
217+
return nil
218+
}
219+
162220
func (r *Registrar) NodeID() (uint32, error) {
163-
return r.returnIfDone(r.GetState().NodeID)
221+
return r.returnIfDone(r.getState().NodeID)
164222
}
165223

166224
func (r *Registrar) TwinID() (uint32, error) {
167-
return r.returnIfDone(r.GetState().TwinID)
225+
return r.returnIfDone(r.getState().TwinID)
168226
}
169227

170228
func (r *Registrar) returnIfDone(v uint32) (uint32, error) {

pkg/stubs/registrar_stub.go

-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package stubs
77
import (
88
"context"
99
zbus "github.com/threefoldtech/zbus"
10-
pkg "github.com/threefoldtech/zos/pkg"
1110
)
1211

1312
type RegistrarStub struct {
@@ -27,22 +26,6 @@ func NewRegistrarStub(client zbus.Client) *RegistrarStub {
2726
}
2827
}
2928

30-
func (s *RegistrarStub) GetState(ctx context.Context) (ret0 pkg.State) {
31-
args := []interface{}{}
32-
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetState", args...)
33-
if err != nil {
34-
panic(err)
35-
}
36-
result.PanicOnError()
37-
loader := zbus.Loader{
38-
&ret0,
39-
}
40-
if err := result.Unmarshal(&loader); err != nil {
41-
panic(err)
42-
}
43-
return
44-
}
45-
4629
func (s *RegistrarStub) NodeID(ctx context.Context) (ret0 uint32, ret1 error) {
4730
args := []interface{}{}
4831
result, err := s.client.RequestContext(ctx, s.module, s.object, "NodeID", args...)

pkg/updater/updater.go

-84
This file was deleted.

0 commit comments

Comments
 (0)