Skip to content

Commit f85e924

Browse files
committed
HHH-7631 - Improve performance of UpdateTimestampsCache
1 parent a8a3f9b commit f85e924

File tree

1 file changed

+59
-64
lines changed

1 file changed

+59
-64
lines changed

hibernate-core/src/main/java/org/hibernate/cache/spi/UpdateTimestampsCache.java

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.Serializable;
2727
import java.util.Properties;
2828
import java.util.Set;
29-
import java.util.concurrent.locks.ReentrantReadWriteLock;
3029

3130
import org.jboss.logging.Logger;
3231

@@ -51,100 +50,96 @@ public class UpdateTimestampsCache {
5150
public static final String REGION_NAME = UpdateTimestampsCache.class.getName();
5251
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, UpdateTimestampsCache.class.getName() );
5352

54-
private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
55-
private final TimestampsRegion region;
5653
private final SessionFactoryImplementor factory;
54+
private final TimestampsRegion region;
5755

5856
public UpdateTimestampsCache(Settings settings, Properties props, final SessionFactoryImplementor factory) throws HibernateException {
5957
this.factory = factory;
60-
String prefix = settings.getCacheRegionPrefix();
61-
String regionName = prefix == null ? REGION_NAME : prefix + '.' + REGION_NAME;
58+
final String prefix = settings.getCacheRegionPrefix();
59+
final String regionName = prefix == null ? REGION_NAME : prefix + '.' + REGION_NAME;
60+
6261
LOG.startingUpdateTimestampsCache( regionName );
6362
this.region = settings.getRegionFactory().buildTimestampsRegion( regionName, props );
6463
}
64+
6565
@SuppressWarnings({"UnusedDeclaration"})
66-
public UpdateTimestampsCache(Settings settings, Properties props)
67-
throws HibernateException {
68-
this(settings, props, null);
66+
public UpdateTimestampsCache(Settings settings, Properties props) throws HibernateException {
67+
this( settings, props, null );
6968
}
7069

7170
@SuppressWarnings({"UnnecessaryBoxing"})
7271
public void preinvalidate(Serializable[] spaces) throws CacheException {
73-
readWriteLock.writeLock().lock();
72+
final boolean debug = LOG.isDebugEnabled();
73+
final boolean stats = factory != null && factory.getStatistics().isStatisticsEnabled();
7474

75-
try {
76-
Long ts = region.nextTimestamp() + region.getTimeout();
77-
for ( Serializable space : spaces ) {
75+
final Long ts = region.nextTimestamp() + region.getTimeout();
76+
77+
for ( Serializable space : spaces ) {
78+
if ( debug ) {
7879
LOG.debugf( "Pre-invalidating space [%s], timestamp: %s", space, ts );
79-
//put() has nowait semantics, is this really appropriate?
80-
//note that it needs to be async replication, never local or sync
81-
region.put( space, ts );
82-
if ( factory != null && factory.getStatistics().isStatisticsEnabled() ) {
83-
factory.getStatisticsImplementor().updateTimestampsCachePut();
84-
}
8580
}
86-
}
87-
finally {
88-
readWriteLock.writeLock().unlock();
81+
//put() has nowait semantics, is this really appropriate?
82+
//note that it needs to be async replication, never local or sync
83+
region.put( space, ts );
84+
if ( stats ) {
85+
factory.getStatisticsImplementor().updateTimestampsCachePut();
86+
}
8987
}
9088
}
9189

92-
@SuppressWarnings({"UnnecessaryBoxing"})
90+
@SuppressWarnings({"UnnecessaryBoxing"})
9391
public void invalidate(Serializable[] spaces) throws CacheException {
94-
readWriteLock.writeLock().lock();
92+
final boolean debug = LOG.isDebugEnabled();
93+
final boolean stats = factory != null && factory.getStatistics().isStatisticsEnabled();
9594

96-
try {
97-
Long ts = region.nextTimestamp();
98-
for (Serializable space : spaces) {
95+
final Long ts = region.nextTimestamp();
96+
97+
for (Serializable space : spaces) {
98+
if ( debug ) {
9999
LOG.debugf( "Invalidating space [%s], timestamp: %s", space, ts );
100-
//put() has nowait semantics, is this really appropriate?
101-
//note that it needs to be async replication, never local or sync
102-
region.put( space, ts );
103-
if ( factory != null && factory.getStatistics().isStatisticsEnabled() ) {
104-
factory.getStatisticsImplementor().updateTimestampsCachePut();
105-
}
106100
}
107-
}
108-
finally {
109-
readWriteLock.writeLock().unlock();
101+
//put() has nowait semantics, is this really appropriate?
102+
//note that it needs to be async replication, never local or sync
103+
region.put( space, ts );
104+
if ( stats ) {
105+
factory.getStatisticsImplementor().updateTimestampsCachePut();
106+
}
110107
}
111108
}
112109

113110
@SuppressWarnings({"unchecked", "UnnecessaryUnboxing"})
114111
public boolean isUpToDate(Set spaces, Long timestamp) throws HibernateException {
115-
readWriteLock.readLock().lock();
116-
117-
try {
118-
for ( Serializable space : (Set<Serializable>) spaces ) {
119-
Long lastUpdate = (Long) region.get( space );
120-
if ( lastUpdate == null ) {
121-
if ( factory != null && factory.getStatistics().isStatisticsEnabled() ) {
122-
factory.getStatisticsImplementor().updateTimestampsCacheMiss();
123-
}
124-
//the last update timestamp was lost from the cache
125-
//(or there were no updates since startup!)
126-
//updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );
127-
//result = false; // safer
112+
final boolean debug = LOG.isDebugEnabled();
113+
final boolean stats = factory != null && factory.getStatistics().isStatisticsEnabled();
114+
115+
for ( Serializable space : (Set<Serializable>) spaces ) {
116+
Long lastUpdate = (Long) region.get( space );
117+
if ( lastUpdate == null ) {
118+
if ( stats ) {
119+
factory.getStatisticsImplementor().updateTimestampsCacheMiss();
120+
}
121+
//the last update timestamp was lost from the cache
122+
//(or there were no updates since startup!)
123+
//updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );
124+
//result = false; // safer
125+
}
126+
else {
127+
if ( debug ) {
128+
LOG.debugf(
129+
"[%s] last update timestamp: %s",
130+
space,
131+
lastUpdate + ", result set timestamp: " + timestamp
132+
);
128133
}
129-
else {
130-
if ( LOG.isDebugEnabled() ) {
131-
LOG.debugf(
132-
"[%s] last update timestamp: %s",
133-
space,
134-
lastUpdate + ", result set timestamp: " + timestamp
135-
);
136-
}
137-
if ( factory != null && factory.getStatistics().isStatisticsEnabled() ) {
138-
factory.getStatisticsImplementor().updateTimestampsCacheHit();
139-
}
140-
if ( lastUpdate >= timestamp ) return false;
134+
if ( stats ) {
135+
factory.getStatisticsImplementor().updateTimestampsCacheHit();
136+
}
137+
if ( lastUpdate >= timestamp ) {
138+
return false;
141139
}
142140
}
143-
return true;
144-
}
145-
finally {
146-
readWriteLock.readLock().unlock();
147141
}
142+
return true;
148143
}
149144

150145
public void clear() throws CacheException {

0 commit comments

Comments
 (0)