ben-manes commented on a change in pull request #16130:
URL: https://github.com/apache/beam/pull/16130#discussion_r765410980



##########
File path: 
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/Caches.java
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.fn.harness;
+
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.SdkHarnessOptions;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.MoreExecutors;
+import org.cache2k.Cache2kBuilder;
+import org.cache2k.operation.Weigher;
+import org.github.jamm.MemoryMeter;
+
+/** Utility methods used to instantiate and operate over cache instances. */
+@SuppressWarnings("nullness")
+public final class Caches {
+
+  /** A cache that never stores any values. */
+  public static <K, V> Cache<K, V> noop() {
+    // We specifically use cache2k since it allows for recursive 
computeIfAbsent calls
+    // preventing deadlock from occurring when a loading function mutates the 
underlying cache

Review comment:
       fwiw, Caffeine handles this through `AsyncCache` where you do not need a 
separate thread to execute on. See FAQ's 
[example](https://github.com/ben-manes/caffeine/wiki/Faq#recursive-computations),
 which is similar to what Apache Solr does in their usage. It is a simple 
workaround for these rare cases and makes it more explicit about what is 
happening, as often recursive computes are surprising and can be error prone. I 
am not sure if one can expect per-key linearizability with recursive writes, so 
the plus/minus of this behavior depends on the use-case.
   
   One very minor topic to be aware of is that the `Cache#computeIfAbsent` used 
here does **not** follow the specification for Map's. In addition, until 
recently the Map's implementation was broken and caused the cache to be in an 
invalid state. See this [bug 
report](https://github.com/cache2k/cache2k/issues/174) where I found these 
issues for the simple single threaded cases. Since these types of mistakes are 
sadly common and only ironed by usage, you might want to spend a little extra 
time validating the behavior and avoiding regressions. Caffeine has had its 
share of bugs and it takes usage to wrangle them out, so early adopters should 
be a little more proactive to help the library authors catch their oversights.




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