use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.
the class TestFunction method execute4.
public void execute4(FunctionContext context) {
if (context instanceof RegionFunctionContext) {
RegionFunctionContext prContext = (RegionFunctionContext) context;
prContext.getDataSet().getCache().getLogger().info("Executing function : TestFunction4-7.execute " + prContext);
if (prContext.getArguments() instanceof Boolean) {
/* return prContext.getArguments(); */
if (hasResult())
prContext.getResultSender().lastResult((Serializable) prContext.getArguments());
} else if (prContext.getArguments() instanceof String) {
String key = (String) prContext.getArguments();
/* return (Serializable)PartitionRegionHelper.getLocalDataForContext(prContext).get(key); */
prContext.getResultSender().lastResult((Serializable) PartitionRegionHelper.getLocalDataForContext(prContext).get(key));
} else if (prContext.getArguments() instanceof Set) {
Set origKeys = (Set) prContext.getArguments();
ArrayList vals = new ArrayList();
for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
Object val = PartitionRegionHelper.getLocalDataForContext(prContext).get(i.next());
if (val != null) {
vals.add(val);
}
}
// prContext.getResultSender().sendResult(vals);
if (hasResult())
prContext.getResultSender().lastResult(vals);
} else if (prContext.getArguments() instanceof HashMap) {
HashMap putData = (HashMap) prContext.getArguments();
for (Iterator i = putData.entrySet().iterator(); i.hasNext(); ) {
Map.Entry me = (Map.Entry) i.next();
prContext.getDataSet().put(me.getKey(), me.getValue());
}
// prContext.getResultSender().sendResult(Boolean.TRUE);
if (hasResult())
prContext.getResultSender().lastResult(Boolean.TRUE);
} else {
// prContext.getResultSender().sendResult(Boolean.FALSE);
if (hasResult())
prContext.getResultSender().lastResult(Boolean.FALSE);
}
} else {
// context.getResultSender().sendResult(Boolean.FALSE);
if (hasResult())
context.getResultSender().lastResult(Boolean.FALSE);
}
}
use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.
the class DistributedRegionFunction method execute.
@Override
public void execute(FunctionContext context) {
RegionFunctionContext rcontext = (RegionFunctionContext) context;
Region<Object, Object> region = rcontext.getDataSet();
InternalDistributedSystem sys = InternalDistributedSystem.getConnectedInstance();
sys.getLogWriter().fine("DistributedRegionFunction#execute( " + rcontext + " )");
Assert.assertTrue(region.getAttributes().getDataPolicy().withStorage());
Assert.assertTrue(region.getAttributes().getDataPolicy() != DataPolicy.NORMAL);
Assert.assertTrue(rcontext.getFilter().size() == 20);
long startTime = System.currentTimeMillis();
// the body itself
if (Boolean.TRUE.equals(rcontext.getArguments())) {
// do not close cache in retry
if (!rcontext.isPossibleDuplicate()) {
sys.disconnect();
throw new CacheClosedException("Throwing CacheClosedException " + "to simulate failover during function exception");
}
} else {
WaitCriterion wc = new WaitCriterion() {
String excuse;
public boolean done() {
return false;
}
public String description() {
return excuse;
}
};
Wait.waitForCriterion(wc, 12000, 500, false);
}
long endTime = System.currentTimeMillis();
// intentionally doing region operation to cause cacheClosedException
region.put("execKey-201", new Integer(201));
if (rcontext.isPossibleDuplicate()) {
// Below operation is done when the
// function is reexecuted
region.put("execKey-202", new Integer(202));
region.put("execKey-203", new Integer(203));
}
sys.getLogWriter().fine("Time wait for Function Execution = " + (endTime - startTime));
for (int i = 0; i < 5000; i++) {
context.getResultSender().sendResult(Boolean.TRUE);
}
context.getResultSender().lastResult(Boolean.TRUE);
}
use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.
the class TestFunction method executeWithThrowException.
private void executeWithThrowException(FunctionContext context) {
DistributedSystem ds = InternalDistributedSystem.getAnyInstance();
RegionFunctionContext rfContext = (RegionFunctionContext) context;
LogWriter logger = ds.getLogWriter();
logger.fine("Executing executeWithThrowException in TestFunction on Member : " + ds.getDistributedMember() + "with Context : " + context);
if (context.getArguments() instanceof Boolean) {
logger.fine("MyFunctionExecutionException Exception is intentionally thrown");
throw new MyFunctionExecutionException("I have been thrown from TestFunction");
} else if (rfContext.getArguments() instanceof Set) {
Set origKeys = (Set) rfContext.getArguments();
for (Iterator i = origKeys.iterator(); i.hasNext(); ) {
Region r = PartitionRegionHelper.getLocalDataForContext(rfContext);
Object key = i.next();
Object val = r.get(key);
if (val != null) {
throw new MyFunctionExecutionException("I have been thrown from TestFunction");
}
}
} else {
logger.fine("Result sent back :" + Boolean.FALSE);
rfContext.getResultSender().lastResult(Boolean.FALSE);
}
}
use of org.apache.geode.cache.execute.RegionFunctionContext in project geode by apache.
the class TestFunction method execute8.
public void execute8(FunctionContext context) {
if (context instanceof RegionFunctionContext) {
RegionFunctionContext rfContext = (RegionFunctionContext) context;
rfContext.getDataSet().getCache().getLogger().info("Executing function : TestFunction8.execute " + rfContext);
if (rfContext.getArguments() instanceof Boolean) {
/* return rfContext.getArguments(); */
// rfContext.getResultSender().sendResult(rfContext.getArguments());
rfContext.getResultSender().lastResult((Serializable) rfContext.getArguments());
} else if (rfContext.getArguments() instanceof String) {
String op = (String) rfContext.getArguments();
if (op.equals("DELETE")) {
Region r = rfContext.getDataSet();
Set s = rfContext.getFilter();
if (s == null) {
/* return Boolean.FALSE; */
// rfContext.getResultSender().sendResult(Boolean.FALSE);
rfContext.getResultSender().lastResult(Boolean.FALSE);
}
Iterator itr = s.iterator();
while (itr.hasNext()) {
r.destroy(itr.next());
}
/* return Boolean.TRUE; */
// rfContext.getResultSender().sendResult(Boolean.TRUE);
rfContext.getResultSender().lastResult(Boolean.TRUE);
} else if (op.equals("GET")) {
Region r = rfContext.getDataSet();
Set s = rfContext.getFilter();
if (s == null) {
/* return Boolean.FALSE; */
rfContext.getResultSender().lastResult(Boolean.FALSE);
}
Iterator itr = s.iterator();
ArrayList vals = new ArrayList();
while (itr.hasNext()) {
vals.add(r.get(itr.next()));
}
/* return vals; */
rfContext.getResultSender().lastResult(vals);
}
/* return Boolean.FALSE; */
rfContext.getResultSender().lastResult(Boolean.FALSE);
} else {
/* return Boolean.FALSE; */
rfContext.getResultSender().lastResult(Boolean.FALSE);
}
} else {
/* return Boolean.FALSE; */
context.getResultSender().lastResult(Boolean.FALSE);
}
}
use of org.apache.geode.cache.execute.RegionFunctionContext 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());
}
Aggregations