busbey commented on a change in pull request #2685:
URL: https://github.com/apache/hbase/pull/2685#discussion_r527968265
##########
File path: hbase-client/src/main/java/org/apache/hadoop/hbase/util/PoolMap.java
##########
@@ -45,177 +36,138 @@
* key. A size of {@link Integer#MAX_VALUE} is interpreted as an unbounded
pool.
* </p>
*
+ * <p>
+ * PoolMap is thread-safe. It does not remove elements automatically. Unused
resources
+ * must be closed and removed explicitly.
+ * </p>
+ *
* @param <K>
* the type of the key to the resource
* @param <V>
* the type of the resource being pooled
*/
@InterfaceAudience.Private
-public class PoolMap<K, V> implements Map<K, V> {
- private PoolType poolType;
-
- private int poolMaxSize;
-
- private Map<K, Pool<V>> pools = new ConcurrentHashMap<>();
-
- public PoolMap(PoolType poolType) {
- this.poolType = poolType;
- }
-
- public PoolMap(PoolType poolType, int poolMaxSize) {
- this.poolType = poolType;
- this.poolMaxSize = poolMaxSize;
+public class PoolMap<K, V> {
+ private final Map<K, Pool<V>> pools;
+ private final PoolType poolType;
+ private final int poolMaxSize;
+
+ public PoolMap(PoolType poolType, int poolMaxSize) {
+ pools = new HashMap<>();
+ this.poolType = poolType;
+ this.poolMaxSize = poolMaxSize;
}
- @Override
- public V get(Object key) {
- Pool<V> pool = pools.get(key);
- return pool != null ? pool.get() : null;
+ public V getOrCreate(K key, PoolResourceSupplier<V> supplier) throws
IOException {
+ synchronized (pools) {
+ Pool<V> pool = pools.get(key);
+
+ if (pool == null) {
+ pool = createPool();
+ pools.put(key, pool);
+ }
+
+ try {
+ return pool.getOrCreate(supplier);
+ } catch (IOException | RuntimeException | Error e) {
+ if (pool.size() == 0) {
+ pools.remove(key);
+ }
+
+ throw e;
+ }
+ }
}
+ public boolean remove(K key, V value) {
+ synchronized (pools) {
+ Pool<V> pool = pools.get(key);
- @Override
- public V put(K key, V value) {
- Pool<V> pool = pools.get(key);
- if (pool == null) {
- pools.put(key, pool = createPool());
- }
- return pool != null ? pool.put(value) : null;
- }
+ if (pool == null) {
+ return false;
+ }
- @SuppressWarnings("unchecked")
- @Override
- public V remove(Object key) {
- Pool<V> pool = pools.remove(key);
- if (pool != null) {
- removeValue((K) key, pool.get());
- }
- return null;
- }
+ boolean removed = pool.remove(value);
- public boolean removeValue(K key, V value) {
- Pool<V> pool = pools.get(key);
- boolean res = false;
- if (pool != null) {
- res = pool.remove(value);
- if (res && pool.size() == 0) {
+ if (removed && pool.size() == 0) {
pools.remove(key);
}
+
+ return removed;
}
- return res;
}
- @Override
- public Collection<V> values() {
- Collection<V> values = new ArrayList<>();
- for (Pool<V> pool : pools.values()) {
- Collection<V> poolValues = pool.values();
- if (poolValues != null) {
- values.addAll(poolValues);
- }
+ public void remove(K key) {
+ synchronized (pools) {
+ pools.remove(key);
}
- return values;
}
- public Collection<V> values(K key) {
- Collection<V> values = new ArrayList<>();
- Pool<V> pool = pools.get(key);
- if (pool != null) {
- Collection<V> poolValues = pool.values();
- if (poolValues != null) {
- values.addAll(poolValues);
+ public List<V> values() {
+ List<V> values = new ArrayList<>();
+
+ synchronized (pools) {
+ for (Pool<V> pool : pools.values()) {
+ Collection<V> poolValues = pool.values();
+ if (poolValues != null) {
+ values.addAll(poolValues);
+ }
}
}
+
return values;
}
+ public List<V> values(K key) {
+ synchronized (pools) {
+ Pool<V> pool = pools.get(key);
- @Override
- public boolean isEmpty() {
- return pools.isEmpty();
+ if (pool == null) {
+ return Collections.emptyList();
+ } else {
+ return new ArrayList<>(pool.values());
+ }
+ }
}
- @Override
public int size() {
- return pools.size();
+ synchronized (pools) {
+ return pools.size();
+ }
}
Review comment:
we still need this? don't see it getting used in `AbstractRpcClient`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]