use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testTransactionalKeyBasedUpdates.
@Test
public void testTransactionalKeyBasedUpdates() 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();
LogWriterI18n logger = getGemfireCache().getLoggerI18n();
Region<CustId, Customer> custPR = getCache().getRegion(CUSTOMER_PR);
for (int i = 1; i <= 2; i++) {
mgr.begin();
logger.fine("TEST:PUT-" + i);
custPR.put(new CustId(i), new Customer("name" + i, "addr" + i));
logger.fine("TEST:COMMIT-" + i);
mgr.commit();
}
// Updates
for (int i = 1; i <= 2; i++) {
CustId custId = new CustId(i);
Customer customer = custPR.get(custId);
assertNotNull(customer);
mgr.begin();
logger.fine("TEST:UPDATE-" + i);
custPR.put(custId, new Customer("name" + i * 2, "addr" + i * 2));
logger.fine("TEST:UPDATED-" + i + "=" + custId + "," + custPR.get(custId));
logger.fine("TEST:UPDATE COMMIT-" + i);
mgr.commit();
logger.fine("TEST:POSTCOMMIT-" + i + "=" + custId + "," + custPR.get(custId));
}
// Verify
for (int i = 1; i <= 2; i++) {
CustId custId = new CustId(i);
Customer customer = custPR.get(custId);
assertNotNull(customer);
logger.fine("TEST:VERIFYING-" + i);
assertEquals(new Customer("name" + i * 2, "addr" + i * 2), customer);
}
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testNonColocatedPutByPartitioning.
/*
* We create 2 partitioned regions one on each server and have a third node as accessor and fire
* transactional operations on it.
*/
@Test
public void testNonColocatedPutByPartitioning() {
Host host = Host.getHost(0);
// datastore
VM server1 = host.getVM(0);
// datastore
VM server2 = host.getVM(1);
// accessor
VM server3 = host.getVM(2);
final String CUSTOMER_PR1 = "CUSTOMER_PR1";
final String CUSTOMER_PR2 = "CUSTOMER_PR2";
// Create CUSTOMER_PR1 on server1
execute(server1, 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(CUSTOMER_PR1, af.create());
return null;
}
});
// Create CUSTOMER_PR2 on server2
execute(server2, 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("resolver2")).setRedundantCopies(0).create());
getCache().createRegion(CUSTOMER_PR2, af.create());
return null;
}
});
// Create both the regions on server3 (accessor)
execute(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(// since this is an accessor
0).setPartitionResolver(new CustomerIDPartitionResolver("resolver1")).setRedundantCopies(0).create());
getCache().createRegion(CUSTOMER_PR1, af.create());
return null;
}
});
execute(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(// since this is an accessor
0).setPartitionResolver(new CustomerIDPartitionResolver("resolver2")).setRedundantCopies(0).create());
getCache().createRegion(CUSTOMER_PR2, af.create());
return null;
}
});
// Now perform tx ops on accessor
execute(server3, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
mgr.begin();
Region<CustId, Customer> custPR1 = getCache().getRegion(CUSTOMER_PR1);
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
custPR1.put(custIdOne, customerOne);
Region<CustId, Customer> custPR2 = getCache().getRegion(CUSTOMER_PR2);
custPR2.put(custIdOne, customerOne);
mgr.commit();
// Verify
assertEquals(1, custPR1.size());
assertEquals(1, custPR2.size());
return null;
}
});
// Verify on one of the servers
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
Region<CustId, Customer> custPR1 = getCache().getRegion(CUSTOMER_PR1);
assertEquals(1, custPR1.size());
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
assertEquals(customerOne, custPR1.get(custIdOne));
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testTransactionalKeyBasedDestroys_RR.
@Test
public void testTransactionalKeyBasedDestroys_RR() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createRR(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> custRR = getCache().getRegion(CUSTOMER_RR);
for (int i = 1; i <= 1000; i++) {
mgr.begin();
custRR.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 = custRR.remove(custId);
assertNotNull(customerRemoved);
mgr.commit();
}
// Verify
for (int i = 1; i <= 100; i++) {
CustId custId = new CustId(1);
Customer customer = custRR.get(custId);
assertNull(customer);
}
assertEquals(900, custRR.size());
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class DistributedTransactionDUnitTest method testTransactionalPutOnReplicatedRegion.
@Test
public void testTransactionalPutOnReplicatedRegion() throws Exception {
Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM server3 = host.getVM(2);
createRR(new VM[] { server1, server2, server3 });
execute(server1, new SerializableCallable() {
@Override
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.setDistributed(true);
mgr.begin();
mgr.commit();
mgr.begin();
Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER_RR);
CustId custId = new CustId(1);
Customer expectedCustomer = custRegion.get(custId);
assertNull(expectedCustomer);
// Perform a put
CustId custIdOne = new CustId(1);
Customer customerOne = new Customer("name1", "addr1");
custRegion.put(custIdOne, customerOne);
// Rollback the transaction
mgr.rollback();
mgr.begin();
// Verify that the entry is rolled back
expectedCustomer = custRegion.get(custId);
assertNull(expectedCustomer);
// Perform two more puts and a commit
CustId custIdTwo = new CustId(2);
Customer customerTwo = new Customer("name2", "addr2");
CustId custIdThree = new CustId(3);
Customer customerThree = new Customer("name3", "addr3");
custRegion.put(custIdTwo, customerTwo);
custRegion.put(custIdThree, customerThree);
mgr.commit();
mgr.begin();
// Verify data
assertEquals(2, custRegion.size());
assertTrue(custRegion.containsKey(custIdTwo));
assertTrue(custRegion.containsKey(custIdThree));
assertEquals(customerTwo, custRegion.get(custIdTwo));
assertEquals(customerThree, custRegion.get(custIdThree));
// Perform one more put but don't commit
custRegion.put(custIdOne, customerOne);
// Verify data
assertEquals(3, custRegion.size());
assertTrue(custRegion.containsKey(custIdOne));
assertEquals(customerOne, custRegion.get(custIdOne));
mgr.commit();
return null;
}
});
}
use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.
the class RemoteTransactionDUnitTest method testDRFunctionExecution.
@Test
public void testDRFunctionExecution() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore1 = host.getVM(1);
VM datastore2 = host.getVM(2);
class CreateDR extends SerializableCallable {
private final boolean isAccessor;
public CreateDR(boolean isAccessor) {
this.isAccessor = isAccessor;
}
public Object call() throws Exception {
AttributesFactory af = new AttributesFactory();
af.setDataPolicy(isAccessor ? DataPolicy.EMPTY : DataPolicy.REPLICATE);
af.setScope(Scope.DISTRIBUTED_ACK);
af.setConcurrencyChecksEnabled(getConcurrencyChecksEnabled());
getCache().createRegion(CUSTOMER, af.create());
if (isAccessor) {
Region custRegion = getCache().getRegion(CUSTOMER);
for (int i = 0; i < 5; i++) {
CustId custId = new CustId(i);
Customer customer = new Customer("customer" + i, "address" + i);
custRegion.put(custId, customer);
}
}
return null;
}
}
datastore1.invoke(new CreateDR(false));
datastore2.invoke(new CreateDR(false));
accessor.invoke(new CreateDR(true));
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 {
Region custRegion = getGemfireCache().getRegion(CUSTOMER);
TXManagerImpl mgr = getGemfireCache().getTXMgr();
mgr.begin();
FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
assertNotNull(mgr.getTXState());
TXStateProxy tx = mgr.internalSuspend();
assertNull(mgr.getTXState());
getGemfireCache().getLogger().fine("SWAP:callingget");
assertNull("expected null but was:" + custRegion.get(expectedCustId), custRegion.get(expectedCustId));
mgr.internalResume(tx);
mgr.commit();
assertEquals(expectedCustomer, custRegion.get(expectedCustId));
return null;
}
});
datastore1.invoke(new SerializableCallable() {
public Object call() throws Exception {
final Region custRegion = getGemfireCache().getRegion(CUSTOMER);
TXManagerImpl mgr = getGemfireCache().getTXMgr();
mgr.begin();
FunctionService.onRegion(custRegion).execute(new FunctionAdapter() {
@Override
public String getId() {
return "LocalDS";
}
@Override
public void execute(FunctionContext context) {
assertNotNull(getGemfireCache().getTxManager().getTXState());
custRegion.destroy(expectedCustId);
context.getResultSender().lastResult(Boolean.TRUE);
}
}).getResult();
TXStateProxy tx = mgr.internalSuspend();
assertEquals(custRegion.get(expectedCustId), expectedCustomer);
mgr.internalResume(tx);
mgr.commit();
assertNull(custRegion.get(expectedCustId));
return null;
}
});
}
Aggregations