Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (5.0/Cloud)

84033: Merged V4.2-BUG-FIX (4.2.4) to HEAD-BUG-FIX (5.0/Cloud)
      83385: MNT-12297: TransactionalCache equality checks lead to false negatives in a cluster
       - TransactionalCache uses ValueHolder to store values put into the cache.
       - Each ValueHolder will have a new random integer to distinguish it from the next
       - TransactionalCache equality checks no longer use reference equality and instead
         use the ValueHolder.equals() method for most cases.  This actual client value
         .equals() is used where 'allowEquals' is true.
       - Immutable caches always assume the backing cache is correct, still.
      83391: Fix test doing checks in low-level cache (r83385 MNT-12297)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@84621 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alan Davis
2014-09-18 17:21:11 +00:00
parent 610afaf8a5
commit fa4d74fa5b
5 changed files with 496 additions and 225 deletions

View File

@@ -83,7 +83,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
/** enable/disable write through to the shared cache */
private boolean disableSharedCache;
/** the shared cache that will get updated after commits */
private SimpleCache<Serializable, Object> sharedCache;
private SimpleCache<Serializable, ValueHolder<V>> sharedCache;
/** can the cached values be modified */
private boolean isMutable;
/** can values be compared using full equality checking */
@@ -145,7 +145,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
*
* @param sharedCache underlying cache shared by transactions
*/
public void setSharedCache(SimpleCache<Serializable, Object> sharedCache)
public void setSharedCache(SimpleCache<Serializable, ValueHolder<V>> sharedCache)
{
this.sharedCache = sharedCache;
}
@@ -406,15 +406,46 @@ public class TransactionalCache<K extends Serializable, V extends Object>
}
/**
* Fetches a value from the shared cache.
* Fetches a value from the shared cache. If values were wrapped,
* then they will be unwrapped before being returned. If code requires
* direct access to the wrapper object as well, then this call should not
* be used.
*
* @param key the key
* @return Returns the value or <tt>null</tt>
*/
@SuppressWarnings("unchecked")
private V getSharedCacheValue(Serializable key)
public static <KEY extends Serializable, VAL> VAL getSharedCacheValue(SimpleCache<KEY, ValueHolder<VAL>> sharedCache, KEY key)
{
return (V) sharedCache.get(key);
Object possibleWrapper = sharedCache.get(key);
if (possibleWrapper == null)
{
return null;
}
else if (possibleWrapper instanceof ValueHolder)
{
ValueHolder<VAL> wrapper = (ValueHolder<VAL>) possibleWrapper;
return wrapper.getValue();
}
else
{
throw new IllegalStateException("All entries for TransactionalCache must be put using TransactionalCache.putSharedCacheValue.");
}
}
/**
* Values written to the backing cache need proper wrapping and unwrapping
*
* @param sharedCache the cache to operate on
* @param key the key
* @param value the value to wrap
*
* @since 4.2.3
*/
public static <KEY extends Serializable, VAL> void putSharedCacheValue(SimpleCache<KEY, ValueHolder<VAL>> sharedCache, KEY key, VAL value)
{
ValueHolder<VAL> wrapper = new ValueHolder<VAL>(value);
sharedCache.put(key, wrapper);
}
/**
@@ -551,7 +582,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
{
// There is no in-txn entry for the key
// Use the value direct from the shared cache
V value = getSharedCacheValue(key);
V value = TransactionalCache.getSharedCacheValue(sharedCache, key);
bucket = new ReadCacheBucket<V>(value);
txnData.updatedItemsCache.put(key, bucket);
return value;
@@ -561,7 +592,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
// no value found - must we ignore the shared cache?
if (!ignoreSharedCache)
{
V value = getSharedCacheValue(key);
V value = TransactionalCache.getSharedCacheValue(sharedCache, key);
// go to the shared cache
if (isDebugEnabled)
{
@@ -590,7 +621,6 @@ public class TransactionalCache<K extends Serializable, V extends Object>
* Where a transaction is present, a cache of updated items is lazily added to the
* thread and the <tt>Object</tt> put onto that.
*/
@SuppressWarnings("unchecked")
public void put(K keyIn, V value)
{
final Serializable key = getTenantAwareCacheKey(keyIn);
@@ -599,7 +629,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
if (AlfrescoTransactionSupport.getTransactionId() == null) // not in transaction
{
// no transaction
sharedCache.put(key, value);
TransactionalCache.putSharedCacheValue(sharedCache, key, value);
// done
if (isDebugEnabled)
{
@@ -652,9 +682,9 @@ public class TransactionalCache<K extends Serializable, V extends Object>
txnData.haveIssuedFullWarning = true;
}
}
Object existingValueObj = txnData.noSharedCacheRead ? null : sharedCache.get(key);
ValueHolder<V> existingValueHolder = txnData.noSharedCacheRead ? null : sharedCache.get(key);
CacheBucket<V> bucket = null;
if (existingValueObj == null)
if (existingValueHolder == null)
{
// ALF-5134: Performance of Alfresco cluster less than performance of single node
// The 'null' marker that used to be inserted also triggered an update in the afterCommit
@@ -667,7 +697,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
else
{
// Record the existing value as is
bucket = new UpdateCacheBucket<V>((V)existingValueObj, value);
bucket = new UpdateCacheBucket<V>(existingValueHolder, value);
}
txnData.updatedItemsCache.put(key, bucket);
// remove the item from the removed cache, if present
@@ -1045,7 +1075,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
* @param key the key that the bucket was stored against
*/
public void doPreCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly);
/**
@@ -1055,7 +1085,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
* @param key the key that the bucket was stored against
*/
public void doPostCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly);
}
@@ -1079,21 +1109,21 @@ public class TransactionalCache<K extends Serializable, V extends Object>
return value;
}
public void doPreCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
}
public void doPostCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
Object sharedObj = sharedCache.get(key);
if (sharedObj == null)
ValueHolder<BV> sharedObjValueHolder = sharedCache.get(key);
if (sharedObjValueHolder == null)
{
// Nothing has changed, write it through
sharedCache.put(key, value);
TransactionalCache.putSharedCacheValue(sharedCache, key, value);
}
else if (!mutable)
{
@@ -1101,15 +1131,9 @@ public class TransactionalCache<K extends Serializable, V extends Object>
// The assumption is that the value will be correct because the values are immutable
// Don't write it unnecessarily.
}
else if (sharedObj == value)
else if (allowEqualsCheck && EqualsHelper.nullSafeEquals(value, sharedObjValueHolder.getValue()))
{
// Someone else put exactly the same value into the cache
// Don't write it unnecessarily.
}
else if (allowEqualsCheck && EqualsHelper.nullSafeEquals(value, sharedObj))
{
// Someone else added a value but we have validated that it is the same
// as the new one that we where going to add.
// The value we want to write is the same as the one in the shared cache.
// Don't write it unnecessarily.
}
else
@@ -1131,10 +1155,10 @@ public class TransactionalCache<K extends Serializable, V extends Object>
private static final long serialVersionUID = 7885689778259779578L;
private final BV value;
private final BV originalValue;
public UpdateCacheBucket(BV originalValue, BV value)
private final ValueHolder<BV> originalValueHolder;
public UpdateCacheBucket(ValueHolder<BV> originalValueHolder, BV value)
{
this.originalValue = originalValue;
this.originalValueHolder = originalValueHolder;
this.value = value;
}
public BV getValue()
@@ -1142,47 +1166,45 @@ public class TransactionalCache<K extends Serializable, V extends Object>
return value;
}
public void doPreCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
}
public void doPostCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
Object sharedObj = sharedCache.get(key);
if (sharedObj == null)
ValueHolder<BV> sharedObjValueHolder = sharedCache.get(key);
if (sharedObjValueHolder == null)
{
// Someone removed the value
if (!mutable)
{
// We can assume that our value is correct because it's immutable
sharedCache.put(key, value);
TransactionalCache.putSharedCacheValue(sharedCache, key, value);
}
else
{
// The value is mutable, so we must behave pessimistically
// The value is mutable, so we must behave pessimistically i.e. leave the shared cache empty
}
}
else if (!mutable)
{
// Someone else has already updated the value.
// This is not normally seen for immutable values. The assumption is that the values
// are equal.
// We assume the configuration is correct and therefore, that we do not need to compare
// the cached value with the updated value. This applies to null as well.
}
else if (allowEqualsCheck && EqualsHelper.nullSafeEquals(value, sharedObjValueHolder.getValue()))
{
// The value we want to write is the same as the one in the shared cache.
// Don't write it unnecessarily.
}
else if (sharedObj == originalValue)
else if (EqualsHelper.nullSafeEquals(originalValueHolder, sharedObjValueHolder))
{
// Nothing has changed, write it through
sharedCache.put(key, value);
}
else if (allowEqualsCheck && EqualsHelper.nullSafeEquals(value, sharedObj))
{
// Someone else updated the value but we have validated that it is the same
// as the one that we where going to update.
// Don't write it unnecessarily.
// The value in the cache did not change from what we observed before.
// Update the value.
TransactionalCache.putSharedCacheValue(sharedCache, key, value);
}
else
{
@@ -1211,13 +1233,13 @@ public class TransactionalCache<K extends Serializable, V extends Object>
return value;
}
public void doPreCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
}
public void doPostCommit(
SimpleCache<Serializable, Object> sharedCache,
SimpleCache<Serializable, ValueHolder<BV>> sharedCache,
Serializable key,
boolean mutable, boolean allowEqualsCheck, boolean readOnly)
{
@@ -1338,4 +1360,57 @@ public class TransactionalCache<K extends Serializable, V extends Object>
return hashCode;
}
}
/**
* A wrapper object to carry object values, but forcing a straight equality check
* based on a random integer only. This is used in cases where cache values do NOT
* have an adequate equals method and we expect serialization of objects.
*
* @author Derek Hulley
* @since 4.2.4
*/
public static final class ValueHolder<V2> implements Serializable
{
private static final long serialVersionUID = -3462098329153772713L;
/**
* A random, positive integer since we only have to
* prevent short-term duplication between values with the same keys.
*/
private final int rand;
private final V2 value;
private ValueHolder(V2 value)
{
this.rand = (int) (Math.random() * Integer.MAX_VALUE);
this.value = value;
}
public final V2 getValue()
{
return value;
}
@Override
public final int hashCode()
{
return rand;
}
@SuppressWarnings("rawtypes")
@Override
public final boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ValueHolder other = (ValueHolder) obj;
return this.rand == other.rand;
}
@Override
public final String toString()
{
return "ValueHolder [value=" + value + "]";
}
}
}

