Skip to content

Commit

Permalink
Adding logic to update entries count after invalidateAll()
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com>
  • Loading branch information
sgup432 committed Mar 18, 2024
1 parent 0c6201f commit ead1c89
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.io.IOUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -62,7 +63,6 @@
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration;
import org.ehcache.spi.loaderwriter.CacheLoadingException;
import org.ehcache.spi.loaderwriter.CacheWritingException;
import org.opensearch.common.util.io.IOUtils;

import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_ALIAS_KEY;
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_EXPIRE_AFTER_ACCESS_KEY;
Expand Down Expand Up @@ -355,6 +355,7 @@ public void invalidate(K key) {
@Override
public void invalidateAll() {
cache.clear();
this.entries.dec(this.entries.count()); // reset to zero.
}

/**
Expand Down Expand Up @@ -394,8 +395,7 @@ public void close() {
} catch (CachePersistenceException e) {
throw new OpenSearchException("Exception occurred while destroying ehcache and associated data", e);
} catch (IOException e) {
logger.error(() -> new ParameterizedMessage("Failed to delete ehcache disk cache data under path: {}",
this.storagePath));
logger.error(() -> new ParameterizedMessage("Failed to delete ehcache disk cache data under path: {}", this.storagePath));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

package org.opensearch.cache.store.disk;

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakAction;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies;
import org.opensearch.cache.EhcacheDiskCacheSettings;
import org.opensearch.common.cache.CacheType;
import org.opensearch.common.cache.ICache;
Expand Down Expand Up @@ -42,7 +38,6 @@
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY;
import static org.hamcrest.CoreMatchers.instanceOf;

@ThreadLeakScope(ThreadLeakScope.Scope.TEST)
public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase {

private static final int CACHE_SIZE_IN_BYTES = 1024 * 101;
Expand Down Expand Up @@ -559,43 +554,7 @@ public void testInvalidateAll() throws Exception {
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
assertNull(ehcacheTest.get(entry.getKey()));
}
ehcacheTest.close();
}
}

public void testInvalidate() throws Exception {
Settings settings = Settings.builder().build();
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
try (NodeEnvironment env = newNodeEnvironment(settings)) {
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias("ehcacheTest")
.setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache")
.setIsEventListenerModeSync(true)
.setKeyType(String.class)
.setValueType(String.class)
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
.setSettings(settings)
.setExpireAfterAccess(TimeValue.MAX_VALUE)
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
.setRemovalListener(removalListener)
.build();
int randomKeys = randomIntBetween(10, 100);
Map<String, String> keyValueMap = new HashMap<>();
for (int i = 0; i < randomKeys; i++) {
keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
}
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
ehcacheTest.put(entry.getKey(), entry.getValue());
}
List<String> removedKeyList = new ArrayList<>();
for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
if (randomBoolean()) {
removedKeyList.add(entry.getKey());
ehcacheTest.invalidate(entry.getKey());
}
}
for (String removedKey: removedKeyList) {
assertNull(ehcacheTest.get(removedKey));
}
assertEquals(0, ehcacheTest.count());
ehcacheTest.close();
}
}
Expand Down

0 comments on commit ead1c89

Please sign in to comment.