use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class RegionTestCase method testEntryFromLoadTtlInvalidate.
// /**
// * Expire an entry with a ttl time. Set a new ttl time, create the
// * same entry again, make sure it observes the <em>new</em> ttl time.
// * Do this many times.
// */
// public void testEntryTtl4() {
// if (!supportsExpiration()) {
// return;
// }
// final String name = this.getUniqueName();
// final int timeout1 = 200; // ms
// final int timeout2 = 2000;
// final String key1 = "KEY1";
// final String value1 = "VALUE1";
// final String value2 = "VALUE2";
//
// AttributesFactory factory = new AttributesFactory(getRegionAttributes());
// ExpirationAttributes expire1 =
// new ExpirationAttributes(timeout1, ExpirationAction.INVALIDATE);
// factory.setEntryTimeToLive(expire1);
// factory.setStatisticsEnabled(true);
// TestCacheListener list = new TestCacheListener() {
// public void afterCreate2(EntryEvent e) { }
// public void afterUpdate2(EntryEvent e) { }
// public void afterDestroy2(EntryEvent e) { }
// public void afterInvalidate2(EntryEvent e) { eventCount ++; }
// };
// eventCount = 0;
// factory.addCacheListener(list);
// RegionAttributes attrs = factory.create();
//
// Region region = null;
// System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY", "true");
// try {
// region = createRegion(name, attrs);
// } finally {
// System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
// }
//
// for (int i = 0; i < 10; i ++) {
// // DebuggerSupport.waitForJavaDebugger(getLogWriter(), "Set breakpoint in invalidate");
// ExpiryTask.suspendExpiration();
// Region.Entry entry = null;
// long tilt;
// try {
// region.create(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// assertTrue(list.waitForInvocation(1000));
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Do it again with a put (I guess)
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value1);
// tilt = System.currentTimeMillis() + timeout1;
// entry = region.getEntry(key1);
// Assert.assertTrue(value1.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// // Change custom expiry for this region now...
// AttributesMutator mutt = region.getAttributesMutator();
// ExpirationAttributes expire2 =
// new ExpirationAttributes(timeout2, ExpirationAction.INVALIDATE);
// mutt.setEntryTimeToLive(expire2);
//
// ExpiryTask.suspendExpiration();
// try {
// region.put(key1, value2);
// tilt = System.currentTimeMillis() + timeout2;
// entry = region.getEntry(key1);
// Assert.assertTrue(value2.equals(entry.getValue()));
// } finally {
// ExpiryTask.permitExpiration();
// }
// waitForInvalidate(entry, tilt);
// assertTrue(list.waitForInvocation(1000));
// assertTrue(eventCount == 1);
// eventCount = 0;
//
// region.destroy(key1);
// }
// }
/**
* Tests that an entry whose value is loaded into a region expires with an invalidation after a
* given time to live.
*/
@Test
public void testEntryFromLoadTtlInvalidate() throws CacheException, InterruptedException {
final String name = this.getUniqueName();
// ms!
final int timeout = 20;
final String key = "KEY";
final String value = "VALUE";
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
factory.setEntryTimeToLive(expire);
factory.setStatisticsEnabled(true);
factory.setCacheLoader(new TestCacheLoader() {
public Object load2(LoaderHelper helper) {
return value;
}
});
RegionAttributes attrs = factory.create();
Region region = null;
System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
try {
region = createRegion(name, attrs);
// DebuggerSupport.waitForJavaDebugger(getLogWriter(), "About to get");
ExpiryTask.suspendExpiration();
Region.Entry entry = null;
long tilt;
try {
region.get(key);
tilt = System.currentTimeMillis() + timeout;
entry = region.getEntry(key);
assertNotNull(entry.getValue());
} finally {
ExpiryTask.permitExpiration();
}
waitForInvalidate(entry, tilt);
} finally {
System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
}
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class SearchLoadAndWriteProcessor method doLocalLoad.
private Object doLocalLoad(CacheLoader loader, boolean netSearchAllowed) throws CacheLoaderException {
Object obj = null;
if (loader != null && !this.attemptedLocalLoad) {
this.attemptedLocalLoad = true;
CachePerfStats stats = region.getCachePerfStats();
LoaderHelper loaderHelper = this.region.loaderHelperFactory.createLoaderHelper(this.key, this.aCallbackArgument, netSearchAllowed, true, /* netLoadAllowed */
this);
long statStart = stats.startLoad();
try {
obj = loader.load(loaderHelper);
} finally {
stats.endLoad(statStart);
}
if (obj != null) {
this.localLoad = true;
}
}
return obj;
}
use of org.apache.geode.cache.LoaderHelper 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.LoaderHelper in project geode by apache.
the class TXDistributedDUnitTest method testDACKLoadedMessage.
/**
* Test distributed ack transactions that consist only of data from loaded values
*/
@Test
public void testDACKLoadedMessage() throws Exception {
final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
final String rgnName = getUniqueName();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) {
return "val" + helper.getArgument();
}
public void close() {
}
});
Region rgn = getCache().createRegion(rgnName, factory.create());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: intial configuration") {
public void run() {
try {
AttributesFactory factory2 = new AttributesFactory();
factory2.setScope(Scope.DISTRIBUTED_ACK);
factory2.setEarlyAck(false);
// factory.setDataPolicy(DataPolicy.REPLICATE);
factory2.setMirrorType(MirrorType.KEYS);
getCache().createRegion(rgnName, factory2.create());
} catch (CacheException e) {
Assert.fail("While creating region", e);
}
}
});
// Confirm the standard case
txMgr.begin();
rgn.put("key1", "val1");
txMgr.commit();
assertEquals("val1", rgn.getEntry("key1").getValue());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val1", rgn1.getEntry("key1").getValue());
}
});
// Confirm loaded value case
txMgr.begin();
rgn.get("key2", new Integer(2));
txMgr.commit();
assertEquals("val2", rgn.getEntry("key2").getValue());
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val2", rgn1.getEntry("key2").getValue());
}
});
// This should use the ack w/ the lockid
txMgr.begin();
rgn.put("key3", "val3");
rgn.get("key4", new Integer(4));
txMgr.commit();
Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {
public void run() {
Region rgn1 = getCache().getRegion(rgnName);
assertEquals("val3", rgn1.getEntry("key3").getValue());
assertEquals("val4", rgn1.getEntry("key4").getValue());
}
});
}
use of org.apache.geode.cache.LoaderHelper in project geode by apache.
the class SearchAndLoadDUnitTest method testNetLoad.
@Test
public void testNetLoad() throws CacheException, InterruptedException {
disconnectAllFromDS();
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final String name = this.getUniqueName() + "-ACK";
final String objectName = "B";
final Integer value = new Integer(43);
loaderInvoked = false;
remoteLoaderInvoked = false;
vm0.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
loaderInvoked = false;
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
// factory.setCacheLoader(new CacheLoader() {
// public Object load(LoaderHelper helper) {
/// loaderInvoked = true;
// return value;
// }
//
// public void close() {
//
// }
// });
Region region = createRegion(name, factory.create());
region.create(objectName, null);
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm1.invoke(new SerializableRunnable("Create ACK Region") {
public void run() {
try {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setEarlyAck(false);
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) {
remoteLoaderInvoked = true;
return value;
}
public void close() {
}
});
createRegion(name, factory.create());
} catch (CacheException ex) {
Assert.fail("While creating ACK region", ex);
}
}
});
vm0.invoke(new SerializableRunnable("Get a value from remote loader") {
public void run() {
for (int i = 0; i < 1; i++) {
try {
Object result = getRootRegion().getSubregion(name).get(objectName);
assertEquals(value, result);
assertEquals(new Boolean(loaderInvoked), Boolean.FALSE);
// getRootRegion().getSubregion(name).invalidate(objectName);
} catch (CacheLoaderException cle) {
Assert.fail("While getting value for ACK region", cle);
}/*
* catch(EntryNotFoundException enfe) { fail("While getting value for ACK region", enfe);
*
* }
*/
catch (TimeoutException te) {
Assert.fail("While getting value for ACK region", te);
}
}
}
});
}
Aggregations