diff --git a/server/src/main/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStore.java b/server/src/main/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStore.java index 4e56e36c130c0..e690deae7b521 100644 --- a/server/src/main/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStore.java +++ b/server/src/main/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStore.java @@ -82,7 +82,7 @@ public int getValue() { protected final Lock readLock = lock.readLock(); protected final Lock writeLock = lock.writeLock(); private long mostRecentByteEstimate; - private final int REFRESH_SIZE_EST_INTERVAL = 10000; + protected final int REFRESH_SIZE_EST_INTERVAL = 10000; // Refresh size estimate every X new elements. Refreshes use the RBM's internal size estimator, which takes ~0.01 ms, // so we don't want to do it on every get(), and it doesn't matter much if there are +- 10000 keys in this store // in terms of storage impact @@ -281,7 +281,14 @@ public boolean isCollision(Integer value1, Integer value2) { } static double getRBMSizeMultiplier(int numEntries, int modulo) { - double x = Math.log10((double) numEntries / modulo); + double effectiveModulo = (double) modulo / 2; + /* This model was created when we used % operator to calculate modulo. This has range (-modulo, modulo). + Now we have optimized to use a bitmask, which has range [0, modulo). So the number of possible values stored + is halved. */ + if (modulo == 0) { + effectiveModulo = Math.pow(2, 32); + } + double x = Math.log10((double) numEntries / effectiveModulo); if (x < -5) { return 7.0; } diff --git a/server/src/test/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStoreTests.java b/server/src/test/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStoreTests.java index c0d16dfbc5744..d9b1ece1310ca 100644 --- a/server/src/test/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStoreTests.java +++ b/server/src/test/java/org/opensearch/common/cache/tier/keystore/RBMIntKeyLookupStoreTests.java @@ -82,9 +82,13 @@ public void testTransformationLogic() throws Exception { public void testContains() throws Exception { RBMIntKeyLookupStore kls = new RBMIntKeyLookupStore(RBMIntKeyLookupStore.KeystoreModuloValue.TWO_TO_TWENTY_NINE, 0L); - for (int i = 0; i < 2000; i++) { + RBMIntKeyLookupStore noModuloKls = new RBMIntKeyLookupStore(RBMIntKeyLookupStore.KeystoreModuloValue.NONE, 0L); + for (int i = 0; i < kls.REFRESH_SIZE_EST_INTERVAL + 1000; i++) { + // set upper bound > number of elements to trigger a size check, ensuring we test that too kls.add(i); assertTrue(kls.contains(i)); + noModuloKls.add(i); + assertTrue(noModuloKls.contains(i)); } }