Top Description Fields Constructors Methods
stormpot

pack-priv final Class BlazePool<T extends Poolable>

extends Pool<T extends Poolable>
implements ManagedPool
Class Inheritance
All Implemented Interfaces
stormpot.ManagedPool
Type Parameters
<T>
The type of Poolable managed by this pool.
Imports
java.util.Objects, java.util.concurrent.LinkedTransferQueue, .TimeUnit

BlazePool is a highly optimised Pool implementation that consists of a queues of Poolable instances, the access to which is made faster with clever use of ThreadLocals. Object allocation always happens in a dedicated thread, off-loading the cost of allocating the pooled objects. This leads to reduced deviation in the times it takes claim method to complete, provided the pool is not depleted. BlazePool optimises for the case where the same threads need to claim and release objects over and over again. On the other hand, if the releasing thread tends to differ from the claiming thread, then the major optimisation in BlazePool is defeated, and performance regresses to a slow-path that is limited by contention on a blocking queue.
Author
Chris Vest

Field Summary

Modifier and TypeField and Description
private final AllocationController<T>
private final Expiration<? super T>
private final RefillPile<T>
pack-priv static final Exception
private final LinkedTransferQueue<BSlot<T>>
private final MetricsRecorder
private final RefillPile<T>
private final BSlot<T>
poisonPill

A special slot used to signal that the pool has been shut down.

private volatile boolean
private static final Exception
private final ThreadLocal<BSlotCache<T>>

Constructor Summary

AccessConstructor and Description
pack-priv
BlazePool(PoolBuilder<T>
The pool configuration to use.
builder
,
AllocationProcess factory)

Construct a new BlazePool instance based on the given PoolBuilder.

Method Summary

Modifier and TypeMethod and Description
public T
claim(Timeout
The timeout of the maximum permitted time-slice to wait for an object to become available. A timeout with a value of zero or less means that the call will do no waiting, preferring instead to return early if no objects are available.
timeout
)

Implements abstract stormpot.PoolTap.claim.

Claim the exclusive rights until released, to an object in the pool.
private boolean
dealWithSlotPoison(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr, Exception poison)

public long
getAllocationCount()

Implements stormpot.ManagedPool.getAllocationCount.

Return the number of objects the pool has allocated since it was created.
public double
getAllocationFailureLatencyPercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getAllocationFailureLatencyPercentile.

Get the approximate latency value, in milliseconds, for failed allocation latencies within the given percentile/quantile of what has been recorded so far.
public double
getAllocationLatencyPercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getAllocationLatencyPercentile.

Get the approximate Poolable object allocation latency value, in milliseconds, of the given percentile/quantile of the values recorded so far.
public double
getDeallocationLatencyPercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getDeallocationLatencyPercentile.

Get the approximate latency value, in milliseconds, for deallocation latencies within the given percentile/quantile of what has been recorded so far.
public long
getFailedAllocationCount()

Implements stormpot.ManagedPool.getFailedAllocationCount.

Return the number of allocations that has failed, either because the allocator threw an exception or because it returned null, since the pool was created.
public long
getLeakedObjectsCount()

Implements stormpot.ManagedPool.getLeakedObjectsCount.

If the pool is capable of precise object leak detection, this method will return the number of object leaks that have been detected, and prevented, since the pool was created.
public ManagedPool
getManagedPool()

Implements abstract stormpot.Pool.getManagedPool.

Get the ManagedPool instance that represents this pool.
public double
getObjectLifetimePercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getObjectLifetimePercentile.

Get the approximate object lifetime, in milliseconds, for the given percentile/quantile.
public double
getReallocationFailureLatencyPercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getReallocationFailureLatencyPercentile.

Get the approximate latency value, in milliseconds, for failed reallocation latencies within the given percentile/quantile of what has been recorded so far.
public double
getReallocationLatencyPercentile(double
The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.
percentile
)

Implements stormpot.ManagedPool.getReallocationLatencyPercentile.

Get the approximate latency value, in milliseconds, for reallocation latencies within the given percentile/quantile of what has been recorded so far.
public int
getTargetSize()

Implements abstract stormpot.Pool.getTargetSize.

Implements stormpot.ManagedPool.getTargetSize.

Get the current target size of the pool.
public PoolTap<T>
getThreadLocalTap()

