Skip to content

Commit 35a22c2

Browse files
author
Peter Alfonsi
committed
Integrated keystore
1 parent cf8a806 commit 35a22c2

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

server/src/main/java/org/opensearch/common/cache/tier/EhCacheDiskCachingTier.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.opensearch.common.cache.RemovalListener;
1515
import org.opensearch.common.cache.RemovalNotification;
1616
import org.opensearch.common.cache.RemovalReason;
17+
import org.opensearch.common.cache.tier.keystore.RBMIntKeyLookupStore;
1718
import org.opensearch.common.metrics.CounterMetric;
1819
import org.opensearch.common.settings.Setting;
1920
import org.opensearch.common.settings.Settings;
@@ -42,6 +43,7 @@
4243
import org.ehcache.event.EventType;
4344
import org.ehcache.expiry.ExpiryPolicy;
4445
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration;
46+
import org.opensearch.core.common.unit.ByteSizeValue;
4547

4648
/**
4749
* @param <K> The key type of cache entries
@@ -92,6 +94,7 @@ public class EhCacheDiskCachingTier<K, V> implements DiskCachingTier<K, V> {
9294
// Defines how many segments the disk cache is separated into. Higher number achieves greater concurrency but
9395
// will hold that many file pointers.
9496
public final Setting<Integer> DISK_SEGMENTS;
97+
private final RBMIntKeyLookupStore keystore;
9598

9699
private final Serializer<K, byte[]> keySerializer;
97100
private final Serializer<V, byte[]> valueSerializer;
@@ -124,6 +127,11 @@ private EhCacheDiskCachingTier(Builder<K, V> builder) {
124127
close();
125128
cacheManager = buildCacheManager();
126129
this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder);
130+
131+
// IndicesRequestCache gets 1%, of which we allocate 5% to the keystore = 0.05%
132+
// TODO: how do we change this automatically based on INDICES_CACHE_QUERY_SIZE setting?
133+
Setting<ByteSizeValue> keystoreSizeSetting = Setting.memorySizeSetting(builder.settingPrefix + ".tiered.disk.keystore_size", "0.05%");
134+
this.keystore = new RBMIntKeyLookupStore(keystoreSizeSetting.get(this.settings).getBytes());
127135
}
128136

129137
private PersistentCacheManager buildCacheManager() {
@@ -193,12 +201,16 @@ private CacheEventListenerConfigurationBuilder getListenerConfiguration(Builder<
193201

194202
@Override
195203
public V get(K key) {
196-
return valueSerializer.deserialize(cache.get(key));
204+
if (keystore.contains(key.hashCode())) { // Check in-memory store of key hashes to avoid unnecessary disk seek
205+
return valueSerializer.deserialize(cache.get(key));
206+
}
207+
return null;
197208
}
198209

199210
@Override
200211
public void put(K key, V value) {
201212
cache.put(key, valueSerializer.serialize(value));
213+
keystore.add(key.hashCode());
202214
}
203215

204216
@Override
@@ -211,6 +223,7 @@ public V computeIfAbsent(K key, TieredCacheLoader<K, V> loader) throws Exception
211223
public void invalidate(K key) {
212224
// There seems to be a thread leak issue while calling this and then closing cache.
213225
cache.remove(key);
226+
keystore.remove(key.hashCode());
214227
}
215228

216229
@Override
@@ -227,6 +240,7 @@ public void setRemovalListener(RemovalListener<K, V> removalListener) {
227240
@Override
228241
public void invalidateAll() {
229242
// Clear up files.
243+
keystore.clear();
230244
}
231245

232246
@Override

server/src/main/java/org/opensearch/common/cache/tier/keystore/KeyLookupStore.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public interface KeyLookupStore<T> {
4747
* @return true if the value was added, false if it wasn't added because of a
4848
* collision or if it was already present.
4949
*/
50-
boolean add(T value) throws Exception;
50+
boolean add(T value);
5151

5252
/**
5353
* Checks if the transformation of the value is in the keystore.
5454
* @param value The value to check.
5555
* @return true if the value was found, false otherwise. Due to collisions, false positives are
5656
* possible, but there should be no false negatives unless forceRemove() is called.
5757
*/
58-
boolean contains(T value) throws Exception;
58+
boolean contains(T value);
5959

6060
/**
6161
* Returns the transformed version of the input value, that would be used to stored it in the keystore.
@@ -72,7 +72,7 @@ public interface KeyLookupStore<T> {
7272
* @param value The value to attempt to remove.
7373
* @return true if the value was removed, false if it wasn't.
7474
*/
75-
boolean remove(T value) throws Exception;
75+
boolean remove(T value);
7676

7777
/**
7878
* Returns the number of distinct values stored in the internal data structure.
@@ -123,10 +123,10 @@ public interface KeyLookupStore<T> {
123123
* Also resets all stats related to adding.
124124
* @param newValues The keys that should be in the reset structure.
125125
*/
126-
void regenerateStore(T[] newValues) throws Exception;
126+
void regenerateStore(T[] newValues);
127127

128128
/**
129129
* Deletes all keys and resets all stats related to adding.
130130
*/
131-
void clear() throws Exception;
131+
void clear();
132132
}

server/src/main/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStore.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void handleCollisions(int transformedValue) {
120120
}
121121

122122
@Override
123-
public boolean add(Integer value) throws Exception {
123+
public boolean add(Integer value) {
124124
if (value == null) {
125125
return false;
126126
}
@@ -159,7 +159,7 @@ public boolean add(Integer value) throws Exception {
159159
}
160160

161161
@Override
162-
public boolean contains(Integer value) throws Exception {
162+
public boolean contains(Integer value) {
163163
if (value == null) {
164164
return false;
165165
}
@@ -185,10 +185,9 @@ public Integer getInternalRepresentation(Integer value) {
185185
* may cause undefined behavior, including future false negatives!!
186186
* @param value The value to attempt to remove.
187187
* @return true if the value was removed, false otherwise
188-
* @throws Exception
189188
*/
190189
@Override
191-
public boolean remove(Integer value) throws Exception {
190+
public boolean remove(Integer value) {
192191
if (value == null) {
193192
return false;
194193
}
@@ -290,7 +289,7 @@ public boolean isFull() {
290289
}
291290

292291
@Override
293-
public void regenerateStore(Integer[] newValues) throws Exception {
292+
public void regenerateStore(Integer[] newValues) {
294293
rbm.clear();
295294
collidedIntCounters = new HashMap<>();
296295
removalSets = new HashMap<>();
@@ -308,7 +307,7 @@ public void regenerateStore(Integer[] newValues) throws Exception {
308307
}
309308

310309
@Override
311-
public void clear() throws Exception {
310+
public void clear() {
312311
regenerateStore(new Integer[] {});
313312
}
314313

0 commit comments

Comments
 (0)