use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class MultiVMRegionTestCase method testRemoteCacheLoader.
/**
* Tests that a {@link CacheLoader} is invoked in a remote VM. This essentially tests
* <code>netLoad</code>.
*/
@Test
public void testRemoteCacheLoader() throws Exception {
assumeTrue(supportsNetLoad());
assertTrue(getRegionAttributes().getScope().isDistributed());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
createRegion(name);
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
vm1.invoke(new CacheSerializableRunnable("Set CacheLoader") {
@Override
public void run2() throws CacheException {
final Region region = getRootRegion().getSubregion(name);
loader = new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
assertEquals(region, helper.getRegion());
assertEquals(key, helper.getKey());
assertNull(helper.getArgument());
return value;
}
};
region.getAttributesMutator().setCacheLoader(loader);
}
});
vm0.invoke(new CacheSerializableRunnable("Remote load") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertEquals(value, region.get(key));
}
});
vm1.invoke(new SerializableRunnable("Verify loader") {
@Override
public void run() {
assertTrue(loader.wasInvoked());
}
});
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class MultiVMRegionTestCase method testMirroredLocalLoad.
/**
* Tests that a local load occurs, even with mirroring
*/
@Test
public void testMirroredLocalLoad() throws Exception {
assumeTrue(supportsReplication());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
// use VMs on different gemfire systems
VM vm2 = host.getVM(2);
vm0.invoke(new CacheSerializableRunnable("Create region with loader") {
@Override
public void run2() throws CacheException {
RegionAttributes ra = getRegionAttributes();
AttributesFactory factory = new AttributesFactory(ra);
if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
factory.setDiskStoreName(null);
}
factory.setDataPolicy(DataPolicy.REPLICATE);
factory.setCacheLoader(new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
return value;
}
});
createRegion(name, factory.create());
}
});
SerializableRunnable create = new CacheSerializableRunnable("Create region with bad loader") {
@Override
public void run2() throws CacheException {
RegionAttributes ra = getRegionAttributes();
AttributesFactory factory = new AttributesFactory(ra);
if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
factory.setDiskStoreName(null);
}
factory.setDataPolicy(DataPolicy.REPLICATE);
loader = new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
fail("Should not be invoked");
return null;
}
};
factory.setCacheLoader(loader);
createRegion(name, factory.create());
}
};
vm2.invoke(create);
vm0.invoke(new CacheSerializableRunnable("Get") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertEquals(value, region.get(key));
}
});
vm2.invoke(new CacheSerializableRunnable("Verify no load") {
@Override
public void run2() throws CacheException {
assertFalse(loader.wasInvoked());
}
});
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class MultiVMRegionTestCase method testNoLoaderWithInvalidEntry.
/**
* Tests that a remote <code>CacheLoader</code> is not invoked if the remote region has an invalid
* entry (that is, a key, but no value).
*/
@Test
public void testNoLoaderWithInvalidEntry() throws Exception {
assumeTrue(supportsNetLoad());
final String name = this.getUniqueName();
final Object key = "KEY";
final Object value = "VALUE";
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
@Override
public void run2() throws CacheException {
Region region = createRegion(name);
loader = new TestCacheLoader() {
@Override
public Object load2(LoaderHelper helper) throws CacheLoaderException {
return value;
}
};
region.getAttributesMutator().setCacheLoader(loader);
}
};
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
vm0.invoke(create);
vm1.invoke(create);
vm1.invoke(new CacheSerializableRunnable("Create invalid entry") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.create(key, null);
}
});
vm0.invoke(new CacheSerializableRunnable("Remote get") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
assertEquals(value, region.get(key));
assertTrue(loader.wasInvoked());
}
});
vm1.invoke(new SerializableRunnable("Verify loader") {
@Override
public void run() {
assertFalse(loader.wasInvoked());
}
});
}
use of org.apache.geode.cache.LoaderHelper 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.LoaderHelper 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);
}
});
}
Aggregations