forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tiered caching] Framework changes (opensearch-project#10753)
* [Tiered caching] Framework changes Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Added javadoc for new files/packages Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Added changelog Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Fixing javadoc warnings Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Addressing comments Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Addressing additional minor comments Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Moving non null check to builder for OS onHeapCache Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Adding package-info for new packages Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Removing service and adding different cache interfaces along with event listener support Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Fixing gradle missingDoc issue Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Changing listener logic, removing tiered cache integration with IRC Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Adding opensearch.internal tag for LoadAwareCacheLoader Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Fixing thread safety issue Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Remove compute function and event listener logic change for TieredCache Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Making Cache.compute function private Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Adding javadoc and more test for cache.put Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Adding write locks to refresh API as well Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Removing unwanted EventType class and refactoring one UT Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> * Removing TieredCache interface Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> --------- Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com> Signed-off-by: Sagar <99425694+sgup432@users.noreply.github.com>
- Loading branch information
Showing
19 changed files
with
1,567 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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> { | ||
boolean isLoaded(); | ||
} |
128 changes: 128 additions & 0 deletions
128
server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java
This file contains 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 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 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.