Change router.Route metric to int64. Set the route metric properly

This commit is contained in:
Milos Gajdos 2019-10-23 12:53:36 +01:00
parent 326156671d
commit 1ff65e140a
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
8 changed files with 67 additions and 33 deletions

View File

@ -111,7 +111,7 @@ func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
Gateway: r.Gateway, Gateway: r.Gateway,
Network: r.Network, Network: r.Network,
Link: r.Link, Link: r.Link,
Metric: int(r.Metric), Metric: r.Metric,
}) })
} }

View File

@ -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 // processCtrlChan processes messages received on ControlChannel
func (n *network) processCtrlChan(listener tunnel.Listener) { func (n *network) processCtrlChan(listener tunnel.Listener) {
// receive control message queue // receive control message queue
@ -703,23 +735,27 @@ func (n *network) processCtrlChan(listener tunnel.Listener) {
Network: event.Route.Network, Network: event.Route.Network,
Router: event.Route.Router, Router: event.Route.Router,
Link: event.Route.Link, Link: event.Route.Link,
Metric: int(event.Route.Metric), Metric: event.Route.Metric,
} }
// set the route metric // set the route metric
n.RLock() n.RLock()
if link, ok := n.peerLinks[event.Route.Gateway]; ok { if link, ok := n.peerLinks[event.Route.Gateway]; ok {
// NOTE: should we change router.Route.Metric to int64? // maka sure delay is non-zero
if int(link.Length()) < route.Metric { delay := link.Delay()
route.Metric = int(link.Length()) 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() 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 // create router event
e := &router.Event{ e := &router.Event{
Type: router.EventType(event.Type), Type: router.EventType(event.Type),

View File

@ -31,7 +31,7 @@ func (r *Router) Lookup(ctx context.Context, req *pb.LookupRequest, resp *pb.Loo
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int64(route.Metric), Metric: route.Metric,
} }
respRoutes = append(respRoutes, respRoute) respRoutes = append(respRoutes, respRoute)
} }
@ -67,7 +67,7 @@ func (r *Router) Advertise(ctx context.Context, req *pb.Request, stream pb.Route
Network: event.Route.Network, Network: event.Route.Network,
Router: event.Route.Router, Router: event.Route.Router,
Link: event.Route.Link, Link: event.Route.Link,
Metric: int64(event.Route.Metric), Metric: event.Route.Metric,
} }
e := &pb.Event{ e := &pb.Event{
Type: pb.EventType(event.Type), Type: pb.EventType(event.Type),
@ -108,7 +108,7 @@ func (r *Router) Process(ctx context.Context, req *pb.Advert, rsp *pb.ProcessRes
Network: event.Route.Network, Network: event.Route.Network,
Router: event.Route.Router, Router: event.Route.Router,
Link: event.Route.Link, Link: event.Route.Link,
Metric: int(event.Route.Metric), Metric: event.Route.Metric,
} }
events[i] = &router.Event{ events[i] = &router.Event{
@ -174,7 +174,7 @@ func (r *Router) Watch(ctx context.Context, req *pb.WatchRequest, stream pb.Rout
Network: event.Route.Network, Network: event.Route.Network,
Router: event.Route.Router, Router: event.Route.Router,
Link: event.Route.Link, Link: event.Route.Link,
Metric: int64(event.Route.Metric), Metric: event.Route.Metric,
} }
tableEvent := &pb.Event{ tableEvent := &pb.Event{

View File

@ -20,7 +20,7 @@ func (t *Table) Create(ctx context.Context, route *pb.Route, resp *pb.CreateResp
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int(route.Metric), Metric: route.Metric,
}) })
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.router", "failed to create route: %s", err) return errors.InternalServerError("go.micro.router", "failed to create route: %s", err)
@ -37,7 +37,7 @@ func (t *Table) Update(ctx context.Context, route *pb.Route, resp *pb.UpdateResp
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int(route.Metric), Metric: route.Metric,
}) })
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.router", "failed to update route: %s", err) return errors.InternalServerError("go.micro.router", "failed to update route: %s", err)
@ -54,7 +54,7 @@ func (t *Table) Delete(ctx context.Context, route *pb.Route, resp *pb.DeleteResp
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int(route.Metric), Metric: route.Metric,
}) })
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.router", "failed to delete route: %s", err) return errors.InternalServerError("go.micro.router", "failed to delete route: %s", err)
@ -79,7 +79,7 @@ func (t *Table) List(ctx context.Context, req *pb.Request, resp *pb.ListResponse
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int64(route.Metric), Metric: route.Metric,
} }
respRoutes = append(respRoutes, respRoute) respRoutes = append(respRoutes, respRoute)
} }
@ -104,7 +104,7 @@ func (t *Table) Query(ctx context.Context, req *pb.QueryRequest, resp *pb.QueryR
Network: route.Network, Network: route.Network,
Router: route.Router, Router: route.Router,
Link: route.Link, Link: route.Link,
Metric: int64(route.Metric), Metric: route.Metric,
} }
respRoutes = append(respRoutes, respRoute) respRoutes = append(respRoutes, respRoute)
} }

