Skip to content

Commit

Permalink
Added logging for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shilparamasamyreddy committed Aug 29, 2024
1 parent 16e2d61 commit 061f668
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
3 changes: 3 additions & 0 deletions internal/cf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func (c *spaceClient) populateResourceCache() {
refreshResourceCacheMutex.Lock()
defer refreshResourceCacheMutex.Unlock()
if c.resourceCache.IsCacheExpired() {
//print cache is expired and populate the cache
fmt.Println("For the first or Cache is expired and populating the cache")
instanceOptions := cfclient.NewServiceInstanceListOptions()
instanceOptions.ListOptions.LabelSelector.EqualTo(labelOwner)
instanceOptions.Page = 1
Expand All @@ -248,6 +250,7 @@ func (c *spaceClient) populateResourceCache() {
for {
srvInstanes, pager, err := c.client.ServiceInstances.List(ctx, instanceOptions)
if err != nil {
//TODO:revisit to see how to handle if error occurs
log.Fatalf("Error listing service instances: %s", err)
}

Expand Down
25 changes: 11 additions & 14 deletions internal/cf/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,25 @@ func (c *spaceClient) GetInstance(ctx context.Context, instanceOpts map[string]s
var instance *facade.Instance
//TODO recheck this logic later
if c.resourceCache.IsCacheExpired() {

//TODO: remove later:Print cache is expited
fmt.Println("Cache is expired")
c.populateResourceCache()
}
if len(c.resourceCache.GetCachedInstances()) != 0 {

instance, instanceInCache = c.resourceCache.GetInstanceFromCache(instanceOpts["owner"])
//TODO:remove later: get all instances and print
instances := c.resourceCache.GetCachedInstances()
for key, value := range instances {
fmt.Printf("Key: %s, Value: %v\n", key, value)
}
//TODO: remove later: print length of cache
fmt.Printf("Length of cache: %d\n", len(c.resourceCache.GetCachedInstances()))

}

if instanceInCache {
// TODO remove this printf later
fmt.Printf("Got the instance from Cache")
return instance, nil
}
}
//TODO:remove later:Print not found in cache or cache is empty or instance not found
fmt.Println("Not found in cache or cache is empty or instance not found in cf")

// Attempt to retrieve instance from Cloud Foundry
var serviceInstance *cfresource.ServiceInstance

Expand Down Expand Up @@ -186,20 +185,18 @@ func (c *spaceClient) UpdateInstance(ctx context.Context, guid string, name stri
c.resourceCache.AddInstanceInCache(owner, instance)

}
//TODO:remove later: print instance added in cache and print the instance
fmt.Println("Instance added or updated in cache from update instance function")
}
//TODO:remove later: get all instances and print
instances := c.resourceCache.GetCachedInstances()
for key, value := range instances {
fmt.Printf("Key: %s, Value: %v\n", key, value)
}

return err
}

func (c *spaceClient) DeleteInstance(ctx context.Context, guid string, owner string) error {
// TODO: return jobGUID to enable querying the job deletion status
_, err := c.client.ServiceInstances.Delete(ctx, guid)
if err == nil && c.resourceCache.IsResourceCacheEnabled() {
c.resourceCache.RemoveInstanceFromCache(owner)
c.resourceCache.DeleteInstanceFromCache(owner)
}
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
type Config struct {

//Resource cache is enabled or disabled
//TODO change it back to false after testing
IsResourceCacheEnabled bool `env:"RESOURCE_CACHE_ENABLED" envDefault:"true"`
IsResourceCacheEnabled bool `env:"RESOURCE_CACHE_ENABLED" envDefault:"false"`

//cache timeout in seconds,minutes or hours
CacheTimeOut string `env:"CACHE_TIMEOUT" envDefault:"1m"`
Expand Down
17 changes: 13 additions & 4 deletions internal/facade/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (c *ResourceCache) SetCacheTimeOut(timeOut string) {
func (c *ResourceCache) IsCacheExpired() bool {

expirationTime := c.lastCacheTime.Add(c.cacheTimeOut)
//expiryTime := time.Until(c.lastCacheTime)
fmt.Printf("Expiry time: %v\n", expirationTime)
fmt.Printf("Cache timeout: %v\n", c.cacheTimeOut)
return time.Now().After(expirationTime)
Expand Down Expand Up @@ -205,27 +204,35 @@ func (c *ResourceCache) AddInstanceInCache(key string, instance *Instance) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.instances[key] = instance
// TODO :remove later:addedinstance to cache and print the instance
fmt.Printf("Added instance to cache: %v\n", instance)
}

// GetInstanceFromCache retrieves an instance from the cache
func (c *ResourceCache) GetInstanceFromCache(key string) (*Instance, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
instance, found := c.instances[key]
// TODO :remove later: remove this printf later
fmt.Printf("Got the instance from Cache: %v", instance)
return instance, found
}

// RemoveInstanceFromCache removes an instance from the cache
// This is used when an instance is deleted
// The instance is removed from the cache to avoid stale data
// The instance is removed from the cache only if the instance is found in the cache
func (c *ResourceCache) RemoveInstanceFromCache(key string) {
func (c *ResourceCache) DeleteInstanceFromCache(key string) {
c.mutex.Lock()
defer c.mutex.Unlock()
_, found := c.instances[key]
if found {
delete(c.instances, key)
//TODO:remove later: print cache found and deleted
fmt.Println("Cache found and deleted")
}
//TODO:remove later: print cache not found
fmt.Println("Cache not found to delete")

}

Expand Down Expand Up @@ -254,11 +261,13 @@ func (c *ResourceCache) UpdateInstanceInCache(guid string, name string, owner st
}
instance.Generation = generation
c.instances[owner] = instance
//TODO:remove later:print updated instance
fmt.Printf("Updated cache instance: %v\n", instance)
return true

}
//TODO:remove later: print all the instances in cache
fmt.Printf("Instances in cache: %v\n", c.instances)
//TODO:remove later: print cache not found
fmt.Println("Cache not found to update")
return false

}
Expand Down

0 comments on commit 061f668

Please sign in to comment.