use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method testRemoteSingleKeyExecution_byInlineFunction.
/**
* Test remote execution of inline function by a pure accessor
*/
@Test
public void testRemoteSingleKeyExecution_byInlineFunction() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(2);
final VM datastore = host.getVM(3);
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;
}
});
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);
getCache().createRegion(rName, raf.create());
return Boolean.TRUE;
}
});
Object o = accessor.invoke(new SerializableCallable("Create data, invoke exectuable") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
DistributedSystem.setThreadsSocketPolicy(false);
Execution dataSet = FunctionService.onRegion(pr);
pr.put(testKey, new Integer(1));
ResultCollector rs1 = 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;
}
});
assertEquals(Boolean.TRUE, ((List) rs1.getResult()).get(0));
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method testLocalDataContext.
/**
* Assert the {@link RegionFunctionContext} yields the proper objects.
*/
@Test
public void testLocalDataContext() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(1);
final VM datastore1 = host.getVM(2);
final VM datastore2 = host.getVM(3);
getCache();
final Integer key1 = new Integer(1);
final Integer key2 = new Integer(2);
final SerializableCallable createDataStore = new SerializableCallable("Create datastore for " + rName) {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
};
datastore1.invoke(createDataStore);
datastore2.invoke(createDataStore);
accessor.invoke(new SerializableCallable("Create accessor for " + rName + ", create buckets") {
public Object call() throws Exception {
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0);
Region pr = getCache().createRegion(rName, ra);
// Assuming that bucket balancing will create a single bucket (per key)
// in different datastores
pr.put(key1, key1);
pr.put(key2, key2);
return Boolean.TRUE;
}
});
final SerializableCallable assertFuncionContext = new SerializableCallable("Invoke function, assert context") {
public Object call() throws Exception {
Region r = getCache().getRegion(rName);
Function f = new FunctionAdapter() {
// @Override
@Override
public void execute(FunctionContext context) {
RegionFunctionContext rContext = (RegionFunctionContext) context;
assertEquals(Collections.singleton(key1), rContext.getFilter());
assertTrue(PartitionRegionHelper.isPartitionedRegion(rContext.getDataSet()));
final Region ld = PartitionRegionHelper.getLocalDataForContext(rContext);
assertTrue(PartitionRegionHelper.getColocatedRegions(ld).isEmpty());
// Assert the data is local only
assertNull(ld.get(key2));
assertEquals(key1, ld.get(key1));
assertLocalKeySet(key1, ld.keySet());
assertLocalValues(key1, ld.values());
assertLocalEntrySet(key1, ld.entrySet());
context.getResultSender().lastResult(Boolean.TRUE);
}
// @Override
@Override
public String getId() {
return getClass().getName();
}
};
ArrayList res = (ArrayList) FunctionService.onRegion(r).withFilter(Collections.singleton(key1)).execute(f).getResult();
assertEquals(1, res.size());
return res.get(0);
}
};
assertTrue(((Boolean) accessor.invoke(assertFuncionContext)).booleanValue());
assertTrue(((Boolean) datastore1.invoke(assertFuncionContext)).booleanValue());
assertTrue(((Boolean) datastore2.invoke(assertFuncionContext)).booleanValue());
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_byInlineFunction.
/**
* Ensure that the execution of inline function is happening all the PR as a whole
*/
@Test
public void testExecutionOnAllNodes_byInlineFunction() 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());
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);
}
Execution dataSet = FunctionService.onRegion(pr);
ResultCollector rc1 = dataSet.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) rc1.getResult());
LogWriterUtils.getLogWriter().info("PRFunctionExecutionDUnitTest#testExecutionOnAllNodes_byName : Result size :" + l.size() + " Result : " + l);
assertEquals(4, l.size());
Iterator iterator = l.iterator();
for (int i = 0; i < 4; i++) {
assertEquals(Boolean.TRUE, iterator.next());
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations