use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class CacheLoaderTestCase method testCacheListenerOnLoad.
/**
* Tests that a <code>CacheListener</code> gets invoked on a <code>load</code>.
*/
@Test
public void testCacheListenerOnLoad() throws CacheException, InterruptedException {
final String name = this.getUniqueName();
final Object key = this.getUniqueName();
final Object oldValue = new Integer(42);
final Object newValue = new Integer(43);
TestCacheLoader loader = new TestCacheLoader() {
public Object load2(LoaderHelper helper) throws CacheLoaderException {
return oldValue;
}
};
TestCacheListener listener = new TestCacheListener() {
public void afterCreate2(EntryEvent event) {
assertEquals(oldValue, event.getNewValue());
assertTrue(event.getOperation().isLoad());
assertTrue(event.getOperation().isLocalLoad());
assertFalse(event.getOperation().isNetLoad());
assertFalse(event.getOperation().isNetSearch());
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setCacheLoader(loader);
factory.setCacheListener(listener);
Region region = createRegion(name, factory.create());
loader.wasInvoked();
assertEquals(oldValue, region.get(key));
assertTrue(loader.wasInvoked());
Wait.pause(500);
assertTrue(listener.wasInvoked());
listener = new TestCacheListener() {
public void afterUpdate2(EntryEvent event) {
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().setCacheListener(listener);
region.put(key, newValue);
Wait.pause(500);
assertFalse(loader.wasInvoked());
assertTrue(listener.wasInvoked());
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class LocalRegion method findObjectInSystem.
/**
*
* Search for the value in a server (if one exists), then try a loader.
*
* If we find a value, we put it in the cache.
*
* @param preferCD return the CacheDeserializable, if that's what the value is.
* @param requestingClient the client making the request, if any
* @param clientEvent the client's event, if any. If not null, we set the version tag
* @return the deserialized value
*/
protected Object findObjectInSystem(KeyInfo keyInfo, boolean isCreate, TXStateInterface tx, boolean generateCallbacks, Object localValue, boolean disableCopyOnRead, boolean preferCD, ClientProxyMembershipID requestingClient, EntryEventImpl clientEvent, boolean returnTombstones) throws CacheLoaderException, TimeoutException {
final Object key = keyInfo.getKey();
final Object aCallbackArgument = keyInfo.getCallbackArg();
Object value = null;
boolean fromServer = false;
VersionTagHolder holder = null;
/*
* First lets try the server
*/
ServerRegionProxy mySRP = getServerProxy();
if (mySRP != null) {
holder = new VersionTagHolder();
value = mySRP.get(key, aCallbackArgument, holder);
fromServer = value != null;
}
/*
* If we didn't get anything from the server, try the loader
*/
if (!fromServer || value == Token.TOMBSTONE) {
// copy into local var to prevent race condition
CacheLoader loader = basicGetLoader();
if (loader != null) {
final LoaderHelper loaderHelper = this.loaderHelperFactory.createLoaderHelper(key, aCallbackArgument, false, /* netSearchAllowed */
true, /* netloadAllowed */
null);
CachePerfStats stats = getCachePerfStats();
long statStart = stats.startLoad();
try {
value = loader.load(loaderHelper);
fromServer = false;
} finally {
stats.endLoad(statStart);
}
}
}
// have concurrency checks enabled
if (fromServer && value == Token.TOMBSTONE && !this.concurrencyChecksEnabled) {
value = null;
}
/*
* If we got a value back, let's put it in the cache.
*/
RegionEntry re = null;
if (value != null && !isMemoryThresholdReachedForLoad()) {
long startPut = CachePerfStats.getStatTime();
validateKey(key);
Operation op;
if (isCreate) {
op = Operation.LOCAL_LOAD_CREATE;
} else {
op = Operation.LOCAL_LOAD_UPDATE;
}
@Released EntryEventImpl event = EntryEventImpl.create(this, op, key, value, aCallbackArgument, false, getMyId(), generateCallbacks);
try {
// already one there with the same version
if (fromServer) {
if (alreadyInvalid(key, event)) {
return null;
}
event.setFromServer(fromServer);
event.setVersionTag(holder.getVersionTag());
if (clientEvent != null) {
clientEvent.setVersionTag(holder.getVersionTag());
}
}
// the value to the server
if (!fromServer) {
event.setNewEventId(this.cache.getDistributedSystem());
}
try {
try {
re = basicPutEntry(event, 0L);
if (!fromServer && clientEvent != null) {
clientEvent.setVersionTag(event.getVersionTag());
clientEvent.isConcurrencyConflict(event.isConcurrencyConflict());
}
if (fromServer && event.getRawNewValue() == Token.TOMBSTONE) {
// tombstones are destroyed entries
return null;
}
} catch (ConcurrentCacheModificationException ignore) {
// this means the value attempted to overwrite a newer modification and was rejected
if (logger.isDebugEnabled()) {
logger.debug("caught concurrent modification attempt when applying {}", event);
}
notifyBridgeClients(event);
}
if (!getDataView().isDeferredStats()) {
getCachePerfStats().endPut(startPut, event.isOriginRemote());
}
} catch (CacheWriterException cwe) {
if (logger.isDebugEnabled()) {
logger.debug("findObjectInSystem: writer exception putting entry {}", event, cwe);
}
}
} finally {
event.release();
}
}
if (isCreate) {
recordMiss(re, key);
}
return value;
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class ProxyDUnitTest method remoteOriginOps.
/**
* check remote ops done in a normal vm are correctly distributed to PROXY regions
*/
private void remoteOriginOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
initOtherId();
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(dp);
af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
af.setScope(Scope.DISTRIBUTED_ACK);
CacheListener cl1 = new CacheListener() {
public void afterUpdate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterCreate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterInvalidate(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterDestroy(EntryEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionInvalidate(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionDestroy(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionClear(RegionEvent e) {
clLastEvent = e;
clInvokeCount++;
}
public void afterRegionCreate(RegionEvent e) {
}
public void afterRegionLive(RegionEvent e) {
}
public void close() {
}
};
af.addCacheListener(cl1);
Region r = createRootRegion("ProxyDUnitTest", af.create());
this.clInvokeCount = 0;
doCreateOtherVm();
DMStats stats = getDMStats();
long receivedMsgs = stats.getReceivedMessages();
if (ip.isAll()) {
getOtherVm().invoke(new CacheSerializableRunnable("do put") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("p", "v");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
// failure
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("p", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do create") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.create("c", "v");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do update") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("c", "v2");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.UPDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals("v2", ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do invalidate") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.invalidate("c");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.INVALIDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do destroy") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.destroy("c");
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.DESTROY, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
assertEquals(null, ((EntryEvent) this.clLastEvent).getNewValue());
assertEquals("c", ((EntryEvent) this.clLastEvent).getKey());
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do putAll") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
Map m = new HashMap();
m.put("putAllKey1", "putAllValue1");
m.put("putAllKey2", "putAllValue2");
r.putAll(m);
}
});
assertEquals(2, this.clInvokeCount);
// @todo darrel; check putAll events
this.clInvokeCount = 0;
getOtherVm().invoke(new CacheSerializableRunnable("do netsearch") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
// total miss
assertEquals(null, r.get("loadkey"));
}
});
assertEquals(0, this.clInvokeCount);
} else {
getOtherVm().invoke(new CacheSerializableRunnable("do entry ops") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.put("p", "v");
r.create("c", "v");
// update
r.put("c", "v");
r.invalidate("c");
r.destroy("c");
{
Map m = new HashMap();
m.put("putAllKey1", "putAllValue1");
m.put("putAllKey2", "putAllValue2");
r.putAll(m);
}
// total miss
assertEquals(null, r.get("loadkey"));
}
});
assertEquals(0, this.clInvokeCount);
assertEquals(0, r.size());
// check the stats to make sure none of the above sent up messages
assertEquals(receivedMsgs, stats.getReceivedMessages());
}
{
AttributesMutator am = r.getAttributesMutator();
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);
}
receivedMsgs = stats.getReceivedMessages();
getOtherVm().invoke(new CacheSerializableRunnable("check net loader") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
// net load
assertEquals("loadvalue", r.get("loadkey"));
// total miss
assertEquals(null, r.get("foobar"));
try {
r.get("loadexception");
fail("expected CacheLoaderException");
} catch (CacheLoaderException expected) {
}
}
});
assertTrue(stats.getReceivedMessages() > receivedMsgs);
if (ip.isAll()) {
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.NET_LOAD_CREATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertEquals(null, ((EntryEvent) this.clLastEvent).getOldValue());
assertEquals(false, ((EntryEvent) this.clLastEvent).isOldValueAvailable());
this.clInvokeCount = 0;
} else {
assertEquals(0, this.clInvokeCount);
}
{
AttributesMutator am = r.getAttributesMutator();
am.setCacheLoader(null);
CacheWriter cw = new CacheWriterAdapter() {
public void beforeCreate(EntryEvent event) throws CacheWriterException {
throw new CacheWriterException("expected");
}
};
am.setCacheWriter(cw);
}
receivedMsgs = stats.getReceivedMessages();
getOtherVm().invoke(new CacheSerializableRunnable("check net write") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
try {
r.put("putkey", "putvalue");
fail("expected CacheWriterException");
} catch (CacheWriterException expected) {
}
}
});
assertTrue(stats.getReceivedMessages() > receivedMsgs);
{
AttributesMutator am = r.getAttributesMutator();
am.setCacheWriter(null);
}
assertEquals(0, this.clInvokeCount);
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region invalidate") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.invalidateRegion();
}
});
assertEquals(1, this.clInvokeCount);
assertEquals(Operation.REGION_INVALIDATE, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region clear") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.clear();
}
});
assertEquals(2, this.clInvokeCount);
assertEquals(Operation.REGION_CLEAR, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
this.clLastEvent = null;
getOtherVm().invoke(new CacheSerializableRunnable("check region destroy") {
public void run2() throws CacheException {
Region r = getRootRegion("ProxyDUnitTest");
r.destroyRegion();
}
});
assertEquals(3, this.clInvokeCount);
assertEquals(Operation.REGION_DESTROY, this.clLastEvent.getOperation());
assertEquals(true, this.clLastEvent.isOriginRemote());
assertEquals(this.otherId, this.clLastEvent.getDistributedMember());
assertTrue(r.isDestroyed());
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class RegionReliabilityTestCase method testNoAccess.
// -------------------------------------------------------------------------
// Tests to be run under every permutation of config options
// Valid configurations include scope D-ACK, D-NOACK, GLOBAL
// -------------------------------------------------------------------------
/**
* Tests affect of NO_ACCESS on region operations.
*/
@Test
public void testNoAccess() throws Exception {
final String name = this.getUniqueName();
final String roleA = name + "-A";
// assign names to 4 vms...
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);
getCache();
// 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.setStatisticsEnabled(true);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// use vm0 for netsearch and netload
Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createConnection(null);
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
fac.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
if ("netload".equals(helper.getKey())) {
return "netload";
} else {
return null;
}
}
public void close() {
}
});
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
Object netsearch = "netsearch";
region.put(netsearch, netsearch);
}
});
// test ops on Region that should throw
assertNoAccessThrows(region);
// 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);
}
});
Role role = (Role) requiredRolesSet.iterator().next();
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
// retest ops on Region to assert no longer throw
assertNoAccessDoesNotThrow(region);
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class RegionReliabilityTestCase method testLimitedAccess.
/**
* Tests affect of LIMITED_ACCESS on region operations.
*/
@Test
public void testLimitedAccess() throws Exception {
final String name = this.getUniqueName();
final String roleA = name + "-A";
// assign names to 4 vms...
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);
getCache();
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(getRegionScope());
fac.setStatisticsEnabled(true);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// use vm0 for netsearch and netload
Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
createConnection(null);
AttributesFactory fac = new AttributesFactory();
fac.setScope(getRegionScope());
fac.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
if ("netload".equals(helper.getKey())) {
return "netload";
} else {
return null;
}
}
public void close() {
}
});
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
Object netsearch = "netsearch";
region.put(netsearch, netsearch);
}
});
// test ops on Region that should throw
assertLimitedAccessThrows(region);
// this query should not throw
QueryService qs = region.getCache().getQueryService();
Query query = qs.newQuery("(select distinct * from " + region.getFullPath() + ").size");
query.execute();
// 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);
}
});
// retest ops on Region to assert no longer throw
assertLimitedAccessDoesNotThrow(region);
}
Aggregations