Package alluxio.resource
Class DynamicResourcePool<T>
- java.lang.Object
-
- alluxio.resource.DynamicResourcePool<T>
-
- Type Parameters:
T
- the type of the resource
- All Implemented Interfaces:
Pool<T>
,java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
NettyChannelPool
@ThreadSafe public abstract class DynamicResourcePool<T> extends java.lang.Object implements Pool<T>
A dynamic pool that manages the resources. It clears old resources. It accepts a min and max capacity. When acquiring resources, the most recently used resource is returned.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DynamicResourcePool.Options
Options to initialize a Dynamic resource pool.protected class
DynamicResourcePool.ResourceInternal<R>
A wrapper on the resource to include the last time at which it was used.static class
DynamicResourcePool.SelectionPolicy
A policy specifying in what order to pick a resource item from a pool.
-
Field Summary
Fields Modifier and Type Field Description protected java.time.Clock
mClock
protected java.util.concurrent.ConcurrentHashMap<T,DynamicResourcePool.ResourceInternal<T>>
mResources
protected DynamicResourcePool.SelectionPolicy
mSelectionPolicy
the selection policy of the resource pool.
-
Constructor Summary
Constructors Constructor Description DynamicResourcePool(DynamicResourcePool.Options options)
Creates a dynamic pool instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description T
acquire()
Acquires a resource of type {code T} from the pool.T
acquire(long time, java.util.concurrent.TimeUnit unit)
Acquires a resource of type {code T} from the pool.void
close()
Closes the pool and clears all the resources.protected abstract void
closeResource(T resource)
Closes the resource.protected abstract T
createNewResource()
Creates a new resource.protected abstract com.codahale.metrics.Counter
getMetricCounter()
protected abstract boolean
isHealthy(T resource)
Checks whether a resource is healthy or not.void
release(T resource)
Releases the resource to the pool.protected abstract boolean
shouldGc(DynamicResourcePool.ResourceInternal<T> resourceInternal)
int
size()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface alluxio.resource.Pool
acquireCloseable, acquireCloseable
-
-
-
-
Field Detail
-
mSelectionPolicy
protected final DynamicResourcePool.SelectionPolicy mSelectionPolicy
the selection policy of the resource pool. seeDynamicResourcePool.SelectionPolicy
for details
-
mResources
protected final java.util.concurrent.ConcurrentHashMap<T,DynamicResourcePool.ResourceInternal<T>> mResources
-
mClock
protected java.time.Clock mClock
-
-
Constructor Detail
-
DynamicResourcePool
public DynamicResourcePool(DynamicResourcePool.Options options)
Creates a dynamic pool instance.- Parameters:
options
- the options
-
-
Method Detail
-
getMetricCounter
protected abstract com.codahale.metrics.Counter getMetricCounter()
-
acquire
public T acquire() throws java.io.IOException
Acquires a resource of type {code T} from the pool.
-
acquire
public T acquire(long time, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.io.IOException
Acquires a resource of type {code T} from the pool. This method is likeacquire()
, but it will time out if an object cannot be acquired before the specified amount of time.- Specified by:
acquire
in interfacePool<T>
- Parameters:
time
- an amount of time to waitunit
- the unit to use for time- Returns:
- a resource taken from the pool
- Throws:
java.util.concurrent.TimeoutException
- if it fails to acquire because of time outjava.io.IOException
- if the thread is interrupted while acquiring the resource
-
release
public void release(T resource)
Releases the resource to the pool. It expects the resource to be released was acquired from this pool.release(Object)
andacquire()
must be paired. Do not release the resource acquired multiple times. The behavior is undefined if that happens.
-
close
public void close() throws java.io.IOException
Closes the pool and clears all the resources. The resource pool should not be used after this.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
shouldGc
protected abstract boolean shouldGc(DynamicResourcePool.ResourceInternal<T> resourceInternal)
- Parameters:
resourceInternal
- the resource to check- Returns:
- true if the resource should be garbage collected
-
isHealthy
protected abstract boolean isHealthy(T resource)
Checks whether a resource is healthy or not.- Parameters:
resource
- the resource to check- Returns:
- true if the resource is healthy
-
closeResource
protected abstract void closeResource(T resource) throws java.io.IOException
Closes the resource. After this, the resource should not be used. It is not guaranteed that the resource is closed after the function returns.- Parameters:
resource
- the resource to close- Throws:
java.io.IOException
-
createNewResource
protected abstract T createNewResource() throws java.io.IOException
Creates a new resource.- Returns:
- the newly created resource
- Throws:
java.io.IOException
-
-