mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Debug messages. Squashed Add Route bugs and few others.
This commit is contained in:
parent
2674294cbe
commit
d3525ebab3
@ -7,6 +7,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/micro/go-log"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
)
|
)
|
||||||
@ -206,6 +207,7 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
watchErr = err
|
watchErr = err
|
||||||
|
log.Logf("[router] registry error: %s", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,18 +220,25 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
|
|||||||
|
|
||||||
switch res.Action {
|
switch res.Action {
|
||||||
case "create":
|
case "create":
|
||||||
|
log.Logf("[router] received <%s> create event for service %s", network, res.Service.Name)
|
||||||
if len(res.Service.Nodes) > 0 {
|
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
|
/// 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 {
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
||||||
return fmt.Errorf("failed to add route for service: %v", res.Service.Name)
|
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":
|
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 {
|
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
|
// 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 {
|
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound {
|
||||||
return fmt.Errorf("failed to delete route for service: %v", res.Service.Name)
|
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 {
|
if err != nil {
|
||||||
watchErr = err
|
watchErr = err
|
||||||
|
log.Logf("[router] routing table error: %s", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,13 +286,18 @@ func (r *router) watchTable(w Watcher) error {
|
|||||||
|
|
||||||
switch event.Type {
|
switch event.Type {
|
||||||
case CreateEvent:
|
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)
|
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:
|
case DeleteEvent:
|
||||||
|
log.Logf("[router] deleting service %s from network registry", event.Route.Options().DestAddr)
|
||||||
if err := r.opts.NetworkRegistry.Deregister(service); err != nil {
|
if err := r.opts.NetworkRegistry.Deregister(service); err != nil {
|
||||||
return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/micro/go-log"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -70,16 +71,28 @@ func (t *table) Add(r Route) error {
|
|||||||
t.Lock()
|
t.Lock()
|
||||||
defer t.Unlock()
|
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
|
// check if the destination has any routes in the table
|
||||||
if _, ok := t.m[destAddr]; !ok {
|
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] = make(map[uint64]Route)
|
||||||
t.m[destAddr][sum] = r
|
t.m[destAddr][sum] = r
|
||||||
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
go t.sendEvent(&Event{Type: CreateEvent, Route: r})
|
||||||
return nil
|
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
|
// only add the route if it exists and if override is requested
|
||||||
if _, ok := t.m[destAddr][sum]; ok && r.Options().Policy == OverrideIfExists {
|
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
|
t.m[destAddr][sum] = r
|
||||||
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
|
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
|
||||||
return nil
|
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
|
// if we reached this point without already returning the route already exists
|
||||||
// we return nil only if explicitly requested by the client
|
// we return nil only if explicitly requested by the client
|
||||||
if r.Options().Policy == IgnoreIfExists {
|
if r.Options().Policy == IgnoreIfExists {
|
||||||
|
log.Logf("[table] route does exist IGNORING: \n%s", r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Logf("[table] AddRoute request: DUPPLICATE ROUTE")
|
||||||
|
|
||||||
return ErrDuplicateRoute
|
return ErrDuplicateRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +118,10 @@ func (t *table) Delete(r Route) error {
|
|||||||
destAddr := r.Options().DestAddr
|
destAddr := r.Options().DestAddr
|
||||||
sum := t.hash(r)
|
sum := t.hash(r)
|
||||||
|
|
||||||
|
log.Logf("[table] DeleteRoute request %d: \n%s", sum, r)
|
||||||
|
|
||||||
if _, ok := t.m[destAddr]; !ok {
|
if _, ok := t.m[destAddr]; !ok {
|
||||||
|
log.Logf("[table] DeleteRoute Route NOT found: %s", r)
|
||||||
return ErrRouteNotFound
|
return ErrRouteNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +256,7 @@ func (t *table) String() string {
|
|||||||
strRoute := []string{
|
strRoute := []string{
|
||||||
route.Options().DestAddr,
|
route.Options().DestAddr,
|
||||||
route.Options().Gateway.Address(),
|
route.Options().Gateway.Address(),
|
||||||
route.Options().Gateway.Network(),
|
route.Options().Network,
|
||||||
fmt.Sprintf("%d", route.Options().Metric),
|
fmt.Sprintf("%d", route.Options().Metric),
|
||||||
}
|
}
|
||||||
table.Append(strRoute)
|
table.Append(strRoute)
|
||||||
@ -252,11 +271,12 @@ func (t *table) String() string {
|
|||||||
|
|
||||||
// hash hashes the route using router gateway and network address
|
// hash hashes the route using router gateway and network address
|
||||||
func (t *table) hash(r Route) uint64 {
|
func (t *table) hash(r Route) uint64 {
|
||||||
|
destAddr := r.Options().DestAddr
|
||||||
gwAddr := r.Options().Gateway.Address()
|
gwAddr := r.Options().Gateway.Address()
|
||||||
netAddr := r.Options().Network
|
netAddr := r.Options().Network
|
||||||
|
|
||||||
t.h.Reset()
|
t.h.Reset()
|
||||||
t.h.Write([]byte(gwAddr + netAddr))
|
t.h.Write([]byte(destAddr + gwAddr + netAddr))
|
||||||
|
|
||||||
return t.h.Sum64()
|
return t.h.Sum64()
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,18 @@ const (
|
|||||||
ClosestMatch
|
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
|
// QueryOption sets routing table query options
|
||||||
type QueryOption func(*QueryOptions)
|
type QueryOption func(*QueryOptions)
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultLocalMetric is default route cost for local network
|
// DefaultLocalMetric is default route cost for local network
|
||||||
@ -19,6 +24,18 @@ const (
|
|||||||
IgnoreIfExists
|
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
|
// RouteOption is used to set routing table entry options
|
||||||
type RouteOption func(*RouteOptions)
|
type RouteOption func(*RouteOptions)
|
||||||
|
|
||||||
@ -34,8 +51,6 @@ type RouteOptions struct {
|
|||||||
Metric int
|
Metric int
|
||||||
// Policy defines route addition policy
|
// Policy defines route addition policy
|
||||||
Policy AddPolicy
|
Policy AddPolicy
|
||||||
// Context stores other arbitrary options
|
|
||||||
Context context.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DestAddr sets destination address
|
// DestAddr sets destination address
|
||||||
@ -100,3 +115,26 @@ func NewRoute(opts ...RouteOption) Route {
|
|||||||
func (r *route) Options() RouteOptions {
|
func (r *route) Options() RouteOptions {
|
||||||
return r.opts
|
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()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user