|
17 | 17 | */ |
18 | 18 | package org.apache.hadoop.hbase.regionserver; |
19 | 19 |
|
20 | | -import com.github.benmanes.caffeine.cache.Cache; |
21 | | -import com.github.benmanes.caffeine.cache.Caffeine; |
22 | | -import com.github.benmanes.caffeine.cache.Policy; |
23 | | -import com.github.benmanes.caffeine.cache.RemovalCause; |
24 | | -import com.github.benmanes.caffeine.cache.RemovalListener; |
25 | | -import java.util.Optional; |
26 | | -import java.util.OptionalLong; |
27 | | -import java.util.concurrent.atomic.LongAdder; |
28 | | -import org.checkerframework.checker.nullness.qual.NonNull; |
29 | | - |
30 | 20 | /** |
31 | | - * A cache that stores rows retrieved by Get operations, using Caffeine as the underlying cache |
32 | | - * implementation. |
| 21 | + * Interface for caching rows retrieved by Get operations. |
33 | 22 | */ |
34 | 23 | @org.apache.yetus.audience.InterfaceAudience.Private |
35 | | -public class RowCache { |
36 | | - private final class EvictionListener |
37 | | - implements RemovalListener<@NonNull RowCacheKey, @NonNull RowCells> { |
38 | | - @Override |
39 | | - public void onRemoval(RowCacheKey key, RowCells value, @NonNull RemovalCause cause) { |
40 | | - evictedRowCount.increment(); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - private final Cache<@NonNull RowCacheKey, RowCells> cache; |
45 | | - |
46 | | - // Cache.stats() does not provide eviction count for entries, so we maintain our own counter. |
47 | | - private final LongAdder evictedRowCount = new LongAdder(); |
48 | | - |
49 | | - RowCache(long maxSizeBytes) { |
50 | | - if (maxSizeBytes <= 0) { |
51 | | - cache = Caffeine.newBuilder().maximumSize(0).build(); |
52 | | - return; |
53 | | - } |
54 | | - |
55 | | - cache = |
56 | | - Caffeine.newBuilder().maximumWeight(maxSizeBytes).removalListener(new EvictionListener()) |
57 | | - .weigher((RowCacheKey key, |
58 | | - RowCells value) -> (int) Math.min(key.heapSize() + value.heapSize(), Integer.MAX_VALUE)) |
59 | | - .recordStats().build(); |
60 | | - } |
61 | | - |
62 | | - void cacheRow(RowCacheKey key, RowCells value) { |
63 | | - cache.put(key, value); |
64 | | - } |
65 | | - |
66 | | - public RowCells getRow(RowCacheKey key, boolean caching) { |
67 | | - if (!caching) { |
68 | | - return null; |
69 | | - } |
70 | | - |
71 | | - return cache.getIfPresent(key); |
72 | | - } |
| 24 | +public interface RowCache { |
| 25 | + /** |
| 26 | + * Cache the specified row. |
| 27 | + * @param key the key of the row to cache |
| 28 | + * @param value the cells of the row to cache |
| 29 | + */ |
| 30 | + void cacheRow(RowCacheKey key, RowCells value); |
73 | 31 |
|
74 | | - void evictRow(RowCacheKey key) { |
75 | | - cache.asMap().remove(key); |
76 | | - } |
| 32 | + /** |
| 33 | + * Evict the specified row. |
| 34 | + * @param key the key of the row to evict |
| 35 | + */ |
| 36 | + void evictRow(RowCacheKey key); |
77 | 37 |
|
78 | 38 | /** |
79 | 39 | * Evict all rows belonging to the specified region. This is heavy operation as it iterates the |
80 | 40 | * entire RowCache key set. |
81 | 41 | * @param region the region whose rows should be evicted |
82 | 42 | */ |
83 | | - void evictRowsByRegion(HRegion region) { |
84 | | - cache.asMap().keySet().removeIf(key -> key.isSameRegion(region)); |
85 | | - } |
| 43 | + void evictRowsByRegion(HRegion region); |
86 | 44 |
|
87 | | - public long getHitCount() { |
88 | | - return cache.stats().hitCount(); |
89 | | - } |
| 45 | + /** |
| 46 | + * Get the number of rows in the cache. |
| 47 | + * @return the number of rows in the cache |
| 48 | + */ |
| 49 | + long getCount(); |
90 | 50 |
|
91 | | - public long getMissCount() { |
92 | | - return cache.stats().missCount(); |
93 | | - } |
| 51 | + /** |
| 52 | + * Get the number of rows evicted from the cache. |
| 53 | + * @return the number of rows evicted from the cache |
| 54 | + */ |
| 55 | + long getEvictedRowCount(); |
94 | 56 |
|
95 | | - public long getEvictedRowCount() { |
96 | | - return evictedRowCount.sum(); |
97 | | - } |
| 57 | + /** |
| 58 | + * Get the hit count. |
| 59 | + * @return the hit count |
| 60 | + */ |
| 61 | + long getHitCount(); |
98 | 62 |
|
99 | | - public long getSize() { |
100 | | - Optional<OptionalLong> result = cache.policy().eviction().map(Policy.Eviction::weightedSize); |
101 | | - return result.orElse(OptionalLong.of(-1L)).orElse(-1L); |
102 | | - } |
| 63 | + /** |
| 64 | + * Get the maximum size of the cache in bytes. |
| 65 | + * @return the maximum size of the cache in bytes |
| 66 | + */ |
| 67 | + long getMaxSize(); |
103 | 68 |
|
104 | | - public long getMaxSize() { |
105 | | - Optional<Long> result = cache.policy().eviction().map(Policy.Eviction::getMaximum); |
106 | | - return result.orElse(-1L); |
107 | | - } |
| 69 | + /** |
| 70 | + * Get the miss count. |
| 71 | + * @return the miss count |
| 72 | + */ |
| 73 | + long getMissCount(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the specified row from the cache. |
| 77 | + * @param key the key of the row to get |
| 78 | + * @param caching whether caching is enabled for this request |
| 79 | + * @return the cells of the row, or null if not found or caching is disabled |
| 80 | + */ |
| 81 | + RowCells getRow(RowCacheKey key, boolean caching); |
108 | 82 |
|
109 | | - public long getCount() { |
110 | | - return cache.estimatedSize(); |
111 | | - } |
| 83 | + /** |
| 84 | + * Get the current size of the cache in bytes. |
| 85 | + * @return the current size of the cache in bytes |
| 86 | + */ |
| 87 | + long getSize(); |
112 | 88 | } |
0 commit comments