gianm commented on a change in pull request #6898: Prohibit assigning concurrent maps into Map-typed variables and fields and fix a race condition in CoordinatorRuleManager URL: https://github.com/apache/incubator-druid/pull/6898#discussion_r251258049
########## File path: indexing-service/src/main/java/org/apache/druid/indexing/common/Counters.java ########## @@ -19,54 +19,31 @@ package org.apache.druid.indexing.common; -import com.google.common.util.concurrent.AtomicDouble; - -import javax.annotation.Nullable; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicReference; -import java.util.function.BinaryOperator; +import java.util.concurrent.atomic.AtomicLong; -public class Counters +public final class Counters { - private final ConcurrentMap<String, AtomicInteger> intCounters = new ConcurrentHashMap<>(); - private final ConcurrentMap<String, AtomicDouble> doubleCounters = new ConcurrentHashMap<>(); - private final ConcurrentMap<String, AtomicReference> objectCounters = new ConcurrentHashMap<>(); - - public int increment(String key, int val) + public static <K> int incrementAndGetInt(ConcurrentHashMap<K, AtomicInteger> counters, K key) { - return intCounters.computeIfAbsent(key, k -> new AtomicInteger()).addAndGet(val); + // get() before computeIfAbsent() is an optimization to avoid locking in computeIfAbsent() if not needed. Review comment: Any idea why ConcurrentHashMap does not already employ an optimization like this? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org For additional commands, e-mail: commits-h...@druid.apache.org