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-kie.git


The following commit(s) were added to refs/heads/dev by this push:
     new 9b9f442  fix the concurrent bug of KvIdCache (#304)
9b9f442 is described below

commit 9b9f4424b6c7d986de1b0d2691805a9edcbaa155
Author: tornado-ssy <64736788+tornado-...@users.noreply.github.com>
AuthorDate: Fri Dec 8 21:54:51 2023 +0800

    fix the concurrent bug of KvIdCache (#304)
    
    Co-authored-by: songshiyuan 00649746 <songshiyu...@huawei.com>
---
 server/datasource/etcd/kv/kv_cache.go | 39 ++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/server/datasource/etcd/kv/kv_cache.go 
b/server/datasource/etcd/kv/kv_cache.go
index 5776bf7..4aadfb1 100644
--- a/server/datasource/etcd/kv/kv_cache.go
+++ b/server/datasource/etcd/kv/kv_cache.go
@@ -9,15 +9,16 @@ import (
        "sync"
        "time"
 
-       "github.com/apache/servicecomb-kie/pkg/model"
-       "github.com/apache/servicecomb-kie/pkg/stringutil"
-       "github.com/apache/servicecomb-kie/server/datasource"
-       "github.com/apache/servicecomb-kie/server/datasource/etcd/key"
        "github.com/go-chassis/foundation/backoff"
        "github.com/go-chassis/openlog"
        "github.com/little-cui/etcdadpt"
        goCache "github.com/patrickmn/go-cache"
        "go.etcd.io/etcd/api/v3/mvccpb"
+
+       "github.com/apache/servicecomb-kie/pkg/model"
+       "github.com/apache/servicecomb-kie/pkg/stringutil"
+       "github.com/apache/servicecomb-kie/server/datasource"
+       "github.com/apache/servicecomb-kie/server/datasource/etcd/key"
 )
 
 func Init() {
@@ -35,8 +36,6 @@ const (
        backOffMinInterval   = 5 * time.Second
 )
 
-type IDSet map[string]struct{}
-
 type Cache struct {
        timeOut    time.Duration
        client     etcdadpt.Client
@@ -158,11 +157,13 @@ func (kc *Cache) cachePut(rsp *etcdadpt.Response) {
                cacheKey := kc.GetCacheKey(kvDoc.Domain, kvDoc.Project, 
kvDoc.Labels)
                m, ok := kc.LoadKvIDSet(cacheKey)
                if !ok {
-                       kc.StoreKvIDSet(cacheKey, IDSet{kvDoc.ID: struct{}{}})
+                       z := &sync.Map{}
+                       z.Store(kvDoc.ID, struct{}{})
+                       kc.StoreKvIDSet(cacheKey, z)
                        openlog.Info("cacheKey " + cacheKey + "not exists")
                        continue
                }
-               m[kvDoc.ID] = struct{}{}
+               m.Store(kvDoc.ID, struct{}{})
        }
 }
 
@@ -180,23 +181,23 @@ func (kc *Cache) cacheDelete(rsp *etcdadpt.Response) {
                        openlog.Error("cacheKey " + cacheKey + "not exists")
                        continue
                }
-               delete(m, kvDoc.ID)
+               m.Delete(kvDoc.ID)
        }
 }
 
-func (kc *Cache) LoadKvIDSet(cacheKey string) (IDSet, bool) {
+func (kc *Cache) LoadKvIDSet(cacheKey string) (*sync.Map, bool) {
        val, ok := kc.kvIDCache.Load(cacheKey)
        if !ok {
                return nil, false
        }
-       kvIds, ok := val.(IDSet)
+       kvIds, ok := val.(*sync.Map)
        if !ok {
                return nil, false
        }
        return kvIds, true
 }
 
-func (kc *Cache) StoreKvIDSet(cacheKey string, kvIds IDSet) {
+func (kc *Cache) StoreKvIDSet(cacheKey string, kvIds *sync.Map) {
        kc.kvIDCache.Store(cacheKey, kvIds)
 }
 
@@ -232,21 +233,21 @@ func Search(ctx context.Context, req *CacheSearchReq) 
(*model.KVResponse, bool,
        cacheKey := kvCache.GetCacheKey(req.Domain, req.Project, 
req.Opts.Labels)
        kvIds, ok := kvCache.LoadKvIDSet(cacheKey)
        if !ok {
-               kvCache.StoreKvIDSet(cacheKey, IDSet{})
+               kvCache.StoreKvIDSet(cacheKey, &sync.Map{})
                return result, true, nil
        }
 
        var docs []*model.KVDoc
 
        var kvIdsLeft []string
-       for kvID := range kvIds {
-               if doc, ok := kvCache.LoadKvDoc(kvID); ok {
+       kvIds.Range(func(kvID, value any) bool {
+               if doc, ok := kvCache.LoadKvDoc(kvID.(string)); ok {
                        docs = append(docs, doc)
-                       continue
+               } else {
+                       kvIdsLeft = append(kvIdsLeft, kvID.(string))
                }
-               kvIdsLeft = append(kvIdsLeft, kvID)
-       }
-
+               return true
+       })
        tpData := kvCache.getKvFromEtcd(ctx, req, kvIdsLeft)
        docs = append(docs, tpData...)
 

Reply via email to