This is an automated email from the ASF dual-hosted git repository.
littlecui pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/dev by this push:
new a6ef87c4 [opt] improve the tps of registerInstance interface (#1458)
a6ef87c4 is described below
commit a6ef87c4be745653c3f68804b42e19250faa253b
Author: tornado-ssy <[email protected]>
AuthorDate: Sat Mar 16 20:32:18 2024 +0800
[opt] improve the tps of registerInstance interface (#1458)
Co-authored-by: songshiyuan 00649746 <[email protected]>
---
datasource/etcd/state/kvstore/cache_kv.go | 46 +++++++++++++++++++++-----
datasource/etcd/state/kvstore/cache_kv_test.go | 14 ++++++++
2 files changed, 52 insertions(+), 8 deletions(-)
diff --git a/datasource/etcd/state/kvstore/cache_kv.go
b/datasource/etcd/state/kvstore/cache_kv.go
index dfbdaa9e..609d3a6f 100644
--- a/datasource/etcd/state/kvstore/cache_kv.go
+++ b/datasource/etcd/state/kvstore/cache_kv.go
@@ -18,21 +18,29 @@
package kvstore
import (
+ regexp2 "regexp"
"strings"
"sync"
"github.com/apache/servicecomb-service-center/pkg/util"
)
+const InitCount = 1
+const InitLayer = 2
+const SPLIT = "/"
+const DomainProjectLayer = 6
+
// KvCache implements Cache.
// KvCache is dedicated to stores service discovery data,
// e.g. service, instance, lease.
type KvCache struct {
- Cfg *Options
- name string
- store map[string]map[string]*KeyValue
- rwMux sync.RWMutex
- dirty bool
+ Cfg *Options
+ name string
+ store map[string]map[string]*KeyValue
+ rwMux sync.RWMutex
+ dirty bool
+ count map[string]int // the number of leaf node
+ keyLayers int // the number of layers of leaf nodes
}
func (c *KvCache) Name() string {
@@ -63,6 +71,12 @@ func (c *KvCache) GetAll(arr *[]*KeyValue) (count int) {
return
}
+func (c *KvCache) getCacheDomainProjectKey(key string) string {
+ regexp, _ := regexp2.Compile(`/(\w)+-(\w)+/(\w)+/(\w)+/(\w)+/(\w)+/`)
+ domainProjectKey := regexp.FindString(key)
+ return domainProjectKey
+}
+
func (c *KvCache) GetPrefix(prefix string, arr *[]*KeyValue) (count int) {
c.rwMux.RLock()
count = c.getPrefixKey(arr, prefix)
@@ -124,6 +138,11 @@ func (c *KvCache) getPrefixKey(arr *[]*KeyValue, prefix
string) (count int) {
return 0
}
+ if arr == nil && strings.Count(prefix, SPLIT) == DomainProjectLayer {
+ count = c.count[prefix]
+ return
+ }
+
// TODO support sort option
if arr == nil {
for key := range keysRef {
@@ -156,6 +175,10 @@ func (c *KvCache) addPrefixKey(key string, val *KeyValue) {
return
}
keys, ok := c.store[prefix]
+ if strings.Count(key, SPLIT) > c.keyLayers {
+ c.count[c.getCacheDomainProjectKey(key)] = InitCount
+ c.keyLayers = strings.Count(key, SPLIT)
+ }
if !ok {
// build parent index key and new child nodes
keys = make(map[string]*KeyValue)
@@ -166,6 +189,8 @@ func (c *KvCache) addPrefixKey(key string, val *KeyValue) {
keys[key] = val
}
return
+ } else if _, ok := keys[key]; !ok {
+ c.count[c.getCacheDomainProjectKey(key)]++
}
keys[key], key = val, prefix
@@ -178,6 +203,9 @@ func (c *KvCache) deletePrefixKey(key string) {
if !ok {
return
}
+ if strings.Count(key, SPLIT) == c.keyLayers {
+ c.count[c.getCacheDomainProjectKey(key)]--
+ }
delete(m, key)
// remove parent which has no child
@@ -189,8 +217,10 @@ func (c *KvCache) deletePrefixKey(key string) {
func NewKvCache(name string, cfg *Options) *KvCache {
return &KvCache{
- Cfg: cfg,
- name: name,
- store: make(map[string]map[string]*KeyValue),
+ Cfg: cfg,
+ name: name,
+ store: make(map[string]map[string]*KeyValue),
+ count: make(map[string]int),
+ keyLayers: InitLayer,
}
}
diff --git a/datasource/etcd/state/kvstore/cache_kv_test.go
b/datasource/etcd/state/kvstore/cache_kv_test.go
new file mode 100644
index 00000000..f18ff48c
--- /dev/null
+++ b/datasource/etcd/state/kvstore/cache_kv_test.go
@@ -0,0 +1,14 @@
+package kvstore
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func Test_getCacheDomainProjectKey(t *testing.T) {
+ testCache := new(KvCache)
+ str := "/cse-sr/inst/files/default/default/heheheh/xixixi/"
+ res := testCache.getCacheDomainProjectKey(str)
+ assert.Equal(t, "/cse-sr/inst/files/default/default/", res)
+}