Skip to content

Commit

Permalink
ISPN-922 Test case using SKIP_LOCK with timeouts and SKIP_STORE flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and mmarkus committed Feb 15, 2011
1 parent b78aef3 commit 7d6811f
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 55 deletions.
Expand Up @@ -49,6 +49,7 @@ public abstract class BaseDistFunctionalTest extends MultipleCacheManagersTest {
protected boolean l1CacheEnabled = true;
protected boolean l1OnRehash = false;
protected boolean performRehashing = false;
protected boolean batchingEnabled = false;
protected int numOwners = 2;

protected void createCacheManagers() throws Throwable {
Expand All @@ -62,7 +63,7 @@ protected void createCacheManagers() throws Throwable {
// tests repeatedly queries changes
configuration.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
}
configuration.setInvocationBatchingEnabled(batchingEnabled());
configuration.setInvocationBatchingEnabled(batchingEnabled);
configuration.setSyncReplTimeout(60, TimeUnit.SECONDS);
configuration.setLockAcquisitionTimeout(45, TimeUnit.SECONDS);
configuration.setL1CacheEnabled(l1CacheEnabled);
Expand All @@ -86,10 +87,6 @@ protected void createCacheManagers() throws Throwable {

}

protected boolean batchingEnabled() {
return false;
}

public static ConsistentHash createNewConsistentHash(List<Address> servers) {
try {
Configuration c = new Configuration();
Expand Down Expand Up @@ -220,6 +217,10 @@ protected static Address addressOf(Cache<?, ?> cache) {
protected Cache<Object, String> getFirstNonOwner(String key) {
return getNonOwners(key)[0];
}

protected Cache<Object, String> getFirstOwner(String key) {
return getOwners(key)[0];
}

protected Cache<Object, String> getSecondNonOwner(String key) {
return getNonOwners(key)[1];
Expand Down
Expand Up @@ -10,6 +10,7 @@ public class DistSkipRemoteLookupTest extends BaseDistFunctionalTest {

public DistSkipRemoteLookupTest() {
cleanup = CleanupPhase.AFTER_METHOD;
batchingEnabled = true;
}

public void testSkipLookupOnGet() {
Expand Down Expand Up @@ -116,9 +117,5 @@ public void testSkipLookupOnAsyncRemove() throws InterruptedException, Execution

assert null == c4.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP).removeAsync(k1).get();
}

@Override
protected boolean batchingEnabled() {
return true;
}

}
Expand Up @@ -22,15 +22,11 @@
package org.infinispan.distribution;

import org.infinispan.Cache;
import org.infinispan.commands.write.ClearCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
import org.infinispan.commands.write.RemoveCommand;
import org.infinispan.commands.write.ReplaceCommand;
import org.infinispan.context.Flag;
import org.infinispan.loaders.CacheLoaderException;
import org.infinispan.loaders.CacheLoaderManager;
import org.infinispan.loaders.CacheStore;
import org.infinispan.test.TestingUtil;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.testng.annotations.Test;

import java.util.HashMap;
Expand All @@ -40,11 +36,14 @@
* DistSyncSharedTest.
*
* @author Galder Zamarreño
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
* @since 4.0
*/
@Test(groups = "functional", testName = "distribution.DistSyncCacheStoreNotSharedTest")
public class DistSyncCacheStoreNotSharedTest extends BaseDistCacheStoreTest {
private static final Log log = LogFactory.getLog(DistSyncCacheStoreNotSharedTest.class);

private static final String k1 = "1", v1 = "one", k2 = "2", v2 = "two", k3 = "3", v3 = "three", k4 = "4", v4 = "four";
private static final String[] keys = new String[]{k1, k2, k3, k4};

public DistSyncCacheStoreNotSharedTest() {
sync = true;
Expand All @@ -57,11 +56,46 @@ public void testPutFromNonOwner() throws Exception {
String key = "k2", value = "value2";
for (Cache<Object, String> c : caches) assert c.isEmpty();
Cache<Object, String> nonOwner = getFirstNonOwner(key);
Cache<Object, String> owner = getFirstOwner(key);
CacheStore nonOwnerStore = TestingUtil.extractComponent(nonOwner, CacheLoaderManager.class).getCacheStore();
CacheStore ownerStore = TestingUtil.extractComponent(owner, CacheLoaderManager.class).getCacheStore();
assert !nonOwnerStore.containsKey(key);
assert !ownerStore.containsKey(key);
Object retval = nonOwner.put(key, value);
asyncWait(key, PutKeyValueCommand.class, getSecondNonOwner(key));
assert !nonOwnerStore.containsKey(key);
assert ownerStore.containsKey(key);
if (testRetVals) assert retval == null;
assertOnAllCachesAndOwnership(key, value);
}

public void testGetFromNonOwnerWithFlags() throws Exception {
String key = "k2", value = "value2";
for (Cache<Object, String> c : caches) assert c.isEmpty();
Cache<Object, String> nonOwner = getFirstNonOwner(key);
Cache<Object, String> owner = getFirstOwner(key);
CacheStore ownerStore = TestingUtil.extractComponent(owner, CacheLoaderManager.class).getCacheStore();
owner.put(key, value);
assert value.equals(ownerStore.load(key).getValue());
owner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).clear();
assert value.equals(ownerStore.load(key).getValue());
assert owner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).get(key) == null;
assert nonOwner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).get(key) == null;
assert value.equals(nonOwner.get(key));
assertOnAllCachesAndOwnership(key, value);
}

public void testPutFromNonOwnerWithFlags() throws Exception {
String key = "k2", value = "value2";
for (Cache<Object, String> c : caches) assert c.isEmpty();
Cache<Object, String> nonOwner = getFirstNonOwner(key);
Cache<Object, String> owner = getFirstOwner(key);
CacheStore nonOwnerStore = TestingUtil.extractComponent(nonOwner, CacheLoaderManager.class).getCacheStore();
CacheStore ownerStore = TestingUtil.extractComponent(owner, CacheLoaderManager.class).getCacheStore();
assert !nonOwnerStore.containsKey(key);
assert !ownerStore.containsKey(key);
Object retval = nonOwner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).put(key, value);
assert !nonOwnerStore.containsKey(key);
assert !ownerStore.containsKey(key);
if (testRetVals) assert retval == null;
assertOnAllCachesAndOwnership(key, value);
}
Expand All @@ -70,7 +104,6 @@ public void testPutFromOwner() throws Exception {
String key = "k3", value = "value3";
for (Cache<Object, String> c : caches) assert c.isEmpty();
getOwners(key)[0].put(key, value);
asyncWait(key, PutKeyValueCommand.class, getNonOwners(key));
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
if (isOwner(c, key)) {
Expand All @@ -84,19 +117,12 @@ public void testPutFromOwner() throws Exception {
}

public void testPutAll() throws Exception {
String k1 = "1", v1 = "one", k2 = "2", v2 = "two", k3 = "3", v3 = "three", k4 = "4", v4 = "four";
String[] keys = new String[]{k1, k2, k3, k4};
Map<String, String> data = new HashMap<String, String>();
data.put(k1, v1);
data.put(k2, v2);
data.put(k3, v3);
data.put(k4, v4);

c1.putAll(data);
c1.putAll(makePutAllTestData());

for (String key : keys) {
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
for (String key : keys) {
if (isOwner(c, key)) {
assertIsInContainerImmortal(c, key);
assert store.containsKey(key);
Expand All @@ -107,6 +133,21 @@ public void testPutAll() throws Exception {
}
}

public void testPutAllWithFlags() throws Exception {
Map<String, String> data = makePutAllTestData();
c1.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).putAll(data);

for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
for (String key : keys) {
assert !store.containsKey(key);
if (isOwner(c, key)) {
assertIsInContainerImmortal(c, key);
}
}
}
}

public void testRemoveFromNonOwner() throws Exception {
String key = "k1", value = "value";
initAndTest();
Expand All @@ -122,13 +163,25 @@ public void testRemoveFromNonOwner() throws Exception {
}

Object retval = getFirstNonOwner(key).remove(key);
asyncWait("k1", RemoveCommand.class, getSecondNonOwner("k1"));
if (testRetVals) assert "value".equals(retval);
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert !store.containsKey(key);
}
}

public void testRemoveFromNonOwnerWithFlags() throws Exception {
String key = "k1", value = "value";
initAndTest();
Object retval = getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).remove(key);
if (testRetVals) assert value.equals(retval);
for (Cache<Object, String> c : caches) {
if (isOwner(c, key)) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert store.containsKey(key);
}
}
}

public void testReplaceFromNonOwner() throws Exception {
String key = "k1", value = "value", value2 = "v2";
Expand All @@ -145,30 +198,105 @@ public void testReplaceFromNonOwner() throws Exception {
}

Object retval = getFirstNonOwner(key).replace(key, value2);
asyncWait(key, ReplaceCommand.class, getSecondNonOwner(key));
if (testRetVals) assert value.equals(retval);
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
if (isOwner(c, key)) {
assertIsInContainerImmortal(c, key);
assert c.get(key).equals(value2);
assert store.load(key).getValue().equals(value2);
} else {
assert !store.containsKey(key);
}
}
}

public void testClear() throws Exception {

public void testReplaceFromNonOwnerWithFlag() throws Exception {
String key = "k1", value = "value", value2 = "v2";
initAndTest();
Object retval = getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).replace(key, value2);
if (testRetVals) assert value.equals(retval);
for (Cache<Object, String> c : caches) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
if (isOwner(c, key)) {
assertIsInContainerImmortal(c, key);
assert c.get(key).equals(value2);
assert store.load(key).getValue().equals(value);
} else {
assert !store.containsKey(key);
}
}
}

public void testAtomicReplaceFromNonOwner() throws Exception {
String key = "k1", value = "value", value2 = "v2";
initAndTest();
boolean replaced = getFirstNonOwner(key).replace(key, value2, value);
assert !replaced;
replaced = getFirstNonOwner(key).replace(key, value, value2);
assert replaced;
for (Cache<Object, String> c : caches) {
c.get(key).equals(value2);
if (isOwner(c, key)) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert store.containsKey(key);
assert store.load(key).getValue().equals(value2);
}
}
}

