Skip to content

Commit

Permalink
move the location update to the register pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Sep 23, 2024
1 parent 435ca91 commit d69a2f7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 138 deletions.
9 changes: 0 additions & 9 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/threefoldtech/zos/pkg/perf/publicip"
"github.com/threefoldtech/zos/pkg/registrar"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/threefoldtech/zos/pkg/updater"
"github.com/threefoldtech/zos/pkg/utils"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -163,14 +162,6 @@ func action(cli *cli.Context) error {

go registerationServer(ctx, msgBrokerCon, env, info)

log.Info().Msg("start node updater")
nodeUpdater := updater.NewUpdater(redis)
if err != nil {
return errors.Wrap(err, "failed to create new node updater")
}

go nodeUpdater.Start(ctx)

log.Info().Msg("start perf scheduler")

perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon)
Expand Down
10 changes: 0 additions & 10 deletions pkg/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ package pkg

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

type RegistrationState string

type State struct {
NodeID uint32
TwinID uint32
State RegistrationState
Msg string
}

type Registrar interface {
NodeID() (uint32, error)
TwinID() (uint32, error)
GetState() State
}
93 changes: 75 additions & 18 deletions pkg/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,67 @@ package registrar

import (
"context"
"fmt"
"os"
"reflect"
"sync"
"time"

"github.com/cenkalti/backoff/v3"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/app"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/geoip"
"github.com/threefoldtech/zos/pkg/stubs"
)

// should any of this be moved to pkg?
type RegistrationState string

const (
Failed pkg.RegistrationState = "Failed"
InProgress pkg.RegistrationState = "InProgress"
Done pkg.RegistrationState = "Done"
Failed RegistrationState = "Failed"
InProgress RegistrationState = "InProgress"
Done RegistrationState = "Done"

monitorAccountEvery = 30 * time.Minute
monitorAccountEvery = 30 * time.Minute
updateLocationInterval = 24 * time.Hour
)

var (
ErrInProgress = errors.New("registration is still in progress")
ErrFailed = errors.New("registration failed")
)

func FailedState(err error) pkg.State {
return pkg.State{
type State struct {
NodeID uint32
TwinID uint32
State RegistrationState
Msg string
}

func FailedState(err error) State {
return State{
0,
0,
Failed,
err.Error(),
}
}

func InProgressState() pkg.State {
return pkg.State{
func InProgressState() State {
return State{
0,
0,
InProgress,
"",
}
}

func DoneState(nodeID uint32, twinID uint32) pkg.State {
return pkg.State{
func DoneState(nodeID uint32, twinID uint32) State {
return State{
nodeID,
twinID,
Done,
Expand All @@ -59,31 +71,32 @@ func DoneState(nodeID uint32, twinID uint32) pkg.State {
}

type Registrar struct {
state pkg.State
state State
mutex sync.RWMutex
}

func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar {
r := Registrar{
pkg.State{
state: State{
0,
0,
InProgress,
"",
},
sync.RWMutex{},
mutex: sync.RWMutex{},
}

go r.register(ctx, cl, env, info)
return &r
}

func (r *Registrar) setState(s pkg.State) {
func (r *Registrar) setState(s State) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.state = s
}

func (r *Registrar) GetState() pkg.State {
func (r *Registrar) getState() State {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.state
Expand Down Expand Up @@ -139,6 +152,10 @@ func (r *Registrar) register(ctx context.Context, cl zbus.Client, env environmen
if err := r.reActivate(ctx, cl, env); err != nil {
log.Error().Err(err).Msg("failed to reactivate account")
}
case <-time.After(updateLocationInterval):
if err := r.updateLocation(ctx, cl); err != nil {
log.Error().Err(err).Msg("updating location failed")
}
case <-addressesUpdate:
log.Info().Msg("zos address has changed, re-register")
register()
Expand All @@ -154,12 +171,52 @@ func (r *Registrar) reActivate(ctx context.Context, cl zbus.Client, env environm
return err
}

// updateLocation validates the node location on chain against the geoip
// service and update it if needed.
func (r *Registrar) updateLocation(ctx context.Context, cl zbus.Client) error {
nodeId, err := r.NodeID()
if err != nil {
return fmt.Errorf("failed to get node id: %w", err)
}

substrateGw := stubs.NewSubstrateGatewayStub(cl)
node, err := substrateGw.GetNode(ctx, nodeId)
if err != nil {
return fmt.Errorf("failed to get node from chain: %w", err)
}

loc, err := geoip.Fetch()
if err != nil {
return fmt.Errorf("failed to fetch location info: %w", err)
}

newLoc := substrate.Location{
City: loc.City,
Country: loc.Country,
Latitude: fmt.Sprintf("%f", loc.Latitude),
Longitude: fmt.Sprintf("%f", loc.Longitude),
}

if reflect.DeepEqual(newLoc, node.Location) {
// no need to update
return nil
}

node.Location = newLoc
if _, err := substrateGw.UpdateNode(ctx, node); err != nil {
return fmt.Errorf("failed to update node on chain: %w", err)
}

log.Info().Msg("node location updated")
return nil
}

func (r *Registrar) NodeID() (uint32, error) {
return r.returnIfDone(r.GetState().NodeID)
return r.returnIfDone(r.getState().NodeID)
}

func (r *Registrar) TwinID() (uint32, error) {
return r.returnIfDone(r.GetState().TwinID)
return r.returnIfDone(r.getState().TwinID)
}

func (r *Registrar) returnIfDone(v uint32) (uint32, error) {
Expand Down
17 changes: 0 additions & 17 deletions pkg/stubs/registrar_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package stubs
import (
"context"
zbus "github.com/threefoldtech/zbus"
pkg "github.com/threefoldtech/zos/pkg"
)

type RegistrarStub struct {
Expand All @@ -27,22 +26,6 @@ func NewRegistrarStub(client zbus.Client) *RegistrarStub {
}
}

func (s *RegistrarStub) GetState(ctx context.Context) (ret0 pkg.State) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetState", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
loader := zbus.Loader{
&ret0,
}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *RegistrarStub) NodeID(ctx context.Context) (ret0 uint32, ret1 error) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "NodeID", args...)
Expand Down
84 changes: 0 additions & 84 deletions pkg/updater/updater.go

This file was deleted.

0 comments on commit d69a2f7

Please sign in to comment.