View File

@@ -26,6 +26,7 @@ import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.alfresco.repo.cache.TransactionalCache.ValueHolder;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport.TxnReadState;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
@@ -53,10 +54,9 @@ public class CacheTest extends TestCase
ApplicationContextHelper.CONFIG_LOCATIONS[0]});
private ServiceRegistry serviceRegistry;
private SimpleCache<String, Object> standaloneCache;
private SimpleCache<String, Object> backingCache;
private TransactionalCache<String, Object> transactionalCache;
private SimpleCache<String, Object> objectCache;
private SimpleCache<String, ValueHolder<Object>> backingCache;
private TransactionalCache<String, Object> transactionalCache;
@SuppressWarnings("unchecked")
@Override
@@ -68,10 +68,9 @@ public class CacheTest extends TestCase
}
serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
standaloneCache = (SimpleCache<String, Object>) ctx.getBean("simpleCache1");
backingCache = (SimpleCache<String, Object>) ctx.getBean("backingCache");
transactionalCache = (TransactionalCache<String, Object>) ctx.getBean("transactionalCache");
objectCache = (SimpleCache<String, Object>) ctx.getBean("objectCache");
backingCache = (SimpleCache<String, ValueHolder<Object>>) ctx.getBean("backingCache");
transactionalCache = (TransactionalCache<String, Object>) ctx.getBean("transactionalCache");
// Make sure that the backing cache is empty
backingCache.clear();
@@ -85,7 +84,7 @@ public class CacheTest extends TestCase
public void tearDown()
{
serviceRegistry = null;
standaloneCache = null;
objectCache = null;
backingCache = null;
transactionalCache = null;
}
@@ -94,30 +93,26 @@ public class CacheTest extends TestCase
{
assertNotNull(serviceRegistry);
assertNotNull(backingCache);
assertNotNull(standaloneCache);
assertNotNull(transactionalCache);
assertNotNull(objectCache);
assertNotNull(transactionalCache);
}
public void testObjectCache() throws Exception
{
objectCache.clear();
objectCache.put("A", this);
Object obj = objectCache.get("A");
assertTrue("Object not cached properly", this == obj);
}
public void testEhcacheAdaptors() throws Exception
{
backingCache.put("A", "AAA");
assertNull("Second cache should not have first's present", standaloneCache.get("A"));
objectCache.put("A", "AAA");
assertEquals("AAA", objectCache.get("A"));
assertEquals("AAA", backingCache.get("A"));
Collection<String> keys = objectCache.getKeys();
assertEquals("Cache didn't return correct number of keys", 1, keys.size());
Collection<String> keys = backingCache.getKeys();
assertEquals("Backing cache didn't return correct number of keys", 1, keys.size());
backingCache.remove("A");
assertNull(backingCache.get("A"));
objectCache.remove("A");
assertNull(objectCache.get("A"));
}
public void testTransactionalCacheNoTxn() throws Exception
@@ -127,18 +122,18 @@ public class CacheTest extends TestCase
// no transaction - do a put
transactionalCache.put(key, value);
// check that the value appears in the backing cache, backingCache
assertEquals("Backing cache not used for put when no transaction present", value, backingCache.get(key));
assertEquals("Backing cache not used for put when no transaction present", value, TransactionalCache.getSharedCacheValue(backingCache, key));
// remove the value from the backing cache and check that it is removed from the transaction cache
backingCache.remove(key);
assertNull("Backing cache not used for removed when no transaction present", transactionalCache.get(key));
// add value into backing cache
backingCache.put(key, value);
TransactionalCache.putSharedCacheValue(backingCache, key, value);
// remove it from the transactional cache
transactionalCache.remove(key);
// check that it is gone from the backing cache
assertNull("Non-transactional remove didn't go to backing cache", backingCache.get(key));
assertNull("Non-transactional remove didn't go to backing cache", TransactionalCache.getSharedCacheValue(backingCache, key));
}
private static final String NEW_GLOBAL_ONE = "new_global_one";
@@ -153,7 +148,7 @@ public class CacheTest extends TestCase
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
// Add items to the global cache
backingCache.put(NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
@@ -195,9 +190,9 @@ public class CacheTest extends TestCase
public void testTransactionalCacheWithSingleTxn() throws Throwable
{
// add item to global cache
backingCache.put(NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
backingCache.put(NEW_GLOBAL_TWO, NEW_GLOBAL_TWO);
backingCache.put(NEW_GLOBAL_THREE, NEW_GLOBAL_THREE);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_THREE, NEW_GLOBAL_THREE);
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction txn = transactionService.getUserTransaction();
@@ -215,7 +210,7 @@ public class CacheTest extends TestCase
// read 2 from the cache
assertEquals("Item not read from backing cache", NEW_GLOBAL_TWO, transactionalCache.get(NEW_GLOBAL_TWO));
// Change the backing cache
backingCache.put(NEW_GLOBAL_TWO, NEW_GLOBAL_TWO + "-updated");
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO + "-updated");
// Ensure read-committed
assertEquals("Read-committed not preserved", NEW_GLOBAL_TWO, transactionalCache.get(NEW_GLOBAL_TWO));
@@ -254,8 +249,8 @@ public class CacheTest extends TestCase
// check that backing cache was updated with the in-transaction changes
assertFalse("Item was not removed from backing cache", backingCache.contains(NEW_GLOBAL_ONE));
assertNull("Item could still be fetched from backing cache", backingCache.get(NEW_GLOBAL_ONE));
assertEquals("Item not updated in backing cache", "XXX", backingCache.get(UPDATE_TXN_THREE));
assertNull("Item could still be fetched from backing cache", TransactionalCache.getSharedCacheValue(backingCache, NEW_GLOBAL_ONE));
assertEquals("Item not updated in backing cache", "XXX", TransactionalCache.getSharedCacheValue(backingCache, UPDATE_TXN_THREE));
// Check that the transactional cache serves get requests
assertEquals("Transactional cache must serve post-commit get requests", "XXX",
@@ -342,9 +337,9 @@ public class CacheTest extends TestCase
public void testTransactionalCacheDisableSharedCaches() throws Throwable
{
// add item to global cache
backingCache.put(NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
backingCache.put(NEW_GLOBAL_TWO, NEW_GLOBAL_TWO);
backingCache.put(NEW_GLOBAL_THREE, NEW_GLOBAL_THREE);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO);
TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_THREE, NEW_GLOBAL_THREE);
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction txn = transactionService.getUserTransaction();
@@ -432,7 +427,7 @@ public class CacheTest extends TestCase
int count = (int) Math.pow(10D, (double)i);
// test standalone
long timePlain = runPerformanceTestOnCache(standaloneCache, count);
long timePlain = runPerformanceTestOnCache(objectCache, count);
// do transactional cache in a transaction
TransactionService transactionService = serviceRegistry.getTransactionService();
@@ -524,7 +519,7 @@ public class CacheTest extends TestCase
txn.begin();
backingCache.put("A", null);
TransactionalCache.putSharedCacheValue(backingCache, "A", null);
transactionalCache.put("A", "AAA");
try
@@ -581,8 +576,14 @@ public class CacheTest extends TestCase
RetryingTransactionCallback<Object> callback,
boolean readOnly,
String key,
Object expectedValue) throws Throwable
Object expectedValue,
boolean mustContainKey) throws Throwable
{
if (expectedValue != null && !mustContainKey)
{
throw new IllegalArgumentException("Why have a value when the key should not be there?");
}
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction txn = transactionService.getUserTransaction(readOnly);
try
@@ -595,8 +596,9 @@ public class CacheTest extends TestCase
{
try { txn.rollback(); } catch (Throwable ee) {}
}
Object actualValue = backingCache.get(key);
Object actualValue = TransactionalCache.getSharedCacheValue(backingCache, key);
assertEquals("Backing cache value was not correct", expectedValue, actualValue);
assertEquals("Backing cache contains(key): ", mustContainKey, backingCache.contains(key));
// Clear the backing cache to ensure that subsequent tests don't run into existing data
backingCache.clear();
@@ -610,8 +612,8 @@ public class CacheTest extends TestCase
public void testValueLockingInTxn() throws Exception
{
// add item to global cache
backingCache.put(DEFINITIVE_TWO, "initial_two");
backingCache.put(DEFINITIVE_THREE, "initial_three");
TransactionalCache.putSharedCacheValue(backingCache, DEFINITIVE_TWO, "initial_two");
TransactionalCache.putSharedCacheValue(backingCache, DEFINITIVE_THREE, "initial_three");
TransactionService transactionService = serviceRegistry.getTransactionService();
UserTransaction txn = transactionService.getUserTransaction();
@@ -668,9 +670,9 @@ public class CacheTest extends TestCase
txn.commit();
// Check post-commit values
assertEquals("Definitive change not written through.", DEFINITIVE_ONE, backingCache.get(DEFINITIVE_ONE));
assertEquals("Definitive change not written through.", DEFINITIVE_TWO, backingCache.get(DEFINITIVE_TWO));
assertEquals("Definitive change not written through.", null, backingCache.get(DEFINITIVE_THREE));
assertEquals("Definitive change not written through.", DEFINITIVE_ONE, TransactionalCache.getSharedCacheValue(backingCache, DEFINITIVE_ONE));
assertEquals("Definitive change not written through.", DEFINITIVE_TWO, TransactionalCache.getSharedCacheValue(backingCache, DEFINITIVE_TWO));
assertEquals("Definitive change not written through.", null, TransactionalCache.getSharedCacheValue(backingCache, DEFINITIVE_THREE));
}
finally
{
@@ -696,25 +698,25 @@ public class CacheTest extends TestCase
public Object execute() throws Throwable
{
transactionalCache.put(COMMON_KEY, VALUE_ONE_A);
backingCache.put(COMMON_KEY, VALUE_ONE_B);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_B);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Mutable: Shared cache value checked
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Mutable: Shared cache value checked
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Mutable: Shared cache value checked
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Mutable: Shared cache value checked
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
@@ -730,25 +732,59 @@ public class CacheTest extends TestCase
public Object execute() throws Throwable
{
transactionalCache.put(COMMON_KEY, VALUE_ONE_A);
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Mutable: Object handle is match
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Mutable: Object handle is match
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: No equality check
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: No equality check
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Immutable: Object handle is match
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Immutable: Object handle is match
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Assumed to be same
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Assumed to be same
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Mutable: Object handle is match
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Mutable: Object handle is match
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Equality check done
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Equality check done
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Immutable: Object handle is match
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Immutable: Object handle is match
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Assumed to be same
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Assumed to be same
}
/**
* <ul>
* <li>Add to the transaction cache</li>
* <li>Add <tt>null</tt> to the backing cache</li>
* <li>Commit</li>
* </ul>
*/
public void testConcurrentAddAgainstAddNull()throws Throwable
{
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
transactionalCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, null);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: No equality check
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: No equality check
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Equality check done
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Equality check done
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
@@ -770,19 +806,19 @@ public class CacheTest extends TestCase
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Mutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Mutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Add back to backing cache
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Add back to backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Mutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Mutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Mutable: Add back to backing cache
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_A, true); // Immutable: Add back to backing cache
}
/**
* <ul>
@@ -798,27 +834,27 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, VALUE_ONE_B);
backingCache.put(COMMON_KEY, VALUE_TWO_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_TWO_A);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_TWO_A); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_TWO_A); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_TWO_A, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_TWO_A, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Shared cache value checked failed
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Shared cache value checked failed
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Shared cache value checked failed
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Shared cache value checked failed
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_TWO_A); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_TWO_A); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_TWO_A, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_TWO_A, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
@@ -834,27 +870,27 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, VALUE_ONE_B);
backingCache.put(COMMON_KEY, null);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, null);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
@@ -870,27 +906,63 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, null);
backingCache.put(COMMON_KEY, VALUE_ONE_B);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_B);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
* <li>Add to the backing cache</li>
* <li>Update the transactional cache with a <tt>null</tt> value</li>
* <li>Update the backing cache with a <tt>null</tt> value</li>
* <li>Commit</li>
* </ul>
*/
public void testConcurrentUpdateNullAgainstUpdateNull()throws Throwable
{
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, null);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, null);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null, true); // Mutable: Equality check
executeAndCheck(callback, true, COMMON_KEY, null, true); // Mutable: Equality check
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null, true); // Immutable: Equality check
executeAndCheck(callback, true, COMMON_KEY, null, true); // Immutable: Equality check
}
/**
* <ul>
@@ -906,7 +978,7 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, VALUE_ONE_B);
backingCache.remove(COMMON_KEY);
return null;
@@ -914,19 +986,19 @@ public class CacheTest extends TestCase
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
}
/**
* <ul>
@@ -942,7 +1014,7 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.put(COMMON_KEY, VALUE_ONE_B);
backingCache.clear();
return null;
@@ -950,19 +1022,19 @@ public class CacheTest extends TestCase
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Add back to backing cache
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Add back to backing cache
}
/**
* <ul>
@@ -980,25 +1052,25 @@ public class CacheTest extends TestCase
{
backingCache.remove(COMMON_KEY);
transactionalCache.remove(COMMON_KEY);
backingCache.put(COMMON_KEY, VALUE_ONE_B);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_B);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
}
/**
* <ul>
@@ -1016,25 +1088,25 @@ public class CacheTest extends TestCase
{
backingCache.remove(COMMON_KEY);
transactionalCache.put(COMMON_KEY, VALUE_ONE_A);
backingCache.put(COMMON_KEY, VALUE_ONE_B);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_B);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Mutable: Shared cache value checked
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Mutable: Shared cache value checked
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Mutable: Shared cache value checked
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Mutable: Shared cache value checked
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B); // Immutable: Assume backing cache is correct
executeAndCheck(callback, false, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
executeAndCheck(callback, true, COMMON_KEY, VALUE_ONE_B, true); // Immutable: Assume backing cache is correct
}
/**
* <ul>
@@ -1050,27 +1122,27 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.remove(COMMON_KEY);
backingCache.put(COMMON_KEY, VALUE_ONE_B);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_B);
return null;
}
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Pessimistic removal
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Pessimistic removal
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Pessimistic removal
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
}
/**
* <ul>
@@ -1086,7 +1158,7 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.remove(COMMON_KEY);
backingCache.remove(COMMON_KEY);
return null;
@@ -1094,19 +1166,19 @@ public class CacheTest extends TestCase
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Remove from backing cache
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Remove from backing cache
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Remove from backing cache
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Remove from backing cache
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Remove from backing cache
}
/**
* <ul>
@@ -1122,7 +1194,7 @@ public class CacheTest extends TestCase
{
public Object execute() throws Throwable
{
backingCache.put(COMMON_KEY, VALUE_ONE_A);
TransactionalCache.putSharedCacheValue(backingCache, COMMON_KEY, VALUE_ONE_A);
transactionalCache.remove(COMMON_KEY);
backingCache.clear();
return null;
@@ -1130,18 +1202,18 @@ public class CacheTest extends TestCase
};
transactionalCache.setAllowEqualsChecks(false);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Nothing to do
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Nothing to do
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Nothing to do
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Nothing to do
transactionalCache.setAllowEqualsChecks(true);
transactionalCache.setMutable(true);
executeAndCheck(callback, false, COMMON_KEY, null); // Mutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null); // Mutable: Nothing to do
executeAndCheck(callback, false, COMMON_KEY, null, false); // Mutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null, false); // Mutable: Nothing to do
transactionalCache.setMutable(false);
executeAndCheck(callback, false, COMMON_KEY, null); // Immutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null); // Immutable: Nothing to do
executeAndCheck(callback, false, COMMON_KEY, null, false); // Immutable: Nothing to do
executeAndCheck(callback, true, COMMON_KEY, null, false); // Immutable: Nothing to do
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import org.springframework.beans.factory.BeanNameAware;
/**
* {@link SimpleCache} implementation backed by a {@link DefaultSimpleCache} but forcing
* object serialization in and out. This is only useful for tests.
*
* @author Derek Hulley
* @since 4.2.3
*/
public final class SerializingSimpleCache<K extends Serializable, V extends Serializable>
implements SimpleCache<K, V>, BeanNameAware
{
private SimpleCache<K, V> cache;
public SerializingSimpleCache(int maxItems, String cacheName)
{
cache = new DefaultSimpleCache<>(maxItems, cacheName);
}
public SerializingSimpleCache()
{
cache = new DefaultSimpleCache<>();
}
@SuppressWarnings("unchecked")
private V serialize(V value)
{
if (value == null)
{
return null;
}
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(value);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
V ret = (V) ois.readObject();
// This is just test code!
bos.close();
oos.close();
bis.close();
ois.close();
// Done
return ret;
}
catch (Exception e)
{
throw new RuntimeException("Failure to serialize/deserialize object: " + value, e);
}
}
@Override
public boolean contains(K key)
{
return cache.contains(key);
}
@Override
public Collection<K> getKeys()
{
return cache.getKeys();
}
@Override
public V get(K key)
{
V ret = cache.get(key);
return serialize(ret);
}
@Override
public void put(K key, V value)
{
value = serialize(value);
cache.put(key, value);
}
@Override
public void remove(K key)
{
cache.remove(key);
}
@Override
public void clear()
{
cache.clear();
}
@Override
public void setBeanName(String cacheName)
{
}
}

