-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[Tiered caching] Framework changes #10753
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
sohami
merged 23 commits into
opensearch-project:main
from
sgup432:tiered_caching_framework
Jan 9, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b196615
[Tiered caching] Framework changes
sgup432 168076f
Added javadoc for new files/packages
sgup432 f98abc2
Merge branch 'main' into tiered_caching_framework
sgup432 8fed7c7
Added changelog
sgup432 5ace3a7
Fixing javadoc warnings
sgup432 2e9c478
Addressing comments
sgup432 c29d346
Addressing additional minor comments
sgup432 965cbef
Merge branch 'main' into tiered_caching_framework
sgup432 2e024a2
Moving non null check to builder for OS onHeapCache
sgup432 51d948f
Adding package-info for new packages
sgup432 64edcfb
Removing service and adding different cache interfaces along with eve…
sgup432 d8156af
Merge branch 'main' into tiered_caching_framework
sgup432 13a03a9
Fixing gradle missingDoc issue
sgup432 42a111d
Changing listener logic, removing tiered cache integration with IRC
sgup432 c9cf2c0
Adding opensearch.internal tag for LoadAwareCacheLoader
sgup432 7318252
Fixing thread safety issue
sgup432 79dd693
Remove compute function and event listener logic change for TieredCache
sgup432 7c10adf
Making Cache.compute function private
sgup432 1832df9
Adding javadoc and more test for cache.put
sgup432 49aab1f
Adding write locks to refresh API as well
sgup432 7e72d07
Merge branch 'main' into tiered_caching_framework
sgup432 fdda280
Removing unwanted EventType class and refactoring one UT
sgup432 9956abc
Removing TieredCache interface
sgup432 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
34 changes: 34 additions & 0 deletions
34
server/src/main/java/org/opensearch/common/cache/ICache.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,34 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.opensearch.common.cache; | ||
|
|
||
| /** | ||
| * Represents a cache interface. | ||
| * @param <K> Type of key. | ||
| * @param <V> Type of value. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| public interface ICache<K, V> { | ||
| V get(K key); | ||
|
|
||
| void put(K key, V value); | ||
|
|
||
| V computeIfAbsent(K key, LoadAwareCacheLoader<K, V> loader) throws Exception; | ||
|
|
||
| void invalidate(K key); | ||
|
|
||
| void invalidateAll(); | ||
|
|
||
| Iterable<K> keys(); | ||
|
|
||
| long count(); | ||
|
|
||
| void refresh(); | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
server/src/main/java/org/opensearch/common/cache/LoadAwareCacheLoader.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,20 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.opensearch.common.cache; | ||
|
|
||
| /** | ||
| * Extends a cache loader with awareness of whether the data is loaded or not. | ||
| * @param <K> Type of key. | ||
| * @param <V> Type of value. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public interface LoadAwareCacheLoader<K, V> extends CacheLoader<K, V> { | ||
sgup432 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| boolean isLoaded(); | ||
| } | ||
128 changes: 128 additions & 0 deletions
128
server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.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,128 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.opensearch.common.cache.store; | ||
|
|
||
| import org.opensearch.common.cache.Cache; | ||
| import org.opensearch.common.cache.CacheBuilder; | ||
| import org.opensearch.common.cache.LoadAwareCacheLoader; | ||
| import org.opensearch.common.cache.RemovalListener; | ||
| import org.opensearch.common.cache.RemovalNotification; | ||
| import org.opensearch.common.cache.store.builders.StoreAwareCacheBuilder; | ||
| import org.opensearch.common.cache.store.enums.CacheStoreType; | ||
| import org.opensearch.common.cache.store.listeners.StoreAwareCacheEventListener; | ||
|
|
||
| /** | ||
| * This variant of on-heap cache uses OpenSearch custom cache implementation. | ||
| * @param <K> Type of key. | ||
| * @param <V> Type of value. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| public class OpenSearchOnHeapCache<K, V> implements StoreAwareCache<K, V>, RemovalListener<K, V> { | ||
|
|
||
| private final Cache<K, V> cache; | ||
|
|
||
| private final StoreAwareCacheEventListener<K, V> eventListener; | ||
|
|
||
| public OpenSearchOnHeapCache(Builder<K, V> builder) { | ||
| CacheBuilder<K, V> cacheBuilder = CacheBuilder.<K, V>builder() | ||
| .setMaximumWeight(builder.getMaxWeightInBytes()) | ||
| .weigher(builder.getWeigher()) | ||
| .removalListener(this); | ||
| if (builder.getExpireAfterAcess() != null) { | ||
| cacheBuilder.setExpireAfterAccess(builder.getExpireAfterAcess()); | ||
| } | ||
| cache = cacheBuilder.build(); | ||
| this.eventListener = builder.getEventListener(); | ||
| } | ||
|
|
||
| @Override | ||
| public V get(K key) { | ||
| V value = cache.get(key); | ||
| if (value != null) { | ||
| eventListener.onHit(key, value, CacheStoreType.ON_HEAP); | ||
| } else { | ||
| eventListener.onMiss(key, CacheStoreType.ON_HEAP); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public void put(K key, V value) { | ||
| cache.put(key, value); | ||
| eventListener.onCached(key, value, CacheStoreType.ON_HEAP); | ||
| } | ||
|
|
||
| @Override | ||
| public V computeIfAbsent(K key, LoadAwareCacheLoader<K, V> loader) throws Exception { | ||
| V value = cache.computeIfAbsent(key, key1 -> loader.load(key)); | ||
| if (!loader.isLoaded()) { | ||
| eventListener.onHit(key, value, CacheStoreType.ON_HEAP); | ||
| } else { | ||
| eventListener.onMiss(key, CacheStoreType.ON_HEAP); | ||
| eventListener.onCached(key, value, CacheStoreType.ON_HEAP); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate(K key) { | ||
| cache.invalidate(key); | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidateAll() { | ||
| cache.invalidateAll(); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<K> keys() { | ||
| return cache.keys(); | ||
| } | ||
|
|
||
| @Override | ||
| public long count() { | ||
| return cache.count(); | ||
| } | ||
|
|
||
| @Override | ||
| public void refresh() { | ||
| cache.refresh(); | ||
| } | ||
|
|
||
| @Override | ||
| public CacheStoreType getTierType() { | ||
| return CacheStoreType.ON_HEAP; | ||
| } | ||
|
|
||
| @Override | ||
| public void onRemoval(RemovalNotification<K, V> notification) { | ||
| eventListener.onRemoval( | ||
| new StoreAwareCacheRemovalNotification<>( | ||
| notification.getKey(), | ||
| notification.getValue(), | ||
| notification.getRemovalReason(), | ||
| CacheStoreType.ON_HEAP | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Builder object | ||
| * @param <K> Type of key | ||
| * @param <V> Type of value | ||
| */ | ||
| public static class Builder<K, V> extends StoreAwareCacheBuilder<K, V> { | ||
|
|
||
| @Override | ||
| public StoreAwareCache<K, V> build() { | ||
| return new OpenSearchOnHeapCache<K, V>(this); | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
server/src/main/java/org/opensearch/common/cache/store/StoreAwareCache.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,23 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.opensearch.common.cache.store; | ||
|
|
||
| import org.opensearch.common.cache.ICache; | ||
| import org.opensearch.common.cache.store.enums.CacheStoreType; | ||
|
|
||
| /** | ||
| * Represents a cache with a specific type of store like onHeap, disk etc. | ||
| * @param <K> Type of key. | ||
| * @param <V> Type of value. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| public interface StoreAwareCache<K, V> extends ICache<K, V> { | ||
| CacheStoreType getTierType(); | ||
| } |
33 changes: 33 additions & 0 deletions
33
...r/src/main/java/org/opensearch/common/cache/store/StoreAwareCacheRemovalNotification.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,33 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.opensearch.common.cache.store; | ||
|
|
||
| import org.opensearch.common.cache.RemovalNotification; | ||
| import org.opensearch.common.cache.RemovalReason; | ||
| import org.opensearch.common.cache.store.enums.CacheStoreType; | ||
|
|
||
| /** | ||
| * Removal notification for store aware cache. | ||
| * @param <K> Type of key. | ||
| * @param <V> Type of value. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public class StoreAwareCacheRemovalNotification<K, V> extends RemovalNotification<K, V> { | ||
| private final CacheStoreType cacheStoreType; | ||
|
|
||
| public StoreAwareCacheRemovalNotification(K key, V value, RemovalReason removalReason, CacheStoreType cacheStoreType) { | ||
| super(key, value, removalReason); | ||
| this.cacheStoreType = cacheStoreType; | ||
| } | ||
|
|
||
| public CacheStoreType getCacheStoreType() { | ||
| return cacheStoreType; | ||
| } | ||
| } |
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.