Top Description Fields Constructors Methods
reactor.pool.introspection

public final Class SamplingAllocationStrategy

extends Object
implements AllocationStrategy
Class Inheritance
All Implemented Interfaces
reactor.pool.AllocationStrategy
Imports
java.util.BitSet, .LinkedList, .Objects, .Random, reactor.core.Exceptions, reactor.core.publisher.Mono, reactor.pool.AllocationStrategy, .PoolBuilder

An AllocationStrategy that delegates to an underlying AllocationStrategy and samples the getPermits(int) and returnPermits(int) methods.

Only a given percentage of calls are sampled, and these are stored as stacktraces in the sampling strategy. If a AllocationStrategy#returnPermits(int) call fails, a new IllegalArgumentException is thrown with a composite cause that represents all the sampled calls (see Exceptions#unwrapMultiple(Throwable)).

The two types of calls are sampled separately. The reservoir sampling algorithm is used to instantiate a sampling decision bitset over 100 slots, which are then applied to windows of 100 calls. Access to the delegate field is possible, as well as gettingSamplingRate/returningSamplingRate configuration fields and gettingSamples/returningSamples collections.

Author
Simon Baslé

Field Summary

Modifier and TypeField and Description
pack-priv long
pack-priv long
public final AllocationStrategy
delegate

The delegate AllocationStrategy backing this sampling strategy.

pack-priv final BitSet
public final LinkedList<Throwable>
gettingSamples

The list of samples for getPermits(int) calls, as Throwable that trace back to the callers of the sampled calls.

public final double
gettingSamplingRate

The configured sampling rate for getPermits(int) calls, as a double between 0d and 1d (percentage).

pack-priv final BitSet
public final LinkedList<Throwable>
returningSamples

The list of samples for returnPermits(int) calls, as Throwable that trace back to the callers of the sampled calls.

public final double
returningSamplingRate

The configured sampling rate for returnPermits(int) calls, as a double between 0d and 1d (percentage).

Constructor Summary

AccessConstructor and Description
pack-priv
SamplingAllocationStrategy(AllocationStrategy delegate, double gettingSamplingRate, double returningSamplingRate)

Method Summary

Modifier and TypeMethod and Description
public int
estimatePermitCount()

Implements reactor.pool.AllocationStrategy.estimatePermitCount.

Best-effort peek at the state of the strategy which indicates roughly how many more resources can currently be allocated.
public int
getPermits(int
the desired number of new resources
desired
)

Implements reactor.pool.AllocationStrategy.getPermits.

Try to get the permission to allocate a desired positive number of new resources.
public int
public int
public int
permitMinimum()

Implements reactor.pool.AllocationStrategy.permitMinimum.

Return the minimum number of permits this strategy tries to maintain granted (reflecting a minimal size for the pool), or 0 for scale-to-zero.
public void
returnPermits(int returned)

Implements reactor.pool.AllocationStrategy.returnPermits.

Update the strategy to indicate that N resources were discarded from the Pool, potentially leaving space for N new ones to be allocated.
pack-priv static BitSet

Returns:

a random bitset
sampleBitSet
(int
cardinality of the bit set
selectedOutOf100
)

Reservoir sampling algorithm borrowed from Stack Overflow.

pack-priv void
sampleGetting(final int desired)

pack-priv void
sampleReturning(final int returned)

pack-priv static AllocationStrategy
sizeBetweenHelper(int min, int max)

public static SamplingAllocationStrategy

Returns:

a sampled AllocationStrategy that otherwise behaves like the PoolBuilder#sizeBetween(int, int) strategy
sizeBetweenWithSampling
(int
the minimum number of permits (see PoolBuilder#sizeBetween(int, int))
min
,
int
the maximum number of permits (see PoolBuilder#sizeBetween(int, int))
max
,
double
the sampling rate for AllocationStrategy#getPermits(int) calls (0.0d to 1.0d)
getPermitsSamplingRate
,
double
the sampling rate for AllocationStrategy#returnPermits(int) calls (0.0d to 1.0d)
returnPermitsSamplingRate
)

An SamplingAllocationStrategy that wraps a sizeBetween AllocationStrategy and samples calls to AllocationStrategy#getPermits(int) and AllocationStrategy#returnPermits(int).

public int
warmupParallelism()

Overrides default reactor.pool.AllocationStrategy.warmupParallelism.

Return the concurrency level used when the allocator is subscribed to during the warmup phase, if any.
public static SamplingAllocationStrategy

Returns:

a sampled AllocationStrategy that otherwise behaves like the delegate
withSampling
(AllocationStrategy
the underlying AllocationStrategy
delegate
,
double
the sampling rate for AllocationStrategy#getPermits(int) calls (0.0d to 1.0d)
getPermitsSamplingRate
,
double
the sampling rate for AllocationStrategy#returnPermits(int) calls (0.0d to 1.0d)
returnPermitsSamplingRate
)

An AllocationStrategy that wraps a delegate and samples calls to AllocationStrategy#getPermits(int) and AllocationStrategy#returnPermits(int).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

countGettingback to summary
pack-priv long countGetting
countReturningback to summary
pack-priv long countReturning
delegateback to summary
public final AllocationStrategy delegate

The delegate AllocationStrategy backing this sampling strategy.

gettingSampleDecisionsback to summary
pack-priv final BitSet gettingSampleDecisions
gettingSamplesback to summary
public final LinkedList<Throwable> gettingSamples

The list of samples for getPermits(int) calls, as Throwable that trace back to the callers of the sampled calls.

gettingSamplingRateback to summary
public final double gettingSamplingRate

The configured sampling rate for getPermits(int) calls, as a double between 0d and 1d (percentage).

returningSampleDecisionsback to summary
pack-priv final BitSet returningSampleDecisions
returningSamplesback to summary
public final LinkedList<Throwable> returningSamples

The list of samples for returnPermits(int) calls, as Throwable that trace back to the callers of the sampled calls.

returningSamplingRateback to summary
public final double returningSamplingRate

The configured sampling rate for returnPermits(int) calls, as a double between 0d and 1d (percentage).

Constructor Detail

SamplingAllocationStrategyback to summary
pack-priv SamplingAllocationStrategy(AllocationStrategy delegate, double gettingSamplingRate, double returningSamplingRate)

Method Detail

estimatePermitCountback to summary
public int estimatePermitCount()

Implements reactor.pool.AllocationStrategy.estimatePermitCount.

Doc from reactor.pool.AllocationStrategy.estimatePermitCount.

Best-effort peek at the state of the strategy which indicates roughly how many more resources can currently be allocated. Should be paired with getPermits(int) for an atomic permission.

Returns:int

an ESTIMATED count of how many more resources can currently be allocated

Annotations
@Override
getPermitsback to summary
public int getPermits(int desired)

Implements reactor.pool.AllocationStrategy.getPermits.

Doc from reactor.pool.AllocationStrategy.getPermits.

Try to get the permission to allocate a desired positive number of new resources. Returns the permissible number of resources which MUST be created (otherwise the internal live counter of the strategy might be off). This permissible number might be zero, and it can also be a greater number than desired, which could for example denote a minimum warmed-up size for the pool to maintain (see below). Once a resource is discarded from the pool, it must update the strategy using returnPermits(int) (which can happen in batches or with value 1).

For the warming up case, the typical pattern would be to call this method with a desired of zero.

Parameters
desired:int

the desired number of new resources

Returns:int

the actual number of new resources that MUST be created, can be 0 and can be more than desired

Annotations
@Override
permitGrantedback to summary
public int permitGranted()

Implements reactor.pool.AllocationStrategy.permitGranted.

Returns:int

Doc from reactor.pool.AllocationStrategy.permitGranted.

a best estimate of the number of permits currently granted, between 0 and Integer#MAX_VALUE

Annotations
@Override
permitMaximumback to summary
public int permitMaximum()

Implements reactor.pool.AllocationStrategy.permitMaximum.

Returns:int

Doc from reactor.pool.AllocationStrategy.permitMaximum.

the maximum number of permits this strategy can grant in total, or Integer#MAX_VALUE for unbounded

Annotations
@Override
permitMinimumback to summary
public int permitMinimum()

Implements reactor.pool.AllocationStrategy.permitMinimum.

Doc from reactor.pool.AllocationStrategy.permitMinimum.

Return the minimum number of permits this strategy tries to maintain granted (reflecting a minimal size for the pool), or 0 for scale-to-zero.

Returns:int

the minimum number of permits this strategy tries to maintain, or 0

Annotations
@Override
returnPermitsback to summary
public void returnPermits(int returned)

Implements reactor.pool.AllocationStrategy.returnPermits.

Doc from reactor.pool.AllocationStrategy.returnPermits.

Update the strategy to indicate that N resources were discarded from the Pool, potentially leaving space for N new ones to be allocated. Users MUST ensure that this method isn't called with a value greater than the number of held permits it has.

Some strategy MIGHT throw an IllegalArgumentException if it can be determined the number of returned permits is not consistent with the strategy's limits and delivered permits.

Annotations
@Override
sampleBitSetback to summary
pack-priv static BitSet sampleBitSet(int selectedOutOf100)

Reservoir sampling algorithm borrowed from Stack Overflow. https://stackoverflow.com/questions/12817946/generate-a-random-bitset-with-n-1s

Parameters
selectedOutOf100:int

cardinality of the bit set

Returns:BitSet

a random bitset

sampleGettingback to summary
pack-priv void sampleGetting(final int desired)
sampleReturningback to summary
pack-priv void sampleReturning(final int returned)
sizeBetweenHelperback to summary
pack-priv static AllocationStrategy sizeBetweenHelper(int min, int max)
sizeBetweenWithSamplingback to summary
public static SamplingAllocationStrategy sizeBetweenWithSampling(int min, int max, double getPermitsSamplingRate, double returnPermitsSamplingRate)

An SamplingAllocationStrategy that wraps a sizeBetween AllocationStrategy and samples calls to AllocationStrategy#getPermits(int) and AllocationStrategy#returnPermits(int).

Parameters
min:int

the minimum number of permits (see PoolBuilder#sizeBetween(int, int))

max:int

the maximum number of permits (see PoolBuilder#sizeBetween(int, int))

getPermitsSamplingRate:double

the sampling rate for AllocationStrategy#getPermits(int) calls (0.0d to 1.0d)

returnPermitsSamplingRate:double

the sampling rate for AllocationStrategy#returnPermits(int) calls (0.0d to 1.0d)

Returns:SamplingAllocationStrategy

a sampled AllocationStrategy that otherwise behaves like the PoolBuilder#sizeBetween(int, int) strategy

warmupParallelismback to summary
public int warmupParallelism()

Overrides default reactor.pool.AllocationStrategy.warmupParallelism.

Doc from reactor.pool.AllocationStrategy.warmupParallelism.

Return the concurrency level used when the allocator is subscribed to during the warmup phase, if any.

The number of resources created concurrently will not exceed the value returned by warmupParallelism(). If the concurrency level is set to 1, pre-allocation of resources will be performed sequentially by subscribing to the allocator one at a time. The process waits for a resource to be created before subscribing again to the allocator. This sequence continues until all pre-allocated resources have been successfully created.

Defaults to 1

Returns:int

The concurrency level used when the allocator is subscribed to during the warmup phase, must be positive, 1 by default

Annotations
@Override
withSamplingback to summary
public static SamplingAllocationStrategy withSampling(AllocationStrategy delegate, double getPermitsSamplingRate, double returnPermitsSamplingRate)

An AllocationStrategy that wraps a delegate and samples calls to AllocationStrategy#getPermits(int) and AllocationStrategy#returnPermits(int). Only a given percentage of calls are sampled, and these are stored as stacktraces in the sampling strategy. If a AllocationStrategy#returnPermits(int) call fails, a new IllegalArgumentException is thrown with a composite cause that represents all the sampled calls (see Exceptions#unwrapMultiple(Throwable)).

The two types of calls are sampled separately. The reservoir sampling algorithm is used to instantiate a sampling decision bitset over 100 slots, which are then applied live to windows of 100 calls.

Parameters
delegate:AllocationStrategy

the underlying AllocationStrategy

getPermitsSamplingRate:double

the sampling rate for AllocationStrategy#getPermits(int) calls (0.0d to 1.0d)

returnPermitsSamplingRate:double

the sampling rate for AllocationStrategy#returnPermits(int) calls (0.0d to 1.0d)

Returns:SamplingAllocationStrategy

a sampled AllocationStrategy that otherwise behaves like the delegate