2019-01-14 15:27:25 +00:00
|
|
|
// Package memory provides an in-memory registry
|
|
|
|
package memory
|
2015-12-09 19:23:16 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-18 17:29:17 +00:00
|
|
|
"context"
|
2019-09-25 18:19:18 +01:00
|
|
|
"strings"
|
2019-01-09 14:24:12 +08:00
|
|
|
"sync"
|
2019-01-18 17:29:17 +00:00
|
|
|
"time"
|
2019-01-09 14:24:12 +08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
"github.com/google/uuid"
|
2015-12-09 19:23:16 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-09-25 16:20:42 +01:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2015-12-09 19:23:16 +00:00
|
|
|
)
|
|
|
|
|
2019-09-25 01:57:37 +01:00
|
|
|
var (
|
2019-09-25 18:19:18 +01:00
|
|
|
sendEventTime = 10 * time.Millisecond
|
|
|
|
ttlPruneTime = 1 * time.Minute
|
|
|
|
DefaultTTL = 1 * time.Minute
|
2019-09-25 01:57:37 +01:00
|
|
|
)
|
|
|
|
|
2019-09-25 16:20:42 +01:00
|
|
|
// node tracks node registration timestamp and TTL
|
|
|
|
type node struct {
|
2019-09-25 18:19:18 +01:00
|
|
|
lastSeen time.Time
|
|
|
|
ttl time.Duration
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
type Registry struct {
|
2019-01-18 17:29:17 +00:00
|
|
|
options registry.Options
|
|
|
|
|
2019-01-09 14:24:12 +08:00
|
|
|
sync.RWMutex
|
2016-02-26 00:09:06 +00:00
|
|
|
Services map[string][]*registry.Service
|
2019-09-25 16:20:42 +01:00
|
|
|
nodes map[string]*node
|
2019-01-18 17:29:17 +00:00
|
|
|
Watchers map[string]*Watcher
|
2016-02-26 00:09:06 +00:00
|
|
|
}
|
2015-12-09 19:23:16 +00:00
|
|
|
|
2019-09-25 01:57:37 +01:00
|
|
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
|
|
|
options := registry.Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2016-03-14 19:15:42 +00:00
|
|
|
|
2019-09-25 01:57:37 +01:00
|
|
|
services := getServices(options.Context)
|
|
|
|
if services == nil {
|
|
|
|
services = make(map[string][]*registry.Service)
|
|
|
|
}
|
|
|
|
|
2019-09-25 18:19:18 +01:00
|
|
|
reg := &Registry{
|
2019-09-25 01:57:37 +01:00
|
|
|
options: options,
|
|
|
|
Services: services,
|
2019-09-25 16:20:42 +01:00
|
|
|
nodes: make(map[string]*node),
|
2019-09-25 01:57:37 +01:00
|
|
|
Watchers: make(map[string]*Watcher),
|
|
|
|
}
|
2019-09-25 18:19:18 +01:00
|
|
|
|
|
|
|
go reg.ttlPrune()
|
|
|
|
|
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeTrackId returns a string we use to track a node of a given service
|
|
|
|
func nodeTrackId(svcName, svcVersion, nodeId string) string {
|
|
|
|
return svcName + "+" + svcVersion + "+" + nodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) ttlPrune() {
|
|
|
|
prune := time.NewTicker(ttlPruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-prune.C:
|
|
|
|
m.Lock()
|
|
|
|
for nodeTrackId, node := range m.nodes {
|
2019-09-29 21:42:30 +01:00
|
|
|
// if the TTL has been set and we exceed the hresholdset by it we stop tracking the node
|
|
|
|
if node.ttl.Seconds() != 0.0 && time.Since(node.lastSeen) > node.ttl {
|
2019-09-25 18:19:18 +01:00
|
|
|
// split nodeTrackID into service Name, Version and Node Id
|
|
|
|
trackIdSplit := strings.Split(nodeTrackId, "+")
|
|
|
|
svcName, svcVersion, nodeId := trackIdSplit[0], trackIdSplit[1], trackIdSplit[2]
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry TTL expired for service %s, node %s", svcName, nodeId)
|
2019-09-25 18:19:18 +01:00
|
|
|
// we need to find a node that expired and delete it from service nodes
|
|
|
|
if _, ok := m.Services[svcName]; ok {
|
|
|
|
for _, service := range m.Services[svcName] {
|
|
|
|
if service.Version != svcVersion {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// find expired service node and delete it
|
|
|
|
var nodes []*registry.Node
|
|
|
|
for _, n := range service.Nodes {
|
|
|
|
var del bool
|
|
|
|
if n.Id == nodeId {
|
|
|
|
del = true
|
|
|
|
}
|
|
|
|
if !del {
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
service.Nodes = nodes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// stop tracking the node
|
|
|
|
delete(m.nodes, nodeTrackId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) sendEvent(r *registry.Result) {
|
2019-01-18 17:29:17 +00:00
|
|
|
var watchers []*Watcher
|
|
|
|
|
|
|
|
m.RLock()
|
|
|
|
for _, w := range m.Watchers {
|
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
|
|
|
delete(m.Watchers, w.id)
|
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
2019-09-25 18:19:18 +01:00
|
|
|
case <-time.After(sendEventTime):
|
2019-01-18 17:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Registry) Init(opts ...registry.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
2019-01-09 14:24:12 +08:00
|
|
|
m.Lock()
|
2019-01-18 17:29:17 +00:00
|
|
|
for k, v := range getServices(m.options.Context) {
|
|
|
|
s := m.Services[k]
|
2019-07-04 11:36:49 +01:00
|
|
|
m.Services[k] = registry.Merge(s, v)
|
2019-01-18 17:29:17 +00:00
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 14:24:12 +08:00
|
|
|
|
2019-01-18 17:29:17 +00:00
|
|
|
func (m *Registry) Options() registry.Options {
|
|
|
|
return m.options
|
|
|
|
}
|
|
|
|
|
2019-07-04 00:15:44 +03:00
|
|
|
func (m *Registry) GetService(name string) ([]*registry.Service, error) {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.RLock()
|
2019-07-04 00:15:44 +03:00
|
|
|
service, ok := m.Services[name]
|
|
|
|
m.RUnlock()
|
|
|
|
if !ok {
|
2016-02-26 00:09:06 +00:00
|
|
|
return nil, registry.ErrNotFound
|
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
|
|
|
|
return service, nil
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) ListServices() ([]*registry.Service, error) {
|
2016-02-26 00:09:06 +00:00
|
|
|
var services []*registry.Service
|
2019-07-04 00:15:44 +03:00
|
|
|
m.RLock()
|
2016-02-26 00:09:06 +00:00
|
|
|
for _, service := range m.Services {
|
|
|
|
services = append(services, service...)
|
|
|
|
}
|
2019-01-18 17:29:17 +00:00
|
|
|
m.RUnlock()
|
2016-02-26 00:09:06 +00:00
|
|
|
return services, nil
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Lock()
|
2019-09-25 01:57:37 +01:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry registering service: %s", s.Name)
|
2019-09-30 12:48:06 +01:00
|
|
|
|
2019-09-25 16:20:42 +01:00
|
|
|
var options registry.RegisterOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-07-04 00:15:44 +03:00
|
|
|
if service, ok := m.Services[s.Name]; !ok {
|
|
|
|
m.Services[s.Name] = []*registry.Service{s}
|
2019-09-25 16:20:42 +01:00
|
|
|
// add all nodes into nodes map to track their TTL
|
|
|
|
for _, n := range s.Nodes {
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry tracking new service: %s, node %s", s.Name, n.Id)
|
2019-09-25 18:19:18 +01:00
|
|
|
m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{
|
|
|
|
lastSeen: time.Now(),
|
|
|
|
ttl: options.TTL,
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
2019-09-30 12:48:06 +01:00
|
|
|
return nil
|
2019-07-04 00:15:44 +03:00
|
|
|
} else {
|
2019-09-30 12:48:06 +01:00
|
|
|
// svcCount keeps the count of all versions of particular service
|
|
|
|
//svcCount := len(service)
|
2019-09-25 16:20:42 +01:00
|
|
|
// svcNodes maintains a list of node Ids per particular service version
|
|
|
|
svcNodes := make(map[string]map[string][]string)
|
|
|
|
// collect all service ids for all service versions
|
2019-09-25 01:57:37 +01:00
|
|
|
for _, s := range service {
|
2019-09-25 16:20:42 +01:00
|
|
|
if _, ok := svcNodes[s.Name]; !ok {
|
|
|
|
svcNodes[s.Name] = make(map[string][]string)
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
2019-09-25 16:20:42 +01:00
|
|
|
if _, ok := svcNodes[s.Name][s.Version]; !ok {
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
svcNodes[s.Name][s.Version] = append(svcNodes[s.Name][s.Version], n.Id)
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 16:20:42 +01:00
|
|
|
// if merged count and original service counts changed we know we are adding a new version of the service
|
2019-09-25 01:57:37 +01:00
|
|
|
merged := registry.Merge(service, []*registry.Service{s})
|
2019-09-30 12:48:06 +01:00
|
|
|
// if the node count of any service [version] changed we know we are adding a new node to the service
|
2019-09-25 01:57:37 +01:00
|
|
|
for _, s := range merged {
|
2019-09-25 16:20:42 +01:00
|
|
|
// we know that if the node counts have changed we need to track new nodes
|
|
|
|
if len(s.Nodes) != len(svcNodes[s.Name][s.Version]) {
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
var found bool
|
|
|
|
for _, id := range svcNodes[s.Name][s.Version] {
|
|
|
|
if n.Id == id {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry tracking new node: %s for service %s", n.Id, s.Name)
|
2019-09-25 18:19:18 +01:00
|
|
|
m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{
|
|
|
|
lastSeen: time.Now(),
|
|
|
|
ttl: options.TTL,
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
m.Services[s.Name] = merged
|
|
|
|
go m.sendEvent(®istry.Result{Action: "update", Service: s})
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-25 18:19:18 +01:00
|
|
|
// refresh the timestamp and TTL of the service node
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
trackId := nodeTrackId(s.Name, s.Version, n.Id)
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry refreshing TTL for node %s for service %s", n.Id, s.Name)
|
2019-09-25 18:19:18 +01:00
|
|
|
if trackedNode, ok := m.nodes[trackId]; ok {
|
|
|
|
trackedNode.lastSeen = time.Now()
|
|
|
|
trackedNode.ttl = options.TTL
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:57:37 +01:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:23:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Deregister(s *registry.Service) error {
|
2019-01-18 17:29:17 +00:00
|
|
|
m.Lock()
|
2019-09-25 01:57:37 +01:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry deregistering service: %s", s.Name)
|
2019-09-30 12:48:06 +01:00
|
|
|
|
2019-07-04 00:15:44 +03:00
|
|
|
if service, ok := m.Services[s.Name]; ok {
|
2019-09-30 12:48:06 +01:00
|
|
|
// svcNodes collects the list of all node Ids for each service version
|
2019-09-25 16:20:42 +01:00
|
|
|
svcNodes := make(map[string]map[string][]string)
|
2019-09-30 12:48:06 +01:00
|
|
|
// collect all service node ids for all service versions
|
|
|
|
for _, svc := range service {
|
|
|
|
if _, ok := svcNodes[svc.Name]; !ok {
|
|
|
|
svcNodes[svc.Name] = make(map[string][]string)
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
2019-09-30 12:48:06 +01:00
|
|
|
if _, ok := svcNodes[svc.Name][svc.Version]; !ok {
|
|
|
|
for _, n := range svc.Nodes {
|
|
|
|
svcNodes[svc.Name][svc.Version] = append(svcNodes[svc.Name][svc.Version], n.Id)
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 12:48:06 +01:00
|
|
|
// if there are no more services we know we have either removed all nodes or there were no nodes
|
|
|
|
if updatedService := registry.Remove(service, []*registry.Service{s}); len(updatedService) == 0 {
|
|
|
|
for _, id := range svcNodes[s.Name][s.Version] {
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry stopped tracking node %s for service %s", id, s.Name)
|
2019-09-30 12:48:06 +01:00
|
|
|
delete(m.nodes, nodeTrackId(s.Name, s.Version, id))
|
|
|
|
go m.sendEvent(®istry.Result{Action: "delete", Service: s})
|
|
|
|
}
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry deleting service %s: no service nodes", s.Name)
|
2019-07-04 00:15:44 +03:00
|
|
|
delete(m.Services, s.Name)
|
2019-09-30 12:48:06 +01:00
|
|
|
return nil
|
2019-07-04 00:15:44 +03:00
|
|
|
} else {
|
2019-09-25 16:20:42 +01:00
|
|
|
// find out which nodes have been removed
|
|
|
|
for _, id := range svcNodes[s.Name][s.Version] {
|
2019-09-30 12:48:06 +01:00
|
|
|
for _, svc := range updatedService {
|
2019-09-25 16:20:42 +01:00
|
|
|
var found bool
|
2019-09-30 12:48:06 +01:00
|
|
|
for _, n := range svc.Nodes {
|
2019-09-25 16:20:42 +01:00
|
|
|
if id == n.Id {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2019-10-02 03:48:51 +03:00
|
|
|
log.Debugf("[memory] Registry stopped tracking node %s for service %s", id, s.Name)
|
2019-09-25 18:19:18 +01:00
|
|
|
delete(m.nodes, nodeTrackId(s.Name, s.Version, id))
|
2019-09-30 12:48:06 +01:00
|
|
|
go m.sendEvent(®istry.Result{Action: "delete", Service: s})
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 12:48:06 +01:00
|
|
|
m.Services[s.Name] = updatedService
|
2019-09-25 16:20:42 +01:00
|
|
|
}
|
2019-07-04 00:15:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 19:23:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
2019-01-18 17:29:17 +00:00
|
|
|
var wo registry.WatchOptions
|
2018-02-19 17:12:37 +00:00
|
|
|
for _, o := range opts {
|
2019-01-18 17:29:17 +00:00
|
|
|
o(&wo)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &Watcher{
|
|
|
|
exit: make(chan bool),
|
|
|
|
res: make(chan *registry.Result),
|
|
|
|
id: uuid.New().String(),
|
|
|
|
wo: wo,
|
2018-02-19 17:12:37 +00:00
|
|
|
}
|
2019-01-18 17:29:17 +00:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
m.Watchers[w.id] = w
|
|
|
|
m.Unlock()
|
|
|
|
return w, nil
|
2015-12-09 19:23:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func (m *Registry) String() string {
|
|
|
|
return "memory"
|
2015-12-19 21:56:14 +00:00
|
|
|
}
|