use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testTransactionalKeyBasedDestroys_PR.
@Test
public void testTransactionalKeyBasedDestroys_PR() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createPR(new VM[] { server1, server2, server3 });
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
// mgr.begin();
Region<CustId, Customer> custPR = getCache().getRegion(CUSTOMER_PR);
for (int i = 1; i <= 1000; i++) {
mgr.begin();
custPR.put(new CustId(i), new Customer("name" + i, "addr" + i));
mgr.commit();
}
// Destroys
for (int i = 1; i <= 100; i++) {
CustId custId = new CustId(i);
mgr.begin();
Object customerRemoved = custPR.remove(custId);
// Removing this assertion since in case of distributed destroys the
// value will not be returned.
// assertNotNull(customerRemoved);
mgr.commit();
}
// Verify
for (int i = 1; i <= 100; i++) {
CustId custId = new CustId(1);
Customer customer = custPR.get(custId);
assertNull(customer);
}
assertEquals(900, custPR.size());
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testPutAllWithTransactions.
@Test
public void testPutAllWithTransactions() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createRegions(new VM[] { server1, server2, server3 });
execute(new VM[] { server1, server2, server3 }, new SerializableCallable() {
@Override
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
af.setPartitionAttributes(new PartitionAttributesFactory<CustId, Customer>().setTotalNumBuckets(4).setLocalMaxMemory(1).setPartitionResolver(new CustomerIDPartitionResolver("resolver1")).setRedundantCopies(0).create());
getCache().createRegion("NONCOLOCATED_PR", af.create());
return null;
}
});
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region custRegion = getCache().getRegion(CUSTOMER_PR);
Region orderRegion = getCache().getRegion(ORDER_PR);
Map custMap = new HashMap();
Map orderMap = new HashMap();
for (int i = 0; i < 5; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("customer" + i, "address" + i);
OrderId orderId = new OrderId(i, custId);
Order order = new Order("order" + i);
custMap.put(custId, customer);
orderMap.put(orderId, order);
}
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
mgr.begin();
custRegion.putAll(custMap);
orderRegion.putAll(orderMap);
mgr.commit();
mgr.begin();
assertEquals(5, custRegion.size());
assertEquals(5, orderRegion.size());
custMap = new HashMap();
orderMap = new HashMap();
for (int i = 5; i < 10; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("customer" + i, "address" + i);
OrderId orderId = new OrderId(i, custId);
Order order = new Order("order" + i);
custMap.put(custId, customer);
orderMap.put(orderId, order);
}
custRegion.putAll(custMap);
orderRegion.putAll(orderMap);
mgr.rollback();
mgr.begin();
assertEquals(5, custRegion.size());
assertEquals(5, orderRegion.size());
custRegion.putAll(custMap);
orderRegion.putAll(orderMap);
assertEquals(10, custRegion.size());
assertEquals(10, orderRegion.size());
// Verify operations involving non colocated PR
Map map = new HashMap();
custMap.clear();
for (int i = 10; i < 15; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("customer" + i, "address" + i);
OrderId orderId = new OrderId(i, custId);
Order order = new Order("order" + i);
custMap.put(custId, customer);
map.put(custId, customer);
}
custRegion.putAll(custMap);
Region nonColocatedRegion = getCache().getRegion("NONCOLOCATED_PR");
nonColocatedRegion.putAll(orderMap);
mgr.commit();
mgr.begin();
assertEquals(15, custRegion.size());
assertEquals(5, nonColocatedRegion.size());
custMap.clear();
map.clear();
for (int i = 15; i < 20; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("customer" + i, "address" + i);
OrderId orderId = new OrderId(i, custId);
Order order = new Order("order" + i);
custMap.put(custId, customer);
map.put(custId, customer);
}
custRegion.putAll(custMap);
nonColocatedRegion.putAll(orderMap);
mgr.rollback();
assertEquals(15, custRegion.size());
assertEquals(5, nonColocatedRegion.size());
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testCommitAndRollback.
@Test
public void testCommitAndRollback() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createPR(new VM[] { server1, server2, server3 });
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
// mgr.begin();
Region<CustId, Customer> custPR = getCache().getRegion(CUSTOMER_PR);
for (int i = 0; i < 1000; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("name" + i, "addr" + i);
mgr.begin();
custPR.put(custId, customer);
if (i % 2 == 0) {
mgr.commit();
} else {
mgr.rollback();
}
}
// Verify number of puts
assertEquals(500, custPR.size());
Set<Region.Entry<?, ?>> entries = custPR.entrySet(false);
assertEquals(500, entries.size());
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testRegionAndEntryVersionsRR.
@Test
public void testRegionAndEntryVersionsRR() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
createRR(new VM[] { server1, server2 });
execute(server2, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
getGemfireCache().getLoggerI18n().fine("TEST:DISTTX=" + mgr.isDistributed());
getGemfireCache().getLoggerI18n().fine("TEST:TX BEGIN");
mgr.begin();
Region<CustId, Customer> region = getCache().getRegion(CUSTOMER_RR);
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
getGemfireCache().getLoggerI18n().fine("TEST:TX PUT 1");
region.put(custIdOne, customerOne);
LocalRegion lr = (LocalRegion) region;
assertEquals(0L, lr.getVersionVector().getCurrentVersion());
getGemfireCache().getLoggerI18n().fine("TEST:TX COMMIT 1");
mgr.commit();
// Verify region version on the region
assertEquals(1L, lr.getVersionVector().getCurrentVersion());
RegionEntry re = lr.getRegionEntry(custIdOne);
getGemfireCache().getLoggerI18n().fine("TEST:VERSION-STAMP:" + re.getVersionStamp());
// Verify region version on the region entry
assertEquals(1L, re.getVersionStamp().getRegionVersion());
// Verify entry version
assertEquals(1, re.getVersionStamp().getEntryVersion());
mgr.begin();
region.put(custIdOne, new Customer("name1_1", "addr1"));
getGemfireCache().getLoggerI18n().fine("TEST:TX COMMIT 2");
assertEquals(1L, lr.getVersionVector().getCurrentVersion());
mgr.commit();
// Verify region version on the region
assertEquals(2L, lr.getVersionVector().getCurrentVersion());
re = lr.getRegionEntry(custIdOne);
getGemfireCache().getLoggerI18n().fine("TEST:VERSION-STAMP:" + re.getVersionStamp());
// Verify region version on the region entry
assertEquals(2L, re.getVersionStamp().getRegionVersion());
// Verify entry version
assertEquals(2, re.getVersionStamp().getEntryVersion());
return null;
}
});
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> region = getCache().getRegion(CUSTOMER_RR);
CustId custIdOne = new CustId(1);
LocalRegion lr = (LocalRegion) region;
// Verify region version on the region
assertEquals(2L, lr.getVersionVector().getCurrentVersion());
// Verify region version ont the region entry
RegionEntry re = lr.getRegionEntry(custIdOne);
assertEquals(2L, re.getVersionStamp().getRegionVersion());
// Verify entry version
assertEquals(2, re.getVersionStamp().getEntryVersion());
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testConcurrentTXAndNonTXOperations.
/*
* Test to reproduce a scenario where: 1. On primary, the tx op is applied first followed by
* non-tx 2. On secondary, non-tx op is applied first followed by tx.
*/
@Ignore
@Test
public void testConcurrentTXAndNonTXOperations() throws Exception {
Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
createPersistentPR(new VM[] { server1 });
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
prRegion.put(custIdOne, customerOne);
BucketRegion br = ((PartitionedRegion) prRegion).getBucketRegion(custIdOne);
String primaryMember = br.getBucketAdvisor().getPrimary().toString();
getGemfireCache().getLoggerI18n().fine("TEST:PRIMARY:" + primaryMember);
String memberId = getGemfireCache().getDistributedSystem().getMemberId();
getGemfireCache().getLoggerI18n().fine("TEST:MEMBERID:" + memberId);
return null;
}
});
createPersistentPR(new VM[] { server2 });
Boolean isPrimary = (Boolean) execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custIdOne = new CustId(1);
BucketRegion br = ((PartitionedRegion) prRegion).getBucketRegion(custIdOne);
String primaryMember = br.getBucketAdvisor().getPrimary().toString();
getGemfireCache().getLoggerI18n().fine("TEST:PRIMARY:" + primaryMember);
String memberId = getGemfireCache().getDistributedSystem().getMemberId();
getGemfireCache().getLoggerI18n().fine("TEST:MEMBERID:" + memberId);
return memberId.equals(primaryMember);
}
});
final VM primary = isPrimary.booleanValue() ? server1 : server2;
final VM secondary = !isPrimary.booleanValue() ? server1 : server2;
System.out.println("TEST:SERVER-1:VM-" + server1.getPid());
System.out.println("TEST:SERVER-2:VM-" + server2.getPid());
System.out.println("TEST:PRIMARY=VM-" + primary.getPid());
System.out.println("TEST:SECONDARY=VM-" + secondary.getPid());
class WaitRelease implements Runnable {
CountDownLatch cdl;
String op;
public WaitRelease(CountDownLatch cdl, String member) {
this.cdl = cdl;
}
@Override
public void run() {
try {
GemFireCacheImpl.getExisting().getLoggerI18n().fine("TEST:TX WAITING - " + op);
cdl.await();
GemFireCacheImpl.getExisting().getLoggerI18n().fine("TEST:TX END WAITING");
} catch (InterruptedException e) {
}
}
public void release() {
GemFireCacheImpl.getExisting().getLoggerI18n().fine("TEST:TX COUNTDOWN - " + op);
cdl.countDown();
}
}
// Install TX hook
SerializableCallable txHook = new SerializableCallable() {
@Override
public Object call() throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
DistTXState.internalBeforeApplyChanges = new WaitRelease(cdl, "TX OP");
return null;
}
};
execute(secondary, txHook);
// Install non-TX hook
SerializableCallable nontxHook = new SerializableCallable() {
@Override
public Object call() throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
DistTXState.internalBeforeNonTXBasicPut = new WaitRelease(cdl, "NON TX OP");
return null;
}
};
// Install the wait-release hook on the secondary
execute(secondary, nontxHook);
// Start a tx operation on primary
execute(primary, new SerializableCallable() {
@Override
public Object call() throws Exception {
// The reason this is run in a separate thread instead of controller thread
// is that this is going to block because the secondary is going to wait.
new Thread() {
public void run() {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
getGemfireCache().getLoggerI18n().fine("TEST:DISTTX=" + mgr.isDistributed());
mgr.begin();
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1_tx", "addr1");
getGemfireCache().getLoggerI18n().fine("TEST:TX UPDATE");
prRegion.put(custIdOne, customerOne);
getGemfireCache().getLoggerI18n().fine("TEST:TX COMMIT");
mgr.commit();
}
}.start();
return null;
}
});
// Let the TX op be applied on primary first
Thread.currentThread().sleep(200);
// Perform a non-tx op on the same key on primary
execute(primary, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1_nontx", "addr1");
getGemfireCache().getLoggerI18n().fine("TEST:TX NONTXUPDATE");
prRegion.put(custIdOne, customerOne);
return null;
}
});
// Wait for a few milliseconds
Thread.currentThread().sleep(200);
// Release the waiting non-tx op first, on secondary
execute(secondary, new SerializableCallable() {
@Override
public Object call() throws Exception {
Runnable r = DistTXState.internalBeforeNonTXBasicPut;
assert (r != null && r instanceof WaitRelease);
WaitRelease e = (WaitRelease) r;
e.release();
return null;
}
});
// Now release the waiting commit on secondary
execute(secondary, new SerializableCallable() {
@Override
public Object call() throws Exception {
Runnable r = DistTXState.internalBeforeApplyChanges;
assert (r != null && r instanceof WaitRelease);
WaitRelease e = (WaitRelease) r;
e.release();
return null;
}
});
// Verify region and entry versions on primary and secondary
SerializableCallable verifyPrimary = new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custId = new CustId(1);
Customer customer = prRegion.get(custId);
BucketRegion br = ((PartitionedRegion) prRegion).getBucketRegion(custId);
RegionEntry re = br.getRegionEntry(custId);
getGemfireCache().getLoggerI18n().fine("TEST:TX PRIMARY CUSTOMER=" + customer);
getGemfireCache().getLoggerI18n().fine("TEST:TX PRIMARY REGION VERSION=" + re.getVersionStamp().getRegionVersion());
getGemfireCache().getLoggerI18n().fine("TEST:TX PRIMARY ENTRY VERSION=" + re.getVersionStamp().getEntryVersion());
return null;
}
};
execute(primary, verifyPrimary);
SerializableCallable verifySecondary = new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> prRegion = getCache().getRegion(PERSISTENT_CUSTOMER_PR);
CustId custId = new CustId(1);
Customer customer = prRegion.get(custId);
BucketRegion br = ((PartitionedRegion) prRegion).getBucketRegion(custId);
RegionEntry re = br.getRegionEntry(custId);
getGemfireCache().getLoggerI18n().fine("TEST:TX SECONDARY CUSTOMER=" + customer);
getGemfireCache().getLoggerI18n().fine("TEST:TX SECONDARY REGION VERSION=" + re.getVersionStamp().getRegionVersion());
getGemfireCache().getLoggerI18n().fine("TEST:TX SECONDARY ENTRY VERSION=" + re.getVersionStamp().getEntryVersion());
return null;
}
};
execute(secondary, verifySecondary);
}
Aggregations