@Override public SoftLock tryLock(final Object key, final Object version) { ExpiryMarker marker; String markerId = nextMarkerId(); while (true) { final Expirable original = cache.get(key); long timeout = nextTimestamp() + CacheEnvironment.getDefaultCacheTimeoutInMillis(); if (original == null) { marker = new ExpiryMarker(version, timeout, markerId); if (cache.putIfAbsent(key, marker) == null) { break; } } else { marker = original.markForExpiration(timeout, markerId); if (cache.replace(key, original, marker)) { break; } } } return new MarkerWrapper(marker); }
public boolean update(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) { if (lock == LOCK_FAILURE) { return false; } final Value currentValue = cache.get(key); if (lock == LOCK_SUCCESS) { if (currentValue != null && currentVersion != null && versionComparator.compare(currentVersion, currentValue.getVersion()) < 0) { return false; } } if (topic != null) { topic.publish(createMessage(key, value, currentVersion)); } cache.put(key, new Value(currentVersion, value, lock, Clock.currentTimeMillis())); return true; }
public SoftLock tryLock(final Object key, final Object version) { final Value value = cache.get(key); if (value == null) { if (cache.putIfAbsent(key, new Value(version, null, LOCK_SUCCESS, Clock.currentTimeMillis())) == null) { return LOCK_SUCCESS; } else { return LOCK_FAILURE; } } else { if (version == null || versionComparator.compare(version, value.getVersion()) >= 0) { if (cache.replace(key, value, value.createLockedValue(LOCK_SUCCESS))) { return LOCK_SUCCESS; } else { return LOCK_FAILURE; } } else { return LOCK_FAILURE; } } }
@Override public void unlock(final Object key, SoftLock lock) { while (true) { final Expirable original = cache.get(key); if (original != null) { if (!(lock instanceof MarkerWrapper)) { break; } final ExpiryMarker unwrappedMarker = ((MarkerWrapper) lock).getMarker(); if (original.matches(unwrappedMarker)) { final Expirable revised = ((ExpiryMarker) original).expire(nextTimestamp()); if (cache.replace(key, original, revised)) { break; } } else if (original.getValue() != null) { if (cache.remove(key, original)) { break; } } else { break; } } else { break; } } maybeNotifyTopic(key, null, null); }
/** * {@inheritDoc} * <p> * Called after <code>com.hazelcast.ReadWriteAccessDelegate.lockItem()</code> * </p> */ @Override public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { try { return cache.update(key, value, currentVersion, lock); } catch (HazelcastException e) { if (log.isFinestEnabled()) { log.finest("Could not update Cache[" + hazelcastRegion.getName() + "]: " + e.getMessage()); } return false; } }
/** * @throws UnsupportedOperationException Always thrown as we cannot update an item in a read-only cache */ @Override public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { throw new UnsupportedOperationException("Cannot update an item in a read-only cache: " + getHazelcastRegion().getName()); }
/** * {@inheritDoc} * <p> * Should be a no-op since this cache is read-only * </p> */ @Override public void unlockItem(final Object key, final SoftLock lock) throws CacheException { /* * To err on the safe side though, follow ReadOnlyEhcacheEntityRegionAccessStrategy which nevertheless evicts * the key. */ evict(key); }
@Override public boolean update(final Object key, final Object newValue, final Object newVersion, final SoftLock lock) { if (lock instanceof MarkerWrapper) { final ExpiryMarker unwrappedMarker = ((MarkerWrapper) lock).getMarker(); return (Boolean) map.executeOnKey(key, new UpdateEntryProcessor(unwrappedMarker, newValue, newVersion, nextMarkerId(), nextTimestamp(hazelcastInstance))); } else { return false; } }
@Override public SoftLock tryLock(final Object key, final Object version) { long timeout = nextTimestamp(hazelcastInstance) + lockTimeout; final ExpiryMarker marker = (ExpiryMarker) map.executeOnKey(key, new LockEntryProcessor(nextMarkerId(), timeout, version)); return new MarkerWrapper(marker); }
@Override public void unlock(final Object key, SoftLock lock) { if (lock instanceof MarkerWrapper) { final ExpiryMarker unwrappedMarker = ((MarkerWrapper) lock).getMarker(); map.executeOnKey(key, new UnlockEntryProcessor(unwrappedMarker, nextMarkerId(), nextTimestamp(hazelcastInstance))); } }
/** * {@inheritDoc} * <p/> * Called after <code>com.hazelcast.hibernate.access.ReadWriteAccessDelegate.lockItem()</code> */ public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { try { return put(key, value, currentVersion, previousVersion); } catch (TimeoutException e) { LOG.log(Level.FINEST, e.getMessage()); } finally { unlockItem(key, lock); } return false; }
public SoftLock lockItem(final Object key, final Object version) throws CacheException { if (lockTimeout > 0) { if (!getCache().tryLock(key, lockTimeout, TimeUnit.SECONDS)) { throw new CacheException("Cache lock could not be acquired! Wait-time: " + lockTimeout + " seconds"); } } else { getCache().lock(key); } return new SoftLock() { }; // dummy lock }
/** * @throws UnsupportedOperationException */ @Override public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { throw new UnsupportedOperationException("Cannot update an item in a read-only cache: " + getHazelcastRegion().getName()); }
public void unlock(final Object key, SoftLock lock) { final Value value = cache.get(key); if (value != null) { final SoftLock currentLock = value.getLock(); if (currentLock == lock) { cache.replace(key, value, value.createUnlockedValue()); } } }
/** * {@inheritDoc} * <p/> * Called after <code>com.hazelcast.hibernate.access.ReadWriteAccessDelegate.lockItem()</code> */ public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { try { return update(key, value, currentVersion, previousVersion, lock); } finally { unlockItem(key, lock); } }
protected boolean update(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) { try { return cache.update(key, value, currentVersion, previousVersion, lock); } catch (HazelcastException e) { LOG.log(Level.FINEST, "Could not update Cache[" + hazelcastRegion.getName() + "]: " + e.getMessage()); return false; } }
public boolean update(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) { if (lock == LOCK_FAILURE) { logger.log(Level.WARNING, "Cache lock could not be acquired!"); return false; } if (versionComparator != null && currentVersion != null) { if (explicitVersionCheckEnabled && value instanceof CacheEntry) { try { final CacheEntry currentEntry = (CacheEntry) value; final CacheEntry previousEntry = (CacheEntry) map.tryLockAndGet(key, tryLockAndGetTimeout, TimeUnit.MILLISECONDS); if (previousEntry == null || versionComparator.compare(currentEntry.getVersion(), previousEntry.getVersion()) > 0) { map.putAndUnlock(key, value); return true; } else { map.unlock(key); return false; } } catch (TimeoutException e) { return false; } } else if (previousVersion == null || versionComparator.compare(currentVersion, previousVersion) > 0) { map.set(key, value, 0, TimeUnit.MILLISECONDS); } return false; } else { map.set(key, value, 0, TimeUnit.MILLISECONDS); return true; } }
@Override public boolean update(final Object key, final Object newValue, final Object newVersion, final SoftLock softLock) { boolean updated = false; while (true) { Expirable original = cache.get(key); Expirable revised; long timestamp = nextTimestamp(); if (original == null) { // The entry must have expired. it should be safe to update revised = new Value(newVersion, timestamp, newValue); updated = true; if (cache.putIfAbsent(key, revised) == null) { break; } } else { if (softLock instanceof MarkerWrapper) { final ExpiryMarker unwrappedMarker = ((MarkerWrapper) softLock).getMarker(); if (original.matches(unwrappedMarker)) { // The lock matches final ExpiryMarker marker = (ExpiryMarker) original; if (marker.isConcurrent()) { revised = marker.expire(timestamp); updated = false; } else { revised = new Value(newVersion, timestamp, newValue); updated = true; } if (cache.replace(key, original, revised)) { break; } } else if (original.getValue() == null) { // It's marked for expiration, leave it as is updated = false; break; } else { // It's a value. Instead of removing it, expire it to prevent stale from in progress // transactions being put in the cache revised = new ExpiryMarker(newVersion, timestamp, nextMarkerId()).expire(timestamp); updated = false; if (cache.replace(key, original, revised)) { break; } } } else { break; } } } maybeNotifyTopic(key, newValue, newVersion); return updated; }
@Override public SoftLock lockItem(final Object key, final Object version) throws CacheException { return cache.tryLock(key, version); }
@Override public void unlockItem(final Object key, final SoftLock lock) throws CacheException { cache.unlock(key, lock); }
@Override public SoftLock lockItem(final Object key, final Object version) throws CacheException { return null; }
/** * @throws UnsupportedOperationException Thrown always because a read-only cache region cannot be locked */ @Override public SoftLock lockRegion() throws CacheException { throw new UnsupportedOperationException("Attempting to lock a read-only cache region: " + getHazelcastRegion().getName()); }
/** * This will issue a log warning stating that an attempt was made to unlock a read-only cache region. */ @Override public void unlockRegion(final SoftLock lock) throws CacheException { log.warning("Attempting to unlock a read-only cache region"); }
@Override public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { unlockItem(key, lock); return false; }
@Override public SoftLock lockItem(Object key, Object version) throws CacheException { return null; }
@Override public void unlockItem(final Object key, final SoftLock lock) throws CacheException { remove(key); }
/** * NO-OP */ @Override public SoftLock lockRegion() throws CacheException { return null; }
@Override public void unlockRegion(final SoftLock lock) throws CacheException { cache.clear(); }
@Override public SoftLock lockItem(final Object key, final Object version) throws CacheException { return delegate.lockItem(key, version); }
@Override public SoftLock lockRegion() throws CacheException { return delegate.lockRegion(); }
@Override public void unlockItem(final Object key, final SoftLock lock) throws CacheException { delegate.unlockItem(key, lock); }
@Override public void unlockRegion(final SoftLock lock) throws CacheException { delegate.unlockRegion(lock); }
@Override public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { return delegate.afterUpdate(key, value, currentVersion, previousVersion, lock); }
@Test public void testAfterUpdate() { when(cache.update(any(), any(), any(), any(SoftLock.class))).thenThrow(new HazelcastException("expected exception")); assertFalse(delegate.afterUpdate(null, null, null, null, null)); }
public boolean afterUpdate(final Object key, final Object value, final Object currentVersion, final Object previousVersion, final SoftLock lock) throws CacheException { return delegate.afterUpdate(key, value, currentVersion, previousVersion, lock); }