Skip to content

Commit

Permalink
Addressed Ankit's comments
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Apr 23, 2024
1 parent 8b63a72 commit c1d7bf9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
/**
* A dummy version of CacheStatsHolder, which cache implementations use when FeatureFlags.PLUGGABLE_CACHES is false.
* Returns all-zero stats when calling getImmutableCacheStatsHolder(). Always returns 0 for count().
* A singleton instance is used for memory purposes.
*/
public class NoopCacheStatsHolder implements CacheStatsHolder {
private static final NoopCacheStatsHolder singletonInstance = new NoopCacheStatsHolder();

public NoopCacheStatsHolder() {}
private NoopCacheStatsHolder() {}

public static NoopCacheStatsHolder getInstance() {
return singletonInstance;
}

@Override
public void incrementHits(List<String> dimensionValues) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public class OpenSearchOnHeapCache<K, V> implements ICache<K, V>, RemovalListene
private final RemovalListener<ICacheKey<K>, V> removalListener;
private final List<String> dimensionNames;
private final ToLongBiFunction<ICacheKey<K>, V> weigher;

private final Settings settings;
private final boolean useNoopStats;

public OpenSearchOnHeapCache(Builder<K, V> builder) {
CacheBuilder<ICacheKey<K>, V> cacheBuilder = CacheBuilder.<ICacheKey<K>, V>builder()
Expand All @@ -66,14 +65,14 @@ public OpenSearchOnHeapCache(Builder<K, V> builder) {
}
cache = cacheBuilder.build();
this.dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null");
if (useNoopStats(builder.getSettings())) {
this.cacheStatsHolder = new NoopCacheStatsHolder();
this.useNoopStats = builder.useNoopStats;
if (useNoopStats) {
this.cacheStatsHolder = NoopCacheStatsHolder.getInstance();
} else {
this.cacheStatsHolder = new DefaultCacheStatsHolder(dimensionNames);
}
this.removalListener = builder.getRemovalListener();
this.weigher = builder.getWeigher();
this.settings = builder.getSettings();
}

@Override
Expand Down Expand Up @@ -130,7 +129,7 @@ public Iterable<ICacheKey<K>> keys() {

@Override
public long count() {
if (useNoopStats(settings)) {
if (useNoopStats) {
return cache.count();
} else {
return cacheStatsHolder.count();
Expand Down Expand Up @@ -165,11 +164,6 @@ public void onRemoval(RemovalNotification<ICacheKey<K>, V> notification) {
}
}

private boolean useNoopStats(Settings settings) {
// Use noop stats when pluggable caching is off
return !FeatureFlags.PLUGGABLE_CACHE_SETTING.get(settings);
}

/**
* Factory to create OpenSearchOnheap cache.
*/
Expand All @@ -182,10 +176,10 @@ public <K, V> ICache<K, V> create(CacheConfig<K, V> config, CacheType cacheType,
Map<String, Setting<?>> settingList = OpenSearchOnHeapCacheSettings.getSettingListForCacheType(cacheType);
Settings settings = config.getSettings();
ICacheBuilder<K, V> builder = new Builder<K, V>().setDimensionNames(config.getDimensionNames())
.setUseNoopStats(useNoopStats(config.getSettings()))
.setMaximumWeightInBytes(((ByteSizeValue) settingList.get(MAXIMUM_SIZE_IN_BYTES_KEY).get(settings)).getBytes())
.setExpireAfterAccess(((TimeValue) settingList.get(EXPIRE_AFTER_ACCESS_KEY).get(settings)))
.setWeigher(config.getWeigher())
.setSettings(config.getSettings())
.setRemovalListener(config.getRemovalListener());
Setting<String> cacheSettingForCacheType = CacheSettings.CACHE_TYPE_STORE_NAME.getConcreteSettingForNamespace(
cacheType.getSettingPrefix()
Expand All @@ -203,6 +197,11 @@ public <K, V> ICache<K, V> create(CacheConfig<K, V> config, CacheType cacheType,
public String getCacheName() {
return NAME;
}

private boolean useNoopStats(Settings settings) {
// Use noop stats when pluggable caching is off
return !FeatureFlags.PLUGGABLE_CACHE_SETTING.get(settings);
}
}

/**
Expand All @@ -212,12 +211,18 @@ public String getCacheName() {
*/
public static class Builder<K, V> extends ICacheBuilder<K, V> {
private List<String> dimensionNames;
private boolean useNoopStats;

public Builder<K, V> setDimensionNames(List<String> dimensionNames) {
this.dimensionNames = dimensionNames;
return this;
}

public Builder<K, V> setUseNoopStats(boolean useNoopStats) {
this.useNoopStats = useNoopStats;
return this;
}

@Override
public ICache<K, V> build() {
return new OpenSearchOnHeapCache<K, V>(this);
Expand Down

0 comments on commit c1d7bf9

Please sign in to comment.