use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrow.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testAfterCommitFailedOnTransactionWriterThrow() 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 expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(0, this.afterRollbacks);
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class MyTransactionFunction method verifyTxStateAndConflicts.
private void verifyTxStateAndConflicts(RegionFunctionContext ctx) {
Region custPR = ctx.getDataSet();
Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
ArrayList args = (ArrayList) ctx.getArguments();
CustId custId = (CustId) args.get(1);
CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
OrderId vOrderId = new OrderId(3000, custId);
Order vOrder = new Order("vOrder");
TXManagerImpl mImp = (TXManagerImpl) mgr;
mImp.begin();
orderPR.put(vOrderId, vOrder);
TXStateProxy txState = mImp.internalSuspend();
Iterator it = txState.getRegions().iterator();
Assert.assertTrue(txState.getRegions().size() == 1, "Expected 1 region; " + "found:" + txState.getRegions().size());
LocalRegion lr = (LocalRegion) it.next();
Assert.assertTrue(lr instanceof BucketRegion);
TXRegionState txRegion = txState.readRegion(lr);
TXEntryState txEntry = txRegion.readEntry(txRegion.getEntryKeys().iterator().next());
mImp.internalResume(txState);
orderPR.put(vOrderId, new Order("foo"));
txState = mImp.internalSuspend();
// since both puts were on same key, verify that
// TxRegionState and TXEntryState are same
LocalRegion lr1 = (LocalRegion) txState.getRegions().iterator().next();
Assert.assertTrue(lr == lr1);
TXRegionState txRegion1 = txState.readRegion(lr);
TXEntryState txEntry1 = txRegion1.readEntry(txRegion.getEntryKeys().iterator().next());
Assert.assertTrue(txEntry == txEntry1);
// to check for conflicts, start a new transaction, operate on same key,
// commit the second and expect the first to fail
mImp.begin();
orderPR.put(vOrderId, new Order("foobar"));
mImp.commit();
// now begin the first
mImp.internalResume(txState);
boolean caughtException = false;
try {
mImp.commit();
} catch (CommitConflictException e) {
caughtException = true;
}
if (!caughtException) {
throw new TestException("An expected exception was not thrown");
}
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class RemoteTransactionDUnitTest method testTxRemoveAll.
@Test
public void testTxRemoveAll() {
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 custId1 = new CustId(1);
final CustId custId2 = new CustId(2);
final CustId custId20 = new CustId(20);
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 customer2 = new Customer("customer2", "address2");
Customer fakeCust = new Customer("foo2", "bar2");
cust.removeAll(Arrays.asList(custId1, custId2, custId20));
ref.removeAll(Arrays.asList(custId1, custId2, custId20));
TXStateProxy tx = mgr.internalSuspend();
assertNotNull(cust.get(custId1));
assertNotNull(ref.get(custId2));
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(4, 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));
// custId20 won't be dirty because it doesn't exist.
assertTrue("key:" + key + " r:" + r.getFullPath(), key.equals(custId20) || 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(custId1));
assertNull(rr.get(custId2));
// check conflict
mgr.begin();
CustId custId3 = new CustId(3);
CustId custId4 = new CustId(4);
getGemfireCache().getLoggerI18n().fine("SWAP:removeConflict");
cust.removeAll(Arrays.asList(custId3, custId20, custId4));
TXStateProxy tx = mgr.internalSuspend();
// cust.put(custId3, new Customer("foo", "bar"));
cust.put(custId20, new Customer("foo", "bar"));
assertNotNull(cust.get(custId20));
cust.put(custId4, new Customer("foo", "bar"));
mgr.internalResume(tx);
try {
mgr.commit();
fail("expected exception not thrown");
} catch (CommitConflictException e) {
}
assertNotNull(cust.get(custId3));
assertNotNull(cust.get(custId4));
assertNotNull(cust.get(custId20));
// Test a removeall an already missing key.
// custId2 has already been removed
mgr.begin();
getGemfireCache().getLoggerI18n().fine("SWAP:removeConflict");
cust.removeAll(Arrays.asList(custId2, custId3));
tx = mgr.internalSuspend();
cust.put(custId2, new Customer("foo", "bar"));
mgr.internalResume(tx);
mgr.commit();
assertNotNull(cust.get(custId2));
assertNull(cust.get(custId3));
return null;
}
});
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class RemoteTransactionDUnitTest method testBug33073.
@Test
public void testBug33073() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore1 = host.getVM(1);
VM datastore2 = host.getVM(2);
initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
final CustId custId = new CustId(19);
datastore1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region<CustId, Customer> refRegion = getCache().getRegion(D_REFERENCE);
assertNull(refRegion.get(custId));
getCache().getCacheTransactionManager().begin();
refRegion.put(custId, new Customer("name1", "address1"));
return null;
}
});
datastore2.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region<CustId, Customer> refRegion = getCache().getRegion(D_REFERENCE);
assertNull(refRegion.get(custId));
refRegion.put(custId, new Customer("nameNew", "addressNew"));
return null;
}
});
datastore1.invoke(new SerializableCallable() {
public Object call() throws Exception {
try {
getCache().getCacheTransactionManager().commit();
fail("expected commit conflict not thrown");
} catch (CommitConflictException cc) {
}
return null;
}
});
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class RemoteTransactionDUnitTest method testTxPutIfAbsent.
@Test
public void testTxPutIfAbsent() {
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 newCustId = new CustId(10);
final Customer updateCust = new Customer("customer10", "address10");
final TXId txId = (TXId) accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTxManager();
mgr.begin();
Region<CustId, Customer> cust = getGemfireCache().getRegion(CUSTOMER);
Region<CustId, Customer> rr = getGemfireCache().getRegion(D_REFERENCE);
Customer expectedCust = new Customer("customer" + 1, "address" + 1);
getGemfireCache().getLoggerI18n().fine("SWAP:doingPutIfAbsent");
CustId oldCustId = new CustId(1);
Customer old = cust.putIfAbsent(oldCustId, updateCust);
assertTrue("expected:" + expectedCust + " but was " + old, expectedCust.equals(old));
// transaction should be bootstrapped
old = rr.putIfAbsent(oldCustId, updateCust);
assertTrue("expected:" + expectedCust + " but was " + old, expectedCust.equals(old));
// now a key that does not exist
old = cust.putIfAbsent(newCustId, updateCust);
assertNull(old);
old = rr.putIfAbsent(newCustId, updateCust);
assertNull(old);
Region<OrderId, Order> order = getGemfireCache().getRegion(ORDER);
Order oldOrder = order.putIfAbsent(new OrderId(10, newCustId), new Order("order10"));
assertNull(old);
assertNull(oldOrder);
assertNotNull(cust.get(newCustId));
assertNotNull(rr.get(newCustId));
TXStateProxy tx = mgr.internalSuspend();
assertNull(cust.get(newCustId));
assertNull(rr.get(newCustId));
mgr.internalResume(tx);
cust.put(oldCustId, new Customer("foo", "bar"));
rr.put(oldCustId, new Customer("foo", "bar"));
return mgr.getTransactionId();
}
});
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region cust = getGemfireCache().getRegion(CUSTOMER);
int hash1 = PartitionedRegionHelper.getHashKey((PartitionedRegion) cust, new CustId(1));
int hash10 = PartitionedRegionHelper.getHashKey((PartitionedRegion) cust, new CustId(10));
TXManagerImpl mgr = getGemfireCache().getTxManager();
assertTrue(mgr.isHostedTxInProgress(txId));
TXStateProxy tx = mgr.getHostedTXState(txId);
// 2 buckets for the two
assertEquals(3 + (hash1 == hash10 ? 0 : 1), 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);
assertNotNull(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);
assertEquals(updateCust, cust.get(newCustId));
assertEquals(updateCust, rr.get(newCustId));
// test conflict
mgr.begin();
CustId conflictCust = new CustId(11);
cust.putIfAbsent(conflictCust, new Customer("name11", "address11"));
TXStateProxy tx = mgr.internalSuspend();
cust.put(conflictCust, new Customer("foo", "bar"));
mgr.internalResume(tx);
try {
mgr.commit();
fail("expected exception not thrown");
} catch (CommitConflictException cce) {
}
return null;
}
});
}
Aggregations