Implements abstract stormpot.Pool.getThreadLocalTap.

Get a PoolTap that only support access by one thread at a time.
private boolean
handleCommonInvalidation(BSlot<T> slot, BSlotCache<T> cache, Throwable exception)

private boolean
handleUncommonInvalidation(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr)

private boolean
isInvalid(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr)

public boolean
isShutDown()

Implements stormpot.ManagedPool.isShutDown.

Returns 'true' if the shut down process has been started on this pool, 'false' otherwise.
private boolean
private void
kill(BSlot<T> slot, BSlotCache<T> cache)

public void
setTargetSize(int
The new target size.
size
)

Implements abstract stormpot.Pool.setTargetSize.

Implements stormpot.ManagedPool.setTargetSize.

Set a new target size of the pool.
public Completion
shutdown()

Implements abstract stormpot.Pool.shutdown.

Initiate the shut down process on this pool, and return a Completion instance representing the shut down procedure.
private T
slowClaim(Timeout timeout, BSlotCache<T> cache)

pack-priv T
tlrClaim(Timeout timeout, BSlotCache<T> cache)

Inherited from stormpot.Pool:
fromfromInlinefromThreadedgetThreadSafeTapof

Field Detail

allocatorback to summary
private final AllocationController<T> allocator
deallocRuleback to summary
private final Expiration<? super T> deallocRule
disregardPileback to summary
private final RefillPile<T> disregardPile
EXPLICIT_EXPIRE_POISONback to summary
pack-priv static final Exception EXPLICIT_EXPIRE_POISON
liveback to summary
private final LinkedTransferQueue<BSlot<T>> live
metricsRecorderback to summary
private final MetricsRecorder metricsRecorder
newAllocationsback to summary
private final RefillPile<T> newAllocations
poisonPillback to summary
private final BSlot<T> poisonPill

A special slot used to signal that the pool has been shut down.

shutdownback to summary
private volatile boolean shutdown
SHUTDOWN_POISONback to summary
private static final Exception SHUTDOWN_POISON
tlrback to summary
private final ThreadLocal<BSlotCache<T>> tlr

Constructor Detail

BlazePoolback to summary
pack-priv BlazePool(PoolBuilder<T> builder, AllocationProcess factory)

Construct a new BlazePool instance based on the given PoolBuilder.

Parameters
builder:PoolBuilder<T>

The pool configuration to use.

Method Detail

claimback to summary
public T claim(Timeout timeout) throws PoolException, InterruptedException

Implements abstract stormpot.PoolTap.claim.

Doc from stormpot.PoolTap.claim.

Claim the exclusive rights until released, to an object in the pool. Possibly waiting up to the specified amount of time, as given by the provided Timeout instance, for one to become available if the pool has been depleted. If the timeout elapses before an object can be claimed, then `null` is returned instead. The timeout will be honoured even if the Allocators allocate methods blocks forever. If the given timeout has a zero or negative value, then the method will not wait. If the current thread has already one or more objects currently claimed, then a distinct object will be returned, if one is or becomes available. This means that it is possible for a single thread to deplete the pool, if it so desires. However, doing so is inherently deadlock prone, so avoid claiming more than one object at a time per thread, if at all possible. This method may throw a PoolException if the pool have trouble allocating objects. That is, if its assigned Allocator throws exceptions from its allocate method, or returns `null`. An InterruptedException will be thrown if the thread has its interrupted flag set upon entry to this method, or is interrupted while waiting. The interrupted flag on the thread will be cleared after this, as per the general contract of interruptible methods. If the pool has been shut down, then an IllegalStateException will be thrown when this method is called. Likewise if we are waiting for an object to become available, and someone shuts the pool down. Here's an example code snippet, where an object is claimed, printed to `System.out`, and then released back to the pool: [source,java] ---- include::../src/test/java/examples/Examples.java[tag=poolClaimPrintExample] ---- Memory effects: * The release of an object happens-before any subsequent claim or deallocation of that object, and, * The allocation of an object happens-before any claim of that object.

Parameters
timeout:Timeout

The timeout of the maximum permitted time-slice to wait for an object to become available. A timeout with a value of zero or less means that the call will do no waiting, preferring instead to return early if no objects are available.

Returns:T

An object of the Poolable subtype T to which the exclusive rights have been claimed, or `null` if the timeout period elapsed before an object became available.

