Skip to content

Commit

Permalink
APIGOV-23542 - fixes for instance counting agains services in cache (#…
Browse files Browse the repository at this point in the history
…530)

* APIGOV-23542 - add instance count to persistence cache

* APIGOV-23542 - alleviate pointer issue

* APIGOV-23542 - export apiServiceToInstaneCount vars

* APIGOV-23542 - count instances when service added

Co-authored-by: sbolosan <[email protected]>
MVP @sbolosan!!!!
  • Loading branch information
jcollins-axway authored Sep 1, 2022
1 parent fa4a2f7 commit 26b8266
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
20 changes: 13 additions & 7 deletions pkg/agent/cache/apiservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

// apiServiceToInstanceCount
type apiServiceToInstanceCount struct {
count int
apiServiceKey string
Count int
ApiServiceKey string
}

// API service cache management
Expand All @@ -28,6 +28,7 @@ func (c *cacheManager) AddAPIService(svc *v1.ResourceInstance) error {
defer c.setCacheUpdated(true)
apiName, _ := util.GetAgentDetailsValue(svc, defs.AttrExternalAPIName)
primaryKey, _ := util.GetAgentDetailsValue(svc, defs.AttrExternalAPIPrimaryKey)
cachedRI, _ := c.GetAPIServiceInstanceByName(apiName)
if primaryKey != "" {
// Verify secondary key and validate if we need to remove it from the apiMap (cache)
if _, err := c.apiMap.Get(apiID); err != nil {
Expand All @@ -41,6 +42,11 @@ func (c *cacheManager) AddAPIService(svc *v1.ResourceInstance) error {
c.apiMap.SetWithSecondaryKey(apiID, apiName, svc)
c.apiMap.SetSecondaryKey(apiID, svc.Name)
}

if cachedRI == nil {
c.countCachedInstancesForAPIService(apiID, primaryKey)
}

c.logger.
WithField("api-name", apiName).
WithField("api-id", apiID).
Expand Down Expand Up @@ -162,13 +168,13 @@ func (c *cacheManager) addToServiceInstanceCount(apiID, primaryKey string) error
svcCount := apiServiceToInstanceCount{}
if svcCountI == nil {
svcCount = apiServiceToInstanceCount{
count: 0,
apiServiceKey: svc.Metadata.ID,
Count: 0,
ApiServiceKey: svc.Metadata.ID,
}
} else {
svcCount = svcCountI.(apiServiceToInstanceCount)
}
svcCount.count++
svcCount.Count++

c.instanceCountMap.Set(key, svcCount)
return nil
Expand All @@ -192,7 +198,7 @@ func (c *cacheManager) removeFromServiceInstanceCount(apiID, primaryKey string)
svcCount := apiServiceToInstanceCount{}
if svcCountI != nil {
svcCount = svcCountI.(apiServiceToInstanceCount)
svcCount.count--
svcCount.Count--
}

c.instanceCountMap.Set(key, svcCount)
Expand All @@ -213,7 +219,7 @@ func (c *cacheManager) GetAPIServiceInstanceCount(svcName string) int {
svcCount := apiServiceToInstanceCount{}
if svcCountI != nil {
svcCount = svcCountI.(apiServiceToInstanceCount)
return svcCount.count
return svcCount.Count
}

return 0
Expand Down
16 changes: 16 additions & 0 deletions pkg/agent/cache/apiserviceinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ func (c *cacheManager) ListAPIServiceInstances() []*v1.ResourceInstance {

return instances
}

// countCachedInstancesForAPIService - count any instances in the cache for hte newly added api
func (c *cacheManager) countCachedInstancesForAPIService(apiID, primaryKey string) {
for _, k := range c.instanceMap.GetKeys() {
item, _ := c.instanceMap.Get(k)
inst, ok := item.(*v1.ResourceInstance)
if !ok {
continue
}
instAPIID, _ := util.GetAgentDetailsValue(inst, defs.AttrExternalAPIID)
instPrimary, _ := util.GetAgentDetailsValue(inst, defs.AttrExternalAPIPrimaryKey)
if apiID == instAPIID || primaryKey == instPrimary {
c.addToServiceInstanceCount(apiID, primaryKey)
}
}
}
17 changes: 14 additions & 3 deletions pkg/agent/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const defaultCacheStoragePath = "./data/cache"
const instanceCount = "instanceCount"

// Manager - interface to manage agent resource
type Manager interface {
Expand Down Expand Up @@ -175,6 +176,7 @@ func (c *cacheManager) initializePersistedCache(cfg config.CentralConfig) {
"apiServices": func(loaded cache.Cache) { c.apiMap = loaded },
"apiServiceInstances": func(loaded cache.Cache) { c.instanceMap = loaded },
"categories": func(loaded cache.Cache) { c.categoryMap = loaded },
instanceCount: func(loaded cache.Cache) { c.instanceCountMap = loaded },
"credReqDef": func(loaded cache.Cache) { c.crdMap = loaded },
"accReqDef": func(loaded cache.Cache) { c.ardMap = loaded },
"teams": func(loaded cache.Cache) { c.teams = loaded },
Expand Down Expand Up @@ -229,10 +231,19 @@ func (c *cacheManager) loadPersistedResourceInstanceCache(cacheMap cache.Cache,
for _, key := range keys {
item, _ := riCache.Get(key)
rawResource, _ := json.Marshal(item)
ri := &v1.ResourceInstance{}
if json.Unmarshal(rawResource, ri) == nil {
riCache.Set(key, ri)
// If instance count then use apiServiceToInstanceCount type
if cacheKey == instanceCount {
ic := apiServiceToInstanceCount{}
if err := json.Unmarshal(rawResource, &ic); err == nil {
riCache.Set(key, ic)
}
} else {
ri := &v1.ResourceInstance{}
if json.Unmarshal(rawResource, ri) == nil {
riCache.Set(key, ri)
}
}

}
cacheMap.Set(cacheKey, riCache)
return riCache, isNew
Expand Down
7 changes: 4 additions & 3 deletions pkg/agent/cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ func TestSequenceCache(t *testing.T) {
// create manager
// add items to cache
// save cache
// create manager intialized with persisted cache
// vallidate all original cached items exists
// create manager initialized with persisted cache
// validate all original cached items exists
func TestCachePersistenc(t *testing.T) {
m := NewAgentCacheManager(&config.CentralConfiguration{AgentName: "test", GRPCCfg: config.GRPCConfig{Enabled: true}}, true)
assert.NotNil(t, m)

api1 := createAPIService("id1", "api1", "")
api1 := createAPIService("id1", "apiID", "")
err := m.AddAPIService(api1)
assert.Nil(t, err)

Expand All @@ -278,6 +278,7 @@ func TestCachePersistenc(t *testing.T) {
persistedAPI := m2.GetAPIServiceWithAPIID("id1")
assert.ElementsMatch(t, m.GetAPIServiceKeys(), m2.GetAPIServiceKeys())
assertResourceInstance(t, api1, persistedAPI)
assert.Equal(t, 1, m2.GetAPIServiceInstanceCount(api1.Name))

persistedInstance, err := m2.GetAPIServiceInstanceByID("id1")
assert.Nil(t, err)
Expand Down

0 comments on commit 26b8266

Please sign in to comment.