View File

@@ -42,6 +42,8 @@ import java.util.Set;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.cache.TransactionalCache;
import org.alfresco.repo.cache.TransactionalCache.ValueHolder;
import org.alfresco.repo.domain.node.Node;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.node.NodeEntity;
@@ -128,9 +130,9 @@ public class NodeServiceTest
private static TransactionService txnService;
private static PolicyComponent policyComponent;
private static CannedQueryDAO cannedQueryDAOForTesting;
private static SimpleCache<Serializable, Serializable> nodesCache;
private static SimpleCache<Serializable, Serializable> propsCache;
private static SimpleCache<Serializable, Serializable> aspectsCache;
private static SimpleCache<Serializable, ValueHolder<Serializable>> nodesCache;
private static SimpleCache<Serializable, ValueHolder<Serializable>> propsCache;
private static SimpleCache<Serializable, ValueHolder<Serializable>> aspectsCache;
private static Long deletedTypeQNameId;
@@ -151,9 +153,9 @@ public class NodeServiceTest
cannedQueryDAOForTesting = (CannedQueryDAO) APP_CONTEXT_INIT.getApplicationContext().getBean("cannedQueryDAOForTesting");
// Get the caches for later testing
nodesCache = (SimpleCache<Serializable, Serializable>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.nodesSharedCache");
propsCache = (SimpleCache<Serializable, Serializable>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.propertiesSharedCache");
aspectsCache = (SimpleCache<Serializable, Serializable>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.aspectsSharedCache");
nodesCache = (SimpleCache<Serializable, ValueHolder<Serializable>>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.nodesSharedCache");
propsCache = (SimpleCache<Serializable, ValueHolder<Serializable>>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.propertiesSharedCache");
aspectsCache = (SimpleCache<Serializable, ValueHolder<Serializable>>) APP_CONTEXT_INIT.getApplicationContext().getBean("node.aspectsSharedCache");
// Clear the caches to remove fluff
nodesCache.clear();
@@ -812,7 +814,7 @@ public class NodeServiceTest
/**
* Looks for a key that contains the toString() of the value
*/
private Object findCacheValue(SimpleCache<Serializable, Serializable> cache, Serializable key)
private Object findCacheValue(SimpleCache<Serializable, ValueHolder<Serializable>> cache, Serializable key)
{
Collection<Serializable> keys = cache.getKeys();
for (Serializable keyInCache : keys)
@@ -821,7 +823,7 @@ public class NodeServiceTest
String keyStr = key.toString();
if (keyInCacheStr.endsWith(keyStr))
{
Object value = cache.get(keyInCache);
Object value = TransactionalCache.getSharedCacheValue(cache, keyInCache);
return value;
}
}

View File

@@ -4,9 +4,9 @@
<!-- sample bean configuration to test cache configurations -->
<beans>
<bean name="simpleCache1" class="org.alfresco.repo.cache.DefaultSimpleCache" />
<bean name="objectCache" class="org.alfresco.repo.cache.DefaultSimpleCache" />
<bean name="backingCache" class="org.alfresco.repo.cache.DefaultSimpleCache" />
<bean name="backingCache" class="org.alfresco.repo.cache.SerializingSimpleCache" />
<bean name="transactionalCache" class="org.alfresco.repo.cache.TransactionalCache">
<property name="sharedCache"><ref bean="backingCache" /></property>
@@ -14,6 +14,4 @@
<property name="maxCacheSize"><value>200000</value></property>
</bean>
<bean name="objectCache" class="org.alfresco.repo.cache.DefaultSimpleCache" />
</beans>