Skip to content

Commit

Permalink
Finish v2.1.2
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/auto-comment.yml
  • Loading branch information
zouyx committed Oct 28, 2019
2 parents 88bf8da + 2d5a23d commit bd0e80a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 142 deletions.
30 changes: 4 additions & 26 deletions agcache/cache.go
Original file line number Diff line number Diff line change
@@ -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)
}
63 changes: 63 additions & 0 deletions agcache/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package agcache

import (
"errors"
"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 {
count++
return true
})
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 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{}
}

//DefaultCacheFactory 构造默认缓存组件工厂类
type DefaultCacheFactory struct {

}

//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create()CacheInterface {
return &DefaultCache{}
}

62 changes: 9 additions & 53 deletions componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)

Expand All @@ -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()
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
32 changes: 10 additions & 22 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -210,24 +210,24 @@ 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))
}
}

// 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
Expand All @@ -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()
Expand All @@ -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
Expand Down
36 changes: 0 additions & 36 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bd0e80a

Please sign in to comment.