use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class DistributedAckRegionCCEDUnitTest method testConcurrentOpWithGII.
/**
* test for bug #45564. a create() is received by region creator and then a later destroy() is
* received in initial image and while the version info from the destroy is recorded we keep the
* value from the create event
*/
@Test
public void testConcurrentOpWithGII() {
if (this.getClass() != DistributedAckRegionCCEDUnitTest.class) {
// not really a scope-related thing
return;
}
final String name = this.getUniqueName() + "-CC";
final String key = "mykey";
VM vm1 = Host.getHost(0).getVM(1);
VM vm2 = Host.getHost(0).getVM(2);
// create some destroyed entries so the GC service is populated
SerializableCallable create = new SerializableCallable("create region") {
public Object call() {
RegionFactory f = getCache().createRegionFactory(getRegionAttributes());
CCRegion = (LocalRegion) f.create(name);
return CCRegion.getDistributionManager().getDistributionManagerId();
}
};
// do conflicting update() and destroy() on the region. We want the update() to
// be sent with a message and the destroy() to be transferred in the initial image
// and be the value that we want to keep
InternalDistributedMember vm1ID = (InternalDistributedMember) vm1.invoke(create);
AsyncInvocation partialCreate = vm2.invokeAsync(new SerializableCallable("create region with stall") {
public Object call() throws Exception {
final GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
RegionFactory f = cache.createRegionFactory(getRegionAttributes());
InitialImageOperation.VMOTION_DURING_GII = true;
// this will stall region creation at the point of asking for an initial image
VMotionObserverHolder.setInstance(new VMotionObserver() {
@Override
public void vMotionBeforeCQRegistration() {
}
@Override
public void vMotionBeforeRegisterInterest() {
}
@Override
public void vMotionDuringGII(Set recipientSet, LocalRegion region) {
InitialImageOperation.VMOTION_DURING_GII = false;
int oldLevel = LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
LocalRegion ccregion = cache.getRegionByPath("/" + name);
try {
// happen
while (!ccregion.isDestroyed() && ccregion.getRegionEntry(key) == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
} finally {
LocalRegion.setThreadInitLevelRequirement(oldLevel);
}
}
});
try {
CCRegion = (LocalRegion) f.create(name);
// at this point we should have received the update op and then the GII, which should
// overwrite
// the conflicting update op
assertFalse("expected initial image transfer to destroy entry", CCRegion.containsKey(key));
} finally {
InitialImageOperation.VMOTION_DURING_GII = false;
}
return null;
}
});
vm1.invoke(new SerializableRunnable("create conflicting events") {
public void run() {
// wait for the other to come on line
long waitEnd = System.currentTimeMillis() + 45000;
DistributionAdvisor adv = ((DistributedRegion) CCRegion).getCacheDistributionAdvisor();
while (System.currentTimeMillis() < waitEnd && adv.adviseGeneric().isEmpty()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
if (adv.adviseGeneric().isEmpty()) {
fail("other member never came on line");
}
// inhibit all messaging
DistributedCacheOperation.LOSS_SIMULATION_RATIO = 200.0;
try {
CCRegion.put("mykey", "initialValue");
CCRegion.destroy("mykey");
} finally {
DistributedCacheOperation.LOSS_SIMULATION_RATIO = 0.0;
}
// generate a fake version tag for the message
VersionTag tag = CCRegion.getRegionEntry(key).getVersionStamp().asVersionTag();
// create a fake member ID that will be < mine and lose a concurrency check
NetMember nm = CCRegion.getDistributionManager().getDistributionManagerId().getNetMember();
InternalDistributedMember mbr = null;
try {
mbr = new InternalDistributedMember(nm.getInetAddress().getCanonicalHostName(), nm.getPort() - 1, "fake_id", "fake_id_ustring", DistributionManager.NORMAL_DM_TYPE, null, null);
tag.setMemberID(mbr);
} catch (UnknownHostException e) {
org.apache.geode.test.dunit.Assert.fail("could not create member id", e);
}
// generate an event to distribute that contains the fake version tag
EntryEventImpl event = EntryEventImpl.create(CCRegion, Operation.UPDATE, key, false, mbr, true, false);
event.setNewValue("newValue");
event.setVersionTag(tag);
// this should update the controller's cache with the updated value but leave this cache
// alone
DistributedCacheOperation op = new UpdateOperation(event, tag.getVersionTimeStamp());
op.distribute();
event.release();
}
});
try {
partialCreate.getResult();
} catch (Throwable e) {
org.apache.geode.test.dunit.Assert.fail("async invocation in vm2 failed", e);
}
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.
@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "Region";
final String key = "testKey";
int port = createServerRegion(vm0, name, true);
vm0.invoke(new SerializableCallable("create old entry") {
public Object call() throws Exception {
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
r.put(key, "value");
AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
// set an old timestamp in the entry - thirty minutes ago
entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
return null;
}
});
createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
vm1.invoke(new SerializableCallable("fetch entry and validate") {
public Object call() throws Exception {
final Long[] expirationTimeMillis = new Long[1];
int expirationSeconds = 15;
LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
AttributesMutator mutator = r.getAttributesMutator();
mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
mutator.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterDestroy(EntryEvent event) {
expirationTimeMillis[0] = System.currentTimeMillis();
}
});
// fetch the entry from the server and make sure it doesn't expire early
if (!r.containsKey(key)) {
r.get(key);
}
final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
return expirationTimeMillis[0] != null;
});
disconnectFromDS();
assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
return null;
}
});
vm0.invoke(new SerializableRunnable() {
public void run() {
disconnectFromDS();
}
});
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class PartitionedRegionDUnitTest method testRegionInvalidationWithAdjunctMessages.
/**
* Bug #47235 concerns assertion failures being thrown when there is a member that receives
* adjunct messages (as in a WAN gateway, a peer with clients, etc).
*/
@Test
public void testRegionInvalidationWithAdjunctMessages() throws Exception {
final String name = getUniqueName();
VM vm1 = Host.getHost(0).getVM(1);
Cache cache = getCache();
RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
Region pr = fact.create(name + "Region");
pr.put("Object1", "Value1");
vm1.invoke(new SerializableRunnable("create PR") {
@Override
public void run() {
RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
fact.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
fact.addCacheListener(new CacheListenerAdapter() {
@Override
public void afterInvalidate(EntryEvent event) {
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("afterInvalidate invoked with " + event);
InvalidateInvoked = true;
}
});
fact.create(name + "Region");
}
});
try {
pr.invalidateRegion();
assertTrue("vm1 should have invoked the listener for an invalidateRegion operation", (Boolean) vm1.invoke(new SerializableCallable("getStatus") {
public Object call() {
return InvalidateInvoked;
}
}));
} finally {
disconnectAllFromDS();
}
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class DistributedLockServiceDUnitTest method testLockQuery.
@Test
public void testLockQuery() throws Exception {
final String dlsName = getUniqueName();
final VM vmGrantor = Host.getHost(0).getVM(0);
final VM vm1 = Host.getHost(0).getVM(1);
final VM vm2 = Host.getHost(0).getVM(2);
final String key1 = "key1";
// vmGrantor creates grantor
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor creates grantor");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
assertTrue(dls.lock(key1, -1, -1));
assertTrue(dls.isLockGrantor());
dls.unlock(key1);
dls.freeResources(key1);
}
});
AsyncInvocation whileVM1Locks = null;
try {
// vm1 locks key1
whileVM1Locks = vm1.invokeAsync(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 locks key1");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
assertTrue(dls.lock(key1, -1, -1));
assertFalse(dls.isLockGrantor());
try {
synchronized (testLockQuery_whileVM1Locks) {
testLockQuery_whileVM1Locks.set(true);
testLockQuery_whileVM1Locks.notifyAll();
long maxWait = 10000;
StopWatch timer = new StopWatch(true);
while (testLockQuery_whileVM1Locks.get()) {
// while true
long timeLeft = maxWait - timer.elapsedTimeMillis();
if (timeLeft > 0) {
testLockQuery_whileVM1Locks.wait(timeLeft);
} else {
fail("Test attempted to wait too long");
}
}
}
} catch (InterruptedException e) {
org.apache.geode.test.dunit.Assert.fail(e.getMessage(), e);
}
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 unlocks key1");
dls.unlock(key1);
dls.freeResources(key1);
}
});
// wait for vm1 to set testLockQuery_whileVM1Locks
// get DistributedMember for vm1
final DistributedMember vm1Member = (DistributedMember) vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 waits for locking thread");
synchronized (testLockQuery_whileVM1Locks) {
long maxWait = 10000;
StopWatch timer = new StopWatch(true);
while (!testLockQuery_whileVM1Locks.get()) {
// while false
long timeLeft = maxWait - timer.elapsedTimeMillis();
if (timeLeft > 0) {
testLockQuery_whileVM1Locks.wait(timeLeft);
} else {
fail("Test attempted to wait too long");
}
}
}
return getSystem().getDistributedMember();
}
});
assertNotNull(vm1Member);
// vmGrantor tests positive local dlock query
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor tests local query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertTrue(result.getLeaseId() != -1);
assertEquals(Long.MAX_VALUE, result.getLeaseExpireTime());
RemoteThread lesseeThread = result.getLesseeThread();
assertNotNull(lesseeThread);
assertEquals(vm1Member, lesseeThread.getDistributedMember());
assertEquals(vm1Member, result.getLessee());
// nothing to test for on threadId unless we serialize info from vm1
}
});
// vm2 tests positive remote dlock query
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm2 tests remote query");
connectDistributedSystem();
DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertTrue(result.getLeaseId() != -1);
assertEquals(Long.MAX_VALUE, result.getLeaseExpireTime());
RemoteThread lesseeThread = result.getLesseeThread();
assertNotNull(lesseeThread);
assertEquals(vm1Member, lesseeThread.getDistributedMember());
assertEquals(vm1Member, result.getLessee());
// nothing to test for on threadId unless we serialize info from vm1
}
});
} finally {
// guarantee that testLockQuery_whileVM1Locks is notfied!
// vm1 sets and notifies testLockQuery_whileVM1Locks to release lock
vm1.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm1 notifies/releases key1");
synchronized (testLockQuery_whileVM1Locks) {
testLockQuery_whileVM1Locks.set(false);
testLockQuery_whileVM1Locks.notifyAll();
}
}
});
ThreadUtils.join(whileVM1Locks, 10 * 1000);
if (whileVM1Locks.exceptionOccurred()) {
org.apache.geode.test.dunit.Assert.fail("Test failed", whileVM1Locks.getException());
}
}
// vmGrantor tests negative local dlock query
vmGrantor.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vmGrantor tests negative query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertEquals(-1, result.getLeaseId());
assertEquals(0, result.getLeaseExpireTime());
assertNull(result.getLesseeThread());
assertNull(result.getLessee());
}
});
// vm2 tests negative remote dlock query
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriterUtils.getLogWriter().info("[testLockQuery] vm2 tests negative query");
DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
DLockRemoteToken result = dls.queryLock(key1);
assertNotNull(result);
assertEquals(key1, result.getName());
assertEquals(-1, result.getLeaseId());
assertEquals(0, result.getLeaseExpireTime());
assertNull(result.getLesseeThread());
assertNull(result.getLessee());
}
});
}
use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.
the class TXOrderDUnitTest method testInternalRegionNotExposed.
@Test
public void testInternalRegionNotExposed() {
Host host = Host.getHost(0);
VM vm1 = host.getVM(0);
VM vm2 = host.getVM(1);
SerializableCallable createRegion = new SerializableCallable() {
public Object call() throws Exception {
ExposedRegionTransactionListener tl = new ExposedRegionTransactionListener();
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ctm.addListener(tl);
ExposedRegionCacheListener cl = new ExposedRegionCacheListener();
AttributesFactory af = new AttributesFactory();
PartitionAttributes pa = new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(1).create();
af.setPartitionAttributes(pa);
af.addCacheListener(cl);
Region pr = createRootRegion("testTxEventForRegion", af.create());
return null;
}
};
vm1.invoke(createRegion);
vm2.invoke(createRegion);
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region pr = getRootRegion("testTxEventForRegion");
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
pr.put(2, "tw");
pr.put(3, "three");
pr.put(4, "four");
ctm.begin();
pr.put(1, "one");
pr.put(2, "two");
pr.invalidate(3);
pr.destroy(4);
ctm.commit();
return null;
}
});
SerializableCallable verifyListener = new SerializableCallable() {
public Object call() throws Exception {
Region pr = getRootRegion("testTxEventForRegion");
CacheTransactionManager ctm = getCache().getCacheTransactionManager();
ExposedRegionTransactionListener tl = (ExposedRegionTransactionListener) ctm.getListeners()[0];
ExposedRegionCacheListener cl = (ExposedRegionCacheListener) pr.getAttributes().getCacheListeners()[0];
assertFalse(tl.exceptionOccurred);
assertFalse(cl.exceptionOccurred);
return null;
}
};
vm1.invoke(verifyListener);
vm2.invoke(verifyListener);
}
Aggregations