use of org.apache.geode.cache.CacheWriter in project geode by apache.
the class LocalRegion method cacheWriteBeforeRegionClear.
private void cacheWriteBeforeRegionClear(RegionEventImpl event) throws CacheWriterException, TimeoutException {
// copy into local var to prevent race condition
CacheWriter writer = basicGetWriter();
if (writer != null) {
final long start = getCachePerfStats().startCacheWriterCall();
try {
writer.beforeRegionClear(event);
} finally {
getCachePerfStats().endCacheWriterCall(start);
}
}
serverRegionClear(event);
}
use of org.apache.geode.cache.CacheWriter in project geode by apache.
the class PartitionedRegion method cacheWriteBeforeDestroy.
/**
* Invoke the CacheWriter before the detroy operation occurs. Each BucketRegion delegates to the
* CacheWriter on the PartitionedRegion meaning that CacheWriters on a BucketRegion should only be
* used for internal purposes.
*
* @see BucketRegion#cacheWriteBeforeDestroy(EntryEventImpl, Object)
*/
@Override
boolean cacheWriteBeforeDestroy(EntryEventImpl event, Object expectedOldValue) throws CacheWriterException, EntryNotFoundException, TimeoutException {
// return if notifications are inhibited
if (event.inhibitAllNotifications()) {
if (logger.isDebugEnabled()) {
logger.debug("Notification inhibited for key {}", event.getKey());
}
return false;
}
if (event.isDistributed()) {
serverDestroy(event, expectedOldValue);
CacheWriter localWriter = basicGetWriter();
Set netWriteRecipients = localWriter == null ? this.distAdvisor.adviseNetWrite() : null;
if (localWriter == null && (netWriteRecipients == null || netWriteRecipients.isEmpty())) {
return false;
}
final long start = getCachePerfStats().startCacheWriterCall();
try {
event.setOldValueFromRegion();
SearchLoadAndWriteProcessor processor = SearchLoadAndWriteProcessor.getProcessor();
processor.initialize(this, event.getKey(), null);
processor.doNetWrite(event, netWriteRecipients, localWriter, SearchLoadAndWriteProcessor.BEFOREDESTROY);
processor.release();
} finally {
getCachePerfStats().endCacheWriterCall(start);
}
return true;
}
return false;
}
use of org.apache.geode.cache.CacheWriter in project geode by apache.
the class CacheXml66DUnitTest method testCacheWriter.
/**
* Tests a cache writer with no parameters
*/
@Test
public void testCacheWriter() throws Exception {
CacheCreation cache = new CacheCreation();
RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
CacheWriter writer = new MyTestCacheWriter();
attrs.setCacheWriter(writer);
cache.createRegion("root", attrs);
testXml(cache);
}
use of org.apache.geode.cache.CacheWriter 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.CacheWriter 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);
}
});
}
Aggregations