Skip to content

Commit acc56e7

Browse files
authored
Split the endpoints cache and default cache (namespace and service) so that relation operations are independent and unblocking. Increase the size of the cache to 2048 accommodate large namespaces. (#249)
1 parent 9b3a1cb commit acc56e7

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

pkg/cloudmap/cache.go

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import (
1111
)
1212

1313
const (
14-
nsKey = "ns-map"
15-
svcKeyPrefix = "svc-map"
16-
endptsKeyPrefix = "endpts"
14+
nsKey = "ns-map"
15+
svcKeyPrefix = "svc-map"
1716

18-
defaultCacheSize = 1024
17+
defaultCacheSize = 2048
1918
defaultNsTTL = 10 * time.Second
2019
defaultSvcTTL = 10 * time.Second
2120
defaultEndptTTL = 5 * time.Second
@@ -34,9 +33,10 @@ type ServiceDiscoveryClientCache interface {
3433
}
3534

3635
type sdCache struct {
37-
log common.Logger
38-
cache *cache.LRUExpireCache
39-
config *SdCacheConfig
36+
log common.Logger
37+
defaultCache *cache.LRUExpireCache
38+
endpointsCache *cache.LRUExpireCache
39+
config *SdCacheConfig
4040
}
4141

4242
type SdCacheConfig struct {
@@ -47,9 +47,10 @@ type SdCacheConfig struct {
4747

4848
func NewServiceDiscoveryClientCache(cacheConfig *SdCacheConfig) ServiceDiscoveryClientCache {
4949
return &sdCache{
50-
log: common.NewLogger("cloudmap"),
51-
cache: cache.NewLRUExpireCache(defaultCacheSize),
52-
config: cacheConfig,
50+
log: common.NewLogger("cloudmap"),
51+
defaultCache: cache.NewLRUExpireCache(defaultCacheSize),
52+
endpointsCache: cache.NewLRUExpireCache(defaultCacheSize),
53+
config: cacheConfig,
5354
}
5455
}
5556

@@ -63,41 +64,41 @@ func NewDefaultServiceDiscoveryClientCache() ServiceDiscoveryClientCache {
6364
}
6465

6566
func (sdCache *sdCache) GetNamespaceMap() (namespaceMap map[string]*model.Namespace, found bool) {
66-
entry, exists := sdCache.cache.Get(nsKey)
67+
entry, exists := sdCache.defaultCache.Get(nsKey)
6768
if !exists {
6869
return nil, false
6970
}
7071

7172
namespaceMap, ok := entry.(map[string]*model.Namespace)
7273
if !ok {
7374
sdCache.log.Error(errors.New("failed to retrieve namespaceMap from cache"), "")
74-
sdCache.cache.Remove(nsKey)
75+
sdCache.defaultCache.Remove(nsKey)
7576
return nil, false
7677
}
7778

7879
return namespaceMap, true
7980
}
8081

8182
func (sdCache *sdCache) CacheNamespaceMap(namespaces map[string]*model.Namespace) {
82-
sdCache.cache.Add(nsKey, namespaces, sdCache.config.NsTTL)
83+
sdCache.defaultCache.Add(nsKey, namespaces, sdCache.config.NsTTL)
8384
}
8485

8586
func (sdCache *sdCache) EvictNamespaceMap() {
86-
sdCache.cache.Remove(nsKey)
87+
sdCache.defaultCache.Remove(nsKey)
8788
}
8889

8990
func (sdCache *sdCache) GetServiceIdMap(nsName string) (serviceIdMap map[string]string, found bool) {
9091
key := sdCache.buildSvcKey(nsName)
91-
entry, exists := sdCache.cache.Get(key)
92+
entry, exists := sdCache.defaultCache.Get(key)
9293
if !exists {
9394
return nil, false
9495
}
9596

9697
serviceIdMap, ok := entry.(map[string]string)
9798
if !ok {
98-
sdCache.log.Error(errors.New("failed to retrieve service IDs from cache"), "",
99-
"nsName", nsName)
100-
sdCache.cache.Remove(key)
99+
err := fmt.Errorf("failed to retrieve service IDs from cache")
100+
sdCache.log.Error(err, err.Error(), "namespace", nsName)
101+
sdCache.defaultCache.Remove(key)
101102
return nil, false
102103
}
103104

@@ -106,26 +107,26 @@ func (sdCache *sdCache) GetServiceIdMap(nsName string) (serviceIdMap map[string]
106107

107108
func (sdCache *sdCache) CacheServiceIdMap(nsName string, serviceIdMap map[string]string) {
108109
key := sdCache.buildSvcKey(nsName)
109-
sdCache.cache.Add(key, serviceIdMap, sdCache.config.SvcTTL)
110+
sdCache.defaultCache.Add(key, serviceIdMap, sdCache.config.SvcTTL)
110111
}
111112

112113
func (sdCache *sdCache) EvictServiceIdMap(nsName string) {
113114
key := sdCache.buildSvcKey(nsName)
114-
sdCache.cache.Remove(key)
115+
sdCache.defaultCache.Remove(key)
115116
}
116117

117118
func (sdCache *sdCache) GetEndpoints(nsName string, svcName string) (endpts []*model.Endpoint, found bool) {
118119
key := sdCache.buildEndptsKey(nsName, svcName)
119-
entry, exists := sdCache.cache.Get(key)
120+
entry, exists := sdCache.endpointsCache.Get(key)
120121
if !exists {
121122
return nil, false
122123
}
123124

124125
endpts, ok := entry.([]*model.Endpoint)
125126
if !ok {
126-
sdCache.log.Error(errors.New("failed to retrieve endpoints from cache"), "",
127-
"ns", "nsName", "svc", svcName)
128-
sdCache.cache.Remove(key)
127+
err := fmt.Errorf("failed to retrieve endpoints from cache")
128+
sdCache.log.Error(err, err.Error(), "namespace", nsName, "service", svcName)
129+
sdCache.endpointsCache.Remove(key)
129130
return nil, false
130131
}
131132

@@ -134,18 +135,18 @@ func (sdCache *sdCache) GetEndpoints(nsName string, svcName string) (endpts []*m
134135

135136
func (sdCache *sdCache) CacheEndpoints(nsName string, svcName string, endpts []*model.Endpoint) {
136137
key := sdCache.buildEndptsKey(nsName, svcName)
137-
sdCache.cache.Add(key, endpts, sdCache.config.EndptTTL)
138+
sdCache.endpointsCache.Add(key, endpts, sdCache.config.EndptTTL)
138139
}
139140

140141
func (sdCache *sdCache) EvictEndpoints(nsName string, svcName string) {
141142
key := sdCache.buildEndptsKey(nsName, svcName)
142-
sdCache.cache.Remove(key)
143+
sdCache.endpointsCache.Remove(key)
143144
}
144145

145146
func (sdCache *sdCache) buildSvcKey(nsName string) (cacheKey string) {
146147
return fmt.Sprintf("%s:%s", svcKeyPrefix, nsName)
147148
}
148149

149150
func (sdCache *sdCache) buildEndptsKey(nsName string, svcName string) string {
150-
return fmt.Sprintf("%s:%s:%s", endptsKeyPrefix, nsName, svcName)
151+
return fmt.Sprintf("%s:%s", nsName, svcName)
151152
}

pkg/cloudmap/cache_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestServiceDiscoveryClientCacheGetNamespaceMap_NotFound(t *testing.T) {
5959

6060
func TestServiceDiscoveryClientCacheGetNamespaceMap_Corrupt(t *testing.T) {
6161
sdc := getCacheImpl(t)
62-
sdc.cache.Add(nsKey, &model.Plan{}, time.Minute)
62+
sdc.defaultCache.Add(nsKey, &model.Plan{}, time.Minute)
6363

6464
nsMap, found := sdc.GetNamespaceMap()
6565
assert.False(t, found)
@@ -99,7 +99,7 @@ func TestServiceDiscoveryClientCacheGetServiceIdMap_NotFound(t *testing.T) {
9999

100100
func TestServiceDiscoveryClientCacheGetServiceIdMap_Corrupt(t *testing.T) {
101101
sdc := getCacheImpl(t)
102-
sdc.cache.Add(sdc.buildSvcKey(test.HttpNsName), &model.Plan{}, time.Minute)
102+
sdc.defaultCache.Add(sdc.buildSvcKey(test.HttpNsName), &model.Plan{}, time.Minute)
103103

104104
svcIdMap, found := sdc.GetServiceIdMap(test.HttpNsName)
105105
assert.False(t, found)
@@ -137,7 +137,7 @@ func TestServiceDiscoveryClientCacheGetEndpoints_NotFound(t *testing.T) {
137137

138138
func TestServiceDiscoveryClientCacheGetEndpoints_Corrupt(t *testing.T) {
139139
sdc := getCacheImpl(t)
140-
sdc.cache.Add(sdc.buildEndptsKey(test.HttpNsName, test.SvcName), &model.Plan{}, time.Minute)
140+
sdc.defaultCache.Add(sdc.buildEndptsKey(test.HttpNsName, test.SvcName), &model.Plan{}, time.Minute)
141141

142142
endpts, found := sdc.GetEndpoints(test.HttpNsName, test.SvcName)
143143
assert.False(t, found)
@@ -156,7 +156,8 @@ func TestServiceDiscoveryClientEvictEndpoints(t *testing.T) {
156156

157157
func getCacheImpl(t *testing.T) sdCache {
158158
return sdCache{
159-
log: common.NewLoggerWithLogr(testr.New(t)),
160-
cache: cache.NewLRUExpireCache(defaultCacheSize),
159+
log: common.NewLoggerWithLogr(testr.New(t)),
160+
defaultCache: cache.NewLRUExpireCache(defaultCacheSize),
161+
endpointsCache: cache.NewLRUExpireCache(defaultCacheSize),
161162
}
162163
}

0 commit comments

Comments
 (0)