HHH-14444 Avoid synchronization for single-tenant generation in PooledLoThreadLocalOptimizer

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
This commit is contained in:
Yoann Rodière 2021-02-08 13:23:58 +01:00 committed by Sanne Grinovero
parent 04a40f8397
commit 5c155f3f8d
1 changed files with 16 additions and 14 deletions

View File

@ -58,28 +58,30 @@ public class PooledLoThreadLocalOptimizer extends AbstractOptimizer {
public Serializable generate(AccessCallback callback) {
if ( callback.getTenantIdentifier() == null ) {
final GenerationState local = localAssignedIds.get();
if ( local.value != null && local.value.lt( local.upperLimitValue ) ) {
return local.value.makeValueThenIncrement();
}
return generate( local, callback );
}
synchronized (this) {
final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() );
if ( generationState.lastSourceValue == null
|| !generationState.value.lt( generationState.upperLimitValue )) {
generationState.lastSourceValue = callback.getNextValue();
generationState.upperLimitValue = generationState.lastSourceValue.copy().add( incrementSize );
generationState.value = generationState.lastSourceValue.copy();
// handle cases where initial-value is less that one (hsqldb for instance).
while (generationState.value.lt( 1 )) {
generationState.value.increment();
}
}
return generationState.value.makeValueThenIncrement();
return generate(generationState, callback);
}
}
private Serializable generate(GenerationState generationState, AccessCallback callback) {
if ( generationState.value == null
|| !generationState.value.lt( generationState.upperLimitValue ) ) {
generationState.lastSourceValue = callback.getNextValue();
generationState.upperLimitValue = generationState.lastSourceValue.copy().add( incrementSize );
generationState.value = generationState.lastSourceValue.copy();
// handle cases where initial-value is less that one (hsqldb for instance).
while ( generationState.value.lt( 1 ) ) {
generationState.value.increment();
}
}
return generationState.value.makeValueThenIncrement();
}
private Map<String, GenerationState> tenantSpecificState;
private final ThreadLocal<GenerationState> localAssignedIds = ThreadLocal.withInitial( GenerationState::new );