Class SamplingAllocationStrategy
- All Implemented Interfaces:
AllocationStrategy
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
FieldsModifier and TypeFieldDescriptionfinal AllocationStrategyThe delegateAllocationStrategybacking this sampling strategy.final LinkedList<Throwable> The list of samples forgetPermits(int)calls, asThrowablethat trace back to the callers of the sampled calls.final doubleThe configured sampling rate forgetPermits(int)calls, as a double between 0d and 1d (percentage).final LinkedList<Throwable> The list of samples forreturnPermits(int)calls, asThrowablethat trace back to the callers of the sampled calls.final doubleThe configured sampling rate forreturnPermits(int)calls, as a double between 0d and 1d (percentage). -
Method Summary
Modifier and TypeMethodDescriptionintBest-effort peek at the state of the strategy which indicates roughly how many more resources can currently be allocated.intgetPermits(int desired) Try to get the permission to allocate adesiredpositive number of new resources.intintintReturn the minimum number of permits this strategy tries to maintain granted (reflecting a minimal size for the pool), or0for scale-to-zero.voidreturnPermits(int returned) Update the strategy to indicate that N resources were discarded from thePool, potentially leaving space for N new ones to be allocated.static SamplingAllocationStrategysizeBetweenWithSampling(int min, int max, double getPermitsSamplingRate, double returnPermitsSamplingRate) AnSamplingAllocationStrategythat wraps asizeBetweenAllocationStrategyand samples calls toAllocationStrategy.getPermits(int)andAllocationStrategy.returnPermits(int).intReturn the concurrency level used when the allocator is subscribed to during the warmup phase, if any.static SamplingAllocationStrategywithSampling(AllocationStrategy delegate, double getPermitsSamplingRate, double returnPermitsSamplingRate) AnAllocationStrategythat wraps adelegateand samples calls toAllocationStrategy.getPermits(int)andAllocationStrategy.returnPermits(int).
-
Field Details
-
delegate
The delegateAllocationStrategybacking this sampling strategy. -
gettingSamples
The list of samples forgetPermits(int)calls, asThrowablethat trace back to the callers of the sampled calls. -
gettingSamplingRate
public final double gettingSamplingRateThe configured sampling rate forgetPermits(int)calls, as a double between 0d and 1d (percentage). -
returningSamples
The list of samples forreturnPermits(int)calls, asThrowablethat trace back to the callers of the sampled calls. -
returningSamplingRate
public final double returningSamplingRateThe configured sampling rate forreturnPermits(int)calls, as a double between 0d and 1d (percentage).
-
-
Method Details
-
sizeBetweenWithSampling
public static SamplingAllocationStrategy sizeBetweenWithSampling(int min, int max, double getPermitsSamplingRate, double returnPermitsSamplingRate) AnSamplingAllocationStrategythat wraps asizeBetweenAllocationStrategyand samples calls toAllocationStrategy.getPermits(int)andAllocationStrategy.returnPermits(int).- Parameters:
min- the minimum number of permits (seePoolBuilder.sizeBetween(int, int))max- the maximum number of permits (seePoolBuilder.sizeBetween(int, int))getPermitsSamplingRate- the sampling rate forAllocationStrategy.getPermits(int)calls (0.0d to 1.0d)returnPermitsSamplingRate- the sampling rate forAllocationStrategy.returnPermits(int)calls (0.0d to 1.0d)- Returns:
- a sampled
AllocationStrategythat otherwise behaves like thePoolBuilder.sizeBetween(int, int)strategy
-
withSampling
public static SamplingAllocationStrategy withSampling(AllocationStrategy delegate, double getPermitsSamplingRate, double returnPermitsSamplingRate) AnAllocationStrategythat wraps adelegateand samples calls toAllocationStrategy.getPermits(int)andAllocationStrategy.returnPermits(int). Only a given percentage of calls are sampled, and these are stored as stacktraces in the sampling strategy. If aAllocationStrategy.returnPermits(int)call fails, a newIllegalArgumentExceptionis thrown with a composite cause that represents all the sampled calls (seeExceptions.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- the underlyingAllocationStrategygetPermitsSamplingRate- the sampling rate forAllocationStrategy.getPermits(int)calls (0.0d to 1.0d)returnPermitsSamplingRate- the sampling rate forAllocationStrategy.returnPermits(int)calls (0.0d to 1.0d)- Returns:
- a sampled
AllocationStrategythat otherwise behaves like thedelegate
-
getPermits
public int getPermits(int desired) Description copied from interface:AllocationStrategyTry to get the permission to allocate adesiredpositive 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 thandesired, 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 usingAllocationStrategy.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
desiredof zero.- Specified by:
getPermitsin interfaceAllocationStrategy- Parameters:
desired- the desired number of new resources- Returns:
- the actual number of new resources that MUST be created, can be 0 and can be more than
desired
-
returnPermits
public void returnPermits(int returned) Description copied from interface:AllocationStrategyUpdate the strategy to indicate that N resources were discarded from thePool, 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
IllegalArgumentExceptionif it can be determined the number of returned permits is not consistent with the strategy's limits and delivered permits.- Specified by:
returnPermitsin interfaceAllocationStrategy
-
estimatePermitCount
public int estimatePermitCount()Description copied from interface:AllocationStrategyBest-effort peek at the state of the strategy which indicates roughly how many more resources can currently be allocated. Should be paired withAllocationStrategy.getPermits(int)for an atomic permission.- Specified by:
estimatePermitCountin interfaceAllocationStrategy- Returns:
- an ESTIMATED count of how many more resources can currently be allocated
-
permitGranted
public int permitGranted()- Specified by:
permitGrantedin interfaceAllocationStrategy- Returns:
- a best estimate of the number of permits currently granted, between 0 and
Integer.MAX_VALUE
-
permitMinimum
public int permitMinimum()Description copied from interface:AllocationStrategyReturn the minimum number of permits this strategy tries to maintain granted (reflecting a minimal size for the pool), or0for scale-to-zero.- Specified by:
permitMinimumin interfaceAllocationStrategy- Returns:
- the minimum number of permits this strategy tries to maintain, or
0
-
permitMaximum
public int permitMaximum()- Specified by:
permitMaximumin interfaceAllocationStrategy- Returns:
- the maximum number of permits this strategy can grant in total, or
Integer.MAX_VALUEfor unbounded
-
warmupParallelism
public int warmupParallelism()Description copied from interface:AllocationStrategyReturn 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
- Specified by:
warmupParallelismin interfaceAllocationStrategy- Returns:
- The concurrency level used when the allocator is subscribed to during the warmup phase, must be positive,
1by default
-