View File

@ -8,9 +8,7 @@ var (
// DefaultLink is default network link // DefaultLink is default network link
DefaultLink = "local" DefaultLink = "local"
// DefaultLocalMetric is default route cost for a local route // DefaultLocalMetric is default route cost for a local route
DefaultLocalMetric = 1 DefaultLocalMetric int64 = 1
// DefaultNetworkMetric is default route cost for a network route
DefaultNetworkMetric = 10
) )
// Route is network route // Route is network route
@ -28,7 +26,7 @@ type Route struct {
// Link is network link // Link is network link
Link string Link string
// Metric is the route cost metric // Metric is the route cost metric
Metric int Metric int64
} }
// Hash returns route hash sum. // Hash returns route hash sum.

View File

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

View File

@ -21,7 +21,7 @@ func (t *table) Create(r router.Route) error {
Gateway: r.Gateway, Gateway: r.Gateway,
Network: r.Network, Network: r.Network,
Link: r.Link, Link: r.Link,
Metric: int64(r.Metric), Metric: r.Metric,
} }
if _, err := t.table.Create(context.Background(), route, t.callOpts...); err != nil { if _, err := t.table.Create(context.Background(), route, t.callOpts...); err != nil {
@ -39,7 +39,7 @@ func (t *table) Delete(r router.Route) error {
Gateway: r.Gateway, Gateway: r.Gateway,
Network: r.Network, Network: r.Network,
Link: r.Link, Link: r.Link,
Metric: int64(r.Metric), Metric: r.Metric,
} }
if _, err := t.table.Delete(context.Background(), route, t.callOpts...); err != nil { if _, err := t.table.Delete(context.Background(), route, t.callOpts...); err != nil {
@ -57,7 +57,7 @@ func (t *table) Update(r router.Route) error {
Gateway: r.Gateway, Gateway: r.Gateway,
Network: r.Network, Network: r.Network,
Link: r.Link, Link: r.Link,
Metric: int64(r.Metric), Metric: r.Metric,
} }
if _, err := t.table.Update(context.Background(), route, t.callOpts...); err != nil { if _, err := t.table.Update(context.Background(), route, t.callOpts...); err != nil {
@ -82,7 +82,7 @@ func (t *table) List() ([]router.Route, error) {
Gateway: route.Gateway, Gateway: route.Gateway,
Network: route.Network, Network: route.Network,
Link: route.Link, Link: route.Link,
Metric: int(route.Metric), Metric: route.Metric,
} }
} }
@ -115,7 +115,7 @@ func (t *table) Query(q ...router.QueryOption) ([]router.Route, error) {
Gateway: route.Gateway, Gateway: route.Gateway,
Network: route.Network, Network: route.Network,
Link: route.Link, Link: route.Link,
Metric: int(route.Metric), Metric: route.Metric,
} }
} }

View File

@ -61,7 +61,7 @@ func (w *watcher) watch(stream pb.Router_WatchService) error {
Gateway: resp.Route.Gateway, Gateway: resp.Route.Gateway,
Network: resp.Route.Network, Network: resp.Route.Network,
Link: resp.Route.Link, Link: resp.Route.Link,
Metric: int(resp.Route.Metric), Metric: resp.Route.Metric,
} }
event := &router.Event{ event := &router.Event{