voldemort.utils.pool
Class QueuedKeyedResourcePool<K,V>

java.lang.Object
  extended by voldemort.utils.pool.KeyedResourcePool<K,V>
      extended by voldemort.utils.pool.QueuedKeyedResourcePool<K,V>

public class QueuedKeyedResourcePool<K,V>
extends KeyedResourcePool<K,V>

Extends simple implementation of a per-key resource pool with a non-blocking interface to enqueue requests for a resource when one becomes available.

Beyond the expectations documented in KeyedResourcePool, the following is expected of the user of this class:


Nested Class Summary
 
Nested classes/interfaces inherited from class voldemort.utils.pool.KeyedResourcePool
KeyedResourcePool.Pool<V>
 
Constructor Summary
QueuedKeyedResourcePool(ResourceFactory<K,V> objectFactory, ResourcePoolConfig config)
           
 
Method Summary
 void checkin(K key, V resource)
          Check the given resource back into the pool
 void close()
          Close the queue and the pool.
static
<K,V> QueuedKeyedResourcePool<K,V>
create(ResourceFactory<K,V> factory)
          Create a new queued pool using the defaults for key of tyep K, request of type R, and value of Type V.
static
<K,V> QueuedKeyedResourcePool<K,V>
create(ResourceFactory<K,V> factory, ResourcePoolConfig config)
          Create a new queued pool with key type K, request type R, and value type V.
protected  void destroyRequest(AsyncResourceRequest<V> resourceRequest)
          A safe wrapper to destroy the given resource request.
 int getRegisteredResourceRequestCount()
          Count the total number of queued resource requests for all queues.
 int getRegisteredResourceRequestCount(K key)
          Count the number of queued resource requests for a specific pool.
protected  java.util.Queue<AsyncResourceRequest<V>> getRequestQueueForExistingKey(K key)
           
protected  java.util.Queue<AsyncResourceRequest<V>> getRequestQueueForKey(K key)
           
protected  boolean internalClose()
           
 void registerResourceRequest(K key, AsyncResourceRequest<V> resourceRequest)
          This method is the asynchronous (nonblocking) version of KeyedResourcePool.checkout.
 
Methods inherited from class voldemort.utils.pool.KeyedResourcePool
attemptNonBlockingCheckout, checkNotClosed, checkout, destroyResource, getBlockingGetsCount, getBlockingGetsCount, getCheckedInResourceCount, getCheckedInResourcesCount, getResourcePoolForExistingKey, getResourcePoolForKey, getTotalResourceCount, getTotalResourceCount, isOpenAndValid, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueuedKeyedResourcePool

public QueuedKeyedResourcePool(ResourceFactory<K,V> objectFactory,
                               ResourcePoolConfig config)
Method Detail

create

public static <K,V> QueuedKeyedResourcePool<K,V> create(ResourceFactory<K,V> factory,
                                                        ResourcePoolConfig config)
Create a new queued pool with key type K, request type R, and value type V.

Parameters:
factory - The factory that creates objects
config - The pool config
Returns:
The created pool

create

public static <K,V> QueuedKeyedResourcePool<K,V> create(ResourceFactory<K,V> factory)
Create a new queued pool using the defaults for key of tyep K, request of type R, and value of Type V.

Parameters:
factory - The factory that creates objects
Returns:
The created pool

registerResourceRequest

public void registerResourceRequest(K key,
                                    AsyncResourceRequest<V> resourceRequest)
This method is the asynchronous (nonblocking) version of KeyedResourcePool.checkout. This method necessarily has a different function declaration (i.e., arguments passed and return type). This method either checks out a resource and uses that resource or enqueues a request to checkout the resource. I.e., there is a non-blocking fast-path that is tried optimistically.

Parameters:
key - The key to checkout the resource for

checkin

public void checkin(K key,
                    V resource)
             throws java.lang.Exception
Check the given resource back into the pool

Overrides:
checkin in class KeyedResourcePool<K,V>
Parameters:
key - The key for the resource
resource - The resource
Throws:
java.lang.Exception

destroyRequest

protected void destroyRequest(AsyncResourceRequest<V> resourceRequest)
A safe wrapper to destroy the given resource request.


internalClose

protected boolean internalClose()
Overrides:
internalClose in class KeyedResourcePool<K,V>

close

public void close()
Close the queue and the pool.

Overrides:
close in class KeyedResourcePool<K,V>

getRequestQueueForKey

protected java.util.Queue<AsyncResourceRequest<V>> getRequestQueueForKey(K key)

getRequestQueueForExistingKey

protected java.util.Queue<AsyncResourceRequest<V>> getRequestQueueForExistingKey(K key)

getRegisteredResourceRequestCount

public int getRegisteredResourceRequestCount(K key)
Count the number of queued resource requests for a specific pool.

Parameters:
key - The key
Returns:
The count of queued resource requests. Returns 0 if no queue exists for given key.

getRegisteredResourceRequestCount

public int getRegisteredResourceRequestCount()
Count the total number of queued resource requests for all queues. The result is "approximate" in the face of concurrency since individual queues can change size during the aggregate count.

Returns:
The (approximate) aggregate count of queued resource requests.


Jay Kreps, Roshan Sumbaly, Alex Feinberg, Bhupesh Bansal, Lei Gao, Chinmay Soman, Vinoth Chandar, Zhongjie Wu