|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cache.common.tier; |
| 10 | + |
| 11 | +import org.opensearch.common.cache.stats.DefaultCacheStatsHolder; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.function.Consumer; |
| 16 | + |
| 17 | +/** |
| 18 | + * A tier-aware version of DefaultCacheStatsHolder. Overrides the incrementer functions, as we can't just add the on-heap |
| 19 | + * and disk stats to get a total for the cache as a whole. If the disk tier is present, the total hits, size, and entries |
| 20 | + * should be the sum of both tiers' values, but the total misses and evictions should be the disk tier's values. |
| 21 | + * When the disk tier isn't present, on-heap misses and evictions should contribute to the total. |
| 22 | + * |
| 23 | + * For example, if the heap tier has 5 misses and the disk tier has 4, the total cache has had 4 misses, not 9. |
| 24 | + * The same goes for evictions. Other stats values add normally. |
| 25 | + * |
| 26 | + * This means for misses and evictions, if we are incrementing for the on-heap tier and the disk tier is present, |
| 27 | + * we have to increment only the leaf nodes corresponding to the on-heap tier itself, and not its ancestors, |
| 28 | + * which correspond to totals including both tiers. If the disk tier is not present, we do increment the ancestor nodes. |
| 29 | + */ |
| 30 | +public class TieredSpilloverCacheStatsHolder extends DefaultCacheStatsHolder { |
| 31 | + |
| 32 | + /** Whether the disk cache is currently enabled. */ |
| 33 | + private boolean diskCacheEnabled; |
| 34 | + |
| 35 | + // Common values used for tier dimension |
| 36 | + |
| 37 | + /** The name for the tier dimension. */ |
| 38 | + public static final String TIER_DIMENSION_NAME = "tier"; |
| 39 | + |
| 40 | + /** Dimension value for on-heap cache, like OpenSearchOnHeapCache.*/ |
| 41 | + public static final String TIER_DIMENSION_VALUE_ON_HEAP = "on_heap"; |
| 42 | + |
| 43 | + /** Dimension value for on-disk cache, like EhcacheDiskCache. */ |
| 44 | + public static final String TIER_DIMENSION_VALUE_DISK = "disk"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor for the stats holder. |
| 48 | + * @param originalDimensionNames the original dimension names, not including TIER_DIMENSION_NAME |
| 49 | + * @param diskCacheEnabled whether the disk tier starts out enabled |
| 50 | + */ |
| 51 | + public TieredSpilloverCacheStatsHolder(List<String> originalDimensionNames, boolean diskCacheEnabled) { |
| 52 | + super( |
| 53 | + getDimensionNamesWithTier(originalDimensionNames), |
| 54 | + TieredSpilloverCache.TieredSpilloverCacheFactory.TIERED_SPILLOVER_CACHE_NAME |
| 55 | + ); |
| 56 | + this.diskCacheEnabled = diskCacheEnabled; |
| 57 | + } |
| 58 | + |
| 59 | + private static List<String> getDimensionNamesWithTier(List<String> dimensionNames) { |
| 60 | + List<String> dimensionNamesWithTier = new ArrayList<>(dimensionNames); |
| 61 | + dimensionNamesWithTier.add(TIER_DIMENSION_NAME); |
| 62 | + return dimensionNamesWithTier; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add tierValue to the end of a copy of the initial dimension values, so they can appropriately be used in this stats holder. |
| 67 | + */ |
| 68 | + List<String> getDimensionsWithTierValue(List<String> initialDimensions, String tierValue) { |
| 69 | + List<String> result = new ArrayList<>(initialDimensions); |
| 70 | + result.add(tierValue); |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private String validateTierDimensionValue(List<String> dimensionValues) { |
| 75 | + String tierDimensionValue = dimensionValues.get(dimensionValues.size() - 1); |
| 76 | + assert tierDimensionValue.equals(TIER_DIMENSION_VALUE_ON_HEAP) || tierDimensionValue.equals(TIER_DIMENSION_VALUE_DISK) |
| 77 | + : "Invalid tier dimension value"; |
| 78 | + return tierDimensionValue; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void incrementHits(List<String> dimensionValues) { |
| 83 | + validateTierDimensionValue(dimensionValues); |
| 84 | + // Hits from either tier should be included in the total values. |
| 85 | + super.incrementHits(dimensionValues); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void incrementMisses(List<String> dimensionValues) { |
| 90 | + final String tierValue = validateTierDimensionValue(dimensionValues); |
| 91 | + |
| 92 | + // If the disk tier is present, only misses from the disk tier should be included in total values. |
| 93 | + Consumer<Node> missIncrementer = (node) -> { |
| 94 | + if (tierValue.equals(TIER_DIMENSION_VALUE_ON_HEAP) && diskCacheEnabled) { |
| 95 | + // If on-heap tier, increment only the leaf node corresponding to the on heap values; not the total values in its parent |
| 96 | + // nodes |
| 97 | + if (node.isAtLowestLevel()) { |
| 98 | + node.incrementMisses(); |
| 99 | + } |
| 100 | + } else { |
| 101 | + // If disk tier, or on-heap tier with a disabled disk tier, increment the leaf node and its parents |
| 102 | + node.incrementMisses(); |
| 103 | + } |
| 104 | + }; |
| 105 | + internalIncrement(dimensionValues, missIncrementer, true); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void incrementEvictions(List<String> dimensionValues) { |
| 110 | + final String tierValue = validateTierDimensionValue(dimensionValues); |
| 111 | + |
| 112 | + // If the disk tier is present, only evictions from the disk tier should be included in total values. |
| 113 | + Consumer<DefaultCacheStatsHolder.Node> evictionsIncrementer = (node) -> { |
| 114 | + if (tierValue.equals(TIER_DIMENSION_VALUE_ON_HEAP) && diskCacheEnabled) { |
| 115 | + // If on-heap tier, increment only the leaf node corresponding to the on heap values; not the total values in its parent |
| 116 | + // nodes |
| 117 | + if (node.isAtLowestLevel()) { |
| 118 | + node.incrementEvictions(); |
| 119 | + } |
| 120 | + } else { |
| 121 | + // If disk tier, or on-heap tier with a disabled disk tier, increment the leaf node and its parents |
| 122 | + node.incrementEvictions(); |
| 123 | + } |
| 124 | + }; |
| 125 | + internalIncrement(dimensionValues, evictionsIncrementer, true); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void incrementSizeInBytes(List<String> dimensionValues, long amountBytes) { |
| 130 | + validateTierDimensionValue(dimensionValues); |
| 131 | + // Size from either tier should be included in the total values. |
| 132 | + super.incrementSizeInBytes(dimensionValues, amountBytes); |
| 133 | + } |
| 134 | + |
| 135 | + // For decrements, we should not create nodes if they are absent. This protects us from erroneously decrementing values for keys |
| 136 | + // which have been entirely deleted, for example in an async removal listener. |
| 137 | + @Override |
| 138 | + public void decrementSizeInBytes(List<String> dimensionValues, long amountBytes) { |
| 139 | + validateTierDimensionValue(dimensionValues); |
| 140 | + // Size from either tier should be included in the total values. |
| 141 | + super.decrementSizeInBytes(dimensionValues, amountBytes); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void incrementItems(List<String> dimensionValues) { |
| 146 | + validateTierDimensionValue(dimensionValues); |
| 147 | + // Entries from either tier should be included in the total values. |
| 148 | + super.incrementItems(dimensionValues); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void decrementItems(List<String> dimensionValues) { |
| 153 | + validateTierDimensionValue(dimensionValues); |
| 154 | + // Entries from either tier should be included in the total values. |
| 155 | + super.decrementItems(dimensionValues); |
| 156 | + } |
| 157 | + |
| 158 | + void setDiskCacheEnabled(boolean diskCacheEnabled) { |
| 159 | + this.diskCacheEnabled = diskCacheEnabled; |
| 160 | + } |
| 161 | +} |
0 commit comments