use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class PutAllCSDUnitTest method testTX.
/**
* Test TX for putAll. There's no TX for c/s. We only test P2P This is disabled because putAll in
* TX is disabled.
*/
@Ignore("TODO: test is disabled")
@Test
public void testTX() throws CacheException, InterruptedException {
final String title = "testTX:";
disconnectAllFromDS();
final Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
final String regionName = getUniqueName();
final int serverPort1 = createBridgeServer(server1, regionName, 0, false, 0, null);
final int serverPort2 = createBridgeServer(server2, regionName, 0, false, 0, null);
// final String serverHost = getServerHostName(server1.getHost());
// set notifyBySubscription=true to test register interest
// add slow listener
server1.invoke(new CacheSerializableRunnable(title + "server1 add slow listener") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
region.getAttributesMutator().addCacheListener(new MyListener(true));
}
});
server2.invoke(new CacheSerializableRunnable(title + "server2 add slow listener") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
region.getAttributesMutator().addCacheListener(new MyListener(true));
}
});
// TX1: server1 do a putAll
AsyncInvocation async1 = server1.invokeAsync(new CacheSerializableRunnable(title + "TX1: async putAll from server1") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
// Get JNDI(Java Naming and Directory interface) context
// CacheTransactionManager tx = getCache().getCacheTransactionManager();
LinkedHashMap map = new LinkedHashMap();
// tx.begin();
for (int i = 0; i < numberOfEntries; i++) {
// region.put("key-"+i, new TestObject(i));
map.put("key-" + i, new TestObject(i));
}
region.putAll(map, "putAllCallback");
try {
LogWriterUtils.getLogWriter().info("before commit TX1");
// tx.commit();
LogWriterUtils.getLogWriter().info("TX1 committed");
} catch (CommitConflictException e) {
LogWriterUtils.getLogWriter().info("TX1 rollbacked");
}
}
});
// we have to pause a while to let TX1 finish earlier
Wait.pause(500);
// TX2: server2 do a putAll
AsyncInvocation async2 = server2.invokeAsync(new CacheSerializableRunnable(title + "TX2: async putAll from server2") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
// Get JNDI(Java Naming and Directory interface) context
// CacheTransactionManager tx = getCache().getCacheTransactionManager();
LinkedHashMap map = new LinkedHashMap();
// tx.begin();
for (int i = 0; i < numberOfEntries; i++) {
// region.put("key-"+i, new TestObject(i + numberOfEntries));
map.put("key-" + i, new TestObject(i + numberOfEntries));
}
region.putAll(map, "putAllCallback");
try {
LogWriterUtils.getLogWriter().info("before commit TX2");
// tx.commit();
LogWriterUtils.getLogWriter().info("TX2 committed");
} catch (CommitConflictException e) {
LogWriterUtils.getLogWriter().info("TX2 rollbacked");
}
}
});
// TX3: server2 do a putAll in another thread
AsyncInvocation async3 = server2.invokeAsync(new CacheSerializableRunnable(title + "TX3: async putAll from server2") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
// Get JNDI(Java Naming and Directory interface) context
// CacheTransactionManager tx = getCache().getCacheTransactionManager();
LinkedHashMap map = new LinkedHashMap();
// tx.begin();
for (int i = 0; i < numberOfEntries; i++) {
// region.put("key-"+i, new TestObject(i+numberOfEntries*2));
map.put("key-" + i, new TestObject(i + numberOfEntries * 2));
}
region.putAll(map, "putAllCallback");
try {
LogWriterUtils.getLogWriter().info("before commit TX3");
// tx.commit();
LogWriterUtils.getLogWriter().info("TX3 committed");
} catch (CommitConflictException e) {
LogWriterUtils.getLogWriter().info("TX3 rollbacked");
}
}
});
ThreadUtils.join(async1, 30 * 1000);
ThreadUtils.join(async2, 30 * 1000);
ThreadUtils.join(async3, 30 * 1000);
// verify server 2 for asyn keys
server2.invoke(new CacheSerializableRunnable(title + "verify Bridge server2 for keys") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
assertEquals(numberOfEntries, region.size());
int tx_no = 0;
for (int i = 0; i < numberOfEntries; i++) {
TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
if (tx_no == 0) {
// only check which tx took control once
if (obj.getPrice() == i) {
tx_no = 1;
} else if (obj.getPrice() == i + numberOfEntries) {
tx_no = 2;
} else if (obj.getPrice() == i + numberOfEntries * 2) {
tx_no = 3;
}
LogWriterUtils.getLogWriter().info("Verifying TX:" + tx_no);
}
if (tx_no == 1) {
assertEquals(i, obj.getPrice());
} else if (tx_no == 2) {
assertEquals(i + numberOfEntries, obj.getPrice());
} else {
assertEquals(i + numberOfEntries * 2, obj.getPrice());
}
}
}
});
// clean up
// Stop server
stopBridgeServers(getCache());
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class TXLockServiceDUnitTest method testTXLock.
@Test
public void testTXLock() {
LogWriterUtils.getLogWriter().info("[testTXLock]");
final int grantorVM = 0;
final int clientA = 1;
final int clientB = 2;
final Set participants = Collections.EMPTY_SET;
final List regionLockReqs = new ArrayList();
regionLockReqs.add(new TXRegionLockRequestImpl("/testTXLock1", new HashSet(Arrays.asList(new String[] { "KEY-1", "KEY-2", "KEY-3", "KEY-4" }))));
regionLockReqs.add(new TXRegionLockRequestImpl("/testTXLock2", new HashSet(Arrays.asList(new String[] { "KEY-A", "KEY-B", "KEY-C", "KEY-D" }))));
// create grantor
LogWriterUtils.getLogWriter().info("[testTXLock] create grantor");
Host.getHost(0).getVM(grantorVM).invoke(new SerializableRunnable() {
public void run() {
TXLockService.createDTLS();
}
});
sleep(20);
// create client and request txLock
LogWriterUtils.getLogWriter().info("[testTXLock] create clientA and request txLock");
Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable() {
public void run() {
TXLockService.createDTLS();
}
});
Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable("[testTXLock] create clientA and request txLock") {
public void run() {
TXLockService dtls = TXLockService.getDTLS();
testTXLock_TXLockId = dtls.txLock(regionLockReqs, participants);
assertNotNull("testTXLock_TXLockId is null", testTXLock_TXLockId);
}
});
// create nuther client and request overlapping txLock... verify fails
LogWriterUtils.getLogWriter().info("[testTXLock] create clientB and fail txLock");
Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable() {
public void run() {
TXLockService.createDTLS();
}
});
Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable() {
public void run() {
try {
TXLockService dtls = TXLockService.getDTLS();
dtls.txLock(regionLockReqs, participants);
fail("expected CommitConflictException");
} catch (CommitConflictException expected) {
}
}
});
/*
* try { Host.getHost(0).getVM(clientB).invoke(() -> TXLockServiceDUnitTest.txLock_DTLS(
* regionLockReqs, participants )); fail("expected CommitConflictException"); } catch
* (RMIException expected) { assertTrue(expected.getCause() instanceof CommitConflictException);
* }
*/
// release txLock
LogWriterUtils.getLogWriter().info("[testTXLock] clientA releases txLock");
Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable("[testTXLock] clientA releases txLock") {
public void run() {
TXLockService dtls = TXLockService.getDTLS();
dtls.release(testTXLock_TXLockId);
}
});
sleep(20);
// try nuther client again and verify success
LogWriterUtils.getLogWriter().info("[testTXLock] clientB requests txLock");
Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable("[testTXLock] clientB requests txLock") {
public void run() {
TXLockService dtls = TXLockService.getDTLS();
testTXLock_TXLockId = dtls.txLock(regionLockReqs, participants);
assertNotNull("testTXLock_TXLockId is null", testTXLock_TXLockId);
}
});
// release txLock
LogWriterUtils.getLogWriter().info("[testTXLock] clientB releases txLock");
Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable("[testTXLock] clientB releases txLock") {
public void run() {
TXLockService dtls = TXLockService.getDTLS();
dtls.release(testTXLock_TXLockId);
}
});
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class RemoteTransactionDUnitTest method testTxReplace.
@Test
public void testTxReplace() {
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 Customer updatedCust = new Customer("updated", "updated");
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.replace(custId, fakeCust, updatedCust));
assertTrue(cust.replace(custId, customer, updatedCust));
assertFalse(ref.replace(custId, fakeCust, updatedCust));
assertTrue(ref.replace(custId, customer, updatedCust));
TXStateProxy tx = mgr.internalSuspend();
assertEquals(cust.get(custId), customer);
assertEquals(ref.get(custId), customer);
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);
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(updatedCust, cust.get(custId));
assertEquals(updatedCust, 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.replace(conflictCust, customer, new Customer("conflict", "conflict")));
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 RemoteTransactionDUnitTest method doTestTxFunction.
private void doTestTxFunction(final Executions e) {
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);
SerializableCallable registerFunction = new SerializableCallable() {
public Object call() throws Exception {
FunctionService.registerFunction(new TXFunction());
return null;
}
};
accessor.invoke(registerFunction);
datastore1.invoke(registerFunction);
datastore2.invoke(registerFunction);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
TXManagerImpl mgr = getGemfireCache().getTXMgr();
Set regions = new HashSet();
regions.add(custRegion);
regions.add(getGemfireCache().getRegion(ORDER));
mgr.begin();
try {
switch(e) {
case OnRegion:
FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
break;
case OnMember:
FunctionService.onMembers().execute(TXFunction.id).getResult();
break;
}
fail("Expected exception not thrown");
} catch (TransactionException expected) {
}
try {
InternalFunctionService.onRegions(regions).execute(TXFunction.id).getResult();
fail("Expected exception not thrown");
} catch (TransactionException expected) {
}
Set filter = new HashSet();
filter.add(expectedCustId);
switch(e) {
case OnRegion:
FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
break;
case OnMember:
DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
FunctionService.onMember(owner).execute(TXFunction.id).getResult();
break;
}
TXStateProxy tx = mgr.internalSuspend();
GemFireCacheImpl.getInstance().getLogger().warning("TX SUSPENDO:" + tx);
assertNull(custRegion.get(expectedCustId));
mgr.internalResume(tx);
return null;
}
});
final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
assertEquals(1, txOnDatastore1 + txOnDatastore2);
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTXMgr();
mgr.commit();
return null;
}
});
datastore1.invoke(new SerializableCallable() {
public Object call() throws Exception {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
TXManagerImpl mgr = getGemfireCache().getTXMgr();
mgr.begin();
PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
Set filter = new HashSet();
filter.add(expectedCustId);
switch(e) {
case OnRegion:
FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
break;
case OnMember:
DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
FunctionService.onMember(owner).execute(TXFunction.id).getResult();
break;
}
TXStateProxy tx = mgr.internalSuspend();
custRegion.put(expectedCustId, new Customer("Cust6", "updated6"));
mgr.internalResume(tx);
try {
mgr.commit();
fail("expected commit conflict not thrown");
} catch (CommitConflictException expected) {
}
return null;
}
});
}
use of org.apache.geode.cache.CommitConflictException in project geode by apache.
the class CacheServerTransactionsDUnitTest method commitTransactionOnClient.
public static void commitTransactionOnClient() {
Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
assertNotNull(r1);
try {
cache.getCacheTransactionManager().begin();
r1.put(k1, client_k1);
r1.put(k2, client_k2);
cache.getCacheTransactionManager().commit();
} catch (CommitConflictException e) {
fail("Test failed due to CommitConflictException on client , which is not expected");
}
assertEquals(r1.getEntry(k1).getValue(), client_k1);
assertEquals(r1.getEntry(k2).getValue(), client_k2);
}
Aggregations