-
Notifications
You must be signed in to change notification settings - Fork 190
Add Model cache to cache model templates in memory #46
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
jmazanec15
merged 12 commits into
opensearch-project:feature/faiss-support
from
jmazanec15:model-cache
Jul 1, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bc0e49c
Add model cache to cache model templates
jmazanec15 a42b36f
Update model cache setting to long
jmazanec15 5f9f982
Add model cache setting to settings list
jmazanec15 f181ee6
Clean up code
jmazanec15 21d2f7b
Merge branch 'feature/faiss-support' into model-cache
jmazanec15 18dee98
Use longSetting
jmazanec15 712b213
Update default to 10 Mb
jmazanec15 539bf14
Remove private put method
jmazanec15 10e69de
Remove exception in delete
jmazanec15 37839d9
Update default to 50 Mb
jmazanec15 5ed5e9e
Remove index created check in get model
jmazanec15 919fd0e
Create index if it doesnt exist put
jmazanec15 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
134 changes: 134 additions & 0 deletions
134
src/main/java/org/opensearch/knn/indices/ModelCache.java
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 |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.knn.indices; | ||
|
|
||
| import com.google.common.cache.Cache; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.cluster.service.ClusterService; | ||
|
|
||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import static org.opensearch.knn.index.KNNSettings.MODEL_CACHE_SIZE_IN_BYTES_SETTING; | ||
|
|
||
|
|
||
| public final class ModelCache { | ||
|
|
||
| private static Logger logger = LogManager.getLogger(ModelCache.class); | ||
|
|
||
| private static ModelCache instance; | ||
| private static ModelDao modelDao; | ||
| private static ClusterService clusterService; | ||
|
|
||
| private Cache<String, byte[]> cache; | ||
| private long cacheSizeInBytes; | ||
|
|
||
| /** | ||
| * Get instance of cache | ||
| * | ||
| * @return singleton instance of cache | ||
| */ | ||
| public static synchronized ModelCache getInstance() { | ||
| if (instance == null) { | ||
| instance = new ModelCache(); | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| /** | ||
| * Initialize the cache | ||
| * | ||
| * @param modelDao modelDao used to read persistence layer for models | ||
| * @param clusterService used to update settings | ||
| */ | ||
| public static void initialize(ModelDao modelDao, ClusterService clusterService) { | ||
| ModelCache.modelDao = modelDao; | ||
| ModelCache.clusterService = clusterService; | ||
| } | ||
|
|
||
| /** | ||
| * Evict all entries and rebuild the graph | ||
| */ | ||
| public synchronized void rebuild() { | ||
| cache.invalidateAll(); | ||
| initCache(); | ||
| } | ||
|
|
||
| protected ModelCache() { | ||
| cacheSizeInBytes = MODEL_CACHE_SIZE_IN_BYTES_SETTING.get(clusterService.getSettings()); | ||
| clusterService.getClusterSettings().addSettingsUpdateConsumer(MODEL_CACHE_SIZE_IN_BYTES_SETTING, it -> { | ||
| cacheSizeInBytes = it; | ||
| rebuild(); | ||
| }); | ||
| initCache(); | ||
| } | ||
|
|
||
| private void initCache() { | ||
| CacheBuilder<String, byte[]> cacheBuilder = CacheBuilder.newBuilder() | ||
| .recordStats() | ||
| .concurrencyLevel(1) | ||
| .maximumWeight(cacheSizeInBytes) | ||
| .weigher((k, v) -> v.length); | ||
|
|
||
| cache = cacheBuilder.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the model from modelId | ||
| * | ||
| * @param modelId model identifier | ||
| * @return byte array representing model | ||
| */ | ||
| public byte[] get(String modelId) { | ||
| try { | ||
| return cache.get(modelId, () -> modelDao.get(modelId)); | ||
| } catch (ExecutionException ee) { | ||
| throw new IllegalStateException("Unable to retrieve model blob for \"" + modelId + "\": " + ee); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get total weight of cache | ||
| * | ||
| * @return total weight | ||
| */ | ||
| public long getTotalWeight() { | ||
| return cache.asMap().values().stream().map(bytes -> (long) bytes.length).reduce(0L, Long::sum); | ||
| } | ||
|
|
||
| /** | ||
| * Remove modelId from cache | ||
| * | ||
| * @param modelId to be removed | ||
| */ | ||
| public void remove(String modelId) { | ||
| cache.invalidate(modelId); | ||
| } | ||
|
|
||
| /** | ||
| * Check if modelId is in the cache | ||
| * | ||
| * @param modelId model id to be checked | ||
| * @return true if model id is in the cache; false otherwise | ||
| */ | ||
| public boolean contains(String modelId) { | ||
| return cache.asMap().containsKey(modelId); | ||
| } | ||
|
|
||
| /** | ||
| * Remove all elements from the cache | ||
| */ | ||
| public void removeAll() { | ||
| cache.invalidateAll(); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.