2019-06-06 16:37:40 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-09 23:09:38 +01:00
|
|
|
"fmt"
|
2019-07-05 19:15:32 +01:00
|
|
|
"math"
|
2019-07-03 19:50:07 +01:00
|
|
|
"sort"
|
2019-06-09 23:09:38 +01:00
|
|
|
"strings"
|
2019-06-11 23:59:25 +01:00
|
|
|
"sync"
|
2019-06-28 11:53:55 +01:00
|
|
|
"time"
|
2019-06-09 23:09:38 +01:00
|
|
|
|
2019-06-06 16:37:40 +01:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
2019-07-03 19:50:07 +01:00
|
|
|
const (
|
2019-07-11 12:36:39 +01:00
|
|
|
// AdvertiseEventsTick is time interval in which the router advertises route updates
|
|
|
|
AdvertiseEventsTick = 5 * time.Second
|
|
|
|
// AdvertiseTableTick is time interval in which router advertises all routes found in routing table
|
|
|
|
AdvertiseTableTick = 1 * time.Minute
|
2019-07-24 17:16:52 +01:00
|
|
|
// AdvertiseFlushTick is time the yet unconsumed advertisements are flush i.e. discarded
|
|
|
|
AdvertiseFlushTick = 15 * time.Second
|
2019-07-05 19:15:32 +01:00
|
|
|
// AdvertSuppress is advert suppression threshold
|
2019-07-16 19:00:25 +01:00
|
|
|
AdvertSuppress = 2000.0
|
2019-07-08 21:03:54 +01:00
|
|
|
// AdvertRecover is advert recovery threshold
|
2019-07-16 19:00:25 +01:00
|
|
|
AdvertRecover = 750.0
|
2019-07-08 21:03:54 +01:00
|
|
|
// DefaultAdvertTTL is default advertisement TTL
|
2019-07-11 12:36:39 +01:00
|
|
|
DefaultAdvertTTL = 1 * time.Minute
|
2019-07-16 19:00:25 +01:00
|
|
|
// DeletePenalty penalises route deletion
|
|
|
|
DeletePenalty = 1000.0
|
2019-07-08 21:03:54 +01:00
|
|
|
// UpdatePenalty penalises route updates
|
2019-07-16 19:00:25 +01:00
|
|
|
UpdatePenalty = 500.0
|
|
|
|
// PenaltyHalfLife is the time the advert penalty decays to half its value
|
|
|
|
PenaltyHalfLife = 2.0
|
|
|
|
// MaxSuppressTime defines time after which the suppressed advert is deleted
|
|
|
|
MaxSuppressTime = 5 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// PenaltyDecay is a coefficient which controls the speed the advert penalty decays
|
|
|
|
PenaltyDecay = math.Log(2) / PenaltyHalfLife
|
2019-07-03 19:50:07 +01:00
|
|
|
)
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// router implements default router
|
2019-06-06 16:37:40 +01:00
|
|
|
type router struct {
|
2019-07-10 07:45:27 +01:00
|
|
|
// embed the table
|
2019-07-25 23:19:05 +01:00
|
|
|
*Table
|
2019-06-27 22:52:51 +01:00
|
|
|
opts Options
|
2019-06-28 18:35:53 +01:00
|
|
|
status Status
|
2019-06-27 22:52:51 +01:00
|
|
|
exit chan struct{}
|
2019-07-24 17:16:52 +01:00
|
|
|
errChan chan error
|
2019-07-25 23:19:05 +01:00
|
|
|
eventChan chan *Event
|
2019-07-05 19:15:32 +01:00
|
|
|
advertChan chan *Advert
|
2019-07-09 12:46:15 +01:00
|
|
|
advertWg *sync.WaitGroup
|
2019-06-27 22:52:51 +01:00
|
|
|
wg *sync.WaitGroup
|
2019-06-28 18:35:53 +01:00
|
|
|
sync.RWMutex
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// newRouter creates new router and returns it
|
2019-06-06 16:37:40 +01:00
|
|
|
func newRouter(opts ...Option) Router {
|
2019-06-12 22:30:42 +01:00
|
|
|
// get default options
|
|
|
|
options := DefaultOptions()
|
2019-06-06 16:37:40 +01:00
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
// apply requested options
|
2019-06-06 16:37:40 +01:00
|
|
|
for _, o := range opts {
|
2019-06-10 19:50:54 +01:00
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
r := &router{
|
|
|
|
Table: options.Table,
|
|
|
|
opts: options,
|
|
|
|
status: Status{Code: Stopped, Error: nil},
|
|
|
|
advertWg: &sync.WaitGroup{},
|
|
|
|
wg: &sync.WaitGroup{},
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|
2019-07-24 17:16:52 +01:00
|
|
|
|
|
|
|
go r.run()
|
|
|
|
|
|
|
|
return r
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 23:29:24 +01:00
|
|
|
// Init initializes router with given options
|
2019-06-06 16:37:40 +01:00
|
|
|
func (r *router) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-06 23:29:24 +01:00
|
|
|
// Options returns router options
|
2019-06-06 16:37:40 +01:00
|
|
|
func (r *router) Options() Options {
|
|
|
|
return r.opts
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// manageRoute applies action on a given route
|
2019-07-25 23:19:05 +01:00
|
|
|
func (r *router) manageRoute(route Route, action string) error {
|
2019-07-11 12:36:39 +01:00
|
|
|
switch action {
|
|
|
|
case "create":
|
2019-07-25 23:19:05 +01:00
|
|
|
if err := r.Create(route); err != nil && err != ErrDuplicateRoute {
|
2019-07-11 12:36:39 +01:00
|
|
|
return fmt.Errorf("failed adding route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "update":
|
2019-07-25 23:19:05 +01:00
|
|
|
if err := r.Update(route); err != nil && err != ErrDuplicateRoute {
|
2019-07-11 12:36:39 +01:00
|
|
|
return fmt.Errorf("failed updating route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
case "delete":
|
2019-07-25 23:19:05 +01:00
|
|
|
if err := r.Delete(route); err != nil && err != ErrRouteNotFound {
|
2019-07-11 12:36:39 +01:00
|
|
|
return fmt.Errorf("failed deleting route for service %s: %s", route.Service, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to manage route for service %s. Unknown action: %s", route.Service, action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:22:27 +01:00
|
|
|
// manageServiceRoutes applies action to all routes of the service.
|
2019-07-24 17:16:52 +01:00
|
|
|
// It returns error of the action fails with error.
|
2019-07-09 12:46:15 +01:00
|
|
|
func (r *router) manageServiceRoutes(service *registry.Service, action string) error {
|
2019-07-08 16:16:50 +01:00
|
|
|
// action is the routing table action
|
|
|
|
action = strings.ToLower(action)
|
2019-07-10 21:28:32 +01:00
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
// take route action on each service node
|
|
|
|
for _, node := range service.Nodes {
|
2019-07-25 23:19:05 +01:00
|
|
|
route := Route{
|
2019-07-09 15:45:42 +01:00
|
|
|
Service: service.Name,
|
|
|
|
Address: node.Address,
|
|
|
|
Gateway: "",
|
|
|
|
Network: r.opts.Network,
|
2019-07-25 23:19:05 +01:00
|
|
|
Link: DefaultLink,
|
|
|
|
Metric: DefaultLocalMetric,
|
2019-07-08 16:16:50 +01:00
|
|
|
}
|
2019-07-10 21:28:32 +01:00
|
|
|
|
2019-07-11 12:36:39 +01:00
|
|
|
if err := r.manageRoute(route, action); err != nil {
|
|
|
|
return err
|
2019-07-08 16:16:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 21:28:32 +01:00
|
|
|
|
2019-07-08 16:16:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:22:27 +01:00
|
|
|
// manageRegistryRoutes applies action to all routes of each service found in the registry.
|
2019-07-08 21:03:54 +01:00
|
|
|
// It returns error if either the services failed to be listed or the routing table action fails.
|
2019-07-09 12:46:15 +01:00
|
|
|
func (r *router) manageRegistryRoutes(reg registry.Registry, action string) error {
|
2019-06-12 22:30:42 +01:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
2019-07-03 19:50:07 +01:00
|
|
|
return fmt.Errorf("failed listing services: %v", err)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 22:52:51 +01:00
|
|
|
// add each service node as a separate route
|
2019-06-12 22:30:42 +01:00
|
|
|
for _, service := range services {
|
2019-06-27 14:37:52 +01:00
|
|
|
// get the service to retrieve all its info
|
|
|
|
srvs, err := reg.GetService(service.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-08 16:51:55 +01:00
|
|
|
// manage the routes for all returned services
|
2019-07-11 12:36:39 +01:00
|
|
|
for _, srv := range srvs {
|
|
|
|
if err := r.manageServiceRoutes(srv, action); err != nil {
|
2019-07-08 16:16:50 +01:00
|
|
|
return err
|
2019-06-19 18:01:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 16:03:19 +01:00
|
|
|
|
|
|
|
return nil
|
2019-06-19 18:01:48 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 00:06:11 +01:00
|
|
|
// watchRegistry watches registry and updates routing table based on the received events.
|
2019-07-16 19:00:25 +01: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-06-13 15:12:07 +01:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
2019-06-11 23:59:25 +01:00
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-01 15:43:50 +01:00
|
|
|
if err != registry.ErrWatcherStopped {
|
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-11 23:59:25 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-07-09 12:46:15 +01:00
|
|
|
if err := r.manageServiceRoutes(res.Service, res.Action); err != nil {
|
2019-07-08 16:16:50 +01:00
|
|
|
return err
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:52:51 +01: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-25 23:19:05 +01:00
|
|
|
func (r *router) watchTable(w Watcher) error {
|
2019-06-27 22:52:51 +01:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
<-r.exit
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var watchErr error
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := w.Next()
|
|
|
|
if err != nil {
|
2019-07-25 23:19:05 +01:00
|
|
|
if err != ErrWatcherStopped {
|
2019-07-01 15:43:50 +01:00
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-27 22:52:51 +01:00
|
|
|
break
|
|
|
|
}
|
2019-07-08 16:51:55 +01:00
|
|
|
|
2019-07-05 19:15:32 +01:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
close(r.eventChan)
|
|
|
|
return nil
|
|
|
|
case r.eventChan <- event:
|
2019-06-28 11:53:55 +01:00
|
|
|
}
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// close event channel on error
|
|
|
|
close(r.eventChan)
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// publishAdvert publishes router advert to advert channel
|
|
|
|
// NOTE: this might cease to be a dedicated method in the future
|
2019-07-25 23:19:05 +01:00
|
|
|
func (r *router) publishAdvert(advType AdvertType, events []*Event) {
|
2019-07-09 12:46:15 +01:00
|
|
|
defer r.advertWg.Done()
|
|
|
|
|
|
|
|
a := &Advert{
|
2019-07-10 07:45:27 +01:00
|
|
|
Id: r.opts.Id,
|
2019-07-09 12:46:15 +01:00
|
|
|
Type: advType,
|
2019-07-11 12:36:39 +01:00
|
|
|
TTL: DefaultAdvertTTL,
|
2019-07-09 12:46:15 +01:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r.advertChan <- a:
|
|
|
|
case <-r.exit:
|
|
|
|
return
|
|
|
|
}
|
2019-07-11 12:36:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// advertiseTable advertises the whole routing table to the network
|
|
|
|
func (r *router) advertiseTable() error {
|
|
|
|
// create table advertisement ticker
|
|
|
|
ticker := time.NewTicker(AdvertiseTableTick)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
// list routing table routes to announce
|
|
|
|
routes, err := r.List()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed listing routes: %s", err)
|
|
|
|
}
|
|
|
|
// collect all the added routes before we attempt to add default gateway
|
2019-07-25 23:19:05 +01:00
|
|
|
events := make([]*Event, len(routes))
|
2019-07-11 12:36:39 +01:00
|
|
|
for i, route := range routes {
|
2019-07-25 23:19:05 +01:00
|
|
|
event := &Event{
|
|
|
|
Type: Update,
|
2019-07-11 12:36:39 +01:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events[i] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise all routes as Update events to subscribers
|
|
|
|
if len(events) > 0 {
|
2019-07-17 13:02:47 +01:00
|
|
|
r.advertWg.Add(1)
|
2019-07-25 23:19:05 +01:00
|
|
|
go r.publishAdvert(RouteUpdate, events)
|
2019-07-11 12:36:39 +01:00
|
|
|
}
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 12:46:15 +01:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:00:25 +01:00
|
|
|
// routeAdvert contains a list of route events to be advertised
|
|
|
|
type routeAdvert struct {
|
2019-07-25 23:19:05 +01:00
|
|
|
events []*Event
|
2019-07-16 19:00:25 +01:00
|
|
|
// lastUpdate records the time of the last advert update
|
|
|
|
lastUpdate time.Time
|
|
|
|
// penalty is current advert penalty
|
2019-07-08 21:03:54 +01:00
|
|
|
penalty float64
|
2019-07-16 19:00:25 +01:00
|
|
|
// isSuppressed flags the advert suppression
|
2019-07-08 21:03:54 +01:00
|
|
|
isSuppressed bool
|
2019-07-16 19:00:25 +01:00
|
|
|
// suppressTime records the time interval the advert has been suppressed for
|
|
|
|
suppressTime time.Time
|
2019-07-08 21:03:54 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// advertiseEvents advertises routing table events
|
2019-07-05 19:15:32 +01:00
|
|
|
// It suppresses unhealthy flapping events and advertises healthy events upstream.
|
2019-07-24 17:16:52 +01:00
|
|
|
func (r *router) advertiseEvents() error {
|
2019-07-05 19:15:32 +01:00
|
|
|
// ticker to periodically scan event for advertising
|
2019-07-11 12:36:39 +01:00
|
|
|
ticker := time.NewTicker(AdvertiseEventsTick)
|
2019-07-16 19:00:25 +01:00
|
|
|
// advertMap is a map of advert events
|
|
|
|
advertMap := make(map[uint64]*routeAdvert)
|
2019-07-05 19:15:32 +01:00
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// routing table watcher
|
|
|
|
tableWatcher, err := r.Watch()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating routing table watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
select {
|
|
|
|
case r.errChan <- r.watchTable(tableWatcher):
|
|
|
|
case <-r.exit:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-05 19:15:32 +01:00
|
|
|
for {
|
2019-06-28 11:53:55 +01:00
|
|
|
select {
|
2019-07-05 19:15:32 +01:00
|
|
|
case <-ticker.C:
|
2019-07-25 23:19:05 +01:00
|
|
|
var events []*Event
|
2019-07-08 21:03:54 +01:00
|
|
|
// collect all events which are not flapping
|
2019-07-16 19:00:25 +01:00
|
|
|
for key, advert := range advertMap {
|
|
|
|
// decay the event penalty
|
|
|
|
delta := time.Since(advert.lastUpdate).Seconds()
|
|
|
|
advert.penalty = advert.penalty * math.Exp(-delta*PenaltyDecay)
|
|
|
|
|
|
|
|
// suppress/recover the event based on its penalty level
|
|
|
|
switch {
|
|
|
|
case advert.penalty > AdvertSuppress && !advert.isSuppressed:
|
|
|
|
advert.isSuppressed = true
|
|
|
|
advert.suppressTime = time.Now()
|
|
|
|
case advert.penalty < AdvertRecover && advert.isSuppressed:
|
|
|
|
advert.isSuppressed = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// max suppression time threshold has been reached, delete the advert
|
|
|
|
if advert.isSuppressed {
|
|
|
|
if time.Since(advert.suppressTime) > MaxSuppressTime {
|
|
|
|
delete(advertMap, key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !advert.isSuppressed {
|
|
|
|
for _, event := range advert.events {
|
2019-07-25 23:19:05 +01:00
|
|
|
e := new(Event)
|
2019-07-16 19:00:25 +01:00
|
|
|
*e = *event
|
|
|
|
events = append(events, e)
|
|
|
|
// delete the advert from the advertMap
|
|
|
|
delete(advertMap, key)
|
|
|
|
}
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:36:39 +01:00
|
|
|
// advertise all Update events to subscribers
|
2019-07-05 19:15:32 +01:00
|
|
|
if len(events) > 0 {
|
2019-07-09 12:46:15 +01:00
|
|
|
r.advertWg.Add(1)
|
2019-07-25 23:19:05 +01:00
|
|
|
go r.publishAdvert(RouteUpdate, events)
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
|
|
|
case e := <-r.eventChan:
|
2019-07-08 21:03:54 +01:00
|
|
|
// if event is nil, continue
|
2019-07-05 19:15:32 +01:00
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 15:55:39 +01:00
|
|
|
|
2019-07-05 19:15:32 +01:00
|
|
|
// determine the event penalty
|
|
|
|
var penalty float64
|
|
|
|
switch e.Type {
|
2019-07-25 23:19:05 +01:00
|
|
|
case Update:
|
2019-07-08 21:03:54 +01:00
|
|
|
penalty = UpdatePenalty
|
2019-07-25 23:19:05 +01:00
|
|
|
case Delete:
|
2019-07-16 19:00:25 +01:00
|
|
|
penalty = DeletePenalty
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
2019-07-16 19:00:25 +01:00
|
|
|
|
|
|
|
// check if we have already registered the route
|
|
|
|
// we use the route hash as advertMap key
|
2019-07-05 19:15:32 +01:00
|
|
|
hash := e.Route.Hash()
|
2019-07-16 19:00:25 +01:00
|
|
|
advert, ok := advertMap[hash]
|
2019-07-05 19:15:32 +01:00
|
|
|
if !ok {
|
2019-07-25 23:19:05 +01:00
|
|
|
events := []*Event{e}
|
2019-07-16 19:00:25 +01:00
|
|
|
advert = &routeAdvert{
|
|
|
|
events: events,
|
|
|
|
penalty: penalty,
|
|
|
|
lastUpdate: time.Now(),
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
2019-07-16 19:00:25 +01:00
|
|
|
advertMap[hash] = advert
|
2019-07-05 19:15:32 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-07-16 19:00:25 +01:00
|
|
|
|
|
|
|
// attempt to squash last two events if possible
|
|
|
|
lastEvent := advert.events[len(advert.events)-1]
|
|
|
|
if lastEvent.Type == e.Type {
|
|
|
|
advert.events[len(advert.events)-1] = e
|
|
|
|
} else {
|
|
|
|
advert.events = append(advert.events, e)
|
2019-07-05 19:15:32 +01:00
|
|
|
}
|
2019-07-16 19:00:25 +01:00
|
|
|
|
|
|
|
// update event penalty and recorded timestamp
|
|
|
|
advert.lastUpdate = time.Now()
|
|
|
|
advert.penalty += penalty
|
|
|
|
|
2019-06-28 11:53:55 +01:00
|
|
|
case <-r.exit:
|
2019-07-09 12:46:15 +01:00
|
|
|
// first wait for the advertiser to finish
|
|
|
|
r.advertWg.Wait()
|
|
|
|
// close the advert channel
|
|
|
|
close(r.advertChan)
|
|
|
|
return nil
|
2019-06-27 22:52:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 16:51:55 +01:00
|
|
|
// watchErrors watches router errors and takes appropriate actions
|
2019-07-24 17:16:52 +01:00
|
|
|
func (r *router) watchErrors() {
|
2019-06-29 00:46:22 +01:00
|
|
|
var err error
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
2019-07-24 17:16:52 +01:00
|
|
|
case err = <-r.errChan:
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2019-07-24 17:16:52 +01:00
|
|
|
if r.status.Code != Stopped {
|
|
|
|
// notify all goroutines to finish
|
2019-06-29 00:46:22 +01:00
|
|
|
close(r.exit)
|
2019-07-01 20:33:08 +01:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 19:15:32 +01:00
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
2019-07-05 19:15:32 +01:00
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
if err != nil {
|
|
|
|
r.status = Status{Code: Error, Error: err}
|
|
|
|
}
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// Run runs the router.
|
|
|
|
// It returns error if the router is already running.
|
|
|
|
func (r *router) run() {
|
2019-06-29 00:46:22 +01:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
switch r.status.Code {
|
|
|
|
case Stopped, Error:
|
2019-07-08 16:16:50 +01:00
|
|
|
// add all local service routes into the routing table
|
2019-07-10 21:28:32 +01:00
|
|
|
if err := r.manageRegistryRoutes(r.opts.Registry, "create"); err != nil {
|
2019-07-24 17:16:52 +01:00
|
|
|
r.status = Status{Code: Error, Error: fmt.Errorf("failed adding registry routes: %s", err)}
|
|
|
|
return
|
2019-07-09 15:01:52 +01:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:46:22 +01:00
|
|
|
// add default gateway into routing table
|
|
|
|
if r.opts.Gateway != "" {
|
|
|
|
// note, the only non-default value is the gateway
|
2019-07-25 23:19:05 +01:00
|
|
|
route := Route{
|
2019-07-09 15:45:42 +01:00
|
|
|
Service: "*",
|
|
|
|
Address: "*",
|
|
|
|
Gateway: r.opts.Gateway,
|
|
|
|
Network: "*",
|
2019-07-25 23:19:05 +01:00
|
|
|
Metric: DefaultLocalMetric,
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
2019-07-10 21:28:32 +01:00
|
|
|
if err := r.Create(route); err != nil {
|
2019-07-24 17:16:52 +01:00
|
|
|
r.status = Status{Code: Error, Error: fmt.Errorf("failed adding default gateway route: %s", err)}
|
|
|
|
return
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// create error and exit channels
|
|
|
|
r.errChan = make(chan error, 1)
|
|
|
|
r.exit = make(chan struct{})
|
2019-07-16 19:00:25 +01:00
|
|
|
|
|
|
|
// registry watcher
|
|
|
|
regWatcher, err := r.opts.Registry.Watch()
|
2019-06-29 00:46:22 +01:00
|
|
|
if err != nil {
|
2019-07-24 17:16:52 +01:00
|
|
|
r.status = Status{Code: Error, Error: fmt.Errorf("failed creating registry watcher: %v", err)}
|
|
|
|
return
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-24 17:16:52 +01:00
|
|
|
select {
|
|
|
|
case r.errChan <- r.watchRegistry(regWatcher):
|
|
|
|
case <-r.exit:
|
|
|
|
}
|
2019-06-29 00:46:22 +01:00
|
|
|
}()
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// watch for errors and cleanup
|
2019-06-29 00:46:22 +01:00
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-24 17:16:52 +01:00
|
|
|
r.watchErrors()
|
2019-07-05 19:15:32 +01:00
|
|
|
}()
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// mark router as Running and set its Error to nil
|
|
|
|
r.status = Status{Code: Running, Error: nil}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
switch r.status.Code {
|
|
|
|
case Advertising:
|
|
|
|
return r.advertChan, nil
|
|
|
|
case Running:
|
|
|
|
// list routing table routes to announce
|
|
|
|
routes, err := r.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed listing routes: %s", err)
|
|
|
|
}
|
|
|
|
// collect all the added routes before we attempt to add default gateway
|
2019-07-25 23:19:05 +01:00
|
|
|
events := make([]*Event, len(routes))
|
2019-07-24 17:16:52 +01:00
|
|
|
for i, route := range routes {
|
2019-07-25 23:19:05 +01:00
|
|
|
event := &Event{
|
|
|
|
Type: Create,
|
2019-07-24 17:16:52 +01:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events[i] = event
|
|
|
|
}
|
|
|
|
|
|
|
|
// create advertise and event channels
|
|
|
|
r.advertChan = make(chan *Advert)
|
2019-07-25 23:19:05 +01:00
|
|
|
r.eventChan = make(chan *Event)
|
2019-07-24 17:16:52 +01:00
|
|
|
|
|
|
|
// advertise your presence
|
|
|
|
r.advertWg.Add(1)
|
|
|
|
go r.publishAdvert(Announce, events)
|
|
|
|
|
2019-07-05 19:15:32 +01:00
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-24 17:16:52 +01:00
|
|
|
select {
|
|
|
|
case r.errChan <- r.advertiseEvents():
|
|
|
|
case <-r.exit:
|
|
|
|
}
|
2019-06-29 00:46:22 +01:00
|
|
|
}()
|
|
|
|
|
2019-07-11 12:36:39 +01:00
|
|
|
r.advertWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.advertWg.Done()
|
|
|
|
// advertise the whole routing table
|
2019-07-24 17:16:52 +01:00
|
|
|
select {
|
|
|
|
case r.errChan <- r.advertiseTable():
|
|
|
|
case <-r.exit:
|
|
|
|
}
|
2019-07-11 12:36:39 +01:00
|
|
|
}()
|
2019-07-01 15:43:50 +01:00
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
// mark router as Running and set its Error to nil
|
|
|
|
r.status = Status{Code: Advertising, Error: nil}
|
2019-07-08 21:03:54 +01:00
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
return r.advertChan, nil
|
|
|
|
case Stopped:
|
|
|
|
return nil, fmt.Errorf("not running")
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:16:52 +01:00
|
|
|
return nil, fmt.Errorf("error: %s", r.status.Error)
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:45:27 +01:00
|
|
|
// Process updates the routing table using the advertised values
|
|
|
|
func (r *router) Process(a *Advert) error {
|
2019-07-03 19:50:07 +01:00
|
|
|
// NOTE: event sorting might not be necessary
|
|
|
|
// copy update events intp new slices
|
2019-07-25 23:19:05 +01:00
|
|
|
events := make([]*Event, len(a.Events))
|
2019-07-04 02:06:59 +01:00
|
|
|
copy(events, a.Events)
|
2019-07-03 19:50:07 +01:00
|
|
|
// sort events by timestamp
|
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
|
|
return events[i].Timestamp.Before(events[j].Timestamp)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, event := range events {
|
2019-07-09 15:45:42 +01:00
|
|
|
// create a copy of the route
|
|
|
|
route := event.Route
|
2019-07-11 12:36:39 +01:00
|
|
|
action := event.Type
|
|
|
|
if err := r.manageRoute(route, fmt.Sprintf("%s", action)); err != nil {
|
|
|
|
return fmt.Errorf("failed applying action %s to routing table: %s", action, err)
|
2019-07-03 19:50:07 +01:00
|
|
|
}
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 19:50:07 +01:00
|
|
|
return nil
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 18:35:53 +01:00
|
|
|
// Status returns router status
|
|
|
|
func (r *router) Status() Status {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-06-29 00:46:22 +01:00
|
|
|
// make a copy of the status
|
|
|
|
status := r.status
|
|
|
|
|
|
|
|
return status
|
2019-06-28 18:35:53 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-07-24 17:16:52 +01:00
|
|
|
r.Lock()
|
|
|
|
// only close the channel if the router is running and/or advertising
|
|
|
|
if r.status.Code == Running || r.status.Code == Advertising {
|
2019-06-29 00:46:22 +01:00
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
2019-07-01 20:33:08 +01:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 19:15:32 +01:00
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
2019-07-24 17:16:52 +01:00
|
|
|
|
|
|
|
// mark the router as Stopped and set its Error to nil
|
|
|
|
r.status = Status{Code: Stopped, Error: nil}
|
2019-06-29 00:46:22 +01:00
|
|
|
}
|
2019-07-24 17:16:52 +01:00
|
|
|
r.Unlock()
|
2019-07-01 15:43:50 +01:00
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
2019-06-19 18:01:48 +01:00
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-06 23:29:24 +01:00
|
|
|
// String prints debugging information about router
|
2019-07-10 07:45:27 +01:00
|
|
|
func (r *router) String() string {
|
2019-07-17 00:06:11 +01:00
|
|
|
return "default"
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|