public void testAtomicReplaceFromNonOwnerWithFlag() throws Exception {
String key = "k1", value = "value", value2 = "v2";
initAndTest();
boolean replaced = getFirstNonOwner(key).replace(key, value2, value);
assert !replaced;
replaced = getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).replace(key, value, value2);
assert replaced;
for (Cache<Object, String> c : caches) {
c.get(key).equals(value2);
if (isOwner(c, key)) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert store.containsKey(key);
assert store.load(key).getValue().equals(value);
}
}
}

public void testAtomicPutIfAbsentFromNonOwner() throws Exception {
String key = "k1", value = "value", value2 = "v2";
for (Cache<Object, String> c : caches) assert c.isEmpty();
for (int i = 0; i < 5; i++) {
getOwners("k" + i)[0].put("k" + i, "value" + i);
asyncWait("k" + i, PutKeyValueCommand.class, getNonOwners("k" + i));
String replaced = getFirstNonOwner(key).putIfAbsent("k1", value);
assert replaced == null;
replaced = getFirstNonOwner(key).putIfAbsent("k1", value2);
assert value.equals(replaced);
for (Cache<Object, String> c : caches) {
c.get(key).equals(replaced);
if (isOwner(c, key)) {
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert store.containsKey(key);
assert store.load(key).getValue().equals(value);
}
}
// this will fill up L1 as well
for (int i = 0; i < 5; i++) assertOnAllCachesAndOwnership("k" + i, "value" + i);
for (Cache<Object, String> c : caches) assert !c.isEmpty();
}

public void testAtomicPutIfAbsentFromNonOwnerWithFlag() throws Exception {
String key = "k1", value = "value";
for (Cache<Object, String> c : caches) assert c.isEmpty();
String replaced = getFirstNonOwner(key).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).putIfAbsent("k1", value);
assert replaced == null;
//interesting case: fails to put as value exists, put actually missing in Store
replaced = getFirstNonOwner(key).putIfAbsent("k1", value);
assert value.equals(replaced);
for (Cache<Object, String> c : caches) {
c.get(key).equals(replaced);
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
assert !store.containsKey(key);
}
}

