use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRTransactionDUnitTest method basicPRTXInFunction.
/**
* Test all the basic functionality of colocated transactions. This method invokes
* {@link MyTransactionFunction} and tells it what to test, using different arguments.
*
* @param redundantBuckets redundant buckets for colocated PRs
* @param populateData if false tests are carried out on empty colocated PRs
*/
protected void basicPRTXInFunction(int redundantBuckets, boolean populateData) {
if (populateData) {
createPopulateAndVerifyCoLocatedPRs(redundantBuckets);
} else {
createColocatedPRs(redundantBuckets);
}
SerializableCallable registerFunction = new SerializableCallable("register Fn") {
public Object call() throws Exception {
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
return Boolean.TRUE;
}
};
dataStore1.invoke(registerFunction);
dataStore2.invoke(registerFunction);
dataStore3.invoke(registerFunction);
accessor.invoke(new SerializableCallable("run function") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
PartitionedRegion orderpr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
CustId custId = new CustId(2);
Customer newCus = new Customer("foo", "bar");
Order order = new Order("fooOrder");
OrderId orderId = new OrderId(22, custId);
ArrayList args = new ArrayList();
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
Execution e = FunctionService.onRegion(pr);
Set filter = new HashSet();
// test transaction non-coLocated operations
filter.clear();
args.clear();
args.add(new Integer(VERIFY_NON_COLOCATION));
LogWriterUtils.getLogWriter().info("VERIFY_NON_COLOCATION");
args.add(custId);
args.add(newCus);
args.add(orderId);
args.add(order);
filter.add(custId);
try {
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
fail("Expected exception was not thrown");
} catch (FunctionException fe) {
LogWriterUtils.getLogWriter().info("Caught Expected exception");
if (fe.getCause() instanceof TransactionDataNotColocatedException) {
} else {
throw new TestException("Expected to catch FunctionException with cause TransactionDataNotColocatedException" + " but cause is " + fe.getCause(), fe.getCause());
}
}
// verify that the transaction modifications are applied
args.set(0, new Integer(VERIFY_TX));
LogWriterUtils.getLogWriter().info("VERIFY_TX");
orderpr.put(orderId, order);
assertNotNull(orderpr.get(orderId));
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
assertTrue("Unexpected customer value after commit", newCus.equals(pr.get(custId)));
Order commitedOrder = (Order) orderpr.get(orderId);
assertTrue("Unexpected order value after commit. Expected:" + order + " Found:" + commitedOrder, order.equals(commitedOrder));
// verify conflict detection
args.set(0, new Integer(VERIFY_TXSTATE_CONFLICT));
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify that the transaction is rolled back
args.set(0, new Integer(VERIFY_ROLLBACK));
LogWriterUtils.getLogWriter().info("VERIFY_ROLLBACK");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify destroy
args.set(0, new Integer(VERIFY_DESTROY));
LogWriterUtils.getLogWriter().info("VERIFY_DESTROY");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
// verify invalidate
args.set(0, new Integer(VERIFY_INVALIDATE));
LogWriterUtils.getLogWriter().info("VERIFY_INVALIDATE");
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
return Boolean.TRUE;
}
});
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRTransactionDUnitTest method testRepeatableRead.
@Test
public void testRepeatableRead() throws Exception {
createColocatedPRs(1);
SerializableCallable registerFunction = new SerializableCallable("register Fn") {
public Object call() throws Exception {
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
return Boolean.TRUE;
}
};
dataStore1.invoke(registerFunction);
dataStore2.invoke(registerFunction);
dataStore3.invoke(registerFunction);
accessor.invoke(new SerializableCallable("run function") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
PartitionedRegion orderpr = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
CustId custId = new CustId(2);
Customer newCus = new Customer("foo", "bar");
Order order = new Order("fooOrder");
OrderId orderId = new OrderId(22, custId);
ArrayList args = new ArrayList();
Function txFunction = new MyTransactionFunction();
FunctionService.registerFunction(txFunction);
Execution e = FunctionService.onRegion(pr);
Set filter = new HashSet();
filter.clear();
args.clear();
args.add(new Integer(VERIFY_REP_READ));
LogWriterUtils.getLogWriter().info("VERIFY_REP_READ");
args.add(custId);
args.add(newCus);
args.add(orderId);
args.add(order);
filter.add(custId);
e.withFilter(filter).setArguments(args).execute(txFunction.getId()).getResult();
return null;
}
});
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRFunctionExecutionTimeOutDUnitTest method testRemoteMultiKeyExecution_byName.
/**
* Test multi-key remote execution by a pure accessor.Then test it using timeout and multiple
* getResult.
*
* @throws Exception
*/
@Test
public void testRemoteMultiKeyExecution_byName() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(3);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
final HashSet testKeysSet = new HashSet();
for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
testKeysSet.add("execKey-" + i);
}
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
try {
dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
} catch (Exception expected) {
assertTrue(expected.getMessage(), expected.getMessage().contains("No target node found for KEY"));
}
int j = 0;
HashSet origVals = new HashSet();
for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
origVals.add(val);
pr.put(i.next(), val);
}
ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
List l = ((List) rs.getResult());
assertEquals(3, l.size());
for (Iterator i = l.iterator(); i.hasNext(); ) {
assertEquals(Boolean.TRUE, i.next());
}
try {
rs.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
// DefaultResultCollector rc2 = new DefaultResultCollector();
ResultCollector rc2 = dataSet.withFilter(testKeysSet).setArguments(testKeysSet).execute(function.getId());
List l2 = ((List) rc2.getResult());
assertEquals(3, l2.size());
HashSet foundVals = new HashSet();
for (Iterator i = l2.iterator(); i.hasNext(); ) {
ArrayList subL = (ArrayList) i.next();
assertTrue(subL.size() > 0);
for (Iterator subI = subL.iterator(); subI.hasNext(); ) {
assertTrue(foundVals.add(subI.next()));
}
}
assertEquals(origVals, foundVals);
try {
rc2.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
ResultCollector rst = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(function.getId());
try {
rst.getResult(1000, TimeUnit.MILLISECONDS);
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
}
try {
rst.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
ResultCollector rct2 = dataSet.withFilter(testKeysSet).setArguments(testKeysSet).execute(function.getId());
try {
rct2.getResult(1000, TimeUnit.MILLISECONDS);
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("All results not recieved in time provided."));
}
try {
rct2.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRFunctionExecutionTimeOutDUnitTest method testRemoteMultiKeyExecution_timeout.
/**
* Test multi-key remote execution by a pure accessor.
*
* @throws Exception
*/
@Test
public void testRemoteMultiKeyExecution_timeout() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(3);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
final HashSet testKeysSet = new HashSet();
for (int i = (pr.getTotalNumberOfBuckets() * 2); i > 0; i--) {
testKeysSet.add("execKey-" + i);
}
DistributedSystem.setThreadsSocketPolicy(false);
Function function = new TestFunction(true, TEST_FUNCTION_TIMEOUT);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
int j = 0;
HashSet origVals = new HashSet();
for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
origVals.add(val);
pr.put(i.next(), val);
}
ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments("TestingTimeOut").execute(function.getId());
List l = ((List) rs.getResult(8000, TimeUnit.MILLISECONDS));
// this test may fail..but rarely
assertEquals(3, l.size());
ResultCollector rst = dataSet.withFilter(testKeysSet).setArguments("TestingTimeOut").execute(function.getId());
rst.getResult(8000, TimeUnit.MILLISECONDS);
assertEquals(3, l.size());
try {
rs.getResult();
fail("Did not get the expected exception.");
} catch (FunctionException fe) {
assertTrue(fe.getMessage(), fe.getMessage().contains("Result already collected"));
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.cache.execute.Execution in project geode by apache.
the class PRFunctionExecutionWithResultSenderDUnitTest method testlonerSystem_Bug41832.
@Test
public void testlonerSystem_Bug41832() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore = host.getVM(3);
Object o = datastore.invoke(new SerializableCallable("Create region") {
public Object call() throws Exception {
// creates loner cache
createLonerCache();
AttributesFactory factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.EMPTY);
Region region = getCache().createRegion(rName, factory.create());
Function function = new TestFunction(true, TEST_FUNCTION2);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(region);
try {
ResultCollector rc = dataSet.setArguments(Boolean.TRUE).execute(function.getId());
Object o = rc.getResult();
fail("Expected Function Exception");
} catch (Exception expected) {
assertTrue(expected.getMessage().startsWith("No Replicated Region found for executing function"));
return Boolean.TRUE;
}
return Boolean.FALSE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations