use of org.apache.geode.cache.TransactionId in project geode by apache.
the class TXManagerImpl method internalResume.
/**
* Activates the specified transaction on the calling thread.
*
* @param tx the transaction to activate.
* @throws IllegalStateException if this thread already has an active transaction
*/
public void internalResume(TXStateProxy tx) {
if (tx != null) {
TransactionId tid = getTransactionId();
if (tid != null) {
throw new java.lang.IllegalStateException(LocalizedStrings.TXManagerImpl_TRANSACTION_0_ALREADY_IN_PROGRESS.toLocalizedString(tid));
}
setTXState(tx);
tx.resume();
}
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class TXManagerImpl method getTransactionId.
/**
* Gets the current transaction identifier or null if no transaction exists
*
*/
public TransactionId getTransactionId() {
TXStateProxy t = getTXState();
TransactionId ret = null;
if (t != null) {
ret = t.getTransactionId();
}
return ret;
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class TXManagerImpl method suspend.
TransactionId suspend(TimeUnit expiryTimeUnit) {
TXStateProxy result = getTXState();
if (result != null) {
TransactionId txId = result.getTransactionId();
internalSuspend();
this.suspendedTXs.put(txId, result);
// wake up waiting threads
Queue<Thread> waitingThreads = this.waitMap.get(txId);
if (waitingThreads != null) {
Thread waitingThread = null;
while (true) {
waitingThread = waitingThreads.poll();
if (waitingThread == null || !Thread.currentThread().equals(waitingThread)) {
break;
}
}
if (waitingThread != null) {
LockSupport.unpark(waitingThread);
}
}
scheduleExpiry(txId, expiryTimeUnit);
return txId;
}
return null;
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class TXJUnitTest method testPublicSuspendResume.
@Test
public void testPublicSuspendResume() {
CacheTransactionManager txMgr = this.txMgr;
assertTrue(!this.txMgr.exists());
assertEquals(null, txMgr.suspend());
TransactionId txId = null;
try {
txMgr.resume(txId);
fail("expected IllegalStateException");
} catch (IllegalStateException e) {
}
assertTrue(!this.txMgr.exists());
this.txMgr.begin();
TransactionId origId = this.txMgr.getTransactionId();
assertTrue(this.txMgr.exists());
{
TransactionId tx = txMgr.suspend();
assertTrue(!this.txMgr.exists());
this.txMgr.begin();
try {
txMgr.resume(tx);
fail("expected IllegalStateException");
} catch (IllegalStateException expected) {
}
this.txMgr.rollback();
assertTrue(!this.txMgr.exists());
txMgr.resume(tx);
}
assertTrue(this.txMgr.exists());
assertEquals(origId, this.txMgr.getTransactionId());
this.txMgr.rollback();
}
use of org.apache.geode.cache.TransactionId in project geode by apache.
the class MultiVMRegionTestCase method testTXSimpleOps.
/**
* Tests that an entry update is propagated to other caches that have that same entry defined.
*/
@Test
public void testTXSimpleOps() throws Exception {
assumeTrue(supportsTransactions());
assertTrue(getRegionAttributes().getScope().isDistributed());
CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
if (getRegionAttributes().getScope().isGlobal() || getRegionAttributes().getDataPolicy().withPersistence()) {
// just make sure transactions are not allowed on global or shared regions
Region rgn = createRegion(getUniqueName());
txMgr.begin();
try {
rgn.put("testTXSimpleOpsKey1", "val");
fail("expected UnsupportedOperationException");
} catch (UnsupportedOperationException ok) {
}
txMgr.rollback();
rgn.localDestroyRegion();
return;
}
final String rgnName = getUniqueName();
SerializableRunnable create = new SerializableRunnable("testTXSimpleOps: Create Region") {
@Override
public void run() {
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = new MyTransactionListener();
txMgr2.addListener(tl);
assertEquals(null, tl.lastEvent);
assertEquals(0, tl.afterCommitCount);
assertEquals(0, tl.afterFailedCommitCount);
assertEquals(0, tl.afterRollbackCount);
assertEquals(0, tl.closeCount);
try {
Region rgn = createRegion(rgnName);
CountingDistCacheListener cacheListener = new CountingDistCacheListener();
rgn.getAttributesMutator().addCacheListener(cacheListener);
cacheListener.assertCount(0, 0, 0, 0);
getSystem().getLogWriter().info("testTXSimpleOps: Created region");
} catch (CacheException e) {
fail("While creating region", e);
}
}
};
SerializableRunnable newKey = new SerializableRunnable("testTXSimpleOps: Create Region & Create Key") {
@Override
public void run() {
try {
Region root = getRootRegion();
Region rgn = root.getSubregion(rgnName);
rgn.create("key", null);
CountingDistCacheListener cacheListener = (CountingDistCacheListener) rgn.getAttributes().getCacheListener();
cacheListener.assertCount(0, 0, 0, 0);
getSystem().getLogWriter().info("testTXSimpleOps: Created Key");
} catch (CacheException e) {
fail("While creating region", e);
}
}
};
Host host = Host.getHost(0);
VM vm = host.getVM(0);
vm.invoke(create);
vm.invoke(newKey);
int vmCount = host.getVMCount();
for (int i = 1; i < vmCount; i++) {
vm = host.getVM(i);
vm.invoke(create);
if (!getRegionAttributes().getDataPolicy().withReplication() && !getRegionAttributes().getDataPolicy().isPreloaded()) {
vm.invoke(newKey);
}
}
try {
Region rgn = createRegion(rgnName);
DMStats dmStats = getSystem().getDistributionManager().getStats();
long cmtMsgs = dmStats.getSentCommitMessages();
long commitWaits = dmStats.getCommitWaits();
txMgr.begin();
rgn.put("key", "value");
TransactionId txId = txMgr.getTransactionId();
txMgr.commit();
assertEquals(cmtMsgs + 1, dmStats.getSentCommitMessages());
if (rgn.getAttributes().getScope().isAck()) {
assertEquals(commitWaits + 1, dmStats.getCommitWaits());
} else {
assertEquals(commitWaits, dmStats.getCommitWaits());
}
getSystem().getLogWriter().info("testTXSimpleOps: Create/Put Value");
Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", null, "value" });
Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {
@Override
public void run2() {
Region rgn1 = getRootRegion().getSubregion(rgnName);
assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
assertEquals("value", rgn1.getEntry("key").getValue());
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
tl.checkAfterCommitCount(1);
assertEquals(0, tl.afterFailedCommitCount);
assertEquals(0, tl.afterRollbackCount);
assertEquals(0, tl.closeCount);
assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
{
Collection events;
RegionAttributes attr = getRegionAttributes();
if (!attr.getDataPolicy().withReplication() || attr.getConcurrencyChecksEnabled()) {
events = tl.lastEvent.getPutEvents();
} else {
events = tl.lastEvent.getCreateEvents();
}
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(tl.expectedTxId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn1);
assertEquals("key", ev.getKey());
assertEquals("value", ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
cdcL.assertCount(0, 1, 0, 0);
}
}, getRepeatTimeoutMs());
txMgr.begin();
rgn.put("key", "value2");
txId = txMgr.getTransactionId();
txMgr.commit();
getSystem().getLogWriter().info("testTXSimpleOps: Put(update) Value2");
Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", "value", "value2" });
Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {
@Override
public void run2() {
Region rgn1 = getRootRegion().getSubregion(rgnName);
assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
assertEquals("value2", rgn1.getEntry("key").getValue());
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
tl.checkAfterCommitCount(2);
assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
{
Collection events = tl.lastEvent.getPutEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(tl.expectedTxId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn1);
assertEquals("key", ev.getKey());
assertEquals("value2", ev.getNewValue());
assertEquals("value", ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
cdcL.assertCount(0, 2, 0, 0);
}
}, getRepeatTimeoutMs());
txMgr.begin();
rgn.invalidate("key");
txId = txMgr.getTransactionId();
txMgr.commit();
getSystem().getLogWriter().info("testTXSimpleOps: invalidate key");
// validate each of the CacheListeners EntryEvents
Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", "value2", null });
Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {
@Override
public void run2() {
Region rgn1 = getRootRegion().getSubregion(rgnName);
assertNotNull("Could not find entry for 'key'", rgn1.getEntry("key"));
assertTrue(rgn1.containsKey("key"));
assertTrue(!rgn1.containsValueForKey("key"));
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
tl.checkAfterCommitCount(3);
assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
{
Collection events = tl.lastEvent.getInvalidateEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(tl.expectedTxId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn1);
assertEquals("key", ev.getKey());
assertEquals(null, ev.getNewValue());
assertEquals("value2", ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
cdcL.assertCount(0, 2, 1, 0);
}
}, getRepeatTimeoutMs());
txMgr.begin();
rgn.destroy("key");
txId = txMgr.getTransactionId();
txMgr.commit();
getSystem().getLogWriter().info("testTXSimpleOps: destroy key");
// validate each of the CacheListeners EntryEvents
Invoke.invokeInEveryVM(MultiVMRegionTestCase.class, "assertCacheCallbackEvents", new Object[] { rgnName, txId, "key", null, null });
Invoke.invokeInEveryVMRepeatingIfNecessary(new CacheSerializableRunnable("testTXSimpleOps: Verify Received Value") {
@Override
public void run2() {
Region rgn1 = getRootRegion().getSubregion(rgnName);
assertTrue(!rgn1.containsKey("key"));
CacheTransactionManager txMgr2 = getCache().getCacheTransactionManager();
MyTransactionListener tl = (MyTransactionListener) txMgr2.getListeners()[0];
tl.checkAfterCommitCount(4);
assertEquals(rgn1.getCache(), tl.lastEvent.getCache());
{
Collection events = tl.lastEvent.getDestroyEvents();
assertEquals(1, events.size());
EntryEvent ev = (EntryEvent) events.iterator().next();
assertEquals(tl.expectedTxId, ev.getTransactionId());
assertTrue(ev.getRegion() == rgn1);
assertEquals("key", ev.getKey());
assertEquals(null, ev.getNewValue());
assertEquals(null, ev.getOldValue());
assertTrue(!ev.getOperation().isLocalLoad());
assertTrue(!ev.getOperation().isNetLoad());
assertTrue(!ev.getOperation().isLoad());
assertTrue(!ev.getOperation().isNetSearch());
assertTrue(!ev.getOperation().isExpiration());
assertEquals(null, ev.getCallbackArgument());
assertEquals(true, ev.isCallbackArgumentAvailable());
assertTrue(ev.isOriginRemote());
assertTrue(ev.getOperation().isDistributed());
}
CountingDistCacheListener cdcL = (CountingDistCacheListener) rgn1.getAttributes().getCacheListeners()[0];
cdcL.assertCount(0, 2, 1, 1);
}
}, getRepeatTimeoutMs());
} catch (Exception e) {
CacheFactory.getInstance(getSystem()).close();
getSystem().getLogWriter().fine("testTXSimpleOps: Caused exception in createRegion");
throw e;
}
}
Aggregations