Annotations
@Override
Exceptions
PoolException:
If an object allocation failed because the Allocator threw an exception from its allocate method, or returned `null`, or the expiration check threw an exception.
InterruptedException:
if the current thread is interrupted upon entry, or becomes interrupted while waiting.
dealWithSlotPoisonback to summary
private boolean dealWithSlotPoison(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr, Exception poison)
getAllocationCountback to summary
public long getAllocationCount()

Implements stormpot.ManagedPool.getAllocationCount.

Doc from stormpot.ManagedPool.getAllocationCount.

Return the number of objects the pool has allocated since it was created.

Returns:long

The number of Poolable objects ever created by this pool.

Annotations
@Override
getAllocationFailureLatencyPercentileback to summary
public double getAllocationFailureLatencyPercentile(double percentile)

Implements stormpot.ManagedPool.getAllocationFailureLatencyPercentile.

Doc from stormpot.ManagedPool.getAllocationFailureLatencyPercentile.

Get the approximate latency value, in milliseconds, for failed allocation latencies within the given percentile/quantile of what has been recorded so far.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate latency for failed allocations in milliseconds, for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getAllocationLatencyPercentileback to summary
public double getAllocationLatencyPercentile(double percentile)

Implements stormpot.ManagedPool.getAllocationLatencyPercentile.

Doc from stormpot.ManagedPool.getAllocationLatencyPercentile.

Get the approximate Poolable object allocation latency value, in milliseconds, of the given percentile/quantile of the values recorded so far.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate latency for allocations in milliseconds, for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getDeallocationLatencyPercentileback to summary
public double getDeallocationLatencyPercentile(double percentile)

Implements stormpot.ManagedPool.getDeallocationLatencyPercentile.

Doc from stormpot.ManagedPool.getDeallocationLatencyPercentile.

Get the approximate latency value, in milliseconds, for deallocation latencies within the given percentile/quantile of what has been recorded so far.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate latency for deallocations, regardless of whether the throw exceptions or not, in milliseconds for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getFailedAllocationCountback to summary
public long getFailedAllocationCount()

Implements stormpot.ManagedPool.getFailedAllocationCount.

Doc from stormpot.ManagedPool.getFailedAllocationCount.

Return the number of allocations that has failed, either because the allocator threw an exception or because it returned null, since the pool was created.

Returns:long

The number of allocations that have failed for one reason or another.

Annotations
@Override
getLeakedObjectsCountback to summary
public long getLeakedObjectsCount()

Implements stormpot.ManagedPool.getLeakedObjectsCount.

Doc from stormpot.ManagedPool.getLeakedObjectsCount.

If the pool is capable of precise object leak detection, this method will return the number of object leaks that have been detected, and prevented, since the pool was created. If the pool does not support precise object leak detection, then this method returns -1. There are two kinds of leaks: One where the application forgets to release an object back to the pool, but keeps a strong reference to the object, and another where the application not only forgets to release the object, but also looses the reference to the object, making it eligible for garbage collection. The precise leak detector will only count leaks of the latter kind, where the leaked object has been garbage collected.

Returns:long

The number of objects leaked from the users of this pool, since the pool was created, or -1 if the pool does not implement precise leak detection.

Annotations
@Override
getManagedPoolback to summary
public ManagedPool getManagedPool()

Implements abstract stormpot.Pool.getManagedPool.

Doc from stormpot.Pool.getManagedPool.

Get the ManagedPool instance that represents this pool.

Returns:ManagedPool

The ManagedPool instance for this pool.

Annotations
@Override
getObjectLifetimePercentileback to summary
public double getObjectLifetimePercentile(double percentile)

Implements stormpot.ManagedPool.getObjectLifetimePercentile.

Doc from stormpot.ManagedPool.getObjectLifetimePercentile.

Get the approximate object lifetime, in milliseconds, for the given percentile/quantile.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate object lifetime in milliseconds, for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getReallocationFailureLatencyPercentileback to summary
public double getReallocationFailureLatencyPercentile(double percentile)

Implements stormpot.ManagedPool.getReallocationFailureLatencyPercentile.

Doc from stormpot.ManagedPool.getReallocationFailureLatencyPercentile.

