scwhittle commented on a change in pull request #16495:
URL: https://github.com/apache/beam/pull/16495#discussion_r787112419



##########
File path: 
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/Caches.java
##########
@@ -165,10 +141,57 @@ public int weigh(Object key, Object value) {
   }
 
   @VisibleForTesting
-  static Cache<Object, Object> forCache(
-      
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache<CompositeKey,
 Object>
-          cache) {
-    return new SubCache<>(cache, CompositeKeyPrefix.ROOT);
+  static <K, V> Cache<K, V> forMaximumBytes(long maximumBytes) {
+    // We specifically use Guava cache since it allows for recursive 
computeIfAbsent calls
+    // preventing deadlock from occurring when a loading function mutates the 
underlying cache
+    LongAdder weightInBytes = new LongAdder();
+    return new SubCache<>(
+        new ShrinkOnEviction(
+                CacheBuilder.newBuilder()
+                    .maximumWeight(maximumBytes >> WEIGHT_RATIO)
+                    .weigher(
+                        new Weigher<Object, WeightedValue<Object>>() {
+
+                          @Override
+                          public int weigh(Object key, WeightedValue<Object> 
value) {
+                            // Round up to the next closest multiple of 
WEIGHT_RATIO
+                            long size = ((value.getWeight() - 1) >> 
WEIGHT_RATIO) + 1;

Review comment:
       It seems like we used to have the cache weigh the key with 
Caches.weigh(key) but I no longer see that happening.  Was that intentional?
   
   If not, add a test where there are large keys to verify that they are 
weighed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to