use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class ClientServerFunctionExecutionDUnitTest method allServerExecution_Inline.
public static void allServerExecution_Inline() {
DistributedSystem.setThreadsSocketPolicy(false);
Execution member = FunctionService.onServers(pool);
try {
ResultCollector rs = member.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
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);
}
}
public String getId() {
return getClass().getName();
}
public boolean hasResult() {
return true;
}
});
List resultList = (List) rs.getResult();
assertEquals(Boolean.TRUE, resultList.get(0));
assertEquals(Boolean.TRUE, resultList.get(1));
assertEquals(Boolean.TRUE, resultList.get(2));
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
fail("Test failed after the execute operation asdfasdfa ");
}
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class ClientServerFunctionExecutionDUnitTest method FunctionExecution_Inline_Bug40714.
public static void FunctionExecution_Inline_Bug40714() {
DistributedSystem.setThreadsSocketPolicy(false);
Execution member = FunctionService.onServers(pool);
try {
ResultCollector rs = member.setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
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);
}
}
public String getId() {
return "Function";
}
public boolean hasResult() {
return true;
}
});
List resultList = (List) rs.getResult();
assertEquals(3, resultList.size());
assertEquals(Boolean.TRUE, resultList.get(0));
assertEquals(Boolean.TRUE, resultList.get(1));
assertEquals(Boolean.TRUE, resultList.get(2));
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
fail("Test failed after the execute operation.");
}
}
use of org.apache.geode.cache.execute.FunctionAdapter 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.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method bug41118.
public static void bug41118() {
InternalDistributedSystem ds = new PRFunctionExecutionDUnitTest().getSystem();
assertNotNull(ds);
ds.disconnect();
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
ds = (InternalDistributedSystem) DistributedSystem.connect(props);
DM dm = ds.getDistributionManager();
assertEquals("Distributed System is not loner", true, dm instanceof LonerDistributionManager);
Cache cache = CacheFactory.create(ds);
AttributesFactory factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.PARTITION);
assertNotNull(cache);
Region region = cache.createRegion("PartitonedRegion", factory.create());
for (int i = 0; i < 20; i++) {
region.put("KEY_" + i, "VALUE_" + i);
}
Set<String> keysForGet = new HashSet<String>();
keysForGet.add("KEY_4");
keysForGet.add("KEY_9");
keysForGet.add("KEY_7");
try {
Execution execution = FunctionService.onRegion(region).withFilter(keysForGet).setArguments(Boolean.TRUE);
ResultCollector rc = execution.execute(new FunctionAdapter() {
@Override
public void execute(FunctionContext fc) {
RegionFunctionContext context = (RegionFunctionContext) fc;
Set keys = context.getFilter();
Set keysTillSecondLast = new HashSet();
int setSize = keys.size();
Iterator keysIterator = keys.iterator();
for (int i = 0; i < (setSize - 1); i++) {
keysTillSecondLast.add(keysIterator.next());
}
for (Object k : keysTillSecondLast) {
context.getResultSender().sendResult((Serializable) PartitionRegionHelper.getLocalDataForContext(context).get(k));
}
Object lastResult = keysIterator.next();
context.getResultSender().lastResult((Serializable) PartitionRegionHelper.getLocalDataForContext(context).get(lastResult));
}
@Override
public String getId() {
return getClass().getName();
}
});
rc.getResult();
ds.disconnect();
} catch (Exception e) {
LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
e.printStackTrace();
Assert.fail("Test failed", e);
}
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method testLocalDataContextWithColocation.
/**
* Assert the {@link RegionFunctionContext} yields the proper objects.
*/
@Test
public void testLocalDataContextWithColocation() throws Exception {
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 String rName_top = rName + "_top";
final String rName_colo1 = rName + "_colo1";
final String rName_colo2 = rName + "_colo2";
final SerializableCallable createDataStore = new SerializableCallable("Create datastore for " + rName + " with colocated Regions") {
public Object call() throws Exception {
// create the "top" root region
createRootRegion(rName_top, createColoRegionAttrs(0, 10, null));
// create a root region colocated with top
RegionAttributes colo = createColoRegionAttrs(0, 10, rName_top);
createRootRegion(rName_colo1, colo);
// Create a subregion colocated with top
createRegion(rName_colo2, colo);
return Boolean.TRUE;
}
};
datastore1.invoke(createDataStore);
datastore2.invoke(createDataStore);
accessor.invoke(new SerializableCallable("Create accessor for " + rName + " with colocated Regions and create buckets") {
public Object call() throws Exception {
// create the "top" root region
Region rtop = createRootRegion(rName_top, createColoRegionAttrs(0, 0, null));
// create a root region colocated with top
RegionAttributes colo = createColoRegionAttrs(0, 0, rName_top);
Region rc1 = createRootRegion(rName_colo1, colo);
// Create a subregion colocated with top
Region rc2 = createRegion(rName_colo2, colo);
// Assuming that bucket balancing will create a single bucket (per key)
// in different datastores
rtop.put(key1, key1);
rtop.put(key2, key2);
rc1.put(key1, key1);
rc1.put(key2, key2);
rc2.put(key1, key1);
rc2.put(key2, key2);
return Boolean.TRUE;
}
});
final SerializableCallable assertFuncionContext = new SerializableCallable("Invoke function, assert context with colocation") {
public Object call() throws Exception {
Region r = getRootRegion(rName_top);
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 pr = rContext.getDataSet();
final Map<String, ? extends Region> prColos = PartitionRegionHelper.getColocatedRegions(pr);
final Region ld = PartitionRegionHelper.getLocalDataForContext(rContext);
final Map<String, ? extends Region> ldColos = PartitionRegionHelper.getColocatedRegions(ld);
// Assert the colocation set doesn't contain the "top"
assertFalse(prColos.containsKey(rName_top));
assertFalse(ldColos.containsKey(rName_top));
Region c1 = getRootRegion(rName_colo1);
Region c2 = getRootRegion().getSubregion(rName_colo2);
// assert colocated regions and local forms of colocated regions
{
assertSame(c1, prColos.get(c1.getFullPath()));
Region lc = PartitionRegionHelper.getLocalData(c1);
assertTrue(lc instanceof LocalDataSet);
assertLocalKeySet(key1, lc.keySet());
assertLocalValues(key1, lc.values());
assertLocalEntrySet(key1, lc.entrySet());
}
{
assertSame(c2, prColos.get(c2.getFullPath()));
Region lc = PartitionRegionHelper.getLocalData(c2);
assertTrue(lc instanceof LocalDataSet);
assertLocalEntrySet(key1, lc.entrySet());
assertLocalKeySet(key1, lc.keySet());
assertLocalValues(key1, lc.values());
}
// Assert context's local colocated data
{
Region lc1 = ldColos.get(c1.getFullPath());
assertEquals(c1.getFullPath(), lc1.getFullPath());
assertTrue(lc1 instanceof LocalDataSet);
assertLocalEntrySet(key1, lc1.entrySet());
assertLocalKeySet(key1, lc1.keySet());
assertLocalValues(key1, lc1.values());
}
{
Region lc2 = ldColos.get(c2.getFullPath());
assertEquals(c2.getFullPath(), lc2.getFullPath());
assertTrue(lc2 instanceof LocalDataSet);
assertLocalEntrySet(key1, lc2.entrySet());
assertLocalKeySet(key1, lc2.keySet());
assertLocalValues(key1, lc2.values());
}
// Assert both local forms of the "target" region is local only
assertNull(ld.get(key2));
assertEquals(key1, ld.get(key1));
assertLocalEntrySet(key1, ld.entrySet());
assertLocalKeySet(key1, ld.keySet());
assertLocalValues(key1, ld.values());
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());
}
Aggregations