Get the approximate latency value, in milliseconds, for failed reallocation latencies within the given percentile/quantile of what has been recorded so far.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate latency for failed reallocations in milliseconds, for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getReallocationLatencyPercentileback to summary
public double getReallocationLatencyPercentile(double percentile)

Implements stormpot.ManagedPool.getReallocationLatencyPercentile.

Doc from stormpot.ManagedPool.getReallocationLatencyPercentile.

Get the approximate latency value, in milliseconds, for reallocation latencies within the given percentile/quantile of what has been recorded so far.

Parameters
percentile:double

The percentile to get, as a decimal, e.g. a number between 0.0 and 1.0.

Returns:double

The approximate latency for reallocations in milliseconds, for the given percentile, or Double.NaN if no stormpot.MetricsRecorder has been configured for the pool.

Annotations
@Override
getTargetSizeback to summary
public int getTargetSize()

Implements abstract stormpot.Pool.getTargetSize.

Implements stormpot.ManagedPool.getTargetSize.

Doc from stormpot.ManagedPool.getTargetSize.

Get the current target size of the pool.

Returns:int

The current target size.

Annotations
@Override
getThreadLocalTapback to summary
public PoolTap<T> getThreadLocalTap()

Implements abstract stormpot.Pool.getThreadLocalTap.

Doc from stormpot.Pool.getThreadLocalTap.

Get a PoolTap that only support access by one thread at a time. In other words, where claim(Timeout) cannot be called concurrently in multiple threads *on the same tap*. The pool itself will still be thread-safe, but each thread that wishes to access the pool via a thread-local tap, must have their own tap instance. It is a use error to access these pool taps concurrently from multiple threads, or to transfer them from one thread to another without safe publication.

Returns:PoolTap<T>

A thread-local PoolTap.

Annotations
@Override
handleCommonInvalidationback to summary
private boolean handleCommonInvalidation(BSlot<T> slot, BSlotCache<T> cache, Throwable exception)
handleUncommonInvalidationback to summary
private boolean handleUncommonInvalidation(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr)
isInvalidback to summary
private boolean isInvalid(BSlot<T> slot, BSlotCache<T> cache, boolean isTlr)
isShutDownback to summary
public boolean isShutDown()

Implements stormpot.ManagedPool.isShutDown.

Doc from stormpot.ManagedPool.isShutDown.

Returns 'true' if the shut down process has been started on this pool, 'false' otherwise. This method does not reveal whether or not the shut down process has completed.

Returns:boolean

'true' if Pool#shutdown() has been called on this pool.

Annotations
@Override
isUncommonlyInvalidback to summary
private boolean isUncommonlyInvalid(BSlot<T> slot)
killback to summary
private void kill(BSlot<T> slot, BSlotCache<T> cache)
setTargetSizeback to summary
public void setTargetSize(int size)

Implements abstract stormpot.Pool.setTargetSize.

Implements stormpot.ManagedPool.setTargetSize.

Doc from stormpot.ManagedPool.setTargetSize.

Set a new target size of the pool.

Parameters
size:int

The new target size.

Annotations
@Override
shutdownback to summary
public Completion shutdown()

Implements abstract stormpot.Pool.shutdown.

Doc from stormpot.Pool.shutdown.

Initiate the shut down process on this pool, and return a Completion instance representing the shut down procedure. The shut down process is asynchronous, and the shutdown method is guaranteed to not wait for any claimed Poolables to be released. The shut down process cannot complete before all Poolables are released back into the pool and deallocated, and all internal resources (such as threads, for instance) have been released as well. Only when all of these things have been taken care of, does the await methods of the Completion return. Once the shut down process has been initiated, that is, as soon as this method is called, the pool can no longer be used and all calls to claim(Timeout) will throw an IllegalStateException. Threads that are already waiting for objects in the claim method, will also wake up and receive an IllegalStateException. All objects that are already claimed when this method is called, will continue to function until they are released. The shut down process is guaranteed to never deallocate objects that are currently claimed. Their deallocation will wait until they are released.

Returns:Completion

A Completion instance that represents the shut down process.

Annotations
@Override
slowClaimback to summary
private T slowClaim(Timeout timeout, BSlotCache<T> cache) throws PoolException, InterruptedException
tlrClaimback to summary
pack-priv T tlrClaim(Timeout timeout, BSlotCache<T> cache) throws PoolException, InterruptedException