public void testClear() throws Exception {
prepareClearTest();
c1.clear();
asyncWait(null, ClearCommand.class);
for (Cache<Object, String> c : caches) assert c.isEmpty();
for (int i = 0; i < 5; i++) {
String key = "k" + i;
Expand All @@ -178,4 +306,51 @@ public void testClear() throws Exception {
}
}
}


public void testClearWithFlag() throws Exception {
prepareClearTest();
c1.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).clear();
for (Cache<Object, String> c : caches) {
assert c.isEmpty();
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
for (int i = 0; i < 5; i++) {
String key = "k" + i;
if (isOwner(c, key)) {
assert store.containsKey(key);
}
}
}
}

/*--- test helpers ---*/

private Map<String, String> makePutAllTestData() {
Map<String, String> data = new HashMap<String, String>();
data.put(k1, v1);
data.put(k2, v2);
data.put(k3, v3);
data.put(k4, v4);
return data;
}

private void prepareClearTest() throws CacheLoaderException {
for (Cache<Object, String> c : caches) assert c.isEmpty();
for (int i = 0; i < 5; i++) {
getOwners("k" + i)[0].put("k" + i, "value" + i);
}
// this will fill up L1 as well
for (int i = 0; i < 5; i++) assertOnAllCachesAndOwnership("k" + i, "value" + i);
for (Cache<Object, String> c : caches) {
assert !c.isEmpty();
CacheStore store = TestingUtil.extractComponent(c, CacheLoaderManager.class).getCacheStore();
for (int i = 0; i < 5; i++) {
String key = "k" + i;
if (isOwner(c, key)) {
assert store.containsKey(key);
}
}
}
}

}

0 comments on commit 7d6811f

Please sign in to comment.