use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class RemoteTransactionDUnitTest method testTxRemove.
@Test
public void testTxRemove() {
Host host = Host.getHost(0);
VM acc = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(acc, datastore, 0);
VM accessor = getVMForTransactions(acc, datastore);
final CustId custId = new CustId(1);
final TXId txId = (TXId) accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region<CustId, Customer> cust = getGemfireCache().getRegion(CUSTOMER);
Region<CustId, Customer> ref = getGemfireCache().getRegion(D_REFERENCE);
TXManagerImpl mgr = getGemfireCache().getTxManager();
mgr.begin();
Customer customer = new Customer("customer1", "address1");
Customer fakeCust = new Customer("foo", "bar");
assertFalse(cust.remove(custId, fakeCust));
assertTrue(cust.remove(custId, customer));
assertFalse(ref.remove(custId, fakeCust));
assertTrue(ref.remove(custId, customer));
TXStateProxy tx = mgr.internalSuspend();
assertNotNull(cust.get(custId));
assertNotNull(ref.get(custId));
mgr.internalResume(tx);
return mgr.getTransactionId();
}
});
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
assertTrue(mgr.isHostedTxInProgress(txId));
TXStateProxy tx = mgr.getHostedTXState(txId);
// 2 buckets for the two puts we
assertEquals(2, tx.getRegions().size());
// different buckets
for (LocalRegion r : tx.getRegions()) {
assertTrue(r instanceof BucketRegion || r instanceof DistributedRegion);
TXRegionState rs = tx.readRegion(r);
for (Object key : rs.getEntryKeys()) {
TXEntryState es = rs.readEntry(key);
assertNull(es.getValue(key, r, false));
assertTrue("key:" + key + " r:" + r.getFullPath(), es.isDirty());
}
}
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
mgr.commit();
Region<CustId, Customer> cust = getGemfireCache().getRegion(CUSTOMER);
Region<CustId, Customer> rr = getGemfireCache().getRegion(D_REFERENCE);
assertNull(cust.get(custId));
assertNull(rr.get(custId));
// check conflict
mgr.begin();
CustId conflictCust = new CustId(2);
Customer customer = new Customer("customer2", "address2");
getGemfireCache().getLoggerI18n().fine("SWAP:removeConflict");
assertTrue(cust.remove(conflictCust, customer));
TXStateProxy tx = mgr.internalSuspend();
cust.put(conflictCust, new Customer("foo", "bar"));
mgr.internalResume(tx);
try {
mgr.commit();
fail("expected exception not thrown");
} catch (CommitConflictException e) {
}
return null;
}
});
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXLockServiceImpl method txLock.
@Override
public TXLockId txLock(List regionLockReqs, Set txParticipants) throws CommitConflictException {
if (regionLockReqs == null) {
throw new IllegalArgumentException(LocalizedStrings.TXLockServiceImpl_REGIONLOCKREQS_MUST_NOT_BE_NULL.toLocalizedString());
}
Set participants = txParticipants;
if (participants == null) {
participants = Collections.EMPTY_SET;
}
boolean gotLocks = false;
TXLockId txLockId = null;
try {
synchronized (this.txLockIdList) {
txLockId = new TXLockIdImpl(this.dlock.getDistributionManager().getId());
this.txLockIdList.add(txLockId);
}
TXLockBatch batch = new TXLockBatch(txLockId, regionLockReqs, participants);
logger.debug("[TXLockServiceImpl.txLock] acquire try-locks for {}", batch);
// TODO: get a readWriteLock to make the following block atomic...
Object[] keyIfFail = new Object[1];
gotLocks = this.dlock.acquireTryLocks(batch, TIMEOUT_MILLIS, LEASE_MILLIS, keyIfFail);
if (gotLocks) {
// ...otherwise race can occur between tryLocks and readLock
acquireRecoveryReadLock();
} else if (keyIfFail[0] != null) {
throw new CommitConflictException(LocalizedStrings.TXLockServiceImpl_CONCURRENT_TRANSACTION_COMMIT_DETECTED_0.toLocalizedString(keyIfFail[0]));
} else {
throw new CommitConflictException(LocalizedStrings.TXLockServiceImpl_FAILED_TO_REQUEST_TRY_LOCKS_FROM_GRANTOR_0.toLocalizedString(this.dlock.getLockGrantorId()));
}
logger.debug("[TXLockServiceImpl.txLock] gotLocks is {}, returning txLockId:{}", gotLocks, txLockId);
return txLockId;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.debug("[TXLockServiceImpl.txLock] was interrupted", e);
if (gotLocks) {
if (txLockId != null) {
synchronized (this.txLockIdList) {
this.txLockIdList.remove(txLockId);
}
}
gotLocks = false;
}
// TODO: change to be TransactionFailedException (after creating TFE)...
throw new CommitConflictException(LocalizedStrings.TXLockServiceImpl_CONCURRENT_TRANSACTION_COMMIT_DETECTED_BECAUSE_REQUEST_WAS_INTERRUPTED.toLocalizedString(), e);
}
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXJUnitTest method testRepeatableRead.
@Test
public void testRepeatableRead() throws CacheException {
final TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
TXStateProxy tx;
// try repeating a get and make sure it doesn't cause a conflict
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals("value1", this.region.get("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals("value1", this.region.get("key1"));
txMgrImpl.commit();
// try repeating a get and modify the entry and make sure it causes a conflict
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals("value1", this.region.get("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals("value1", this.region.get("key1"));
this.region.put("key1", "value3");
assertEquals("value3", this.region.get("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try repeating a getEntry and make sure it doesn't cause a conflict
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
this.region.getEntry("key1");
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals("value1", this.region.get("key1"));
txMgrImpl.commit();
// try repeating a getEntry and modify the entry and make sure it causes a conflict
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
this.region.getEntry("key1");
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
this.region.put("key1", "value3");
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try RR when entry fetched using entrySet
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
// bootstrap the tx, entrySet does not
this.region.get("key1");
this.region.entrySet(false).iterator().next();
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals("value1", this.region.get("key1"));
txMgrImpl.commit();
// try RRW->CONFLICT when entry fetched using entrySet
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
// bootstrap the tx, entrySet does not
this.region.get("key1");
this.region.entrySet(false).iterator().next();
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals("value1", this.region.get("key1"));
this.region.put("key1", "value3");
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try containsKey
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals(true, this.region.containsKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsKey("key1"));
txMgrImpl.commit();
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals(true, this.region.containsKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsKey("key1"));
this.region.put("key1", "value3");
assertEquals(true, this.region.containsKey("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try containsValueForKey
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals(true, this.region.containsValueForKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsValueForKey("key1"));
txMgrImpl.commit();
// non-tx
this.region.put("key1", "value1");
txMgrImpl.begin();
assertEquals(true, this.region.containsValueForKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsValueForKey("key1"));
this.region.put("key1", "value3");
assertEquals(true, this.region.containsValueForKey("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// now try the same things but with no entry in committed state at
// the time of the first read
// try repeating a get and make sure it doesn't cause a conflict
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(null, this.region.get("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(null, this.region.get("key1"));
txMgrImpl.commit();
// try repeating a get and modify the entry and make sure it causes a conflict
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(null, this.region.get("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(null, this.region.get("key1"));
this.region.put("key1", "value3");
assertEquals("value3", this.region.get("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try repeating a getEntry and make sure it doesn't cause a conflict
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(null, this.region.getEntry("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(null, this.region.getEntry("key1"));
txMgrImpl.commit();
// try repeating a getEntry and modify the entry and make sure it causes a conflict
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(null, this.region.getEntry("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(null, this.region.getEntry("key1"));
this.region.put("key1", "value3");
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try containsKey
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(false, this.region.containsKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(false, this.region.containsKey("key1"));
txMgrImpl.commit();
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(false, this.region.containsKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(false, this.region.containsKey("key1"));
this.region.put("key1", "value3");
assertEquals(true, this.region.containsKey("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try containsValueForKey
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(false, this.region.containsValueForKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(false, this.region.containsValueForKey("key1"));
txMgrImpl.commit();
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
assertEquals(false, this.region.containsValueForKey("key1"));
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.put("key1", "value2");
txMgrImpl.internalResume(tx);
assertEquals(false, this.region.containsValueForKey("key1"));
this.region.put("key1", "value3");
assertEquals(true, this.region.containsValueForKey("key1"));
try {
txMgrImpl.commit();
fail("expected CommitConflictException");
} catch (CommitConflictException ex) {
}
// try an invalidate of an already invalid entry
// non-tx
this.region.remove("key1");
// non-tx
this.region.create("key1", null);
txMgrImpl.begin();
this.region.get("key1");
// should be a noop since it is already invalid
this.region.localInvalidate("key1");
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
txMgrImpl.commit();
assertEquals(false, this.region.containsKey("key1"));
// make sure a noop invalidate is repeatable read
// non-tx
this.region.remove("key1");
// non-tx
this.region.create("key1", null);
txMgrImpl.begin();
// should be a noop since it is already invalid
this.region.localInvalidate("key1");
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsKey("key1"));
assertEquals(false, this.region.containsValueForKey("key1"));
txMgrImpl.commit();
assertEquals(false, this.region.containsKey("key1"));
// make sure a destroy that throws entryNotFound is repeatable read
// non-tx
this.region.remove("key1");
txMgrImpl.begin();
try {
this.region.localDestroy("key1");
fail("expected EntryNotFoundException");
} catch (EntryNotFoundException expected) {
}
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.create("key1", "value1");
txMgrImpl.internalResume(tx);
assertEquals(false, this.region.containsKey("key1"));
txMgrImpl.commit();
assertEquals(true, this.region.containsKey("key1"));
// non-tx
this.region.remove("key1");
// make sure a create that throws entryExists is repeatable read
// non-tx
this.region.create("key1", "non-tx-value1");
txMgrImpl.begin();
try {
this.region.create("key1", "value1");
fail("expected EntryExistsException");
} catch (EntryExistsException expected) {
}
tx = txMgrImpl.internalSuspend();
// non-tx
this.region.remove("key1");
txMgrImpl.internalResume(tx);
assertEquals(true, this.region.containsKey("key1"));
txMgrImpl.commit();
assertEquals(false, this.region.containsKey("key1"));
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXWriterJUnitTest method testNoCallbacksOnTransactionWriterThrow.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testNoCallbacksOnTransactionWriterThrow() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException cce) {
assertNotNull(cce.getCause());
assertTrue(cce.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.cbCount = 0;
this.region.create("key1", "value1");
// do a sanity check to make sure callbacks are installed
// 2 -> 1writer + 1listener
assertEquals(2, this.cbCount);
this.txMgr.begin();
this.region.put("key1", "value2");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
this.region.create("key1", "value1");
this.txMgr.begin();
this.region.localDestroy("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.destroy("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.localInvalidate("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
this.region.put("key1", "value1");
this.txMgr.begin();
this.region.invalidate("key1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
this.region.localDestroy("key1");
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnThrowNPE.
@Test
public void testAfterCommitFailedOnThrowNPE() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new NullPointerException("this is expected!");
}
public void close() {
}
});
installTransactionListener();
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof NullPointerException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(0, this.afterRollbacks);
}
Aggregations