From a46ea436cdbc53e7fad644753acfc679782b1b53 Mon Sep 17 00:00:00 2001 From: zengfanwei Date: Wed, 14 Apr 2021 18:57:36 +0800 Subject: [PATCH 1/2] fix registery dir cache key issue --- common/url.go | 10 ++++++++++ registry/directory/directory.go | 6 +++--- registry/event.go | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/common/url.go b/common/url.go index 03882f1478..07027b7e0e 100644 --- a/common/url.go +++ b/common/url.go @@ -344,6 +344,16 @@ func (c *URL) Key() string { return buildString } +//CacheInvokerMapKey get dir cacheInvokerMap key +func (c *URL) CacheInvokerMapKey() string { + urlNew, _ := NewURL(c.PrimitiveURL) + + buildString := fmt.Sprintf("%s://%s:%s@%s:%s/?interface=%s&group=%s&version=%s×tamp=%s", + c.Protocol, c.Username, c.Password, c.Ip, c.Port, c.Service(), c.GetParam(constant.GROUP_KEY, ""), + c.GetParam(constant.VERSION_KEY, ""), urlNew.GetParam(constant.TIMESTAMP_KEY, "")) + return buildString +} + // ServiceKey gets a unique key of a service. func (c *URL) ServiceKey() string { return ServiceKey(c.GetParam(constant.INTERFACE_KEY, strings.TrimPrefix(c.Path, "/")), diff --git a/registry/directory/directory.go b/registry/directory/directory.go index da05c02d21..a062fc76ce 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -224,7 +224,7 @@ func (dir *RegistryDirectory) invokerCacheKey(event *registry.ServiceEvent) stri referenceUrl := dir.GetDirectoryUrl().SubURL newUrl := common.MergeURL(event.Service, referenceUrl) event.Update(newUrl) - return newUrl.Key() + return newUrl.CacheInvokerMapKey() } // setNewInvokers groups the invokers from the cache first, then set the result to both directory and router chain. @@ -317,7 +317,7 @@ func (dir *RegistryDirectory) toGroupInvokers() []protocol.Invoker { // uncacheInvoker will return abandoned Invoker, if no Invoker to be abandoned, return nil func (dir *RegistryDirectory) uncacheInvoker(url *common.URL) protocol.Invoker { - return dir.uncacheInvokerWithKey(url.Key()) + return dir.uncacheInvokerWithKey(url.CacheInvokerMapKey()) } func (dir *RegistryDirectory) uncacheInvokerWithKey(key string) protocol.Invoker { @@ -356,7 +356,7 @@ func (dir *RegistryDirectory) cacheInvoker(url *common.URL) protocol.Invoker { } func (dir *RegistryDirectory) doCacheInvoker(newUrl *common.URL) (protocol.Invoker, bool) { - key := newUrl.Key() + key := newUrl.CacheInvokerMapKey() if cacheInvoker, ok := dir.cacheInvokersMap.Load(key); !ok { logger.Debugf("service will be added in cache invokers: invokers url is %s!", newUrl) newInvoker := extension.GetProtocol(protocolwrapper.FILTER).Refer(newUrl) diff --git a/registry/event.go b/registry/event.go index 2ebc375597..39bfa62c11 100644 --- a/registry/event.go +++ b/registry/event.go @@ -69,7 +69,7 @@ func (e *ServiceEvent) Key() string { if len(e.key) > 0 { return e.key } - e.key = e.Service.Key() + e.key = e.Service.CacheInvokerMapKey() return e.key } From faf3bfbb101a03fb6c9646816f20d674b8cecda7 Mon Sep 17 00:00:00 2001 From: zengfanwei Date: Thu, 15 Apr 2021 11:46:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/url.go | 4 ++-- registry/directory/directory.go | 6 +++--- registry/event.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/common/url.go b/common/url.go index 07027b7e0e..6863230c93 100644 --- a/common/url.go +++ b/common/url.go @@ -344,8 +344,8 @@ func (c *URL) Key() string { return buildString } -//CacheInvokerMapKey get dir cacheInvokerMap key -func (c *URL) CacheInvokerMapKey() string { +//GetCacheInvokerMapKey get directory cacheInvokerMap key +func (c *URL) GetCacheInvokerMapKey() string { urlNew, _ := NewURL(c.PrimitiveURL) buildString := fmt.Sprintf("%s://%s:%s@%s:%s/?interface=%s&group=%s&version=%s×tamp=%s", diff --git a/registry/directory/directory.go b/registry/directory/directory.go index a062fc76ce..7ed4cd0270 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -224,7 +224,7 @@ func (dir *RegistryDirectory) invokerCacheKey(event *registry.ServiceEvent) stri referenceUrl := dir.GetDirectoryUrl().SubURL newUrl := common.MergeURL(event.Service, referenceUrl) event.Update(newUrl) - return newUrl.CacheInvokerMapKey() + return newUrl.GetCacheInvokerMapKey() } // setNewInvokers groups the invokers from the cache first, then set the result to both directory and router chain. @@ -317,7 +317,7 @@ func (dir *RegistryDirectory) toGroupInvokers() []protocol.Invoker { // uncacheInvoker will return abandoned Invoker, if no Invoker to be abandoned, return nil func (dir *RegistryDirectory) uncacheInvoker(url *common.URL) protocol.Invoker { - return dir.uncacheInvokerWithKey(url.CacheInvokerMapKey()) + return dir.uncacheInvokerWithKey(url.GetCacheInvokerMapKey()) } func (dir *RegistryDirectory) uncacheInvokerWithKey(key string) protocol.Invoker { @@ -356,7 +356,7 @@ func (dir *RegistryDirectory) cacheInvoker(url *common.URL) protocol.Invoker { } func (dir *RegistryDirectory) doCacheInvoker(newUrl *common.URL) (protocol.Invoker, bool) { - key := newUrl.CacheInvokerMapKey() + key := newUrl.GetCacheInvokerMapKey() if cacheInvoker, ok := dir.cacheInvokersMap.Load(key); !ok { logger.Debugf("service will be added in cache invokers: invokers url is %s!", newUrl) newInvoker := extension.GetProtocol(protocolwrapper.FILTER).Refer(newUrl) diff --git a/registry/event.go b/registry/event.go index 39bfa62c11..396a947c6e 100644 --- a/registry/event.go +++ b/registry/event.go @@ -69,7 +69,7 @@ func (e *ServiceEvent) Key() string { if len(e.key) > 0 { return e.key } - e.key = e.Service.CacheInvokerMapKey() + e.key = e.Service.GetCacheInvokerMapKey() return e.key }