use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class EventIDVerificationDUnitTest method createServerCache.
public static Integer createServerCache() throws Exception {
new EventIDVerificationDUnitTest().createCache(new Properties());
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setCacheListener(new CacheListenerAdapter() {
public void afterCreate(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterUpdate(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterDestroy(EntryEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterRegionDestroy(RegionEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
public void afterRegionClear(RegionEvent event) {
synchronized (EventIDVerificationDUnitTest.class) {
gotCallback = true;
// verifyEventIDsDuringRegionDestroy(event);
testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
EventIDVerificationDUnitTest.class.notify();
}
}
});
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
CacheServer server1 = cache.addCacheServer();
server1.setPort(port);
server1.setNotifyBySubscription(true);
server1.start();
return new Integer(server1.getPort());
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class EventIDVerificationDUnitTest method createClientCache.
public static void createClientCache(String host, Integer port1, Integer port2) throws Exception {
PORT1 = port1.intValue();
PORT2 = port2.intValue();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
new EventIDVerificationDUnitTest().createCache(props);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setMirrorType(MirrorType.NONE);
ClientServerTestCase.configureConnectionPool(factory, host, new int[] { PORT1, PORT2 }, true, -1, 2, null, -1, -1, false, -2);
CacheWriter writer = new CacheWriterAdapter() {
public void beforeCreate(EntryEvent event) {
EventID eventId = ((EntryEventImpl) event).getEventId();
vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
try {
super.beforeCreate(event);
} catch (CacheWriterException e) {
e.printStackTrace();
fail("Test failed bcoz of exception =" + e);
}
}
public void beforeUpdate(EntryEvent event) {
EventID eventId = ((EntryEventImpl) event).getEventId();
vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
try {
super.beforeUpdate(event);
} catch (CacheWriterException e) {
e.printStackTrace();
fail("Test failed bcoz of exception =" + e);
}
}
public void beforeDestroy(EntryEvent event) {
EventID eventId = ((EntryEventImpl) event).getEventId();
vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
try {
super.beforeDestroy(event);
} catch (CacheWriterException e) {
e.printStackTrace();
fail("Test failed bcoz of exception =" + e);
}
}
public void beforeRegionDestroy(RegionEvent event) {
EventID eventId = ((RegionEventImpl) event).getEventId();
vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
try {
super.beforeRegionDestroy(event);
} catch (CacheWriterException e) {
e.printStackTrace();
fail("Test failed bcoz of exception =" + e);
}
}
public void beforeRegionClear(RegionEvent event) {
EventID eventId = ((RegionEventImpl) event).getEventId();
vm0.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
vm1.invoke(() -> EventIDVerificationDUnitTest.setEventIDData(eventId));
try {
super.beforeRegionClear(event);
} catch (CacheWriterException e) {
e.printStackTrace();
fail("Test failed bcoz of exception =" + e);
}
}
};
factory.setCacheWriter(writer);
/*
* factory.setCacheListener(new CacheListenerAdapter() { public void afterCreate(EntryEvent
* event) { synchronized (this) { threadId = ((EntryEventImpl)event).getEventId().getThreadID();
* membershipId = ((EntryEventImpl)event).getEventId().getMembershipID(); } }
*
* public void afterUpdate(EntryEvent event) { synchronized (this) { verifyEventIDs(event); } }
*
* public void afterDestroy(EntryEvent event) { synchronized (this) { verifyEventIDs(event); } }
* public void afterRegionDestroy(RegionEvent event) { synchronized (this) { threadId =
* ((RegionEventImpl)event).getEventId().getThreadID(); membershipId =
* ((RegionEventImpl)event).getEventId().getMembershipID(); } } });
*/
RegionAttributes attrs = factory.create();
Region r = cache.createRegion(REGION_NAME, attrs);
r.registerInterest("ALL_KEYS");
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class SearchAndLoadDUnitTest method testOneHopNetWriteRemoteWriter.
/** same as the previous test but the cache writer is in a third, non-replicated, vm */
@Test
public void testOneHopNetWriteRemoteWriter() throws CacheException, InterruptedException {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
final String name = this.getUniqueName() + "Region";
final String objectName = "Object7";
final Integer value = new Integer(483);
final Integer updateValue = new Integer(484);
vm0.invoke(new SerializableRunnable("Create replicate Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating empty region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create empty Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.EMPTY);
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating empty region", ex);
}
}
});
vm2.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {
public void run() {
netWriteInvoked = false;
operationWasCreate = false;
originWasRemote = false;
writerInvocationCount = 0;
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.EMPTY);
factory.setCacheWriter(new CacheWriter() {
public void beforeCreate(EntryEvent e) throws CacheWriterException {
e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
netWriteInvoked = true;
operationWasCreate = true;
originWasRemote = e.isOriginRemote();
writerInvocationCount++;
return;
}
public void beforeUpdate(EntryEvent e) throws CacheWriterException {
e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
netWriteInvoked = true;
operationWasCreate = false;
originWasRemote = e.isOriginRemote();
writerInvocationCount++;
return;
}
public void beforeDestroy(EntryEvent e) throws CacheWriterException {
}
public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
}
public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating replicated region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {
public void run() {
try {
getRootRegion().getSubregion(name).put(objectName, value);
} catch (CacheWriterException cwe) {
} catch (TimeoutException te) {
}
}
});
vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
public void run() {
assertTrue("expected cache writer to be invoked", netWriteInvoked);
assertTrue("expected originRemote to be true", originWasRemote);
assertTrue("expected event to be create", operationWasCreate);
assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
// set flags for the next test - updating the same key
netWriteInvoked = false;
writerInvocationCount = 0;
}
});
vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {
public void run() {
try {
getRootRegion().getSubregion(name).put(objectName, updateValue);
} catch (CacheWriterException cwe) {
} catch (TimeoutException te) {
}
}
});
vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {
public void run() {
assertTrue("expected cache writer to be invoked", netWriteInvoked);
assertTrue("expected originRemote to be true", originWasRemote);
assertTrue("expected event to be create", operationWasCreate);
assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
}
});
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class RegionTestCase method testRegionIdleInvalidate.
/**
* Tests that a region that remains idle for a given amount of time is invalidated. Also tests
* that accessing an entry of a region or a subregion counts as an access.
*/
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
if (getRegionAttributes().getPartitionAttributes() != null) {
// PR does not support INVALID ExpirationAction
return;
}
final String name = this.getUniqueName();
final String subname = this.getUniqueName() + "-SUB";
// ms
final int timeout = 22;
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {
public void run2() throws CacheException {
TestCacheListener list = new TestCacheListener() {
private int createCount = 0;
public void afterInvalidate2(EntryEvent e) {
e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
}
public void afterRegionInvalidate2(RegionEvent e) {
}
public void afterUpdate2(EntryEvent e) {
// Clear the flag
this.wasInvoked();
}
public void afterCreate2(EntryEvent e) {
this.createCount++;
// we only expect one create; all the rest should be updates
assertEquals(1, this.createCount);
// Clear the flag
this.wasInvoked();
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setRegionIdleTimeout(expire);
factory.setStatisticsEnabled(true);
RegionAttributes subRegAttrs = factory.create();
factory.setCacheListener(list);
RegionAttributes attrs = factory.create();
Region region = null;
Region sub = null;
Region.Entry entry = null;
long tilt;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
ExpiryTask.suspendExpiration();
try {
region = createRegion(name, attrs);
region.put(key, value);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertEquals(value, entry.getValue());
sub = region.createSubregion(subname, subRegAttrs);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt, 10);
assertTrue(list.waitForInvocation(333));
// The next phase of the test verifies that a get will cause the
// expiration time to be extended.
// For this phase we don't worry about actually expiring but just
// making sure the expiration time gets extended.
final int EXPIRATION_MS = 9000;
region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
LocalRegion lr = (LocalRegion) region;
{
ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
region.put(key, value);
long createExpiry = expiryTask.getExpirationTime();
long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
region.put(key, "VALUE2");
long putExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
region.get(key);
long getExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
sub.put(key, value);
long subPutExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
sub.get(key);
long subGetExpiry = expiryTask.getExpirationTime();
assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
}
}
});
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class RegionReliabilityTestCase method testCommitDistributionException.
@Test
public void testCommitDistributionException() throws Exception {
if (getRegionScope().isGlobal())
// skip test under Global
return;
if (getRegionScope().isDistributedNoAck())
// skip test under DistributedNoAck
return;
final String name = this.getUniqueName();
final String roleA = name + "-A";
final String[] requiredRoles = { roleA };
Set requiredRolesSet = new HashSet();
for (int i = 0; i < requiredRoles.length; i++) {
requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
}
assertEquals(requiredRoles.length, requiredRolesSet.size());
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
RegionMembershipListener listener = new RegionMembershipListenerAdapter() {
public void afterRemoteRegionDeparture(RegionEvent event) {
synchronized (detectedDeparture_testCommitDistributionException) {
detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
detectedDeparture_testCommitDistributionException.notify();
}
}
};
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(getRegionScope());
fac.addCacheListener(listener);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// use vm1 to create role
Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createConnection(new String[] { roleA });
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
});
// define the afterReleaseLocalLocks callback
SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {
public void run() {
Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {
public void run() {
getRootRegion(name).close();
}
});
try {
synchronized (detectedDeparture_testCommitDistributionException) {
while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
detectedDeparture_testCommitDistributionException.wait();
}
}
} catch (InterruptedException e) {
fail("interrupted");
}
}
};
// define the add and remove expected exceptions
final String expectedExceptions = "org.apache.geode.internal.cache.CommitReplyException";
SerializableRunnable addExpectedExceptions = new CacheSerializableRunnable("addExpectedExceptions") {
public void run2() throws CacheException {
getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
}
};
SerializableRunnable removeExpectedExceptions = new CacheSerializableRunnable("removeExpectedExceptions") {
public void run2() throws CacheException {
getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
}
};
// perform the actual test...
CacheTransactionManager ctm = cache.getCacheTransactionManager();
ctm.begin();
TXStateInterface txStateProxy = ((TXManagerImpl) ctm).getTXState();
((TXStateProxyImpl) txStateProxy).forceLocalBootstrap();
TXState txState = (TXState) ((TXStateProxyImpl) txStateProxy).getRealDeal(null, null);
txState.setBeforeSend(() -> {
try {
removeRequiredRole.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
// now start a transaction and commit it
region.put("KEY", "VAL");
addExpectedExceptions.run();
Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
try {
ctm.commit();
fail("Should have thrown CommitDistributionException");
} catch (CommitDistributionException e) {
// pass
} finally {
removeExpectedExceptions.run();
Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
}
}
Aggregations