|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.cache.infinispan.access; |
| 8 | + |
| 9 | +import org.hibernate.cache.CacheException; |
| 10 | +import org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion; |
| 11 | +import org.hibernate.cache.infinispan.util.Caches; |
| 12 | +import org.hibernate.cache.infinispan.util.VersionedEntry; |
| 13 | +import org.hibernate.cache.spi.access.SoftLock; |
| 14 | +import org.hibernate.cache.spi.entry.CacheEntry; |
| 15 | +import org.hibernate.engine.spi.SessionImplementor; |
| 16 | +import org.hibernate.resource.transaction.TransactionCoordinator; |
| 17 | +import org.infinispan.AdvancedCache; |
| 18 | +import org.infinispan.configuration.cache.Configuration; |
| 19 | +import org.infinispan.context.Flag; |
| 20 | +import org.infinispan.util.logging.Log; |
| 21 | +import org.infinispan.util.logging.LogFactory; |
| 22 | + |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +/** |
| 27 | + * Access delegate that relaxes the consistency a bit: stale reads are prohibited only after the transaction |
| 28 | + * commits. This should also be able to work with async caches, and that would allow the replication delay |
| 29 | + * even after the commit. |
| 30 | + * |
| 31 | + * @author Radim Vansa <[email protected]> |
| 32 | + */ |
| 33 | +public class NonStrictAccessDelegate implements AccessDelegate { |
| 34 | + private static final Log log = LogFactory.getLog( NonStrictAccessDelegate.class ); |
| 35 | + |
| 36 | + private final BaseTransactionalDataRegion region; |
| 37 | + private final AdvancedCache cache; |
| 38 | + private final AdvancedCache writeCache; |
| 39 | + private final AdvancedCache putFromLoadCache; |
| 40 | + private final Comparator versionComparator; |
| 41 | + |
| 42 | + |
| 43 | + public NonStrictAccessDelegate(BaseTransactionalDataRegion region) { |
| 44 | + this.region = region; |
| 45 | + this.cache = region.getCache(); |
| 46 | + this.writeCache = Caches.ignoreReturnValuesCache(cache); |
| 47 | + this.putFromLoadCache = writeCache.withFlags( Flag.ZERO_LOCK_ACQUISITION_TIMEOUT, Flag.FAIL_SILENTLY ); |
| 48 | + Configuration configuration = cache.getCacheConfiguration(); |
| 49 | + if (configuration.clustering().cacheMode().isInvalidation()) { |
| 50 | + throw new IllegalArgumentException("Nonstrict-read-write mode cannot use invalidation."); |
| 51 | + } |
| 52 | + if (configuration.transaction().transactionMode().isTransactional()) { |
| 53 | + throw new IllegalArgumentException("Currently transactional caches are not supported."); |
| 54 | + } |
| 55 | + this.versionComparator = region.getCacheDataDescription().getVersionComparator(); |
| 56 | + if (versionComparator == null) { |
| 57 | + throw new IllegalArgumentException("This strategy requires versioned entities/collections but region " + region.getName() + " contains non-versioned data!"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object get(SessionImplementor session, Object key, long txTimestamp) throws CacheException { |
| 63 | + if (txTimestamp < region.getLastRegionInvalidation() ) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + Object value = cache.get(key); |
| 67 | + if (value instanceof VersionedEntry) { |
| 68 | + return ((VersionedEntry) value).getValue(); |
| 69 | + } |
| 70 | + return value; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean putFromLoad(SessionImplementor session, Object key, Object value, long txTimestamp, Object version) { |
| 75 | + return putFromLoad(session, key, value, txTimestamp, version, false); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean putFromLoad(SessionImplementor session, Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) throws CacheException { |
| 80 | + long lastRegionInvalidation = region.getLastRegionInvalidation(); |
| 81 | + if (txTimestamp < lastRegionInvalidation) { |
| 82 | + log.tracef("putFromLoad not executed since tx started at %d, before last region invalidation finished = %d", txTimestamp, lastRegionInvalidation); |
| 83 | + return false; |
| 84 | + } |
| 85 | + assert version != null; |
| 86 | + |
| 87 | + if (minimalPutOverride) { |
| 88 | + Object prev = cache.get(key); |
| 89 | + if (prev != null) { |
| 90 | + Object oldVersion = getVersion(prev); |
| 91 | + if (oldVersion != null) { |
| 92 | + if (versionComparator.compare(version, oldVersion) <= 0) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + else if (prev instanceof VersionedEntry && txTimestamp <= ((VersionedEntry) prev).getTimestamp()) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + // we can't use putForExternalRead since the PFER flag means that entry is not wrapped into context |
| 102 | + // when it is present in the container. TombstoneCallInterceptor will deal with this. |
| 103 | + if (!(value instanceof CacheEntry)) { |
| 104 | + value = new VersionedEntry(value, version, txTimestamp); |
| 105 | + } |
| 106 | + putFromLoadCache.put(key, value); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean insert(SessionImplementor session, Object key, Object value, Object version) throws CacheException { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean update(SessionImplementor session, Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void remove(SessionImplementor session, Object key) throws CacheException { |
| 122 | + Object value = cache.get(key); |
| 123 | + Object version = getVersion(value); |
| 124 | + // there's no 'afterRemove', so we have to use our own synchronization |
| 125 | + TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator(); |
| 126 | + RemovalSynchronization sync = new RemovalSynchronization(transactionCoordinator, writeCache, false, region, key, version); |
| 127 | + transactionCoordinator.getLocalSynchronizations().registerSynchronization(sync); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void removeAll() throws CacheException { |
| 132 | + region.beginInvalidation(); |
| 133 | + try { |
| 134 | + Caches.broadcastEvictAll(cache); |
| 135 | + } |
| 136 | + finally { |
| 137 | + region.endInvalidation(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void evict(Object key) throws CacheException { |
| 143 | + writeCache.put(key, new VersionedEntry(null, null, region.nextTimestamp()), region.getTombstoneExpiration(), TimeUnit.MILLISECONDS); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void evictAll() throws CacheException { |
| 148 | + region.beginInvalidation(); |
| 149 | + try { |
| 150 | + Caches.broadcastEvictAll(cache); |
| 151 | + } |
| 152 | + finally { |
| 153 | + region.endInvalidation(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void unlockItem(SessionImplementor session, Object key) throws CacheException { |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public boolean afterInsert(SessionImplementor session, Object key, Object value, Object version) { |
| 163 | + writeCache.put(key, getVersioned(value, version, session.getTimestamp())); |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public boolean afterUpdate(SessionImplementor session, Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) { |
| 169 | + writeCache.put(key, getVersioned(value, currentVersion, session.getTimestamp())); |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + protected Object getVersion(Object value) { |
| 174 | + if (value instanceof CacheEntry) { |
| 175 | + return ((CacheEntry) value).getVersion(); |
| 176 | + } |
| 177 | + else if (value instanceof VersionedEntry) { |
| 178 | + return ((VersionedEntry) value).getVersion(); |
| 179 | + } |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + protected Object getVersioned(Object value, Object version, long timestamp) { |
| 184 | + assert value != null; |
| 185 | + assert version != null; |
| 186 | + return new VersionedEntry(value, version, timestamp); |
| 187 | + } |
| 188 | +} |
0 commit comments