wchevreuil commented on a change in pull request #2685:
URL: https://github.com/apache/hbase/pull/2685#discussion_r527776795
##########
File path: hbase-client/src/main/java/org/apache/hadoop/hbase/util/PoolMap.java
##########
@@ -85,18 +83,17 @@ public V put(K key, V value) {
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
- Pool<V> pool = pools.remove(key);
- if (pool != null) {
- removeValue((K) key, pool.get());
- }
+ pools.remove(key);
return null;
}
Review comment:
I know it was already returning null before this PR, but for compliance
with Map interface definition, we should rather return `pools.remove(key)`
##########
File path: hbase-client/src/main/java/org/apache/hadoop/hbase/util/PoolMap.java
##########
@@ -321,62 +345,44 @@ public R get() {
* @param <R>
* the type of the resource
*/
- static class ThreadLocalPool<R> extends ThreadLocal<R> implements Pool<R> {
- private static final Map<ThreadLocalPool<?>, AtomicInteger> poolSizes =
new HashMap<>();
+ static class ThreadLocalPool<R> implements Pool<R> {
Review comment:
Should we rename this class entirely? `ThreadLocal` in the name may
mislead developers into thinking this fully behaves like a `ThreadLocal`.
##########
File path: hbase-client/src/main/java/org/apache/hadoop/hbase/util/PoolMap.java
##########
@@ -275,43 +272,70 @@ public static PoolType fuzzyMatch(String name) {
*
*/
@SuppressWarnings("serial")
- static class RoundRobinPool<R> extends CopyOnWriteArrayList<R> implements
Pool<R> {
- private int maxSize;
- private int nextResource = 0;
+ static class RoundRobinPool<R> implements Pool<R> {
+ private final List<R> resources;
+ private final int maxSize;
+
+ private int nextIndex;
public RoundRobinPool(int maxSize) {
+ if (maxSize <= 0) {
+ throw new IllegalArgumentException("maxSize must be positive");
+ }
+
+ resources = new ArrayList<>(maxSize);
this.maxSize = maxSize;
}
@Override
- public R put(R resource) {
- if (super.size() < maxSize) {
- add(resource);
+ public R get() {
+ int size = resources.size();
+
+ /* letting pool to grow */
+ if (size < maxSize) {
+ return null;
}
+
+ R resource = resources.get(nextIndex);
+
+ /* at this point size cannot be 0 */
+ nextIndex = (nextIndex + 1) % size;
+
+ return resource;
+ }
+
+ @Override
+ public R put(R resource) {
+ resources.add(resource);
return null;
}
@Override
- public R get() {
- if (super.size() < maxSize) {
- return null;
- }
- nextResource %= super.size();
- R resource = get(nextResource++);
- return resource;
+ public boolean remove(R resource) {
+ return resources.remove(resource);
+ }
+
+ @Override
+ public void clear() {
+ resources.clear();
}
@Override
public Collection<R> values() {
- return this;
+ return resources;
}
+ @Override
+ public int size() {
+ return resources.size();
+ }
}
/**
* The <code>ThreadLocalPool</code> represents a {@link PoolMap.Pool} that
- * builds on the {@link ThreadLocal} class. It essentially binds the resource
- * to the thread from which it is accessed.
+ * works similarly to {@link ThreadLocal} class. It essentially binds the
resource
+ * to the thread from which it is accessed. It doesn't remove resources when
a thread exists,
Review comment:
nit: `exits`, not `exists`
----------------------------------------------------------------
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]