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



##########
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:
       Originally I was having trouble getting a good way of not paying the 
cost of weighing the key over and over but I solved that and added it back.




-- 
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