Renamed variables, options and functions

This commit is contained in:
Milos Gajdos 2019-06-19 21:22:14 +01:00
parent 4e5fbbf7eb
commit 10a3636a9f
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
8 changed files with 158 additions and 260 deletions

View File

@ -12,12 +12,12 @@ import (
)
var (
// AdvertiseToNetworkTick defines how often in seconds do we scal the local registry
// AdvertiseTick defines how often in seconds do we scal the local registry
// to advertise the local services to the network registry
AdvertiseToNetworkTick = 5 * time.Second
// AdvertiseNetworkTTL defines network registry TTL in seconds
AdvertiseTick = 5 * time.Second
// AdvertiseTTL defines network registry TTL in seconds
// NOTE: this is a rather arbitrary picked value subject to change
AdvertiseNetworkTTL = 120 * time.Second
AdvertiseTTL = 120 * time.Second
)
type router struct {
@ -71,25 +71,20 @@ func (r *router) Address() string {
return r.opts.Address
}
// Gossip returns gossip bind address
func (r *router) Gossip() string {
return r.opts.GossipAddress
}
// Network returns router's micro network
// Network returns the address router advertises to the network
func (r *router) Network() string {
return r.opts.NetworkAddress
return r.opts.Advertise
}
// Start starts the router
func (r *router) Start() error {
// add local service routes into the routing table
if err := r.addServiceRoutes(r.opts.LocalRegistry, r.opts.Address, DefaultLocalMetric); err != nil {
if err := r.addServiceRoutes(r.opts.Registry, DefaultLocalMetric); err != nil {
return fmt.Errorf("failed adding routes for local services: %v", err)
}
// add network service routes into the routing table
if err := r.addServiceRoutes(r.opts.NetworkRegistry, r.opts.NetworkAddress, DefaultNetworkMetric); err != nil {
if err := r.addServiceRoutes(r.opts.Network, DefaultNetworkMetric); err != nil {
return fmt.Errorf("failed adding routes for network services: %v", err)
}
@ -98,12 +93,12 @@ func (r *router) Start() error {
return fmt.Errorf("failed to parse router into service node: %v", err)
}
localRegWatcher, err := r.opts.LocalRegistry.Watch()
localWatcher, err := r.opts.Registry.Watch()
if err != nil {
return fmt.Errorf("failed to create local registry watcher: %v", err)
}
networkRegWatcher, err := r.opts.NetworkRegistry.Watch()
networkWatcher, err := r.opts.Network.Watch()
if err != nil {
return fmt.Errorf("failed to create network registry watcher: %v", err)
}
@ -115,14 +110,14 @@ func (r *router) Start() error {
go func() {
defer r.wg.Done()
// watch local registry and register routes in routine table
errChan <- r.manageServiceRoutes(localRegWatcher, r.opts.Address, DefaultLocalMetric)
errChan <- r.manageServiceRoutes(localWatcher, DefaultLocalMetric)
}()
r.wg.Add(1)
go func() {
defer r.wg.Done()
// watch network registry and register routes in routine table
errChan <- r.manageServiceRoutes(networkRegWatcher, r.opts.NetworkAddress, DefaultNetworkMetric)
errChan <- r.manageServiceRoutes(networkWatcher, DefaultNetworkMetric)
}()
r.wg.Add(1)
@ -138,19 +133,19 @@ func (r *router) Start() error {
// addServiceRouteslists all available services in given registry and adds them to the routing table.
// 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.
func (r *router) addServiceRoutes(reg registry.Registry, network string, metric int) error {
func (r *router) addServiceRoutes(reg registry.Registry, metric int) error {
services, err := reg.ListServices()
if err != nil {
return fmt.Errorf("failed to list services: %v", err)
}
for _, service := range services {
route := NewRoute(
DestAddr(service.Name),
Gateway(r),
Network(network),
Metric(metric),
)
route := Route{
Destination: service.Name,
Router: r,
Network: r.opts.Advertise,
Metric: metric,
}
if err := r.opts.Table.Add(route); err != nil && err != ErrDuplicateRoute {
return fmt.Errorf("error adding route for service: %s", service.Name)
}
@ -160,15 +155,15 @@ func (r *router) addServiceRoutes(reg registry.Registry, network string, metric
}
// parseToNode parses router into registry.Node and returns the result.
// It returns error if the router network address could not be parsed into service host and port.
// NOTE: We use ":" as the default delimiter when we split the network address.
// It returns error if the router network address could not be parsed into host and port.
// NOTE: We use ":" as the delimiter when we splitting the router network address.
func (r *router) parseToNode() (*registry.Node, error) {
// split on ":" as a standard host/port delimiter
addr := strings.Split(r.opts.NetworkAddress, ":")
addr := strings.Split(r.opts.Advertise, ":")
// try to parse network port into integer
port, err := strconv.Atoi(addr[1])
if err != nil {
return nil, fmt.Errorf("could not parse router network address from %s: %v", r.opts.NetworkAddress, err)
return nil, fmt.Errorf("could not parse router network address: %v", err)
}
node := &registry.Node{
@ -184,7 +179,7 @@ func (r *router) parseToNode() (*registry.Node, error) {
// 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
ticker := time.NewTicker(AdvertiseToNetworkTick)
ticker := time.NewTicker(AdvertiseTick)
for {
select {
@ -192,7 +187,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
return nil
case <-ticker.C:
// list all local services
services, err := r.opts.LocalRegistry.ListServices()
services, err := r.opts.Registry.ListServices()
if err != nil {
return fmt.Errorf("failed to list local services: %v", err)
}
@ -203,7 +198,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
Nodes: []*registry.Node{node},
}
// register the local service in the network registry
if err := r.opts.NetworkRegistry.Register(svc, registry.RegisterTTL(AdvertiseNetworkTTL)); err != nil {
if err := r.opts.Network.Register(svc, registry.RegisterTTL(AdvertiseTTL)); err != nil {
return fmt.Errorf("failed to register service %s in network registry: %v", svc.Name, err)
}
}
@ -213,7 +208,7 @@ func (r *router) advertiseToNetwork(node *registry.Node) error {
// 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.
func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric int) error {
func (r *router) manageServiceRoutes(w registry.Watcher, metric int) error {
// wait in the background for the router to stop
// when the router stops, stop the watcher and exit
r.wg.Add(1)
@ -236,17 +231,17 @@ func (r *router) manageServiceRoutes(w registry.Watcher, network string, metric
break
}
route := NewRoute(
DestAddr(res.Service.Name),
Gateway(r),
Network(network),
Metric(metric),
)
route := Route{
Destination: res.Service.Name,
Router: r,
Network: r.opts.Advertise,
Metric: metric,
}
switch res.Action {
case "create":
if len(res.Service.Nodes) > 0 {
/// only return error if the route is not duplicate, but something else has failed
// 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", res.Service.Name)
}
@ -274,8 +269,7 @@ func (r *router) Stop() error {
// NOTE: we need a more efficient way of doing this e.g. network routes
// should ideally be autodeleted when the router stops gossiping
// deregister all services advertised by this router from remote registry
query := NewQuery(QueryGateway(r), QueryNetwork(r.opts.NetworkAddress))
query := NewQuery(QueryRouter(r), QueryNetwork(r.opts.Advertise))
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)
@ -289,10 +283,10 @@ func (r *router) Stop() error {
for _, route := range routes {
service := &registry.Service{
Name: route.Options().DestAddr,
Name: route.Destination,
Nodes: []*registry.Node{node},
}
if err := r.opts.NetworkRegistry.Deregister(service); err != nil {
if err := r.opts.Network.Deregister(service); err != nil {
return fmt.Errorf("failed to deregister service %s from network registry: %v", service.Name, err)
}
}
@ -310,7 +304,7 @@ func (r *router) String() string {
data := []string{
r.opts.ID,
r.opts.Address,
r.opts.NetworkAddress,
r.opts.Advertise,
fmt.Sprintf("%d", r.opts.Table.Size()),
}
table.Append(data)

View File

@ -64,7 +64,7 @@ func (t *table) Options() TableOptions {
// Add adds a route to the routing table
func (t *table) Add(r Route) error {
destAddr := r.Options().DestAddr
destAddr := r.Destination
sum := t.hash(r)
t.Lock()
@ -86,7 +86,7 @@ func (t *table) Add(r Route) error {
}
// only add the route if the route override is explicitly requested
if _, ok := t.m[destAddr][sum]; ok && r.Options().Policy == OverrideIfExists {
if _, ok := t.m[destAddr][sum]; ok && r.Policy == OverrideIfExists {
t.m[destAddr][sum] = r
go t.sendEvent(&Event{Type: UpdateEvent, Route: r})
return nil
@ -94,7 +94,7 @@ func (t *table) Add(r Route) error {
// if we reached this point without already returning the route already exists
// we return nil only if explicitly requested by the client
if r.Options().Policy == IgnoreIfExists {
if r.Policy == IgnoreIfExists {
return nil
}
@ -106,7 +106,7 @@ func (t *table) Delete(r Route) error {
t.Lock()
defer t.Unlock()
destAddr := r.Options().DestAddr
destAddr := r.Destination
sum := t.hash(r)
if _, ok := t.m[destAddr]; !ok {
@ -121,7 +121,7 @@ func (t *table) Delete(r Route) error {
// Update updates routing table with new route
func (t *table) Update(r Route) error {
destAddr := r.Options().DestAddr
destAddr := r.Destination
sum := t.hash(r)
t.Lock()
@ -165,24 +165,28 @@ func (t *table) Lookup(q Query) ([]Route, error) {
var results []Route
for destAddr, routes := range t.m {
if q.Options().DestAddr != "*" {
if q.Options().DestAddr != destAddr {
if q.Options().Destination != "*" {
if q.Options().Destination != destAddr {
continue
}
for _, route := range routes {
if q.Options().Network == "*" || q.Options().Network == route.Options().Network {
if q.Options().Gateway.ID() == "*" || q.Options().Gateway.ID() == route.Options().Gateway.ID() {
results = append(results, route)
if q.Options().Network == "*" || q.Options().Network == route.Network {
if q.Options().Router.ID() == "*" || q.Options().Router.ID() == route.Router.ID() {
if route.Metric <= q.Options().Metric {
results = append(results, route)
}
}
}
}
}
if q.Options().DestAddr == "*" {
if q.Options().Destination == "*" {
for _, route := range routes {
if q.Options().Network == "*" || q.Options().Network == route.Options().Network {
if q.Options().Gateway.ID() == "*" || q.Options().Gateway.ID() == route.Options().Gateway.ID() {
results = append(results, route)
if q.Options().Network == "*" || q.Options().Network == route.Router.Network() {
if q.Options().Router.ID() == "*" || q.Options().Router.ID() == route.Router.ID() {
if route.Metric <= q.Options().Metric {
results = append(results, route)
}
}
}
}
@ -200,8 +204,8 @@ func (t *table) Lookup(q Query) ([]Route, error) {
func (t *table) Watch(opts ...WatchOption) (Watcher, error) {
// by default watch everything
wopts := WatchOptions{
DestAddr: "*",
Network: "*",
Destination: "*",
Network: "*",
}
for _, o := range opts {
@ -252,15 +256,15 @@ func (t *table) String() string {
// create nice table printing structure
table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
for _, destRoute := range t.m {
for _, route := range destRoute {
strRoute := []string{
route.Options().DestAddr,
route.Options().Gateway.Address(),
route.Options().Network,
fmt.Sprintf("%d", route.Options().Metric),
route.Destination,
route.Router.Address(),
route.Network,
fmt.Sprintf("%d", route.Metric),
}
table.Append(strRoute)
}
@ -274,12 +278,12 @@ func (t *table) String() string {
// hash hashes the route using router gateway and network address
func (t *table) hash(r Route) uint64 {
destAddr := r.Options().DestAddr
gwAddr := r.Options().Gateway.Address()
netAddr := r.Options().Network
destAddr := r.Destination
routerAddr := r.Router.Address()
netAddr := r.Network
t.h.Reset()
t.h.Write([]byte(destAddr + gwAddr + netAddr))
t.h.Write([]byte(destAddr + routerAddr + netAddr))
return t.h.Sum64()
}

View File

@ -6,31 +6,26 @@ import (
)
var (
// DefaultGossipAddress is default gossip bind address
DefaultGossipAddress = ":9093"
// DefaultNetworkAddress is default network bind address
DefaultNetworkAddress = ":9094"
// DefaultAddress is default router address
DefaultAddress = ":9093"
// DefaultAdvertise is default address advertised to the network
DefaultAdvertise = ":9094"
)
// Options are router options
type Options struct {
// ID is router id
ID string
// Address is router micro service address
// Address is router address
Address string
// GossipAddress is router gossip address
GossipAddress string
// NetworkAddress is micro network address
NetworkAddress string
// LocalRegistry is router local registry
LocalRegistry registry.Registry
// NetworkRegistry is router remote registry
// NOTE: we need some abstraction on top of gossip.Registry
NetworkRegistry registry.Registry
// Advertise is the address advertised to the network
Advertise string
// Registry is the local registry
Registry registry.Registry
// Networkis the network registry
Network registry.Registry
// Table is routing table
Table Table
// RIB is Routing Information Base
RIB RIB
}
// ID sets Router ID
@ -47,59 +42,43 @@ func Address(a string) Option {
}
}
// GossipAddress sets router gossip address
func GossipAddress(a string) Option {
// Advertise sets the address that is advertise to the network
func Advertise(n string) Option {
return func(o *Options) {
o.GossipAddress = a
o.Advertise = n
}
}
// NetworkAddress sets router network address
func NetworkAddress(n string) Option {
return func(o *Options) {
o.NetworkAddress = n
}
}
// RoutingTable allows to specify custom routing table
// RoutingTable sets the routing table
func RoutingTable(t Table) Option {
return func(o *Options) {
o.Table = t
}
}
// LocalRegistry allows to specify local registry
func LocalRegistry(r registry.Registry) Option {
// Registry sets the local registry
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.LocalRegistry = r
o.Registry = r
}
}
// NetworkRegistry allows to specify remote registry
func NetworkRegistry(r registry.Registry) Option {
// Network sets the network registry
func Network(r registry.Registry) Option {
return func(o *Options) {
o.NetworkRegistry = r
}
}
// RouterRIB allows to configure RIB
func RouterRIB(r RIB) Option {
return func(o *Options) {
o.RIB = r
o.Network = r
}
}
// DefaultOptions returns router default options
func DefaultOptions() Options {
// NOTE: by default both local and network registies use default registry i.e. mdns
// TODO: DefaultRIB needs to be added once it's properly figured out
return Options{
ID: uuid.New().String(),
Address: ":8083",
GossipAddress: DefaultGossipAddress,
NetworkAddress: DefaultNetworkAddress,
LocalRegistry: registry.DefaultRegistry,
NetworkRegistry: registry.DefaultRegistry,
Table: NewTable(),
ID: uuid.New().String(),
Address: DefaultAddress,
Advertise: DefaultAdvertise,
Registry: registry.DefaultRegistry,
Network: registry.DefaultRegistry,
Table: NewTable(),
}
}

View File

@ -27,20 +27,22 @@ type QueryOption func(*QueryOptions)
// QueryOptions are routing table query options
type QueryOptions struct {
// DestAddr is destination address
DestAddr string
// NetworkAddress is network address
// Destination is destination address
Destination string
// Network is network address
Network string
// Gateway is gateway address
Gateway Router
// Router is gateway address
Router Router
// Metric is route metric
Metric int
// Policy is query lookup policy
Policy LookupPolicy
}
// QueryDestAddr sets query destination address
func QueryDestAddr(a string) QueryOption {
// QueryDestination sets query destination address
func QueryDestination(a string) QueryOption {
return func(o *QueryOptions) {
o.DestAddr = a
o.Destination = a
}
}
@ -51,14 +53,22 @@ func QueryNetwork(a string) QueryOption {
}
}
// QueryGateway sets query gateway address
func QueryGateway(r Router) QueryOption {
// QueryRouter sets query gateway address
func QueryRouter(r Router) QueryOption {
return func(o *QueryOptions) {
o.Gateway = r
o.Router = r
}
}
// QueryMetric sets query metric
func QueryMetric(m int) QueryOption {
return func(o *QueryOptions) {
o.Metric = m
}
}
// QueryPolicy sets query policy
// NOTE: this might be renamed to filter or some such
func QueryPolicy(p LookupPolicy) QueryOption {
return func(o *QueryOptions) {
o.Policy = p
@ -82,11 +92,13 @@ func NewQuery(opts ...QueryOption) Query {
r := newRouter(ID("*"))
// default options
// NOTE: by default we use DefaultNetworkMetric
qopts := QueryOptions{
DestAddr: "*",
Network: "*",
Gateway: r,
Policy: DiscardNoRoute,
Destination: "*",
Network: "*",
Router: r,
Metric: DefaultNetworkMetric,
Policy: DiscardNoRoute,
}
for _, o := range opts {

View File

@ -1,30 +0,0 @@
package router
// RIB is Routing Information Base.
// RIB is used to source the base routing table.
type RIB interface {
// Initi initializes RIB
Init(...RIBOption) error
// Options returns RIB options
Options() RIBOptions
// Routes returns routes
Routes() []Route
// String returns debug info
String() string
}
// RIBOption sets RIB options
type RIBOption func(*RIBOptions)
// RIBOptions are RIB options
type RIBOptions struct {
// Source defines RIB source URL
Source string
}
// Source sets RIB source
func Source(s string) RIBOption {
return func(o *RIBOptions) {
o.Source = s
}
}

View File

@ -14,18 +14,18 @@ var (
DefaultNetworkMetric = 10
)
// AddPolicy defines routing table addition policy
type AddPolicy int
// RoutePolicy defines routing table addition policy
type RoutePolicy int
const (
// OverrideIfExists overrides route if it already exists
OverrideIfExists AddPolicy = iota
OverrideIfExists RoutePolicy = iota
// IgnoreIfExists does not modify existing route
IgnoreIfExists
)
// String returns human reprensentation of policy
func (p AddPolicy) String() string {
func (p RoutePolicy) String() string {
switch p {
case OverrideIfExists:
return "OVERRIDE"
@ -36,100 +36,34 @@ func (p AddPolicy) String() string {
}
}
// RouteOption is used to set routing table entry options
type RouteOption func(*RouteOptions)
// RouteOptions are route options
type RouteOptions struct {
// DestAddr is destination address
DestAddr string
// Gateway is the next route hop
Gateway Router
// Network defines micro network
// Route is network route
type Route struct {
// Destination is destination address
Destination string
// Router is the network router
Router Router
// Network is micro network address
Network string
// Metric is route cost metric
// Metric is the route cost metric
Metric int
// Policy defines route addition policy
Policy AddPolicy
}
// DestAddr sets destination address
func DestAddr(a string) RouteOption {
return func(o *RouteOptions) {
o.DestAddr = a
}
}
// Gateway sets the route gateway
func Gateway(r Router) RouteOption {
return func(o *RouteOptions) {
o.Gateway = r
}
}
// Network sets micro network
func Network(n string) RouteOption {
return func(o *RouteOptions) {
o.Network = n
}
}
// Metric sets route metric
func Metric(m int) RouteOption {
return func(o *RouteOptions) {
o.Metric = m
}
}
// RoutePolicy sets add route policy
func RoutePolicy(p AddPolicy) RouteOption {
return func(o *RouteOptions) {
o.Policy = p
}
}
// Route is routing table route
type Route interface {
// Options returns route options
Options() RouteOptions
}
type route struct {
opts RouteOptions
}
// NewRoute returns new routing table route
func NewRoute(opts ...RouteOption) Route {
eopts := RouteOptions{}
for _, o := range opts {
o(&eopts)
}
return &route{
opts: eopts,
}
}
// Options returns route options
func (r *route) Options() RouteOptions {
return r.opts
// Policy defines route policy
Policy RoutePolicy
}
// String allows to print the route
func (r *route) String() string {
func (r *Route) String() string {
// this will help us build routing table string
sb := &strings.Builder{}
// create nice table printing structure
table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
strRoute := []string{
r.opts.DestAddr,
r.opts.Gateway.Address(),
r.opts.Network,
fmt.Sprintf("%d", r.opts.Metric),
r.Destination,
r.Router.Address(),
r.Network,
fmt.Sprintf("%d", r.Metric),
}
table.Append(strRoute)

View File

@ -13,8 +13,6 @@ type Router interface {
Table() Table
// Address returns the router adddress
Address() string
// Gossip returns the router gossip address
Gossip() string
// Network returns the router network address
Network() string
// Start starts the router

View File

@ -54,6 +54,8 @@ type WatchOption func(*WatchOptions)
type Watcher interface {
// Next is a blocking call that returns watch result
Next() (*Event, error)
// Chan returns event channel
Chan() (<-chan *Event, error)
// Stop stops watcher
Stop()
}
@ -61,16 +63,16 @@ type Watcher interface {
// WatchOptions are table watcher options
type WatchOptions struct {
// Specify destination address to watch
DestAddr string
Destination string
// Specify network to watch
Network string
}
// WatchDestAddr sets what destination to watch
// WatchDestination sets what destination to watch
// Destination is usually microservice name
func WatchDestAddr(a string) WatchOption {
func WatchDestination(a string) WatchOption {
return func(o *WatchOptions) {
o.DestAddr = a
o.Destination = a
}
}
@ -94,13 +96,13 @@ func (w *tableWatcher) Next() (*Event, error) {
for {
select {
case res := <-w.resChan:
switch w.opts.DestAddr {
switch w.opts.Destination {
case "*", "":
if w.opts.Network == "*" || w.opts.Network == res.Route.Options().Network {
if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
return res, nil
}
case res.Route.Options().DestAddr:
if w.opts.Network == "*" || w.opts.Network == res.Route.Options().Network {
case res.Route.Destination:
if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
return res, nil
}
}
@ -110,6 +112,11 @@ func (w *tableWatcher) Next() (*Event, error) {
}
}
// Chan returns watcher events channel
func (w *tableWatcher) Chan() (<-chan *Event, error) {
return w.resChan, nil
}
// Stop stops routing table watcher
func (w *tableWatcher) Stop() {
select {
@ -125,10 +132,10 @@ func (w *tableWatcher) String() string {
sb := &strings.Builder{}
table := tablewriter.NewWriter(sb)
table.SetHeader([]string{"DestAddr", "Network"})
table.SetHeader([]string{"Destination", "Network"})
data := []string{
w.opts.DestAddr,
w.opts.Destination,
w.opts.Network,
}
table.Append(data)