2019-06-06 17:37:40 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-01-22 18:33:31 +02:00
|
|
|
"errors"
|
2019-06-10 00:09:38 +02:00
|
|
|
"fmt"
|
2019-07-05 20:15:32 +02:00
|
|
|
"math"
|
2019-07-03 20:50:07 +02:00
|
|
|
"sort"
|
2019-06-10 00:09:38 +02:00
|
|
|
"strings"
|
2019-06-12 00:59:25 +02:00
|
|
|
"sync"
|
2019-06-28 12:53:55 +02:00
|
|
|
"time"
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-07-29 19:57:40 +02:00
|
|
|
"github.com/google/uuid"
|
2020-03-11 19:55:39 +02:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-30 13:39:00 +02:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2019-06-06 17:37:40 +02:00
|
|
|
)
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
var (
|
2019-07-11 13:36:39 +02:00
|
|
|
// AdvertiseEventsTick is time interval in which the router advertises route updates
|
2019-12-05 19:11:17 +02:00
|
|
|
AdvertiseEventsTick = 10 * time.Second
|
2019-11-05 19:44:24 +02:00
|
|
|
// DefaultAdvertTTL is default advertisement TTL
|
2019-12-05 19:50:32 +02:00
|
|
|
DefaultAdvertTTL = 2 * time.Minute
|
2019-07-05 20:15:32 +02:00
|
|
|
// AdvertSuppress is advert suppression threshold
|
2019-11-04 21:01:52 +02:00
|
|
|
AdvertSuppress = 200.0
|
2019-07-08 22:03:54 +02:00
|
|
|
// AdvertRecover is advert recovery threshold
|
2019-11-29 19:27:29 +02:00
|
|
|
AdvertRecover = 20.0
|
2019-11-03 18:29:10 +02:00
|
|
|
// Penalty for routes processed multiple times
|
2019-11-04 21:01:52 +02:00
|
|
|
Penalty = 100.0
|
2019-07-16 20:00:25 +02:00
|
|
|
// PenaltyHalfLife is the time the advert penalty decays to half its value
|
2019-11-04 21:01:52 +02:00
|
|
|
PenaltyHalfLife = 30.0
|
2019-07-16 20:00:25 +02:00
|
|
|
// MaxSuppressTime defines time after which the suppressed advert is deleted
|
2019-12-05 19:11:17 +02:00
|
|
|
MaxSuppressTime = 90 * time.Second
|
2019-07-16 20:00:25 +02:00
|
|
|
// PenaltyDecay is a coefficient which controls the speed the advert penalty decays
|
|
|
|
PenaltyDecay = math.Log(2) / PenaltyHalfLife
|
2019-07-03 20:50:07 +02:00
|
|
|
)
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// router implements default router
|
2019-06-06 17:37:40 +02:00
|
|
|
type router struct {
|
2019-07-29 13:44:28 +02:00
|
|
|
sync.RWMutex
|
2020-01-22 18:33:31 +02:00
|
|
|
|
|
|
|
running bool
|
2019-08-12 19:18:17 +02:00
|
|
|
table *table
|
2020-01-22 18:33:31 +02:00
|
|
|
options Options
|
|
|
|
exit chan bool
|
2019-07-29 19:57:40 +02:00
|
|
|
eventChan chan *Event
|
|
|
|
|
|
|
|
// advert subscribers
|
2019-11-05 19:44:24 +02:00
|
|
|
sub sync.RWMutex
|
2019-07-29 19:57:40 +02:00
|
|
|
subscribers map[string]chan *Advert
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// newRouter creates new router and returns it
|
2019-06-06 17:37:40 +02:00
|
|
|
func newRouter(opts ...Option) Router {
|
2019-06-12 23:30:42 +02:00
|
|
|
// get default options
|
|
|
|
options := DefaultOptions()
|
2019-06-06 17:37:40 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// apply requested options
|
2019-06-06 17:37:40 +02:00
|
|
|
for _, o := range opts {
|
2019-06-10 20:50:54 +02:00
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
return &router{
|
2019-08-28 00:08:35 +02:00
|
|
|
options: options,
|
2019-08-12 19:18:17 +02:00
|
|
|
table: newTable(),
|
2019-07-29 19:57:40 +02:00
|
|
|
subscribers: make(map[string]chan *Advert),
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// Init initializes router with given options
|
2019-06-06 17:37:40 +02:00
|
|
|
func (r *router) Init(opts ...Option) error {
|
2019-08-12 19:18:17 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-06-06 17:37:40 +02:00
|
|
|
for _, o := range opts {
|
2019-08-28 00:08:35 +02:00
|
|
|
o(&r.options)
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
2019-08-12 19:18:17 +02:00
|
|
|
|
2019-06-06 17:37:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// Options returns router options
|
2019-06-06 17:37:40 +02:00
|
|
|
func (r *router) Options() Options {
|
2020-01-17 18:25:18 +02:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
options := r.options
|
2019-08-12 19:18:17 +02:00
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
return options
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// Table returns routing table
|
2019-07-29 19:57:40 +02:00
|
|
|
func (r *router) Table() Table {
|
|
|
|
return r.table
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// manageRoute applies action on a given route
|
2019-07-26 00:19:05 +02:00
|
|
|
func (r *router) manageRoute(route Route, action string) error {
|
2019-07-11 13:36:39 +02:00
|
|
|
switch action {
|
|
|
|
case "create":
|
2019-07-29 19:57:40 +02:00
|
|
|
if err := r.table.Create(route); err != nil && err != ErrDuplicateRoute {
|
2019-07-11 13:36:39 +02:00
|
|
|
return fmt.Errorf("failed adding route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "delete":
|
2019-07-29 19:57:40 +02:00
|
|
|
if err := r.table.Delete(route); err != nil && err != ErrRouteNotFound {
|
2019-07-11 13:36:39 +02:00
|
|
|
return fmt.Errorf("failed deleting route for service %s: %s", route.Service, err)
|
|
|
|
}
|
2019-09-26 12:56:30 +02:00
|
|
|
case "update":
|
|
|
|
if err := r.table.Update(route); err != nil {
|
|
|
|
return fmt.Errorf("failed updating route for service %s: %s", route.Service, err)
|
|
|
|
}
|
2019-07-11 13:36:39 +02:00
|
|
|
default:
|
2019-09-26 12:56:30 +02:00
|
|
|
return fmt.Errorf("failed to manage route for service %s: unknown action %s", route.Service, action)
|
2019-07-11 13:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:22:27 +02:00
|
|
|
// manageServiceRoutes applies action to all routes of the service.
|
2019-07-24 18:16:52 +02:00
|
|
|
// It returns error of the action fails with error.
|
2020-01-22 18:33:31 +02:00
|
|
|
func (r *router) manageRoutes(service *registry.Service, action string) error {
|
2019-07-08 17:16:50 +02:00
|
|
|
// action is the routing table action
|
|
|
|
action = strings.ToLower(action)
|
2019-07-10 22:28:32 +02:00
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
// take route action on each service node
|
|
|
|
for _, node := range service.Nodes {
|
2019-07-26 00:19:05 +02:00
|
|
|
route := Route{
|
2019-07-09 16:45:42 +02:00
|
|
|
Service: service.Name,
|
|
|
|
Address: node.Address,
|
|
|
|
Gateway: "",
|
2019-08-28 00:08:35 +02:00
|
|
|
Network: r.options.Network,
|
|
|
|
Router: r.options.Id,
|
2019-07-26 00:19:05 +02:00
|
|
|
Link: DefaultLink,
|
|
|
|
Metric: DefaultLocalMetric,
|
2019-07-08 17:16:50 +02:00
|
|
|
}
|
2019-07-10 22:28:32 +02:00
|
|
|
|
2019-07-11 13:36:39 +02:00
|
|
|
if err := r.manageRoute(route, action); err != nil {
|
|
|
|
return err
|
2019-07-08 17:16:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 22:28:32 +02:00
|
|
|
|
2019-07-08 17:16:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:22:27 +02:00
|
|
|
// manageRegistryRoutes applies action to all routes of each service found in the registry.
|
2019-07-08 22:03:54 +02:00
|
|
|
// It returns error if either the services failed to be listed or the routing table action fails.
|
2019-07-09 13:46:15 +02:00
|
|
|
func (r *router) manageRegistryRoutes(reg registry.Registry, action string) error {
|
2019-06-12 23:30:42 +02:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
2019-07-03 20:50:07 +02:00
|
|
|
return fmt.Errorf("failed listing services: %v", err)
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
// add each service node as a separate route
|
2019-06-12 23:30:42 +02:00
|
|
|
for _, service := range services {
|
2019-06-27 15:37:52 +02:00
|
|
|
// get the service to retrieve all its info
|
|
|
|
srvs, err := reg.GetService(service.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-08 17:51:55 +02:00
|
|
|
// manage the routes for all returned services
|
2019-07-11 13:36:39 +02:00
|
|
|
for _, srv := range srvs {
|
2020-01-22 18:33:31 +02:00
|
|
|
if err := r.manageRoutes(srv, action); err != nil {
|
2019-07-08 17:16:50 +02:00
|
|
|
return err
|
2019-06-19 19:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 17:03:19 +02:00
|
|
|
|
|
|
|
return nil
|
2019-06-19 19:01:48 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:06:11 +02:00
|
|
|
// watchRegistry watches registry and updates routing table based on the received events.
|
2019-07-16 20:00:25 +02:00
|
|
|
// It returns error if either the registry watcher fails with error or if the routing table update fails.
|
|
|
|
func (r *router) watchRegistry(w registry.Watcher) error {
|
2019-08-02 16:17:48 +02:00
|
|
|
exit := make(chan bool)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(exit)
|
|
|
|
}()
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
go func() {
|
2019-08-02 16:17:48 +02:00
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
return
|
2020-01-22 18:33:31 +02:00
|
|
|
case <-r.exit:
|
|
|
|
return
|
2019-08-02 16:17:48 +02:00
|
|
|
}
|
2019-06-12 00:59:25 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-01 16:43:50 +02:00
|
|
|
if err != registry.ErrWatcherStopped {
|
2020-01-22 18:33:31 +02:00
|
|
|
return err
|
2019-07-01 16:43:50 +02:00
|
|
|
}
|
2019-06-12 00:59:25 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
if err := r.manageRoutes(res.Service, res.Action); err != nil {
|
2019-07-08 17:16:50 +02:00
|
|
|
return err
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
return nil
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
// watchTable watches routing table entries and either adds or deletes locally registered service to/from network registry
|
|
|
|
// It returns error if the locally registered services either fails to be added/deleted to/from network registry.
|
2019-07-26 00:19:05 +02:00
|
|
|
func (r *router) watchTable(w Watcher) error {
|
2019-08-02 16:17:48 +02:00
|
|
|
exit := make(chan bool)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(exit)
|
|
|
|
}()
|
|
|
|
|
2019-08-21 22:10:42 +02:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
2019-06-27 23:52:51 +02:00
|
|
|
go func() {
|
2019-08-02 16:17:48 +02:00
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
case <-exit:
|
|
|
|
return
|
|
|
|
}
|
2019-06-27 23:52:51 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-26 00:19:05 +02:00
|
|
|
if err != ErrWatcherStopped {
|
2020-01-22 18:33:31 +02:00
|
|
|
return err
|
2019-07-01 16:43:50 +02:00
|
|
|
}
|
2019-06-27 23:52:51 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-08 17:51:55 +02:00
|
|
|
|
2019-07-05 20:15:32 +02:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
close(r.eventChan)
|
|
|
|
return nil
|
|
|
|
case r.eventChan <- event:
|
2020-01-22 18:33:31 +02:00
|
|
|
// process event
|
2019-06-28 12:53:55 +02:00
|
|
|
}
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
return nil
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// publishAdvert publishes router advert to advert channel
|
2019-07-26 00:19:05 +02:00
|
|
|
func (r *router) publishAdvert(advType AdvertType, events []*Event) {
|
2019-07-09 13:46:15 +02:00
|
|
|
a := &Advert{
|
2019-08-28 00:08:35 +02:00
|
|
|
Id: r.options.Id,
|
2019-07-09 13:46:15 +02:00
|
|
|
Type: advType,
|
2019-07-11 13:36:39 +02:00
|
|
|
TTL: DefaultAdvertTTL,
|
2019-07-09 13:46:15 +02:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
r.sub.RLock()
|
2019-07-29 19:57:40 +02:00
|
|
|
for _, sub := range r.subscribers {
|
|
|
|
// now send the message
|
|
|
|
select {
|
|
|
|
case sub <- a:
|
2019-11-05 19:44:24 +02:00
|
|
|
case <-r.exit:
|
|
|
|
r.sub.RUnlock()
|
|
|
|
return
|
2019-07-29 19:57:40 +02:00
|
|
|
}
|
2019-07-09 13:46:15 +02:00
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
r.sub.RUnlock()
|
2019-07-11 13:36:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
// advert contains a route event to be advertised
|
|
|
|
type advert struct {
|
2019-09-26 18:51:57 +02:00
|
|
|
// event received from routing table
|
|
|
|
event *Event
|
2019-11-05 19:44:24 +02:00
|
|
|
// lastSeen records the time of the last advert update
|
|
|
|
lastSeen time.Time
|
2019-07-16 20:00:25 +02:00
|
|
|
// penalty is current advert penalty
|
2019-07-08 22:03:54 +02:00
|
|
|
penalty float64
|
2019-07-16 20:00:25 +02:00
|
|
|
// isSuppressed flags the advert suppression
|
2019-07-08 22:03:54 +02:00
|
|
|
isSuppressed bool
|
2019-07-16 20:00:25 +02:00
|
|
|
// suppressTime records the time interval the advert has been suppressed for
|
|
|
|
suppressTime time.Time
|
2019-07-08 22:03:54 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
// adverts maintains a map of router adverts
|
|
|
|
type adverts map[uint64]*advert
|
|
|
|
|
|
|
|
// process processes advert
|
|
|
|
// It updates advert timestamp, increments its penalty and
|
|
|
|
// marks upresses or recovers it if it reaches configured thresholds
|
|
|
|
func (m adverts) process(a *advert) error {
|
|
|
|
// lookup advert in adverts
|
|
|
|
hash := a.event.Route.Hash()
|
|
|
|
a, ok := m[hash]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("advert not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// decay the event penalty
|
|
|
|
delta := time.Since(a.lastSeen).Seconds()
|
|
|
|
|
|
|
|
// decay advert penalty
|
|
|
|
a.penalty = a.penalty * math.Exp(-delta*PenaltyDecay)
|
|
|
|
service := a.event.Route.Service
|
|
|
|
address := a.event.Route.Address
|
|
|
|
|
|
|
|
// suppress/recover the event based on its penalty level
|
|
|
|
switch {
|
|
|
|
case a.penalty > AdvertSuppress && !a.isSuppressed:
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router suppressing advert %d %.2f for route %s %s", hash, a.penalty, service, address)
|
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
a.isSuppressed = true
|
|
|
|
a.suppressTime = time.Now()
|
|
|
|
case a.penalty < AdvertRecover && a.isSuppressed:
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router recovering advert %d %.2f for route %s %s", hash, a.penalty, service, address)
|
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
a.isSuppressed = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if suppressed, checked how long has it been suppressed for
|
|
|
|
if a.isSuppressed {
|
|
|
|
// max suppression time threshold has been reached, delete the advert
|
|
|
|
if time.Since(a.suppressTime) > MaxSuppressTime {
|
|
|
|
delete(m, hash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// advertiseEvents advertises routing table events
|
2019-07-05 20:15:32 +02:00
|
|
|
// It suppresses unhealthy flapping events and advertises healthy events upstream.
|
2019-07-24 18:16:52 +02:00
|
|
|
func (r *router) advertiseEvents() error {
|
2019-07-05 20:15:32 +02:00
|
|
|
// ticker to periodically scan event for advertising
|
2019-07-11 13:36:39 +02:00
|
|
|
ticker := time.NewTicker(AdvertiseEventsTick)
|
2019-08-02 15:44:11 +02:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
// adverts is a map of advert events
|
|
|
|
adverts := make(adverts)
|
2019-07-05 20:15:32 +02:00
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// routing table watcher
|
2020-01-22 18:33:31 +02:00
|
|
|
w, err := r.Watch()
|
2019-07-24 18:16:52 +02:00
|
|
|
if err != nil {
|
2020-01-22 18:33:31 +02:00
|
|
|
return err
|
2019-07-24 18:16:52 +02:00
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
defer w.Stop()
|
2019-07-24 18:16:52 +02:00
|
|
|
|
|
|
|
go func() {
|
2020-01-22 18:33:31 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if w == nil {
|
|
|
|
// routing table watcher
|
|
|
|
w, err = r.Watch()
|
|
|
|
if err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Errorf("Error creating watcher: %v", err)
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.watchTable(w); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Errorf("Error watching table: %v", err)
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2020-02-25 01:16:00 +02:00
|
|
|
if w != nil {
|
|
|
|
// reset
|
|
|
|
w.Stop()
|
|
|
|
w = nil
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
}
|
2019-07-24 18:16:52 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-05 20:15:32 +02:00
|
|
|
for {
|
2019-06-28 12:53:55 +02:00
|
|
|
select {
|
2019-07-05 20:15:32 +02:00
|
|
|
case <-ticker.C:
|
2019-11-11 17:28:37 +02:00
|
|
|
// If we're not advertising any events then sip processing them entirely
|
|
|
|
if r.options.Advertise == AdvertiseNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-26 00:19:05 +02:00
|
|
|
var events []*Event
|
2019-11-11 17:28:37 +02:00
|
|
|
|
2019-07-08 22:03:54 +02:00
|
|
|
// collect all events which are not flapping
|
2019-11-05 19:44:24 +02:00
|
|
|
for key, advert := range adverts {
|
|
|
|
// process the advert
|
|
|
|
if err := adverts.process(advert); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router failed processing advert %d: %v", key, err)
|
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
continue
|
2019-07-16 20:00:25 +02:00
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
// if suppressed go to the next advert
|
2019-07-16 20:00:25 +02:00
|
|
|
if advert.isSuppressed {
|
2019-11-04 21:01:52 +02:00
|
|
|
continue
|
2019-07-16 20:00:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-11 17:28:37 +02:00
|
|
|
// if we only advertise local routes skip processing anything not link local
|
|
|
|
if r.options.Advertise == AdvertiseLocal && advert.event.Route.Link != "local" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-04 21:01:52 +02:00
|
|
|
// copy the event and append
|
|
|
|
e := new(Event)
|
|
|
|
// this is ok, because router.Event only contains builtin types
|
2019-11-05 19:44:24 +02:00
|
|
|
// and no references so this creates a deep copy of struct Event
|
2019-11-04 21:01:52 +02:00
|
|
|
*e = *(advert.event)
|
|
|
|
events = append(events, e)
|
2019-11-05 19:44:24 +02:00
|
|
|
// delete the advert from adverts
|
|
|
|
delete(adverts, key)
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
// advertise events to subscribers
|
2019-07-05 20:15:32 +02:00
|
|
|
if len(events) > 0 {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router publishing %d events", len(events))
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
go r.publishAdvert(RouteUpdate, events)
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
|
|
|
case e := <-r.eventChan:
|
2019-07-08 22:03:54 +02:00
|
|
|
// if event is nil, continue
|
2019-07-05 20:15:32 +02:00
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-11 17:28:37 +02:00
|
|
|
|
|
|
|
// If we're not advertising any events then skip processing them entirely
|
|
|
|
if r.options.Advertise == AdvertiseNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we only advertise local routes skip processing anything not link local
|
|
|
|
if r.options.Advertise == AdvertiseLocal && e.Route.Link != "local" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
now := time.Now()
|
2019-11-03 18:29:10 +02:00
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router processing table event %s for service %s %s", e.Type, e.Route.Service, e.Route.Address)
|
|
|
|
}
|
2019-07-16 20:00:25 +02:00
|
|
|
|
|
|
|
// check if we have already registered the route
|
2019-07-05 20:15:32 +02:00
|
|
|
hash := e.Route.Hash()
|
2019-11-05 19:44:24 +02:00
|
|
|
a, ok := adverts[hash]
|
2019-07-05 20:15:32 +02:00
|
|
|
if !ok {
|
2019-11-05 19:44:24 +02:00
|
|
|
a = &advert{
|
|
|
|
event: e,
|
|
|
|
penalty: Penalty,
|
|
|
|
lastSeen: now,
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
adverts[hash] = a
|
2019-07-05 20:15:32 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-07-16 20:00:25 +02:00
|
|
|
|
2019-11-05 19:44:24 +02:00
|
|
|
// override the route event only if the previous event was different
|
|
|
|
if a.event.Type != e.Type {
|
|
|
|
a.event = e
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the advert
|
|
|
|
if err := adverts.process(a); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router error processing advert %d: %v", hash, err)
|
|
|
|
}
|
2019-11-05 19:44:24 +02:00
|
|
|
continue
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
2019-07-16 20:00:25 +02:00
|
|
|
|
2019-09-26 18:51:57 +02:00
|
|
|
// update event penalty and timestamp
|
2019-11-05 19:44:24 +02:00
|
|
|
a.lastSeen = now
|
|
|
|
// increment the penalty
|
|
|
|
a.penalty += Penalty
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router advert %d for route %s %s event penalty: %f", hash, a.event.Route.Service, a.event.Route.Address, a.penalty)
|
|
|
|
}
|
2019-06-28 12:53:55 +02:00
|
|
|
case <-r.exit:
|
2020-02-25 01:16:00 +02:00
|
|
|
if w != nil {
|
|
|
|
w.Stop()
|
|
|
|
}
|
2019-07-09 13:46:15 +02:00
|
|
|
return nil
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// drain all the events, only called on Stop
|
|
|
|
func (r *router) drain() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.eventChan:
|
|
|
|
default:
|
|
|
|
return
|
2019-07-05 20:15:32 +02:00
|
|
|
}
|
2019-07-24 18:16:52 +02:00
|
|
|
}
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// Start starts the router
|
|
|
|
func (r *router) Start() error {
|
2019-06-29 01:46:22 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
if r.running {
|
2019-08-21 19:58:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// add all local service routes into the routing table
|
2019-08-28 00:08:35 +02:00
|
|
|
if err := r.manageRegistryRoutes(r.options.Registry, "create"); err != nil {
|
2020-01-22 18:33:31 +02:00
|
|
|
return fmt.Errorf("failed adding registry routes: %s", err)
|
2019-08-12 19:18:17 +02:00
|
|
|
}
|
2019-07-09 16:01:52 +02:00
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// add default gateway into routing table
|
2019-08-28 00:08:35 +02:00
|
|
|
if r.options.Gateway != "" {
|
2019-08-12 19:18:17 +02:00
|
|
|
// note, the only non-default value is the gateway
|
|
|
|
route := Route{
|
|
|
|
Service: "*",
|
|
|
|
Address: "*",
|
2019-08-28 00:08:35 +02:00
|
|
|
Gateway: r.options.Gateway,
|
2019-08-12 19:18:17 +02:00
|
|
|
Network: "*",
|
2019-08-28 00:08:35 +02:00
|
|
|
Router: r.options.Id,
|
|
|
|
Link: DefaultLink,
|
2019-08-12 19:18:17 +02:00
|
|
|
Metric: DefaultLocalMetric,
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
2019-08-12 19:18:17 +02:00
|
|
|
if err := r.table.Create(route); err != nil {
|
2020-01-22 18:33:31 +02:00
|
|
|
return fmt.Errorf("failed adding default gateway route: %s", err)
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
2019-08-12 19:18:17 +02:00
|
|
|
}
|
2019-06-29 01:46:22 +02:00
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// create error and exit channels
|
2020-01-22 18:33:31 +02:00
|
|
|
r.exit = make(chan bool)
|
2019-06-29 01:46:22 +02:00
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
// registry watcher
|
2020-01-22 18:33:31 +02:00
|
|
|
w, err := r.options.Registry.Watch()
|
2019-08-12 19:18:17 +02:00
|
|
|
if err != nil {
|
2020-01-22 18:33:31 +02:00
|
|
|
return fmt.Errorf("failed creating registry watcher: %v", err)
|
2019-08-12 19:18:17 +02:00
|
|
|
}
|
2019-07-05 20:15:32 +02:00
|
|
|
|
2019-08-12 19:18:17 +02:00
|
|
|
go func() {
|
2020-01-22 18:33:31 +02:00
|
|
|
var err error
|
2019-07-24 18:16:52 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
2020-02-10 17:38:41 +02:00
|
|
|
if w != nil {
|
|
|
|
w.Stop()
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
if w == nil {
|
|
|
|
w, err = r.options.Registry.Watch()
|
|
|
|
if err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Errorf("failed creating registry watcher: %v", err)
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.watchRegistry(w); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Errorf("Error watching the registry: %v", err)
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:38:41 +02:00
|
|
|
if w != nil {
|
|
|
|
w.Stop()
|
|
|
|
w = nil
|
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 19:18:17 +02:00
|
|
|
}()
|
2019-07-24 18:16:52 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
r.running = true
|
2019-08-12 19:18:17 +02:00
|
|
|
|
|
|
|
return nil
|
2019-07-24 18:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advertise stars advertising the routes to the network and returns the advertisements channel to consume from.
|
|
|
|
// If the router is already advertising it returns the channel to consume from.
|
|
|
|
// It returns error if either the router is not running or if the routing table fails to list the routes to advertise.
|
|
|
|
func (r *router) Advertise() (<-chan *Advert, error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
if !r.running {
|
|
|
|
return nil, errors.New("not running")
|
|
|
|
}
|
|
|
|
|
|
|
|
// already advertising
|
|
|
|
if r.eventChan != nil {
|
2019-09-05 20:05:47 +02:00
|
|
|
advertChan := make(chan *Advert, 128)
|
2019-07-29 19:57:40 +02:00
|
|
|
r.subscribers[uuid.New().String()] = advertChan
|
|
|
|
return advertChan, nil
|
2020-01-22 18:33:31 +02:00
|
|
|
}
|
2019-07-24 18:16:52 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// list all the routes and pack them into even slice to advertise
|
|
|
|
events, err := r.flushRouteEvents(Create)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to flush routes: %s", err)
|
|
|
|
}
|
2019-07-24 18:16:52 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// create event channels
|
|
|
|
r.eventChan = make(chan *Event)
|
2019-11-05 19:44:24 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// create advert channel
|
|
|
|
advertChan := make(chan *Advert, 128)
|
|
|
|
r.subscribers[uuid.New().String()] = advertChan
|
2019-07-24 18:16:52 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// advertise your presence
|
|
|
|
go r.publishAdvert(Announce, events)
|
2019-06-29 01:46:22 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if err := r.advertiseEvents(); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
logger.Errorf("Error adveritising events: %v", err)
|
|
|
|
}
|
2019-07-24 18:16:52 +02:00
|
|
|
}
|
2020-01-22 18:33:31 +02:00
|
|
|
}
|
|
|
|
}()
|
2019-07-01 16:43:50 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
return advertChan, nil
|
2019-07-08 22:03:54 +02:00
|
|
|
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:45:27 +02:00
|
|
|
// Process updates the routing table using the advertised values
|
|
|
|
func (r *router) Process(a *Advert) error {
|
2019-07-03 20:50:07 +02:00
|
|
|
// NOTE: event sorting might not be necessary
|
|
|
|
// copy update events intp new slices
|
2019-07-26 00:19:05 +02:00
|
|
|
events := make([]*Event, len(a.Events))
|
2019-07-04 03:06:59 +02:00
|
|
|
copy(events, a.Events)
|
2019-07-03 20:50:07 +02:00
|
|
|
// sort events by timestamp
|
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
|
|
return events[i].Timestamp.Before(events[j].Timestamp)
|
|
|
|
})
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Router %s processing advert from: %s", r.options.Id, a.Id)
|
|
|
|
}
|
2019-09-26 13:07:26 +02:00
|
|
|
|
2019-07-03 20:50:07 +02:00
|
|
|
for _, event := range events {
|
2019-08-28 00:08:35 +02:00
|
|
|
// skip if the router is the origin of this route
|
|
|
|
if event.Route.Router == r.options.Id {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Router skipping processing its own route: %s", r.options.Id)
|
|
|
|
}
|
2019-08-28 00:08:35 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-07-09 16:45:42 +02:00
|
|
|
// create a copy of the route
|
|
|
|
route := event.Route
|
2019-07-11 13:36:39 +02:00
|
|
|
action := event.Type
|
2019-12-09 11:32:17 +02:00
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("Router %s applying %s from router %s for service %s %s", r.options.Id, action, route.Router, route.Service, route.Address)
|
|
|
|
}
|
2019-12-09 11:32:17 +02:00
|
|
|
|
2019-12-03 09:25:58 +02:00
|
|
|
if err := r.manageRoute(route, action.String()); err != nil {
|
2019-07-11 13:36:39 +02:00
|
|
|
return fmt.Errorf("failed applying action %s to routing table: %s", action, err)
|
2019-07-03 20:50:07 +02:00
|
|
|
}
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:50:07 +02:00
|
|
|
return nil
|
2019-06-29 01:46:22 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 17:04:44 +02:00
|
|
|
// flushRouteEvents returns a slice of events, one per each route in the routing table
|
|
|
|
func (r *router) flushRouteEvents(evType EventType) ([]*Event, error) {
|
2020-01-17 18:25:18 +02:00
|
|
|
// get a list of routes for each service in our routing table
|
|
|
|
// for the configured advertising strategy
|
|
|
|
q := []QueryOption{
|
|
|
|
QueryStrategy(r.options.Advertise),
|
2019-10-09 18:23:02 +02:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:25:18 +02:00
|
|
|
routes, err := r.Table().Query(q...)
|
|
|
|
if err != nil && err != ErrRouteNotFound {
|
|
|
|
return nil, err
|
2019-10-09 18:23:02 +02:00
|
|
|
}
|
2019-10-09 17:03:06 +02:00
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Router advertising %d routes with strategy %s", len(routes), r.options.Advertise)
|
|
|
|
}
|
2019-10-09 20:08:24 +02:00
|
|
|
|
2019-09-05 14:23:33 +02:00
|
|
|
// build a list of events to advertise
|
2020-01-17 18:25:18 +02:00
|
|
|
events := make([]*Event, len(routes))
|
2019-12-07 21:54:29 +02:00
|
|
|
var i int
|
|
|
|
|
2020-01-17 18:25:18 +02:00
|
|
|
for _, route := range routes {
|
2019-09-05 14:23:33 +02:00
|
|
|
event := &Event{
|
2019-09-05 17:04:44 +02:00
|
|
|
Type: evType,
|
2019-09-05 14:23:33 +02:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events[i] = event
|
2019-10-09 18:23:02 +02:00
|
|
|
i++
|
2019-09-05 14:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:10:42 +02:00
|
|
|
// Lookup routes in the routing table
|
2019-10-09 18:13:52 +02:00
|
|
|
func (r *router) Lookup(q ...QueryOption) ([]Route, error) {
|
|
|
|
return r.table.Query(q...)
|
2019-07-29 19:57:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:10:42 +02:00
|
|
|
// Watch routes
|
2019-07-29 19:57:40 +02:00
|
|
|
func (r *router) Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
return r.table.Watch(opts...)
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-07-24 18:16:52 +02:00
|
|
|
r.Lock()
|
2020-01-22 18:33:31 +02:00
|
|
|
defer r.Unlock()
|
2019-11-05 19:44:24 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
default:
|
2019-12-03 17:22:54 +02:00
|
|
|
close(r.exit)
|
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// extract the events
|
|
|
|
r.drain()
|
2019-11-05 19:44:24 +02:00
|
|
|
|
2020-02-25 01:16:00 +02:00
|
|
|
r.sub.Lock()
|
2020-01-22 18:33:31 +02:00
|
|
|
// close advert subscribers
|
|
|
|
for id, sub := range r.subscribers {
|
|
|
|
// close the channel
|
|
|
|
close(sub)
|
|
|
|
// delete the subscriber
|
|
|
|
delete(r.subscribers, id)
|
|
|
|
}
|
2020-02-25 01:16:00 +02:00
|
|
|
r.sub.Unlock()
|
2020-01-22 18:33:31 +02:00
|
|
|
}
|
2019-06-19 19:01:48 +02:00
|
|
|
|
2020-01-22 18:33:31 +02:00
|
|
|
// remove event chan
|
|
|
|
r.eventChan = nil
|
2019-11-05 19:44:24 +02:00
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// String prints debugging information about router
|
2019-07-10 08:45:27 +02:00
|
|
|
func (r *router) String() string {
|
2020-01-22 18:33:31 +02:00
|
|
|
return "registry"
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|