Skip to content

Commit

Permalink
Address Sagar's comment
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Jan 9, 2025
1 parent ff2f1e5 commit 224cc37
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ static class TieredSpilloverCacheSegment<K, V> implements ICache<K, V> {

private final TieredSpilloverCacheStatsHolder statsHolder;

private final long onHeapCacheMaxWeight;
private final long diskCacheMaxWeight;

/**
* This map is used to handle concurrent requests for same key in computeIfAbsent() to ensure we load the value
* only once.
Expand Down Expand Up @@ -218,6 +221,8 @@ static class TieredSpilloverCacheSegment<K, V> implements ICache<K, V> {
cacheListMap.put(diskCache, new TierInfo(isDiskCacheEnabled, TIER_DIMENSION_VALUE_DISK));
this.caches = Collections.synchronizedMap(cacheListMap);
this.policies = builder.policies; // Will never be null; builder initializes it to an empty list
this.onHeapCacheMaxWeight = onHeapCacheSizeInBytes;
this.diskCacheMaxWeight = diskCacheSizeInBytes;
}

// Package private for testing
Expand Down Expand Up @@ -526,12 +531,14 @@ void updateStatsOnPut(String destinationTierValue, ICacheKey<K> key, V value) {
statsHolder.incrementSizeInBytes(dimensionValues, weigher.applyAsLong(key, value));
}

@Override
public long getMaximumWeight() {
if (caches.get(diskCache).isEnabled()) {
return onHeapCache.getMaximumWeight() + diskCache.getMaximumWeight();
}
return onHeapCache.getMaximumWeight();
// pkg-private for testing
long getOnHeapCacheMaxWeight() {
return onHeapCacheMaxWeight;
}

// pkg-private for testing
long getDiskCacheMaxWeight() {
return diskCacheMaxWeight;
}

/**
Expand Down Expand Up @@ -700,11 +707,6 @@ long diskCacheCount() {
return diskCacheEntries;
}

@Override
public long getMaximumWeight() {
return tieredSpilloverCacheSegments[0].getMaximumWeight() * numberOfSegments;
}

/**
* ConcatenatedIterables which combines cache iterables and supports remove() functionality as well if underlying
* iterator supports it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public void close() {

}

@Override
public long getMaximumWeight() {
long getMaximumWeight() {
return maxSize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2293,16 +2293,12 @@ private void checkSegmentSizes(TieredSpilloverCache<String, String> cache, long
OpenSearchOnHeapCache<String, String> segmentHeapCache = (OpenSearchOnHeapCache<
String,
String>) cache.tieredSpilloverCacheSegments[0].getOnHeapCache();
assertEquals(expectedHeapSize / cache.getNumberOfSegments(), segmentHeapCache.getMaximumWeight());
TieredSpilloverCache.TieredSpilloverCacheSegment<String, String> segment = cache.tieredSpilloverCacheSegments[0];
assertEquals(expectedHeapSize / cache.getNumberOfSegments(), segment.getOnHeapCacheMaxWeight());

MockDiskCache<String, String> segmentDiskCache = (MockDiskCache<String, String>) cache.tieredSpilloverCacheSegments[0]
.getDiskCache();
assertEquals(expectedDiskSize / cache.getNumberOfSegments(), segmentDiskCache.getMaximumWeight());
assertEquals(
expectedHeapSize / cache.getNumberOfSegments() + expectedDiskSize / cache.getNumberOfSegments(),
cache.tieredSpilloverCacheSegments[0].getMaximumWeight()
);
assertEquals(expectedHeapSize + expectedDiskSize, cache.getMaximumWeight());
assertEquals(expectedDiskSize / cache.getNumberOfSegments(), segment.getDiskCacheMaxWeight());
}

private List<String> getMockDimensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,7 @@ private V deserializeValue(ByteArrayWrapper binary) {
}

// Pkg-private for testing.
@Override
public long getMaximumWeight() {
long getMaximumWeight() {
return maxWeightInBytes;
}

Expand Down
2 changes: 0 additions & 2 deletions server/src/main/java/org/opensearch/common/cache/ICache.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ default ImmutableCacheStatsHolder stats() {
// Return stats aggregated by the provided levels. If levels is null or an empty array, return total stats only.
ImmutableCacheStatsHolder stats(String[] levels);

long getMaximumWeight();

/**
* Factory to create objects.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public OpenSearchOnHeapCache(Builder<K, V> builder) {
this.weigher = builder.getWeigher();
}

@Override
public long getMaximumWeight() {
// pkg-private for testing
long getMaximumWeight() {
return this.maximumWeight;
}

Expand Down

0 comments on commit 224cc37

Please sign in to comment.