Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-Memory Cache for CF Resources (Service Instances) #59

Open
wants to merge 57 commits into
base: main
Choose a base branch
from

Conversation

santiago-ventura
Copy link
Contributor

Summary

We want to introduce a mechanism for batching and caching CF resources. By storing the CF resources in memory, we aim to reduce the number of requests to the CF API and avoid reaching the rate limit.

Approach

  • The cache feature is optional and can be enabled via the environment variable RESOURCE_CACHE_ENABLED, which can have values of true or false.

  • Another environment variable, CACHE_TIMEOUT, is introduced to control the refresh of the cached resources, based on a lastCacheTime attribute of the Cache instance.

  • At the start of the operator, an instance of a Config object is created. This config object has two attributes with values of the environment variables described above.

  • The first time the' spaceClient' is created, an instance of the Cache object is created and populated.

  • The Cache instance will be refreshed every time the cache time-out is reached.

  • This PR has been tested manually in one of our Kubernetes Clusters.
  • A second PR will be following this one; implementing the solution for the CF resources Space, and ServiceBinding. A third PR will have the documentation and the integration, and unit test.

internal/cf/instance.go Outdated Show resolved Hide resolved
serviceBinding.SetReadyCondition(cfv1alpha1.ConditionUnknown, string(cfbinding.State), cfbinding.StateDescription)
return ctrl.Result{Requeue: true}, nil
//return the reconcile function to not reconcile and error message
return ctrl.Result{}, fmt.Errorf("orphaned instance is not ready to be adopted")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be orphaned binding?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right


// TODO: document which keys are used for the maps
// TODO: check if the keys for resources like spaces are unique across all CF organziations
// Scenario:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keys used for the maps are the label owner which is constructed this way, "service-operator.cf.cs.sap.com/owner": "K8s UID".
This applies to the bindings, spaces, and instances. Not sure if that answers these two TODOs.

// getCachedInstances retrieves all instances from the cache
func (c *resourceCache) getCachedInstances() map[string]*facade.Instance {
return c.instances
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if another reconciliation tries to modify the cache by adding or deleting instances we can have a race condition. I would add an RLock() so we are sure that it is safe to access when retrieving instances.

I think the same about the spaces and bindings.

@@ -210,5 +254,257 @@ func NewSpaceHealthChecker(spaceGuid string, url string, username string, passwo
}
}

if config.IsResourceCacheEnabled && client.resourceCache == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheBigElmo
It assigns the resourceCache created by the organization unit client and space client and will use the spaceCache populated by the organization unit client in the cf/health.go.

Currently the health.go uses the spaceClient to call the CF API. Is this OK to use the cache populated by the organization client?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants