Skip to content

Commit a6ef87c

Browse files
tornado-ssysongshiyuan 00649746
and
songshiyuan 00649746
authored
[opt] improve the tps of registerInstance interface (#1458)
Co-authored-by: songshiyuan 00649746 <songshiyuan3@huawei.com>
1 parent 58dd5cd commit a6ef87c

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

datasource/etcd/state/kvstore/cache_kv.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
package kvstore
1919

2020
import (
21+
regexp2 "regexp"
2122
"strings"
2223
"sync"
2324

2425
"github.com/apache/servicecomb-service-center/pkg/util"
2526
)
2627

28+
const InitCount = 1
29+
const InitLayer = 2
30+
const SPLIT = "/"
31+
const DomainProjectLayer = 6
32+
2733
// KvCache implements Cache.
2834
// KvCache is dedicated to stores service discovery data,
2935
// e.g. service, instance, lease.
3036
type KvCache struct {
31-
Cfg *Options
32-
name string
33-
store map[string]map[string]*KeyValue
34-
rwMux sync.RWMutex
35-
dirty bool
37+
Cfg *Options
38+
name string
39+
store map[string]map[string]*KeyValue
40+
rwMux sync.RWMutex
41+
dirty bool
42+
count map[string]int // the number of leaf node
43+
keyLayers int // the number of layers of leaf nodes
3644
}
3745

3846
func (c *KvCache) Name() string {
@@ -63,6 +71,12 @@ func (c *KvCache) GetAll(arr *[]*KeyValue) (count int) {
6371
return
6472
}
6573

74+
func (c *KvCache) getCacheDomainProjectKey(key string) string {
75+
regexp, _ := regexp2.Compile(`/(\w)+-(\w)+/(\w)+/(\w)+/(\w)+/(\w)+/`)
76+
domainProjectKey := regexp.FindString(key)
77+
return domainProjectKey
78+
}
79+
6680
func (c *KvCache) GetPrefix(prefix string, arr *[]*KeyValue) (count int) {
6781
c.rwMux.RLock()
6882
count = c.getPrefixKey(arr, prefix)
@@ -124,6 +138,11 @@ func (c *KvCache) getPrefixKey(arr *[]*KeyValue, prefix string) (count int) {
124138
return 0
125139
}
126140

141+
if arr == nil && strings.Count(prefix, SPLIT) == DomainProjectLayer {
142+
count = c.count[prefix]
143+
return
144+
}
145+
127146
// TODO support sort option
128147
if arr == nil {
129148
for key := range keysRef {
@@ -156,6 +175,10 @@ func (c *KvCache) addPrefixKey(key string, val *KeyValue) {
156175
return
157176
}
158177
keys, ok := c.store[prefix]
178+
if strings.Count(key, SPLIT) > c.keyLayers {
179+
c.count[c.getCacheDomainProjectKey(key)] = InitCount
180+
c.keyLayers = strings.Count(key, SPLIT)
181+
}
159182
if !ok {
160183
// build parent index key and new child nodes
161184
keys = make(map[string]*KeyValue)
@@ -166,6 +189,8 @@ func (c *KvCache) addPrefixKey(key string, val *KeyValue) {
166189
keys[key] = val
167190
}
168191
return
192+
} else if _, ok := keys[key]; !ok {
193+
c.count[c.getCacheDomainProjectKey(key)]++
169194
}
170195

171196
keys[key], key = val, prefix
@@ -178,6 +203,9 @@ func (c *KvCache) deletePrefixKey(key string) {
178203
if !ok {
179204
return
180205
}
206+
if strings.Count(key, SPLIT) == c.keyLayers {
207+
c.count[c.getCacheDomainProjectKey(key)]--
208+
}
181209
delete(m, key)
182210

183211
// remove parent which has no child
@@ -189,8 +217,10 @@ func (c *KvCache) deletePrefixKey(key string) {
189217

190218
func NewKvCache(name string, cfg *Options) *KvCache {
191219
return &KvCache{
192-
Cfg: cfg,
193-
name: name,
194-
store: make(map[string]map[string]*KeyValue),
220+
Cfg: cfg,
221+
name: name,
222+
store: make(map[string]map[string]*KeyValue),
223+
count: make(map[string]int),
224+
keyLayers: InitLayer,
195225
}
196226
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package kvstore
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_getCacheDomainProjectKey(t *testing.T) {
10+
testCache := new(KvCache)
11+
str := "/cse-sr/inst/files/default/default/heheheh/xixixi/"
12+
res := testCache.getCacheDomainProjectKey(str)
13+
assert.Equal(t, "/cse-sr/inst/files/default/default/", res)
14+
}

0 commit comments

Comments
 (0)