use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRClientServerRegionFunctionExecutionSelectorNoSingleHopDUnitTest method serverSingleKeyExecution_Inline.
public static void serverSingleKeyExecution_Inline() {
Region region = cache.getRegion(PartitionedRegionName);
assertNotNull(region);
final String testKey = "execKey";
final Set testKeysSet = new HashSet();
testKeysSet.add(testKey);
DistributedSystem.setThreadsSocketPolicy(false);
Execution dataSet = FunctionService.onRegion(region);
try {
cache.getLogger().info("<ExpectedException action=add>" + "No target node found for KEY = " + "|Server could not send the reply" + "|Unexpected exception during" + "</ExpectedException>");
dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
}
context.getResultSender().lastResult("Failure");
}
public String getId() {
return getClass().getName();
}
public boolean hasResult() {
return true;
}
});
} catch (Exception expected) {
LogWriterUtils.getLogWriter().fine("Exception occurred : " + expected.getMessage());
assertTrue(expected.getMessage().contains("No target node found for KEY = " + testKey) || expected.getMessage().startsWith("Server could not send the reply") || expected.getMessage().startsWith("Unexpected exception during"));
} finally {
cache.getLogger().info("<ExpectedException action=remove>" + "No target node found for KEY = " + "|Server could not send the reply" + "|Unexpected exception during" + "</ExpectedException>");
}
region.put(testKey, new Integer(1));
try {
ResultCollector rs = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
} else {
context.getResultSender().lastResult("Failure");
}
}
public String getId() {
return getClass().getName();
}
public boolean hasResult() {
return true;
}
});
assertEquals("Failure", ((List) rs.getResult()).get(0));
ResultCollector rs2 = dataSet.withFilter(testKeysSet).setArguments(testKey).execute(new FunctionAdapter() {
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Success");
} else {
context.getResultSender().lastResult("Failure");
}
}
public String getId() {
return getClass().getName();
}
public boolean hasResult() {
return true;
}
});
assertEquals("Success", ((List) rs2.getResult()).get(0));
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
Assert.fail("Test failed after the put operation", ex);
}
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRClientServerRegionFunctionExecutionSingleHopDUnitTest method serverMultiKeyExecution_Inline.
public static void serverMultiKeyExecution_Inline() {
Region region = cache.getRegion(PartitionedRegionName);
assertNotNull(region);
final HashSet testKeysSet = new HashSet();
for (int i = (totalNumBuckets.intValue() * 2); i > 0; i--) {
testKeysSet.add("execKey-" + i);
}
DistributedSystem.setThreadsSocketPolicy(false);
Execution dataSet = FunctionService.onRegion(region);
try {
int j = 0;
HashSet origVals = new HashSet();
for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
Integer val = new Integer(j++);
origVals.add(val);
region.put(i.next(), val);
}
List l = null;
ResultCollector rc1 = 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;
}
});
l = ((List) rc1.getResult());
LogWriterUtils.getLogWriter().info("Result size : " + l.size());
assertEquals(3, l.size());
for (Iterator i = l.iterator(); i.hasNext(); ) {
assertEquals(Boolean.TRUE, i.next());
}
} catch (Exception e) {
LogWriterUtils.getLogWriter().info("Exception : " + e.getMessage());
e.printStackTrace();
fail("Test failed after the put operation");
}
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class ClientServerFunctionExecutionDUnitTest method serverExecution_Inline.
public static void serverExecution_Inline() {
DistributedSystem.setThreadsSocketPolicy(false);
Execution member = FunctionService.onServer(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;
}
});
assertEquals(Boolean.TRUE, ((List) rs.getResult()).get(0));
} catch (Exception ex) {
ex.printStackTrace();
LogWriterUtils.getLogWriter().info("Exception : ", ex);
fail("Test failed after the execute operation nn TRUE");
}
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRColocationDUnitTest method executeFunction.
public static void executeFunction() {
Function inlineFunction = new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
RegionFunctionContext rfContext = (RegionFunctionContext) context;
Region r = rfContext.getDataSet();
if (r.getName().equals(CustomerPartitionedRegionName)) {
Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(2, map.size());
rfContext.getResultSender().sendResult(map.size());
map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(3, map.size());
rfContext.getResultSender().lastResult(map.size());
} else if (r.getName().equals(OrderPartitionedRegionName)) {
Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(2, map.size());
rfContext.getResultSender().sendResult(map.size());
map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(3, map.size());
rfContext.getResultSender().lastResult(map.size());
} else if (r.getName().equals(ShipmentPartitionedRegionName)) {
Map map = ColocationHelper.getColocatedLocalDataSetsForBuckets((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(2, map.size());
rfContext.getResultSender().sendResult(map.size());
map = ColocationHelper.constructAndGetAllColocatedLocalDataSet((PartitionedRegion) r, new HashSet<Integer>());
assertEquals(3, map.size());
rfContext.getResultSender().lastResult(map.size());
}
}
@Override
public String getId() {
return "inlineFunction";
}
@Override
public boolean hasResult() {
return true;
}
@Override
public boolean isHA() {
return false;
}
@Override
public boolean optimizeForWrite() {
return false;
}
};
PartitionedRegion prForCustomer = (PartitionedRegion) basicGetCache().getRegion(CustomerPartitionedRegionName);
final Set testKeysSet = new HashSet();
DummyKeyBasedRoutingResolver dummy = new DummyKeyBasedRoutingResolver(10);
testKeysSet.add(dummy);
Execution dataSet = FunctionService.onRegion(prForCustomer);
ResultCollector rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
assertEquals(2, ((List) rc.getResult()).size());
PartitionedRegion prForOrder = (PartitionedRegion) basicGetCache().getRegion(OrderPartitionedRegionName);
dataSet = FunctionService.onRegion(prForOrder);
rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
assertEquals(2, ((List) rc.getResult()).size());
PartitionedRegion prForShipment = (PartitionedRegion) basicGetCache().getRegion(ShipmentPartitionedRegionName);
dataSet = FunctionService.onRegion(prForShipment);
rc = dataSet.withFilter(testKeysSet).execute(inlineFunction);
assertEquals(2, ((List) rc.getResult()).size());
}
use of org.apache.geode.cache.execute.FunctionAdapter in project geode by apache.
the class PRFunctionExecutionDUnitTest method testBug40714.
@Test
public void testBug40714() 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());
FunctionService.registerFunction(new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
if (context.getArguments() instanceof String) {
context.getResultSender().lastResult("Failure");
} else if (context.getArguments() instanceof Boolean) {
context.getResultSender().lastResult(Boolean.FALSE);
}
}
@Override
public String getId() {
return "Function";
}
@Override
public boolean hasResult() {
return true;
}
});
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 "Function";
}
@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++) {
Boolean res = (Boolean) iterator.next();
assertEquals(Boolean.TRUE, res);
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations