2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-10 01:09:38 +03:00
|
|
|
"fmt"
|
2019-07-05 21:15:32 +03:00
|
|
|
"math"
|
2019-07-03 21:50:07 +03:00
|
|
|
"sort"
|
2019-06-10 01:09:38 +03:00
|
|
|
"strings"
|
2019-06-12 01:59:25 +03:00
|
|
|
"sync"
|
2019-06-28 13:53:55 +03:00
|
|
|
"time"
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
"github.com/micro/go-log"
|
2019-06-06 18:37:40 +03:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-06-10 21:50:54 +03:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-06 18:37:40 +03:00
|
|
|
)
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
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
|
2019-07-05 21:15:32 +03:00
|
|
|
// AdvertSuppress is advert suppression threshold
|
|
|
|
AdvertSuppress = 2000
|
|
|
|
// AdvertRecover is advert suppression recovery threshold
|
|
|
|
AdvertRecover = 750
|
|
|
|
// PenaltyDecay is the "half-life" of the penalty
|
|
|
|
PenaltyDecay = 1.15
|
2019-07-03 21:50:07 +03:00
|
|
|
)
|
|
|
|
|
2019-06-26 18:03:19 +03:00
|
|
|
// router provides default router implementation
|
2019-06-06 18:37:40 +03:00
|
|
|
type router struct {
|
2019-06-28 00:52:51 +03:00
|
|
|
opts Options
|
2019-06-28 20:35:53 +03:00
|
|
|
status Status
|
2019-06-28 00:52:51 +03:00
|
|
|
exit chan struct{}
|
2019-07-05 21:15:32 +03:00
|
|
|
eventChan chan *Event
|
|
|
|
advertChan chan *Advert
|
2019-06-28 00:52:51 +03:00
|
|
|
wg *sync.WaitGroup
|
2019-06-28 20:35:53 +03:00
|
|
|
sync.RWMutex
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// newRouter creates a new router and returns it
|
2019-06-06 18:37:40 +03:00
|
|
|
func newRouter(opts ...Option) Router {
|
2019-06-13 00:30:42 +03:00
|
|
|
// get default options
|
|
|
|
options := DefaultOptions()
|
2019-06-06 18:37:40 +03:00
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// apply requested options
|
2019-06-06 18:37:40 +03:00
|
|
|
for _, o := range opts {
|
2019-06-10 21:50:54 +03:00
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
return &router{
|
2019-06-28 00:52:51 +03:00
|
|
|
opts: options,
|
2019-07-08 18:16:50 +03:00
|
|
|
status: Status{Error: nil, Code: Stopped},
|
2019-06-28 00:52:51 +03:00
|
|
|
exit: make(chan struct{}),
|
2019-07-05 21:15:32 +03:00
|
|
|
eventChan: make(chan *Event),
|
|
|
|
advertChan: make(chan *Advert),
|
2019-06-28 00:52:51 +03:00
|
|
|
wg: &sync.WaitGroup{},
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Init initializes router with given options
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Options returns router options
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) Options() Options {
|
|
|
|
return r.opts
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:09:59 +03:00
|
|
|
// ID returns router ID
|
|
|
|
func (r *router) ID() string {
|
|
|
|
return r.opts.ID
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Table returns routing table
|
2019-06-07 15:29:09 +03:00
|
|
|
func (r *router) Table() Table {
|
2019-06-10 21:50:54 +03:00
|
|
|
return r.opts.Table
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 19:20:22 +03:00
|
|
|
// Address returns router's bind address
|
2019-06-07 01:29:24 +03:00
|
|
|
func (r *router) Address() string {
|
2019-06-07 15:29:09 +03:00
|
|
|
return r.opts.Address
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Network returns the address router advertises to the network
|
2019-06-10 21:50:54 +03:00
|
|
|
func (r *router) Network() string {
|
2019-06-26 18:03:19 +03:00
|
|
|
return r.opts.Network
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// manageServiceRoutes manages the routes for a given service.
|
|
|
|
// It returns error of the routing table action fails with error.
|
|
|
|
func (r *router) manageServiceRoutes(service *registry.Service, action string, metric int) error {
|
|
|
|
// action is the routing table action
|
|
|
|
action = strings.ToLower(action)
|
|
|
|
// take route action on each service node
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
route := Route{
|
|
|
|
Destination: service.Name,
|
|
|
|
Gateway: node.Address,
|
|
|
|
Router: r.opts.Address,
|
|
|
|
Network: r.opts.Network,
|
|
|
|
Metric: metric,
|
|
|
|
}
|
|
|
|
switch action {
|
|
|
|
case "insert", "create":
|
|
|
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("failed adding route for service %s: %s", service.Name, err)
|
|
|
|
}
|
|
|
|
case "delete":
|
|
|
|
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed deleting route for service %v: %s", service.Name, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to manage route for service %v. Unknown action: %s", service.Name, action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func (r *router) manageRegistryRoutes(reg registry.Registry, action string, metric int) error {
|
2019-06-13 00:30:42 +03:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return fmt.Errorf("failed listing services: %v", err)
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 00:52:51 +03:00
|
|
|
// add each service node as a separate route
|
2019-06-13 00:30:42 +03:00
|
|
|
for _, service := range services {
|
2019-06-27 16:37:52 +03:00
|
|
|
// get the service to retrieve all its info
|
|
|
|
srvs, err := reg.GetService(service.Name)
|
|
|
|
if err != nil {
|
2019-07-08 18:16:50 +03:00
|
|
|
log.Logf("r.manageRegistryRoutes() GetService() error: %v", err)
|
2019-06-27 16:37:52 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-07-08 18:16:50 +03:00
|
|
|
// manage the routes for all return services
|
2019-06-27 16:37:52 +03:00
|
|
|
for _, s := range srvs {
|
2019-07-08 18:16:50 +03:00
|
|
|
if err := r.manageServiceRoutes(s, action, metric); err != nil {
|
|
|
|
return err
|
2019-06-19 20:01:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 18:03:19 +03:00
|
|
|
|
|
|
|
return nil
|
2019-06-19 20:01:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// watchServices watches services in given registry and updates the routing table accordingly.
|
|
|
|
// It returns error if the service registry watcher stops or if the routing table can't be updated.
|
|
|
|
func (r *router) watchServices(w registry.Watcher) error {
|
2019-06-13 17:12:07 +03:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
2019-06-12 01:59:25 +03: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 17:43:50 +03:00
|
|
|
if err != registry.ErrWatcherStopped {
|
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-12 01:59:25 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-07-06 03:31:59 +03:00
|
|
|
log.Logf("r.watchServices() new service event: Action: %s Service: %v", res.Action, res.Service)
|
2019-06-13 00:30:42 +03:00
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
if err := r.manageServiceRoutes(res.Service, res.Action, DefaultLocalMetric); err != nil {
|
|
|
|
return err
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-28 00:52:51 +03: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.
|
|
|
|
func (r *router) watchTable(w Watcher) error {
|
|
|
|
// 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-01 17:43:50 +03:00
|
|
|
if err != ErrWatcherStopped {
|
|
|
|
watchErr = err
|
|
|
|
}
|
2019-06-28 00:52:51 +03:00
|
|
|
break
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
close(r.eventChan)
|
|
|
|
return nil
|
|
|
|
case r.eventChan <- event:
|
2019-06-28 13:53:55 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// close event channel on error
|
|
|
|
close(r.eventChan)
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventFlap(curr, prev *Event) bool {
|
|
|
|
if curr.Type == UpdateEvent && prev.Type == UpdateEvent {
|
|
|
|
// update flap: this can be either metric or whatnot
|
|
|
|
log.Logf("eventFlap(): Update flap")
|
|
|
|
return true
|
|
|
|
}
|
2019-06-28 13:53:55 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
if curr.Type == CreateEvent && prev.Type == DeleteEvent || curr.Type == DeleteEvent && prev.Type == CreateEvent {
|
|
|
|
log.Logf("eventFlap(): Create/Delete flap")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
*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)
|
|
|
|
// lock to protect access to eventMap
|
|
|
|
mu := &sync.RWMutex{}
|
|
|
|
// waitgroup to manage advertisement goroutines
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
process:
|
|
|
|
for {
|
2019-06-28 13:53:55 +03:00
|
|
|
select {
|
2019-07-05 21:15:32 +03:00
|
|
|
case <-ticker.C:
|
|
|
|
var events []*Event
|
|
|
|
// decay the penalties of existing events
|
2019-07-06 02:36:15 +03:00
|
|
|
mu.Lock()
|
|
|
|
for advert, event := range eventMap {
|
2019-07-05 21:15:32 +03:00
|
|
|
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 {
|
|
|
|
e := new(Event)
|
|
|
|
*e = *event.Event
|
|
|
|
events = append(events, e)
|
2019-07-06 02:36:15 +03:00
|
|
|
// this deletes the advertised event from the map
|
|
|
|
delete(eventMap, advert)
|
2019-07-05 21:15:32 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-06 02:36:15 +03:00
|
|
|
mu.Unlock()
|
2019-07-05 21:15:32 +03:00
|
|
|
|
|
|
|
if len(events) > 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(events []*Event) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
log.Logf("go advertise(): start")
|
|
|
|
|
|
|
|
a := &Advert{
|
|
|
|
ID: r.ID(),
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
log.Logf("go advertise(): exit")
|
|
|
|
}(events)
|
|
|
|
}
|
|
|
|
case e := <-r.eventChan:
|
|
|
|
// if event is nil, break
|
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Logf("r.processEvents(): event received:\n%s", e)
|
|
|
|
// determine the event penalty
|
|
|
|
var penalty float64
|
|
|
|
switch e.Type {
|
|
|
|
case UpdateEvent:
|
|
|
|
penalty = UpdateRoutePenalty
|
|
|
|
case CreateEvent, DeleteEvent:
|
|
|
|
penalty = DeleteRoutePenalty
|
|
|
|
}
|
|
|
|
// we use route hash as eventMap key
|
|
|
|
hash := e.Route.Hash()
|
|
|
|
event, ok := eventMap[hash]
|
|
|
|
if !ok {
|
|
|
|
event = &advertEvent{
|
|
|
|
Event: e,
|
|
|
|
penalty: penalty,
|
|
|
|
timestamp: time.Now(),
|
|
|
|
}
|
|
|
|
eventMap[hash] = event
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// 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()
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
// if not suppressed decide if if its flapping
|
|
|
|
if !event.isSuppressed {
|
|
|
|
// detect if its flapping
|
|
|
|
event.isFlapping = eventFlap(e, event.Event)
|
|
|
|
}
|
2019-06-28 13:53:55 +03:00
|
|
|
case <-r.exit:
|
2019-07-05 21:15:32 +03:00
|
|
|
break process
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 02:36:15 +03:00
|
|
|
// first wait for the advertiser to finish
|
2019-07-05 21:15:32 +03:00
|
|
|
wg.Wait()
|
2019-07-06 02:36:15 +03:00
|
|
|
// close the advert channel
|
2019-07-01 17:43:50 +03:00
|
|
|
close(r.advertChan)
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.processEvents(): event processor stopped")
|
|
|
|
|
|
|
|
return nil
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// manage watches router errors and takes appropriate actions
|
|
|
|
func (r *router) manage(errChan <-chan error) {
|
2019-06-29 02:46:22 +03:00
|
|
|
defer r.wg.Done()
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.manage(): manage start")
|
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
var code StatusCode
|
|
|
|
var err error
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
code = Stopped
|
|
|
|
case err = <-errChan:
|
|
|
|
code = Error
|
|
|
|
}
|
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.manage(): manage exiting")
|
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2019-07-01 17:43:50 +03:00
|
|
|
status := Status{
|
|
|
|
Code: code,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
r.status = status
|
2019-06-29 02:46:22 +03:00
|
|
|
|
|
|
|
// stop the router if some error happened
|
|
|
|
if err != nil && code != Stopped {
|
2019-07-01 22:33:08 +03:00
|
|
|
// this will stop watchers which will close r.advertChan
|
2019-06-29 02:46:22 +03:00
|
|
|
close(r.exit)
|
2019-07-01 22:33:08 +03:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
|
|
|
|
log.Logf("r.manage(): manage exit")
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advertise advertises the routes to the network.
|
|
|
|
// It returns error if any of the launched goroutines fail with error.
|
2019-07-04 04:06:59 +03:00
|
|
|
func (r *router) Advertise() (<-chan *Advert, error) {
|
2019-06-29 02:46:22 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if r.status.Code != Running {
|
2019-07-08 18:16:50 +03:00
|
|
|
// add all local service routes into the routing table
|
|
|
|
if err := r.manageRegistryRoutes(r.opts.Registry, "insert", DefaultLocalMetric); err != nil {
|
2019-06-29 02:46:22 +03:00
|
|
|
return nil, fmt.Errorf("failed adding routes: %v", err)
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("Routing table:\n%s", r.opts.Table)
|
2019-06-29 02:46:22 +03:00
|
|
|
// add default gateway into routing table
|
|
|
|
if r.opts.Gateway != "" {
|
|
|
|
// note, the only non-default value is the gateway
|
|
|
|
route := Route{
|
|
|
|
Destination: "*",
|
|
|
|
Gateway: r.opts.Gateway,
|
|
|
|
Router: "*",
|
|
|
|
Network: "*",
|
|
|
|
Metric: DefaultLocalMetric,
|
|
|
|
}
|
|
|
|
if err := r.opts.Table.Add(route); err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil, fmt.Errorf("failed adding default gateway route: %s", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we only need to recreate the exit/advertChan if the router errored or was stopped
|
2019-07-05 21:15:32 +03:00
|
|
|
// TODO: these channels most likely won't have to be the struct fields
|
2019-06-29 02:46:22 +03:00
|
|
|
if r.status.Code == Error || r.status.Code == Stopped {
|
|
|
|
r.exit = make(chan struct{})
|
2019-07-05 21:15:32 +03:00
|
|
|
r.eventChan = make(chan *Event)
|
2019-07-04 04:06:59 +03:00
|
|
|
r.advertChan = make(chan *Advert)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// routing table watcher which watches all routes i.e. to every destination
|
|
|
|
tableWatcher, err := r.opts.Table.Watch(WatchDestination("*"))
|
|
|
|
if err != nil {
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil, fmt.Errorf("failed creating routing table watcher: %v", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
// service registry watcher
|
|
|
|
svcWatcher, err := r.opts.Registry.Watch()
|
2019-06-29 02:46:22 +03:00
|
|
|
if err != nil {
|
2019-07-05 21:15:32 +03:00
|
|
|
return nil, fmt.Errorf("failed creating service registry watcher: %v", err)
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// error channel collecting goroutine errors
|
2019-07-05 21:15:32 +03:00
|
|
|
errChan := make(chan error, 3)
|
2019-06-29 02:46:22 +03:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Advertise(): r.watchServices() start")
|
2019-06-29 02:46:22 +03:00
|
|
|
// watch local registry and register routes in routine table
|
2019-07-05 21:15:32 +03:00
|
|
|
errChan <- r.watchServices(svcWatcher)
|
|
|
|
log.Logf("r.Advertise(): r.watchServices() exit")
|
2019-06-29 02:46:22 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Advertise(): r.watchTable() start")
|
2019-06-29 02:46:22 +03:00
|
|
|
// watch local registry and register routes in routing table
|
|
|
|
errChan <- r.watchTable(tableWatcher)
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Advertise(): r.watchTable() exit")
|
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
log.Logf("r.Advertise(): r.processEvents() start")
|
|
|
|
// listen to routing table events and process them
|
|
|
|
errChan <- r.processEvents()
|
|
|
|
log.Logf("r.Advertise(): r.processEvents() exit")
|
2019-06-29 02:46:22 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
2019-07-05 21:15:32 +03:00
|
|
|
go r.manage(errChan)
|
2019-07-01 17:43:50 +03:00
|
|
|
|
|
|
|
// mark router as running and set its Error to nil
|
|
|
|
status := Status{
|
|
|
|
Code: Running,
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
r.status = status
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return r.advertChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the routing table using the advertised values
|
2019-07-04 04:06:59 +03:00
|
|
|
func (r *router) Update(a *Advert) error {
|
2019-07-03 21:50:07 +03:00
|
|
|
// NOTE: event sorting might not be necessary
|
|
|
|
// copy update events intp new slices
|
2019-07-04 04:06:59 +03:00
|
|
|
events := make([]*Event, len(a.Events))
|
|
|
|
copy(events, a.Events)
|
2019-07-03 21:50:07 +03: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 {
|
|
|
|
// we extract the route from advertisement and update the routing table
|
|
|
|
route := Route{
|
|
|
|
Destination: event.Route.Destination,
|
|
|
|
Gateway: event.Route.Gateway,
|
|
|
|
Router: event.Route.Router,
|
|
|
|
Network: event.Route.Network,
|
|
|
|
Metric: event.Route.Metric,
|
2019-07-08 18:16:50 +03:00
|
|
|
Policy: Insert,
|
2019-07-03 21:50:07 +03:00
|
|
|
}
|
|
|
|
if err := r.opts.Table.Update(route); err != nil {
|
|
|
|
return fmt.Errorf("failed updating routing table: %v", err)
|
|
|
|
}
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:50:07 +03:00
|
|
|
return nil
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 20:35:53 +03:00
|
|
|
// Status returns router status
|
|
|
|
func (r *router) Status() Status {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-06-29 02:46:22 +03:00
|
|
|
// make a copy of the status
|
|
|
|
status := r.status
|
|
|
|
|
|
|
|
return status
|
2019-06-28 20:35:53 +03:00
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Stop(): Stopping router")
|
2019-06-29 02:46:22 +03:00
|
|
|
r.RLock()
|
|
|
|
// only close the channel if the router is running
|
|
|
|
if r.status.Code == Running {
|
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Stop(): exit closed")
|
2019-07-01 22:33:08 +03:00
|
|
|
// drain the advertise channel
|
|
|
|
for range r.advertChan {
|
|
|
|
}
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Stop(): advert channel drained")
|
|
|
|
// drain the event channel
|
|
|
|
for range r.eventChan {
|
|
|
|
}
|
|
|
|
log.Logf("r.Stop(): event channel drained")
|
2019-06-29 02:46:22 +03:00
|
|
|
}
|
2019-07-01 17:43:50 +03:00
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
2019-06-19 20:01:48 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
log.Logf("r.Stop(): Router stopped")
|
2019-06-12 01:59:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// String prints debugging information about router
|
2019-06-06 18:37:40 +03:00
|
|
|
func (r *router) String() string {
|
2019-06-10 01:09:38 +03:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-29 02:46:22 +03:00
|
|
|
table.SetHeader([]string{"ID", "Address", "Network", "Table", "Status"})
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
data := []string{
|
|
|
|
r.opts.ID,
|
|
|
|
r.opts.Address,
|
2019-06-26 18:03:19 +03:00
|
|
|
r.opts.Network,
|
2019-06-10 21:50:54 +03:00
|
|
|
fmt.Sprintf("%d", r.opts.Table.Size()),
|
2019-06-29 02:46:22 +03:00
|
|
|
r.status.Code.String(),
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
table.Append(data)
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
return sb.String()
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|