Skip to content

Commit

Permalink
Fixing gradle build issue
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 5, 2024
1 parent eb31e01 commit 1cafda4
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected Settings nodeSettings(int nodeOrdinal) {
TieredSpilloverCacheSettings.TIERED_SPILLOVER_DISK_STORE_NAME.getConcreteSettingForNamespace(
CacheType.INDICES_REQUEST_CACHE.getSettingPrefix()
).getKey(),
MockOnDiskCache.MockDiskCacheFactory.NAME
MockDiskCache.MockDiskCacheFactory.NAME
)
.build();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public MockDiskCachePlugin() {}

@Override
public Map<String, ICache.Factory> getCacheFactoryMap() {
return Map.of(MockOnDiskCache.MockDiskCacheFactory.NAME, new MockOnDiskCache.MockDiskCacheFactory(0, 1000));
return Map.of(MockDiskCache.MockDiskCacheFactory.NAME, new MockDiskCache.MockDiskCacheFactory(0, 1000));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.cache.common.tier;

import org.opensearch.common.cache.CacheType;
import org.opensearch.common.cache.ICache;
import org.opensearch.common.cache.LoadAwareCacheLoader;
import org.opensearch.common.cache.store.builders.ICacheBuilder;
import org.opensearch.common.cache.store.config.CacheConfig;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MockDiskCache<K, V> implements ICache<K, V> {

Map<K, V> cache;
int maxSize;
long delay;

public MockDiskCache(int maxSize, long delay) {
this.maxSize = maxSize;
this.delay = delay;
this.cache = new ConcurrentHashMap<K, V>();
}

@Override
public V get(K key) {
V value = cache.get(key);
return value;
}

@Override
public void put(K key, V value) {
if (this.cache.size() >= maxSize) { // For simplification
return;
}
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
this.cache.put(key, value);
}

@Override
public V computeIfAbsent(K key, LoadAwareCacheLoader<K, V> loader) {
V value = cache.computeIfAbsent(key, key1 -> {
try {
return loader.load(key);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return value;
}

@Override
public void invalidate(K key) {
this.cache.remove(key);
}

@Override
public void invalidateAll() {
this.cache.clear();
}

@Override
public Iterable<K> keys() {
return this.cache.keySet();
}

@Override
public long count() {
return this.cache.size();
}

@Override
public void refresh() {}

@Override
public void close() {

}

public static class MockDiskCacheFactory implements Factory {

public static final String NAME = "mockDiskCache";
final long delay;
final int maxSize;

public MockDiskCacheFactory(long delay, int maxSize) {
this.delay = delay;
this.maxSize = maxSize;
}

@Override
public <K, V> ICache<K, V> create(CacheConfig<K, V> config, CacheType cacheType, Map<String, Factory> cacheFactories) {
return new Builder<K, V>().setMaxSize(maxSize).setDeliberateDelay(delay).build();
}

@Override
public String getCacheName() {
return NAME;
}
}

public static class Builder<K, V> extends ICacheBuilder<K, V> {

int maxSize;
long delay;

@Override
public ICache<K, V> build() {
return new MockDiskCache<K, V>(this.maxSize, this.delay);
}

public Builder<K, V> setMaxSize(int maxSize) {
this.maxSize = maxSize;
return this;
}

public Builder<K, V> setDeliberateDelay(long millis) {
this.delay = millis;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.opensearch.common.cache.RemovalListener;
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.store.OpenSearchOnHeapCache;
import org.opensearch.common.cache.store.builders.ICacheBuilder;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings;
import org.opensearch.common.metrics.CounterMetric;
Expand All @@ -25,7 +24,6 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Phaser;
Expand Down Expand Up @@ -105,7 +103,7 @@ public void testComputeIfAbsentWithFactoryBasedCacheCreation() throws Exception
TieredSpilloverCacheSettings.TIERED_SPILLOVER_DISK_STORE_NAME.getConcreteSettingForNamespace(
CacheType.INDICES_REQUEST_CACHE.getSettingPrefix()
).getKey(),
MockOnDiskCache.MockDiskCacheFactory.NAME
MockDiskCache.MockDiskCacheFactory.NAME
)
.put(
OpenSearchOnHeapCacheSettings.getSettingListForCacheType(CacheType.INDICES_REQUEST_CACHE)
Expand All @@ -126,8 +124,8 @@ public void testComputeIfAbsentWithFactoryBasedCacheCreation() throws Exception
Map.of(
OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory.NAME,
new OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory(),
MockOnDiskCache.MockDiskCacheFactory.NAME,
new MockOnDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
MockDiskCache.MockDiskCacheFactory.NAME,
new MockDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
)
);

Expand Down Expand Up @@ -164,7 +162,7 @@ public void testWithFactoryCreationWithOnHeapCacheNotPresent() {
TieredSpilloverCacheSettings.TIERED_SPILLOVER_DISK_STORE_NAME.getConcreteSettingForNamespace(
CacheType.INDICES_REQUEST_CACHE.getSettingPrefix()
).getKey(),
MockOnDiskCache.MockDiskCacheFactory.NAME
MockDiskCache.MockDiskCacheFactory.NAME
)
.put(
OpenSearchOnHeapCacheSettings.getSettingListForCacheType(CacheType.INDICES_REQUEST_CACHE)
Expand All @@ -187,8 +185,8 @@ public void testWithFactoryCreationWithOnHeapCacheNotPresent() {
Map.of(
OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory.NAME,
new OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory(),
MockOnDiskCache.MockDiskCacheFactory.NAME,
new MockOnDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
MockDiskCache.MockDiskCacheFactory.NAME,
new MockDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
)
)
);
Expand Down Expand Up @@ -232,8 +230,8 @@ public void testWithFactoryCreationWithDiskCacheNotPresent() {
Map.of(
OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory.NAME,
new OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory(),
MockOnDiskCache.MockDiskCacheFactory.NAME,
new MockOnDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
MockDiskCache.MockDiskCacheFactory.NAME,
new MockDiskCache.MockDiskCacheFactory(0, randomIntBetween(100, 300))
)
)
);
Expand Down Expand Up @@ -266,7 +264,7 @@ public void testComputeIfAbsentWithEvictionsFromOnHeapCache() throws Exception {
)
.build();

ICache.Factory mockDiskCacheFactory = new MockOnDiskCache.MockDiskCacheFactory(0, diskCacheSize);
ICache.Factory mockDiskCacheFactory = new MockDiskCache.MockDiskCacheFactory(0, diskCacheSize);

TieredSpilloverCache<String, String> tieredSpilloverCache = new TieredSpilloverCache.Builder<String, String>()
.setOnHeapCacheFactory(onHeapCacheFactory)
Expand Down Expand Up @@ -741,7 +739,7 @@ public void testConcurrencyForEvictionFlow() throws Exception {
MockCacheRemovalListener<String, String> removalListener = new MockCacheRemovalListener<>();

ICache.Factory onHeapCacheFactory = new OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory();
ICache.Factory diskCacheFactory = new MockOnDiskCache.MockDiskCacheFactory(500, diskCacheSize);
ICache.Factory diskCacheFactory = new MockDiskCache.MockDiskCacheFactory(500, diskCacheSize);
CacheConfig<String, String> cacheConfig = new CacheConfig.Builder<String, String>().setKeyType(String.class)
.setKeyType(String.class)
.setWeigher((k, v) -> 150)
Expand Down Expand Up @@ -861,7 +859,7 @@ private TieredSpilloverCache<String, String> intializeTieredSpilloverCache(
.setSettings(settings)
.build();

ICache.Factory mockDiskCacheFactory = new MockOnDiskCache.MockDiskCacheFactory(diskDeliberateDelay, diskCacheSize);
ICache.Factory mockDiskCacheFactory = new MockDiskCache.MockDiskCacheFactory(diskDeliberateDelay, diskCacheSize);

return new TieredSpilloverCache.Builder<String, String>().setCacheType(CacheType.INDICES_REQUEST_CACHE)
.setRemovalListener(removalListener)
Expand All @@ -871,118 +869,3 @@ private TieredSpilloverCache<String, String> intializeTieredSpilloverCache(
.build();
}
}

class MockOnDiskCache<K, V> implements ICache<K, V> {

Map<K, V> cache;
int maxSize;
long delay;

MockOnDiskCache(int maxSize, long delay) {
this.maxSize = maxSize;
this.delay = delay;
this.cache = new ConcurrentHashMap<K, V>();
}

@Override
public V get(K key) {
V value = cache.get(key);
return value;
}

@Override
public void put(K key, V value) {
if (this.cache.size() >= maxSize) { // For simplification
return;
}
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
this.cache.put(key, value);
}

@Override
public V computeIfAbsent(K key, LoadAwareCacheLoader<K, V> loader) {
V value = cache.computeIfAbsent(key, key1 -> {
try {
return loader.load(key);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return value;
}

@Override
public void invalidate(K key) {
this.cache.remove(key);
}

@Override
public void invalidateAll() {
this.cache.clear();
}

@Override
public Iterable<K> keys() {
return this.cache.keySet();
}

@Override
public long count() {
return this.cache.size();
}

@Override
public void refresh() {}

@Override
public void close() {

}

public static class MockDiskCacheFactory implements Factory {

static final String NAME = "mockDiskCache";
final long delay;
final int maxSize;

MockDiskCacheFactory(long delay, int maxSize) {
this.delay = delay;
this.maxSize = maxSize;
}

@Override
public <K, V> ICache<K, V> create(CacheConfig<K, V> config, CacheType cacheType, Map<String, Factory> cacheFactories) {
return new Builder<K, V>().setMaxSize(maxSize).setDeliberateDelay(delay).build();
}

@Override
public String getCacheName() {
return NAME;
}
}

public static class Builder<K, V> extends ICacheBuilder<K, V> {

int maxSize;
long delay;

@Override
public ICache<K, V> build() {
return new MockOnDiskCache<K, V>(this.maxSize, this.delay);
}

public Builder<K, V> setMaxSize(int maxSize) {
this.maxSize = maxSize;
return this;
}

public Builder<K, V> setDeliberateDelay(long millis) {
this.delay = millis;
return this;
}
}
}

0 comments on commit 1cafda4

Please sign in to comment.