2019-06-06 16:37:40 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-09 23:09:38 +01:00
|
|
|
"fmt"
|
2019-06-20 13:04:58 +01:00
|
|
|
"net"
|
2019-06-12 22:30:42 +01:00
|
|
|
"strconv"
|
2019-06-09 23:09:38 +01:00
|
|
|
"strings"
|
2019-06-11 23:59:25 +01:00
|
|
|
"sync"
|
2019-06-12 22:30:42 +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-06-10 19:50:54 +01:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-06 16:37:40 +01:00
|
|
|
)
|
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
var (
|
2019-06-19 21:22:14 +01:00
|
|
|
// AdvertiseTick defines how often in seconds do we scal the local registry
|
2019-06-19 18:01:48 +01:00
|
|
|
// to advertise the local services to the network registry
|
2019-06-19 21:22:14 +01:00
|
|
|
AdvertiseTick = 5 * time.Second
|
|
|
|
// AdvertiseTTL defines network registry TTL in seconds
|
2019-06-19 18:01:48 +01:00
|
|
|
// NOTE: this is a rather arbitrary picked value subject to change
|
2019-06-19 21:22:14 +01:00
|
|
|
AdvertiseTTL = 120 * time.Second
|
2019-06-19 18:01:48 +01:00
|
|
|
)
|
|
|
|
|
2019-06-06 16:37:40 +01:00
|
|
|
type router struct {
|
2019-06-10 19:50:54 +01:00
|
|
|
opts Options
|
2019-06-11 23:59:25 +01:00
|
|
|
exit chan struct{}
|
|
|
|
wg *sync.WaitGroup
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:07 +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-06-11 23:59:25 +01:00
|
|
|
return &router{
|
2019-06-10 19:50:54 +01:00
|
|
|
opts: options,
|
2019-06-11 23:59:25 +01:00
|
|
|
exit: make(chan struct{}),
|
|
|
|
wg: &sync.WaitGroup{},
|
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-06-16 23:09:59 +01:00
|
|
|
// ID returns router ID
|
|
|
|
func (r *router) ID() string {
|
|
|
|
return r.opts.ID
|
|
|
|
}
|
|
|
|
|
2019-06-06 23:29:24 +01:00
|
|
|
// Table returns routing table
|
2019-06-07 13:29:09 +01:00
|
|
|
func (r *router) Table() Table {
|
2019-06-10 19:50:54 +01:00
|
|
|
return r.opts.Table
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 17:20:22 +01:00
|
|
|
// Address returns router's bind address
|
2019-06-06 23:29:24 +01:00
|
|
|
func (r *router) Address() string {
|
2019-06-07 13:29:09 +01:00
|
|
|
return r.opts.Address
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Network returns the address router advertises to the network
|
2019-06-10 19:50:54 +01:00
|
|
|
func (r *router) Network() string {
|
2019-06-19 21:22:14 +01:00
|
|
|
return r.opts.Advertise
|
2019-06-10 19:50:54 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 13:04:58 +01:00
|
|
|
// Advertise advertises the router routes to the network.
|
|
|
|
// Advertise is a blocking function. It launches multiple goroutines that watch
|
|
|
|
// service registries and advertise the router routes to other routers in the network.
|
|
|
|
// It returns error if any of the launched goroutines fail with error.
|
|
|
|
func (r *router) Advertise() error {
|
2019-06-13 15:12:07 +01:00
|
|
|
// add local service routes into the routing table
|
2019-06-19 21:22:14 +01:00
|
|
|
if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil {
|
2019-06-13 15:12:07 +01:00
|
|
|
return fmt.Errorf("failed adding routes for local services: %v", err)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:07 +01:00
|
|
|
// add network service routes into the routing table
|
2019-06-19 21:22:14 +01:00
|
|
|
if err := r.addServiceRoutes(r.opts.Network, DefaultNetworkMetric); err != nil {
|
2019-06-13 15:12:07 +01:00
|
|
|
return fmt.Errorf("failed adding routes for network services: %v", err)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
node, err := r.parseToNode()
|
2019-06-12 22:30:42 +01:00
|
|
|
if err != nil {
|
2019-06-16 23:09:59 +01:00
|
|
|
return fmt.Errorf("failed to parse router into service node: %v", err)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
localWatcher, err := r.opts.Registry.Watch()
|
2019-06-12 22:30:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create local registry watcher: %v", err)
|
|
|
|
}
|
2019-06-11 23:59:25 +01:00
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
networkWatcher, err := r.opts.Network.Watch()
|
2019-06-11 23:59:25 +01:00
|
|
|
if err != nil {
|
2019-06-12 22:30:42 +01:00
|
|
|
return fmt.Errorf("failed to create network registry watcher: %v", err)
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
// error channel collecting goroutine errors
|
|
|
|
errChan := make(chan error, 3)
|
2019-06-11 23:59:25 +01:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
2019-06-19 18:01:48 +01:00
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch local registry and register routes in routine table
|
2019-06-19 21:22:14 +01:00
|
|
|
errChan <- r.manageServiceRoutes(localWatcher, DefaultLocalMetric)
|
2019-06-19 18:01:48 +01:00
|
|
|
}()
|
2019-06-12 22:30:42 +01:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
2019-06-19 18:01:48 +01:00
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch network registry and register routes in routine table
|
2019-06-19 21:22:14 +01:00
|
|
|
errChan <- r.manageServiceRoutes(networkWatcher, DefaultNetworkMetric)
|
2019-06-19 18:01:48 +01:00
|
|
|
}()
|
2019-06-11 23:59:25 +01:00
|
|
|
|
|
|
|
r.wg.Add(1)
|
2019-06-19 18:01:48 +01:00
|
|
|
go func() {
|
|
|
|
defer r.wg.Done()
|
|
|
|
// watch local registry and advertise local service to the network
|
|
|
|
errChan <- r.advertiseToNetwork(node)
|
|
|
|
}()
|
2019-06-11 23:59:25 +01:00
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
return <-errChan
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 13:04:58 +01:00
|
|
|
// addServiceRoutes adds all services in given registry to the routing table.
|
2019-06-17 19:51:13 +01:00
|
|
|
// NOTE: this is a one-off operation done when bootstrapping the routing table of the new router.
|
|
|
|
// It returns error if either the services could not be listed or if the routes could not be added to the routing table.
|
2019-06-19 21:22:14 +01:00
|
|
|
func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
|
2019-06-12 22:30:42 +01:00
|
|
|
services, err := reg.ListServices()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list services: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
2019-06-19 21:22:14 +01:00
|
|
|
route := Route{
|
|
|
|
Destination: service.Name,
|
|
|
|
Router: r,
|
|
|
|
Network: r.opts.Advertise,
|
|
|
|
Metric: metric,
|
|
|
|
}
|
2019-06-16 23:09:59 +01:00
|
|
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
2019-06-13 23:28:47 +02:00
|
|
|
return fmt.Errorf("error adding route for service: %s", service.Name)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 19:51:13 +01:00
|
|
|
// parseToNode parses router into registry.Node and returns the result.
|
2019-06-19 21:22:14 +01:00
|
|
|
// It returns error if the router network address could not be parsed into host and port.
|
2019-06-13 23:28:47 +02:00
|
|
|
func (r *router) parseToNode() (*registry.Node, error) {
|
2019-06-20 13:04:58 +01:00
|
|
|
// split router address to host and port part
|
|
|
|
addr, portStr, err := net.SplitHostPort(r.opts.Advertise)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse router address: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// try to parse network port into integer
|
2019-06-20 13:04:58 +01:00
|
|
|
port, err := strconv.Atoi(portStr)
|
2019-06-13 23:28:47 +02:00
|
|
|
if err != nil {
|
2019-06-19 21:22:14 +01:00
|
|
|
return nil, fmt.Errorf("could not parse router network address: %v", err)
|
2019-06-13 23:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
node := ®istry.Node{
|
|
|
|
Id: r.opts.ID,
|
2019-06-20 13:04:58 +01:00
|
|
|
Address: addr,
|
2019-06-13 23:28:47 +02:00
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
// advertiseToNetwork periodically scans local registry and registers (i.e. advertises) all the local services in the network registry.
|
|
|
|
// It returns error if either the local services failed to be listed or if it fails to register local service in network registry.
|
|
|
|
func (r *router) advertiseToNetwork(node *registry.Node) error {
|
|
|
|
// ticker to periodically scan the local registry
|
2019-06-19 21:22:14 +01:00
|
|
|
ticker := time.NewTicker(AdvertiseTick)
|
2019-06-19 18:01:48 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
|
|
|
// list all local services
|
2019-06-19 21:22:14 +01:00
|
|
|
services, err := r.opts.Registry.ListServices()
|
2019-06-19 18:01:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list local services: %v", err)
|
|
|
|
}
|
|
|
|
// loop through all registered local services and register them in the network registry
|
|
|
|
for _, service := range services {
|
|
|
|
svc := ®istry.Service{
|
|
|
|
Name: service.Name,
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
|
|
|
// register the local service in the network registry
|
2019-06-19 21:22:14 +01:00
|
|
|
if err := r.opts.Network.Register(svc, registry.RegisterTTL(AdvertiseTTL)); err != nil {
|
2019-06-19 18:01:48 +01:00
|
|
|
return fmt.Errorf("failed to register service %s in network registry: %v", svc.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:07 +01: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 21:22:14 +01:00
|
|
|
func (r *router) manageServiceRoutes(w registry.Watcher, metric int) 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 == registry.ErrWatcherStopped {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
route := Route{
|
|
|
|
Destination: res.Service.Name,
|
|
|
|
Router: r,
|
|
|
|
Network: r.opts.Advertise,
|
|
|
|
Metric: metric,
|
|
|
|
}
|
2019-06-12 22:30:42 +01:00
|
|
|
|
|
|
|
switch res.Action {
|
|
|
|
case "create":
|
|
|
|
if len(res.Service.Nodes) > 0 {
|
2019-06-19 21:22:14 +01:00
|
|
|
// only return error if the route is not duplicate, but something else has failed
|
2019-06-16 23:09:59 +01:00
|
|
|
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
|
2019-06-13 15:12:07 +01:00
|
|
|
return fmt.Errorf("failed to add route for service: %v", res.Service.Name)
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "delete":
|
2019-06-18 11:44:09 +01:00
|
|
|
if len(res.Service.Nodes) < 1 {
|
2019-06-17 19:51:13 +01:00
|
|
|
// only return error if the route is present 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", res.Service.Name)
|
|
|
|
}
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchErr
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops the router
|
|
|
|
func (r *router) Stop() error {
|
2019-06-19 18:01:48 +01:00
|
|
|
// notify all goroutines to finish
|
|
|
|
close(r.exit)
|
|
|
|
|
|
|
|
// wait for all goroutines to finish
|
|
|
|
r.wg.Wait()
|
|
|
|
|
2019-06-17 19:51:13 +01:00
|
|
|
// NOTE: we need a more efficient way of doing this e.g. network routes
|
|
|
|
// should ideally be autodeleted when the router stops gossiping
|
2019-06-19 21:22:14 +01:00
|
|
|
query := NewQuery(QueryRouter(r), QueryNetwork(r.opts.Advertise))
|
2019-06-16 23:09:59 +01:00
|
|
|
routes, err := r.opts.Table.Lookup(query)
|
|
|
|
if err != nil && err != ErrRouteNotFound {
|
|
|
|
return fmt.Errorf("failed to lookup routes for router %s: %v", r.opts.ID, err)
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:01:48 +01:00
|
|
|
// parse router to registry.Node
|
2019-06-16 23:09:59 +01:00
|
|
|
node, err := r.parseToNode()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse router into service node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
service := ®istry.Service{
|
2019-06-19 21:22:14 +01:00
|
|
|
Name: route.Destination,
|
2019-06-16 23:09:59 +01:00
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
2019-06-19 21:22:14 +01:00
|
|
|
if err := r.opts.Network.Deregister(service); err != nil {
|
2019-06-16 23:09:59 +01:00
|
|
|
return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-06 16:37:40 +01:00
|
|
|
func (r *router) String() string {
|
2019-06-09 23:09:38 +01:00
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
2019-06-10 19:50:54 +01:00
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-12 22:30:42 +01:00
|
|
|
table.SetHeader([]string{"ID", "Address", "Network", "Table"})
|
2019-06-09 23:09:38 +01:00
|
|
|
|
2019-06-10 19:50:54 +01:00
|
|
|
data := []string{
|
|
|
|
r.opts.ID,
|
|
|
|
r.opts.Address,
|
2019-06-19 21:22:14 +01:00
|
|
|
r.opts.Advertise,
|
2019-06-10 19:50:54 +01:00
|
|
|
fmt.Sprintf("%d", r.opts.Table.Size()),
|
|
|
|
}
|
|
|
|
table.Append(data)
|
2019-06-09 23:09:38 +01:00
|
|
|
|
2019-06-10 19:50:54 +01:00
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
2019-06-09 23:09:38 +01:00
|
|
|
|
|
|
|
return sb.String()
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|