2020-07-27 15:22:00 +03:00
|
|
|
package registry
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2019-07-10 19:46:22 +03:00
|
|
|
"time"
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/logger"
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
2019-08-28 01:08:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// table is an in-memory routing table
|
2019-07-29 14:40:13 +03:00
|
|
|
type table struct {
|
2019-07-29 14:44:28 +03:00
|
|
|
sync.RWMutex
|
2020-06-30 11:53:49 +03:00
|
|
|
// fetchRoutes for a service
|
|
|
|
fetchRoutes func(string) error
|
2019-07-26 01:19:05 +03:00
|
|
|
// routes stores service routes
|
2020-07-27 15:22:00 +03:00
|
|
|
routes map[string]map[uint64]router.Route
|
2019-07-26 01:19:05 +03:00
|
|
|
// watchers stores table watchers
|
|
|
|
watchers map[string]*tableWatcher
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 14:40:13 +03:00
|
|
|
// newtable creates a new routing table and returns it
|
2020-07-27 15:22:00 +03:00
|
|
|
func newTable(fetchRoutes func(string) error, opts ...router.Option) *table {
|
2019-07-29 14:40:13 +03:00
|
|
|
return &table{
|
2020-06-30 11:53:49 +03:00
|
|
|
fetchRoutes: fetchRoutes,
|
2020-07-27 15:22:00 +03:00
|
|
|
routes: make(map[string]map[uint64]router.Route),
|
2020-06-30 11:53:49 +03:00
|
|
|
watchers: make(map[string]*tableWatcher),
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// sendEvent sends events to all subscribed watchers
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) sendEvent(e *router.Event) {
|
2019-08-28 01:08:35 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2020-01-23 14:44:06 +03:00
|
|
|
if len(e.Id) == 0 {
|
|
|
|
e.Id = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
for _, w := range t.watchers {
|
|
|
|
select {
|
|
|
|
case w.resChan <- e:
|
|
|
|
case <-w.done:
|
2020-01-17 18:23:10 +03:00
|
|
|
// don't block forever
|
|
|
|
case <-time.After(time.Second):
|
2019-08-28 01:08:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:46:22 +03:00
|
|
|
// Create creates new route in the routing table
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Create(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if there are any routes in the table for the route destination
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
t.routes[service] = make(map[uint64]router.Route)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// add new route to the table for the route destination
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
t.routes[service][sum] = r
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-07-27 15:22:00 +03:00
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Create, r.Address)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
go t.sendEvent(&router.Event{Type: router.Create, Timestamp: time.Now(), Route: r})
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
return router.ErrDuplicateRoute
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:51:13 +03:00
|
|
|
// Delete deletes the route from the routing table
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Delete(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
return router.ErrRouteNotFound
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-09-26 13:56:30 +03:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
return router.ErrRouteNotFound
|
2019-08-29 18:25:21 +03:00
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-09-26 13:56:30 +03:00
|
|
|
delete(t.routes[service], sum)
|
2020-07-01 17:20:30 +03:00
|
|
|
if len(t.routes[service]) == 0 {
|
|
|
|
delete(t.routes, service)
|
|
|
|
}
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-07-27 15:22:00 +03:00
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Delete, r.Address)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
go t.sendEvent(&router.Event{Type: router.Delete, Timestamp: time.Now(), Route: r})
|
2019-09-26 13:56:30 +03:00
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// Update updates routing table with the new route
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Update(r router.Route) error {
|
2019-07-09 17:45:42 +03:00
|
|
|
service := r.Service
|
2019-07-08 18:16:50 +03:00
|
|
|
sum := r.Hash()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// check if the route destination has any routes in the table
|
2019-07-26 01:19:05 +03:00
|
|
|
if _, ok := t.routes[service]; !ok {
|
2020-07-27 15:22:00 +03:00
|
|
|
t.routes[service] = make(map[uint64]router.Route)
|
2019-08-29 18:21:30 +03:00
|
|
|
}
|
2019-07-10 23:28:32 +03:00
|
|
|
|
2019-09-26 14:45:10 +03:00
|
|
|
if _, ok := t.routes[service][sum]; !ok {
|
|
|
|
t.routes[service][sum] = r
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-07-27 15:22:00 +03:00
|
|
|
logger.Debugf("Router emitting %s for route: %s", router.Update, r.Address)
|
2020-03-11 20:55:39 +03:00
|
|
|
}
|
2020-07-27 15:22:00 +03:00
|
|
|
go t.sendEvent(&router.Event{Type: router.Update, Timestamp: time.Now(), Route: r})
|
2019-09-26 14:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// just update the route, but dont emit Update event
|
2019-09-13 20:46:24 +03:00
|
|
|
t.routes[service][sum] = r
|
|
|
|
|
2019-07-10 23:28:32 +03:00
|
|
|
return nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 20:01:48 +03:00
|
|
|
// List returns a list of all routes in the table
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) List() ([]router.Route, error) {
|
2019-06-19 20:01:48 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
var routes []router.Route
|
2019-07-26 01:19:05 +03:00
|
|
|
for _, rmap := range t.routes {
|
2019-06-19 20:01:48 +03:00
|
|
|
for _, route := range rmap {
|
|
|
|
routes = append(routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// isMatch checks if the route matches given query options
|
2020-07-27 15:22:00 +03:00
|
|
|
func isMatch(route router.Route, address, gateway, network, rtr string, strategy router.Strategy) bool {
|
2019-10-09 19:13:52 +03:00
|
|
|
// matches the values provided
|
|
|
|
match := func(a, b string) bool {
|
2020-06-29 18:37:45 +03:00
|
|
|
if a == "*" || b == "*" || a == b {
|
2019-10-09 19:13:52 +03:00
|
|
|
return true
|
2019-07-01 22:33:08 +03:00
|
|
|
}
|
2019-10-09 19:13:52 +03:00
|
|
|
return false
|
2019-07-01 22:33:08 +03:00
|
|
|
}
|
2019-10-09 19:13:52 +03:00
|
|
|
|
|
|
|
// a simple struct to hold our values
|
|
|
|
type compare struct {
|
|
|
|
a string
|
|
|
|
b string
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:48:36 +03:00
|
|
|
// by default assume we are querying all routes
|
|
|
|
link := "*"
|
|
|
|
// if AdvertiseLocal change the link query accordingly
|
2020-07-27 15:22:00 +03:00
|
|
|
if strategy == router.AdvertiseLocal {
|
2020-01-16 15:48:36 +03:00
|
|
|
link = "local"
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
// compare the following values
|
|
|
|
values := []compare{
|
|
|
|
{gateway, route.Gateway},
|
|
|
|
{network, route.Network},
|
2020-07-27 15:22:00 +03:00
|
|
|
{rtr, route.Router},
|
2019-10-09 19:13:52 +03:00
|
|
|
{address, route.Address},
|
2020-01-16 15:48:36 +03:00
|
|
|
{link, route.Link},
|
2019-10-09 19:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range values {
|
|
|
|
// attempt to match each value
|
|
|
|
if !match(v.a, v.b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-07-01 22:33:08 +03:00
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// findRoutes finds all the routes for given network and router and returns them
|
2020-07-27 15:22:00 +03:00
|
|
|
func findRoutes(routes map[uint64]router.Route, address, gateway, network, rtr string, strategy router.Strategy) []router.Route {
|
2020-01-16 15:48:36 +03:00
|
|
|
// routeMap stores the routes we're going to advertise
|
2020-07-27 15:22:00 +03:00
|
|
|
routeMap := make(map[string][]router.Route)
|
2020-01-16 15:48:36 +03:00
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
for _, route := range routes {
|
2020-07-27 15:22:00 +03:00
|
|
|
if isMatch(route, address, gateway, network, rtr, strategy) {
|
2020-01-16 15:48:36 +03:00
|
|
|
// add matchihg route to the routeMap
|
|
|
|
routeKey := route.Service + "@" + route.Network
|
|
|
|
// append the first found route to routeMap
|
|
|
|
_, ok := routeMap[routeKey]
|
|
|
|
if !ok {
|
|
|
|
routeMap[routeKey] = append(routeMap[routeKey], route)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if AdvertiseAll, keep appending
|
2020-07-27 15:22:00 +03:00
|
|
|
if strategy == router.AdvertiseAll || strategy == router.AdvertiseLocal {
|
2020-01-16 15:48:36 +03:00
|
|
|
routeMap[routeKey] = append(routeMap[routeKey], route)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we're going to find the best routes
|
2020-07-27 15:22:00 +03:00
|
|
|
if strategy == router.AdvertiseBest {
|
2020-01-16 15:48:36 +03:00
|
|
|
// if the current optimal route metric is higher than routing table route, replace it
|
|
|
|
if len(routeMap[routeKey]) > 0 {
|
|
|
|
// NOTE: we know that when AdvertiseBest is set, we only ever have one item in current
|
|
|
|
if routeMap[routeKey][0].Metric > route.Metric {
|
|
|
|
routeMap[routeKey][0] = route
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2019-07-01 22:33:08 +03:00
|
|
|
}
|
2020-01-16 15:48:36 +03:00
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
var results []router.Route
|
2020-01-16 15:48:36 +03:00
|
|
|
for _, route := range routeMap {
|
|
|
|
results = append(results, route...)
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
return results
|
|
|
|
}
|
2019-06-13 14:09:49 +03:00
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// Lookup queries routing table and returns all routes that match the lookup query
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Query(q ...router.QueryOption) ([]router.Route, error) {
|
2019-10-09 19:13:52 +03:00
|
|
|
// create new query options
|
2020-07-27 15:22:00 +03:00
|
|
|
opts := router.NewQuery(q...)
|
2019-10-09 19:13:52 +03:00
|
|
|
|
2020-01-16 15:48:36 +03:00
|
|
|
// create a cwslicelist of query results
|
2020-07-27 15:22:00 +03:00
|
|
|
results := make([]router.Route, 0, len(t.routes))
|
2020-01-16 15:48:36 +03:00
|
|
|
|
|
|
|
// if No routes are queried, return early
|
2020-07-27 15:22:00 +03:00
|
|
|
if opts.Strategy == router.AdvertiseNone {
|
2020-01-16 15:48:36 +03:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:29:11 +03:00
|
|
|
// readAndFilter routes for this service under read lock.
|
2020-07-27 15:22:00 +03:00
|
|
|
readAndFilter := func() ([]router.Route, bool) {
|
2020-07-02 20:29:11 +03:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
routes, ok := t.routes[opts.Service]
|
|
|
|
if !ok || len(routes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy), true
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:13:52 +03:00
|
|
|
if opts.Service != "*" {
|
2020-06-30 11:53:49 +03:00
|
|
|
// try and load services from the cache
|
2020-07-02 20:29:11 +03:00
|
|
|
if routes, ok := readAndFilter(); ok {
|
|
|
|
return routes, nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2020-06-30 11:53:49 +03:00
|
|
|
|
|
|
|
// load the cache and try again
|
|
|
|
if err := t.fetchRoutes(opts.Service); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-02 20:29:11 +03:00
|
|
|
|
|
|
|
// try again
|
|
|
|
if routes, ok := readAndFilter(); ok {
|
|
|
|
return routes, nil
|
2020-06-30 11:53:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
return nil, router.ErrRouteNotFound
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 22:33:08 +03:00
|
|
|
// search through all destinations
|
2020-07-02 20:29:11 +03:00
|
|
|
t.RLock()
|
2019-07-26 01:19:05 +03:00
|
|
|
for _, routes := range t.routes {
|
2020-01-16 15:48:36 +03:00
|
|
|
results = append(results, findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router, opts.Strategy)...)
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
2020-07-02 20:29:11 +03:00
|
|
|
t.RUnlock()
|
2019-06-13 14:09:49 +03:00
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch returns routing table entry watcher
|
2020-07-27 15:22:00 +03:00
|
|
|
func (t *table) Watch(opts ...router.WatchOption) (router.Watcher, error) {
|
2019-06-13 14:09:49 +03:00
|
|
|
// by default watch everything
|
2020-07-27 15:22:00 +03:00
|
|
|
wopts := router.WatchOptions{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: "*",
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&wopts)
|
|
|
|
}
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
w := &tableWatcher{
|
2019-08-02 16:59:08 +03:00
|
|
|
id: uuid.New().String(),
|
2019-06-13 14:09:49 +03:00
|
|
|
opts: wopts,
|
2020-07-27 15:22:00 +03:00
|
|
|
resChan: make(chan *router.Event, 10),
|
2019-06-13 14:09:49 +03:00
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:59:08 +03:00
|
|
|
// when the watcher is stopped delete it
|
|
|
|
go func() {
|
|
|
|
<-w.done
|
|
|
|
t.Lock()
|
|
|
|
delete(t.watchers, w.id)
|
|
|
|
t.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// save the watcher
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Lock()
|
2019-08-02 16:59:08 +03:00
|
|
|
t.watchers[w.id] = w
|
2019-06-13 14:09:49 +03:00
|
|
|
t.Unlock()
|
|
|
|
|
2019-07-26 01:19:05 +03:00
|
|
|
return w, nil
|
2019-06-13 14:09:49 +03:00
|
|
|
}
|