Skip to content

Commit

Permalink
Simplified table logic. Lookup tests. mucp/cient update
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgajdos committed Jul 9, 2019
1 parent cc590f5 commit b822454
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 193 deletions.
11 changes: 6 additions & 5 deletions client/selector/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/network/router"
pb "github.com/micro/go-micro/network/router/proto"
"github.com/micro/go-micro/network/router/table"
"github.com/micro/go-micro/registry"
)

Expand Down Expand Up @@ -40,11 +41,11 @@ type clientKey struct{}
type routerKey struct{}

// getRoutes returns the routes whether they are remote or local
func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
func (r *routerSelector) getRoutes(service string) ([]table.Route, error) {
if !r.remote {
// lookup router for routes for the service
return r.r.Table().Lookup(router.NewQuery(
router.QueryDestination(service),
return r.r.Table().Lookup(table.NewQuery(
table.QueryDestination(service),
))
}

Expand Down Expand Up @@ -101,11 +102,11 @@ func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
return nil, selector.ErrNoneAvailable
}

var routes []router.Route
var routes []table.Route

// convert from pb to []*router.Route
for _, r := range pbRoutes.Routes {
routes = append(routes, router.Route{
routes = append(routes, table.Route{
Destination: r.Destination,
Gateway: r.Gateway,
Router: r.Router,
Expand Down
11 changes: 6 additions & 5 deletions network/proxy/mucp/mucp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/micro/go-micro/server"

pb "github.com/micro/go-micro/network/router/proto"
"github.com/micro/go-micro/network/router/table"
)

// Proxy will transparently proxy requests to an endpoint.
Expand All @@ -40,7 +41,7 @@ type Proxy struct {

// A fib of routes service:address
sync.RWMutex
Routes map[string][]router.Route
Routes map[string][]table.Route
}

// read client request and write to server
Expand Down Expand Up @@ -80,7 +81,7 @@ func readLoop(r server.Request, s client.Stream) error {

func (p *Proxy) getRoute(service string) ([]string, error) {
// converts routes to just addresses
toNodes := func(routes []router.Route) []string {
toNodes := func(routes []table.Route) []string {
var nodes []string
for _, node := range routes {
nodes = append(nodes, node.Gateway)
Expand All @@ -106,15 +107,15 @@ func (p *Proxy) getRoute(service string) ([]string, error) {
if p.Router != nil {
// lookup the router
routes, err := p.Router.Table().Lookup(
router.NewQuery(router.QueryDestination(service)),
table.NewQuery(table.QueryDestination(service)),
)
if err != nil {
return nil, err
}

p.Lock()
if p.Routes == nil {
p.Routes = make(map[string][]router.Route)
p.Routes = make(map[string][]table.Route)
}
p.Routes[service] = routes
p.Unlock()
Expand Down Expand Up @@ -203,7 +204,7 @@ func (p *Proxy) getRoute(service string) ([]string, error) {

// convert from pb to []*router.Route
for _, r := range pbRoutes.Routes {
routes = append(routes, router.Route{
routes = append(routes, table.Route{
Destination: r.Destination,
Gateway: r.Gateway,
Router: r.Router,
Expand Down
111 changes: 52 additions & 59 deletions network/router/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import (
)

const (
// UpdateRoutePenalty penalises route updates
UpdateRoutePenalty = 500
// DeleteRoutePenalty penalises route deletes
DeleteRoutePenalty = 1000
// AdvertiseTick is time interval in which we advertise route updates
AdvertiseTick = 5 * time.Second
// AdvertSuppress is advert suppression threshold
AdvertSuppress = 2000
// AdvertRecover is advert suppression recovery threshold
// AdvertRecover is advert recovery threshold
AdvertRecover = 750
// PenaltyDecay is the "half-life" of the penalty
// DefaultAdvertTTL is default advertisement TTL
DefaultAdvertTTL = time.Minute
// PenaltyDecay is the penalty decay
PenaltyDecay = 1.15
// Delete penalises route addition and deletion
Delete = 1000
// UpdatePenalty penalises route updates
UpdatePenalty = 500
)

// router provides default router implementation
Expand Down Expand Up @@ -93,8 +95,8 @@ func (r *router) Network() string {
return r.opts.Network
}

// manageServiceRoutes manages the routes for a given service.
// It returns error of the routing table action fails with error.
// manageServiceRoutes manages routes for a given service.
// It returns error of the routing table action fails.
func (r *router) manageServiceRoutes(service *registry.Service, action string, metric int) error {
// action is the routing table action
action = strings.ToLower(action)
Expand Down Expand Up @@ -124,7 +126,7 @@ func (r *router) manageServiceRoutes(service *registry.Service, action string, m
}

// manageRegistryRoutes manages routes for each service found in the registry.
// It returns error if either the services failed to be listed or if the routing table action fails wirh error
// It returns error if either the services failed to be listed or the routing table action fails.
func (r *router) manageRegistryRoutes(reg registry.Registry, action string, metric int) error {
services, err := reg.ListServices()
if err != nil {
Expand Down Expand Up @@ -222,66 +224,60 @@ func (r *router) watchTable(w table.Watcher) error {
return watchErr
}

func eventFlap(curr, prev *table.Event) bool {
// isFlapping detects if the event is flapping based on the current and previous event status.
func isFlapping(curr, prev *table.Event) bool {
if curr.Type == table.Update && prev.Type == table.Update {
// update flap: this can be either metric or whatnot
log.Logf("eventFlap(): Update flap")
log.Logf("isFlapping(): Update flap")
return true
}

if curr.Type == table.Create && prev.Type == table.Delete || curr.Type == table.Delete && prev.Type == table.Create {
log.Logf("eventFlap(): Create/Delete flap")
if curr.Type == table.Insert && prev.Type == table.Delete || curr.Type == table.Delete && prev.Type == table.Insert {
log.Logf("isFlapping(): Create/Delete flap")
return true
}

return false
}

// updateEvent is a table event enriched with advertisement data
type updateEvent struct {
*table.Event
// timestamp marks the time the event has been received
timestamp time.Time
// penalty is current event penalty
penalty float64
// isSuppressed flags if the event should be considered for flap detection
isSuppressed bool
// isFlapping marks the event as flapping event
isFlapping bool
}

// processEvents processes routing table events.
// It suppresses unhealthy flapping events and advertises healthy events upstream.
func (r *router) processEvents() error {
// ticker to periodically scan event for advertising
ticker := time.NewTicker(AdvertiseTick)

// advertEvent is a table event enriched with advert data
type advertEvent struct {
*table.Event
timestamp time.Time
penalty float64
isSuppressed bool
isFlapping bool
}

// eventMap is a map of advert events that might end up being advertised
eventMap := make(map[uint64]*advertEvent)
// eventMap is a map of advert events
eventMap := make(map[uint64]*updateEvent)
// lock to protect access to eventMap
mu := &sync.RWMutex{}
// waitgroup to manage advertisement goroutines
var wg sync.WaitGroup

process:
processLoop:
for {
select {
case <-ticker.C:
var events []*table.Event
// decay the penalties of existing events
// collect all events which are not flapping
mu.Lock()
for advert, event := range eventMap {
delta := time.Since(event.timestamp).Seconds()
event.penalty = event.penalty * math.Exp(delta)
// suppress or recover the event based on its current penalty
if !event.isSuppressed && event.penalty > AdvertSuppress {
event.isSuppressed = true
} else if event.penalty < AdvertRecover {
event.isSuppressed = false
event.isFlapping = false
}
if !event.isFlapping {
for key, event := range eventMap {
if !event.isFlapping && !event.isSuppressed {
e := new(table.Event)
*e = *event.Event
events = append(events, e)
// this deletes the advertised event from the map
delete(eventMap, advert)
delete(eventMap, key)
}
}
mu.Unlock()
Expand All @@ -301,12 +297,6 @@ process:

select {
case r.advertChan <- a:
mu.Lock()
// once we've advertised the events, we need to delete them
for _, event := range a.Events {
delete(eventMap, event.Route.Hash())
}
mu.Unlock()
case <-r.exit:
log.Logf("go advertise(): exit")
return
Expand All @@ -315,7 +305,9 @@ process:
}(events)
}
case e := <-r.eventChan:
// if event is nil, break
// event timestamp
now := time.Now()
// if event is nil, continue
if e == nil {
continue
}
Expand All @@ -324,15 +316,15 @@ process:
var penalty float64
switch e.Type {
case table.Update:
penalty = UpdateRoutePenalty
case table.Create, table.Delete:
penalty = DeleteRoutePenalty
penalty = UpdatePenalty
case table.Delete:
penalty = Delete
}
// we use route hash as eventMap key
hash := e.Route.Hash()
event, ok := eventMap[hash]
if !ok {
event = &advertEvent{
event = &updateEvent{
Event: e,
penalty: penalty,
timestamp: time.Now(),
Expand All @@ -342,8 +334,8 @@ process:
}
// update penalty for existing event: decay existing and add new penalty
delta := time.Since(event.timestamp).Seconds()
event.penalty = event.penalty*math.Exp(delta) + penalty
event.timestamp = time.Now()
event.penalty = event.penalty*math.Exp(-delta) + penalty
event.timestamp = now
// suppress or recover the event based on its current penalty
if !event.isSuppressed && event.penalty > AdvertSuppress {
event.isSuppressed = true
Expand All @@ -352,11 +344,11 @@ process:
}
// if not suppressed decide if if its flapping
if !event.isSuppressed {
// detect if its flapping
event.isFlapping = eventFlap(e, event.Event)
// detect if its flapping by comparing current and previous event
event.isFlapping = isFlapping(e, event.Event)
}
case <-r.exit:
break process
break processLoop
}
}

Expand Down Expand Up @@ -438,8 +430,7 @@ func (r *router) Advertise() (<-chan *Advert, error) {
}
}

// NOTE: we only need to recreate the exit/advertChan if the router errored or was stopped
// TODO: these channels most likely won't have to be the struct fields
// NOTE: we only need to recreate these if the router errored or was stopped
if r.status.Code == Error || r.status.Code == Stopped {
r.exit = make(chan struct{})
r.eventChan = make(chan *table.Event)
Expand Down Expand Up @@ -490,6 +481,9 @@ func (r *router) Advertise() (<-chan *Advert, error) {
r.wg.Add(1)
go r.watchErrors(errChan)

// TODO: send router announcement update comes here
// the announcement update contains routes from routing table

// mark router as running and set its Error to nil
status := Status{
Code: Running,
Expand Down Expand Up @@ -520,7 +514,6 @@ func (r *router) Update(a *Advert) error {
Router: event.Route.Router,
Network: event.Route.Network,
Metric: event.Route.Metric,
Policy: table.Insert,
}
if err := r.opts.Table.Update(route); err != nil {
return fmt.Errorf("failed updating routing table: %v", err)
Expand Down
7 changes: 5 additions & 2 deletions network/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ type Router interface {
Options() Options
// ID returns the ID of the router
ID() string
// Table returns the routing table
Table() table.Table
// Address returns the router adddress
Address() string
// Network returns the network address of the router
Network() string
// Table returns the routing table
Table() table.Table
// Advertise advertises routes to the network
Advertise() (<-chan *Advert, error)
// Update updates the routing table
Expand Down Expand Up @@ -69,6 +69,9 @@ type Advert struct {
ID string
// Timestamp marks the time when the update is sent
Timestamp time.Time
// TTL is Advert TTL
// TODO: not used
TTL time.Time
// Events is a list of routing table events to advertise
Events []*table.Event
}
Expand Down
Loading

0 comments on commit b822454

Please sign in to comment.