You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2021/11/23 06:12:21 UTC

[GitHub] [cassandra] jacek-lewandowski commented on a change in pull request #1331: CASSANDRA-17071-trunk: Refactor keyspaces management in Schema

jacek-lewandowski commented on a change in pull request #1331:
URL: https://github.com/apache/cassandra/pull/1331#discussion_r754824930



##########
File path: src/java/org/apache/cassandra/utils/concurrent/ExtendedNonBlockingHashMap.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.cassandra.utils.concurrent;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import org.cliffc.high_scale_lib.NonBlockingHashMap;
+
+import org.apache.cassandra.utils.Throwables;
+
+/**
+ * An extension of {@link NonBlockingHashMap} where all values are wrapped by {@link Future}.
+ * <p>
+ * The main purpose of this class is to provide the functionality of concurrent hash map which may perform operations like
+ * {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} and {@link ConcurrentHashMap#computeIfPresent(Object, BiFunction)}
+ * with synchronization scope reduced to the single key - that is, when dealing with a single key, unline
+ * {@link ConcurrentHashMap} we do not lock the whole map for the time the mapping function is running. This may help
+ * to avoid the case when we want to load/unload a value for a key K1 while loading/unloading a value for a key K2. Such
+ * scenario is forbidden in case of {@link ConcurrentHashMap} and leads to a deadlock. On the other hand, {@link NonBlockingHashMap}
+ * does not guarantee at-most-once sematnics of running the mapping function for a single key.
+ *
+ * @param <K>
+ * @param <V>
+ */
+public class ExtendedNonBlockingHashMap<K, V> extends NonBlockingHashMap<K, Future<V>>
+{
+
+    /**
+     * Get a value for a given key, waiting for initialization if necessary. This method does not initialize the value
+     * if missing. It returns {@code null} regardless the value is missing or failed to initialize.
+     */
+    public V blockingGet(K key)
+    {
+        Future<V> future = get(key);
+        if (future != null && future.isDone())
+            return future.awaitUninterruptibly().getNow();

Review comment:
       yes, indeed, this is the result of the evolution of this class, I forgot to update this method




-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org