Skip to content

Commit

Permalink
Move routing table initialization inside the router. Proxy all visor …
Browse files Browse the repository at this point in the history
…RPC calls to RT through router interface
  • Loading branch information
Darkren committed Feb 10, 2020
1 parent 1ce164c commit 66fc6f4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
35 changes: 33 additions & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type Config struct {
PubKey cipher.PubKey
SecKey cipher.SecKey
TransportManager *transport.Manager
RoutingTable routing.Table
RouteFinder rfclient.Client
RouteGroupDialer setupclient.RouteGroupDialer
SetupNodes []cipher.PubKey
Expand Down Expand Up @@ -106,6 +105,13 @@ type Router interface {
IntroduceRules(rules routing.EdgeRules) error
Serve(context.Context) error
SetupIsTrusted(cipher.PubKey) bool

// routing table related methods
RoutesCount() int
Rules() []routing.Rule
Rule(routing.RouteID) (routing.Rule, error)
SaveRule(routing.Rule) error
DelRules([]routing.RouteID)
}

// Router implements visor.PacketRouter. It manages routing table by
Expand Down Expand Up @@ -148,7 +154,7 @@ func New(n *snet.Network, config *Config) (Router, error) {
logger: config.Logger,
n: n,
tm: config.TransportManager,
rt: config.RoutingTable,
rt: routing.NewTable(routing.DefaultConfig()),
sl: sl,
rfc: config.RouteFinder,
rgs: make(map[routing.RouteDescriptor]*RouteGroup),
Expand Down Expand Up @@ -644,3 +650,28 @@ func (r *router) IntroduceRules(rules routing.EdgeRules) error {
}
}
}

// RoutesCount returns count of the routes stored within the routing table.
func (r *router) RoutesCount() int {
return r.rt.Count()
}

// Rules gets all the rules stored within the routing table.
func (r *router) Rules() []routing.Rule {
return r.rt.AllRules()
}

// Rule fetches rule by the route `id`.
func (r *router) Rule(id routing.RouteID) (routing.Rule, error) {
return r.rt.Rule(id)
}

// SaveRule stores the `rule` within the routing table.
func (r *router) SaveRule(rule routing.Rule) error {
return r.rt.SaveRule(rule)
}

// DelRules removes rules associated with `ids` from the routing table.
func (r *router) DelRules(ids []routing.RouteID) {
r.rt.DelRules(ids)
}
5 changes: 0 additions & 5 deletions pkg/visor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ func (c *Config) TransportLogStore() (transport.LogStore, error) {
return transport.InMemoryTransportLogStore(), nil
}

// RoutingTable returns configure routing.Table.
func (c *Config) RoutingTable() (routing.Table, error) {
return routing.NewTable(routing.DefaultConfig()), nil
}

// AppsConfig decodes AppsConfig from a local json config file.
func (c *Config) AppsConfig() (map[string]AppConfig, error) {
apps := make(map[string]AppConfig)
Expand Down
13 changes: 0 additions & 13 deletions pkg/visor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package visor

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -74,18 +73,6 @@ func TestTransportLogStore(t *testing.T) {
require.NotNil(t, ls)
}

func TestRoutingTable(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "routing")
require.NoError(t, err)
defer func() {
require.NoError(t, os.Remove(tmpfile.Name()))
}()

conf := Config{}
_, err = conf.RoutingTable()
require.NoError(t, err)
}

func TestAppsConfig(t *testing.T) {
conf := Config{Version: "1.0"}
conf.Apps = []AppConfig{
Expand Down
14 changes: 7 additions & 7 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (r *RPC) Summary(_ *struct{}, out *Summary) error {
AppProtoVersion: supportedProtocolVersion,
Apps: r.visor.Apps(),
Transports: summaries,
RoutesCount: r.visor.rt.Count(),
RoutesCount: r.visor.router.RoutesCount(),
}
return nil
}
Expand Down Expand Up @@ -354,25 +354,25 @@ func (r *RPC) DiscoverTransportByID(id *uuid.UUID, out *transport.EntryWithStatu

// RoutingRules obtains all routing rules of the RoutingTable.
func (r *RPC) RoutingRules(_ *struct{}, out *[]routing.Rule) error {
*out = r.visor.rt.AllRules()
*out = r.visor.router.Rules()
return nil
}

// RoutingRule obtains a routing rule of given RouteID.
func (r *RPC) RoutingRule(key *routing.RouteID, rule *routing.Rule) error {
var err error
*rule, err = r.visor.rt.Rule(*key)
*rule, err = r.visor.router.Rule(*key)
return err
}

// SaveRoutingRule saves a routing rule.
func (r *RPC) SaveRoutingRule(in *routing.Rule, _ *struct{}) error {
return r.visor.rt.SaveRule(*in)
return r.visor.router.SaveRule(*in)
}

// RemoveRoutingRule removes a RoutingRule based on given RouteID key.
func (r *RPC) RemoveRoutingRule(key *routing.RouteID, _ *struct{}) error {
r.visor.rt.DelRules([]routing.RouteID{*key})
r.visor.router.DelRules([]routing.RouteID{*key})
return nil
}

Expand All @@ -391,14 +391,14 @@ type LoopInfo struct {
func (r *RPC) Loops(_ *struct{}, out *[]LoopInfo) error {
var loops []LoopInfo

rules := r.visor.rt.AllRules()
rules := r.visor.router.Rules()
for _, rule := range rules {
if rule.Type() != routing.RuleConsume {
continue
}

fwdRID := rule.NextRouteID()
rule, err := r.visor.rt.Rule(fwdRID)
rule, err := r.visor.router.Rule(fwdRID)
if err != nil {
return err
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type Visor struct {
router router.Router
n *snet.Network
tm *transport.Manager
rt routing.Table
pty *dmsgpty.Host // TODO(evanlinjin): Complete.

Logger *logging.MasterLogger
Expand Down Expand Up @@ -166,17 +165,11 @@ func NewVisor(cfg *Config, logger *logging.MasterLogger, restartCtx *restart.Con
return nil, fmt.Errorf("transport manager: %s", err)
}

visor.rt, err = cfg.RoutingTable()
if err != nil {
return nil, fmt.Errorf("routing table: %s", err)
}

rConfig := &router.Config{
Logger: visor.Logger.PackageLogger("router"),
PubKey: pk,
SecKey: sk,
TransportManager: visor.tm,
RoutingTable: visor.rt,
RouteFinder: rfclient.NewHTTP(cfg.Routing.RouteFinder, time.Duration(cfg.Routing.RouteFinderTimeout)),
SetupNodes: cfg.Routing.SetupNodes,
}
Expand Down

0 comments on commit 66fc6f4

Please sign in to comment.