diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java index dca84bbd78e9..eab59166400a 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java @@ -12,7 +12,7 @@ import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.action.spi.Executable; import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.SessionImplementor; @@ -76,12 +76,14 @@ public final void beforeExecutions() throws CacheException { // bidirectional association and it is one of the earlier entity actions which actually updates // the database (this action is responsible for second-level cache invalidation only) if ( persister.hasCache() ) { - final CacheKey ck = session.generateCacheKey( + final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( key, - persister.getKeyType(), - persister.getRole() + persister, + session.getFactory(), + session.getTenantIdentifier() ); - final SoftLock lock = persister.getCacheAccessStrategy().lockItem( ck, null ); + final SoftLock lock = cache.lockItem( ck, null ); // the old behavior used key as opposed to getKey() afterTransactionProcess = new CacheCleanupProcess( key, persister, lock ); } @@ -127,12 +129,14 @@ protected final SessionImplementor getSession() { protected final void evict() throws CacheException { if ( persister.hasCache() ) { - final CacheKey ck = session.generateCacheKey( + final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( key, - persister.getKeyType(), - persister.getRole() + persister, + session.getFactory(), + session.getTenantIdentifier() ); - persister.getCacheAccessStrategy().remove( ck ); + cache.remove( ck ); } } @@ -169,12 +173,14 @@ private CacheCleanupProcess(Serializable key, CollectionPersister persister, Sof @Override public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { - final CacheKey ck = session.generateCacheKey( + final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( key, - persister.getKeyType(), - persister.getRole() + persister, + session.getFactory(), + session.getTenantIdentifier() ); - persister.getCacheAccessStrategy().unlockItem( ck, lock ); + cache.unlockItem( ck, lock ); } } @@ -191,8 +197,3 @@ protected EventSource eventSource() { } } - - - - - diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java index 27f32c00ac8e..2ac9736be879 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java @@ -10,7 +10,7 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.PersistenceContext; @@ -84,10 +84,11 @@ public void execute() throws HibernateException { version = persister.getVersion( instance ); } - final CacheKey ck; + final Object ck; if ( persister.hasCache() ) { - ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() ); - lock = persister.getCacheAccessStrategy().lockItem( ck, version ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() ); + lock = cache.lockItem( ck, version ); } else { ck = null; @@ -184,13 +185,16 @@ private void postCommitDelete(boolean success) { @Override public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws HibernateException { - if ( getPersister().hasCache() ) { - final CacheKey ck = getSession().generateCacheKey( + EntityPersister entityPersister = getPersister(); + if ( entityPersister.hasCache() ) { + EntityRegionAccessStrategy cache = entityPersister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( getId(), - getPersister().getIdentifierType(), - getPersister().getRootEntityName() + entityPersister, + session.getFactory(), + session.getTenantIdentifier() ); - getPersister().getCacheAccessStrategy().unlockItem( ck, lock ); + cache.unlockItem( ck, lock ); } postCommitDelete( success ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java index c927873a69d2..8b64f155c3b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java @@ -10,11 +10,13 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; +import org.hibernate.engine.spi.PersistenceContext; +import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.event.service.spi.EventListenerGroup; @@ -85,8 +87,8 @@ public void execute() throws HibernateException { if ( !veto ) { persister.insert( id, getState(), instance, session ); - - final EntityEntry entry = session.getPersistenceContext().getEntry( instance ); + PersistenceContext persistenceContext = session.getPersistenceContext(); + final EntityEntry entry = persistenceContext.getEntry( instance ); if ( entry == null ) { throw new AssertionFailure( "possible non-threadsafe access to session" ); } @@ -101,10 +103,10 @@ public void execute() throws HibernateException { entry.postUpdate( instance, getState(), version ); } - getSession().getPersistenceContext().registerInsertedKey( getPersister(), getId() ); + persistenceContext.registerInsertedKey( persister, getId() ); } - final SessionFactoryImplementor factory = getSession().getFactory(); + final SessionFactoryImplementor factory = session.getFactory(); if ( isCachePutEnabled( persister, session ) ) { final CacheEntry ce = persister.buildCacheEntry( @@ -114,12 +116,13 @@ public void execute() throws HibernateException { session ); cacheEntry = persister.getCacheEntryStructure().structure( ce ); - final CacheKey ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( id, persister, factory, session.getTenantIdentifier() ); final boolean put = cacheInsert( persister, ck ); if ( put && factory.getStatistics().isStatisticsEnabled() ) { - factory.getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() ); + factory.getStatisticsImplementor().secondLevelCachePut( cache.getRegion().getName() ); } } @@ -134,7 +137,7 @@ public void execute() throws HibernateException { markExecuted(); } - private boolean cacheInsert(EntityPersister persister, CacheKey ck) { + private boolean cacheInsert(EntityPersister persister, Object ck) { try { getSession().getEventListenerManager().cachePutStart(); return persister.getCacheAccessStrategy().insert( ck, cacheEntry, version ); @@ -207,24 +210,27 @@ private boolean preInsert() { public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws HibernateException { final EntityPersister persister = getPersister(); if ( success && isCachePutEnabled( persister, getSession() ) ) { - final CacheKey ck = getSession().generateCacheKey( getId(), persister.getIdentifierType(), persister.getRootEntityName() ); - final boolean put = cacheAfterInsert( persister, ck ); - - if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) { - getSession().getFactory().getStatisticsImplementor() - .secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + SessionFactoryImplementor sessionFactoryImplementor = session.getFactory(); + final Object ck = cache.generateCacheKey( getId(), persister, sessionFactoryImplementor, session.getTenantIdentifier() ); + final boolean put = cacheAfterInsert( cache, ck ); + + if ( put && sessionFactoryImplementor.getStatistics().isStatisticsEnabled() ) { + sessionFactoryImplementor.getStatisticsImplementor() + .secondLevelCachePut( cache.getRegion().getName() ); } } postCommitInsert( success ); } - private boolean cacheAfterInsert(EntityPersister persister, CacheKey ck) { + private boolean cacheAfterInsert(EntityRegionAccessStrategy cache, Object ck) { + final SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager(); try { - getSession().getEventListenerManager().cachePutStart(); - return persister.getCacheAccessStrategy().afterInsert( ck, cacheEntry, version ); + eventListenerManager.cachePutStart(); + return cache.afterInsert( ck, cacheEntry, version ); } finally { - getSession().getEventListenerManager().cachePutEnd(); + eventListenerManager.cachePutEnd(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java index 343d477db5ae..ea449655faa2 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java @@ -11,12 +11,13 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.spi.CachedNaturalIdValueSource; import org.hibernate.engine.spi.EntityEntry; +import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.Status; @@ -116,7 +117,7 @@ public void execute() throws HibernateException { final boolean veto = preUpdate(); - final SessionFactoryImplementor factory = getSession().getFactory(); + final SessionFactoryImplementor factory = session.getFactory(); Object previousVersion = this.previousVersion; if ( persister.isVersionPropertyGenerated() ) { // we need to grab the version value from the entity, otherwise @@ -125,14 +126,16 @@ public void execute() throws HibernateException { previousVersion = persister.getVersion( instance ); } - final CacheKey ck; + final Object ck; if ( persister.hasCache() ) { - ck = session.generateCacheKey( + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + ck = cache.generateCacheKey( id, - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + factory, + session.getTenantIdentifier() ); - lock = persister.getCacheAccessStrategy().lockItem( ck, previousVersion ); + lock = cache.lockItem( ck, previousVersion ); } else { ck = null; @@ -152,7 +155,7 @@ public void execute() throws HibernateException { ); } - final EntityEntry entry = getSession().getPersistenceContext().getEntry( instance ); + final EntityEntry entry = session.getPersistenceContext().getEntry( instance ); if ( entry == null ) { throw new AssertionFailure( "possible nonthreadsafe access to session" ); } @@ -212,7 +215,7 @@ public void execute() throws HibernateException { } } - private boolean cacheUpdate(EntityPersister persister, Object previousVersion, CacheKey ck) { + private boolean cacheUpdate(EntityPersister persister, Object previousVersion, Object ck) { try { getSession().getEventListenerManager().cachePutStart(); return persister.getCacheAccessStrategy().update( ck, cacheEntry, nextVersion, previousVersion ); @@ -307,34 +310,37 @@ protected boolean hasPostCommitEventListeners() { public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws CacheException { final EntityPersister persister = getPersister(); if ( persister.hasCache() ) { - - final CacheKey ck = getSession().generateCacheKey( + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( getId(), - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + session.getFactory(), + session.getTenantIdentifier() + ); - + if ( success && cacheEntry!=null /*!persister.isCacheInvalidationRequired()*/ ) { - final boolean put = cacheAfterUpdate( persister, ck ); + final boolean put = cacheAfterUpdate( cache, ck ); if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) { - getSession().getFactory().getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() ); + getSession().getFactory().getStatisticsImplementor().secondLevelCachePut( cache.getRegion().getName() ); } } else { - persister.getCacheAccessStrategy().unlockItem( ck, lock ); + cache.unlockItem( ck, lock ); } } postCommitUpdate( success ); } - private boolean cacheAfterUpdate(EntityPersister persister, CacheKey ck) { + private boolean cacheAfterUpdate(EntityRegionAccessStrategy cache, Object ck) { + SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager(); try { - getSession().getEventListenerManager().cachePutStart(); - return persister.getCacheAccessStrategy().afterUpdate( ck, cacheEntry, nextVersion, previousVersion, lock ); + eventListenerManager.cachePutStart(); + return cache.afterUpdate( ck, cacheEntry, nextVersion, previousVersion, lock ); } finally { - getSession().getEventListenerManager().cachePutEnd(); + eventListenerManager.cachePutEnd(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/CacheDataDescriptionImpl.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/CacheDataDescriptionImpl.java index 28610c21ef75..b849330f396e 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/CacheDataDescriptionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/CacheDataDescriptionImpl.java @@ -11,6 +11,7 @@ import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.mapping.Collection; import org.hibernate.mapping.PersistentClass; +import org.hibernate.type.Type; import org.hibernate.type.VersionType; /** @@ -22,19 +23,21 @@ public class CacheDataDescriptionImpl implements CacheDataDescription { private final boolean mutable; private final boolean versioned; private final Comparator versionComparator; + private final Type keyType; /** * Constructs a CacheDataDescriptionImpl instance. Generally speaking, code should use one of the * overloaded {@link #decode} methods rather than direct instantiation. - * * @param mutable Is the described data mutable? * @param versioned Is the described data versioned? * @param versionComparator The described data's version value comparator (if versioned). + * @param keyType */ - public CacheDataDescriptionImpl(boolean mutable, boolean versioned, Comparator versionComparator) { + public CacheDataDescriptionImpl(boolean mutable, boolean versioned, Comparator versionComparator, Type keyType) { this.mutable = mutable; this.versioned = versioned; this.versionComparator = versionComparator; + this.keyType = keyType; } @Override @@ -52,6 +55,11 @@ public Comparator getVersionComparator() { return versionComparator; } + @Override + public Type getKeyType() { + return keyType; + } + /** * Builds a CacheDataDescriptionImpl from the mapping model of an entity class. * @@ -65,8 +73,8 @@ public static CacheDataDescriptionImpl decode(PersistentClass model) { model.isVersioned(), model.isVersioned() ? ( (VersionType) model.getVersion().getType() ).getComparator() - : null - ); + : null, + model.getIdentifierProperty().getType()); } /** @@ -82,8 +90,8 @@ public static CacheDataDescriptionImpl decode(Collection model) { model.getOwner().isVersioned(), model.getOwner().isVersioned() ? ( (VersionType) model.getOwner().getVersion().getType() ).getComparator() - : null - ); + : null, + model.getKey().getType()); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java index 6e52e1546609..773ce619e63f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java @@ -10,7 +10,7 @@ import java.util.Set; import org.hibernate.boot.Metadata; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.event.service.spi.EventListenerRegistry; import org.hibernate.event.spi.EventSource; @@ -136,8 +136,16 @@ private void evictCache(Object entity, EntityPersister persister, EventSource se } private void evict(Serializable id, CollectionPersister collectionPersister, EventSource session) { - LOG.debug( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id ); - CacheKey key = session.generateCacheKey( id, collectionPersister.getKeyType(), collectionPersister.getRole() ); - collectionPersister.getCacheAccessStrategy().evict( key ); + if ( LOG.isDebugEnabled() ) { + LOG.debug( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id ); + } + CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy(); + Object key = cache.generateCacheKey( + id, + collectionPersister, + session.getFactory(), + session.getTenantIdentifier() + ); + cache.evict( key ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/DefaultCacheKeysFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/DefaultCacheKeysFactory.java new file mode 100644 index 000000000000..15aa4c6ce43d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/DefaultCacheKeysFactory.java @@ -0,0 +1,98 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.cache.internal; + +import org.hibernate.cache.spi.CacheKeysFactory; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.EntityPersister; + +/** + * Second level cache providers now have the option to use custom key implementations. + * This was done as the default key implementation is very generic and is quite + * a large object to allocate in large quantities at runtime. + * In some extreme cases, for example when the hit ratio is very low, this was making the efficiency + * penalty vs its benefits tradeoff questionable. + *

+ * Depending on configuration settings there might be opportunities to + * use simpler key implementations, for example when multi-tenancy is not being used to + * avoid the tenant identifier, or when a cache instance is entirely dedicated to a single type + * to use the primary id only, skipping the role or entity name. + *

+ * Even with multiple types sharing the same cache, their identifiers could be of the same + * {@link org.hibernate.type.Type}; in this case the cache container could + * use a single type reference to implement a custom equality function without having + * to look it up on each equality check: that's a small optimisation but the + * equality function is often invoked extremely frequently. + *

+ * Another reason is to make it more convenient to implement custom serialization protocols when the + * implementation supports clustering. + * + * @see org.hibernate.type.Type#getHashCode(Object, SessionFactoryImplementor) + * @see org.hibernate.type.Type#isEqual(Object, Object) + * @author Sanne Grinovero + * @since 5.0 + */ +public class DefaultCacheKeysFactory { + + public static Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return new OldCacheKeyImplementation( id, persister.getKeyType(), persister.getRole(), tenantIdentifier, factory ); + } + + public static Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return new OldCacheKeyImplementation( id, persister.getIdentifierType(), persister.getRootEntityName(), tenantIdentifier, factory ); + } + + public static Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return new OldNaturalIdCacheKey( naturalIdValues, persister.getPropertyTypes(), persister.getNaturalIdentifierProperties(), persister.getRootEntityName(), session ); + } + + public static Object getEntityId(Object cacheKey) { + return ((OldCacheKeyImplementation) cacheKey).getId(); + } + + public static Object getCollectionId(Object cacheKey) { + return ((OldCacheKeyImplementation) cacheKey).getId(); + } + + public static Object[] getNaturalIdValues(Object cacheKey) { + return ((OldNaturalIdCacheKey) cacheKey).getNaturalIdValues(); + } + + public static CacheKeysFactory INSTANCE = new CacheKeysFactory() { + @Override + public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object getEntityId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } + + @Override + public Object getCollectionId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } + }; +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/OldCacheKeyImplementation.java old mode 100755 new mode 100644 similarity index 75% rename from hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java rename to hibernate-core/src/main/java/org/hibernate/cache/internal/OldCacheKeyImplementation.java index f33d311af7ca..20098a1d8956 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/OldCacheKeyImplementation.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.cache.spi; +package org.hibernate.cache.internal; import java.io.Serializable; @@ -16,11 +16,15 @@ * Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite * keys which do not properly implement equals()/hashCode(). * + * This was named org.hibernate.cache.spi.CacheKey in Hibernate until version 5. + * Temporarily maintained as a reference while all components catch up with the refactoring to the caching interfaces. + * * @author Gavin King * @author Steve Ebersole */ -public class CacheKey implements Serializable { - private final Serializable key; +@Deprecated +final class OldCacheKeyImplementation implements Serializable { + private final Object id; private final Type type; private final String entityOrRoleName; private final String tenantId; @@ -37,13 +41,13 @@ public class CacheKey implements Serializable { * @param tenantId The tenant identifier associated this data. * @param factory The session factory for which we are caching */ - public CacheKey( - final Serializable id, + OldCacheKeyImplementation( + final Object id, final Type type, final String entityOrRoleName, final String tenantId, final SessionFactoryImplementor factory) { - this.key = id; + this.id = id; this.type = type; this.entityOrRoleName = entityOrRoleName; this.tenantId = tenantId; @@ -51,21 +55,13 @@ public CacheKey( } private int calculateHashCode(Type type, SessionFactoryImplementor factory) { - int result = type.getHashCode( key, factory ); + int result = type.getHashCode(id, factory ); result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0); return result; } - public Serializable getKey() { - return key; - } - - public String getEntityOrRoleName() { - return entityOrRoleName; - } - - public String getTenantId() { - return tenantId; + public Object getId() { + return id; } @Override @@ -76,13 +72,13 @@ public boolean equals(Object other) { if ( this == other ) { return true; } - if ( hashCode != other.hashCode() || !( other instanceof CacheKey ) ) { + if ( hashCode != other.hashCode() || !( other instanceof OldCacheKeyImplementation ) ) { //hashCode is part of this check since it is pre-calculated and hash must match for equals to be true return false; } - final CacheKey that = (CacheKey) other; + final OldCacheKeyImplementation that = (OldCacheKeyImplementation) other; return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName ) - && type.isEqual( key, that.key ) + && type.isEqual(id, that.id) && EqualsHelper.equals( tenantId, that.tenantId ); } @@ -94,6 +90,6 @@ public int hashCode() { @Override public String toString() { // Used to be required for OSCache - return entityOrRoleName + '#' + key.toString(); + return entityOrRoleName + '#' + id.toString(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/NaturalIdCacheKey.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/OldNaturalIdCacheKey.java similarity index 85% rename from hibernate-core/src/main/java/org/hibernate/cache/spi/NaturalIdCacheKey.java rename to hibernate-core/src/main/java/org/hibernate/cache/internal/OldNaturalIdCacheKey.java index eb1e2a70665d..e0aa87e596b8 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/NaturalIdCacheKey.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/OldNaturalIdCacheKey.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.cache.spi; +package org.hibernate.cache.internal; import java.io.IOException; import java.io.ObjectInputStream; @@ -15,17 +15,20 @@ import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.internal.util.ValueHolder; import org.hibernate.internal.util.compare.EqualsHelper; -import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.EntityType; import org.hibernate.type.Type; /** * Defines a key for caching natural identifier resolutions into the second level cache. * + * This was named org.hibernate.cache.spi.NaturalIdCacheKey in Hibernate until version 5. + * Temporarily maintained as a reference while all components catch up with the refactoring to the caching interfaces. + * * @author Eric Dalquist * @author Steve Ebersole */ -public class NaturalIdCacheKey implements Serializable { +@Deprecated +public class OldNaturalIdCacheKey implements Serializable { private final Serializable[] naturalIdValues; private final String entityName; private final String tenantId; @@ -35,25 +38,22 @@ public class NaturalIdCacheKey implements Serializable { /** * Construct a new key for a caching natural identifier resolutions into the second level cache. - * Note that an entity name should always be the root entity name, not a subclass entity name. - * * @param naturalIdValues The naturalIdValues associated with the cached data - * @param persister The persister for the entity + * @param propertyTypes + * @param naturalIdPropertyIndexes * @param session The originating session */ - public NaturalIdCacheKey( + public OldNaturalIdCacheKey( final Object[] naturalIdValues, - final EntityPersister persister, + Type[] propertyTypes, int[] naturalIdPropertyIndexes, final String entityName, final SessionImplementor session) { - this.entityName = persister.getRootEntityName(); + this.entityName = entityName; this.tenantId = session.getTenantIdentifier(); this.naturalIdValues = new Serializable[naturalIdValues.length]; final SessionFactoryImplementor factory = session.getFactory(); - final int[] naturalIdPropertyIndexes = persister.getNaturalIdentifierProperties(); - final Type[] propertyTypes = persister.getPropertyTypes(); final int prime = 31; int result = 1; @@ -88,7 +88,7 @@ private void initTransients() { public String initialize() { //Complex toString is needed as naturalIds for entities are not simply based on a single value like primary keys //the only same way to differentiate the keys is to included the disassembled values in the string. - final StringBuilder toStringBuilder = new StringBuilder( entityName ).append( "##NaturalId[" ); + final StringBuilder toStringBuilder = new StringBuilder().append( entityName ).append( "##NaturalId[" ); for ( int i = 0; i < naturalIdValues.length; i++ ) { toStringBuilder.append( naturalIdValues[i] ); if ( i + 1 < naturalIdValues.length ) { @@ -137,12 +137,12 @@ public boolean equals(Object o) { return true; } - if ( hashCode != o.hashCode() || !( o instanceof NaturalIdCacheKey ) ) { + if ( hashCode != o.hashCode() || !( o instanceof OldNaturalIdCacheKey ) ) { //hashCode is part of this check since it is pre-calculated and hash must match for equals to be true return false; } - final NaturalIdCacheKey other = (NaturalIdCacheKey) o; + final OldNaturalIdCacheKey other = (OldNaturalIdCacheKey) o; return EqualsHelper.equals( entityName, other.entityName ) && EqualsHelper.equals( tenantId, other.tenantId ) && Arrays.deepEquals( this.naturalIdValues, other.naturalIdValues ); diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/SimpleCacheKeysFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/SimpleCacheKeysFactory.java new file mode 100644 index 000000000000..831ccf8b9e7d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/SimpleCacheKeysFactory.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.cache.internal; + +import org.hibernate.cache.spi.CacheKeysFactory; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.EntityPersister; + +/** + * Factory that does not fill in the entityName or role + * + * @author Radim Vansa <rvansa@redhat.com> + */ +public class SimpleCacheKeysFactory implements CacheKeysFactory { + + public static CacheKeysFactory INSTANCE = new SimpleCacheKeysFactory(); + + @Override + public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return id; + } + + @Override + public Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return id; + } + + @Override + public Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + // natural ids always need to be wrapped + return new OldNaturalIdCacheKey(naturalIdValues, persister.getPropertyTypes(), persister.getNaturalIdentifierProperties(), null, session); + } + + @Override + public Object getEntityId(Object cacheKey) { + return cacheKey; + } + + @Override + public Object getCollectionId(Object cacheKey) { + return cacheKey; + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return ((OldNaturalIdCacheKey) cacheKey).getNaturalIdValues(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheDataDescription.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheDataDescription.java index 70b77582380e..eeecaaf9690b 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheDataDescription.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheDataDescription.java @@ -8,6 +8,8 @@ import java.util.Comparator; +import org.hibernate.type.Type; + /** * Describes attributes regarding the type of data to be cached. * @@ -37,4 +39,10 @@ public interface CacheDataDescription { * @return The comparator for versions, or {@code null} */ public Comparator getVersionComparator(); + + /** + * @return Type of the key that will be used as the key in the cache, or {@code null} if the natural comparison + * ({@link Object#hashCode()} and {@link Object#equals(Object)} methods should be used. + */ + Type getKeyType(); } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKeysFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKeysFactory.java new file mode 100644 index 000000000000..d9ce86fce57f --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKeysFactory.java @@ -0,0 +1,29 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.cache.spi; + +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.EntityPersister; + +/** + * @author Radim Vansa <rvansa@redhat.com> + */ +public interface CacheKeysFactory { + Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier); + + Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier); + + Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session); + + Object getEntityId(Object cacheKey); + + Object getCollectionId(Object cacheKey); + + Object[] getNaturalIdValues(Object cacheKey); +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/CollectionRegionAccessStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/CollectionRegionAccessStrategy.java index 7bf21b364c82..621dc3c992e0 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/CollectionRegionAccessStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/CollectionRegionAccessStrategy.java @@ -7,6 +7,8 @@ package org.hibernate.cache.spi.access; import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Contract for managing transactional and concurrent access to cached collection @@ -23,6 +25,25 @@ */ public interface CollectionRegionAccessStrategy extends RegionAccessStrategy { + /** + * To create instances of CollectionCacheKey for this region, Hibernate will invoke this method + * exclusively so that generated implementations can generate optimised keys. + * @param id the primary identifier of the Collection + * @param persister the persister for the type for which a key is being generated + * @param factory a reference to the current SessionFactory + * @param tenantIdentifier the tenant id, or null if multi-tenancy is not being used. + * @return a key which can be used to identify this collection on this same region + */ + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier); + + /** + * Performs reverse operation to {@link #generateCacheKey(Object, CollectionPersister, SessionFactoryImplementor, String)} + * + * @param cacheKey key previously returned from {@link #generateCacheKey(Object, CollectionPersister, SessionFactoryImplementor, String)} + * @return original key passed to {@link #generateCacheKey(Object, CollectionPersister, SessionFactoryImplementor, String)} + */ + public Object getCacheKeyId(Object cacheKey); + /** * Get the wrapped collection cache region * diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java index b63c85fa33c7..32c5382b94b2 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java @@ -8,6 +8,8 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Contract for managing transactional and concurrent access to cached entity @@ -25,7 +27,26 @@ * @author Gavin King * @author Steve Ebersole */ -public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ +public interface EntityRegionAccessStrategy extends RegionAccessStrategy { + + /** + * To create instances of EntityCacheKey for this region, Hibernate will invoke this method + * exclusively so that generated implementations can generate optimised keys. + * @param id the primary identifier of the entity + * @param persister the persister for the type for which a key is being generated + * @param factory a reference to the current SessionFactory + * @param tenantIdentifier the tenant id, or null if multi-tenancy is not being used. + * @return a key which can be used to identify this entity on this same region + */ + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier); + + /** + * Performs reverse operation to {@link #generateCacheKey(Object, EntityPersister, SessionFactoryImplementor, String)} + * + * @param cacheKey key previously returned from {@link #generateCacheKey(Object, EntityPersister, SessionFactoryImplementor, String)} + * @return original id passed to {@link #generateCacheKey(Object, EntityPersister, SessionFactoryImplementor, String)} + */ + public Object getCacheKeyId(Object cacheKey); /** * Get the wrapped entity cache region @@ -43,7 +64,7 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ * @param value The item * @param version The item's version value * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean insert(Object key, Object value, Object version) throws CacheException; @@ -56,7 +77,7 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ * @param value The item * @param version The item's version value * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean afterInsert(Object key, Object value, Object version) throws CacheException; @@ -70,7 +91,7 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ * @param currentVersion The item's current version value * @param previousVersion The item's previous version value * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException; @@ -85,7 +106,7 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ * @param previousVersion The item's previous version value * @param lock The lock previously obtained from {@link #lockItem} * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException; } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/NaturalIdRegionAccessStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/NaturalIdRegionAccessStrategy.java index 83e4c8871a7c..bbf60eb8c9a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/NaturalIdRegionAccessStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/NaturalIdRegionAccessStrategy.java @@ -8,6 +8,8 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.NaturalIdRegion; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Contract for managing transactional and concurrent access to cached naturalId @@ -36,6 +38,24 @@ */ public interface NaturalIdRegionAccessStrategy extends RegionAccessStrategy { + /** + * To create instances of NaturalIdCacheKey for this region, Hibernate will invoke this method + * exclusively so that generated implementations can generate optimised keys. + * @param naturalIdValues the sequence of values which unequivocally identifies a cached element on this region + * @param persister the persister of the element being cached + * @param session + * @return a key which can be used to identify this an element unequivocally on this same region + */ + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session); + + /** + * Performs reverse operation to {@link #generateCacheKey(Object[], EntityPersister, SessionImplementor)}, returning + * the original naturalIdValues. + * @param cacheKey key returned from {@link #generateCacheKey(Object[], EntityPersister, SessionImplementor)} + * @return the sequence of values which unequivocally identifies a cached element on this region + */ + public Object[] getNaturalIdValues(Object cacheKey); + /** * Get the wrapped naturalId cache region * @@ -51,7 +71,7 @@ public interface NaturalIdRegionAccessStrategy extends RegionAccessStrategy { * @param key The item key * @param value The item * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean insert(Object key, Object value) throws CacheException; @@ -63,7 +83,7 @@ public interface NaturalIdRegionAccessStrategy extends RegionAccessStrategy { * @param key The item key * @param value The item * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean afterInsert(Object key, Object value) throws CacheException; @@ -75,7 +95,7 @@ public interface NaturalIdRegionAccessStrategy extends RegionAccessStrategy { * @param key The item key * @param value The item * @return Were the contents of the cache actual changed by this operation? - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + * @throws CacheException Propagated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean update(Object key, Object value) throws CacheException; diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/RegionAccessStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/RegionAccessStrategy.java index a06b22c4be6d..42e4b8d71095 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/RegionAccessStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/RegionAccessStrategy.java @@ -6,6 +6,7 @@ */ package org.hibernate.cache.spi.access; + import org.hibernate.cache.CacheException; /** @@ -14,6 +15,7 @@ * @author Gail Badner */ public interface RegionAccessStrategy { + /** * Attempt to retrieve an object from the cache. Mainly used in attempting * to resolve entities/collections from the second level cache. diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/CacheHelper.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/CacheHelper.java index 884b4736fe2c..6eb0ff52dad7 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/CacheHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/CacheHelper.java @@ -8,45 +8,33 @@ import java.io.Serializable; -import org.hibernate.cache.spi.CacheKey; -import org.hibernate.cache.spi.NaturalIdCacheKey; -import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.RegionAccessStrategy; +import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionImplementor; /** * @author Steve Ebersole + * @author Sanne Grinovero */ public final class CacheHelper { + private CacheHelper() { } public static Serializable fromSharedCache( - SessionImplementor session, - NaturalIdCacheKey cacheKey, - NaturalIdRegionAccessStrategy cacheAccessStrategy) { - return fromSharedCache( session, (Object) cacheKey, cacheAccessStrategy ); - } - - private static Serializable fromSharedCache( SessionImplementor session, Object cacheKey, RegionAccessStrategy cacheAccessStrategy) { + final SessionEventListenerManager eventListenerManager = session.getEventListenerManager(); Serializable cachedValue = null; + eventListenerManager.cacheGetStart(); try { - session.getEventListenerManager().cacheGetStart(); cachedValue = (Serializable) cacheAccessStrategy.get( cacheKey, session.getTimestamp() ); } finally { - session.getEventListenerManager().cacheGetEnd( cachedValue != null ); + eventListenerManager.cacheGetEnd( cachedValue != null ); } return cachedValue; } - public static Serializable fromSharedCache( - SessionImplementor session, - CacheKey cacheKey, - RegionAccessStrategy cacheAccessStrategy) { - return fromSharedCache( session, (Object) cacheKey, cacheAccessStrategy ); - } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java index f3a2be5ae78d..0ef204f9c841 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/NaturalIdXrefDelegate.java @@ -15,7 +15,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.hibernate.AssertionFailure; -import org.hibernate.cache.spi.NaturalIdCacheKey; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -110,12 +109,12 @@ public Object[] removeNaturalIdCrossReference(EntityPersister persister, Seriali if ( persister.hasNaturalIdCache() ) { final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister .getNaturalIdCacheAccessStrategy(); - final NaturalIdCacheKey naturalIdCacheKey = new NaturalIdCacheKey( naturalIdValues, persister, session() ); + final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session() ); naturalIdCacheAccessStrategy.evict( naturalIdCacheKey ); if ( sessionCachedNaturalIdValues != null && !Arrays.equals( sessionCachedNaturalIdValues, naturalIdValues ) ) { - final NaturalIdCacheKey sessionNaturalIdCacheKey = new NaturalIdCacheKey( sessionCachedNaturalIdValues, persister, session() ); + final Object sessionNaturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( sessionCachedNaturalIdValues, persister, session() ); naturalIdCacheAccessStrategy.evict( sessionNaturalIdCacheKey ); } } @@ -239,9 +238,9 @@ public Serializable findCachedNaturalIdResolution(EntityPersister persister, Obj } // Try resolution from second-level cache - final NaturalIdCacheKey naturalIdCacheKey = new NaturalIdCacheKey( naturalIdValues, persister, session() ); - final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy(); + final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session() ); + pk = CacheHelper.fromSharedCache( session(), naturalIdCacheKey, naturalIdCacheAccessStrategy ); // Found in second-level cache, store in session cache diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index ad511753d424..becb76f8b4a8 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -31,7 +31,6 @@ import org.hibernate.PersistentObjectException; import org.hibernate.TransientObjectException; import org.hibernate.action.spi.AfterTransactionCompletionProcess; -import org.hibernate.cache.spi.NaturalIdCacheKey; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.collection.spi.PersistentCollection; @@ -1738,7 +1737,7 @@ private void managedSharedCacheEntries( Object[] previousNaturalIdValues, CachedNaturalIdValueSource source) { final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy(); - final NaturalIdCacheKey naturalIdCacheKey = new NaturalIdCacheKey( naturalIdValues, persister, session ); + final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session ); final SessionFactoryImplementor factory = session.getFactory(); @@ -1793,7 +1792,7 @@ public void doAfterTransactionCompletion(boolean success, SessionImplementor ses break; } case UPDATE: { - final NaturalIdCacheKey previousCacheKey = new NaturalIdCacheKey( previousNaturalIdValues, persister, session ); + final Object previousCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( previousNaturalIdValues, persister, session ); if ( naturalIdCacheKey.equals( previousCacheKey ) ) { // prevent identical re-caching, solves HHH-7309 return; @@ -1877,7 +1876,7 @@ public void removeSharedNaturalIdCrossReference(EntityPersister persister, Seria persister = locateProperPersister( persister ); final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy(); - final NaturalIdCacheKey naturalIdCacheKey = new NaturalIdCacheKey( naturalIdValues, persister, session ); + final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey( naturalIdValues, persister, session ); naturalIdCacheAccessStrategy.evict( naturalIdCacheKey ); // if ( sessionCachedNaturalIdValues != null diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/TwoPhaseLoad.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/TwoPhaseLoad.java index 4781c6838193..21bf12fec1b2 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/TwoPhaseLoad.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/TwoPhaseLoad.java @@ -13,11 +13,12 @@ import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; +import org.hibernate.engine.spi.SessionEventListenerManager; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.Status; @@ -182,7 +183,8 @@ private static void doInitializeEntity( final Object version = Versioning.getVersion( hydratedState, persister ); final CacheEntry entry = persister.buildCacheEntry( entity, hydratedState, version, session ); - final CacheKey cacheKey = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object cacheKey = cache.generateCacheKey( id, persister, factory, session.getTenantIdentifier() ); // explicit handling of caching for rows just inserted and then somehow forced to be read // from the database *within the same transaction*. usually this is done by @@ -191,7 +193,7 @@ private static void doInitializeEntity( // // we need to be careful not to clobber the lock here in the cache so that it can be rolled back if need be if ( session.getPersistenceContext().wasInsertedDuringTransaction( persister, id ) ) { - persister.getCacheAccessStrategy().update( + cache.update( cacheKey, persister.getCacheEntryStructure().structure( entry ), version, @@ -199,9 +201,10 @@ private static void doInitializeEntity( ); } else { + final SessionEventListenerManager eventListenerManager = session.getEventListenerManager(); try { - session.getEventListenerManager().cachePutStart(); - final boolean put = persister.getCacheAccessStrategy().putFromLoad( + eventListenerManager.cachePutStart(); + final boolean put = cache.putFromLoad( cacheKey, persister.getCacheEntryStructure().structure( entry ), session.getTimestamp(), @@ -210,11 +213,11 @@ private static void doInitializeEntity( ); if ( put && factory.getStatistics().isStatisticsEnabled() ) { - factory.getStatisticsImplementor().secondLevelCachePut( persister.getCacheAccessStrategy().getRegion().getName() ); + factory.getStatisticsImplementor().secondLevelCachePut( cache.getRegion().getName() ); } } finally { - session.getEventListenerManager().cachePutEnd(); + eventListenerManager.cachePutEnd(); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java b/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java index 0e4399f82cf6..af8624032a4f 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java @@ -17,7 +17,7 @@ import org.hibernate.CacheMode; import org.hibernate.EntityMode; import org.hibernate.HibernateException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.entry.CollectionCacheEntry; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.CollectionEntry; @@ -332,7 +332,13 @@ private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersiste } final CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister ); - final CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() ); + final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object cacheKey = cache.generateCacheKey( + lce.getKey(), + persister, + session.getFactory(), + session.getTenantIdentifier() + ); boolean isPutFromLoad = true; if ( persister.getElementType().isAssociationType() ) { @@ -349,7 +355,7 @@ private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersiste if (isPutFromLoad) { try { session.getEventListenerManager().cachePutStart(); - final boolean put = persister.getCacheAccessStrategy().putFromLoad( + final boolean put = cache.putFromLoad( cacheKey, persister.getCacheEntryStructure().structure( entry ), session.getTimestamp(), diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java index 0a721b4c301b..b28669d43be5 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/BatchFetchQueue.java @@ -14,7 +14,8 @@ import java.util.Map.Entry; import org.hibernate.EntityMode; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.internal.CacheHelper; import org.hibernate.internal.CoreLogging; @@ -201,13 +202,16 @@ public Serializable[] getEntityBatch( } private boolean isCached(EntityKey entityKey, EntityPersister persister) { + final SessionImplementor session = context.getSession(); if ( context.getSession().getCacheMode().isGetEnabled() && persister.hasCache() ) { - final CacheKey key = context.getSession().generateCacheKey( + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object key = cache.generateCacheKey( entityKey.getIdentifier(), - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + session.getFactory(), + session.getTenantIdentifier() ); - return CacheHelper.fromSharedCache( context.getSession(), key, persister.getCacheAccessStrategy() ) != null; + return CacheHelper.fromSharedCache( session, key, cache ) != null; } return false; } @@ -314,14 +318,18 @@ else if ( !isCached( ce.getLoadedKey(), collectionPersister ) ) { } private boolean isCached(Serializable collectionKey, CollectionPersister persister) { - if ( context.getSession().getCacheMode().isGetEnabled() && persister.hasCache() ) { - CacheKey cacheKey = context.getSession().generateCacheKey( + SessionImplementor session = context.getSession(); + if ( session.getCacheMode().isGetEnabled() && persister.hasCache() ) { + CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + Object cacheKey = cache.generateCacheKey( collectionKey, - persister.getKeyType(), - persister.getRole() + persister, + session.getFactory(), + session.getTenantIdentifier() ); - return CacheHelper.fromSharedCache( context.getSession(), cacheKey, persister.getCacheAccessStrategy() ) != null; + return CacheHelper.fromSharedCache( session, cacheKey, cache ) != null; } return false; } + } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 68d2e2cb0b7d..5bbe3c475867 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -35,7 +35,6 @@ import org.hibernate.Transaction; import org.hibernate.TypeHelper; import org.hibernate.UnknownProfileException; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; @@ -47,7 +46,6 @@ import org.hibernate.procedure.ProcedureCall; import org.hibernate.resource.transaction.TransactionCoordinator; import org.hibernate.stat.SessionStatistics; -import org.hibernate.type.Type; /** * This class is meant to be extended. @@ -97,11 +95,6 @@ public EntityKey generateEntityKey(Serializable id, EntityPersister persister) { return sessionImplementor.generateEntityKey( id, persister ); } - @Override - public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) { - return sessionImplementor.generateCacheKey( id, type, entityOrRoleName ); - } - @Override public Interceptor getInterceptor() { return sessionImplementor.getInterceptor(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java index 9d40e98005d2..fabdf879bf18 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryDelegatingImpl.java @@ -6,29 +6,19 @@ */ package org.hibernate.engine.spi; +import javax.naming.NamingException; +import javax.naming.Reference; import java.sql.Connection; import java.util.Map; import java.util.Properties; import java.util.Set; -import javax.naming.NamingException; -import javax.naming.Reference; -import org.hibernate.Cache; -import org.hibernate.CustomEntityDirtinessStrategy; -import org.hibernate.EntityNameResolver; -import org.hibernate.HibernateException; -import org.hibernate.Interceptor; -import org.hibernate.MappingException; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.SessionFactoryObserver; -import org.hibernate.StatelessSession; -import org.hibernate.StatelessSessionBuilder; -import org.hibernate.TypeHelper; +import org.hibernate.*; import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.spi.QueryCache; import org.hibernate.cache.spi.Region; import org.hibernate.cache.spi.UpdateTimestampsCache; +import org.hibernate.cache.spi.access.RegionAccessStrategy; import org.hibernate.cfg.Settings; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.dialect.Dialect; @@ -292,11 +282,21 @@ public Region getSecondLevelCacheRegion(String regionName) { return delegate.getSecondLevelCacheRegion( regionName ); } + @Override + public RegionAccessStrategy getSecondLevelCacheRegionAccessStrategy(String regionName) { + return delegate.getSecondLevelCacheRegionAccessStrategy(regionName); + } + @Override public Region getNaturalIdCacheRegion(String regionName) { return delegate.getNaturalIdCacheRegion( regionName ); } + @Override + public RegionAccessStrategy getNaturalIdCacheRegionAccessStrategy(String regionName) { + return delegate.getNaturalIdCacheRegionAccessStrategy(regionName); + } + @Override public Map getAllSecondLevelCacheRegions() { return delegate.getAllSecondLevelCacheRegions(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java index da77d0ecac1a..2a29d8213665 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionFactoryImplementor.java @@ -21,6 +21,7 @@ import org.hibernate.cache.spi.QueryCache; import org.hibernate.cache.spi.Region; import org.hibernate.cache.spi.UpdateTimestampsCache; +import org.hibernate.cache.spi.access.RegionAccessStrategy; import org.hibernate.cfg.Settings; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.dialect.Dialect; @@ -189,6 +190,13 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory { * @return The region */ Region getSecondLevelCacheRegion(String regionName); + + /** + * Get access strategy to second-level cache region + * @param regionName + * @return + */ + RegionAccessStrategy getSecondLevelCacheRegionAccessStrategy(String regionName); /** * Get a named naturalId cache region @@ -198,6 +206,13 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory { */ Region getNaturalIdCacheRegion(String regionName); + /** + * Get access strategy to naturalId cache region + * @param regionName + * @return + */ + RegionAccessStrategy getNaturalIdCacheRegionAccessStrategy(String regionName); + /** * Get a map of all the second level cache regions currently maintained in * this session factory. The map is structured with the region name as the diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java index fe1c164f7151..9ed6dfa3b9a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java @@ -20,7 +20,6 @@ import org.hibernate.SQLQuery; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; @@ -64,17 +63,6 @@ public interface SessionImplementor extends Serializable, LobCreationContext { */ EntityKey generateEntityKey(Serializable id, EntityPersister persister); - /** - * Hide the changing requirements of cache key creation. - * - * @param id The entity identifier or collection key. - * @param type The type - * @param entityOrRoleName The entity name or collection role. - * - * @return The cache key - */ - CacheKey generateCacheKey(Serializable id, final Type type, final String entityOrRoleName); - /** * Retrieves the interceptor currently in use by this event source. * diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractLockUpgradeEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractLockUpgradeEventListener.java index 1d8a5851e3d1..66169048d299 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractLockUpgradeEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractLockUpgradeEventListener.java @@ -9,7 +9,7 @@ import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.ObjectDeletedException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.Status; @@ -17,7 +17,6 @@ import org.hibernate.internal.CoreLogging; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.pretty.MessageHelper; - import org.jboss.logging.Logger; /** @@ -62,18 +61,16 @@ protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOpt ); } - final SoftLock lock; - final CacheKey ck; - if ( persister.hasCache() ) { - ck = source.generateCacheKey( entry.getId(), persister.getIdentifierType(), persister.getRootEntityName() ); - lock = persister.getCacheAccessStrategy().lockItem( ck, entry.getVersion() ); - } - else { - ck = null; - lock = null; - } - + final boolean cachingEnabled = persister.hasCache(); + SoftLock lock = null; + Object ck = null; try { + if ( cachingEnabled ) { + EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + ck = cache.generateCacheKey( entry.getId(), persister, source.getFactory(), source.getTenantIdentifier() ); + lock = cache.lockItem( ck, entry.getVersion() ); + } + if ( persister.isVersioned() && requestedLockMode == LockMode.FORCE ) { // todo : should we check the current isolation mode explicitly? Object nextVersion = persister.forceVersionIncrement( @@ -89,7 +86,7 @@ protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOpt finally { // the database now holds a lock + the object is flushed from the cache, // so release the soft lock - if ( persister.hasCache() ) { + if ( cachingEnabled ) { persister.getCacheAccessStrategy().unlockItem( ck, lock ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java index 38cc1b518b5e..2118ecd20bc6 100755 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultInitializeCollectionEventListener.java @@ -9,7 +9,7 @@ import java.io.Serializable; import org.hibernate.HibernateException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.entry.CollectionCacheEntry; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.internal.CacheHelper; @@ -116,17 +116,18 @@ private boolean initializeCollectionFromCache( } final SessionFactoryImplementor factory = source.getFactory(); - final CacheKey ck = source.generateCacheKey( id, persister.getKeyType(), persister.getRole() ); + final CollectionRegionAccessStrategy cacheAccessStrategy = persister.getCacheAccessStrategy(); + final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() ); final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() ); if ( factory.getStatistics().isStatisticsEnabled() ) { if ( ce == null ) { factory.getStatisticsImplementor() - .secondLevelCacheMiss( persister.getCacheAccessStrategy().getRegion().getName() ); + .secondLevelCacheMiss( cacheAccessStrategy.getRegion().getName() ); } else { factory.getStatisticsImplementor() - .secondLevelCacheHit( persister.getCacheAccessStrategy().getRegion().getName() ); + .secondLevelCacheHit( cacheAccessStrategy.getRegion().getName() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java index 11d4984761e9..135ea67787e0 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java @@ -14,7 +14,7 @@ import org.hibernate.PersistentObjectException; import org.hibernate.TypeMismatchException; import org.hibernate.WrongClassException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl; @@ -357,12 +357,14 @@ protected Object lockAndLoad( final LoadEventListener.LoadType options, final SessionImplementor source) { SoftLock lock = null; - final CacheKey ck; + final Object ck; + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); if ( persister.hasCache() ) { - ck = source.generateCacheKey( + ck = cache.generateCacheKey( event.getEntityId(), - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + source.getFactory(), + source.getTenantIdentifier() ); lock = persister.getCacheAccessStrategy().lockItem( ck, null ); } @@ -376,7 +378,7 @@ protected Object lockAndLoad( } finally { if ( persister.hasCache() ) { - persister.getCacheAccessStrategy().unlockItem( ck, lock ); + cache.unlockItem( ck, lock ); } } @@ -572,22 +574,24 @@ protected Object loadFromSecondLevelCache( } final SessionFactoryImplementor factory = source.getFactory(); - final CacheKey ck = source.generateCacheKey( + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( event.getEntityId(), - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + factory, + source.getTenantIdentifier() ); final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() ); if ( factory.getStatistics().isStatisticsEnabled() ) { if ( ce == null ) { factory.getStatisticsImplementor().secondLevelCacheMiss( - persister.getCacheAccessStrategy().getRegion().getName() + cache.getRegion().getName() ); } else { factory.getStatisticsImplementor().secondLevelCacheHit( - persister.getCacheAccessStrategy().getRegion().getName() + cache.getRegion().getName() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java index fc01c483681b..6ca71fe98da9 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java @@ -13,7 +13,7 @@ import org.hibernate.HibernateException; import org.hibernate.PersistentObjectException; import org.hibernate.UnresolvableObjectException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.engine.internal.Cascade; import org.hibernate.engine.internal.CascadePoint; import org.hibernate.engine.spi.CascadingActions; @@ -136,12 +136,14 @@ public void onRefresh(RefreshEvent event, Map refreshedAlready) { } if ( persister.hasCache() ) { - final CacheKey ck = source.generateCacheKey( + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + Object ck = cache.generateCacheKey( id, - persister.getIdentifierType(), - persister.getRootEntityName() + persister, + source.getFactory(), + source.getTenantIdentifier() ); - persister.getCacheAccessStrategy().evict( ck ); + cache.evict( ck ); } evictCachedCollections( persister, id, source.getFactory() ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java index cc330e1f5aa8..96131d991c99 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java @@ -26,7 +26,6 @@ import org.hibernate.SharedSessionContract; import org.hibernate.Transaction; import org.hibernate.boot.spi.SessionFactoryOptions; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; @@ -58,7 +57,6 @@ import org.hibernate.resource.transaction.TransactionCoordinatorBuilder.TransactionCoordinatorOptions; import org.hibernate.resource.transaction.spi.TransactionStatus; import org.hibernate.service.ServiceRegistry; -import org.hibernate.type.Type; /** * Functionality common to stateless and stateful sessions @@ -336,11 +334,6 @@ public EntityKey generateEntityKey(Serializable id, EntityPersister persister) { return new EntityKey( id, persister ); } - @Override - public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) { - return new CacheKey( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() ); - } - private transient JdbcConnectionAccess jdbcConnectionAccess; @Override diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CacheImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/CacheImpl.java index 94769b542ad9..dfac9b16a3de 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CacheImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CacheImpl.java @@ -14,11 +14,12 @@ import org.hibernate.HibernateException; import org.hibernate.boot.spi.SessionFactoryOptions; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.QueryCache; import org.hibernate.cache.spi.Region; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.UpdateTimestampsCache; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.engine.spi.CacheImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.util.collections.CollectionHelper; @@ -73,8 +74,14 @@ public boolean containsEntity(Class entityClass, Serializable identifier) { @Override public boolean containsEntity(String entityName, Serializable identifier) { EntityPersister p = sessionFactory.getEntityPersister( entityName ); - return p.hasCache() && - p.getCacheAccessStrategy().getRegion().contains( buildCacheKey( identifier, p ) ); + if ( p.hasCache() ) { + EntityRegionAccessStrategy cache = p.getCacheAccessStrategy(); + Object key = cache.generateCacheKey( identifier, p, sessionFactory, null ); // have to assume non tenancy + return cache.getRegion().contains( key ); + } + else { + return false; + } } @Override @@ -92,20 +99,12 @@ public void evictEntity(String entityName, Serializable identifier) { MessageHelper.infoString( p, identifier, sessionFactory ) ); } - p.getCacheAccessStrategy().evict( buildCacheKey( identifier, p ) ); + EntityRegionAccessStrategy cache = p.getCacheAccessStrategy(); + Object key = cache.generateCacheKey( identifier, p, sessionFactory, null ); // have to assume non tenancy + cache.evict( key ); } } - private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) { - return new CacheKey( - identifier, - p.getIdentifierType(), - p.getRootEntityName(), - null, // have to assume non tenancy - sessionFactory - ); - } - @Override public void evictEntityRegion(Class entityClass) { evictEntityRegion( entityClass.getName() ); @@ -155,8 +154,14 @@ public void evictNaturalIdRegions() { @Override public boolean containsCollection(String role, Serializable ownerIdentifier) { CollectionPersister p = sessionFactory.getCollectionPersister( role ); - return p.hasCache() && - p.getCacheAccessStrategy().getRegion().contains( buildCacheKey( ownerIdentifier, p ) ); + if ( p.hasCache() ) { + CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy(); + Object key = cache.generateCacheKey( ownerIdentifier, p, sessionFactory, null ); // have to assume non tenancy + return cache.getRegion().contains( key ); + } + else { + return false; + } } @Override @@ -169,21 +174,12 @@ public void evictCollection(String role, Serializable ownerIdentifier) { MessageHelper.collectionInfoString( p, ownerIdentifier, sessionFactory ) ); } - CacheKey cacheKey = buildCacheKey( ownerIdentifier, p ); - p.getCacheAccessStrategy().evict( cacheKey ); + CollectionRegionAccessStrategy cache = p.getCacheAccessStrategy(); + Object key = cache.generateCacheKey( ownerIdentifier, p, sessionFactory, null ); // have to assume non tenancy + cache.evict( key ); } } - private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) { - return new CacheKey( - ownerIdentifier, - p.getKeyType(), - p.getRole(), - null, // have to assume non tenancy - sessionFactory - ); - } - @Override public void evictCollectionRegion(String role) { CollectionPersister p = sessionFactory.getCollectionPersister( role ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 5ae0a321cebf..a99d0e222bee 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import org.hibernate.cache.spi.access.RegionAccessStrategy; import org.jboss.logging.Logger; import org.hibernate.AssertionFailure; @@ -200,6 +201,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { private final transient TypeResolver typeResolver; private final transient TypeHelper typeHelper; private final transient SessionFactoryOptions sessionFactoryOptions; + private final transient Map cacheAccessStrategiesMap = new HashMap(); public SessionFactoryImpl(final MetadataImplementor metadata, SessionFactoryOptions options) { LOG.debug( "Building session factory" ); @@ -319,7 +321,6 @@ public MetadataImplementor getMetadata() { // todo : similar for CollectionPersister/CollectionMetadata this.entityPersisters = new HashMap(); - Map cacheAccessStrategiesMap = new HashMap(); Map inFlightClassMetadataMap = new HashMap(); this.entityProxyInterfaceMap = CollectionHelper.concurrentMap( metadata.getEntityBindings().size() ); for ( final PersistentClass model : metadata.getEntityBindings() ) { @@ -429,6 +430,7 @@ public MetadataImplementor getMetadata() { roles.add( persister.getRole() ); } } + this.collectionMetadata = Collections.unmodifiableMap( tmpCollectionMetadata ); for ( Map.Entry> entityToCollectionRoleMapEntry : inFlightEntityToCollectionRoleMap.entrySet() ) { @@ -1119,10 +1121,20 @@ public Region getSecondLevelCacheRegion(String regionName) { return cacheAccess.getSecondLevelCacheRegion( regionName ); } + @Override + public RegionAccessStrategy getSecondLevelCacheRegionAccessStrategy(String regionName) { + return cacheAccessStrategiesMap.get(regionName); + } + public Region getNaturalIdCacheRegion(String regionName) { return cacheAccess.getNaturalIdCacheRegion( regionName ); } + @Override + public RegionAccessStrategy getNaturalIdCacheRegionAccessStrategy(String regionName) { + return cacheAccessStrategiesMap.get(regionName); + } + @SuppressWarnings( {"unchecked"}) public Map getAllSecondLevelCacheRegions() { return cacheAccess.getAllSecondLevelCacheRegions(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index 404b572d1923..9c2c42507284 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -28,7 +28,7 @@ import org.hibernate.StatelessSession; import org.hibernate.Transaction; import org.hibernate.UnresolvableObjectException; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.internal.SessionEventListenerManagerImpl; import org.hibernate.engine.internal.StatefulPersistenceContext; @@ -270,8 +270,9 @@ public void refresh(String entityName, Object entity, LockMode lockMode) { // } if ( persister.hasCache() ) { - final CacheKey ck = generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() ); - persister.getCacheAccessStrategy().evict( ck ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( id, persister, getFactory(), getTenantIdentifier() ); + cache.evict( ck ); } String previousFetchProfile = this.getLoadQueryInfluencers().getInternalFetchProfile(); Object result = null; diff --git a/hibernate-core/src/main/java/org/hibernate/loader/Loader.java b/hibernate-core/src/main/java/org/hibernate/loader/Loader.java index 84c01c97fff1..51bc9b9a2b01 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/Loader.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/Loader.java @@ -35,6 +35,7 @@ import org.hibernate.cache.spi.FilterKey; import org.hibernate.cache.spi.QueryCache; import org.hibernate.cache.spi.QueryKey; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl; import org.hibernate.collection.spi.PersistentCollection; @@ -1620,15 +1621,14 @@ private Object instanceNotYetLoaded( // see if the entity defines reference caching, and if so use the cached reference (if one). if ( session.getCacheMode().isGetEnabled() && persister.canUseReferenceCacheEntries() ) { - final Object cachedEntry = CacheHelper.fromSharedCache( - session, - session.generateCacheKey( - key.getIdentifier(), - persister.getEntityMetamodel().getEntityType(), - key.getEntityName() - ), - persister.getCacheAccessStrategy() - ); + final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( + key.getIdentifier(), + persister, + session.getFactory(), + session.getTenantIdentifier() + ); + final Object cachedEntry = CacheHelper.fromSharedCache( session, ck, cache ); if ( cachedEntry != null ) { CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( cachedEntry, factory ); return ( (ReferenceCacheEntryImpl) entry ).getReference(); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 964b32e22003..327ff5c19797 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -36,7 +36,6 @@ import org.hibernate.bytecode.instrumentation.spi.FieldInterceptor; import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer; import org.hibernate.bytecode.spi.EntityInstrumentationMetadata; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.entry.CacheEntry; @@ -906,8 +905,9 @@ public Object initializeLazyProperty(String fieldName, Object entity, SessionImp } if ( session.getCacheMode().isGetEnabled() && hasCache() ) { - final CacheKey cacheKey = session.generateCacheKey( id, getIdentifierType(), getRootEntityName() ); - final Object ce = CacheHelper.fromSharedCache( session, cacheKey, getCacheAccessStrategy() ); + final EntityRegionAccessStrategy cache = getCacheAccessStrategy(); + final Object cacheKey = cache.generateCacheKey(id, this, session.getFactory(), session.getTenantIdentifier() ); + final Object ce = CacheHelper.fromSharedCache( session, cacheKey, cache ); if ( ce != null ) { final CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure( ce, factory ); if ( !cacheEntry.areLazyPropertiesUnfetched() ) { @@ -4269,7 +4269,8 @@ public Boolean isTransient(Object entity, SessionImplementor session) throws Hib // check to see if it is in the second-level cache if ( session.getCacheMode().isGetEnabled() && hasCache() ) { - final CacheKey ck = session.generateCacheKey( id, getIdentifierType(), getRootEntityName() ); + final EntityRegionAccessStrategy cache = getCacheAccessStrategy(); + final Object ck = cache.generateCacheKey( id, this, session.getFactory(), session.getTenantIdentifier() ); final Object ce = CacheHelper.fromSharedCache( session, ck, getCacheAccessStrategy() ); if ( ce != null ) { return Boolean.FALSE; diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentNaturalIdCacheStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentNaturalIdCacheStatisticsImpl.java index db14beb0c18d..7eedbe4676f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentNaturalIdCacheStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentNaturalIdCacheStatisticsImpl.java @@ -13,8 +13,8 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import org.hibernate.cache.spi.NaturalIdCacheKey; import org.hibernate.cache.spi.Region; +import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.stat.NaturalIdCacheStatistics; /** @@ -25,6 +25,7 @@ public class ConcurrentNaturalIdCacheStatisticsImpl extends CategorizedStatistics implements NaturalIdCacheStatistics { private static final long serialVersionUID = 1L; private final transient Region region; + private final transient NaturalIdRegionAccessStrategy accessStrategy; private final AtomicLong hitCount = new AtomicLong(); private final AtomicLong missCount = new AtomicLong(); private final AtomicLong putCount = new AtomicLong(); @@ -35,15 +36,17 @@ public class ConcurrentNaturalIdCacheStatisticsImpl extends CategorizedStatistic private final Lock readLock; private final Lock writeLock; + { final ReadWriteLock lock = new ReentrantReadWriteLock(); this.readLock = lock.readLock(); this.writeLock = lock.writeLock(); } - ConcurrentNaturalIdCacheStatisticsImpl(Region region) { + ConcurrentNaturalIdCacheStatisticsImpl(Region region, NaturalIdRegionAccessStrategy accessStrategy) { super( region.getName() ); this.region = region; + this.accessStrategy = accessStrategy; } @Override @@ -126,8 +129,8 @@ public long getSizeInMemory() { public Map getEntries() { final Map map = new HashMap(); for ( Object o : this.region.toMap().entrySet() ) { - final Map.Entry me = (Map.Entry) o; - map.put( ( (NaturalIdCacheKey) me.getKey() ).getNaturalIdValues(), me.getValue() ); + Map.Entry me = (Map.Entry) o; + map.put( accessStrategy.getNaturalIdValues(me.getKey()), me.getValue() ); } return map; } diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentSecondLevelCacheStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentSecondLevelCacheStatisticsImpl.java index d7292d231f2a..a6bce5e32b49 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentSecondLevelCacheStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentSecondLevelCacheStatisticsImpl.java @@ -7,12 +7,12 @@ package org.hibernate.stat.internal; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; -import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.Region; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.stat.SecondLevelCacheStatistics; /** @@ -22,13 +22,19 @@ */ public class ConcurrentSecondLevelCacheStatisticsImpl extends CategorizedStatistics implements SecondLevelCacheStatistics { private final transient Region region; + private final transient EntityRegionAccessStrategy entityRegionAccessStrategy; + private final transient CollectionRegionAccessStrategy collectionRegionAccessStrategy; private AtomicLong hitCount = new AtomicLong(); private AtomicLong missCount = new AtomicLong(); private AtomicLong putCount = new AtomicLong(); - ConcurrentSecondLevelCacheStatisticsImpl(Region region) { + ConcurrentSecondLevelCacheStatisticsImpl(Region region, + EntityRegionAccessStrategy entityRegionAccessStrategy, + CollectionRegionAccessStrategy collectionRegionAccessStrategy) { super( region.getName() ); this.region = region; + this.entityRegionAccessStrategy = entityRegionAccessStrategy; + this.collectionRegionAccessStrategy = collectionRegionAccessStrategy; } public long getHitCount() { @@ -57,10 +63,17 @@ public long getSizeInMemory() { public Map getEntries() { Map map = new HashMap(); - Iterator iter = region.toMap().entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry me = (Map.Entry) iter.next(); - map.put(((CacheKey) me.getKey()).getKey(), me.getValue()); + for (Object o : region.toMap().entrySet()) { + Map.Entry me = (Map.Entry) o; + Object id; + if (entityRegionAccessStrategy != null) { + id = entityRegionAccessStrategy.getCacheKeyId(me.getKey()); + } else if (collectionRegionAccessStrategy != null) { + id = collectionRegionAccessStrategy.getCacheKeyId(me.getKey()); + } else { + id = me.getKey(); + } + map.put(id, me.getValue()); } return map; } diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentStatisticsImpl.java index b9b6e87deefd..16b5478fb6f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/ConcurrentStatisticsImpl.java @@ -11,6 +11,10 @@ import java.util.concurrent.atomic.AtomicLong; import org.hibernate.cache.spi.Region; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; +import org.hibernate.cache.spi.access.RegionAccessStrategy; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.ArrayHelper; @@ -299,7 +303,10 @@ public NaturalIdCacheStatistics getNaturalIdCacheStatistics(String regionName) { if ( region == null ) { return null; } - nics = new ConcurrentNaturalIdCacheStatisticsImpl( region ); + NaturalIdRegionAccessStrategy accessStrategy + = (NaturalIdRegionAccessStrategy) sessionFactory.getNaturalIdCacheRegionAccessStrategy(regionName); + + nics = new ConcurrentNaturalIdCacheStatisticsImpl( region, accessStrategy ); ConcurrentNaturalIdCacheStatisticsImpl previous; if ( ( previous = (ConcurrentNaturalIdCacheStatisticsImpl) naturalIdCacheStatistics.putIfAbsent( regionName, nics @@ -328,7 +335,16 @@ public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionNam if ( region == null ) { return null; } - slcs = new ConcurrentSecondLevelCacheStatisticsImpl( region ); + RegionAccessStrategy accessStrategy = sessionFactory.getSecondLevelCacheRegionAccessStrategy(regionName); + + EntityRegionAccessStrategy entityRegionAccessStrategy + = accessStrategy instanceof EntityRegionAccessStrategy ? + (EntityRegionAccessStrategy) accessStrategy : null; + CollectionRegionAccessStrategy collectionRegionAccessStrategy + = accessStrategy instanceof CollectionRegionAccessStrategy ? + (CollectionRegionAccessStrategy) accessStrategy : null; + + slcs = new ConcurrentSecondLevelCacheStatisticsImpl( region, entityRegionAccessStrategy, collectionRegionAccessStrategy ); ConcurrentSecondLevelCacheStatisticsImpl previous; if ( ( previous = (ConcurrentSecondLevelCacheStatisticsImpl) secondLevelCacheStatistics.putIfAbsent( regionName, slcs diff --git a/hibernate-core/src/test/java/org/hibernate/cache/spi/NaturalIdCacheKeyTest.java b/hibernate-core/src/test/java/org/hibernate/cache/spi/NaturalIdCacheKeyTest.java index cd9e92612561..8cc1ef75c2f3 100644 --- a/hibernate-core/src/test/java/org/hibernate/cache/spi/NaturalIdCacheKeyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/cache/spi/NaturalIdCacheKeyTest.java @@ -6,18 +6,13 @@ */ package org.hibernate.cache.spi; -import static junit.framework.Assert.assertEquals; -import static org.junit.Assert.assertArrayEquals; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; +import org.hibernate.cache.internal.OldNaturalIdCacheKey; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.persister.entity.EntityPersister; @@ -26,6 +21,13 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class NaturalIdCacheKeyTest { @Test public void testSerializationRoundTrip() throws Exception { @@ -58,15 +60,15 @@ public Object answer(InvocationOnMock invocation) throws Throwable { return invocation.getArguments()[0]; } }); - - final NaturalIdCacheKey key = new NaturalIdCacheKey(new Object[] {"a", "b", "c"}, entityPersister, sessionImplementor); - + + final OldNaturalIdCacheKey key = (OldNaturalIdCacheKey) DefaultCacheKeysFactory.createNaturalIdKey( new Object[] {"a", "b", "c"}, entityPersister, sessionImplementor ); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(key); final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); - final NaturalIdCacheKey keyClone = (NaturalIdCacheKey)ois.readObject(); + final OldNaturalIdCacheKey keyClone = (OldNaturalIdCacheKey) ois.readObject(); assertEquals(key, keyClone); assertEquals(key.hashCode(), keyClone.hashCode()); diff --git a/hibernate-core/src/test/java/org/hibernate/test/filter/DynamicFilterTest.java b/hibernate-core/src/test/java/org/hibernate/test/filter/DynamicFilterTest.java index 8a3ae668bc4e..2357c7071627 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/filter/DynamicFilterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/filter/DynamicFilterTest.java @@ -20,7 +20,7 @@ import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.cache.spi.CacheKey; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.entry.CollectionCacheEntry; import org.hibernate.cfg.AvailableSettings; import org.hibernate.criterion.DetachedCriteria; @@ -100,12 +100,14 @@ public void testSecondLevelCachedCollectionsFiltering() { Hibernate.initialize( sp.getOrders() ); CollectionPersister persister = sessionFactory().getCollectionPersister( Salesperson.class.getName() + ".orders" ); assertTrue( "No cache for collection", persister.hasCache() ); - CacheKey cacheKey = ( (SessionImplementor) session ).generateCacheKey( + CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy(); + Object cacheKey = cache.generateCacheKey( testData.steveId, - persister.getKeyType(), - persister.getRole() + persister, + sessionFactory(), + session.getTenantIdentifier() ); - CollectionCacheEntry cachedData = ( CollectionCacheEntry ) persister.getCacheAccessStrategy().get( cacheKey, ts ); + CollectionCacheEntry cachedData = ( CollectionCacheEntry ) cache.get( cacheKey, ts ); assertNotNull( "collection was not in cache", cachedData ); session.close(); @@ -114,14 +116,15 @@ public void testSecondLevelCachedCollectionsFiltering() { ts = ( ( SessionImplementor ) session ).getTimestamp(); session.enableFilter( "fulfilledOrders" ).setParameter( "asOfDate", testData.lastMonth.getTime() ); sp = ( Salesperson ) session.createQuery( "from Salesperson as s where s.id = :id" ) - .setLong( "id", testData.steveId ) - .uniqueResult(); + .setLong( "id", testData.steveId ) + .uniqueResult(); assertEquals( "Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size() ); - CacheKey cacheKey2 = ( (SessionImplementor) session ).generateCacheKey( + Object cacheKey2 = cache.generateCacheKey( testData.steveId, - persister.getKeyType(), - persister.getRole() + persister, + sessionFactory(), + session.getTenantIdentifier() ); CollectionCacheEntry cachedData2 = ( CollectionCacheEntry ) persister.getCacheAccessStrategy().get( cacheKey2, ts ); assertNotNull( "collection no longer in cache!", cachedData2 ); diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareCollectionRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareCollectionRegionAccessStrategy.java index e22c9b5d1ebb..ffa8c52fef85 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareCollectionRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareCollectionRegionAccessStrategy.java @@ -7,11 +7,13 @@ package org.hibernate.cache.ehcache.internal.nonstop; import net.sf.ehcache.constructs.nonstop.NonStopCacheException; - import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Implementation of {@link CollectionRegionAccessStrategy} that handles {@link NonStopCacheException} using @@ -158,4 +160,13 @@ public void unlockRegion(SoftLock lock) throws CacheException { } } + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareEntityRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareEntityRegionAccessStrategy.java index b54154faba84..7371faaf8998 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareEntityRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareEntityRegionAccessStrategy.java @@ -7,11 +7,13 @@ package org.hibernate.cache.ehcache.internal.nonstop; import net.sf.ehcache.constructs.nonstop.NonStopCacheException; - import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Implementation of {@link EntityRegionAccessStrategy} that handles {@link net.sf.ehcache.constructs.nonstop.NonStopCacheException} using @@ -203,4 +205,14 @@ public boolean update(Object key, Object value, Object currentVersion, Object pr return false; } } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareNaturalIdRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareNaturalIdRegionAccessStrategy.java index f259b2370ab2..67d3283830b6 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareNaturalIdRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareNaturalIdRegionAccessStrategy.java @@ -7,11 +7,13 @@ package org.hibernate.cache.ehcache.internal.nonstop; import net.sf.ehcache.constructs.nonstop.NonStopCacheException; - import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Implementation of {@link NaturalIdRegionAccessStrategy} that handles {@link NonStopCacheException} using @@ -202,4 +204,13 @@ public void unlockRegion(SoftLock lock) throws CacheException { } } + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session ); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/AbstractReadWriteEhcacheAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/AbstractReadWriteEhcacheAccessStrategy.java index a17c67e13e05..192647e3efe2 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/AbstractReadWriteEhcacheAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/AbstractReadWriteEhcacheAccessStrategy.java @@ -16,7 +16,6 @@ import org.hibernate.cache.ehcache.EhCacheMessageLogger; import org.hibernate.cache.ehcache.internal.regions.EhcacheTransactionalDataRegion; import org.hibernate.cache.spi.access.SoftLock; - import org.jboss.logging.Logger; /** @@ -27,8 +26,7 @@ * @author Chris Dennis * @author Alex Snaps */ -abstract class AbstractReadWriteEhcacheAccessStrategy +abstract class AbstractReadWriteEhcacheAccessStrategy extends AbstractEhcacheAccessStrategy { private static final EhCacheMessageLogger LOG = Logger.getMessageLogger( diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheCollectionRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheCollectionRegionAccessStrategy.java index 3d8170fb2712..b7278bad9d16 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheCollectionRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheCollectionRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheCollectionRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Ehcache specific non-strict read/write collection region access strategy @@ -79,4 +82,14 @@ public void unlockItem(Object key, SoftLock lock) throws CacheException { public void remove(Object key) throws CacheException { region().remove( key ); } + + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheEntityRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheEntityRegionAccessStrategy.java index abe9ee21b680..54e5af723acd 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheEntityRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheEntityRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheEntityRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific non-strict read/write entity region access strategy @@ -118,4 +121,14 @@ public boolean afterUpdate(Object key, Object value, Object currentVersion, Obje public void remove(Object key) throws CacheException { region().remove( key ); } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheNaturalIdRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheNaturalIdRegionAccessStrategy.java index a25baea452bb..2999c3d3a39f 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheNaturalIdRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheNaturalIdRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheNaturalIdRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific non-strict read/write NaturalId region access strategy @@ -116,4 +119,14 @@ public boolean afterUpdate(Object key, Object value, SoftLock lock) throws Cache public void remove(Object key) throws CacheException { region().remove( key ); } + + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheCollectionRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheCollectionRegionAccessStrategy.java index 08b1870758a1..7de3b272eddc 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheCollectionRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheCollectionRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheCollectionRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Ehcache specific read-only collection region access strategy @@ -68,4 +71,14 @@ public SoftLock lockItem(Object key, Object version) throws UnsupportedOperation @Override public void unlockItem(Object key, SoftLock lock) throws CacheException { } + + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheEntityRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheEntityRegionAccessStrategy.java index c7d0fd050644..43d16d3f7071 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheEntityRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheEntityRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheEntityRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific read-only entity region access strategy @@ -110,4 +113,14 @@ public boolean afterUpdate(Object key, Object value, Object currentVersion, Obje throws UnsupportedOperationException { throw new UnsupportedOperationException( "Can't write to a readonly object" ); } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheNaturalIdRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheNaturalIdRegionAccessStrategy.java index fd51e20ca433..35926666b9e9 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheNaturalIdRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadOnlyEhcacheNaturalIdRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheNaturalIdRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific read-only NaturalId region access strategy @@ -109,4 +112,14 @@ public boolean update(Object key, Object value) throws UnsupportedOperationExcep public boolean afterUpdate(Object key, Object value, SoftLock lock) throws UnsupportedOperationException { throw new UnsupportedOperationException( "Can't write to a readonly object" ); } + + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheCollectionRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheCollectionRegionAccessStrategy.java index 02dd3106ed11..2402e372f325 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheCollectionRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheCollectionRegionAccessStrategy.java @@ -8,8 +8,11 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.ehcache.internal.regions.EhcacheCollectionRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Ehcache specific read/write collection region access strategy @@ -35,4 +38,15 @@ public ReadWriteEhcacheCollectionRegionAccessStrategy(EhcacheCollectionRegion re public CollectionRegion getRegion() { return region(); } + + + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheEntityRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheEntityRegionAccessStrategy.java index d887bab7d4c2..a691e2bc9a06 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheEntityRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheEntityRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheEntityRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific read/write entity region access strategy @@ -117,4 +120,14 @@ public boolean afterUpdate(Object key, Object value, Object currentVersion, Obje region().writeUnlock( key ); } } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheNaturalIdRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheNaturalIdRegionAccessStrategy.java index d40da2186e28..61fcb06a7dc8 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheNaturalIdRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/ReadWriteEhcacheNaturalIdRegionAccessStrategy.java @@ -9,9 +9,12 @@ import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheNaturalIdRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Ehcache specific read/write NaturalId region access strategy @@ -115,4 +118,14 @@ public boolean afterUpdate(Object key, Object value, SoftLock lock) throws Cache region().writeUnlock( key ); } } + + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheCollectionRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheCollectionRegionAccessStrategy.java index dddec56ef49d..c23efe9a9419 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheCollectionRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheCollectionRegionAccessStrategy.java @@ -8,13 +8,15 @@ import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; - import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheCollectionRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * JTA CollectionRegionAccessStrategy. @@ -100,4 +102,13 @@ public void unlockItem(Object key, SoftLock lock) throws CacheException { // no-op } + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheEntityRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheEntityRegionAccessStrategy.java index 82f3072cdf32..1cc4a2e504ac 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheEntityRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheEntityRegionAccessStrategy.java @@ -8,13 +8,15 @@ import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; - import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheEntityRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * JTA EntityRegionAccessStrategy. @@ -136,4 +138,14 @@ public boolean update( throw new CacheException( e ); } } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheNaturalIdRegionAccessStrategy.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheNaturalIdRegionAccessStrategy.java index 1d5fa9a195e9..a840c2325034 100644 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheNaturalIdRegionAccessStrategy.java +++ b/hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/TransactionalEhcacheNaturalIdRegionAccessStrategy.java @@ -8,13 +8,15 @@ import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; - import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.ehcache.internal.regions.EhcacheNaturalIdRegion; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * JTA NaturalIdRegionAccessStrategy. @@ -133,4 +135,13 @@ public boolean update(Object key, Object value) throws CacheException { } } + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java index b88131817fed..75fec1936c94 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/InfinispanRegionFactory.java @@ -6,6 +6,21 @@ */ package org.hibernate.cache.infinispan; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.hibernate.MultiTenancyStrategy; import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.collection.CollectionRegionImpl; @@ -19,7 +34,10 @@ import org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup; import org.hibernate.cache.infinispan.util.CacheCommandFactory; import org.hibernate.cache.infinispan.util.Caches; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; +import org.hibernate.cache.internal.SimpleCacheKeysFactory; import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.NaturalIdRegion; @@ -46,20 +64,6 @@ import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.TimeUnit; - /** * A {@link RegionFactory} for Infinispan-backed cache * regions. @@ -215,6 +219,7 @@ public class InfinispanRegionFactory implements RegionFactory { private org.infinispan.transaction.lookup.TransactionManagerLookup transactionManagerlookup; private List regionNames = new ArrayList(); + private SessionFactoryOptions settings; /** * Create a new instance using the default configuration. @@ -239,8 +244,8 @@ public CollectionRegion buildCollectionRegion( if ( log.isDebugEnabled() ) { log.debug( "Building collection cache region [" + regionName + "]" ); } - final AdvancedCache cache = getCache( regionName, COLLECTION_KEY, properties ); - final CollectionRegionImpl region = new CollectionRegionImpl( cache, regionName, metadata, this ); + final AdvancedCache cache = getCache( regionName, COLLECTION_KEY, properties, metadata); + final CollectionRegionImpl region = new CollectionRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory() ); startRegion( region, regionName ); return region; } @@ -249,10 +254,10 @@ public CollectionRegion buildCollectionRegion( public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException { if ( log.isDebugEnabled() ) { - log.debugf( "Building entity cache region [%s] (mutable=%s, versioned=%s)", regionName, metadata.isMutable(), metadata.isVersioned()); + log.debugf("Building entity cache region [%s] (mutable=%s, versioned=%s)", regionName, metadata.isMutable(), metadata.isVersioned()); } - final AdvancedCache cache = getCache( regionName, metadata.isMutable() ? ENTITY_KEY : IMMUTABLE_ENTITY_KEY, properties ); - final EntityRegionImpl region = new EntityRegionImpl( cache, regionName, metadata, this ); + final AdvancedCache cache = getCache( regionName, metadata.isMutable() ? ENTITY_KEY : IMMUTABLE_ENTITY_KEY, properties, metadata ); + final EntityRegionImpl region = new EntityRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory() ); startRegion( region, regionName ); return region; } @@ -261,10 +266,10 @@ public EntityRegion buildEntityRegion(String regionName, Properties properties, public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException { if ( log.isDebugEnabled() ) { - log.debug( "Building natural id cache region [" + regionName + "]" ); + log.debug("Building natural id cache region [" + regionName + "]"); } - final AdvancedCache cache = getCache( regionName, NATURAL_ID_KEY, properties ); - final NaturalIdRegionImpl region = new NaturalIdRegionImpl( cache, regionName, metadata, this ); + final AdvancedCache cache = getCache( regionName, NATURAL_ID_KEY, properties, metadata); + final NaturalIdRegionImpl region = new NaturalIdRegionImpl( cache, regionName, metadata, this, buildCacheKeysFactory()); startRegion( region, regionName ); return region; } @@ -281,7 +286,7 @@ public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties cacheName = regionName; } - final AdvancedCache cache = getCache( cacheName, QUERY_KEY, properties ); + final AdvancedCache cache = getCache( cacheName, QUERY_KEY, properties, null); final QueryResultsRegionImpl region = new QueryResultsRegionImpl( cache, regionName, this ); startRegion( region, regionName ); return region; @@ -293,7 +298,7 @@ public TimestampsRegion buildTimestampsRegion(String regionName, Properties prop if ( log.isDebugEnabled() ) { log.debug( "Building timestamps cache region [" + regionName + "]" ); } - final AdvancedCache cache = getCache( regionName, TIMESTAMPS_KEY, properties ); + final AdvancedCache cache = getCache( regionName, TIMESTAMPS_KEY, properties, null); final TimestampsRegionImpl region = createTimestampsRegion( cache, regionName ); startRegion( region, regionName ); return region; @@ -301,7 +306,7 @@ public TimestampsRegion buildTimestampsRegion(String regionName, Properties prop protected TimestampsRegionImpl createTimestampsRegion( AdvancedCache cache, String regionName) { - if ( Caches.isClustered( cache ) ) { + if ( Caches.isClustered(cache) ) { return new ClusteredTimestampsRegionImpl( cache, regionName, this ); } else { @@ -309,6 +314,14 @@ protected TimestampsRegionImpl createTimestampsRegion( } } + private CacheKeysFactory buildCacheKeysFactory() { + if (settings.getMultiTenancyStrategy() != MultiTenancyStrategy.NONE) { + return DefaultCacheKeysFactory.INSTANCE; + } else { + return SimpleCacheKeysFactory.INSTANCE; + } + } + @Override public boolean isMinimalPutsEnabledByDefault() { return true; @@ -338,6 +351,7 @@ public void start(SessionFactoryOptions settings, Properties properties) throws try { transactionManagerlookup = createTransactionManagerLookup( settings, properties ); manager = createCacheManager( properties ); + this.settings = settings; initGenericDataTypeOverrides(); final Enumeration keys = properties.propertyNames(); while ( keys.hasMoreElements() ) { @@ -543,7 +557,7 @@ private void defineGenericDataTypeCacheConfigurations(Properties properties) { } } - private AdvancedCache getCache(String regionName, String typeKey, Properties properties) { + private AdvancedCache getCache(String regionName, String typeKey, Properties properties, CacheDataDescription metadata) { TypeOverrides regionOverride = typeOverrides.get( regionName ); if ( !definedConfigurations.contains( regionName ) ) { final String templateCacheName; @@ -577,6 +591,13 @@ private AdvancedCache getCache(String regionName, String typeKey, Properties pro // Apply overrides typeOverrides.get( typeKey ).applyTo( builder ); } + // with multi-tenancy the keys will be wrapped + if (settings.getMultiTenancyStrategy() == MultiTenancyStrategy.NONE) { + // the keys may not define hashCode/equals correctly (e.g. arrays) + if (metadata != null && metadata.getKeyType() != null) { + builder.dataContainer().keyEquivalence(new TypeEquivalance(metadata.getKeyType())); + } + } // Configure transaction manager configureTransactionManager( builder, templateCacheName, properties ); // Define configuration diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java new file mode 100644 index 000000000000..82bd5639fc98 --- /dev/null +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/TypeEquivalance.java @@ -0,0 +1,40 @@ +package org.hibernate.cache.infinispan; + +import org.hibernate.type.Type; +import org.infinispan.commons.equivalence.Equivalence; + +/** + * @author Radim Vansa <rvansa@redhat.com> + */ +public class TypeEquivalance implements Equivalence { + private final Type type; + + public TypeEquivalance(Type type) { + this.type = type; + } + + @Override + public int hashCode(Object o) { + return type.getHashCode(o); + } + + @Override + public boolean equals(Object x, Object y) { + return type.isEqual(x, y); + } + + @Override + public String toString(Object o) { + return String.valueOf(o); + } + + @Override + public boolean isComparable(Object o) { + return true; // cannot guess from the type + } + + @Override + public int compare(Object x, Object y) { + return type.compare(x, y); + } +} diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java index 9ab7ac06536d..87a6d6be9c1d 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/PutFromLoadValidator.java @@ -6,23 +6,24 @@ */ package org.hibernate.cache.infinispan.access; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +import java.util.HashSet; +import java.util.Iterator; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.Set; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Lock; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; import java.util.concurrent.locks.ReentrantLock; -import javax.transaction.SystemException; -import javax.transaction.Transaction; -import javax.transaction.TransactionManager; import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.InfinispanRegionFactory; - import org.infinispan.AdvancedCache; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; import org.infinispan.manager.EmbeddedCacheManager; /** @@ -40,9 +41,9 @@ *
  • Call {@link #registerPendingPut(Object)}
  • *
  • Read the database
  • *
  • Call {@link #acquirePutFromLoadLock(Object)} - *
  • if above returns false, the thread should not cache the data; - * only if above returns true, put data in the cache and...
  • - *
  • then call {@link #releasePutFromLoadLock(Object)}
  • + *
  • if above returns null, the thread should not cache the data; + * only if above returns instance of AcquiredLock, put data in the cache and...
  • + *
  • then call {@link #releasePutFromLoadLock(Object, Lock)}
  • * *

    *

    @@ -52,15 +53,19 @@ * call *

    *

      - *
    • {@link #invalidateKey(Object)} (for a single key invalidation)
    • + *
    • {@link #beginInvalidatingKey(Object)} (for a single key invalidation)
    • *
    • or {@link #invalidateRegion()} (for a general invalidation all pending puts)
    • *
    + * After transaction commit (when the DB is updated) {@link #endInvalidatingKey(Object)} should + * be called in order to allow further attempts to cache entry. *

    *

    *

    * This class also supports the concept of "naked puts", which are calls to - * {@link #acquirePutFromLoadLock(Object)} without a preceding {@link #registerPendingPut(Object)} - * call. + * {@link #acquirePutFromLoadLock(Object)} without a preceding {@link #registerPendingPut(Object)}. + * Besides not acquiring lock in {@link #registerPendingPut(Object)} this can happen when collection + * elements are loaded after the collection has not been found in the cache, where the elements + * don't have their own table but can be listed as 'select ... from Element where collection_id = ...'. *

    * * @author Brian Stansberry @@ -88,26 +93,15 @@ public class PutFromLoadValidator { */ private final ConcurrentMap pendingPuts; - private final ConcurrentMap recentRemovals = new ConcurrentHashMap(); - /** - * List of recent removals. Used to ensure we don't leak memory via the recentRemovals map - */ - private final List removalsQueue = new LinkedList(); /** - * The time when the first element in removalsQueue will expire. No reason to do housekeeping on - * the queue before this time. - */ - private volatile long earliestRemovalTimestamp; - /** - * Lock controlling access to removalsQueue - */ - private final Lock removalsLock = new ReentrantLock(); - - /** - * The time of the last call to regionRemoved(), plus NAKED_PUT_INVALIDATION_PERIOD. All naked + * The time of the last call to {@link #invalidateRegion()}, plus NAKED_PUT_INVALIDATION_PERIOD. All naked * puts will be rejected until the current time is greater than this value. + * NOTE: update only through {@link #invalidationUpdater}! */ - private volatile long invalidationTimestamp; + private volatile long invalidationTimestamp = Long.MIN_VALUE; + + private static final AtomicLongFieldUpdater invalidationUpdater + = AtomicLongFieldUpdater.newUpdater(PutFromLoadValidator.class, "invalidationTimestamp"); /** * Creates a new put from load validator instance. @@ -127,111 +121,138 @@ public PutFromLoadValidator(AdvancedCache cache) { * {@link #registerPendingPut(Object) pre-registered} (aka a "naked put") * will return false. */ - public PutFromLoadValidator( - AdvancedCache cache, - long nakedPutInvalidationPeriod) { - this( - cache.getCacheManager(), cache.getTransactionManager(), + public PutFromLoadValidator(AdvancedCache cache, long nakedPutInvalidationPeriod) { + this(cache, cache.getCacheManager(), cache.getTransactionManager(), nakedPutInvalidationPeriod ); } /** * Creates a new put from load validator instance. - * - * @param cacheManager where to find a cache to store pending put information - * @param tm transaction manager - * @param nakedPutInvalidationPeriod Period (in ms) after a removal during which a call to - * {@link #acquirePutFromLoadLock(Object)} that hasn't been - * {@link #registerPendingPut(Object) pre-registered} (aka a "naked put") - * will return false. - */ - public PutFromLoadValidator( + * + * @param cache Cache instance on which to store pending put information. + * @param cacheManager where to find a cache to store pending put information + * @param tm transaction manager + * @param nakedPutInvalidationPeriod Period (in ms) after a removal during which a call to + * {@link #acquirePutFromLoadLock(Object)} that hasn't been + * {@link #registerPendingPut(Object) pre-registered} (aka a "naked put") + * will return false. + */ + public PutFromLoadValidator(AdvancedCache cache, EmbeddedCacheManager cacheManager, TransactionManager tm, long nakedPutInvalidationPeriod) { - this.pendingPuts = cacheManager - .getCache( InfinispanRegionFactory.PENDING_PUTS_CACHE_NAME ); + + Configuration cacheConfiguration = cache.getCacheConfiguration(); + Configuration pendingPutsConfiguration = cacheManager.getCacheConfiguration(InfinispanRegionFactory.PENDING_PUTS_CACHE_NAME); + ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + if (pendingPutsConfiguration != null) { + configurationBuilder.read(pendingPutsConfiguration); + } + configurationBuilder.dataContainer().keyEquivalence(cacheConfiguration.dataContainer().keyEquivalence()); + String pendingPutsName = cache.getName() + "-" + InfinispanRegionFactory.PENDING_PUTS_CACHE_NAME; + cacheManager.defineConfiguration(pendingPutsName, configurationBuilder.build()); + + this.pendingPuts = cacheManager.getCache(pendingPutsName); this.transactionManager = tm; this.nakedPutInvalidationPeriod = nakedPutInvalidationPeriod; } // ----------------------------------------------------------------- Public + /** + * Marker for lock acquired in {@link #acquirePutFromLoadLock(Object)} + */ + public static class Lock { + protected Lock() {} + } + /** * Acquire a lock giving the calling thread the right to put data in the * cache for the given key. *

    * NOTE: A call to this method that returns true - * should always be matched with a call to {@link #releasePutFromLoadLock(Object)}. + * should always be matched with a call to {@link #releasePutFromLoadLock(Object, Lock)}. *

    * * @param key the key * - * @return true if the lock is acquired and the cache put - * can proceed; false if the data should not be cached + * @return AcquiredLock if the lock is acquired and the cache put + * can proceed; null if the data should not be cached */ - public boolean acquirePutFromLoadLock(Object key) { + public Lock acquirePutFromLoadLock(Object key) { boolean valid = false; boolean locked = false; - final long now = System.currentTimeMillis(); + long now = Long.MIN_VALUE; - try { - final PendingPutMap pending = pendingPuts.get( key ); - if ( pending != null ) { - locked = pending.acquireLock( 100, TimeUnit.MILLISECONDS ); - if ( locked ) { - try { - final PendingPut toCancel = pending.remove( getOwnerForPut() ); - if ( toCancel != null ) { - valid = !toCancel.completed; - toCancel.completed = true; + PendingPutMap pending = pendingPuts.get( key ); + for (;;) { + try { + if (pending != null) { + locked = pending.acquireLock(100, TimeUnit.MILLISECONDS); + if (locked) { + try { + final PendingPut toCancel = pending.remove(getOwnerForPut()); + if (toCancel != null) { + valid = !toCancel.completed; + toCancel.completed = true; + } else { + // this is a naked put + if (pending.hasInvalidator()) { + valid = false; + } else { + if (now == Long.MIN_VALUE) { + now = System.currentTimeMillis(); + } + valid = now > pending.nakedPutsDeadline; + } + } + return valid ? pending : null; + } finally { + if (!valid) { + pending.releaseLock(); + locked = false; + } } + } else { + // oops, we have leaked record for this owner, but we don't want to wait here + return null; } - finally { - if ( !valid ) { - pending.releaseLock(); - locked = false; + } else { + // Key wasn't in pendingPuts, so either this is a "naked put" + // or regionRemoved has been called. Check if we can proceed + long invalidationTimestamp = this.invalidationTimestamp; + if (invalidationTimestamp != Long.MIN_VALUE) { + now = System.currentTimeMillis(); + if (now > invalidationTimestamp) { + // time is +- monotonic se don't let other threads do the expensive currentTimeMillis() + invalidationUpdater.compareAndSet(this, invalidationTimestamp, Long.MIN_VALUE); + } else { + return null; } } - } - } - else { - // Key wasn't in pendingPuts, so either this is a "naked put" - // or regionRemoved has been called. Check if we can proceed - if ( now > invalidationTimestamp ) { - final Long removedTime = recentRemovals.get( key ); - if ( removedTime == null || now > removedTime ) { - // It's legal to proceed. But we have to record this key - // in pendingPuts so releasePutFromLoadLock can find it. - // To do this we basically simulate a normal "register - // then acquire lock" pattern - registerPendingPut( key ); - locked = acquirePutFromLoadLock( key ); - valid = locked; + + PendingPut pendingPut = new PendingPut(getOwnerForPut()); + pending = new PendingPutMap(pendingPut); + PendingPutMap existing = pendingPuts.putIfAbsent(key, pending); + if (existing != null) { + pending = existing; } + // continue in next loop with lock acquisition } - } - } - catch (Throwable t) { - if ( locked ) { - final PendingPutMap toRelease = pendingPuts.get( key ); - if ( toRelease != null ) { - toRelease.releaseLock(); + } catch (Throwable t) { + if (locked) { + pending.releaseLock(); } - } - if ( t instanceof RuntimeException ) { - throw (RuntimeException) t; - } - else if ( t instanceof Error ) { - throw (Error) t; - } - else { - throw new RuntimeException( t ); + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } else if (t instanceof Error) { + throw (Error) t; + } else { + throw new RuntimeException(t); + } } } - - return valid; } /** @@ -240,87 +261,16 @@ else if ( t instanceof Error ) { * * @param key the key */ - public void releasePutFromLoadLock(Object key) { - final PendingPutMap pending = pendingPuts.get( key ); + public void releasePutFromLoadLock(Object key, Lock lock) { + final PendingPutMap pending = (PendingPutMap) lock; if ( pending != null ) { - if ( pending.size() == 0 ) { + if ( pending.canRemove() ) { pendingPuts.remove( key, pending ); } pending.releaseLock(); } } - /** - * Invalidates any {@link #registerPendingPut(Object) previously registered pending puts} ensuring a subsequent call to - * {@link #acquirePutFromLoadLock(Object)} will return false.

    This method will block until any - * concurrent thread that has {@link #acquirePutFromLoadLock(Object) acquired the putFromLoad lock} for the given key - * has released the lock. This allows the caller to be certain the putFromLoad will not execute after this method - * returns, possibly caching stale data.

    - * - * @param key key identifying data whose pending puts should be invalidated - * - * @return true if the invalidation was successful; false if a problem occured (which the - * caller should treat as an exception condition) - */ - public boolean invalidateKey(Object key) { - boolean success = true; - - // Invalidate any pending puts - final PendingPutMap pending = pendingPuts.get( key ); - if ( pending != null ) { - // This lock should be available very quickly, but we'll be - // very patient waiting for it as callers should treat not - // acquiring it as an exception condition - if ( pending.acquireLock( 60, TimeUnit.SECONDS ) ) { - try { - pending.invalidate(); - } - finally { - pending.releaseLock(); - } - } - else { - success = false; - } - } - - // Record when this occurred to invalidate later naked puts - final RecentRemoval removal = new RecentRemoval( key, this.nakedPutInvalidationPeriod ); - recentRemovals.put( key, removal.timestamp ); - - // Don't let recentRemovals map become a memory leak - RecentRemoval toClean = null; - final boolean attemptClean = removal.timestamp > earliestRemovalTimestamp; - removalsLock.lock(); - try { - removalsQueue.add( removal ); - - if ( attemptClean ) { - if ( removalsQueue.size() > 1 ) { - // we have at least one as we just added it - toClean = removalsQueue.remove( 0 ); - } - earliestRemovalTimestamp = removalsQueue.get( 0 ).timestamp; - } - } - finally { - removalsLock.unlock(); - } - - if ( toClean != null ) { - Long cleaned = recentRemovals.get( toClean.key ); - if ( cleaned != null && cleaned.equals( toClean.timestamp ) ) { - cleaned = recentRemovals.remove( toClean.key ); - if ( cleaned != null && !cleaned.equals( toClean.timestamp ) ) { - // Oops; removed the wrong timestamp; restore it - recentRemovals.putIfAbsent( toClean.key, cleaned ); - } - } - } - - return success; - } - /** * Invalidates all {@link #registerPendingPut(Object) previously registered pending puts} ensuring a subsequent call to * {@link #acquirePutFromLoadLock(Object)} will return false.

    This method will block until any @@ -332,15 +282,16 @@ public boolean invalidateKey(Object key) { * caller should treat as an exception condition) */ public boolean invalidateRegion() { - - boolean ok = false; - invalidationTimestamp = System.currentTimeMillis() + this.nakedPutInvalidationPeriod; - + // TODO: not sure what happens with locks acquired *after* calling this method but before + // the actual invalidation + boolean ok = true; + invalidationUpdater.set(this, System.currentTimeMillis() + nakedPutInvalidationPeriod); try { // Acquire the lock for each entry to ensure any ongoing // work associated with it is completed before we return - for ( PendingPutMap entry : pendingPuts.values() ) { + for ( Iterator it = pendingPuts.values().iterator(); it.hasNext(); ) { + PendingPutMap entry = it.next(); if ( entry.acquireLock( 60, TimeUnit.SECONDS ) ) { try { entry.invalidate(); @@ -348,30 +299,15 @@ public boolean invalidateRegion() { finally { entry.releaseLock(); } + it.remove(); } else { ok = false; } } - - removalsLock.lock(); - try { - recentRemovals.clear(); - removalsQueue.clear(); - - ok = true; - - } - finally { - removalsLock.unlock(); - } - } - catch (Exception e) { + } catch (Exception e) { ok = false; } - finally { - earliestRemovalTimestamp = invalidationTimestamp; - } return ok; } @@ -382,8 +318,7 @@ public boolean invalidateRegion() { * wherein it is expected that a database read plus cache put will occur. Calling this method allows the validator to * treat the subsequent acquirePutFromLoadLock as if the database read occurred when this method was * invoked. This allows the validator to compare the timestamp of this call against the timestamp of subsequent removal - * notifications. A put that occurs without this call preceding it is "naked"; i.e the validator must assume the put is - * not valid if any relevant removal has occurred within {@link #NAKED_PUT_INVALIDATION_PERIOD} milliseconds. + * notifications. * * @param key key that will be used for subsequent cache put */ @@ -391,50 +326,83 @@ public void registerPendingPut(Object key) { final PendingPut pendingPut = new PendingPut( getOwnerForPut() ); final PendingPutMap pendingForKey = new PendingPutMap( pendingPut ); - for (; ; ) { - final PendingPutMap existing = pendingPuts.putIfAbsent( key, pendingForKey ); - if ( existing != null ) { - if ( existing.acquireLock( 10, TimeUnit.SECONDS ) ) { - - try { - existing.put( pendingPut ); - final PendingPutMap doublecheck = pendingPuts.putIfAbsent( key, existing ); - if ( doublecheck == null || doublecheck == existing ) { - break; - } - // else we hit a race and need to loop to try again - } - finally { - existing.releaseLock(); + final PendingPutMap existing = pendingPuts.putIfAbsent( key, pendingForKey ); + if ( existing != null ) { + if ( existing.acquireLock( 10, TimeUnit.SECONDS ) ) { + try { + if ( !existing.hasInvalidator() ) { + existing.put(pendingPut); } - } - else { - // Can't get the lock; when we come back we'll be a "naked put" - break; + } finally { + existing.releaseLock(); } } else { - // normal case - break; + // Can't get the lock; when we come back we'll be a "naked put" } } } - // -------------------------------------------------------------- Protected - /** - * Only for use by unit tests; may be removed at any time + * Invalidates any {@link #registerPendingPut(Object) previously registered pending puts} + * and disables further registrations ensuring a subsequent call to {@link #acquirePutFromLoadLock(Object)} + * will return false.

    This method will block until any concurrent thread that has + * {@link #acquirePutFromLoadLock(Object) acquired the putFromLoad lock} for the given key + * has released the lock. This allows the caller to be certain the putFromLoad will not execute after this method + * returns, possibly caching stale data.

    + * After this transaction completes, {@link #endInvalidatingKey(Object)} needs to be called } + * + * @param key key identifying data whose pending puts should be invalidated + * + * @return true if the invalidation was successful; false if a problem occured (which the + * caller should treat as an exception condition) */ - protected int getRemovalQueueLength() { - removalsLock.lock(); - try { - return removalsQueue.size(); + public boolean beginInvalidatingKey(Object key) { + PendingPutMap pending = new PendingPutMap(null); + PendingPutMap prev = pendingPuts.putIfAbsent(key, pending); + if (prev != null) { + pending = prev; } - finally { - removalsLock.unlock(); + if (pending.acquireLock(60, TimeUnit.SECONDS)) { + try { + pending.invalidate(); + pending.addInvalidator(getOwnerForPut(), System.currentTimeMillis() + nakedPutInvalidationPeriod); + } finally { + pending.releaseLock(); + } + return true; + } else { + return false; } } + /** + * Called after the transaction completes, allowing caching of entries. It is possible that this method + * is called without previous invocation of {@link #beginInvalidatingKey(Object)}, then it should be noop. + * + * @param key + * @return + */ + public boolean endInvalidatingKey(Object key) { + PendingPutMap pending = pendingPuts.get(key); + if (pending == null) { + return true; + } + if (pending.acquireLock(60, TimeUnit.SECONDS)) { + try { + pending.removeInvalidator(getOwnerForPut()); + // we can't remove the pending put yet because we wait for naked puts + // pendingPuts should be configured with maxIdle time so won't have memory leak + return true; + } finally { + pending.releaseLock(); + } + } else { + return false; + } + } + + // ---------------------------------------------------------------- Private private Object getOwnerForPut() { @@ -457,10 +425,13 @@ private Object getOwnerForPut() { *

    * This class is NOT THREAD SAFE. All operations on it must be performed with the lock held. */ - private static class PendingPutMap { + private static class PendingPutMap extends Lock { private PendingPut singlePendingPut; private Map fullMap; - private final Lock lock = new ReentrantLock(); + private final java.util.concurrent.locks.Lock lock = new ReentrantLock(); + private Object singleInvalidator; + private Set invalidators; + private long nakedPutsDeadline = Long.MIN_VALUE; PendingPutMap(PendingPut singleItem) { this.singlePendingPut = singleItem; @@ -533,6 +504,41 @@ else if ( fullMap != null ) { fullMap = null; } } + + public void addInvalidator(Object invalidator, long deadline) { + if (invalidators == null) { + if (singleInvalidator == null) { + singleInvalidator = invalidator; + } else { + invalidators = new HashSet(); + invalidators.add(singleInvalidator); + invalidators.add(invalidator); + singleInvalidator = null; + } + } else { + invalidators.add(invalidator); + } + nakedPutsDeadline = Math.max(nakedPutsDeadline, deadline); + } + + public boolean hasInvalidator() { + return singleInvalidator != null || (invalidators != null && !invalidators.isEmpty()); + } + + public void removeInvalidator(Object invalidator) { + if (invalidators == null) { + if (singleInvalidator != null && singleInvalidator.equals(invalidator)) { + singleInvalidator = null; + } + } else { + invalidators.remove(invalidator); + } + } + + public boolean canRemove() { + return size() == 0 && !hasInvalidator() && + (nakedPutsDeadline == Long.MIN_VALUE || nakedPutsDeadline < System.currentTimeMillis()); + } } private static class PendingPut { @@ -543,15 +549,4 @@ private PendingPut(Object owner) { this.owner = owner; } } - - private static class RecentRemoval { - private final Object key; - private final Long timestamp; - - private RecentRemoval(Object key, long nakedPutInvalidationPeriod) { - this.key = key; - timestamp = System.currentTimeMillis() + nakedPutInvalidationPeriod; - } - } - } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/TransactionalAccessDelegate.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/TransactionalAccessDelegate.java index bafe2d980025..3c196784e786 100755 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/TransactionalAccessDelegate.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/access/TransactionalAccessDelegate.java @@ -9,7 +9,6 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.impl.BaseRegion; import org.hibernate.cache.infinispan.util.Caches; - import org.infinispan.AdvancedCache; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; @@ -111,7 +110,8 @@ public boolean putFromLoad(Object key, Object value, long txTimestamp, Object ve return false; } - if ( !putValidator.acquirePutFromLoadLock( key ) ) { + PutFromLoadValidator.Lock lock = putValidator.acquirePutFromLoadLock(key); + if ( lock == null) { if ( TRACE_ENABLED ) { log.tracef( "Put from load lock not acquired for key %s", key ); } @@ -131,7 +131,7 @@ public boolean putFromLoad(Object key, Object value, long txTimestamp, Object ve } } finally { - putValidator.releasePutFromLoadLock( key ); + putValidator.releasePutFromLoadLock( key, lock); } return true; @@ -185,7 +185,7 @@ public boolean update(Object key, Object value, Object currentVersion, Object pr * @throws CacheException if removing the cached item fails */ public void remove(Object key) throws CacheException { - if ( !putValidator.invalidateKey( key ) ) { + if ( !putValidator.beginInvalidatingKey(key)) { throw new CacheException( "Failed to invalidate pending putFromLoad calls for key " + key + " from region " + region.getName() ); @@ -216,7 +216,7 @@ public void removeAll() throws CacheException { * @throws CacheException if evicting the item fails */ public void evict(Object key) throws CacheException { - if ( !putValidator.invalidateKey( key ) ) { + if ( !putValidator.beginInvalidatingKey(key)) { throw new CacheException( "Failed to invalidate pending putFromLoad calls for key " + key + " from region " + region.getName() ); @@ -240,4 +240,19 @@ public void evictAll() throws CacheException { Caches.broadcastEvictAll( cache ); } + /** + * Called when we have finished the attempted update/delete (which may or + * may not have been successful), after transaction completion. This method + * is used by "asynchronous" concurrency strategies. + * + * @param key The item key + * @throws org.hibernate.cache.CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} + */ + public void unlockItem(Object key) throws CacheException { + if ( !putValidator.endInvalidatingKey(key) ) { + // TODO: localization + log.warn("Failed to end invalidating pending putFromLoad calls for key " + key + " from region " + + region.getName() + "; the key won't be cached in the future."); + } + } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java index 0150e7954a67..869c6bef24be 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/CollectionRegionImpl.java @@ -10,11 +10,11 @@ import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion; import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; - import org.infinispan.AdvancedCache; /** @@ -33,11 +33,12 @@ public class CollectionRegionImpl extends BaseTransactionalDataRegion implements * @param name of collection type * @param metadata for the collection type * @param factory for the region + * @param cacheKeysFactory factory for cache keys */ public CollectionRegionImpl( AdvancedCache cache, String name, - CacheDataDescription metadata, RegionFactory factory) { - super( cache, name, metadata, factory ); + CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { + super( cache, name, metadata, factory, cacheKeysFactory ); } @Override diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/TransactionalAccess.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/TransactionalAccess.java index d174c1ad6403..e75d142aef52 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/TransactionalAccess.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/collection/TransactionalAccess.java @@ -11,6 +11,8 @@ import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * Transactional collection region access for Infinispan. @@ -72,9 +74,20 @@ public SoftLock lockRegion() throws CacheException { } public void unlockItem(Object key, SoftLock lock) throws CacheException { + delegate.unlockItem(key); } public void unlockRegion(SoftLock lock) throws CacheException { } + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return region.getCacheKeysFactory().createCollectionKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return region.getCacheKeysFactory().getCollectionId(cacheKey); + } + } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java index f81a642211d9..1cc5215a5455 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/EntityRegionImpl.java @@ -10,6 +10,7 @@ import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion; import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.access.AccessType; @@ -33,11 +34,12 @@ public class EntityRegionImpl extends BaseTransactionalDataRegion implements Ent * @param name of entity type * @param metadata for the entity type * @param factory for the region + * @param cacheKeysFactory factory for cache keys */ public EntityRegionImpl( AdvancedCache cache, String name, - CacheDataDescription metadata, RegionFactory factory) { - super( cache, name, metadata, factory ); + CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { + super( cache, name, metadata, factory, cacheKeysFactory); } @Override @@ -60,5 +62,4 @@ public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) thr public PutFromLoadValidator getPutFromLoadValidator() { return new PutFromLoadValidator( cache ); } - } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/TransactionalAccess.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/TransactionalAccess.java index c9f997fe0e06..64319523b706 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/TransactionalAccess.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/entity/TransactionalAccess.java @@ -11,6 +11,8 @@ import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * Transactional entity region access for Infinispan. @@ -81,6 +83,7 @@ public SoftLock lockRegion() throws CacheException { } public void unlockItem(Object key, SoftLock lock) throws CacheException { + delegate.unlockItem(key); } public void unlockRegion(SoftLock lock) throws CacheException { @@ -94,4 +97,14 @@ public boolean afterUpdate(Object key, Object value, Object currentVersion, Obje throws CacheException { return false; } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return region.getCacheKeysFactory().createEntityKey(id, persister, factory, tenantIdentifier); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return region.getCacheKeysFactory().getEntityId(cacheKey); + } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java index e9746b4d02c9..06a81dba02ef 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseTransactionalDataRegion.java @@ -7,6 +7,7 @@ package org.hibernate.cache.infinispan.impl; import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.TransactionalDataRegion; @@ -23,6 +24,7 @@ public abstract class BaseTransactionalDataRegion extends BaseRegion implements TransactionalDataRegion { private final CacheDataDescription metadata; + private final CacheKeysFactory cacheKeysFactory; /** * Base transactional region constructor @@ -31,12 +33,14 @@ public abstract class BaseTransactionalDataRegion * @param name of the transactional region * @param metadata for the transactional region * @param factory for the transactional region + * @param cacheKeysFactory factory for cache keys */ public BaseTransactionalDataRegion( AdvancedCache cache, String name, - CacheDataDescription metadata, RegionFactory factory) { - super( cache, name, factory ); + CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { + super( cache, name, factory); this.metadata = metadata; + this.cacheKeysFactory = cacheKeysFactory; } @Override @@ -44,4 +48,7 @@ public CacheDataDescription getCacheDataDescription() { return metadata; } + public CacheKeysFactory getCacheKeysFactory() { + return cacheKeysFactory; + } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java index 89c1f5e19bcd..3c7b6d82f456 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/NaturalIdRegionImpl.java @@ -10,11 +10,11 @@ import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion; import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; - import org.infinispan.AdvancedCache; /** @@ -33,11 +33,12 @@ public class NaturalIdRegionImpl extends BaseTransactionalDataRegion * @param name of natural id region * @param metadata for the natural id region * @param factory for the natural id region + * @param cacheKeysFactory factory for cache keys */ public NaturalIdRegionImpl( AdvancedCache cache, String name, - CacheDataDescription metadata, RegionFactory factory) { - super( cache, name, metadata, factory ); + CacheDataDescription metadata, RegionFactory factory, CacheKeysFactory cacheKeysFactory) { + super( cache, name, metadata, factory, cacheKeysFactory ); } @Override diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/ReadOnlyAccess.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/ReadOnlyAccess.java index a287f2b59158..adccce120b23 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/ReadOnlyAccess.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/ReadOnlyAccess.java @@ -18,7 +18,6 @@ class ReadOnlyAccess extends TransactionalAccess { super( naturalIdRegion ); } - @Override public boolean update(Object key, Object value) throws CacheException { throw new UnsupportedOperationException( "Illegal attempt to edit read only item" ); @@ -28,4 +27,5 @@ public boolean update(Object key, Object value) throws CacheException { public boolean afterUpdate(Object key, Object value, SoftLock lock) throws CacheException { throw new UnsupportedOperationException( "Illegal attempt to edit read only item" ); } + } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/TransactionalAccess.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/TransactionalAccess.java index b13f160398de..4ceb67eedcf9 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/TransactionalAccess.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/naturalid/TransactionalAccess.java @@ -11,6 +11,8 @@ import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * @author Strong Liu @@ -87,6 +89,7 @@ public SoftLock lockRegion() throws CacheException { @Override public void unlockItem(Object key, SoftLock lock) throws CacheException { + delegate.unlockItem(key); } @Override @@ -103,4 +106,13 @@ public boolean afterUpdate(Object key, Object value, SoftLock lock) throws Cache return false; } + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return region.getCacheKeysFactory().createNaturalIdKey(naturalIdValues, persister, session); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return region.getCacheKeysFactory().getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java index cbbf0b14491f..a5c2eb3f968d 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java @@ -13,7 +13,6 @@ import org.hibernate.cache.infinispan.util.Caches; import org.hibernate.cache.spi.QueryResultsRegion; import org.hibernate.cache.spi.RegionFactory; - import org.infinispan.AdvancedCache; import org.infinispan.context.Flag; @@ -38,7 +37,7 @@ public class QueryResultsRegionImpl extends BaseTransactionalDataRegion implemen * @param factory for the query region */ public QueryResultsRegionImpl(AdvancedCache cache, String name, RegionFactory factory) { - super( cache, name, null, factory ); + super( cache, name, null, factory, null ); // If Infinispan is using INVALIDATION for query cache, we don't want to propagate changes. // We use the Timestamps cache to manage invalidation final boolean localOnly = Caches.isInvalidationCache( cache ); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractEntityCollectionRegionTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractEntityCollectionRegionTestCase.java index 7ef398ba7f4a..d8b84bf9fe19 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractEntityCollectionRegionTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractEntityCollectionRegionTestCase.java @@ -11,11 +11,12 @@ import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cache.infinispan.InfinispanRegionFactory; +import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.TransactionalDataRegion; import org.hibernate.cache.spi.access.AccessType; - +import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.junit.Test; @@ -30,6 +31,8 @@ * @since 3.5 */ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractRegionImplTestCase { + protected static CacheDataDescription MUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(true, false, ComparableComparator.INSTANCE, null); + @Test public void testSupportedAccessTypes() throws Exception { supportedAccessTypeTest(); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java index 09ba0db2a9f5..2de5a570c9ca 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractGeneralDataRegionTestCase.java @@ -15,15 +15,12 @@ import org.hibernate.cache.spi.GeneralDataRegion; import org.hibernate.cache.spi.QueryResultsRegion; import org.hibernate.cache.spi.Region; - import org.hibernate.test.cache.infinispan.util.CacheTestUtil; -import org.junit.Ignore; -import org.junit.Test; - import org.infinispan.AdvancedCache; import org.infinispan.transaction.tm.BatchModeTransactionManager; - import org.jboss.logging.Logger; +import org.junit.Ignore; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractRegionImplTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractRegionImplTestCase.java index 2daf5ad2e0d9..57c3198b7fe9 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractRegionImplTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/AbstractRegionImplTestCase.java @@ -32,7 +32,7 @@ public abstract class AbstractRegionImplTestCase extends AbstractNonFunctionalTe protected abstract void removeFromRegion(Region region, Object key); protected CacheDataDescription getCacheDataDescription() { - return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE); + return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE, null); } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/InfinispanRegionFactoryTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/InfinispanRegionFactoryTestCase.java index f58a4e0c3738..05655a138b29 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/InfinispanRegionFactoryTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/InfinispanRegionFactoryTestCase.java @@ -6,6 +6,12 @@ */ package org.hibernate.test.cache.infinispan; +import javax.transaction.TransactionManager; +import java.util.Properties; + +import org.hibernate.boot.internal.SessionFactoryBuilderImpl; +import org.hibernate.boot.internal.SessionFactoryOptionsImpl; +import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.InfinispanRegionFactory; @@ -20,6 +26,7 @@ import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform; import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform; import org.hibernate.test.cache.infinispan.functional.SingleNodeTestCase; +import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import org.hibernate.testing.ServiceRegistryBuilder; import org.infinispan.AdvancedCache; import org.infinispan.configuration.cache.CacheMode; @@ -33,10 +40,12 @@ import org.infinispan.transaction.TransactionMode; import org.junit.Test; -import javax.transaction.TransactionManager; -import java.util.Properties; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * InfinispanRegionFactoryTestCase. @@ -45,8 +54,13 @@ * @since 3.5 */ public class InfinispanRegionFactoryTestCase { - private static CacheDataDescription MUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(true, false, null); - private static CacheDataDescription IMMUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(false, false, null); + private static final CacheDataDescription MUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(true, false, null, null); + private static final CacheDataDescription IMMUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(false, false, null, null); + + private static final StandardServiceRegistry REGISTRY + = CacheTestUtil.buildBaselineStandardServiceRegistryBuilder("test", InfinispanRegionFactory.class, true, false).build(); + private static final SessionFactoryOptions SETTINGS = new SessionFactoryOptionsImpl( + new SessionFactoryBuilderImpl.SessionFactoryOptionsStateStandardImpl( REGISTRY )); @Test public void testConfigurationProcessing() { @@ -553,6 +567,10 @@ private InfinispanRegionFactory createRegionFactory(Properties p) { } private InfinispanRegionFactory createRegionFactory(final EmbeddedCacheManager manager, Properties p) { + return createRegionFactory(manager, p, SETTINGS); + } + + private InfinispanRegionFactory createRegionFactory(final EmbeddedCacheManager manager, Properties p, SessionFactoryOptions settings) { final InfinispanRegionFactory factory = new SingleNodeTestCase.TestInfinispanRegionFactory() { @Override @@ -577,7 +595,7 @@ protected EmbeddedCacheManager createCacheManager(Properties properties) throws }; - factory.start(null, p); + factory.start(settings, p); return factory; } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/access/PutFromLoadValidatorUnitTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/access/PutFromLoadValidatorUnitTestCase.java index de2b93b3b24f..407d832e3cb8 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/access/PutFromLoadValidatorUnitTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/access/PutFromLoadValidatorUnitTestCase.java @@ -6,6 +6,7 @@ */ package org.hibernate.test.cache.infinispan.access; +import javax.transaction.TransactionManager; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; @@ -15,24 +16,21 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import javax.transaction.TransactionManager; import org.hibernate.cache.infinispan.access.PutFromLoadValidator; - import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.infinispan.manager.EmbeddedCacheManager; import org.infinispan.test.CacheManagerCallable; import org.infinispan.test.fwk.TestCacheManagerFactory; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import static org.infinispan.test.TestingUtil.withCacheManager; +import static org.infinispan.test.TestingUtil.withTx; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -83,28 +81,13 @@ private void nakedPutTest(final boolean transactional) throws Exception { TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - try { - PutFromLoadValidator testee = new PutFromLoadValidator(cm, - transactional ? tm : null, - PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); - if (transactional) { - tm.begin(); - } - boolean lockable = testee.acquirePutFromLoadLock(KEY1); - try { - assertTrue(lockable); - } - finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, + transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + exec(transactional, new NakedPut(testee, true)); } }); } + @Test public void testRegisteredPut() throws Exception { registeredPutTest(false); @@ -119,30 +102,13 @@ private void registeredPutTest(final boolean transactional) throws Exception { TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator testee = new PutFromLoadValidator(cm, - transactional ? tm : null, - PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); - try { - if (transactional) { - tm.begin(); - } - testee.registerPendingPut(KEY1); - - boolean lockable = testee.acquirePutFromLoadLock(KEY1); - try { - assertTrue(lockable); - } - finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, + transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + exec(transactional, new RegularPut(testee)); } }); } + @Test public void testNakedPutAfterKeyRemoval() throws Exception { nakedPutAfterRemovalTest(false, false); @@ -166,35 +132,16 @@ private void nakedPutAfterRemovalTest(final boolean transactional, TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator testee = new PutFromLoadValidator(cm, - transactional ? tm : null, - PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); - if (removeRegion) { - testee.invalidateRegion(); - } else { - testee.invalidateKey(KEY1); - } - try { - if (transactional) { - tm.begin(); - } - - boolean lockable = testee.acquirePutFromLoadLock(KEY1); - try { - assertFalse(lockable); - } - finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, + transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + Invalidation invalidation = new Invalidation(testee, removeRegion); + NakedPut nakedPut = new NakedPut(testee, false); + exec(transactional, invalidation, nakedPut); } }); } + @Test public void testRegisteredPutAfterKeyRemoval() throws Exception { registeredPutAfterRemovalTest(false, false); @@ -218,32 +165,11 @@ private void registeredPutAfterRemovalTest(final boolean transactional, TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator testee = new PutFromLoadValidator(cm, - transactional ? tm : null, - PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); - if (removeRegion) { - testee.invalidateRegion(); - } else { - testee.invalidateKey(KEY1); - } - try { - if (transactional) { - tm.begin(); - } - testee.registerPendingPut(KEY1); - - boolean lockable = testee.acquirePutFromLoadLock(KEY1); - try { - assertTrue(lockable); - } - finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, + transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + Invalidation invalidation = new Invalidation(testee, removeRegion); + RegularPut regularPut = new RegularPut(testee); + exec(transactional, invalidation, regularPut); } }); @@ -272,9 +198,8 @@ private void registeredPutWithInterveningRemovalTest( TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator testee = new PutFromLoadValidator(cm, - transactional ? tm : null, - PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, + transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); try { if (transactional) { tm.begin(); @@ -283,17 +208,18 @@ public void call() { if (removeRegion) { testee.invalidateRegion(); } else { - testee.invalidateKey(KEY1); + testee.beginInvalidatingKey(KEY1); } - boolean lockable = testee.acquirePutFromLoadLock(KEY1); + PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(KEY1); try { - assertFalse(lockable); + assertNull(lock); } finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); + if (lock != null) { + testee.releasePutFromLoadLock(KEY1, lock); } + testee.endInvalidatingKey(KEY1); } } catch (Exception e) { throw new RuntimeException(e); @@ -301,6 +227,7 @@ public void call() { } }); } + @Test public void testDelayedNakedPutAfterKeyRemoval() throws Exception { delayedNakedPutAfterRemovalTest(false, false); @@ -325,12 +252,13 @@ private void delayedNakedPutAfterRemovalTest( TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator testee = new TestValidator(cm, + PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, transactional ? tm : null, 100); if (removeRegion) { testee.invalidateRegion(); } else { - testee.invalidateKey(KEY1); + testee.beginInvalidatingKey(KEY1); + testee.endInvalidatingKey(KEY1); } try { if (transactional) { @@ -338,12 +266,12 @@ public void call() { } Thread.sleep(110); - boolean lockable = testee.acquirePutFromLoadLock(KEY1); + PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(KEY1); try { - assertTrue(lockable); + assertNotNull(lock); } finally { - if (lockable) { - testee.releasePutFromLoadLock(KEY1); + if (lock != null) { + testee.releasePutFromLoadLock(KEY1, null); } } } catch (Exception e) { @@ -368,7 +296,7 @@ private void multipleRegistrationtest(final boolean transactional) throws Except TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - final PutFromLoadValidator testee = new PutFromLoadValidator(cm, + final PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), cm, transactional ? tm : null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); @@ -385,12 +313,13 @@ public void run() { testee.registerPendingPut(KEY1); registeredLatch.countDown(); registeredLatch.await(5, TimeUnit.SECONDS); - if (testee.acquirePutFromLoadLock(KEY1)) { + PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(KEY1); + if (lock != null) { try { log.trace("Put from load lock acquired for key = " + KEY1); success.incrementAndGet(); } finally { - testee.releasePutFromLoadLock(KEY1); + testee.releasePutFromLoadLock(KEY1, lock); } } else { log.trace("Unable to acquired putFromLoad lock for key = " + KEY1); @@ -440,8 +369,8 @@ private void invalidationBlocksForInProgressPutTest(final boolean keyOnly) throw TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - final PutFromLoadValidator testee = new PutFromLoadValidator( - cm, null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); + final PutFromLoadValidator testee = new PutFromLoadValidator(cm.getCache().getAdvancedCache(), + cm, null, PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD); final CountDownLatch removeLatch = new CountDownLatch(1); final CountDownLatch pferLatch = new CountDownLatch(1); final AtomicReference cache = new AtomicReference("INITIAL"); @@ -449,7 +378,8 @@ public void call() { Callable pferCallable = new Callable() { public Boolean call() throws Exception { testee.registerPendingPut(KEY1); - if (testee.acquirePutFromLoadLock(KEY1)) { + PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(KEY1); + if (lock != null) { try { removeLatch.countDown(); pferLatch.await(); @@ -457,7 +387,7 @@ public Boolean call() throws Exception { return Boolean.TRUE; } finally { - testee.releasePutFromLoadLock(KEY1); + testee.releasePutFromLoadLock(KEY1, lock); } } return Boolean.FALSE; @@ -468,7 +398,7 @@ public Boolean call() throws Exception { public Void call() throws Exception { removeLatch.await(); if (keyOnly) { - testee.invalidateKey(KEY1); + testee.beginInvalidatingKey(KEY1); } else { testee.invalidateRegion(); } @@ -501,18 +431,104 @@ public Void call() throws Exception { }); } - private static class TestValidator extends PutFromLoadValidator { + protected void exec(boolean transactional, Callable... callables) { + try { + if (transactional) { + for (Callable c : callables) { + withTx(tm, c); + } + } else { + for (Callable c : callables) { + c.call(); + } + } + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private class Invalidation implements Callable { + private PutFromLoadValidator putFromLoadValidator; + private boolean removeRegion; - protected TestValidator(EmbeddedCacheManager cm, - TransactionManager transactionManager, - long nakedPutInvalidationPeriod) { - super(cm, transactionManager, nakedPutInvalidationPeriod); + public Invalidation(PutFromLoadValidator putFromLoadValidator, boolean removeRegion) { + this.putFromLoadValidator = putFromLoadValidator; + this.removeRegion = removeRegion; } @Override - public int getRemovalQueueLength() { - return super.getRemovalQueueLength(); + public Void call() throws Exception { + if (removeRegion) { + boolean success = putFromLoadValidator.invalidateRegion(); + assertTrue(success); + } else { + boolean success = putFromLoadValidator.beginInvalidatingKey(KEY1); + assertTrue(success); + success = putFromLoadValidator.endInvalidatingKey(KEY1); + assertTrue(success); + } + return null; + } + } + + private class RegularPut implements Callable { + private PutFromLoadValidator putFromLoadValidator; + + public RegularPut(PutFromLoadValidator putFromLoadValidator) { + this.putFromLoadValidator = putFromLoadValidator; } + @Override + public Void call() throws Exception { + try { + putFromLoadValidator.registerPendingPut(KEY1); + + PutFromLoadValidator.Lock lock = putFromLoadValidator.acquirePutFromLoadLock(KEY1); + try { + assertNotNull(lock); + } finally { + if (lock != null) { + putFromLoadValidator.releasePutFromLoadLock(KEY1, lock); + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return null; + } + } + + private class NakedPut implements Callable { + private final PutFromLoadValidator testee; + private final boolean expectSuccess; + + public NakedPut(PutFromLoadValidator testee, boolean expectSuccess) { + this.testee = testee; + this.expectSuccess = expectSuccess; + } + + @Override + public Void call() throws Exception { + try { + PutFromLoadValidator.Lock lock = testee.acquirePutFromLoadLock(KEY1); + try { + if (expectSuccess) { + assertNotNull(lock); + } else { + assertNull(lock); + } + } + finally { + if (lock != null) { + testee.releasePutFromLoadLock(KEY1, lock); + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + return null; + } } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/AbstractCollectionRegionAccessStrategyTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/AbstractCollectionRegionAccessStrategyTestCase.java index 1ba12fd326a8..3c30c64497fe 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/AbstractCollectionRegionAccessStrategyTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/AbstractCollectionRegionAccessStrategyTestCase.java @@ -6,14 +6,15 @@ */ package org.hibernate.test.cache.infinispan.collection; +import javax.transaction.TransactionManager; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import javax.transaction.TransactionManager; +import junit.framework.AssertionFailedError; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.access.PutFromLoadValidator; @@ -25,20 +26,17 @@ import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.internal.util.compare.ComparableComparator; - import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.NodeEnvironment; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import junit.framework.AssertionFailedError; - +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.infinispan.test.CacheManagerCallable; import org.infinispan.test.fwk.TestCacheManagerFactory; import org.infinispan.transaction.tm.BatchModeTransactionManager; - import org.jboss.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import static org.infinispan.test.TestingUtil.withCacheManager; import static org.junit.Assert.assertEquals; @@ -54,7 +52,6 @@ */ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends AbstractNonFunctionalTestCase { private static final Logger log = Logger.getLogger( AbstractCollectionRegionAccessStrategyTestCase.class ); - public static final String REGION_NAME = "test/com.foo.test"; public static final String KEY_BASE = "KEY"; public static final String VALUE1 = "VALUE1"; @@ -118,7 +115,7 @@ protected static StandardServiceRegistryBuilder createStandardServiceRegistryBui } protected CacheDataDescription getCacheDataDescription() { - return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE ); + return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE, null); } @After @@ -155,11 +152,11 @@ public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception { withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) { @Override public void call() { - PutFromLoadValidator validator = new PutFromLoadValidator(cm, + PutFromLoadValidator validator = new PutFromLoadValidator(remoteCollectionRegion.getCache(), cm, remoteTm, 20000) { @Override - public boolean acquirePutFromLoadLock(Object key) { - boolean acquired = super.acquirePutFromLoadLock( key ); + public Lock acquirePutFromLoadLock(Object key) { + Lock lock = super.acquirePutFromLoadLock( key ); try { removeLatch.countDown(); pferLatch.await( 2, TimeUnit.SECONDS ); @@ -172,7 +169,7 @@ public boolean acquirePutFromLoadLock(Object key) { log.error( "Error", e ); throw new RuntimeException( "Error", e ); } - return acquired; + return lock; } }; @@ -230,7 +227,7 @@ public void testPutFromLoadMinimal() throws Exception { private void putFromLoadTest(final boolean useMinimalAPI) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateCollectionCacheKey( KEY_BASE + testCount++ ); final CountDownLatch writeLatch1 = new CountDownLatch( 1 ); final CountDownLatch writeLatch2 = new CountDownLatch( 1 ); @@ -382,7 +379,7 @@ public void testEvictAll() throws Exception { private void evictOrRemoveTest(final boolean evict) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateCollectionCacheKey( KEY_BASE + testCount++ ); assertNull( "local is clean", localAccessStrategy.get( KEY, System.currentTimeMillis() ) ); assertNull( "remote is clean", remoteAccessStrategy.get( KEY, System.currentTimeMillis() ) ); @@ -413,7 +410,7 @@ public Void call() throws Exception { private void evictOrRemoveAllTest(final boolean evict) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateCollectionCacheKey( KEY_BASE + testCount++ ); assertEquals( 0, getValidKeyCount( localCollectionRegion.getCache().keySet() ) ); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/CollectionRegionImplTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/CollectionRegionImplTestCase.java index 81ae720d4bf0..6b96a699996e 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/CollectionRegionImplTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/CollectionRegionImplTestCase.java @@ -6,9 +6,10 @@ */ package org.hibernate.test.cache.infinispan.collection; +import java.util.Properties; + import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.InfinispanRegionFactory; -import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.Region; @@ -18,8 +19,6 @@ import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase; import org.infinispan.AdvancedCache; -import java.util.Properties; - import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; @@ -29,8 +28,6 @@ * @author Galder Zamarreño */ public class CollectionRegionImplTestCase extends AbstractEntityCollectionRegionTestCase { - private static CacheDataDescription MUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(true, false, null); - @Override protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) { CollectionRegion region = regionFactory.buildCollectionRegion("test", properties, MUTABLE_NON_VERSIONED); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/TransactionalExtraAPITestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/TransactionalExtraAPITestCase.java index 74b7a1f1c471..07e594845262 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/TransactionalExtraAPITestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/collection/TransactionalExtraAPITestCase.java @@ -8,13 +8,16 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cache.infinispan.InfinispanRegionFactory; +import org.hibernate.cache.internal.CacheDataDescriptionImpl; +import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; - +import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.NodeEnvironment; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -30,9 +33,9 @@ public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase { public static final String REGION_NAME = "test/com.foo.test"; - public static final String KEY = "KEY"; - public static final String VALUE1 = "VALUE1"; - public static final String VALUE2 = "VALUE2"; + public static final Object KEY = TestingKeyFactory.generateCollectionCacheKey( "KEY" ); + public static final CacheDataDescription CACHE_DATA_DESCRIPTION + = new CacheDataDescriptionImpl(false, false, ComparableComparator.INSTANCE, null); private NodeEnvironment environment; private static CollectionRegionAccessStrategy accessStrategy; @@ -45,7 +48,7 @@ public final void prepareLocalAccessStrategy() throws Exception { // Sleep a bit to avoid concurrent FLUSH problem avoidConcurrentFlush(); - accessStrategy = environment.getCollectionRegion( REGION_NAME, null ).buildAccessStrategy( getAccessType() ); + accessStrategy = environment.getCollectionRegion( REGION_NAME, CACHE_DATA_DESCRIPTION).buildAccessStrategy( getAccessType() ); } protected StandardServiceRegistryBuilder createStandardServiceRegistryBuilder() { diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractEntityRegionAccessStrategyTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractEntityRegionAccessStrategyTestCase.java index cc0153f008f3..0eab81e6ba42 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractEntityRegionAccessStrategyTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractEntityRegionAccessStrategyTestCase.java @@ -11,6 +11,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import junit.framework.AssertionFailedError; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.entity.EntityRegionImpl; @@ -20,20 +21,17 @@ import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.internal.util.compare.ComparableComparator; - import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.NodeEnvironment; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import junit.framework.AssertionFailedError; - +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.infinispan.Cache; import org.infinispan.test.TestingUtil; import org.infinispan.transaction.tm.BatchModeTransactionManager; - import org.jboss.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -117,7 +115,7 @@ protected static StandardServiceRegistryBuilder createStandardServiceRegistryBui } protected CacheDataDescription getCacheDataDescription() { - return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE); + return new CacheDataDescriptionImpl(true, true, ComparableComparator.INSTANCE, null); } @After @@ -193,7 +191,7 @@ public void testPutFromLoadMinimal() throws Exception { */ private void putFromLoadTest(final boolean useMinimalAPI) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); final CountDownLatch writeLatch1 = new CountDownLatch(1); final CountDownLatch writeLatch2 = new CountDownLatch(1); @@ -297,7 +295,7 @@ public void run() { @Test public void testInsert() throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); final CountDownLatch readLatch = new CountDownLatch(1); final CountDownLatch commitLatch = new CountDownLatch(1); @@ -386,7 +384,7 @@ public void run() { @Test public void testUpdate() throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); // Set up initial state localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); @@ -502,7 +500,7 @@ public void testEvictAll() throws Exception { } private void evictOrRemoveTest(final boolean evict) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet())); assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet())); @@ -531,7 +529,7 @@ public Void call() throws Exception { } private void evictOrRemoveAllTest(final boolean evict) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); assertEquals(0, getValidKeyCount(localEntityRegion.getCache().keySet())); assertEquals(0, getValidKeyCount(remoteEntityRegion.getCache().keySet())); assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis())); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractReadOnlyAccessTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractReadOnlyAccessTestCase.java index 7c8d7de2afe5..d3a4dbce3791 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractReadOnlyAccessTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractReadOnlyAccessTestCase.java @@ -7,10 +7,9 @@ package org.hibernate.test.cache.infinispan.entity; import org.hibernate.cache.spi.access.AccessType; - -import org.junit.Test; - +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.infinispan.transaction.tm.BatchModeTransactionManager; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -42,7 +41,7 @@ public void testPutFromLoadMinimal() throws Exception { private void putFromLoadTest(boolean minimal) throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); long txTimestamp = System.currentTimeMillis(); BatchModeTransactionManager.getInstance().begin(); @@ -64,8 +63,8 @@ private void putFromLoadTest(boolean minimal) throws Exception { @Test(expected = UnsupportedOperationException.class) @Override public void testUpdate() throws Exception { - localAccessStrategy.update(KEY_BASE + testCount++, - VALUE2, 2, 1); + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); + localAccessStrategy.update( KEY, VALUE2, 2, 1); } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractTransactionalAccessTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractTransactionalAccessTestCase.java index b390e48dc81e..fd3dce02576e 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractTransactionalAccessTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/AbstractTransactionalAccessTestCase.java @@ -10,11 +10,11 @@ import java.util.concurrent.TimeUnit; import junit.framework.AssertionFailedError; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.infinispan.transaction.tm.BatchModeTransactionManager; import org.jboss.logging.Logger; -import org.hibernate.cache.spi.access.AccessType; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -34,7 +34,7 @@ protected AccessType getAccessType() { public void testContestedPutFromLoad() throws Exception { - final String KEY = KEY_BASE + testCount++; + final Object KEY = TestingKeyFactory.generateEntityCacheKey( KEY_BASE + testCount++ ); localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1)); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/EntityRegionImplTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/EntityRegionImplTestCase.java index f20f93320924..645bc5a2502e 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/EntityRegionImplTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/EntityRegionImplTestCase.java @@ -6,9 +6,10 @@ */ package org.hibernate.test.cache.infinispan.entity; +import java.util.Properties; + import org.hibernate.cache.CacheException; import org.hibernate.cache.infinispan.InfinispanRegionFactory; -import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.Region; @@ -17,8 +18,6 @@ import org.hibernate.test.cache.infinispan.AbstractEntityCollectionRegionTestCase; import org.infinispan.AdvancedCache; -import java.util.Properties; - import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; @@ -29,7 +28,6 @@ * @since 3.5 */ public class EntityRegionImplTestCase extends AbstractEntityCollectionRegionTestCase { - private static CacheDataDescription MUTABLE_NON_VERSIONED = new CacheDataDescriptionImpl(true, false, null); @Override protected void supportedAccessTypeTest(RegionFactory regionFactory, Properties properties) { diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/TransactionalExtraAPITestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/TransactionalExtraAPITestCase.java index d0bc47dd2008..18ca82ebd668 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/TransactionalExtraAPITestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/entity/TransactionalExtraAPITestCase.java @@ -7,15 +7,16 @@ package org.hibernate.test.cache.infinispan.entity; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.infinispan.InfinispanRegionFactory; +import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; - +import org.hibernate.internal.util.compare.ComparableComparator; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.NodeEnvironment; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; +import org.hibernate.test.cache.infinispan.util.TestingKeyFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,9 +36,11 @@ */ public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase { public static final String REGION_NAME = "test/com.foo.test"; - public static final String KEY = "KEY"; + public static final Object KEY = TestingKeyFactory.generateEntityCacheKey( "KEY" ); public static final String VALUE1 = "VALUE1"; public static final String VALUE2 = "VALUE2"; + protected static final CacheDataDescriptionImpl CACHE_DATA_DESCRIPTION + = new CacheDataDescriptionImpl(true, false, ComparableComparator.INSTANCE, null); private NodeEnvironment environment; private EntityRegionAccessStrategy accessStrategy; @@ -50,7 +53,7 @@ public final void prepareLocalAccessStrategy() throws Exception { // Sleep a bit to avoid concurrent FLUSH problem avoidConcurrentFlush(); - accessStrategy = environment.getEntityRegion( REGION_NAME, new CacheDataDescriptionImpl(true, false, null)).buildAccessStrategy( getAccessType() ); + accessStrategy = environment.getEntityRegion( REGION_NAME, CACHE_DATA_DESCRIPTION).buildAccessStrategy( getAccessType() ); } protected StandardServiceRegistryBuilder createStandardServiceRegistryBuilder() { diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/AbstractFunctionalTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/AbstractFunctionalTestCase.java index bf7045443624..c21b493d9398 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/AbstractFunctionalTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/AbstractFunctionalTestCase.java @@ -6,18 +6,33 @@ */ package org.hibernate.test.cache.infinispan.functional; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.Phaser; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.hibernate.PessimisticLockException; import org.hibernate.Session; +import org.hibernate.cache.infinispan.InfinispanRegionFactory; +import org.hibernate.cache.infinispan.entity.EntityRegionImpl; +import org.hibernate.cache.spi.Region; import org.hibernate.stat.SecondLevelCacheStatistics; import org.hibernate.stat.Statistics; +import org.hibernate.testing.TestForIssue; +import org.infinispan.AdvancedCache; +import org.infinispan.commands.read.GetKeyValueCommand; +import org.infinispan.context.InvocationContext; +import org.infinispan.interceptors.base.BaseCustomInterceptor; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; import org.junit.Test; -import java.util.Map; -import java.util.concurrent.Callable; - import static org.infinispan.test.TestingUtil.withTx; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; /** * Parent tests for both transactional and @@ -122,4 +137,152 @@ public Void call() throws Exception { }); } + @Test + @TestForIssue(jiraKey = "HHH-9868") + public void testConcurrentRemoveAndPutFromLoad() throws Exception { + final Item item = new Item( "chris", "Chris's Item" ); + + withTx(tm, () -> { + Session s = openSession(); + s.getTransaction().begin(); + s.persist(item); + s.getTransaction().commit(); + s.close(); + return null; + }); + Region region = sessionFactory().getSecondLevelCacheRegion(Item.class.getName()); + + Phaser deletePhaser = new Phaser(2); + Phaser getPhaser = new Phaser(2); + HookInterceptor hook = new HookInterceptor(); + + AdvancedCache entityCache = ((EntityRegionImpl) region).getCache(); + AdvancedCache pendingPutsCache = entityCache.getCacheManager().getCache( + entityCache.getName() + "-" + InfinispanRegionFactory.PENDING_PUTS_CACHE_NAME).getAdvancedCache(); + pendingPutsCache.addInterceptor(hook, 0); + + Thread deleteThread = new Thread(() -> { + try { + withTx(tm, () -> { + Session s = openSession(); + log.trace("Session opened"); + s.getTransaction().begin(); + log.trace("TX started"); + Item loadedItem = s.get(Item.class, item.getId()); + assertNotNull(loadedItem); + arriveAndAwait(deletePhaser); + arriveAndAwait(deletePhaser); + log.trace("Item loaded"); + s.delete(loadedItem); + log.trace("Item deleted"); + s.getTransaction().commit(); + log.trace("TX committed"); + // start get-thread here + arriveAndAwait(deletePhaser); + arriveAndAwait(deletePhaser); + s.close(); + log.trace("Session closed"); + return null; + }); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, "delete-thread"); + Thread getThread = new Thread(() -> { + try { + withTx(tm, () -> { + Session s = openSession(); + log.trace("Session opened"); + s.getTransaction().begin(); + log.trace("TX started"); + // DB load should happen before the record is deleted, + // putFromLoad should happen after deleteThread ends + Item loadedItem = s.get(Item.class, item.getId()); + assertNotNull(loadedItem); + s.close(); + log.trace("Session closed"); + return null; + }); + } catch (PessimisticLockException e) { + // If we end up here, database locks guard us against situation tested + // in this case and HHH-9868 cannot happen. + // (delete-thread has ITEMS table write-locked and we try to acquire read-lock) + try { + arriveAndAwait(getPhaser); + arriveAndAwait(getPhaser); + } catch (Exception e1) { + throw new RuntimeException(e1); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + }, "get-thread"); + + deleteThread.start(); + // deleteThread loads the entity + arriveAndAwait(deletePhaser); + withTx(tm, () -> { + sessionFactory().getCache().evictEntity(Item.class, item.getId()); + assertFalse(sessionFactory().getCache().containsEntity(Item.class, item.getId())); + return null; + }); + arriveAndAwait(deletePhaser); + // delete thread invalidates PFER + arriveAndAwait(deletePhaser); + // get thread gets the entity from DB + hook.block(getPhaser, getThread); + getThread.start(); + arriveAndAwait(getPhaser); + arriveAndAwait(deletePhaser); + // delete thread finishes the remove from DB and cache + deleteThread.join(); + hook.unblock(); + arriveAndAwait(getPhaser); + // get thread puts the entry into cache + getThread.join(); + + withTx(tm, () -> { + Session s = openSession(); + s.getTransaction().begin(); + Item loadedItem = s.get(Item.class, item.getId()); + assertNull(loadedItem); + s.getTransaction().commit(); + s.close(); + return null; + }); + } + + protected static void arriveAndAwait(Phaser phaser) throws TimeoutException, InterruptedException { + phaser.awaitAdvanceInterruptibly(phaser.arrive(), 10, TimeUnit.SECONDS); + } + + private static class HookInterceptor extends BaseCustomInterceptor { + Phaser phaser; + Thread thread; + + public synchronized void block(Phaser phaser, Thread thread) { + this.phaser = phaser; + this.thread = thread; + } + + public synchronized void unblock() { + phaser = null; + thread = null; + } + + @Override + public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable { + Phaser phaser; + Thread thread; + synchronized (this) { + phaser = this.phaser; + thread = this.thread; + } + if (phaser != null && Thread.currentThread() == thread) { + arriveAndAwait(phaser); + arriveAndAwait(phaser); + } + return super.visitGetKeyValueCommand(ctx, command); + } + } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/BasicTransactionalTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/BasicTransactionalTestCase.java index ccdecf4bdd4a..6fca62927788 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/BasicTransactionalTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/BasicTransactionalTestCase.java @@ -11,7 +11,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; import org.hibernate.Cache; import org.hibernate.Criteria; @@ -19,12 +18,10 @@ import org.hibernate.NaturalIdLoadAccess; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.criterion.Restrictions; import org.hibernate.stat.SecondLevelCacheStatistics; import org.hibernate.stat.Statistics; - import org.hibernate.testing.TestForIssue; import org.junit.After; import org.junit.Test; @@ -954,7 +951,6 @@ public Citizen call() throws Exception { // TODO: Clear caches manually via cache manager (it's faster!!) this.cleanupCache(); - Thread.sleep(PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD + TimeUnit.SECONDS.toMillis(1)); stats.setStatisticsEnabled( true ); stats.clear(); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java new file mode 100644 index 000000000000..6046c331bcf0 --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/EqualityTest.java @@ -0,0 +1,77 @@ +package org.hibernate.test.cache.infinispan.functional; + +import java.util.concurrent.Callable; + +import org.hibernate.Session; +import org.hibernate.stat.Statistics; +import org.junit.Test; + +import static org.infinispan.test.TestingUtil.withTx; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Persons should be correctly indexed since we can use Type for comparison + * + * @author Radim Vansa <rvansa@redhat.com> + */ +public class EqualityTest extends SingleNodeTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class }; + } + + @Test + public void testEqualityFromType() throws Exception { + Person john = new Person("John", "Black", 26); + Person peter = new Person("Peter", "White", 32); + + withTx(tm, new Callable() { + @Override + public Void call() throws Exception { + Session session = openSession(); + session.getTransaction().begin(); + session.persist(john); + session.persist(peter); + session.getTransaction().commit(); + session.close(); + return null; + } + }); + + Statistics statistics = sessionFactory().getStatistics(); + statistics.clear(); + + for (int i = 0; i < 5; ++i) { + withTx(tm, new Callable() { + @Override + public Void call() throws Exception { + Session session = openSession(); + session.getTransaction().begin(); + Person p1 = session.get(Person.class, john.name); + assertPersonEquals(john, p1); + Person p2 = session.get(Person.class, peter.name); + assertPersonEquals(peter, p2); + Person p3 = session.get(Person.class, new Name("Foo", "Bar")); + assertNull(p3); + session.getTransaction().commit(); + session.close(); + return null; + } + }); + } + + assertTrue(statistics.getSecondLevelCacheHitCount() > 0); + assertTrue(statistics.getSecondLevelCacheMissCount() > 0); + } + + private static void assertPersonEquals(Person expected, Person person) { + assertNotNull(person); + assertNotNull(person.getName()); + assertEquals(expected.getName().getFirstName(), person.getName().getFirstName()); + assertEquals(expected.getName().getLastName(), person.getName().getLastName()); + assertEquals(expected.getAge(), person.getAge()); + } +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/MultiTenancyTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/MultiTenancyTestCase.java new file mode 100644 index 000000000000..c2f2eab6d14f --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/MultiTenancyTestCase.java @@ -0,0 +1,118 @@ +package org.hibernate.test.cache.infinispan.functional; + +import java.util.concurrent.Callable; + +import org.hibernate.MultiTenancyStrategy; +import org.hibernate.Session; +import org.hibernate.boot.SessionFactoryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cache.infinispan.entity.EntityRegionImpl; +import org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider; +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; +import org.hibernate.test.cache.infinispan.tm.XaConnectionProvider; +import org.hibernate.testing.env.ConnectionProviderBuilder; +import org.infinispan.commons.util.CloseableIteratorSet; +import org.infinispan.context.Flag; +import org.junit.Test; + +import static org.infinispan.test.TestingUtil.withTx; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Radim Vansa <rvansa@redhat.com> + */ +public class MultiTenancyTestCase extends SingleNodeTestCase { + + private static final String DB1 = "db1"; + private static final String DB2 = "db2"; + private final ConnectionProvider db1 + = new XaConnectionProvider(ConnectionProviderBuilder.buildConnectionProvider(DB1)); + private final ConnectionProvider db2 + = new XaConnectionProvider(ConnectionProviderBuilder.buildConnectionProvider(DB2)); + + @Override + protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { + super.configureStandardServiceRegistryBuilder(ssrb); + ssrb.addService(MultiTenantConnectionProvider.class, new AbstractMultiTenantConnectionProvider() { + + @Override + protected ConnectionProvider getAnyConnectionProvider() { + return db1; + } + + @Override + protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) { + if (DB1.equals(tenantIdentifier)) return db1; + if (DB2.equals(tenantIdentifier)) return db2; + throw new IllegalArgumentException(); + } + }); + } + + @Override + protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { + super.configureSessionFactoryBuilder(sfb); + sfb.applyMultiTenancyStrategy(MultiTenancyStrategy.DATABASE); + } + + @Override + protected void cleanupTest() throws Exception { + db1.getConnection().close(); + db2.getConnection().close(); + } + + @Test + public void testMultiTenancy() throws Exception { + final Item item = new Item("my item", "description" ); + + long id = withTx(tm, new Callable() { + @Override + public Long call() throws Exception { + Session s = sessionFactory().withOptions().tenantIdentifier(DB1).openSession(); + s.getTransaction().begin(); + s.persist(item); + s.getTransaction().commit(); + s.close(); + return item.getId(); + } + }); + for (int i = 0; i < 5; ++i) { // make sure we get something cached + withTx(tm, new Callable() { + @Override + public Void call() throws Exception { + Session s = sessionFactory().withOptions().tenantIdentifier(DB1).openSession(); + s.getTransaction().begin(); + Item item2 = s.get(Item.class, id); + s.getTransaction().commit(); + s.close(); + assertNotNull(item2); + assertEquals(item.getName(), item2.getName()); + return null; + } + }); + + } + // The table ITEMS is not created in DB2 - we would get just an exception +// for (int i = 0; i < 5; ++i) { // make sure we get something cached +// withTx(tm, new Callable() { +// @Override +// public Void call() throws Exception { +// Session s = sessionFactory().withOptions().tenantIdentifier(DB2).openSession(); +// s.getTransaction().begin(); +// Item item2 = s.get(Item.class, id); +// s.getTransaction().commit(); +// s.close(); +// assertNull(item2); +// return null; +// } +// }); +// } + EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName()); + CloseableIteratorSet keySet = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).keySet(); + assertEquals(1, keySet.size()); + assertEquals("OldCacheKeyImplementation", keySet.iterator().next().getClass().getSimpleName()); + } + +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Name.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Name.java new file mode 100644 index 000000000000..efc9996aca85 --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Name.java @@ -0,0 +1,48 @@ +package org.hibernate.test.cache.infinispan.functional; + +import javax.persistence.Embeddable; +import java.io.Serializable; + +/** + * Test class with incorrectly defined equals and hashCode. + * + * @author Radim Vansa <rvansa@redhat.com> + */ +@Embeddable +public class Name implements Serializable { + String firstName; + String lastName; + + public Name() {} + + public Name(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(Object obj) { + return false; + } +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/NoTenancyTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/NoTenancyTestCase.java new file mode 100644 index 000000000000..e79541c36e74 --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/NoTenancyTestCase.java @@ -0,0 +1,56 @@ +package org.hibernate.test.cache.infinispan.functional; + +import java.util.concurrent.Callable; + +import org.hibernate.Session; +import org.hibernate.cache.infinispan.entity.EntityRegionImpl; +import org.infinispan.commons.util.CloseableIteratorSet; +import org.infinispan.context.Flag; +import org.junit.Test; + +import static org.infinispan.test.TestingUtil.withTx; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Radim Vansa <rvansa@redhat.com> + */ +public class NoTenancyTestCase extends SingleNodeTestCase { + @Test + public void testNoTenancy() throws Exception { + final Item item = new Item("my item", "description" ); + + long id = withTx(tm, new Callable() { + @Override + public Long call() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + s.persist(item); + s.getTransaction().commit(); + s.close(); + return item.getId(); + } + }); + for (int i = 0; i < 5; ++i) { // make sure we get something cached + withTx(tm, new Callable() { + @Override + public Void call() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + Item item2 = s.get(Item.class, id); + s.getTransaction().commit(); + s.close(); + assertNotNull(item2); + assertEquals(item.getName(), item2.getName()); + return null; + } + }); + + } + EntityRegionImpl region = (EntityRegionImpl) sessionFactory().getSecondLevelCacheRegion(Item.class.getName()); + CloseableIteratorSet keySet = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).keySet(); + assertEquals(1, keySet.size()); + assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), keySet.iterator().next().getClass()); + } + +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Person.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Person.java new file mode 100644 index 000000000000..222525c4fff5 --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/Person.java @@ -0,0 +1,41 @@ +package org.hibernate.test.cache.infinispan.functional; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import java.io.Serializable; + +/** + * Test class using EmbeddedId + * + * @author Radim Vansa <rvansa@redhat.com> + */ +@Entity +public class Person implements Serializable { + @EmbeddedId + Name name; + + int age; + + public Person() {} + + public Person(String firstName, String lastName, int age) { + name = new Name(firstName, lastName); + this.age = age; + } + + public Name getName() { + return name; + } + + public void setName(Name name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java index 73d68de78b38..83656d776fe3 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java @@ -6,13 +6,6 @@ */ package org.hibernate.test.cache.infinispan.functional.cluster; -import java.sql.Connection; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import javax.transaction.HeuristicMixedException; import javax.transaction.HeuristicRollbackException; import javax.transaction.RollbackException; @@ -23,6 +16,13 @@ import javax.transaction.xa.XAException; import javax.transaction.xa.XAResource; import javax.transaction.xa.Xid; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; @@ -62,9 +62,11 @@ public void commit() throws RollbackException, HeuristicMixedException, } else { status = Status.STATUS_PREPARING; - for (int i = 0; i < synchronizations.size(); i++) { - Synchronization s = (Synchronization) synchronizations.get(i); - s.beforeCompletion(); + if (synchronizations != null) { + for (int i = 0; i < synchronizations.size(); i++) { + Synchronization s = (Synchronization) synchronizations.get(i); + s.beforeCompletion(); + } } if (!runXaResourcePrepare()) { @@ -89,9 +91,11 @@ public void commit() throws RollbackException, HeuristicMixedException, status = Status.STATUS_COMMITTED; - for (int i = 0; i < synchronizations.size(); i++) { - Synchronization s = (Synchronization) synchronizations.get(i); - s.afterCompletion(status); + if (synchronizations != null) { + for (int i = 0; i < synchronizations.size(); i++) { + Synchronization s = (Synchronization) synchronizations.get(i); + s.afterCompletion(status); + } } // status = Status.STATUS_NO_TRANSACTION; diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/EntityCollectionInvalidationTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/EntityCollectionInvalidationTestCase.java index 5ea6017f99fd..782e14491404 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/EntityCollectionInvalidationTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/EntityCollectionInvalidationTestCase.java @@ -6,11 +6,15 @@ */ package org.hibernate.test.cache.infinispan.functional.cluster; +import javax.transaction.TransactionManager; import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import javax.transaction.TransactionManager; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.test.cache.infinispan.functional.Contact; +import org.hibernate.test.cache.infinispan.functional.Customer; import org.infinispan.Cache; import org.infinispan.manager.CacheContainer; import org.infinispan.notifications.Listener; @@ -21,12 +25,6 @@ import org.jboss.util.collection.ConcurrentSet; import org.junit.Test; -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.cache.spi.CacheKey; -import org.hibernate.test.cache.infinispan.functional.Contact; -import org.hibernate.test.cache.infinispan.functional.Customer; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -345,9 +343,7 @@ public boolean isEmpty() { public void nodeVisited(CacheEntryVisitedEvent event) { log.debug( event.toString() ); if ( !event.isPre() ) { - CacheKey cacheKey = (CacheKey) event.getKey(); - Integer primKey = (Integer) cacheKey.getKey(); - String key = cacheKey.getEntityOrRoleName() + '#' + primKey; + String key = event.getCache().getName() + "#" + event.getKey(); log.debug( "MyListener[" + name + "] - Visiting key " + key ); // String name = fqn.toString(); String token = ".functional."; @@ -355,7 +351,7 @@ public void nodeVisited(CacheEntryVisitedEvent event) { if ( index > -1 ) { index += token.length(); key = key.substring( index ); - log.debug( "MyListener[" + name + "] - recording visit to " + key ); + log.debug( "MyListener[" + this.name + "] - recording visit to " + key ); visited.add( key ); } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/NaturalIdInvalidationTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/NaturalIdInvalidationTestCase.java index 2b1891de0bb0..dd6099b01031 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/NaturalIdInvalidationTestCase.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/NaturalIdInvalidationTestCase.java @@ -6,11 +6,14 @@ */ package org.hibernate.test.cache.infinispan.functional.cluster; +import javax.transaction.TransactionManager; +import java.util.Set; +import java.util.concurrent.Callable; + import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import org.hibernate.cache.spi.NaturalIdCacheKey; import org.hibernate.criterion.Restrictions; import org.hibernate.test.cache.infinispan.functional.Citizen; import org.hibernate.test.cache.infinispan.functional.NaturalIdOnManyToOne; @@ -23,15 +26,8 @@ import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; import org.jboss.util.collection.ConcurrentSet; -import org.junit.After; import org.junit.Test; -import javax.transaction.TransactionManager; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Callable; - -import static org.infinispan.test.TestingUtil.tmpDirectory; import static org.infinispan.test.TestingUtil.withTx; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -263,8 +259,7 @@ public boolean isEmpty() { public void nodeVisited(CacheEntryVisitedEvent event) { log.debug( event.toString() ); if ( !event.isPre() ) { - NaturalIdCacheKey cacheKey = (NaturalIdCacheKey) event.getKey(); - visited.add(cacheKey.toString()); + visited.add(event.getKey().toString()); // Integer primKey = (Integer) cacheKey.getKey(); // String key = (String) cacheKey.getEntityOrRoleName() + '#' + primKey; // log.debug( "MyListener[" + name + "] - Visiting key " + key ); diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/CorrectnessTestCase.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/CorrectnessTestCase.java new file mode 100644 index 000000000000..a4cb2668c347 --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/CorrectnessTestCase.java @@ -0,0 +1,595 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +package org.hibernate.test.cache.infinispan.stress; + +import javax.transaction.Status; +import javax.transaction.TransactionManager; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Environment; +import org.hibernate.criterion.Restrictions; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.RootClass; +import org.hibernate.test.cache.infinispan.stress.entities.Address; +import org.hibernate.test.cache.infinispan.stress.entities.Family; +import org.hibernate.test.cache.infinispan.stress.entities.Person; +import org.infinispan.util.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Tries to execute random operations for {@link #EXECUTION_TIME} and then verify the log for correctness. + * + * Assumes serializable consistency. + * + * @author Radim Vansa + */ +@Ignore // as long-running test, we'll execute it only by hand +public class CorrectnessTestCase { + static final org.infinispan.util.logging.Log log = LogFactory.getLog(CorrectnessTestCase.class); + static final long EXECUTION_TIME = TimeUnit.MINUTES.toMillis(5); + static final int NUM_THREADS = 10; + static final int NUM_FAMILIES = 1; + static final int NUM_ACCESS_AFTER_REMOVAL = NUM_THREADS * 2; + static final String REMOVED = "__REMOVED__"; + + AtomicInteger timestampGenerator = new AtomicInteger(); + ConcurrentSkipListMap familyIds = new ConcurrentSkipListMap<>(); + SessionFactory sessionFactory; + TransactionManager tm; + volatile boolean running = true; + + ThreadLocal>>> familyNames = new ThreadLocal>>>() { + @Override + protected Map>> initialValue() { + return new HashMap<>(); + } + }; + ThreadLocal>>>> familyMembers = new ThreadLocal>>>>() { + @Override + protected Map>>> initialValue() { + return new HashMap<>(); + } + }; + + @Before + public void beforeClass() { + StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().enableAutoClose() + .applySetting( Environment.USE_SECOND_LEVEL_CACHE, "true" ) + .applySetting( Environment.USE_QUERY_CACHE, "true" ) + .applySetting( Environment.DRIVER, "org.h2.Driver" ) + .applySetting( Environment.URL, "jdbc:h2:mem:test") + .applySetting( Environment.DIALECT, H2Dialect.class.getName() ) + .applySetting( Environment.HBM2DDL_AUTO, "create-drop" ) + .applySetting( Environment.CACHE_REGION_FACTORY, "org.hibernate.cache.infinispan.InfinispanRegionFactory" ) + .applySetting( Environment.JTA_PLATFORM, "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" ) + .applySetting( Environment.GENERATE_STATISTICS, "false" ) + ; + + StandardServiceRegistry registry = ssrb.build(); + + Metadata metadata = buildMetadata( registry ); + + sessionFactory = metadata.buildSessionFactory(); + + tm = com.arjuna.ats.jta.TransactionManager.transactionManager(); + } + + @After + public void afterClass() { + sessionFactory.close(); + } + + public static Class[] getAnnotatedClasses() { + return new Class[] {Family.class, Person.class, Address.class}; + } + + private static Metadata buildMetadata(StandardServiceRegistry registry) { + final String cacheStrategy = "transactional"; + + MetadataSources metadataSources = new MetadataSources( registry ); + for ( Class entityClass : getAnnotatedClasses() ) { + metadataSources.addAnnotatedClass( entityClass ); + } + + Metadata metadata = metadataSources.buildMetadata(); + + for ( PersistentClass entityBinding : metadata.getEntityBindings() ) { + if (!entityBinding.isInherited()) { + ( (RootClass) entityBinding ).setCacheConcurrencyStrategy( cacheStrategy); + } + } + + for ( Collection collectionBinding : metadata.getCollectionBindings() ) { + collectionBinding.setCacheConcurrencyStrategy( cacheStrategy ); + } + + return metadata; + } + + @Test + public void test() throws InterruptedException, ExecutionException { + ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS); + + Map>> allFamilyNames = new HashMap<>(); + Map>>> allFamilyMembers = new HashMap<>(); + + running = true; + List> futures = new ArrayList<>(); + for (int i = 0; i < NUM_THREADS; ++i) { + futures.add(exec.submit(() -> { + ThreadLocalRandom random = ThreadLocalRandom.current(); + while (running) { + Operation operation; + if (familyIds.size() < NUM_FAMILIES) { + operation = new InsertFamily(); + } else { + int r = random.nextInt(100); + if (r == 0) operation = new InvalidateCache(); + else if (r < 5) operation = new QueryFamilies(); + else if (r < 10) operation = new RemoveFamily(); + else if (r < 20) operation = new UpdateFamily(); + else if (r < 35) operation = new AddMember(); + else if (r < 50) operation = new RemoveMember(); + else operation = new ReadFamily(r < 75); + } + try { + operation.run(); + } catch (Exception e) { + // ignore exceptions from optimistic failures + } + } + synchronized (allFamilyNames) { + for (Map.Entry>> entry : familyNames.get().entrySet()) { + List> list = allFamilyNames.get(entry.getKey()); + if (list == null) allFamilyNames.put(entry.getKey(), list = new ArrayList<>()); + list.addAll(entry.getValue()); + } + for (Map.Entry>>> entry : familyMembers.get().entrySet()) { + List>> list = allFamilyMembers.get(entry.getKey()); + if (list == null) allFamilyMembers.put(entry.getKey(), list = new ArrayList<>()); + list.addAll(entry.getValue()); + } + } + return null; + })); + } + Thread.sleep(EXECUTION_TIME); + running = false; + exec.shutdown(); + if (!exec.awaitTermination(1000, TimeUnit.SECONDS)) throw new IllegalStateException(); + for (Future f : futures) { + f.get(); // check for exceptions + } + + log.infof("Generated %d timestamps%n", timestampGenerator.get()); + AtomicInteger created = new AtomicInteger(); + AtomicInteger removed = new AtomicInteger(); + ForkJoinPool threadPool = ForkJoinPool.commonPool(); + ArrayList> tasks = new ArrayList<>(); + for (List> list : allFamilyNames.values()) { + tasks.add(threadPool.submit(() -> { + created.incrementAndGet(); + NavigableMap>> logByTime = getWritesAtTime(list); + checkCorrectness(list, logByTime); + if (list.stream().anyMatch(l -> !l.read && l.getValue() == null)) { + removed.incrementAndGet(); + } + })); + } + for (List>> list : allFamilyMembers.values()) { + tasks.add(threadPool.submit(() -> { + NavigableMap>>> logByTime = getWritesAtTime(list); + checkCorrectness(list, logByTime); + })); + } + for (ForkJoinTask task : tasks) { + task.get(); // propagate exception + } + log.infof("Created %d families, removed %d%n", created.get(), removed.get()); + } + + private NavigableMap>> getWritesAtTime(List> list) { + NavigableMap>> writes = new TreeMap<>(); + for (Log log : list) { + if (log.read) continue; + for (int time = log.before; time <= log.after; ++time) { + List> onTime = writes.get(time); + if (onTime == null) { + writes.put(time, onTime = new ArrayList<>()); + } + onTime.add(log); + } + } + return writes; + } + + private void checkCorrectness(List> logs, NavigableMap>> writesByTime) { + int nullReads = 0, reads = 0, writes = 0; + for (Log read : logs) { + if (!read.read) { + writes++; + continue; + } + if (read.getValue() == null || isEmptyCollection(read)) nullReads++; + else reads++; + + Map> possibleValues = new HashMap<>(); + for (List> list : writesByTime.subMap(read.before, true, read.after, true).values()) { + for (Log write : list) { + if (read.precedes(write)) continue; + possibleValues.put(write.getValue(), write); + } + } + int startOfLastWriteBeforeRead = 0; + for (Map.Entry>> entry : writesByTime.headMap(read.before, false).descendingMap().entrySet()) { + int time = entry.getKey(); + if (time < startOfLastWriteBeforeRead) break; + for (Log write : entry.getValue()) { + if (write.after < read.before && write.before > startOfLastWriteBeforeRead) { + startOfLastWriteBeforeRead = write.before; + } + possibleValues.put(write.getValue(), write); + } + } + + if (!possibleValues.containsKey(read.getValue())) { + throw new IllegalStateException(String.format("R: %d .. %d (%s, %s) -> %s not in %s (%d+)", + read.before, read.after, read.threadName, new SimpleDateFormat("HH:mm:ss,SSS").format(new Date(read.wallClockTime)), + read.getValue(), possibleValues.values(), startOfLastWriteBeforeRead)); + } + } + log.infof("Checked %d null reads, %d reads and %d writes%n", nullReads, reads, writes); + } + + private static boolean isEmptyCollection(Log read) { + return read.getValue() instanceof java.util.Collection && ((java.util.Collection) read.getValue()).isEmpty(); + } + + private abstract class Operation { + public abstract void run() throws Exception; + + protected void withSession(Consumer consumer) throws Exception { + tm.begin(); + try { + Session s = sessionFactory.openSession(); + s.getTransaction().begin(); + consumer.accept(s); + s.getTransaction().commit(); + s.close(); + } catch (RuntimeException e) { + tm.setRollbackOnly(); + throw e; + } finally { + if (tm.getStatus() == Status.STATUS_ACTIVE) { + tm.commit(); + } else { + tm.rollback(); + } + } + } + + protected void withRandomFamily(BiConsumer consumer, Optional familyNameUpdate, Optional> familyMembersUpdate) throws Exception { + int id = randomFamilyId(ThreadLocalRandom.current()); + int before = timestampGenerator.getAndIncrement(); + log.tracef("Started %s at %d", getClass().getSimpleName(), before); + Log familyNameLog = new Log<>(true); + Log> familyMembersLog = new Log<>(true); + + withSession(s -> { + Family f = s.get(Family.class, id); + if (f == null) { + familyNameLog.setValue(null); + familyMembersLog.setValue(Collections.EMPTY_SET); + familyNotFound(id); + } else { + familyNameLog.setValue(f.getName()); + familyMembersLog.setValue(membersToNames(f.getMembers())); + consumer.accept(s, f); + } + }); + + int after = timestampGenerator.getAndIncrement(); + log.tracef("Finished %s at %d", getClass().getSimpleName(), after); + familyNameLog.setTimes(before, after); + familyMembersLog.setTimes(before, after); + + getRecordList(familyNames, id).add(familyNameLog); + getRecordList(familyMembers, id).add(familyMembersLog); + + if (familyNameLog.getValue() != null) { + if (familyNameUpdate.isPresent()) { + getRecordList(familyNames, id).add(new Log<>(before, after, familyNameUpdate.get() == REMOVED ? null : familyNameUpdate.get(), false, familyNameLog)); + } + if (familyMembersUpdate.isPresent()) { + getRecordList(familyMembers, id).add(new Log<>(before, after, familyMembersUpdate.get(), false, familyMembersLog)); + } + } + } + } + + private class InsertFamily extends Operation { + @Override + public void run() throws Exception { + Family family = createFamily(); + int before = timestampGenerator.getAndIncrement(); + log.trace("Started InsertFamily at " + before); + withSession(s -> s.persist(family)); + familyIds.put(family.getId(), new AtomicInteger(NUM_ACCESS_AFTER_REMOVAL)); + int after = timestampGenerator.getAndIncrement(); + log.trace("Finished InsertFamily at " + after); + getRecordList(familyNames, family.getId()).add(new Log<>(before, after, family.getName(), false)); + getRecordList(familyMembers, family.getId()).add(new Log<>(before, after, membersToNames(family.getMembers()), false)); + } + } + + private Set membersToNames(Set members) { + return members.stream().map(p -> p.getFirstName()).collect(Collectors.toSet()); + } + + private class ReadFamily extends Operation { + private final boolean evict; + + public ReadFamily(boolean evict) { + this.evict = evict; + } + + @Override + public void run() throws Exception { + withRandomFamily((s, f) -> { + if (evict) { + sessionFactory.getCache().evictEntity(Family.class, f.getId()); + } + }, Optional.empty(), Optional.empty()); + } + } + + private class UpdateFamily extends Operation { + @Override + public void run() throws Exception { + String newName = randomString(ThreadLocalRandom.current()); + withRandomFamily((s, f) -> { + f.setName(newName); + s.persist(f); + }, Optional.of(newName), Optional.empty()); + } + } + + private class RemoveFamily extends Operation { + @Override + public void run() throws Exception { + withRandomFamily((s, f) -> s.delete(f), Optional.of(REMOVED), Optional.of(Collections.EMPTY_SET)); + } + } + + private abstract class MemberOperation extends Operation { + @Override + public void run() throws Exception { + Set newMembers = new HashSet<>(); + withRandomFamily((s, f) -> { + updateMembers(s, ThreadLocalRandom.current(), f); + newMembers.addAll(membersToNames(f.getMembers())); + s.persist(f); + }, Optional.empty(), Optional.of(newMembers)); + } + + protected abstract void updateMembers(Session s, ThreadLocalRandom random, Family f); + } + + private class AddMember extends MemberOperation { + protected void updateMembers(Session s, ThreadLocalRandom random, Family f) { + f.getMembers().add(createPerson(random, f)); + } + } + + private class RemoveMember extends MemberOperation { + @Override + protected void updateMembers(Session s, ThreadLocalRandom random, Family f) { + int numMembers = f.getMembers().size(); + if (numMembers > 0) { + Iterator it = f.getMembers().iterator(); + Person person = null; + for (int i = random.nextInt(numMembers); i >= 0; --i) { + person = it.next(); + } + it.remove(); + if (person != null) { + s.delete(person); + } + } + } + } + + private class QueryFamilies extends Operation { + final static int MAX_RESULTS = 10; + + @Override + public void run() throws Exception { + String prefix = new StringBuilder(2) + .append((char) ThreadLocalRandom.current().nextInt('A', 'Z' + 1)).append('%').toString(); + int[] ids = new int[MAX_RESULTS]; + String[] names = new String[MAX_RESULTS]; + Set[] members = new Set[MAX_RESULTS]; + + int before = timestampGenerator.getAndIncrement(); + log.tracef("Started QueryFamilies at %d", before); + withSession(s -> { + List results = s.createCriteria(Family.class) + .add(Restrictions.like("name", prefix)) + .setMaxResults(MAX_RESULTS) + .setCacheable(true) + .list(); + int index = 0; + for (Family f : results) { + ids[index] = f.getId(); + names[index] = f.getName(); + members[index] = membersToNames(f.getMembers()); + ++index; + } + }); + + int after = timestampGenerator.getAndIncrement(); + log.tracef("Finsihed QueryFamilies at %d", after); + for (int index = 0; index < MAX_RESULTS; ++index) { + if (names[index] == null) break; + getRecordList(familyNames, ids[index]).add(new Log<>(before, after, names[index], true)); + getRecordList(familyMembers, ids[index]).add(new Log<>(before, after, members[index], true)); + } + } + } + + private class InvalidateCache extends Operation { + @Override + public void run() throws Exception { + log.trace("Invalidating all caches"); + tm.begin(); + try { + sessionFactory.getCache().evictAllRegions(); + } catch (RuntimeException e) { + tm.setRollbackOnly(); + throw e; + } finally { + if (tm.getStatus() == Status.STATUS_ACTIVE) { + tm.commit(); + } else { + tm.rollback(); + } + } + } + } + + private void familyNotFound(int id) { + AtomicInteger access = familyIds.get(id); + if (access == null) return; + if (access.decrementAndGet() == 0) { + familyIds.remove(id); + } + } + + private List getRecordList(ThreadLocal>> tlListMap, int id) { + Map> map = tlListMap.get(); + List list = map.get(id); + if (list == null) map.put(id, list = new ArrayList<>()); + return list; + } + + private int randomFamilyId(ThreadLocalRandom random) { + Integer first = familyIds.firstKey(); + Integer last = familyIds.lastKey(); + if (first == null || last == null) return 0; + return familyIds.ceilingKey(random.nextInt(first, last + 1)); + } + + private static Family createFamily() { + ThreadLocalRandom random = ThreadLocalRandom.current(); + String familyName = randomString(random); + Family f = new Family(familyName); + HashSet members = new HashSet<>(); + members.add(createPerson(random, f)); + f.setMembers(members); + return f; + } + + private static Person createPerson(ThreadLocalRandom random, Family family) { + return new Person(randomString(random), family); + } + + private static String randomString(ThreadLocalRandom random) { + StringBuilder sb = new StringBuilder(10); + for (int i = 0; i < 10; ++i) { + sb.append((char) random.nextInt('A', 'Z' + 1)); + } + return sb.toString(); + } + + private class Log { + int before; + int after; + T value; + boolean read; + Log[] preceding; + String threadName; + long wallClockTime; + + public Log(int time) { + this(); + this.before = time; + this.after = time; + } + + public Log(int before, int after, T value, boolean read, Log... preceding) { + this(); + this.before = before; + this.after = after; + this.value = value; + this.read = read; + this.preceding = preceding; + } + + public Log(boolean read) { + this(); + this.read = read; + } + + protected Log() { + threadName = Thread.currentThread().getName(); + wallClockTime = System.currentTimeMillis(); + } + + public void setTimes(int before, int after) { + this.before = before; + this.after = after; + } + + public void setValue(T value) { + this.value = value; + } + + public T getValue() { + return value; + } + + public boolean precedes(Log write) { + if (write.preceding == null) return false; + for (Log l : write.preceding) { + if (l == this) return true; + } + return false; + } + + @Override + public String toString() { + return String.format("%c: %s (%s, %s), %d - %d", read ? 'R' : 'W', value, threadName, + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date(wallClockTime)), before, after); + } + } +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Address.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Address.java index d547a857006b..0a52e8e9ac75 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Address.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Address.java @@ -153,6 +153,7 @@ public boolean equals(Object o) { Address address = (Address) o; + // inhabitants must not be in the comparison since we would end up in infinite recursion if (id != address.id) return false; if (streetNumber != address.streetNumber) return false; if (version != address.version) return false; @@ -160,8 +161,6 @@ public boolean equals(Object o) { return false; if (countryName != null ? !countryName.equals(address.countryName) : address.countryName != null) return false; - if (inhabitants != null ? !inhabitants.equals(address.inhabitants) : address.inhabitants != null) - return false; if (streetName != null ? !streetName.equals(address.streetName) : address.streetName != null) return false; if (zipCode != null ? !zipCode.equals(address.zipCode) : address.zipCode != null) @@ -177,7 +176,6 @@ public int hashCode() { result = 31 * result + (cityName != null ? cityName.hashCode() : 0); result = 31 * result + (countryName != null ? countryName.hashCode() : 0); result = 31 * result + (zipCode != null ? zipCode.hashCode() : 0); - result = 31 * result + (inhabitants != null ? inhabitants.hashCode() : 0); result = 31 * result + id; result = 31 * result + version; return result; diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Family.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Family.java index ebe7f1a8b887..9f323a518457 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Family.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/stress/entities/Family.java @@ -7,10 +7,12 @@ package org.hibernate.test.cache.infinispan.stress.entities; +import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.Version; import java.util.HashSet; import java.util.Set; @@ -22,8 +24,9 @@ public final class Family { private int id; private String name; private String secondName; - @OneToMany + @OneToMany(cascade = CascadeType.ALL) private Set members; + @Version private int version; public Family(String name) { @@ -97,10 +100,9 @@ public boolean equals(Object o) { Family family = (Family) o; + // members must not be in the comparison since we would end up in infinite recursive call if (id != family.id) return false; if (version != family.version) return false; - if (members != null ? !members.equals(family.members) : family.members != null) - return false; if (name != null ? !name.equals(family.name) : family.name != null) return false; if (secondName != null ? !secondName.equals(family.secondName) : family.secondName != null) @@ -113,7 +115,6 @@ public boolean equals(Object o) { public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (secondName != null ? secondName.hashCode() : 0); - result = 31 * result + (members != null ? members.hashCode() : 0); result = 31 * result + id; result = 31 * result + version; return result; diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaConnectionProvider.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaConnectionProvider.java index 93074c5eaa6f..d44349d822a6 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaConnectionProvider.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaConnectionProvider.java @@ -23,10 +23,19 @@ * @since 3.5 */ public class XaConnectionProvider implements ConnectionProvider { - private static ConnectionProvider actualConnectionProvider = ConnectionProviderBuilder.buildConnectionProvider(); + private final static ConnectionProvider DEFAULT_CONNECTION_PROVIDER = ConnectionProviderBuilder.buildConnectionProvider(); + private final ConnectionProvider actualConnectionProvider; private boolean isTransactional; - public static ConnectionProvider getActualConnectionProvider() { + public XaConnectionProvider() { + this(DEFAULT_CONNECTION_PROVIDER); + } + + public XaConnectionProvider(ConnectionProvider connectionProvider) { + this.actualConnectionProvider = connectionProvider; + } + + public ConnectionProvider getActualConnectionProvider() { return actualConnectionProvider; } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionManagerImpl.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionManagerImpl.java index a02943159dae..17ae60596cd7 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionManagerImpl.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionManagerImpl.java @@ -23,41 +23,44 @@ */ public class XaTransactionManagerImpl implements TransactionManager { private static final XaTransactionManagerImpl INSTANCE = new XaTransactionManagerImpl(); - private XaTransactionImpl currentTransaction; + private final ThreadLocal currentTransaction = new ThreadLocal<>(); public static XaTransactionManagerImpl getInstance() { return INSTANCE; } public int getStatus() throws SystemException { + XaTransactionImpl currentTransaction = this.currentTransaction.get(); return currentTransaction == null ? Status.STATUS_NO_TRANSACTION : currentTransaction.getStatus(); } public Transaction getTransaction() throws SystemException { - return currentTransaction; + return currentTransaction.get(); } public XaTransactionImpl getCurrentTransaction() { - return currentTransaction; + return currentTransaction.get(); } public void begin() throws NotSupportedException, SystemException { - currentTransaction = new XaTransactionImpl(this); + if (currentTransaction.get() != null) throw new IllegalStateException("Transaction already started."); + currentTransaction.set(new XaTransactionImpl(this)); } public Transaction suspend() throws SystemException { - Transaction suspended = currentTransaction; - currentTransaction = null; + Transaction suspended = currentTransaction.get(); + currentTransaction.remove(); return suspended; } public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { - currentTransaction = (XaTransactionImpl) transaction; + currentTransaction.set((XaTransactionImpl) transaction); } public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException { + XaTransactionImpl currentTransaction = this.currentTransaction.get(); if (currentTransaction == null) { throw new IllegalStateException("no current transaction to commit"); } @@ -65,6 +68,7 @@ public void commit() throws RollbackException, HeuristicMixedException, Heuristi } public void rollback() throws IllegalStateException, SecurityException, SystemException { + XaTransactionImpl currentTransaction = this.currentTransaction.get(); if (currentTransaction == null) { throw new IllegalStateException("no current transaction"); } @@ -72,6 +76,7 @@ public void rollback() throws IllegalStateException, SecurityException, SystemEx } public void setRollbackOnly() throws IllegalStateException, SystemException { + XaTransactionImpl currentTransaction = this.currentTransaction.get(); if (currentTransaction == null) { throw new IllegalStateException("no current transaction"); } @@ -82,8 +87,6 @@ public void setTransactionTimeout(int i) throws SystemException { } void endCurrent(Transaction transaction) { - if (transaction == currentTransaction) { - currentTransaction = null; - } + currentTransaction.remove(); } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/util/TestingKeyFactory.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/util/TestingKeyFactory.java new file mode 100644 index 000000000000..26c695d8b1be --- /dev/null +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/util/TestingKeyFactory.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.cache.infinispan.util; + +import java.io.Serializable; + +public class TestingKeyFactory { + + private TestingKeyFactory() { + //Not to be constructed + } + + public static Object generateEntityCacheKey(String id) { + return new TestingEntityCacheKey( id ); + } + + public static Object generateCollectionCacheKey(String id) { + return new TestingEntityCacheKey( id ); + } + + //For convenience implement both interfaces. + private static class TestingEntityCacheKey implements Serializable { + + private final String id; + + public TestingEntityCacheKey(String id) { + this.id = id; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TestingEntityCacheKey other = (TestingEntityCacheKey) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + } + +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java index 92716139a50e..973b7dd5012d 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java @@ -6,13 +6,17 @@ */ package org.hibernate.testing.cache; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * @author Strong Liu */ class BaseCollectionRegionAccessStrategy extends BaseRegionAccessStrategy implements CollectionRegionAccessStrategy { + private final CollectionRegionImpl region; BaseCollectionRegionAccessStrategy(CollectionRegionImpl region) { @@ -33,4 +37,15 @@ protected boolean isDefaultMinimalPutOverride() { public CollectionRegion getRegion() { return region; } + + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } + } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java index 920f86c8b7d0..57023e59ba49 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java @@ -7,21 +7,24 @@ package org.hibernate.testing.cache; import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * @author Strong Liu */ class BaseEntityRegionAccessStrategy extends BaseRegionAccessStrategy implements EntityRegionAccessStrategy { + private final EntityRegionImpl region; BaseEntityRegionAccessStrategy(EntityRegionImpl region) { this.region = region; } - @Override public EntityRegion getRegion() { return region; @@ -58,4 +61,14 @@ protected BaseGeneralDataRegion getInternalRegion() { protected boolean isDefaultMinimalPutOverride() { return region.getSettings().isMinimalPutsEnabled(); } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseNaturalIdRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseNaturalIdRegionAccessStrategy.java index 3afdcda7afb1..560b46a31f90 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseNaturalIdRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseNaturalIdRegionAccessStrategy.java @@ -7,9 +7,12 @@ package org.hibernate.testing.cache; import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * @author Eric Dalquist @@ -55,4 +58,14 @@ public boolean afterUpdate(Object key, Object value, SoftLock lock) throws Cache BaseNaturalIdRegionAccessStrategy(NaturalIdRegionImpl region) { this.region = region; } + + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session ); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java index 2f52739736c5..7b277e026fc1 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java @@ -9,15 +9,14 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.access.RegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; - import org.jboss.logging.Logger; /** * @author Strong Liu */ abstract class BaseRegionAccessStrategy implements RegionAccessStrategy { - private static final Logger LOG = Logger.getLogger( BaseRegionAccessStrategy.class ); + private static final Logger LOG = Logger.getLogger( BaseRegionAccessStrategy.class ); protected abstract BaseGeneralDataRegion getInternalRegion(); diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java index 73c00984c7c1..18bfb5d5bf5f 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java @@ -8,7 +8,6 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.access.SoftLock; - import org.jboss.logging.Logger; /** diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java index 7d710343f4be..d73eeb81d7e1 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java @@ -8,8 +8,11 @@ import java.util.Comparator; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; /** * @author Strong Liu @@ -42,4 +45,14 @@ protected boolean isDefaultMinimalPutOverride() { public CollectionRegion getRegion() { return region; } + + @Override + public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getCollectionId(cacheKey); + } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java index ee94d910c6f3..42283092b776 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java @@ -9,9 +9,12 @@ import java.util.Comparator; import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.EntityRegion; import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * @author Strong Liu @@ -103,4 +106,14 @@ Comparator getVersionComparator() { public EntityRegion getRegion() { return region; } + + @Override + public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) { + return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier ); + } + + @Override + public Object getCacheKeyId(Object cacheKey) { + return DefaultCacheKeysFactory.getEntityId(cacheKey); + } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteNaturalIdRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteNaturalIdRegionAccessStrategy.java index 476cd77347c6..caefe030e71a 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteNaturalIdRegionAccessStrategy.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteNaturalIdRegionAccessStrategy.java @@ -9,9 +9,12 @@ import java.util.Comparator; import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; import org.hibernate.cache.spi.NaturalIdRegion; import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; /** * @author Eric Dalquist @@ -101,4 +104,14 @@ protected boolean isDefaultMinimalPutOverride() { public NaturalIdRegion getRegion() { return region; } + + @Override + public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) { + return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session ); + } + + @Override + public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey); + } }