use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class TXOrderDUnitTest method testFarSideOpForLoad.
/**
* Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
* when create executed transactionally"
*/
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
Host host = Host.getHost(0);
VM vm1 = host.getVM(0);
VM vm2 = host.getVM(1);
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
assertTrue(e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
return helper.getKey() + "_value";
}
public void close() {
}
};
af.setCacheLoader(cl);
createRootRegion("r1", af.create());
return null;
}
});
vm2.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListenerAdapter() {
public void afterCreate(EntryEvent e) {
LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
assertTrue(!e.getOperation().isLocalLoad());
}
};
af.addCacheListener(cl1);
createRootRegion("r1", af.create());
return null;
}
});
vm1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region r = getRootRegion("r1");
getCache().getCacheTransactionManager().begin();
r.get("obj_2");
getCache().getCacheTransactionManager().commit();
return null;
}
});
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class SearchAndLoadDUnitTest method testConcurrentLoad.
/**
* This test is for a bug in which a cache loader threw an exception that caused the wrong value
* to be put in a Future in nonTxnFindObject. This in turn caused a concurrent search for the
* object to not invoke the loader a second time.
*
* VM0 is used to create a cache and a region having a loader that simulates the conditions that
* caused the bug. One async thread then does a get() which invokes the loader. Another async
* thread does a get() which reaches nonTxnFindObject and blocks waiting for the first thread's
* load to complete. The loader then throws an exception that is sent back to the first thread.
* The second thread should then cause the loader to be invoked again, and this time the loader
* will return a value. Both threads then validate that they received the expected result.
*/
@Test
public void testConcurrentLoad() throws Throwable {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
final String name = this.getUniqueName() + "Region";
final String objectName = "theKey";
final Integer value = new Integer(44);
final String exceptionString = "causing first cache-load to fail";
remoteLoaderInvoked = false;
loaderInvoked = false;
vm0.invoke(new CacheSerializableRunnable("create region " + name + " in vm0") {
public void run2() {
remoteLoaderInvoked = false;
loaderInvoked = false;
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setConcurrencyChecksEnabled(true);
factory.setCacheLoader(new CacheLoader() {
boolean firstInvocation = true;
public synchronized Object load(LoaderHelper helper) {
System.out.println("invoked cache loader for " + helper.getKey());
loaderInvoked = true;
loaderInvokedLatch.countDown();
if (firstInvocation) {
firstInvocation = false;
try {
// wait for both threads to be ready for the exception to be thrown
System.out.println("waiting for vm0t2 to be ready before throwing exception");
readyForExceptionLatch.await(30, TimeUnit.SECONDS);
// give the second thread time to get into loader code
Thread.sleep(5000);
} catch (InterruptedException e) {
fail("interrupted");
}
System.out.println("throwing exception");
exceptionThrown = true;
throw new RuntimeException(exceptionString);
}
System.out.println("returning value=" + value);
return value;
}
public void close() {
}
});
Region region = createRegion(name, factory.create());
region.create(objectName, null);
IgnoredException.addIgnoredException(exceptionString);
}
});
AsyncInvocation async1 = null;
try {
async1 = vm0.invokeAsync(new CacheSerializableRunnable("Concurrently invoke the remote loader on the same key - t1") {
public void run2() {
Region region = getCache().getRegion("root/" + name);
LogWriterUtils.getLogWriter().info("t1 is invoking get(" + objectName + ")");
try {
LogWriterUtils.getLogWriter().info("t1 retrieved value " + region.get(objectName));
fail("first load should have triggered an exception");
} catch (RuntimeException e) {
if (!e.getMessage().contains(exceptionString)) {
throw e;
}
}
}
});
vm0.invoke(new CacheSerializableRunnable("Concurrently invoke the loader on the same key - t2") {
public void run2() {
final Region region = getCache().getRegion("root/" + name);
final Object[] valueHolder = new Object[1];
// wait for vm1 to cause the loader to be invoked
LogWriterUtils.getLogWriter().info("t2 is waiting for loader to be invoked by t1");
try {
loaderInvokedLatch.await(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("interrupted");
}
assertTrue(loaderInvoked);
Thread t = new Thread("invoke get()") {
public void run() {
try {
valueHolder[0] = region.get(objectName);
} catch (RuntimeException e) {
valueHolder[0] = e;
}
}
};
t.setDaemon(true);
t.start();
try {
// let the thread get to the point of blocking on vm1's Future
// in LocalRegion.nonTxnFindObject()
Thread.sleep(5000);
} catch (InterruptedException e) {
fail("interrupted");
}
readyForExceptionLatch.countDown();
try {
t.join(30000);
} catch (InterruptedException e) {
fail("interrupted");
}
if (t.isAlive()) {
t.interrupt();
fail("get() operation blocked for too long - test needs some work");
}
LogWriterUtils.getLogWriter().info("t2 is invoking get(" + objectName + ")");
Object value = valueHolder[0];
if (value instanceof RuntimeException) {
if (((Exception) value).getMessage().contains(exceptionString)) {
fail("second load should not have thrown an exception");
} else {
throw (RuntimeException) value;
}
} else {
LogWriterUtils.getLogWriter().info("t2 retrieved value " + value);
assertNotNull(value);
}
}
});
} finally {
if (async1 != null) {
async1.join();
if (async1.exceptionOccurred()) {
throw async1.getException();
}
}
}
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class MultiVMRegionTestCase method versionTestConcurrentEventsOnNonReplicatedRegion.
/**
* This tests the concurrency versioning system to ensure that event conflation happens correctly
* and that the statistic is being updated properly
*/
public void versionTestConcurrentEventsOnNonReplicatedRegion() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
// this VM, but treat as a remote for uniformity
VM vm3 = host.getVM(3);
final boolean noAck = !getRegionAttributes().getScope().isAck();
// create an empty region in vm0 and replicated regions in VM 1 and 3,
// then perform concurrent ops
// on the same key while creating the region in VM2. Afterward make
// sure that all three regions are consistent
final String name = this.getUniqueName() + "-CC";
SerializableRunnable createRegion = new SerializableRunnable("Create Region") {
@Override
public void run() {
try {
final RegionFactory f;
if (VM.getCurrentVMNum() == 0) {
f = getCache().createRegionFactory(getRegionAttributes(RegionShortcut.LOCAL.toString()));
f.setScope(getRegionAttributes().getScope());
} else {
f = getCache().createRegionFactory(getRegionAttributes());
}
CCRegion = (LocalRegion) f.create(name);
} catch (CacheException ex) {
fail("While creating region", ex);
}
}
};
vm0.invoke(createRegion);
vm1.invoke(createRegion);
vm3.invoke(createRegion);
SerializableRunnable performOps = new SerializableRunnable("perform concurrent ops") {
@Override
public void run() {
try {
doOpsLoop(5000, false);
sendSerialMessageToAll();
if (CCRegion.getAttributes().getDataPolicy().withReplication()) {
long events = CCRegion.getCachePerfStats().getConflatedEventsCount();
assertTrue("expected some event conflation", events > 0);
}
} catch (CacheException e) {
fail("while performing concurrent operations", e);
}
}
};
AsyncInvocation a0 = vm0.invokeAsync(performOps);
AsyncInvocation a1 = vm1.invokeAsync(performOps);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
fail("sleep was interrupted");
}
vm2.invoke(createRegion);
boolean a0failed = waitForAsyncProcessing(a0, "expected some event conflation");
boolean a1failed = waitForAsyncProcessing(a1, "expected some event conflation");
if (a0failed && a1failed) {
fail("neither member saw event conflation - check stats for " + name);
}
// check consistency of the regions
Map r0Contents = (Map) vm0.invoke(() -> this.getCCRegionContents());
Map r1Contents = (Map) vm1.invoke(() -> this.getCCRegionContents());
Map r2Contents = (Map) vm2.invoke(() -> this.getCCRegionContents());
Map r3Contents = (Map) vm3.invoke(() -> this.getCCRegionContents());
for (int i = 0; i < 10; i++) {
String key = "cckey" + i;
assertEquals("region contents are not consistent", r1Contents.get(key), r2Contents.get(key));
assertEquals("region contents are not consistent", r2Contents.get(key), r3Contents.get(key));
for (int subi = 1; subi < 3; subi++) {
String subkey = key + "-" + subi;
if (r1Contents.containsKey(subkey)) {
assertEquals("region contents are not consistent", r1Contents.get(subkey), r2Contents.get(subkey));
assertEquals("region contents are not consistent", r2Contents.get(subkey), r3Contents.get(subkey));
if (r0Contents.containsKey(subkey)) {
assertEquals("region contents are not consistent", r1Contents.get(subkey), r0Contents.get(subkey));
}
} else {
assertTrue(!r2Contents.containsKey(subkey));
assertTrue(!r3Contents.containsKey(subkey));
assertTrue(!r0Contents.containsKey(subkey));
}
}
}
// a non-replicate region should not carry tombstones, so we'll check for that.
// then we'll use a cache loader and make sure that 1-hop is used to put the
// entry into the cache in a replicate. The new entry should have a version
// of 1 when it's first created, the tombstone should have version 2 and
// the loaded value should have version 3
final String loadKey = "loadKey";
vm0.invoke(new SerializableRunnable("add cache loader and create destroyed entry") {
@Override
public void run() {
CCRegion.getAttributesMutator().setCacheLoader(new CacheLoader() {
@Override
public void close() {
}
@Override
public Object load(LoaderHelper helper) throws CacheLoaderException {
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("The test CacheLoader has been invoked for key '" + helper.getKey() + "'");
return "loadedValue";
}
});
CCRegion.put(loadKey, "willbeLoadedInitialValue");
CCRegion.destroy(loadKey);
if (noAck) {
// flush for validation to work
sendSerialMessageToAll();
}
// this assertion guarantees that non-replicated regions do not create tombstones.
// this is currently not the case but is an open issue
// assertTrue(CCRegion.getRegionEntry(loadKey) == null);
}
});
vm1.invoke(new SerializableRunnable("confirm tombstone") {
@Override
public void run() {
assertTrue(Token.TOMBSTONE == CCRegion.getRegionEntry(loadKey).getValueInVM(CCRegion));
}
});
vm0.invoke(new SerializableRunnable("use cache loader") {
@Override
public void run() {
assertEquals("loadedValue", CCRegion.get(loadKey));
assertEquals(3, (CCRegion.getRegionEntry(loadKey)).getVersionStamp().getEntryVersion());
}
});
if (!noAck) {
// might be 3 or 4 with no-ack
vm1.invoke(new SerializableRunnable("verify version number") {
@Override
public void run() {
assertEquals("loadedValue", CCRegion.get(loadKey));
assertEquals(3, (CCRegion.getRegionEntry(loadKey)).getVersionStamp().getEntryVersion());
}
});
}
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class ProxyDUnitTest method distributedOps.
////////////////////// Test Methods //////////////////////
/**
* check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
*/
private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(dp);
af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
af.setScope(Scope.DISTRIBUTED_ACK);
Region r = createRootRegion("ProxyDUnitTest", af.create());
doCreateOtherVm();
r.put("putkey", "putvalue1");
getOtherVm().invoke(new CacheSerializableRunnable("check put") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(true, r.containsKey("putkey"));
assertEquals("putvalue1", r.getEntry("putkey").getValue());
r.put("putkey", "putvalue2");
}
});
assertEquals(false, r.containsKey("putkey"));
// netsearch
assertEquals("putvalue2", r.get("putkey"));
r.invalidate("putkey");
getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(true, r.containsKey("putkey"));
assertEquals(null, r.getEntry("putkey").getValue());
}
});
// invalid so total miss
assertEquals(null, r.get("putkey"));
r.destroy("putkey");
getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(false, r.containsKey("putkey"));
}
});
// total miss
assertEquals(null, r.get("putkey"));
r.create("createKey", "createValue1");
getOtherVm().invoke(new CacheSerializableRunnable("check create") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(true, r.containsKey("createKey"));
assertEquals("createValue1", r.getEntry("createKey").getValue());
}
});
{
Map m = new HashMap();
m.put("putAllKey1", "putAllValue1");
m.put("putAllKey2", "putAllValue2");
r.putAll(m, "putAllCallback");
}
getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(true, r.containsKey("putAllKey1"));
assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
assertEquals(true, r.containsKey("putAllKey2"));
assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
}
});
r.clear();
getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(0, r.size());
}
});
getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
AttributesMutator am = r.getAttributesMutator();
CacheWriter cw = new CacheWriterAdapter() {
public void beforeCreate(EntryEvent event) throws CacheWriterException {
throw new CacheWriterException("expected");
}
};
am.setCacheWriter(cw);
}
});
try {
r.put("putkey", "putvalue");
fail("expected CacheWriterException");
} catch (CacheWriterException expected) {
}
getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(0, r.size());
}
});
// total miss
assertEquals(null, r.get("loadkey"));
getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
AttributesMutator am = r.getAttributesMutator();
// clear csche writer
am.setCacheWriter(null);
CacheLoader cl = new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
if (helper.getKey().equals("loadkey")) {
return "loadvalue";
} else if (helper.getKey().equals("loadexception")) {
throw new CacheLoaderException("expected");
} else {
return null;
}
}
public void close() {
}
};
am.setCacheLoader(cl);
}
});
// net load
assertEquals("loadvalue", r.get("loadkey"));
// total miss
assertEquals(null, r.get("foobar"));
try {
r.get("loadexception");
fail("expected CacheLoaderException");
} catch (CacheLoaderException expected) {
}
r.destroyRegion();
getOtherVm().invoke(new CacheSerializableRunnable("check clear") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
assertEquals(null, r);
}
});
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class LuceneQueriesIntegrationTest method shouldReturnCorrectResultsOnDeletionAfterQueryExecutionWithLoader.
@Test
public void shouldReturnCorrectResultsOnDeletionAfterQueryExecutionWithLoader() throws Exception {
final int pageSize = 2;
final LuceneQuery<Object, Object> query = addValuesAndCreateQuery(pageSize);
region.getAttributesMutator().setCacheLoader(new CacheLoader() {
@Override
public Object load(final LoaderHelper helper) throws CacheLoaderException {
return new TestObject("should not", "load this");
}
@Override
public void close() {
}
});
final PageableLuceneQueryResults<Object, Object> pages = query.findPages();
List<LuceneResultStruct<Object, Object>> allEntries = new ArrayList<>();
assertTrue(pages.hasNext());
assertEquals(7, pages.size());
// Destroying an entry from the region after the query is executed.
region.destroy("C");
final List<LuceneResultStruct<Object, Object>> page1 = pages.next();
assertEquals(pageSize, page1.size());
final List<LuceneResultStruct<Object, Object>> page2 = pages.next();
assertEquals(pageSize, page2.size());
final List<LuceneResultStruct<Object, Object>> page3 = pages.next();
assertEquals(pageSize, page3.size());
assertFalse(pages.hasNext());
allEntries.addAll(page1);
allEntries.addAll(page2);
allEntries.addAll(page3);
assertEquals(region.keySet(), allEntries.stream().map(entry -> entry.getKey()).collect(Collectors.toSet()));
assertEquals(region.values(), allEntries.stream().map(entry -> entry.getValue()).collect(Collectors.toSet()));
}
Aggregations