2019-06-06 17:37:40 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-10 00:09:38 +02:00
|
|
|
"fmt"
|
|
|
|
"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-06-06 17:37:40 +02:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-06-10 20:50:54 +02:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-06 17:37:40 +02:00
|
|
|
)
|
|
|
|
|
2019-06-26 17:03:19 +02:00
|
|
|
// router provides default router implementation
|
2019-06-06 17:37:40 +02:00
|
|
|
type router struct {
|
2019-06-27 23:52:51 +02:00
|
|
|
opts Options
|
2019-06-28 19:35:53 +02:00
|
|
|
status Status
|
2019-06-28 12:53:55 +02:00
|
|
|
advertChan chan *Update
|
2019-06-27 23:52:51 +02:00
|
|
|
exit chan struct{}
|
|
|
|
wg *sync.WaitGroup
|
2019-06-28 19:35:53 +02:00
|
|
|
sync.RWMutex
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 16:12:07 +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-06-12 00:59:25 +02:00
|
|
|
return &router{
|
2019-06-27 23:52:51 +02:00
|
|
|
opts: options,
|
2019-06-28 19:35:53 +02:00
|
|
|
status: Status{Error: nil, Code: Stopped},
|
2019-06-28 12:53:55 +02:00
|
|
|
advertChan: make(chan *Update),
|
2019-06-27 23:52:51 +02:00
|
|
|
exit: make(chan struct{}),
|
|
|
|
wg: &sync.WaitGroup{},
|
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 {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.opts)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return r.opts
|
|
|
|
}
|
|
|
|
|
2019-06-17 00:09:59 +02:00
|
|
|
// ID returns router ID
|
|
|
|
func (r *router) ID() string {
|
|
|
|
return r.opts.ID
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:29:24 +02:00
|
|
|
// Table returns routing table
|
2019-06-07 14:29:09 +02:00
|
|
|
func (r *router) Table() Table {
|
2019-06-10 20:50:54 +02:00
|
|
|
return r.opts.Table
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 18:20:22 +02:00
|
|
|
// Address returns router's bind address
|
2019-06-07 00:29:24 +02:00
|
|
|
func (r *router) Address() string {
|
2019-06-07 14:29:09 +02:00
|
|
|
return r.opts.Address
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 22:22:14 +02:00
|
|
|
// Network returns the address router advertises to the network
|
2019-06-10 20:50:54 +02:00
|
|
|
func (r *router) Network() string {
|
2019-06-26 17:03:19 +02:00
|
|
|
return r.opts.Network
|
2019-06-10 20:50:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
// Advertise advertises the routes to the network.
|
2019-06-20 14:04:58 +02:00
|
|
|
// It returns error if any of the launched goroutines fail with error.
|
2019-06-28 12:53:55 +02:00
|
|
|
func (r *router) Advertise() (<-chan *Update, error) {
|
2019-06-28 19:35:53 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if r.status.Code != Running {
|
2019-06-27 23:52:51 +02:00
|
|
|
// add local service routes into the routing table
|
|
|
|
if err := r.addServiceRoutes(r.opts.Registry, "local", DefaultLocalMetric); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed adding routes: %v", err)
|
|
|
|
}
|
2019-06-12 23:30:42 +02:00
|
|
|
|
2019-06-27 23:52:51 +02: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-06-28 19:35:53 +02:00
|
|
|
return nil, fmt.Errorf("error to add default gateway route: %s", err)
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 19:35:53 +02:00
|
|
|
// routing table watcher which watches all routes i.e. to every destination
|
2019-06-27 23:52:51 +02:00
|
|
|
tableWatcher, err := r.opts.Table.Watch(WatchDestination("*"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create routing table watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// registry watcher
|
|
|
|
regWatcher, err := r.opts.Registry.Watch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create registry watcher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// error channel collecting goroutine errors
|
|
|
|
errChan := make(chan error, 2)
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch local registry and register routes in routine table
|
|
|
|
errChan <- r.manageServiceRoutes(regWatcher, DefaultLocalMetric)
|
|
|
|
}()
|
|
|
|
|
|
|
|
r.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
2019-06-28 19:35:53 +02:00
|
|
|
// watch local registry and register routes in routing table
|
2019-06-27 23:52:51 +02:00
|
|
|
errChan <- r.watchTable(tableWatcher)
|
|
|
|
}()
|
|
|
|
|
2019-06-28 19:35:53 +02:00
|
|
|
r.wg.Add(1)
|
2019-06-27 23:52:51 +02:00
|
|
|
go func() {
|
2019-06-28 19:35:53 +02:00
|
|
|
defer r.wg.Done()
|
2019-06-27 23:52:51 +02:00
|
|
|
select {
|
|
|
|
// wait for exit chan
|
|
|
|
case <-r.exit:
|
2019-06-28 19:35:53 +02:00
|
|
|
r.status.Code = Stopped
|
|
|
|
case err := <-errChan:
|
|
|
|
r.status.Code = Error
|
|
|
|
r.status.Error = err
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
// close the advertise channel
|
|
|
|
close(r.advertChan)
|
|
|
|
}()
|
|
|
|
|
2019-06-28 12:53:55 +02:00
|
|
|
// mark the router as running
|
2019-06-28 19:35:53 +02:00
|
|
|
r.status.Code = Running
|
2019-06-12 00:59:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
return r.advertChan, nil
|
|
|
|
}
|
2019-06-12 00:59:25 +02:00
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
// Update updates the routing table using the advertised values
|
2019-06-28 12:53:55 +02:00
|
|
|
func (r *router) Update(a *Update) error {
|
2019-06-27 23:52:51 +02:00
|
|
|
// we extract the route from advertisement and update the routing table
|
|
|
|
route := Route{
|
|
|
|
Destination: a.Event.Route.Destination,
|
|
|
|
Gateway: a.Event.Route.Gateway,
|
|
|
|
Router: a.Event.Route.Router,
|
|
|
|
Network: a.Event.Route.Network,
|
|
|
|
Metric: a.Event.Route.Metric,
|
|
|
|
Policy: AddIfNotExists,
|
|
|
|
}
|
2019-06-12 23:30:42 +02:00
|
|
|
|
2019-06-27 23:52:51 +02:00
|
|
|
return r.opts.Table.Update(route)
|
2019-06-12 00:59:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-20 14:04:58 +02:00
|
|
|
// addServiceRoutes adds all services in given registry to the routing table.
|
2019-06-26 17:03:19 +02:00
|
|
|
// NOTE: this is a one-off operation done when bootstrapping the routing table
|
|
|
|
// It returns error if either the services failed to be listed or
|
2019-06-27 23:52:51 +02:00
|
|
|
// if any of the the routes could not be added to the routing table.
|
2019-06-26 17:03:19 +02:00
|
|
|
func (r *router) addServiceRoutes(reg registry.Registry, network string, metric int) error {
|
2019-06-12 23:30:42 +02:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list services: %v", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a flat slide of nodes
|
|
|
|
var nodes []*registry.Node
|
|
|
|
for _, s := range srvs {
|
|
|
|
nodes = append(nodes, s.Nodes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// range over the flat slice of nodes
|
|
|
|
for _, node := range nodes {
|
2019-06-28 19:35:53 +02:00
|
|
|
gateway := node.Address
|
2019-06-26 17:03:19 +02:00
|
|
|
if node.Port > 0 {
|
2019-06-28 19:35:53 +02:00
|
|
|
gateway = fmt.Sprintf("%s:%d", node.Address, node.Port)
|
2019-06-26 17:03:19 +02:00
|
|
|
}
|
|
|
|
route := Route{
|
|
|
|
Destination: service.Name,
|
2019-06-28 19:35:53 +02:00
|
|
|
Gateway: gateway,
|
2019-06-26 17:03:19 +02:00
|
|
|
Router: r.opts.Address,
|
|
|
|
Network: r.opts.Network,
|
|
|
|
Metric: metric,
|
2019-06-19 19:01:48 +02:00
|
|
|
}
|
2019-06-26 17:03:19 +02:00
|
|
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("error adding route for service %s: %s", service.Name, 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-06-13 16:12:07 +02:00
|
|
|
// manageServiceRoutes watches services in given registry and updates the routing table accordingly.
|
|
|
|
// It returns error if the service registry watcher has stopped or if the routing table failed to be updated.
|
2019-06-19 22:22:14 +02:00
|
|
|
func (r *router) manageServiceRoutes(w registry.Watcher, metric int) error {
|
2019-06-13 16:12:07 +02:00
|
|
|
// wait in the background for the router to stop
|
|
|
|
// when the router stops, stop the watcher and exit
|
2019-06-12 00:59:25 +02: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 == registry.ErrWatcherStopped {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:22:14 +02:00
|
|
|
route := Route{
|
|
|
|
Destination: res.Service.Name,
|
2019-06-26 17:03:19 +02:00
|
|
|
Router: r.opts.Address,
|
|
|
|
Network: r.opts.Network,
|
2019-06-19 22:22:14 +02:00
|
|
|
Metric: metric,
|
|
|
|
}
|
2019-06-12 23:30:42 +02:00
|
|
|
|
|
|
|
switch res.Action {
|
|
|
|
case "create":
|
2019-06-26 17:03:19 +02:00
|
|
|
// only return error if the route is not duplicate, but something else has failed
|
|
|
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
|
|
|
return fmt.Errorf("failed to add route for service %v: %s", res.Service.Name, err)
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
|
|
|
case "delete":
|
2019-06-26 17:03:19 +02:00
|
|
|
// only return error if the route is not in the table, but something else has failed
|
|
|
|
if err := r.opts.Table.Delete(route); err != nil && err != ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed to delete route for service %v: %s", res.Service.Name, err)
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
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 == ErrWatcherStopped {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-28 12:53:55 +02:00
|
|
|
u := &Update{
|
|
|
|
ID: r.ID(),
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Event: event,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
case r.advertChan <- u:
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-28 19:35:53 +02:00
|
|
|
// Status returns router status
|
|
|
|
func (r *router) Status() Status {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
|
|
|
return r.status
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-06-19 19:01:48 +02:00
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
|
|
|
|
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-06-06 17:37:40 +02:00
|
|
|
func (r *router) String() string {
|
2019-06-10 00:09:38 +02:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
2019-06-10 20:50:54 +02:00
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-12 23:30:42 +02:00
|
|
|
table.SetHeader([]string{"ID", "Address", "Network", "Table"})
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-10 20:50:54 +02:00
|
|
|
data := []string{
|
|
|
|
r.opts.ID,
|
|
|
|
r.opts.Address,
|
2019-06-26 17:03:19 +02:00
|
|
|
r.opts.Network,
|
2019-06-10 20:50:54 +02:00
|
|
|
fmt.Sprintf("%d", r.opts.Table.Size()),
|
|
|
|
}
|
|
|
|
table.Append(data)
|
2019-06-10 00:09:38 +02:00
|
|
|
|
2019-06-10 20:50:54 +02:00
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
2019-06-10 00:09:38 +02:00
|
|
|
|
|
|
|
return sb.String()
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|