From bac84b35cb8ee759c66ac05fa997d0f3b1139b4c Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 28 Oct 2019 14:12:59 +0800 Subject: [PATCH 1/3] [Bug]Fixed #62 --- agcache/cache.go | 30 +++----------------- agcache/default.go | 58 ++++++++++++++++++++++++++++++++++++++ componet_notify_test.go | 62 ++++++----------------------------------- go.mod | 6 +--- go.sum | 1 + repository.go | 32 +++++++-------------- repository_test.go | 36 ------------------------ 7 files changed, 83 insertions(+), 142 deletions(-) create mode 100644 agcache/default.go diff --git a/agcache/cache.go b/agcache/cache.go index 10a74bf7..8a2b5c59 100644 --- a/agcache/cache.go +++ b/agcache/cache.go @@ -1,44 +1,22 @@ package agcache -import "github.com/coocood/freecache" - //CacheInterface 自定义缓存组件接口 type CacheInterface interface { - Set(key, value []byte, expireSeconds int) (err error) + Set(key string, value []byte, expireSeconds int) (err error) EntryCount() (entryCount int64) - Get(key []byte) (value []byte, err error) - - Del(key []byte) (affected bool) + Get(key string) (value []byte, err error) - NewIterator() *freecache.Iterator + Del(key string) (affected bool) - TTL(key []byte) (timeLeft uint32, err error) + Range(f func(key, value interface{}) bool) Clear() } -const ( - //50m - apolloConfigCacheSize = 50 * 1024 * 1024 - - //1 minute - configCacheExpireTime = 120 -) - //CacheFactory 缓存组件工厂接口 type CacheFactory interface { //Create 创建缓存组件 Create() CacheInterface -} - -//DefaultCacheFactory 构造默认缓存组件工厂类 -type DefaultCacheFactory struct { - -} - -//Create 创建默认缓存组件 -func (d *DefaultCacheFactory) Create()CacheInterface { - return freecache.NewCache(apolloConfigCacheSize) } \ No newline at end of file diff --git a/agcache/default.go b/agcache/default.go new file mode 100644 index 00000000..c9270915 --- /dev/null +++ b/agcache/default.go @@ -0,0 +1,58 @@ +package agcache + +import ( + "errors" + "sync" +) + + +type DefaultCache struct { + defaultCache sync.Map +} + +func (d *DefaultCache)Set(key string, value []byte, expireSeconds int) (err error) { + d.defaultCache.Store(key,value) + return nil +} + +func (d *DefaultCache)EntryCount() (entryCount int64){ + count:=int64(0) + d.defaultCache.Range(func(key, value interface{}) bool { + count++ + return true + }) + return count +} + + +func (d *DefaultCache)Get(key string) (value []byte, err error){ + v, ok := d.defaultCache.Load(key) + if !ok{ + return nil,errors.New("load default cache fail!") + } + return v.([]byte),nil +} + +func (d *DefaultCache)Range(f func(key, value interface{}) bool){ + d.defaultCache.Range(f) +} + +func (d *DefaultCache)Del(key string) (affected bool) { + d.defaultCache.Delete(key) + return true +} + +func (d *DefaultCache)Clear() { + d.defaultCache=sync.Map{} +} + +//DefaultCacheFactory 构造默认缓存组件工厂类 +type DefaultCacheFactory struct { + +} + +//Create 创建默认缓存组件 +func (d *DefaultCacheFactory) Create()CacheInterface { + return &DefaultCache{} +} + diff --git a/componet_notify_test.go b/componet_notify_test.go index cbbc66bd..5d70ff72 100644 --- a/componet_notify_test.go +++ b/componet_notify_test.go @@ -169,17 +169,11 @@ func TestAutoSyncConfigServicesNormal2NotModified(t *testing.T) { fmt.Println("checking agcache time left") defaultConfigCache := getDefaultConfigCache() - it := defaultConfigCache.NewIterator() - for i := int64(0); i < defaultConfigCache.EntryCount(); i++ { - entry := it.Next() - if entry == nil { - break - } - timeLeft, err := defaultConfigCache.TTL([]byte(entry.Key)) - Assert(t, err,NilVal()) - fmt.Printf("key:%s,time:%v \n", string(entry.Key), timeLeft) - Assert(t, timeLeft >= 110, Equal(true)) - } + + defaultConfigCache.Range(func(key, value interface{}) bool { + Assert(t, string(value.([]byte)),NotNilVal()) + return true + }) Assert(t, "100004458", Equal(config.AppId)) Assert(t, "default", Equal(config.Cluster)) @@ -191,17 +185,10 @@ func TestAutoSyncConfigServicesNormal2NotModified(t *testing.T) { err := autoSyncConfigServices(newAppConfig) fmt.Println("checking agcache time left") - it1 := defaultConfigCache.NewIterator() - for i := int64(0); i < defaultConfigCache.EntryCount(); i++ { - entry := it1.Next() - if entry == nil { - break - } - timeLeft, err := defaultConfigCache.TTL([]byte(entry.Key)) - Assert(t, err,NilVal()) - fmt.Printf("key:%s,time:%v \n", string(entry.Key), timeLeft) - Assert(t, timeLeft >= 120, Equal(true)) - } + defaultConfigCache.Range(func(key, value interface{}) bool { + Assert(t, string(value.([]byte)),NotNilVal()) + return true + }) fmt.Println(err) @@ -220,37 +207,6 @@ func checkBackupFile(t *testing.T) { } } -//test if not modify -func TestAutoSyncConfigServicesNotModify(t *testing.T) { - server := runNotModifyConfigResponse() - newAppConfig := getTestAppConfig() - newAppConfig.Ip = server.URL - - apolloConfig, err := createApolloConfigWithJson([]byte(configResponseStr)) - updateApolloConfig(apolloConfig, true) - - time.Sleep(10 * time.Second) - checkCacheLeft(t, configCacheExpireTime-10) - - appConfig.NextTryConnTime = 0 - - err = autoSyncConfigServices(newAppConfig) - - Assert(t, err,NilVal()) - - config := GetCurrentApolloConfig()[newAppConfig.NamespaceName] - - Assert(t, "100004458", Equal(config.AppId)) - Assert(t, "default", Equal(config.Cluster)) - Assert(t, "application", Equal(config.NamespaceName)) - Assert(t, "20170430092936-dee2d58e74515ff3", Equal(config.ReleaseKey)) - - checkCacheLeft(t, configCacheExpireTime) - - //Assert(t,"value1",config.Configurations["key1"]) - //Assert(t,"value2",config.Configurations["key2"]) -} - func TestAutoSyncConfigServicesError(t *testing.T) { //reload app properties go initFileConfig() diff --git a/go.mod b/go.mod index e68b939f..c377ab7e 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,3 @@ module github.com/zouyx/agollo -require ( - github.com/cespare/xxhash v1.1.0 - github.com/coocood/freecache v1.1.0 - github.com/tevid/gohamcrest v1.1.1 -) +require github.com/tevid/gohamcrest v1.1.1 diff --git a/go.sum b/go.sum index 3ef43d81..02cabcf3 100644 --- a/go.sum +++ b/go.sum @@ -2,4 +2,5 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/coocood/freecache v1.1.0/go.mod h1:ePwxCDzOYvARfHdr1pByNct1at3CoKnsipOHwKlNbzI= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/tevid/gohamcrest v1.1.1 h1:ou+xSqlIw1xfGTg1uq1nif/htZ2S3EzRqLm2BP+tYU0= github.com/tevid/gohamcrest v1.1.1/go.mod h1:3UvtWlqm8j5JbwYZh80D/PVBt0mJ1eJiYgZMibh0H/k= diff --git a/repository.go b/repository.go index 29eee52c..0a89652b 100644 --- a/repository.go +++ b/repository.go @@ -63,7 +63,7 @@ type Config struct { } //getConfigValue 获取配置值 func (this *Config) getConfigValue(key string) interface{} { - value, err := this.cache.Get([]byte(key)) + value, err := this.cache.Get(key) if err != nil { logger.Errorf("get config value fail!key:%s,err:%s", key, err) return empty @@ -193,10 +193,10 @@ func updateApolloConfigCache(configurations map[string]string, expireTime int,na //get old keys mp := map[string]bool{} - it := config.cache.NewIterator() - for en := it.Next(); en != nil; en = it.Next() { - mp[string(en.Key)] = true - } + config.cache.Range(func(key, value interface{}) bool { + mp[key.(string)] = true + return true + }) changes := make(map[string]*ConfigChange) @@ -210,13 +210,13 @@ func updateApolloConfigCache(configurations map[string]string, expireTime int,na changes[key] = createAddConfigChange(value) } else { //update - oldValue, _ := config.cache.Get([]byte(key)) + oldValue, _ := config.cache.Get(key) if string(oldValue) != value { changes[key] = createModifyConfigChange(string(oldValue), value) } } - config.cache.Set([]byte(key), []byte(value), expireTime) + config.cache.Set(key, []byte(value), expireTime) delete(mp, string(key)) } } @@ -224,10 +224,10 @@ func updateApolloConfigCache(configurations map[string]string, expireTime int,na // remove del keys for key := range mp { //get old value and del - oldValue, _ :=config.cache.Get([]byte(key)) + oldValue, _ :=config.cache.Get(key) changes[key] = createDeletedConfigChange(string(oldValue)) - config.cache.Del([]byte(key)) + config.cache.Del(key) } return changes @@ -242,21 +242,9 @@ func createConfigChangeEvent(changes map[string]*ConfigChange, nameSpace string) } func touchApolloConfigCache() error { - updateApolloConfigCacheTime(configCacheExpireTime) return nil } -func updateApolloConfigCacheTime(expireTime int) { - it := getDefaultConfigCache().NewIterator() - for i := int64(0); i < getDefaultConfigCache().EntryCount(); i++ { - entry := it.Next() - if entry == nil { - break - } - getDefaultConfigCache().Set([]byte(entry.Key), []byte(entry.Value), expireTime) - } -} - //GetApolloConfigCache 获取默认namespace的apollo配置 func GetApolloConfigCache() agcache.CacheInterface { return getDefaultConfigCache() @@ -282,7 +270,7 @@ func getCurrentApolloConfigReleaseKey(namespace string) string { } func getConfigValue(key string) interface{} { - value, err := getDefaultConfigCache().Get([]byte(key)) + value, err := getDefaultConfigCache().Get(key) if err != nil { logger.Errorf("get config value fail!key:%s,err:%s", key, err) return empty diff --git a/repository_test.go b/repository_test.go index 7e89601b..47de3aa9 100644 --- a/repository_test.go +++ b/repository_test.go @@ -27,27 +27,6 @@ func createMockApolloConfig(expireTime int) map[string]string { return configs } -func TestTouchApolloConfigCache(t *testing.T) { - createMockApolloConfig(10) - - time.Sleep(5 * time.Second) - checkCacheLeft(t, 5) - - updateApolloConfigCacheTime(10) - - checkCacheLeft(t, 10) -} - -func checkCacheLeft(t *testing.T, excepted uint32) { - defaultConfigCache := getDefaultConfigCache() - it := defaultConfigCache.NewIterator() - for i := int64(0); i < defaultConfigCache.EntryCount(); i++ { - entry := it.Next() - left, _ := defaultConfigCache.TTL(entry.Key) - Assert(t, true, Equal(left == uint32(excepted))) - } -} - func getFirstApolloConfig(t *testing.T,currentConfig map[string]*ApolloConnConfig)[]byte { i:=0 var currentJSON []byte @@ -101,21 +80,6 @@ func TestGetApolloConfigCache(t *testing.T) { Assert(t, cache,NotNilVal()) } -func TestGetConfigValueTimeout(t *testing.T) { - expireTime := 5 - configs := createMockApolloConfig(expireTime) - - for k, v := range configs { - Assert(t, v, Equal(getValue(k))) - } - - time.Sleep(time.Duration(expireTime) * time.Second) - - for k := range configs { - Assert(t, "", Equal(getValue(k))) - } -} - func TestGetConfigValueNullApolloConfig(t *testing.T) { //clear Configurations defaultConfigCache := getDefaultConfigCache() From 712a0940acb5ec3d9aebf1bfd740efbb5ce5bdf2 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 28 Oct 2019 14:53:31 +0800 Subject: [PATCH 2/3] [Bug]Fixed pr problems --- agcache/default.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/agcache/default.go b/agcache/default.go index c9270915..4c7aeb66 100644 --- a/agcache/default.go +++ b/agcache/default.go @@ -5,16 +5,18 @@ import ( "sync" ) - +//DefaultCache 默认缓存 type DefaultCache struct { defaultCache sync.Map } +//Set 获取缓存 func (d *DefaultCache)Set(key string, value []byte, expireSeconds int) (err error) { d.defaultCache.Store(key,value) return nil } +//EntryCount 获取实体数量 func (d *DefaultCache)EntryCount() (entryCount int64){ count:=int64(0) d.defaultCache.Range(func(key, value interface{}) bool { @@ -24,24 +26,27 @@ func (d *DefaultCache)EntryCount() (entryCount int64){ return count } - +//Get 获取缓存 func (d *DefaultCache)Get(key string) (value []byte, err error){ v, ok := d.defaultCache.Load(key) if !ok{ - return nil,errors.New("load default cache fail!") + return nil,errors.New("load default cache fail") } return v.([]byte),nil } +//Range 遍历缓存 func (d *DefaultCache)Range(f func(key, value interface{}) bool){ d.defaultCache.Range(f) } +//Del 删除缓存 func (d *DefaultCache)Del(key string) (affected bool) { d.defaultCache.Delete(key) return true } +//Clear 清除所有缓存 func (d *DefaultCache)Clear() { d.defaultCache=sync.Map{} } From 71b2538ae187478399c4eb05be8451184e286fd6 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 28 Oct 2019 14:58:05 +0800 Subject: [PATCH 3/3] [Feature]Add auto comment --- .github/auto-comment.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/auto-comment.yml diff --git a/.github/auto-comment.yml b/.github/auto-comment.yml new file mode 100644 index 00000000..8fcd15f4 --- /dev/null +++ b/.github/auto-comment.yml @@ -0,0 +1,17 @@ +# Number of days of inactivity before an issue becomes stale +daysUntilStale: 60 +# Number of days of inactivity before a stale issue is closed +daysUntilClose: 30 +# Issues with these labels will never be considered stale +exemptLabels: +- pinned +- security +# Label to use when marking an issue as stale +staleLabel: stale +# Comment to post when marking an issue as stale. Set to `false` to disable +markComment: > + This issue has been automatically marked as stale because it has not had + recent activity. It will be closed if no further activity occurs. Thank you + for your contributions. +# Comment to post when closing a stale issue. Set to `false` to disable +closeComment: false \ No newline at end of file