-
Notifications
You must be signed in to change notification settings - Fork 16
Added support for async cache loading in EntityCache and EdsCacheClient #267
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,8 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import javax.annotation.Nonnull; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.hypertrace.core.grpcutils.context.ContextualKey; | ||
| import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; | ||
|
|
@@ -30,105 +30,103 @@ public class EntityCache { | |
| * Cache to cache the service fqn to service Entity mapping so that we don't look it up over and | ||
| * over. | ||
| */ | ||
| private final LoadingCache<Pair<String, String>, Optional<Entity>> fqnToServiceEntity = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .recordStats() | ||
| .build( | ||
| new CacheLoader<>() { | ||
| public Optional<Entity> load(@Nonnull Pair<String, String> pair) { | ||
| AttributeValue fqnAttribute = | ||
| AttributeValue.newBuilder() | ||
| .setValue(Value.newBuilder().setString(pair.getRight())) | ||
| .build(); | ||
|
|
||
| ByTypeAndIdentifyingAttributes request = | ||
| ByTypeAndIdentifyingAttributes.newBuilder() | ||
| .setEntityType(EntityType.SERVICE.name()) | ||
| .putIdentifyingAttributes( | ||
| EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), | ||
| fqnAttribute) | ||
| .build(); | ||
|
|
||
| return Optional.ofNullable( | ||
| edsClient.getByTypeAndIdentifyingAttributes(pair.getLeft(), request)); | ||
| } | ||
| }); | ||
|
|
||
| private final LoadingCache<Pair<String, String>, Optional<Entity>> fqnToServiceEntityCache; | ||
| /** | ||
| * Cache to cache the service name to a list of services mapping so that we don't look it up over | ||
| * and over. | ||
| */ | ||
| private final LoadingCache<Pair<String, String>, List<Entity>> nameToServiceEntities = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .build( | ||
| new CacheLoader<>() { | ||
| @Override | ||
| public List<Entity> load(@Nonnull Pair<String, String> pair) { | ||
| // Lookup by name first, to see if there are any services with that name. | ||
| return edsClient.getEntitiesByName( | ||
| pair.getLeft(), EntityType.SERVICE.name(), pair.getRight()); | ||
| } | ||
| }); | ||
| private final LoadingCache<Pair<String, String>, List<Entity>> nameToServiceEntitiesCache; | ||
|
|
||
| /** | ||
| * Cache of K8S namespaces Key: Customer Id, Namespace name Value: List of Namespace entity ids | ||
| */ | ||
| private final LoadingCache<Pair<String, String>, List<Entity>> namespaceCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .build( | ||
| new CacheLoader<>() { | ||
| @Override | ||
| public List<Entity> load(@Nonnull Pair<String, String> key) { | ||
| return edsClient.getEntitiesByName( | ||
| key.getLeft(), EntityType.K8S_NAMESPACE.name(), key.getRight()); | ||
| } | ||
| }); | ||
| private final LoadingCache<Pair<String, String>, List<Entity>> nameToNamespaceEntitiesCache; | ||
|
|
||
| /** | ||
| * Cache of Backend identifying attributes to Entity Key: Map of identifying attributes Value: | ||
| * Optional Backend entity | ||
| */ | ||
| private final LoadingCache<ContextualKey<Map<String, AttributeValue>>, Optional<Entity>> | ||
| backendIdAttrsToEntityCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .build(CacheLoader.from(this::loadBackendFromIdentifyingAttributes)); | ||
| backendIdAttrsToEntityCache; | ||
|
|
||
| public EntityCache(EdsClient edsClient) { | ||
| public EntityCache(EdsClient edsClient, Executor asyncCacheLoaderExecutor) { | ||
| this.edsClient = edsClient; | ||
| fqnToServiceEntityCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .refreshAfterWrite(4, TimeUnit.MINUTES) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .recordStats() | ||
| .build( | ||
| CacheLoader.asyncReloading( | ||
| CacheLoader.from(this::loadServiceFromFQN), asyncCacheLoaderExecutor)); | ||
|
|
||
| nameToServiceEntitiesCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .refreshAfterWrite(4, TimeUnit.MINUTES) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .build( | ||
| CacheLoader.asyncReloading( | ||
| CacheLoader.from( | ||
| tenantIdServiceNamePair -> | ||
| edsClient.getEntitiesByName( | ||
| tenantIdServiceNamePair.getLeft(), | ||
| EntityType.SERVICE.name(), | ||
| tenantIdServiceNamePair.getRight())), | ||
| asyncCacheLoaderExecutor)); | ||
|
|
||
| nameToNamespaceEntitiesCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .refreshAfterWrite(4, TimeUnit.MINUTES) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
| .build( | ||
| CacheLoader.asyncReloading( | ||
| CacheLoader.from( | ||
| key -> | ||
| edsClient.getEntitiesByName( | ||
| key.getLeft(), EntityType.K8S_NAMESPACE.name(), key.getRight())), | ||
| asyncCacheLoaderExecutor)); | ||
|
|
||
| backendIdAttrsToEntityCache = | ||
| CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .refreshAfterWrite(4, TimeUnit.MINUTES) | ||
| .expireAfterWrite(5, TimeUnit.MINUTES) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we missing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Fixed it. |
||
| .build( | ||
| CacheLoader.asyncReloading( | ||
| CacheLoader.from(this::loadBackendFromIdentifyingAttributes), | ||
| asyncCacheLoaderExecutor)); | ||
|
|
||
| PlatformMetricsRegistry.registerCache( | ||
| this.getClass().getName() + DOT + "fqnToServiceEntity", | ||
| fqnToServiceEntity, | ||
| this.getClass().getName() + DOT + "fqnToServiceEntityCache", | ||
| fqnToServiceEntityCache, | ||
| Collections.emptyMap()); | ||
| PlatformMetricsRegistry.registerCache( | ||
| this.getClass().getName() + DOT + "nameToServiceEntities", | ||
| nameToServiceEntities, | ||
| this.getClass().getName() + DOT + "nameToServiceEntitiesCache", | ||
| nameToServiceEntitiesCache, | ||
| Collections.emptyMap()); | ||
| PlatformMetricsRegistry.registerCache( | ||
| this.getClass().getName() + DOT + "namespaceCache", namespaceCache, Collections.emptyMap()); | ||
| this.getClass().getName() + DOT + "nameToNamespaceEntitiesCache", | ||
| nameToNamespaceEntitiesCache, | ||
| Collections.emptyMap()); | ||
| PlatformMetricsRegistry.registerCache( | ||
| this.getClass().getName() + DOT + "backendIdAttrsToEntityCache", | ||
| backendIdAttrsToEntityCache, | ||
| Collections.emptyMap()); | ||
| } | ||
|
|
||
| public LoadingCache<Pair<String, String>, Optional<Entity>> getFqnToServiceEntityCache() { | ||
| return fqnToServiceEntity; | ||
| return fqnToServiceEntityCache; | ||
| } | ||
|
|
||
| public LoadingCache<Pair<String, String>, List<Entity>> getNameToServiceEntitiesCache() { | ||
| return nameToServiceEntities; | ||
| return nameToServiceEntitiesCache; | ||
| } | ||
|
|
||
| public LoadingCache<Pair<String, String>, List<Entity>> getNameToNamespaceEntityIdCache() { | ||
| return namespaceCache; | ||
| return nameToNamespaceEntitiesCache; | ||
| } | ||
|
|
||
| public LoadingCache<ContextualKey<Map<String, AttributeValue>>, Optional<Entity>> | ||
|
|
@@ -147,4 +145,21 @@ protected Optional<Entity> loadBackendFromIdentifyingAttributes( | |
| edsClient.getByTypeAndIdentifyingAttributes( | ||
| key.getContext().getTenantId().orElseThrow(), request)); | ||
| } | ||
|
|
||
| protected Optional<Entity> loadServiceFromFQN(Pair<String, String> tenantIdFQNPair) { | ||
| AttributeValue fqnAttribute = | ||
| AttributeValue.newBuilder() | ||
| .setValue(Value.newBuilder().setString(tenantIdFQNPair.getRight())) | ||
| .build(); | ||
|
|
||
| ByTypeAndIdentifyingAttributes request = | ||
| ByTypeAndIdentifyingAttributes.newBuilder() | ||
| .setEntityType(EntityType.SERVICE.name()) | ||
| .putIdentifyingAttributes( | ||
| EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), fqnAttribute) | ||
| .build(); | ||
|
|
||
| return Optional.ofNullable( | ||
| edsClient.getByTypeAndIdentifyingAttributes(tenantIdFQNPair.getLeft(), request)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ability to configure these caches is an existing problem. Will address them in a different PR.