Skip to content

Commit

Permalink
Moved keystore to the ehcache plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Feb 1, 2024
1 parent 651e1aa commit c700142
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 46 deletions.
25 changes: 0 additions & 25 deletions modules/roaringbitmap-keystore/build.gradle

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions plugins/cache-ehcache/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ opensearchplugin {
}

dependencies {
// for ehcache
api "org.ehcache:ehcache:${versions.ehcache}"

// For roaring bitmaps
api 'org.roaringbitmap:RoaringBitmap:0.9.49'
runtimeOnly 'org.roaringbitmap:shims:0.9.49'
}

thirdPartyAudit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.cache.keystore.RBMIntKeyLookupStore;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.cache.LoadAwareCacheLoader;
Expand All @@ -23,6 +24,7 @@
import org.opensearch.common.cache.store.config.StoreAwareCacheConfig;
import org.opensearch.common.cache.store.enums.CacheStoreType;
import org.opensearch.common.cache.store.listeners.StoreAwareCacheEventListener;
import org.opensearch.common.cache.tier.keystore.KeyLookupStore;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.metrics.CounterMetric;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -61,6 +63,8 @@
import static org.opensearch.cache.EhcacheSettings.DISK_WRITE_CONCURRENCY;
import static org.opensearch.cache.EhcacheSettings.DISK_WRITE_MAXIMUM_THREADS;
import static org.opensearch.cache.EhcacheSettings.DISK_WRITE_MINIMUM_THREADS;
import static org.opensearch.cache.EhcacheSettings.RBM_KEYSTORE_SIZE;
import static org.opensearch.cache.EhcacheSettings.USE_RBM_KEYSTORE;

/**
* This variant of disk cache uses Ehcache underneath.
Expand Down Expand Up @@ -111,6 +115,8 @@ public class EhcacheDiskCache<K, V> implements StoreAwareCache<K, V> {
*/
Map<K, CompletableFuture<Tuple<K, V>>> completableFutureMap = new ConcurrentHashMap<>();

KeyLookupStore<Integer> keystore = null;

private EhcacheDiskCache(Builder<K, V> builder) {
this.keyType = Objects.requireNonNull(builder.keyType, "Key type shouldn't be null");
this.valueType = Objects.requireNonNull(builder.valueType, "Value type shouldn't be null");
Expand All @@ -131,6 +137,10 @@ private EhcacheDiskCache(Builder<K, V> builder) {
this.eventListener = builder.getEventListener();
this.ehCacheEventListener = new EhCacheEventListener<K, V>(builder.getEventListener());
this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder);
if (USE_RBM_KEYSTORE.get(settings)) {
long keystoreSize = RBM_KEYSTORE_SIZE.get(settings).getBytes();
this.keystore = new RBMIntKeyLookupStore(keystoreSize);
}
}

private Cache<K, V> buildCache(Duration expireAfterAccess, Builder<K, V> builder) {
Expand Down Expand Up @@ -216,11 +226,13 @@ public V get(K key) {
if (key == null) {
throw new IllegalArgumentException("Key passed to ehcache disk cache was null.");
}
V value;
try {
value = cache.get(key);
} catch (CacheLoadingException ex) {
throw new OpenSearchException("Exception occurred while trying to fetch item from ehcache disk cache");
V value = null;
if (keystore == null || keystore.contains(key.hashCode()) || keystore.isFull()) {
try {
value = cache.get(key);
} catch (CacheLoadingException ex) {
throw new OpenSearchException("Exception occurred while trying to fetch item from ehcache disk cache");
}
}
if (value != null) {
eventListener.onHit(key, value, CacheStoreType.DISK);
Expand All @@ -239,6 +251,9 @@ public V get(K key) {
public void put(K key, V value) {
try {
cache.put(key, value);
if (keystore != null) {
keystore.add(key.hashCode());
}
} catch (CacheWritingException ex) {
throw new OpenSearchException("Exception occurred while put item to ehcache disk cache");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.opensearch.cache;

import org.opensearch.common.settings.Setting;
import org.opensearch.core.common.unit.ByteSizeValue;

/**
* Settings related to ehcache.
Expand Down Expand Up @@ -45,6 +46,20 @@ public class EhcacheSettings {
*/
public static final Setting<Integer> DISK_SEGMENTS = Setting.intSetting(SETTING_PREFIX + ".segments", 16, 1, 32);

/**
* Defines whether to use an in-memory keystore to check for probable presence of keys before having to go to disk.
*/
public static final Setting<Boolean> USE_RBM_KEYSTORE = Setting.boolSetting(SETTING_PREFIX + ".use_keystore", true);

/**
* Defines the max size of the RBM keystore if used (as a percentage of heap memory)
*/
public static final Setting<ByteSizeValue> RBM_KEYSTORE_SIZE = Setting.memorySizeSetting(
SETTING_PREFIX + ".keystore_size",
"0.05%",
Setting.Property.Dynamic
);

/**
* Default constructor. Added to fix javadocs.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.cache;
package org.opensearch.cache.keystore;

import org.opensearch.common.metrics.CounterMetric;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.cache;
package org.opensearch.cache.keystore;

import org.opensearch.common.cache.tier.keystore.KeyLookupStore;
import org.opensearch.common.metrics.CounterMetric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.cache;
package org.opensearch.cache.keystore;

import org.opensearch.common.Randomness;
import org.opensearch.common.metrics.CounterMetric;
Expand Down

0 comments on commit c700142

Please sign in to comment.