use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_byName.
/**
* Ensure that the execution is happening all the PR as a whole
*/
@Test
public void testExecutionOnAllNodes_byName() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
final VM datastore3 = host.getVM(3);
getCache();
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());
pa.setTotalNumBuckets(17);
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
// Function function = new TestFunction(true,"TestFunction2");
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION2);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
datastore3.invoke(dataStoreCreate);
Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
DistributedSystem.setThreadsSocketPolicy(false);
final HashSet testKeys = new HashSet();
for (int i = (pr.getTotalNumberOfBuckets() * 3); i > 0; i--) {
testKeys.add("execKey-" + i);
}
int j = 0;
for (Iterator i = testKeys.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
pr.put(i.next(), val);
}
// Assert there is data in each bucket
for (int bid = 0; bid < pr.getTotalNumberOfBuckets(); bid++) {
assertTrue(pr.getBucketKeys(bid).size() > 0);
}
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION2);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector rc1 = dataSet.setArguments(Boolean.TRUE).execute(function.getId());
List l = ((List) rc1.getResult());
LogWriterUtils.getLogWriter().info("PRFunctionExecutionDUnitTest#testExecutionOnAllNodes_byName : Result size :" + l.size() + " Result : " + l);
assertEquals(4, l.size());
for (int i = 0; i < 4; i++) {
assertEquals(Boolean.TRUE, l.iterator().next());
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testRemoteMultiKeyExecution_byInlineFunction.
/**
* Test multi-key remote execution of inline function by a pure accessor ResultCollector =
* DefaultResultCollector haveResults = true;
*/
@Test
public void testRemoteMultiKeyExecution_byInlineFunction() 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());
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);
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(Boolean.TRUE).execute(new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
} else if (context.getArguments() instanceof Boolean) {
context.getResultSender().lastResult(Boolean.TRUE);
}
}
@Override
public String getId() {
return getClass().getName();
}
@Override
public boolean hasResult() {
return true;
}
});
List l = ((List) rs.getResult());
assertEquals(3, l.size());
for (Iterator i = l.iterator(); i.hasNext(); ) {
assertEquals(Boolean.TRUE, i.next());
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testFunctionExecutionException_41779.
@Test
public void testFunctionExecutionException_41779() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore1 = host.getVM(0);
final VM datastore2 = host.getVM(1);
final VM datastore3 = host.getVM(2);
final VM datastore4 = host.getVM(3);
final Cache cache = getCache();
@SuppressWarnings("serial") final SerializableRunnable createFact = new SerializableRunnable("Create PR with Function Factory") {
public void run() {
RegionAttributes<?, ?> ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
@SuppressWarnings("unchecked") AttributesFactory<?, ?> raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegionFactory(raf.create()).create(rName);
Function function = new TestFunctionException();
FunctionService.registerFunction(function);
}
};
// create stores on all VMs including controller
datastore1.invoke(createFact);
datastore2.invoke(createFact);
datastore3.invoke(createFact);
datastore4.invoke(createFact);
createFact.run();
InternalExecution exec = (InternalExecution) FunctionService.onRegion(cache.getRegion(rName));
exec.setWaitOnExceptionFlag(true);
try {
List results = (List) exec.execute(TestFunctionException.ID).getResult();
fail("expected a function exception");
} catch (FunctionException fe) {
// expect exceptions from all VMs
assertEquals("did not get expected number of exceptions: " + fe.getExceptions(), 5, fe.getExceptions().size());
}
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testFunctionExecution.
/**
* Test to validate that the function execution is successful on PR with Loner Distributed System
*/
@Test
public void testFunctionExecution() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore = host.getVM(3);
datastore.invoke(new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
DistributedSystem ds = getSystem(props);
assertNotNull(ds);
ds.disconnect();
ds = getSystem(props);
cache = CacheFactory.create(ds);
assertNotNull(cache);
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
Region pr = cache.createRegion(rName, raf.create());
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION1);
FunctionService.registerFunction(function);
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector result = dataSet.setArguments(Boolean.TRUE).withFilter(testKeysSet).execute(function);
System.out.println("KBKBKB : Result I got : " + result.getResult());
return Boolean.TRUE;
}
});
}
use of org.apache.geode.internal.cache.PartitionAttributesImpl in project geode by apache.
the class PRFunctionExecutionDUnitTest method testHAFunctionExecution.
@Test
public void testHAFunctionExecution() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM datastore = host.getVM(3);
getCache();
datastore.invoke(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);
Region pr = getCache().createRegion(rName, raf.create());
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
Function function = new TestFunction(false, TestFunction.TEST_FUNCTION10);
try {
FunctionService.registerFunction(function);
fail("It should have failed with Function attributes don't match");
} catch (Exception expected) {
expected.printStackTrace();
assertTrue(expected.getMessage().contains("For Functions with isHA true, hasResult must also be true."));
}
try {
Execution dataSet = FunctionService.onRegion(pr);
dataSet.withFilter(testKeysSet).setArguments(testKey).execute(function);
fail("It should have failed with Function attributes don't match");
} catch (Exception expected) {
expected.printStackTrace();
assertTrue(expected.getMessage().contains("For Functions with isHA true, hasResult must also be true."));
}
return Boolean.TRUE;
}
});
}
Aggregations