use of org.apache.geode.cache.CacheLoaderException in project geode by apache.
the class CacheLoaderTestCase method testCacheLoader.
/////////////////////// Test Methods ///////////////////////
@Test
public void testCacheLoader() throws CacheException {
final String name = this.getUniqueName();
final Object key = this.getUniqueName();
final Object value = new Integer(42);
final Object arg = "ARG";
final String exception = "EXCEPTION";
TestCacheLoader loader = new TestCacheLoader() {
public Object load2(LoaderHelper helper) throws CacheLoaderException {
assertEquals(key, helper.getKey());
assertEquals(name, helper.getRegion().getName());
try {
RegionAttributes attrs = helper.getRegion().getAttributes();
if (attrs.getScope().isDistributed()) {
assertNull(helper.netSearch(false));
assertNull(helper.netSearch(true));
}
} catch (TimeoutException ex) {
Assert.fail("Why did I time out?", ex);
}
Object argument = helper.getArgument();
if (argument != null) {
if (argument.equals(exception)) {
String s = "Test Exception";
throw new CacheLoaderException(s);
} else {
assertEquals(arg, argument);
}
}
return value;
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setCacheLoader(loader);
Region region = createRegion(name, factory.create());
loader.wasInvoked();
Region.Entry entry = region.getEntry(key);
assertNull(entry);
region.create(key, null);
entry = region.getEntry(key);
assertNotNull(entry);
assertNull(entry.getValue());
assertEquals(value, region.get(key));
assertTrue(loader.wasInvoked());
assertEquals(value, region.getEntry(key).getValue());
}
use of org.apache.geode.cache.CacheLoaderException 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.CacheLoaderException in project geode by apache.
the class SearchLoadAndWriteProcessor method doNetLoad.
Object doNetLoad() throws CacheLoaderException, TimeoutException {
if (this.netLoadDone)
return null;
this.netLoadDone = true;
if (advisor != null) {
Set loadCandidatesSet = advisor.adviseNetLoad();
if (loadCandidatesSet.isEmpty()) {
return null;
}
CachePerfStats stats = region.getCachePerfStats();
long start = stats.startNetload();
ArrayList list = new ArrayList(loadCandidatesSet);
Collections.shuffle(list);
InternalDistributedMember[] loadCandidates = (InternalDistributedMember[]) list.toArray(new InternalDistributedMember[list.size()]);
initRemainingTimeout();
RegionAttributes attrs = region.getAttributes();
int index = 0;
// never set to true!
boolean stayInLoop = false;
this.remoteLoadInProgress = true;
try {
do {
// the only time this loop repeats is when continue is called
InternalDistributedMember next = loadCandidates[index++];
setSelectedNode(next);
this.lastNotifySpot = 0;
this.requestInProgress = true;
if (this.remainingTimeout <= 0) {
// @todo this looks wrong; why not a timeout exception?
break;
}
this.remoteException = null;
NetLoadRequestMessage.sendMessage(this, this.regionName, this.key, this.aCallbackArgument, next, this.remainingTimeout, attrs.getEntryTimeToLive().getTimeout(), attrs.getEntryIdleTimeout().getTimeout());
waitForObject2(this.remainingTimeout);
if (this.remoteException == null) {
if (!this.requestInProgress) {
// note even if result is null we are done; see bug 39738
this.localLoad = false;
if (this.result != null) {
this.netLoad = true;
}
return this.result;
} else {
// even if we don't have a result yet and have not tried everyone.
if ((this.selectedNodeDead) && (index < loadCandidates.length)) {
continue;
}
// otherwise we are done
}
} else {
Throwable cause;
if (this.remoteException instanceof TryAgainException) {
if (index < loadCandidates.length) {
continue;
} else {
break;
}
}
if (this.remoteException instanceof CacheLoaderException) {
cause = this.remoteException.getCause();
} else {
cause = this.remoteException;
}
throw new CacheLoaderException(LocalizedStrings.SearchLoadAndWriteProcessor_WHILE_INVOKING_A_REMOTE_NETLOAD_0.toLocalizedString(cause), cause);
}
} while (stayInLoop);
} finally {
stats.endNetload(start);
}
}
return null;
}
use of org.apache.geode.cache.CacheLoaderException in project geode by apache.
the class TXJUnitTest method testJTAEnlistment.
@Test
public void testJTAEnlistment() throws CacheException, javax.transaction.NotSupportedException, javax.transaction.RollbackException, javax.transaction.SystemException, javax.transaction.HeuristicMixedException, javax.transaction.HeuristicRollbackException {
TransactionListener tl = new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
++listenerAfterCommit;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
++listenerAfterFailedCommit;
te = event;
}
@Override
public void afterRollback(TransactionEvent event) {
++listenerAfterRollback;
te = event;
}
@Override
public void close() {
++listenerClose;
}
};
this.txMgr.addListener(tl);
javax.transaction.UserTransaction userTx = null;
try {
userTx = (javax.transaction.UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable badDog) {
fail("Expected to get a healthy UserTransaction!");
}
// Test enlistment for put
// Test enlisted rollback
// Test prevention of rollback/commit for enlisted transaction
assertEquals(0, this.listenerAfterRollback);
userTx.begin();
this.region.put("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertNotNull(this.txMgr.getTransactionId());
try {
this.txMgr.rollback();
fail("Should not allow a CacheTransactionManager.rollback call once the GF Tx is enlisted");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable ok) {
}
try {
this.txMgr.commit();
fail("Should not allow a CacheTransactionManager.commit() call once the GF Tx is enlisted");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable alsoOk) {
}
userTx.rollback();
assertNull(this.txMgr.getTransactionId());
assertTrue(!this.region.containsKey("enlistKey"));
assertEquals(1, this.listenerAfterRollback);
// Test enlistment for create
// Test commit
assertEquals(0, this.listenerAfterCommit);
userTx.begin();
this.region.create("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertEquals("enlistVal", this.region.getEntry("enlistKey").getValue());
assertEquals(1, this.listenerAfterCommit);
// Test enlistment for get
assertEquals(1, this.listenerAfterCommit);
userTx.begin();
assertEquals("enlistVal", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertEquals(2, this.listenerAfterCommit);
// Test enlistment for invalidate
assertEquals(2, this.listenerAfterCommit);
userTx.begin();
this.region.invalidate("enlistKey");
assertTrue(this.region.containsKey("enlistKey"));
assertTrue(!this.region.containsValueForKey("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertTrue(!this.region.containsValueForKey("enlistKey"));
assertEquals(3, this.listenerAfterCommit);
// Test enlistment for destroy
assertEquals(3, this.listenerAfterCommit);
userTx.begin();
this.region.destroy("enlistKey");
assertTrue(!this.region.containsKey("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(!this.region.containsKey("enlistKey"));
assertEquals(4, this.listenerAfterCommit);
// Test enlistment for load
AttributesMutator<String, String> mutator = this.region.getAttributesMutator();
mutator.setCacheLoader(new CacheLoader<String, String>() {
int count = 0;
@Override
public String load(LoaderHelper helper) throws CacheLoaderException {
return String.valueOf(count++);
}
@Override
public void close() {
}
});
assertEquals(4, this.listenerAfterCommit);
userTx.begin();
assertEquals("0", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
userTx.commit();
assertNull(this.txMgr.getTransactionId());
assertTrue(this.region.containsKey("enlistKey"));
assertEquals("0", this.region.getEntry("enlistKey").getValue());
assertEquals(5, this.listenerAfterCommit);
mutator.setCacheLoader(null);
// Test enlisted failed commit
assertEquals(0, this.listenerAfterFailedCommit);
userTx.begin();
this.region.put("enlistKey", "enlistVal");
assertEquals("enlistVal", this.region.get("enlistKey"));
assertNotNull(this.txMgr.getTransactionId());
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
TXStateProxy gfTx = gfTxMgrImpl.internalSuspend();
javax.transaction.TransactionManager jtaTxMgr = this.cache.getJTATransactionManager();
javax.transaction.Transaction jtaTx = jtaTxMgr.suspend();
this.region.put("enlistKey", "conflictVal");
assertEquals("conflictVal", this.region.get("enlistKey"));
try {
jtaTxMgr.resume(jtaTx);
} catch (Exception failure) {
fail("JTA resume failed");
}
gfTxMgrImpl.internalResume(gfTx);
}
assertEquals("enlistVal", this.region.get("enlistKey"));
try {
userTx.commit();
fail("Expected JTA commit exception!");
} catch (javax.transaction.HeuristicRollbackException expected) {
} catch (javax.transaction.RollbackException alsoExpected) {
} catch (Exception yuk) {
fail("Did not expect this exception from JTA commit: " + yuk);
}
assertNull(this.txMgr.getTransactionId());
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
assertEquals(1, this.listenerAfterFailedCommit);
// Test rollbackOnly UserTransaction enlistment
userTx.begin();
assertNull(this.txMgr.getTransactionId());
userTx.setRollbackOnly();
assertEquals(javax.transaction.Status.STATUS_MARKED_ROLLBACK, userTx.getStatus());
try {
this.region.put("enlistKey", "enlistVal2");
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertNull(this.txMgr.getTransactionId());
try {
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertTrue(!this.region.containsKey("enlistKey2"));
try {
this.region.put("enlistKey2", "enlistVal3");
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertNull(this.txMgr.getTransactionId());
try {
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
fail("Expected to get a FailedSynchronizationException!");
} catch (FailedSynchronizationException okay) {
}
assertTrue(!this.region.containsKey("enlistKey2"));
userTx.rollback();
assertEquals("conflictVal", this.region.getEntry("enlistKey").getValue());
assertTrue(!this.region.containsKey("enlistKey2"));
this.txMgr.removeListener(tl);
}
use of org.apache.geode.cache.CacheLoaderException in project geode by apache.
the class DiskRegionDUnitTest method testLRUCCSizeOne.
/**
* Tests a disk-based region with an {@link LRUCapacityController} with size 1 and an eviction
* action of "overflow".
*/
@Test
public void testLRUCCSizeOne() throws CacheException {
int threshold = 1;
final String name = this.getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold, EvictionAction.OVERFLOW_TO_DISK));
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return "LOADED VALUE";
}
public void close() {
}
});
DiskStoreFactory dsf = getCache().createDiskStoreFactory();
factory.setDiskSynchronous(true);
File d = new File("DiskRegions" + OSProcess.getId());
d.mkdirs();
dsf.setDiskDirs(new File[] { d });
DiskStore ds = dsf.create(name);
factory.setDiskStoreName(ds.getName());
Region region = createRegion(name, factory.create());
LRUStatistics lruStats = getLRUStats(region);
assertNotNull(lruStats);
for (int i = 1; i <= 1; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(0, lruStats.getEvictions());
}
for (int i = 2; i <= 10; i++) {
Object key = new Integer(i);
Object value = String.valueOf(i);
region.put(key, value);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
for (int i = 11; i <= 20; i++) {
Object key = new Integer(i);
// Object value = String.valueOf(i);
// Invoke loader
region.get(key);
assertEquals(1, lruStats.getCounter());
assertEquals(i - 1, lruStats.getEvictions());
}
}
Aggregations