From 16c7b3a39018ab41ddd979bf7c6104c244345faf Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 25 Sep 2019 16:20:42 +0100 Subject: [PATCH 1/9] Added Registry TTL to memory registry. Tracking node lifetimes. --- registry/handler/handler.go | 10 +++- registry/memory/memory.go | 114 +++++++++++++++++++++++++++++++----- registry/service/service.go | 7 ++- 3 files changed, 114 insertions(+), 17 deletions(-) diff --git a/registry/handler/handler.go b/registry/handler/handler.go index f86148cd..5c7fd5c9 100644 --- a/registry/handler/handler.go +++ b/registry/handler/handler.go @@ -2,6 +2,7 @@ package handler import ( "context" + "time" "github.com/micro/go-micro/errors" "github.com/micro/go-micro/registry" @@ -26,10 +27,17 @@ func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.G } func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error { - err := r.Registry.Register(service.ToService(req)) + var regOpts []registry.RegisterOption + regTTL, ok := ctx.Value("register_ttl").(time.Duration) + if ok { + regOpts = append(regOpts, registry.RegisterTTL(regTTL)) + } + + err := r.Registry.Register(service.ToService(req), regOpts...) if err != nil { return errors.InternalServerError("go.micro.registry", err.Error()) } + return nil } diff --git a/registry/memory/memory.go b/registry/memory/memory.go index 446aa0f0..c9e8b353 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -8,17 +8,25 @@ import ( "github.com/google/uuid" "github.com/micro/go-micro/registry" + "github.com/micro/go-micro/util/log" ) var ( timeout = time.Millisecond * 10 ) +// node tracks node registration timestamp and TTL +type node struct { + ts time.Time + ttl time.Duration +} + type Registry struct { options registry.Options sync.RWMutex Services map[string][]*registry.Service + nodes map[string]*node Watchers map[string]*Watcher } @@ -39,6 +47,7 @@ func NewRegistry(opts ...registry.Option) registry.Registry { return &Registry{ options: options, Services: services, + nodes: make(map[string]*node), Watchers: make(map[string]*Watcher), } } @@ -111,30 +120,73 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption m.Lock() defer m.Unlock() + var options registry.RegisterOptions + for _, o := range opts { + o(&options) + } + if service, ok := m.Services[s.Name]; !ok { m.Services[s.Name] = []*registry.Service{s} - go m.sendEvent(®istry.Result{Action: "update", Service: s}) - } else { - svcCount := len(service) - svcNodeCounts := make(map[string]map[string]int) - for _, s := range service { - if _, ok := svcNodeCounts[s.Name]; !ok { - svcNodeCounts[s.Name] = make(map[string]int) - } - if _, ok := svcNodeCounts[s.Name][s.Version]; !ok { - svcNodeCounts[s.Name][s.Version] = len(s.Nodes) + // add all nodes into nodes map to track their TTL + for _, n := range s.Nodes { + log.Logf("Tracking node %s for service %s", n.Id, s.Name) + m.nodes[s.Name+n.Id] = &node{ + ts: time.Now(), + ttl: options.TTL, } } - // if merged count and original service counts changed we added new version of the service + go m.sendEvent(®istry.Result{Action: "update", Service: s}) + } else { + // svcCount essentially keep the count of all service vesions + svcCount := len(service) + // 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 + for _, s := range service { + if _, ok := svcNodes[s.Name]; !ok { + svcNodes[s.Name] = make(map[string][]string) + } + 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) + } + } + } + // if merged count and original service counts changed we know we are adding a new version of the service merged := registry.Merge(service, []*registry.Service{s}) if len(merged) != svcCount { m.Services[s.Name] = merged + // we know s is the new [version of] service; we need to strart tracking its nodes + for _, n := range s.Nodes { + log.Logf("Tracking node %s for service %s", n.Id, s.Name) + m.nodes[s.Name+n.Id] = &node{ + ts: time.Now(), + ttl: options.TTL, + } + } go m.sendEvent(®istry.Result{Action: "update", Service: s}) return nil } - // if the node count for a particular service has changed we added a new node to the service + // if the node count of any particular service [version] changed we know we are adding a new node to the service for _, s := range merged { - if len(s.Nodes) != svcNodeCounts[s.Name][s.Version] { + // 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 { + log.Logf("Tracking node %s for service %s", n.Id, s.Name) + m.nodes[s.Name+n.Id] = &node{ + ts: time.Now(), + ttl: options.TTL, + } + } + } m.Services[s.Name] = merged go m.sendEvent(®istry.Result{Action: "update", Service: s}) return nil @@ -150,12 +202,44 @@ func (m *Registry) Deregister(s *registry.Service) error { defer m.Unlock() if service, ok := m.Services[s.Name]; ok { - go m.sendEvent(®istry.Result{Action: "delete", Service: s}) + // 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 + for _, s := range service { + if _, ok := svcNodes[s.Name]; !ok { + svcNodes[s.Name] = make(map[string][]string) + } + 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) + } + } + } if service := registry.Remove(service, []*registry.Service{s}); len(service) == 0 { + id := svcNodes[s.Name][s.Version][0] + log.Logf("Stopped tracking node %s for service %s", id, s.Name) + delete(m.nodes, s.Name+id) delete(m.Services, s.Name) } else { - m.Services[s.Name] = service + // find out which nodes have been removed + for _, id := range svcNodes[s.Name][s.Version] { + for _, s := range service { + var found bool + for _, n := range s.Nodes { + if id == n.Id { + found = true + break + } + } + if !found { + log.Logf("Stopped tracking node %s for service %s", id, s.Name) + delete(m.nodes, s.Name+id) + } + } + m.Services[s.Name] = service + } } + go m.sendEvent(®istry.Result{Action: "delete", Service: s}) } return nil diff --git a/registry/service/service.go b/registry/service/service.go index 4af5ab40..9585c242 100644 --- a/registry/service/service.go +++ b/registry/service/service.go @@ -58,8 +58,13 @@ func (s *serviceRegistry) Register(srv *registry.Service, opts ...registry.Regis o(&options) } + ctx := context.Background() + if options.TTL.Nanoseconds() != 0.0 { + ctx = context.WithValue(ctx, "register_ttl", options.TTL) + } + // register the service - _, err := s.client.Register(context.TODO(), ToProto(srv), s.callOpts()...) + _, err := s.client.Register(ctx, ToProto(srv), s.callOpts()...) if err != nil { return err } From 27bd9581bfc4809784dabe401369a73f85c17be8 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Wed, 25 Sep 2019 18:19:18 +0100 Subject: [PATCH 2/9] Refresh TTL; prune expired nodes. --- registry/memory/memory.go | 104 ++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/registry/memory/memory.go b/registry/memory/memory.go index c9e8b353..f7134bf2 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -3,6 +3,7 @@ package memory import ( "context" + "strings" "sync" "time" @@ -12,13 +13,15 @@ import ( ) var ( - timeout = time.Millisecond * 10 + sendEventTime = 10 * time.Millisecond + ttlPruneTime = 1 * time.Minute + DefaultTTL = 1 * time.Minute ) // node tracks node registration timestamp and TTL type node struct { - ts time.Time - ttl time.Duration + lastSeen time.Time + ttl time.Duration } type Registry struct { @@ -44,12 +47,67 @@ func NewRegistry(opts ...registry.Option) registry.Registry { services = make(map[string][]*registry.Service) } - return &Registry{ + reg := &Registry{ options: options, Services: services, nodes: make(map[string]*node), Watchers: make(map[string]*Watcher), } + + 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 { + // if we exceed the TTL threshold we need to stop tracking the node + if time.Since(node.lastSeen) > node.ttl { + // split nodeTrackID into service Name, Version and Node Id + trackIdSplit := strings.Split(nodeTrackId, "+") + svcName, svcVersion, nodeId := trackIdSplit[0], trackIdSplit[1], trackIdSplit[2] + log.Logf("TTL threshold reached for node %s for service %s", nodeId, svcName) + // 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 } func (m *Registry) sendEvent(r *registry.Result) { @@ -70,7 +128,7 @@ func (m *Registry) sendEvent(r *registry.Result) { default: select { case w.res <- r: - case <-time.After(timeout): + case <-time.After(sendEventTime): } } } @@ -125,14 +183,19 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption o(&options) } + // if no TTL has been set, set it to DefaultTTL + if options.TTL.Seconds() == 0.0 { + options.TTL = DefaultTTL + } + if service, ok := m.Services[s.Name]; !ok { m.Services[s.Name] = []*registry.Service{s} // add all nodes into nodes map to track their TTL for _, n := range s.Nodes { log.Logf("Tracking node %s for service %s", n.Id, s.Name) - m.nodes[s.Name+n.Id] = &node{ - ts: time.Now(), - ttl: options.TTL, + m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ + lastSeen: time.Now(), + ttl: options.TTL, } } go m.sendEvent(®istry.Result{Action: "update", Service: s}) @@ -159,9 +222,9 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption // we know s is the new [version of] service; we need to strart tracking its nodes for _, n := range s.Nodes { log.Logf("Tracking node %s for service %s", n.Id, s.Name) - m.nodes[s.Name+n.Id] = &node{ - ts: time.Now(), - ttl: options.TTL, + m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ + lastSeen: time.Now(), + ttl: options.TTL, } } go m.sendEvent(®istry.Result{Action: "update", Service: s}) @@ -181,9 +244,9 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption } if !found { log.Logf("Tracking node %s for service %s", n.Id, s.Name) - m.nodes[s.Name+n.Id] = &node{ - ts: time.Now(), - ttl: options.TTL, + m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ + lastSeen: time.Now(), + ttl: options.TTL, } } } @@ -191,6 +254,15 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption go m.sendEvent(®istry.Result{Action: "update", Service: s}) return nil } + // refresh the timestamp and TTL of the service node + for _, n := range s.Nodes { + trackId := nodeTrackId(s.Name, s.Version, n.Id) + log.Logf("Refreshing TTL for node %s for service %s", n.Id, s.Name) + if trackedNode, ok := m.nodes[trackId]; ok { + trackedNode.lastSeen = time.Now() + trackedNode.ttl = options.TTL + } + } } } @@ -218,7 +290,7 @@ func (m *Registry) Deregister(s *registry.Service) error { if service := registry.Remove(service, []*registry.Service{s}); len(service) == 0 { id := svcNodes[s.Name][s.Version][0] log.Logf("Stopped tracking node %s for service %s", id, s.Name) - delete(m.nodes, s.Name+id) + delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) delete(m.Services, s.Name) } else { // find out which nodes have been removed @@ -233,7 +305,7 @@ func (m *Registry) Deregister(s *registry.Service) error { } if !found { log.Logf("Stopped tracking node %s for service %s", id, s.Name) - delete(m.nodes, s.Name+id) + delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) } } m.Services[s.Name] = service From e973bfaa2571f3d1487f82586301203bf4e655fa Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 27 Sep 2019 11:25:06 +0100 Subject: [PATCH 3/9] Add TTL options to memory registry. --- registry/handler/handler.go | 6 +- registry/proto/registry.pb.go | 149 +++++++++++++++++++++++----------- registry/proto/registry.proto | 6 ++ 3 files changed, 108 insertions(+), 53 deletions(-) diff --git a/registry/handler/handler.go b/registry/handler/handler.go index 5c7fd5c9..e828ecdb 100644 --- a/registry/handler/handler.go +++ b/registry/handler/handler.go @@ -28,10 +28,8 @@ func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.G func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error { var regOpts []registry.RegisterOption - regTTL, ok := ctx.Value("register_ttl").(time.Duration) - if ok { - regOpts = append(regOpts, registry.RegisterTTL(regTTL)) - } + ttl := time.Duration(req.Options.Ttl) + regOpts = append(regOpts, registry.RegisterTTL(ttl)) err := r.Registry.Register(service.ToService(req), regOpts...) if err != nil { diff --git a/registry/proto/registry.pb.go b/registry/proto/registry.pb.go index 9c62a1fb..c6862d59 100644 --- a/registry/proto/registry.pb.go +++ b/registry/proto/registry.pb.go @@ -56,6 +56,7 @@ type Service struct { Metadata map[string]string `protobuf:"bytes,3,rep,name=metadata,proto3" json:"metadata,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` Endpoints []*Endpoint `protobuf:"bytes,4,rep,name=endpoints,proto3" json:"endpoints,omitempty"` Nodes []*Node `protobuf:"bytes,5,rep,name=nodes,proto3" json:"nodes,omitempty"` + Options *Options `protobuf:"bytes,6,opt,name=options,proto3" json:"options,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -121,6 +122,13 @@ func (m *Service) GetNodes() []*Node { return nil } +func (m *Service) GetOptions() *Options { + if m != nil { + return m.Options + } + return nil +} + // Node represents the node the service is on type Node struct { Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` @@ -305,6 +313,46 @@ func (m *Value) GetValues() []*Value { return nil } +// Options are registry options +type Options struct { + Ttl int64 `protobuf:"varint,1,opt,name=ttl,proto3" json:"ttl,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Options) Reset() { *m = Options{} } +func (m *Options) String() string { return proto.CompactTextString(m) } +func (*Options) ProtoMessage() {} +func (*Options) Descriptor() ([]byte, []int) { + return fileDescriptor_41af05d40a615591, []int{4} +} + +func (m *Options) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Options.Unmarshal(m, b) +} +func (m *Options) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Options.Marshal(b, m, deterministic) +} +func (m *Options) XXX_Merge(src proto.Message) { + xxx_messageInfo_Options.Merge(m, src) +} +func (m *Options) XXX_Size() int { + return xxx_messageInfo_Options.Size(m) +} +func (m *Options) XXX_DiscardUnknown() { + xxx_messageInfo_Options.DiscardUnknown(m) +} + +var xxx_messageInfo_Options proto.InternalMessageInfo + +func (m *Options) GetTtl() int64 { + if m != nil { + return m.Ttl + } + return 0 +} + // Result is returns by the watcher type Result struct { Action string `protobuf:"bytes,1,opt,name=action,proto3" json:"action,omitempty"` @@ -319,7 +367,7 @@ func (m *Result) Reset() { *m = Result{} } func (m *Result) String() string { return proto.CompactTextString(m) } func (*Result) ProtoMessage() {} func (*Result) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{4} + return fileDescriptor_41af05d40a615591, []int{5} } func (m *Result) XXX_Unmarshal(b []byte) error { @@ -371,7 +419,7 @@ func (m *EmptyResponse) Reset() { *m = EmptyResponse{} } func (m *EmptyResponse) String() string { return proto.CompactTextString(m) } func (*EmptyResponse) ProtoMessage() {} func (*EmptyResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{5} + return fileDescriptor_41af05d40a615591, []int{6} } func (m *EmptyResponse) XXX_Unmarshal(b []byte) error { @@ -403,7 +451,7 @@ func (m *GetRequest) Reset() { *m = GetRequest{} } func (m *GetRequest) String() string { return proto.CompactTextString(m) } func (*GetRequest) ProtoMessage() {} func (*GetRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{6} + return fileDescriptor_41af05d40a615591, []int{7} } func (m *GetRequest) XXX_Unmarshal(b []byte) error { @@ -442,7 +490,7 @@ func (m *GetResponse) Reset() { *m = GetResponse{} } func (m *GetResponse) String() string { return proto.CompactTextString(m) } func (*GetResponse) ProtoMessage() {} func (*GetResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{7} + return fileDescriptor_41af05d40a615591, []int{8} } func (m *GetResponse) XXX_Unmarshal(b []byte) error { @@ -480,7 +528,7 @@ func (m *ListRequest) Reset() { *m = ListRequest{} } func (m *ListRequest) String() string { return proto.CompactTextString(m) } func (*ListRequest) ProtoMessage() {} func (*ListRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{8} + return fileDescriptor_41af05d40a615591, []int{9} } func (m *ListRequest) XXX_Unmarshal(b []byte) error { @@ -512,7 +560,7 @@ func (m *ListResponse) Reset() { *m = ListResponse{} } func (m *ListResponse) String() string { return proto.CompactTextString(m) } func (*ListResponse) ProtoMessage() {} func (*ListResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{9} + return fileDescriptor_41af05d40a615591, []int{10} } func (m *ListResponse) XXX_Unmarshal(b []byte) error { @@ -552,7 +600,7 @@ func (m *WatchRequest) Reset() { *m = WatchRequest{} } func (m *WatchRequest) String() string { return proto.CompactTextString(m) } func (*WatchRequest) ProtoMessage() {} func (*WatchRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{10} + return fileDescriptor_41af05d40a615591, []int{11} } func (m *WatchRequest) XXX_Unmarshal(b []byte) error { @@ -599,7 +647,7 @@ func (m *Event) Reset() { *m = Event{} } func (m *Event) String() string { return proto.CompactTextString(m) } func (*Event) ProtoMessage() {} func (*Event) Descriptor() ([]byte, []int) { - return fileDescriptor_41af05d40a615591, []int{11} + return fileDescriptor_41af05d40a615591, []int{12} } func (m *Event) XXX_Unmarshal(b []byte) error { @@ -657,6 +705,7 @@ func init() { proto.RegisterType((*Endpoint)(nil), "go.micro.registry.Endpoint") proto.RegisterMapType((map[string]string)(nil), "go.micro.registry.Endpoint.MetadataEntry") proto.RegisterType((*Value)(nil), "go.micro.registry.Value") + proto.RegisterType((*Options)(nil), "go.micro.registry.Options") proto.RegisterType((*Result)(nil), "go.micro.registry.Result") proto.RegisterType((*EmptyResponse)(nil), "go.micro.registry.EmptyResponse") proto.RegisterType((*GetRequest)(nil), "go.micro.registry.GetRequest") @@ -670,45 +719,47 @@ func init() { func init() { proto.RegisterFile("registry.proto", fileDescriptor_41af05d40a615591) } var fileDescriptor_41af05d40a615591 = []byte{ - // 632 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0xdb, 0x6e, 0xd3, 0x4c, - 0x10, 0x8e, 0xed, 0x38, 0x87, 0x49, 0xdb, 0xbf, 0xff, 0x08, 0x81, 0x31, 0x05, 0x22, 0x4b, 0xa0, - 0x80, 0x84, 0xa9, 0x42, 0x85, 0x38, 0x5c, 0x21, 0x62, 0x2a, 0xa1, 0x16, 0x09, 0x73, 0xba, 0x36, - 0xf1, 0xa8, 0x58, 0xc4, 0x07, 0x76, 0x37, 0x91, 0xfc, 0x0e, 0x48, 0x3c, 0x01, 0x77, 0x3c, 0x0a, - 0x0f, 0x86, 0xbc, 0x5e, 0x27, 0xa9, 0x62, 0x07, 0xa4, 0xc2, 0xdd, 0x8c, 0xf7, 0x9b, 0x6f, 0x67, - 0xbe, 0x6f, 0x36, 0x81, 0x3d, 0x46, 0x67, 0x11, 0x17, 0x2c, 0x77, 0x33, 0x96, 0x8a, 0x14, 0xff, - 0x3f, 0x4b, 0xdd, 0x38, 0x9a, 0xb2, 0xd4, 0xad, 0x0e, 0x9c, 0x1f, 0x3a, 0x74, 0xdf, 0x10, 0x5b, - 0x44, 0x53, 0x42, 0x84, 0x76, 0x12, 0xc4, 0x64, 0x69, 0x43, 0x6d, 0xd4, 0xf7, 0x65, 0x8c, 0x16, - 0x74, 0x17, 0xc4, 0x78, 0x94, 0x26, 0x96, 0x2e, 0x3f, 0x57, 0x29, 0x4e, 0xa0, 0x17, 0x93, 0x08, - 0xc2, 0x40, 0x04, 0x96, 0x31, 0x34, 0x46, 0x83, 0xf1, 0xc8, 0xdd, 0xe0, 0x77, 0x15, 0xb7, 0x7b, - 0xaa, 0xa0, 0x5e, 0x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- 0xf1, 0x5d, 0x1f, 0x7f, 0x33, 0xa0, 0xe7, 0x2b, 0x3a, 0x3c, 0x95, 0x6e, 0x56, 0x3f, 0xdb, 0xd7, - 0x6b, 0x2e, 0x5c, 0x99, 0x6d, 0xdf, 0x68, 0x3a, 0x56, 0xab, 0xd1, 0xc2, 0x97, 0x15, 0x35, 0x31, - 0xdc, 0xd2, 0xbd, 0x3d, 0xac, 0x13, 0xeb, 0xdc, 0x9a, 0xb5, 0xf0, 0x04, 0x60, 0x42, 0xec, 0x6f, - 0xb1, 0xbd, 0x2e, 0x17, 0x47, 0x95, 0x70, 0xac, 0x9b, 0x65, 0x6d, 0xd1, 0xec, 0x9b, 0x8d, 0xe7, - 0x4b, 0xca, 0x63, 0x30, 0xe5, 0x0e, 0x61, 0x1d, 0x76, 0x7d, 0xbb, 0xec, 0xab, 0x35, 0x80, 0xf2, - 0x2d, 0x3b, 0xad, 0x43, 0xed, 0x63, 0x47, 0xfe, 0xa7, 0x3e, 0xf8, 0x15, 0x00, 0x00, 0xff, 0xff, - 0x08, 0x0e, 0xe4, 0xae, 0x65, 0x07, 0x00, 0x00, + // 667 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x55, 0xdd, 0x6e, 0xd3, 0x4c, + 0x10, 0x8d, 0xed, 0xfc, 0x4e, 0xda, 0x7e, 0xfd, 0x46, 0x08, 0x8c, 0x5b, 0x20, 0xb2, 0x04, 0x0a, + 0x48, 0x84, 0x2a, 0x54, 0x88, 0x9f, 0x2b, 0x44, 0x43, 0x25, 0xd4, 0x82, 0x58, 0xfe, 0xae, 0x4d, + 0x3c, 0x2a, 0x16, 0x89, 0x6d, 0x76, 0xb7, 0x91, 0xf2, 0x0e, 0x48, 0x3c, 0x01, 0x6f, 0xc3, 0x53, + 0xf0, 0x34, 0x68, 0xd7, 0xeb, 0x24, 0x55, 0xd7, 0x01, 0xa9, 0x70, 0x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, 0xe8, 0x84, 0x3f, 0x1c, 0xa8, 0xab, 0x11, 0x70, 0x0b, 0xdc, 0x24, 0x36, 0x35, 0x6e, 0x12, + 0x2b, 0x56, 0xa3, 0x38, 0xe6, 0x24, 0x44, 0xc9, 0xaa, 0x49, 0x95, 0x06, 0x79, 0xc6, 0xa5, 0xef, + 0xf5, 0x9c, 0xbe, 0xc7, 0x74, 0x8c, 0x4f, 0x57, 0x98, 0x2e, 0x28, 0xba, 0x59, 0xb1, 0x6b, 0x15, + 0xcd, 0x17, 0x5b, 0xe3, 0xab, 0x0b, 0xed, 0x52, 0x00, 0xab, 0x49, 0x86, 0xd0, 0xe2, 0xf4, 0xe5, + 0x94, 0x84, 0xd4, 0xc5, 0xdd, 0xa1, 0x6f, 0x99, 0xef, 0xbd, 0xea, 0xc7, 0x4a, 0x20, 0xee, 0x43, + 0x9b, 0x93, 0xc8, 0xb3, 0x54, 0x90, 0x5e, 0x76, 0x5d, 0xd1, 0x02, 0x89, 0xa3, 0x73, 0x54, 0xdc, + 0x5e, 0xe3, 0x96, 0x7f, 0x43, 0x47, 0x04, 0x0d, 0x3d, 0x96, 0x95, 0x0a, 0x84, 0xba, 0x9c, 0xe7, + 0x65, 0x95, 0x8e, 0x71, 0x0f, 0x9a, 0xba, 0x5a, 0x98, 0x77, 0x52, 0xbd, 0xa8, 0xc1, 0x85, 0x3b, + 0xd0, 0x32, 0x4e, 0x54, 0x93, 0x49, 0x39, 0xd1, 0x77, 0x78, 0x4c, 0x85, 0xa1, 0x84, 0x26, 0x23, + 0x71, 0x3a, 0x91, 0x78, 0x19, 0x9a, 0xd1, 0x58, 0xc1, 0xcc, 0x08, 0x26, 0x53, 0x56, 0x17, 0xc5, + 0xbb, 0x33, 0x7a, 0x04, 0xd5, 0x2f, 0x93, 0x95, 0x50, 0xdc, 0x85, 0x8e, 0x4c, 0xa6, 0x24, 0x64, + 0x34, 0xcd, 0x8d, 0xff, 0x96, 0x1f, 0xc2, 0xff, 0x60, 0x73, 0x34, 0xcd, 0xe5, 0x9c, 0x19, 0x29, + 0xc2, 0x5b, 0x00, 0x87, 0x24, 0x99, 0x91, 0xd3, 0x5f, 0x5e, 0x59, 0xcc, 0x52, 0xa6, 0xe1, 0x08, + 0xba, 0x1a, 0x67, 0x14, 0x7c, 0x00, 0x6d, 0x73, 0x22, 0x7c, 0x47, 0xd3, 0xb1, 0x6e, 0xb8, 0x05, + 0x36, 0xdc, 0x84, 0xee, 0x51, 0x22, 0xca, 0xfb, 0xc2, 0xe7, 0xb0, 0x51, 0xa4, 0x17, 0x6c, 0xdb, + 0x87, 0x8d, 0x0f, 0x91, 0x1c, 0x7f, 0xfa, 0xfd, 0x1e, 0xdf, 0x1d, 0x68, 0x8c, 0x66, 0x94, 0xca, + 0x73, 0xaf, 0x79, 0x6f, 0x45, 0xf3, 0xad, 0xe1, 0xae, 0xcd, 0x90, 0xaa, 0xee, 0xed, 0x3c, 0x27, + 0xe3, 0x88, 0xb5, 0x54, 0xaf, 0xca, 0x57, 0xff, 0x63, 0xf9, 0xee, 0xdc, 0x83, 0xce, 0xe2, 0x1a, + 0x04, 0x68, 0x3e, 0xe3, 0x14, 0x49, 0xda, 0xae, 0xa9, 0xf8, 0x80, 0x26, 0x24, 0x69, 0xdb, 0x51, + 0xf1, 0xbb, 0x3c, 0x56, 0xdf, 0xdd, 0xe1, 0x37, 0x0f, 0xda, 0xcc, 0xb4, 0xc3, 0x63, 0xad, 0x66, + 0xf9, 0x27, 0xb8, 0x66, 0xb9, 0x70, 0x29, 0x76, 0x70, 0xbd, 0xea, 0xd8, 0x58, 0xa3, 0x86, 0x2f, + 0xca, 0xd6, 0xc4, 0x71, 0xcd, 0xf4, 0x41, 0xcf, 0x46, 0xd6, 0x19, 0x9b, 0xd5, 0xf0, 0x08, 0xe0, + 0x80, 0xf8, 0xdf, 0xea, 0xf6, 0xba, 0x30, 0x8e, 0x29, 0x11, 0x68, 0xdb, 0x65, 0xc5, 0x68, 0xc1, + 0x8d, 0xca, 0xf3, 0x45, 0xcb, 0x43, 0x68, 0x68, 0x0f, 0xa1, 0x0d, 0xbb, 0xea, 0xae, 0xe0, 0xaa, + 0x05, 0x50, 0xbc, 0xe5, 0xb0, 0xb6, 0xe7, 0x7c, 0x6c, 0xea, 0xdf, 0xf4, 0xfd, 0x5f, 0x01, 0x00, + 0x00, 0xff, 0xff, 0xfb, 0x3e, 0x7d, 0xa4, 0xb8, 0x07, 0x00, 0x00, } diff --git a/registry/proto/registry.proto b/registry/proto/registry.proto index 6883102a..c161828b 100644 --- a/registry/proto/registry.proto +++ b/registry/proto/registry.proto @@ -17,6 +17,7 @@ message Service { map metadata = 3; repeated Endpoint endpoints = 4; repeated Node nodes = 5; + Options options = 6; } // Node represents the node the service is on @@ -42,6 +43,11 @@ message Value { repeated Value values = 3; } +// Options are registry options +message Options { + int64 ttl = 1; +} + // Result is returns by the watcher message Result { string action = 1; // create, update, delete From d85ca7abd23d9d22cbd14b35ce4affd25db47286 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 27 Sep 2019 14:29:00 +0100 Subject: [PATCH 4/9] Set registry TTL properly via protobuf Options --- registry/service/service.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/registry/service/service.go b/registry/service/service.go index 9585c242..5bc3cffb 100644 --- a/registry/service/service.go +++ b/registry/service/service.go @@ -58,13 +58,12 @@ func (s *serviceRegistry) Register(srv *registry.Service, opts ...registry.Regis o(&options) } - ctx := context.Background() - if options.TTL.Nanoseconds() != 0.0 { - ctx = context.WithValue(ctx, "register_ttl", options.TTL) - } + // encode srv into protobuf and pack Register TTL into it + pbSrv := ToProto(srv) + pbSrv.Options.Ttl = int64(options.TTL.Seconds()) // register the service - _, err := s.client.Register(ctx, ToProto(srv), s.callOpts()...) + _, err := s.client.Register(context.TODO(), pbSrv, s.callOpts()...) if err != nil { return err } From 8417361bceb8810cd400263d1758bc47d56d414f Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 27 Sep 2019 15:00:25 +0100 Subject: [PATCH 5/9] Set registry TTL to seconds, not the nanoseconds --- registry/handler/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/handler/handler.go b/registry/handler/handler.go index e828ecdb..68d4523b 100644 --- a/registry/handler/handler.go +++ b/registry/handler/handler.go @@ -28,7 +28,7 @@ func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.G func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error { var regOpts []registry.RegisterOption - ttl := time.Duration(req.Options.Ttl) + ttl := time.Duration(req.Options.Ttl) * time.Second regOpts = append(regOpts, registry.RegisterTTL(ttl)) err := r.Registry.Register(service.ToService(req), regOpts...) From 204c7d1fcfc64ec1b56b76d1c7fca427e8d9ca71 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Sun, 29 Sep 2019 15:03:21 +0100 Subject: [PATCH 6/9] Fixed options bug and proto indenting --- registry/handler/handler.go | 6 ++++-- registry/proto/registry.proto | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/registry/handler/handler.go b/registry/handler/handler.go index 68d4523b..a592dffa 100644 --- a/registry/handler/handler.go +++ b/registry/handler/handler.go @@ -28,8 +28,10 @@ func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.G func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error { var regOpts []registry.RegisterOption - ttl := time.Duration(req.Options.Ttl) * time.Second - regOpts = append(regOpts, registry.RegisterTTL(ttl)) + if req.Options != nil { + ttl := time.Duration(req.Options.Ttl) * time.Second + regOpts = append(regOpts, registry.RegisterTTL(ttl)) + } err := r.Registry.Register(service.ToService(req), regOpts...) if err != nil { diff --git a/registry/proto/registry.proto b/registry/proto/registry.proto index c161828b..ddc09669 100644 --- a/registry/proto/registry.proto +++ b/registry/proto/registry.proto @@ -12,11 +12,11 @@ service Registry { // Service represents a go-micro service message Service { - string name = 1; - string version = 2; - map metadata = 3; - repeated Endpoint endpoints = 4; - repeated Node nodes = 5; + string name = 1; + string version = 2; + map metadata = 3; + repeated Endpoint endpoints = 4; + repeated Node nodes = 5; Options options = 6; } From 859ecb18725b3f3955d9d98ba4a89edce3d5b157 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Sun, 29 Sep 2019 21:42:30 +0100 Subject: [PATCH 7/9] Dont set default TTL. Stop tracking nodes with no TTL --- registry/memory/memory.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/registry/memory/memory.go b/registry/memory/memory.go index f7134bf2..72d62298 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -73,8 +73,8 @@ func (m *Registry) ttlPrune() { case <-prune.C: m.Lock() for nodeTrackId, node := range m.nodes { - // if we exceed the TTL threshold we need to stop tracking the node - if time.Since(node.lastSeen) > node.ttl { + // 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 { // split nodeTrackID into service Name, Version and Node Id trackIdSplit := strings.Split(nodeTrackId, "+") svcName, svcVersion, nodeId := trackIdSplit[0], trackIdSplit[1], trackIdSplit[2] @@ -183,11 +183,6 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption o(&options) } - // if no TTL has been set, set it to DefaultTTL - if options.TTL.Seconds() == 0.0 { - options.TTL = DefaultTTL - } - if service, ok := m.Services[s.Name]; !ok { m.Services[s.Name] = []*registry.Service{s} // add all nodes into nodes map to track their TTL From 3f3f1272b358b1369147f227a55a6d4181eae13b Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Mon, 30 Sep 2019 11:11:32 +0100 Subject: [PATCH 8/9] Turn regular logs to Debug logs; annotate with Registry "tag" --- registry/memory/memory.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/registry/memory/memory.go b/registry/memory/memory.go index 72d62298..1cad2274 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -78,7 +78,7 @@ func (m *Registry) ttlPrune() { // split nodeTrackID into service Name, Version and Node Id trackIdSplit := strings.Split(nodeTrackId, "+") svcName, svcVersion, nodeId := trackIdSplit[0], trackIdSplit[1], trackIdSplit[2] - log.Logf("TTL threshold reached for node %s for service %s", nodeId, svcName) + log.Debugf("Registry TTL expired for service %s, node %s", svcName, nodeId) // 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] { @@ -187,7 +187,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption m.Services[s.Name] = []*registry.Service{s} // add all nodes into nodes map to track their TTL for _, n := range s.Nodes { - log.Logf("Tracking node %s for service %s", n.Id, s.Name) + log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ lastSeen: time.Now(), ttl: options.TTL, @@ -216,7 +216,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption m.Services[s.Name] = merged // we know s is the new [version of] service; we need to strart tracking its nodes for _, n := range s.Nodes { - log.Logf("Tracking node %s for service %s", n.Id, s.Name) + log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ lastSeen: time.Now(), ttl: options.TTL, @@ -238,7 +238,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption } } if !found { - log.Logf("Tracking node %s for service %s", n.Id, s.Name) + log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ lastSeen: time.Now(), ttl: options.TTL, @@ -252,7 +252,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption // refresh the timestamp and TTL of the service node for _, n := range s.Nodes { trackId := nodeTrackId(s.Name, s.Version, n.Id) - log.Logf("Refreshing TTL for node %s for service %s", n.Id, s.Name) + log.Debugf("Registry refreshing TTL for node %s for service %s", n.Id, s.Name) if trackedNode, ok := m.nodes[trackId]; ok { trackedNode.lastSeen = time.Now() trackedNode.ttl = options.TTL @@ -284,7 +284,7 @@ func (m *Registry) Deregister(s *registry.Service) error { } if service := registry.Remove(service, []*registry.Service{s}); len(service) == 0 { id := svcNodes[s.Name][s.Version][0] - log.Logf("Stopped tracking node %s for service %s", id, s.Name) + log.Debugf("Registry stopped tracking node %s for service %s", id, s.Name) delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) delete(m.Services, s.Name) } else { @@ -299,7 +299,7 @@ func (m *Registry) Deregister(s *registry.Service) error { } } if !found { - log.Logf("Stopped tracking node %s for service %s", id, s.Name) + log.Debugf("Registry stopped tracking node %s for service %s", id, s.Name) delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) } } From 4b73ac9dc5b6b6c418159e528ae77171f5b59892 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Mon, 30 Sep 2019 12:48:06 +0100 Subject: [PATCH 9/9] Simplified code. Small bug fix the used to lead to multi-registry loop. --- registry/memory/memory.go | 65 +++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/registry/memory/memory.go b/registry/memory/memory.go index 1cad2274..810d8d4c 100644 --- a/registry/memory/memory.go +++ b/registry/memory/memory.go @@ -178,6 +178,8 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption m.Lock() defer m.Unlock() + log.Debugf("Registry deregistering service: %s", s.Name) + var options registry.RegisterOptions for _, o := range opts { o(&options) @@ -187,16 +189,17 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption m.Services[s.Name] = []*registry.Service{s} // add all nodes into nodes map to track their TTL for _, n := range s.Nodes { - log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) + log.Debugf("Registry tracking new service: %s, node %s", s.Name, n.Id) m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ lastSeen: time.Now(), ttl: options.TTL, } } go m.sendEvent(®istry.Result{Action: "update", Service: s}) + return nil } else { - // svcCount essentially keep the count of all service vesions - svcCount := len(service) + // svcCount keeps the count of all versions of particular service + //svcCount := len(service) // 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 @@ -212,20 +215,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption } // if merged count and original service counts changed we know we are adding a new version of the service merged := registry.Merge(service, []*registry.Service{s}) - if len(merged) != svcCount { - m.Services[s.Name] = merged - // we know s is the new [version of] service; we need to strart tracking its nodes - for _, n := range s.Nodes { - log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) - m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ - lastSeen: time.Now(), - ttl: options.TTL, - } - } - go m.sendEvent(®istry.Result{Action: "update", Service: s}) - return nil - } - // if the node count of any particular service [version] changed we know we are adding a new node to the service + // if the node count of any service [version] changed we know we are adding a new node to the service for _, s := range merged { // 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]) { @@ -238,7 +228,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption } } if !found { - log.Debugf("Registry tracking node %s for service %s", n.Id, s.Name) + log.Debugf("Registry tracking new node: %s for service %s", n.Id, s.Name) m.nodes[nodeTrackId(s.Name, s.Version, n.Id)] = &node{ lastSeen: time.Now(), ttl: options.TTL, @@ -268,31 +258,38 @@ func (m *Registry) Deregister(s *registry.Service) error { m.Lock() defer m.Unlock() + log.Debugf("Registry deregistering service: %s", s.Name) + if service, ok := m.Services[s.Name]; ok { - // svcNodes maintains a list of node Ids per particular service version + // svcNodes collects the list of all node Ids for each service version svcNodes := make(map[string]map[string][]string) - // collect all service ids for all service versions - for _, s := range service { - if _, ok := svcNodes[s.Name]; !ok { - svcNodes[s.Name] = make(map[string][]string) + // 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) } - 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) + 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) } } } - if service := registry.Remove(service, []*registry.Service{s}); len(service) == 0 { - id := svcNodes[s.Name][s.Version][0] - log.Debugf("Registry stopped tracking node %s for service %s", id, s.Name) - delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) + // 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] { + log.Debugf("Registry stopped tracking node %s for service %s", id, s.Name) + delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) + go m.sendEvent(®istry.Result{Action: "delete", Service: s}) + } + log.Debugf("Registry deleting service %s: no service nodes", s.Name) delete(m.Services, s.Name) + return nil } else { // find out which nodes have been removed for _, id := range svcNodes[s.Name][s.Version] { - for _, s := range service { + for _, svc := range updatedService { var found bool - for _, n := range s.Nodes { + for _, n := range svc.Nodes { if id == n.Id { found = true break @@ -301,12 +298,12 @@ func (m *Registry) Deregister(s *registry.Service) error { if !found { log.Debugf("Registry stopped tracking node %s for service %s", id, s.Name) delete(m.nodes, nodeTrackId(s.Name, s.Version, id)) + go m.sendEvent(®istry.Result{Action: "delete", Service: s}) } } - m.Services[s.Name] = service + m.Services[s.Name] = updatedService } } - go m.sendEvent(®istry.Result{Action: "delete", Service: s}) } return nil