Skip to content

Commit

Permalink
Change router.Route metric to int64. Set the route metric properly
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgajdos committed Oct 23, 2019
1 parent 3261566 commit 1ff65e1
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion client/selector/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
Gateway: r.Gateway,
Network: r.Network,
Link: r.Link,
Metric: int(r.Metric),
Metric: r.Metric,
})
}

Expand Down
56 changes: 46 additions & 10 deletions network/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,38 @@ func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *Message) {
}
}

// getHopCount queries network graph and returns hop count for given router
// - Routes for local services have hop count 1
// - Routes with ID of adjacent nodes have hop count 2
// - Routes by peers of the advertiser have hop count 3
// - Routes beyond node neighbourhood have hop count 4
func (n *network) getHopCount(rtr string) int {
// make sure node.peers are not modified
n.node.RLock()
defer n.node.RUnlock()

// we are the origin of the route
if rtr == n.options.Id {
return 1
}

// the route origin is our peer
if _, ok := n.peers[rtr]; ok {
return 2
}

// the route origin is the peer of our peer
for _, peer := range n.peers {
for id := range peer.peers {
if rtr == id {
return 3
}
}
}
// otherwise we are three hops away
return 4
}

// processCtrlChan processes messages received on ControlChannel
func (n *network) processCtrlChan(listener tunnel.Listener) {
// receive control message queue
Expand Down Expand Up @@ -703,23 +735,27 @@ func (n *network) processCtrlChan(listener tunnel.Listener) {
Network: event.Route.Network,
Router: event.Route.Router,
Link: event.Route.Link,
Metric: int(event.Route.Metric),
Metric: event.Route.Metric,
}
// set the route metric
n.RLock()
if link, ok := n.peerLinks[event.Route.Gateway]; ok {
// NOTE: should we change router.Route.Metric to int64?
if int(link.Length()) < route.Metric {
route.Metric = int(link.Length())
// maka sure delay is non-zero
delay := link.Delay()
if delay == 0 {
delay = 1
}
// get the route hop count
hops := n.getHopCount(event.Route.Router)
// make sure length is non-zero
length := link.Length()
if length == 0 {
length = 10e10
}
metric := (delay * length * int64(hops)) / 10e9
route.Metric += metric
}
n.RUnlock()
// TODO: Are we dropping any routes?
// throw away metric bigger than 1000
//if route.Metric > 1000 {
// log.Debugf("Network route metric %d dropping node: %s", route.Metric, route.Router)
// continue
//}
// create router event
e := &router.Event{
Type: router.EventType(event.Type),
Expand Down
8 changes: 4 additions & 4 deletions router/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r *Router) Lookup(ctx context.Context, req *pb.LookupRequest, resp *pb.Loo
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int64(route.Metric),
Metric: route.Metric,
}
respRoutes = append(respRoutes, respRoute)
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (r *Router) Advertise(ctx context.Context, req *pb.Request, stream pb.Route
Network: event.Route.Network,
Router: event.Route.Router,
Link: event.Route.Link,
Metric: int64(event.Route.Metric),
Metric: event.Route.Metric,
}
e := &pb.Event{
Type: pb.EventType(event.Type),
Expand Down Expand Up @@ -108,7 +108,7 @@ func (r *Router) Process(ctx context.Context, req *pb.Advert, rsp *pb.ProcessRes
Network: event.Route.Network,
Router: event.Route.Router,
Link: event.Route.Link,
Metric: int(event.Route.Metric),
Metric: event.Route.Metric,
}

events[i] = &router.Event{
Expand Down Expand Up @@ -174,7 +174,7 @@ func (r *Router) Watch(ctx context.Context, req *pb.WatchRequest, stream pb.Rout
Network: event.Route.Network,
Router: event.Route.Router,
Link: event.Route.Link,
Metric: int64(event.Route.Metric),
Metric: event.Route.Metric,
}

tableEvent := &pb.Event{
Expand Down
10 changes: 5 additions & 5 deletions router/handler/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (t *Table) Create(ctx context.Context, route *pb.Route, resp *pb.CreateResp
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to create route: %s", err)
Expand All @@ -37,7 +37,7 @@ func (t *Table) Update(ctx context.Context, route *pb.Route, resp *pb.UpdateResp
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to update route: %s", err)
Expand All @@ -54,7 +54,7 @@ func (t *Table) Delete(ctx context.Context, route *pb.Route, resp *pb.DeleteResp
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
})
if err != nil {
return errors.InternalServerError("go.micro.router", "failed to delete route: %s", err)
Expand All @@ -79,7 +79,7 @@ func (t *Table) List(ctx context.Context, req *pb.Request, resp *pb.ListResponse
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int64(route.Metric),
Metric: route.Metric,
}
respRoutes = append(respRoutes, respRoute)
}
Expand All @@ -104,7 +104,7 @@ func (t *Table) Query(ctx context.Context, req *pb.QueryRequest, resp *pb.QueryR
Network: route.Network,
Router: route.Router,
Link: route.Link,
Metric: int64(route.Metric),
Metric: route.Metric,
}
respRoutes = append(respRoutes, respRoute)
}
Expand Down
6 changes: 2 additions & 4 deletions router/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ var (
// DefaultLink is default network link
DefaultLink = "local"
// DefaultLocalMetric is default route cost for a local route
DefaultLocalMetric = 1
// DefaultNetworkMetric is default route cost for a network route
DefaultNetworkMetric = 10
DefaultLocalMetric int64 = 1
)

// Route is network route
Expand All @@ -28,7 +26,7 @@ type Route struct {
// Link is network link
Link string
// Metric is the route cost metric
Metric int
Metric int64
}

// Hash returns route hash sum.
Expand Down
6 changes: 3 additions & 3 deletions router/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (s *svc) advertiseEvents(advertChan chan *router.Advert, stream pb.Router_A
Gateway: event.Route.Gateway,
Network: event.Route.Network,
Link: event.Route.Link,
Metric: int(event.Route.Metric),
Metric: event.Route.Metric,
}

events[i] = &router.Event{
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *svc) Process(advert *router.Advert) error {
Gateway: event.Route.Gateway,
Network: event.Route.Network,
Link: event.Route.Link,
Metric: int64(event.Route.Metric),
Metric: event.Route.Metric,
}
e := &pb.Event{
Type: pb.EventType(event.Type),
Expand Down Expand Up @@ -346,7 +346,7 @@ func (s *svc) Lookup(q ...router.QueryOption) ([]router.Route, error) {
Gateway: route.Gateway,
Network: route.Network,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
}
}

Expand Down
10 changes: 5 additions & 5 deletions router/service/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (t *table) Create(r router.Route) error {
Gateway: r.Gateway,
Network: r.Network,
Link: r.Link,
Metric: int64(r.Metric),
Metric: r.Metric,
}

if _, err := t.table.Create(context.Background(), route, t.callOpts...); err != nil {
Expand All @@ -39,7 +39,7 @@ func (t *table) Delete(r router.Route) error {
Gateway: r.Gateway,
Network: r.Network,
Link: r.Link,
Metric: int64(r.Metric),
Metric: r.Metric,
}

if _, err := t.table.Delete(context.Background(), route, t.callOpts...); err != nil {
Expand All @@ -57,7 +57,7 @@ func (t *table) Update(r router.Route) error {
Gateway: r.Gateway,
Network: r.Network,
Link: r.Link,
Metric: int64(r.Metric),
Metric: r.Metric,
}

if _, err := t.table.Update(context.Background(), route, t.callOpts...); err != nil {
Expand All @@ -82,7 +82,7 @@ func (t *table) List() ([]router.Route, error) {
Gateway: route.Gateway,
Network: route.Network,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (t *table) Query(q ...router.QueryOption) ([]router.Route, error) {
Gateway: route.Gateway,
Network: route.Network,
Link: route.Link,
Metric: int(route.Metric),
Metric: route.Metric,
}
}

Expand Down
2 changes: 1 addition & 1 deletion router/service/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w *watcher) watch(stream pb.Router_WatchService) error {
Gateway: resp.Route.Gateway,
Network: resp.Route.Network,
Link: resp.Route.Link,
Metric: int(resp.Route.Metric),
Metric: resp.Route.Metric,
}

event := &router.Event{
Expand Down

0 comments on commit 1ff65e1

Please sign in to comment.