use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class MultiVMRegionTestCase method testLocalAndRemoteCacheWriters.
/**
* Tests that, when given a choice, a local <code>CacheWriter</code> is invoked instead of a
* remote one.
*/
@Test
public void testLocalAndRemoteCacheWriters() throws Exception {
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object oldValue = "OLD_VALUE";
final Object newValue = "NEW_VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(new CacheSerializableRunnable("Create \"Local\" Region") {
@Override
public void run2() throws CacheException {
Region region = createRegion(name);
writer = new TestCacheWriter() {
@Override
public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
}
@Override
public void beforeCreate2(EntryEvent event) throws CacheWriterException {
}
@Override
public void beforeDestroy2(EntryEvent event) throws CacheWriterException {
}
@Override
public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
SerializableRunnable create = new CacheSerializableRunnable("Create \"Local\" Region") {
@Override
public void run2() throws CacheException {
Region region = createRegion(name);
writer = new TestCacheWriter() {
};
region.getAttributesMutator().setCacheWriter(writer);
}
};
vm1.invoke(create);
SerializableRunnable verify = new SerializableRunnable("Verify no callback") {
@Override
public void run() {
assertFalse(writer.wasInvoked());
}
};
vm0.invoke(new CacheSerializableRunnable("Create entry") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, oldValue);
assertTrue(writer.wasInvoked());
}
});
vm1.invoke(verify);
vm0.invoke(new CacheSerializableRunnable("Update entry") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, newValue);
assertTrue(writer.wasInvoked());
}
});
vm1.invoke(verify);
vm0.invoke(new CacheSerializableRunnable("Destroy entry") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.destroy(key);
assertTrue(writer.wasInvoked());
}
});
vm1.invoke(verify);
vm0.invoke(new CacheSerializableRunnable("Destroy region") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.destroyRegion();
assertTrue(writer.wasInvoked());
}
});
vm1.invoke(verify);
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class MultiVMRegionTestCase method testRemoteCacheWriter.
/**
* Tests that a remote {@link CacheWriter} is invoked and that <code>CacheWriter</code> arguments
* and {@link CacheWriterException}s are propagated appropriately.
*/
@Test
public void testRemoteCacheWriter() throws Exception {
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object oldValue = "OLD_VALUE";
final Object newValue = "NEW_VALUE";
final Object arg = "ARG";
final Object exception = "EXCEPTION";
final Object key2 = "KEY2";
final Object value2 = "VALUE2";
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
Region region = createRegion(name);
// Put key2 in the region before any callbacks are
// registered, so it can be destroyed later
region.put(key2, value2);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
LocalRegion reRegion;
reRegion = (LocalRegion) region;
RegionEntry re = reRegion.getRegionEntry(key2);
StoredObject so = (StoredObject) re._getValue();
assertEquals(1, so.getRefCount());
assertEquals(1, ma.getStats().getObjects());
}
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
//////// Create
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeCreate2(EntryEvent event) throws CacheWriterException {
if (exception.equals(event.getCallbackArgument())) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isCreate());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(null, event.getOldValue());
assertEquals(oldValue, event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
flushIfNecessary(region);
}
});
vm0.invoke(new CacheSerializableRunnable("Create with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.put(key, oldValue, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
assertNull(region.getEntry(key));
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Create with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, oldValue, arg);
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
assertEquals(1, so.getRefCount());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Update
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isUpdate());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(oldValue, event.getOldValue());
assertEquals(newValue, event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Update with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.put(key, newValue, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
Region.Entry entry = region.getEntry(key);
assertEquals(oldValue, entry.getValue());
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
StoredObject so = (StoredObject) reRegion.getRegionEntry(key)._getValue();
assertEquals(1, so.getRefCount());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Update with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.put(key, newValue, arg);
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Destroy
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeDestroy2(EntryEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isDestroy());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
assertEquals(key, event.getKey());
assertEquals(newValue, event.getOldValue());
assertNull(event.getNewValue());
assertFalse(event.getOperation().isLoad());
assertFalse(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.destroy(key, exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
assertNotNull(region.getEntry(key));
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.destroy(key, arg);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
//////// Region Destroy
vm1.invoke(new CacheSerializableRunnable("Set Writer") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
writer = new TestCacheWriter() {
@Override
public void beforeRegionDestroy2(RegionEvent event) throws CacheWriterException {
Object argument = event.getCallbackArgument();
if (exception.equals(argument)) {
String s = "Test Exception";
throw new CacheWriterException(s);
}
assertEquals(arg, argument);
assertEquals(region, event.getRegion());
assertTrue(event.getOperation().isRegionDestroy());
assertTrue(event.getOperation().isDistributed());
assertFalse(event.getOperation().isExpiration());
assertTrue(event.isOriginRemote());
}
};
region.getAttributesMutator().setCacheWriter(writer);
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Exception") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
region.destroyRegion(exception);
fail("Should have thrown a CacheWriterException");
} catch (CacheWriterException ex) {
if (region.isDestroyed()) {
fail("should not have an exception if region is destroyed", ex);
}
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
vm0.invoke(new CacheSerializableRunnable("Destroy with Argument") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
region.destroyRegion(arg);
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
final MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
WaitCriterion waitForStatChange = new WaitCriterion() {
@Override
public boolean done() {
return ma.getStats().getObjects() == 0;
}
@Override
public String description() {
return "never saw off-heap object count go to zero. Last value was " + ma.getStats().getObjects();
}
};
Wait.waitForCriterion(waitForStatChange, 3000, 10, true);
}
}
});
vm1.invoke(new SerializableRunnable("Verify callback") {
@Override
public void run() {
assertTrue(writer.wasInvoked());
}
});
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class MapInterface2JUnitTest method testBasicMapAfterClearCalback.
@Test
public void testBasicMapAfterClearCalback() {
Region rgn = CacheUtils.getRegion("Portfolios");
AttributesMutator atm = rgn.getAttributesMutator();
atm.setCacheListener(new CacheListenerAdapter() {
public void afterRegionClear(RegionEvent event) {
synchronized (MapInterface2JUnitTest.this) {
event.getRegion().getCache().getLogger().info("afterRegionClear call back " + event);
afterClearCallbackOccurred = true;
MapInterface2JUnitTest.this.notify();
}
}
});
int size = rgn.size();
assertTrue("MapInterface2JUnitTest::basicMapClearNonTranxn: The init size of region is zero", size > 0);
rgn.clear();
if (rgn.size() != 0) {
fail("The region size is non zero even after issuing clear");
}
if (rgn.size() != 0) {
fail("The region size is non zero even after issuing clear");
}
try {
synchronized (this) {
if (!this.afterClearCallbackOccurred) {
this.wait(10000);
}
}
} catch (InterruptedException ie) {
fail(ie.toString());
}
if (!this.afterClearCallbackOccurred) {
fail("afterClear Callback not issued");
}
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class MapInterface2JUnitTest method testBlockGlobalScopeInSingleVM.
@Test
public void testBlockGlobalScopeInSingleVM() {
CacheUtils.getCache().setLockLease(40);
CacheUtils.getCache().setLockTimeout(5);
final Region region = CacheUtils.createRegion("Global", String.class, Scope.GLOBAL);
for (int i = 0; i < 10; i++) {
region.put("" + i, "" + i);
}
final Object callbackSync = new Object();
final boolean[] canCallbackProceed = new boolean[] { false };
LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
CacheObserverHolder.setInstance(new CacheObserverAdapter() {
public void afterRegionClear(RegionEvent event) {
try {
synchronized (MapInterface2JUnitTest.this) {
MapInterface2JUnitTest.this.mainThreadProceed = true;
MapInterface2JUnitTest.this.notify();
}
event.getRegion().getCache().getLogger().info("*******Main THread Notified *********");
synchronized (callbackSync) {
long maxWait = 20000;
StopWatch timer = new StopWatch(true);
while (!canCallbackProceed[0]) {
long timeLeft = maxWait - timer.elapsedTimeMillis();
if (timeLeft > 0) {
callbackSync.wait(timeLeft);
} else {
fail("testBlockGlobalScopeInSingleVM attempted to wait too long");
}
}
}
event.getRegion().getCache().getLogger().info("******* Callback complete *********");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
});
Thread th = new Thread(new Runnable() {
public void run() {
region.clear();
}
});
th.start();
try {
synchronized (this) {
if (!this.mainThreadProceed) {
region.getCache().getLogger().info("*******Main THread is going in wait********");
this.wait();
}
}
region.getCache().getLogger().info("*******Main THread coming out of wait*********");
region.put("test", "test");
fail("The put operation should not have succeeded");
} catch (org.apache.geode.cache.TimeoutException cwe) {
assertTrue("The test correctly encounetred a TimeoutException" + cwe.toString(), true);
} catch (InterruptedException ie) {
fail("The main thread experienced Interruption" + ie);
} finally {
synchronized (callbackSync) {
canCallbackProceed[0] = true;
callbackSync.notify();
}
}
ThreadUtils.join(th, 30 * 1000);
}
use of org.apache.geode.cache.RegionEvent in project geode by apache.
the class MapInterfaceJUnitTest method testBeforeRegionClearCallBack.
@Test
public void testBeforeRegionClearCallBack() {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = null;
Region region = null;
AttributesFactory factory = null;
try {
cache = CacheFactory.create(ds);
factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setCacheWriter(new CacheWriterAdapter() {
@Override
public void beforeRegionClear(RegionEvent event) throws CacheWriterException {
synchronized (this) {
this.notify();
MapInterfaceJUnitTest.this.hasBeenNotified = true;
}
}
});
region = cache.createRegion("testingRegion", factory.create());
DoesClear doesClear = new DoesClear(region);
new Thread(doesClear).start();
synchronized (this) {
if (!this.hasBeenNotified) {
this.wait(3000);
}
}
if (!this.hasBeenNotified) {
fail(" beforeRegionClear call back did not come");
}
} catch (Exception e) {
throw new AssertionError(" failed due to ", e);
}
for (int i = 0; i < 100; i++) {
region.put(new Integer(i), new Integer(i));
}
assertEquals(new Integer(50), region.get(new Integer(50)));
region.localClear();
assertEquals(null, region.get(new Integer(50)));
region.close();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
try {
region = cache.createRegion("testingRegion", factory.create());
} catch (Exception e) {
throw new AssertionError(" failed in creating region due to ", e);
}
boolean exceptionOccurred = false;
try {
region.localClear();
} catch (UnsupportedOperationException e) {
exceptionOccurred = true;
}
if (!exceptionOccurred) {
fail(" exception did not occur when it was supposed to occur");
}
region.close();
cache.close();
ds.disconnect();
}
Aggregations