Debug messages. Squashed Add Route bugs and few others.

This commit is contained in:
Milos Gajdos 2019-06-18 18:33:05 +01:00
parent 2674294cbe
commit d3525ebab3
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
4 changed files with 91 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"sync"
"time"
"github.com/micro/go-log"
"github.com/micro/go-micro/registry"
"github.com/olekukonko/tablewriter"
)
@ -206,6 +207,7 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
if err != nil {
watchErr = err
log.Logf("[router] registry error: %s", err)
break
}
@ -218,18 +220,25 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
switch res.Action {
case "create":
log.Logf("[router] received <%s> create event for service %s", network, res.Service.Name)
if len(res.Service.Nodes) > 0 {
log.Logf("[router] adding <%s> service %s to routing table", network, res.Service.Name)
/// only return error if the route is not duplicate, but something else has failed
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("failed to add route for service: %v", res.Service.Name)
}
log.Logf("[router] route successfully added; routing table: \n%s", r.opts.Table)
}
case "delete":
log.Logf("[router] received <%s> delete event for service %s", network, res.Service.Name)
//log.Logf("[router] <%s> service nodes: %v", network, res.Service.Nodes)
if len(res.Service.Nodes) < 1 {
log.Logf("[router] removing <%s> service %s from routing table", network, res.Service.Name)
// only return error if the route is present in the table, but something else has failed
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound {
return fmt.Errorf("failed to delete route for service: %v", res.Service.Name)
}
log.Logf("[router] route successfully deleted; routing table: \n%s", r.opts.Table)
}
}
}
@ -260,6 +269,7 @@ func (r *router) watchTable(w Watcher) error {
if err != nil {
watchErr = err
log.Logf("[router] routing table error: %s", err)
break
}
@ -276,13 +286,18 @@ func (r *router) watchTable(w Watcher) error {
switch event.Type {
case CreateEvent:
if err := r.opts.NetworkRegistry.Register(service, registry.RegisterTTL(120*time.Second)); err != nil {
log.Logf("[router] adding service %s to network registry", event.Route.Options().DestAddr)
//if err := r.opts.NetworkRegistry.Register(service, registry.RegisterTTL(120*time.Second)); err != nil {
if err := r.opts.NetworkRegistry.Register(service, registry.RegisterTTL(5*time.Second)); err != nil {
return fmt.Errorf("failed to register service %s in network registry: %v", service.Name, err)
}
log.Logf("[router] successfully added service %s to network registry", event.Route.Options().DestAddr)
case DeleteEvent:
log.Logf("[router] deleting service %s from network registry", event.Route.Options().DestAddr)
if err := r.opts.NetworkRegistry.Deregister(service); err != nil {
return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err)
}
log.Logf("[router] successfully deleted service %s from network registry", event.Route.Options().DestAddr)
}
}

View File

@ -8,6 +8,7 @@ import (
"sync"
"github.com/google/uuid"
"github.com/micro/go-log"
"github.com/olekukonko/tablewriter"
)
@ -70,16 +71,28 @@ func (t *table) Add(r Route) error {
t.Lock()
defer t.Unlock()
log.Logf("[table] AddRoute request %d %s: \n%s", sum, r.Options().Policy, r)
// check if the destination has any routes in the table
if _, ok := t.m[destAddr]; !ok {
log.Logf("[table] destination does NOT exist ADDING: \n%s", r)
t.m[destAddr] = make(map[uint64]Route)
t.m[destAddr][sum] = r
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
return nil
}
// add new route to the table for the given destination
if _, ok := t.m[destAddr][sum]; !ok {
log.Logf("[table] route does NOT exist ADDING: \n%s", r)
t.m[destAddr][sum] = r
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
return nil
}
// only add the route if it exists and if override is requested
if _, ok := t.m[destAddr][sum]; ok && r.Options().Policy == OverrideIfExists {
log.Logf("[table] route does exist OVERRIDING: \n%s", r)
t.m[destAddr][sum] = r
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
return nil
@ -88,9 +101,12 @@ func (t *table) Add(r Route) error {
// if we reached this point without already returning the route already exists
// we return nil only if explicitly requested by the client
if r.Options().Policy == IgnoreIfExists {
log.Logf("[table] route does exist IGNORING: \n%s", r)
return nil
}
log.Logf("[table] AddRoute request: DUPPLICATE ROUTE")
return ErrDuplicateRoute
}
@ -102,7 +118,10 @@ func (t *table) Delete(r Route) error {
destAddr := r.Options().DestAddr
sum := t.hash(r)
log.Logf("[table] DeleteRoute request %d: \n%s", sum, r)
if _, ok := t.m[destAddr]; !ok {
log.Logf("[table] DeleteRoute Route NOT found: %s", r)
return ErrRouteNotFound
}
@ -237,7 +256,7 @@ func (t *table) String() string {
strRoute := []string{
route.Options().DestAddr,
route.Options().Gateway.Address(),
route.Options().Gateway.Network(),
route.Options().Network,
fmt.Sprintf("%d", route.Options().Metric),
}
table.Append(strRoute)
@ -252,11 +271,12 @@ func (t *table) String() string {
// hash hashes the route using router gateway and network address
func (t *table) hash(r Route) uint64 {
destAddr := r.Options().DestAddr
gwAddr := r.Options().Gateway.Address()
netAddr := r.Options().Network
t.h.Reset()
t.h.Write([]byte(gwAddr + netAddr))
t.h.Write([]byte(destAddr + gwAddr + netAddr))
return t.h.Sum64()
}

View File

@ -10,6 +10,18 @@ const (
ClosestMatch
)
// String returns human representation of LookupPolicy
func (lp LookupPolicy) String() string {
switch lp {
case DiscardNoRoute:
return "DISCARD"
case ClosestMatch:
return "CLOSEST"
default:
return "UNKNOWN"
}
}
// QueryOption sets routing table query options
type QueryOption func(*QueryOptions)

View File

@ -1,6 +1,11 @@
package router
import "context"
import (
"fmt"
"strings"
"github.com/olekukonko/tablewriter"
)
var (
// DefaultLocalMetric is default route cost for local network
@ -19,6 +24,18 @@ const (
IgnoreIfExists
)
// String returns human reprensentation of policy
func (p AddPolicy) String() string {
switch p {
case OverrideIfExists:
return "OVERRIDE"
case IgnoreIfExists:
return "IGNORE"
default:
return "UNKNOWN"
}
}
// RouteOption is used to set routing table entry options
type RouteOption func(*RouteOptions)
@ -34,8 +51,6 @@ type RouteOptions struct {
Metric int
// Policy defines route addition policy
Policy AddPolicy
// Context stores other arbitrary options
Context context.Context
}
// DestAddr sets destination address
@ -100,3 +115,26 @@ func NewRoute(opts ...RouteOption) Route {
func (r *route) Options() RouteOptions {
return r.opts
}
// String allows to print the route
func (r *route) String() string {
// this will help us build routing table string
sb := &strings.Builder{}
// create nice table printing structure
table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
strRoute := []string{
r.opts.DestAddr,
r.opts.Gateway.Address(),
r.opts.Network,
fmt.Sprintf("%d", r.opts.Metric),
}
table.Append(strRoute)
// render table into sb
table.Render()
return sb.String()
}