use of org.apache.geode.cache.execute.FunctionContext 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());
}
use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.
the class ExecuteFunction65 method cmdExecute.
@Override
public void cmdExecute(Message clientMessage, ServerConnection servConn, long start) throws IOException {
Object function = null;
Object args = null;
MemberMappedArgument memberMappedArg = null;
byte hasResult = 0;
byte functionState = 0;
boolean isReexecute = false;
try {
functionState = clientMessage.getPart(0).getSerializedForm()[0];
if (functionState == AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE_REEXECUTE) {
functionState = AbstractExecution.HA_HASRESULT_NO_OPTIMIZEFORWRITE;
isReexecute = true;
} else if (functionState == AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE_REEXECUTE) {
functionState = AbstractExecution.HA_HASRESULT_OPTIMIZEFORWRITE;
isReexecute = true;
}
if (functionState != 1) {
hasResult = (byte) ((functionState & 2) - 1);
} else {
hasResult = functionState;
}
if (hasResult == 1) {
servConn.setAsTrue(REQUIRES_RESPONSE);
servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
}
function = clientMessage.getPart(1).getStringOrObject();
args = clientMessage.getPart(2).getObject();
Part part = clientMessage.getPart(3);
if (part != null) {
memberMappedArg = (MemberMappedArgument) part.getObject();
}
} catch (ClassNotFoundException exception) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), exception);
if (hasResult == 1) {
writeChunkedException(clientMessage, exception, servConn);
servConn.setAsTrue(RESPONDED);
return;
}
}
if (function == null) {
final String message = LocalizedStrings.ExecuteFunction_THE_INPUT_FUNCTION_FOR_THE_EXECUTE_FUNCTION_REQUEST_IS_NULL.toLocalizedString();
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
}
// Execute function on the cache
try {
Function functionObject = null;
if (function instanceof String) {
functionObject = FunctionService.getFunction((String) function);
if (functionObject == null) {
final String message = LocalizedStrings.ExecuteFunction_FUNCTION_NAMED_0_IS_NOT_REGISTERED.toLocalizedString(function);
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
} else {
byte functionStateOnServerSide = AbstractExecution.getFunctionState(functionObject.isHA(), functionObject.hasResult(), functionObject.optimizeForWrite());
if (logger.isDebugEnabled()) {
logger.debug("Function State on server side: {} on client: {}", functionStateOnServerSide, functionState);
}
if (functionStateOnServerSide != functionState) {
String message = LocalizedStrings.FunctionService_FUNCTION_ATTRIBUTE_MISMATCH_CLIENT_SERVER.toLocalizedString(function);
logger.warn("{}: {}", servConn.getName(), message);
sendError(hasResult, clientMessage, message, servConn);
return;
}
}
} else {
functionObject = (Function) function;
}
FunctionStats stats = FunctionStats.getFunctionStats(functionObject.getId());
this.securityService.authorizeDataWrite();
// check if the caller is authorized to do this operation on server
AuthorizeRequest authzRequest = servConn.getAuthzRequest();
ExecuteFunctionOperationContext executeContext = null;
if (authzRequest != null) {
executeContext = authzRequest.executeFunctionAuthorize(functionObject.getId(), null, null, args, functionObject.optimizeForWrite());
}
ChunkedMessage m = servConn.getFunctionResponseMessage();
m.setTransactionId(clientMessage.getTransactionId());
ResultSender resultSender = new ServerToClientFunctionResultSender65(m, MessageType.EXECUTE_FUNCTION_RESULT, servConn, functionObject, executeContext);
InternalDistributedMember localVM = (InternalDistributedMember) servConn.getCache().getDistributedSystem().getDistributedMember();
FunctionContext context = null;
if (memberMappedArg != null) {
context = new FunctionContextImpl(functionObject.getId(), memberMappedArg.getArgumentsForMember(localVM.getId()), resultSender, isReexecute);
} else {
context = new FunctionContextImpl(functionObject.getId(), args, resultSender, isReexecute);
}
HandShake handShake = (HandShake) servConn.getHandshake();
int earlierClientReadTimeout = handShake.getClientReadTimeout();
handShake.setClientReadTimeout(0);
try {
long startExecution = stats.startTime();
stats.startFunctionExecution(functionObject.hasResult());
if (logger.isDebugEnabled()) {
logger.debug("Executing Function on Server: {} with context: {}", servConn, context);
}
InternalCache cache = servConn.getCache();
HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
if (functionObject.optimizeForWrite() && cache != null && hmm.getState().isCritical() && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
Set<DistributedMember> sm = Collections.singleton((DistributedMember) cache.getMyId());
Exception e = new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { functionObject.getId(), sm }), sm);
sendException(hasResult, clientMessage, e.getMessage(), servConn, e);
return;
}
functionObject.execute(context);
if (!((ServerToClientFunctionResultSender65) resultSender).isLastResultReceived() && functionObject.hasResult()) {
throw new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(functionObject.getId()));
}
stats.endFunctionExecution(startExecution, functionObject.hasResult());
} catch (FunctionException functionException) {
stats.endFunctionExecutionWithException(functionObject.hasResult());
throw functionException;
} catch (Exception exception) {
stats.endFunctionExecutionWithException(functionObject.hasResult());
throw new FunctionException(exception);
} finally {
handShake.setClientReadTimeout(earlierClientReadTimeout);
}
} catch (IOException ioException) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), ioException);
String message = LocalizedStrings.ExecuteFunction_SERVER_COULD_NOT_SEND_THE_REPLY.toLocalizedString();
sendException(hasResult, clientMessage, message, servConn, ioException);
} catch (InternalFunctionInvocationTargetException internalfunctionException) {
// 2> in case of HA member departed
if (logger.isDebugEnabled()) {
logger.debug(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, new Object[] { function }), internalfunctionException);
}
final String message = internalfunctionException.getMessage();
sendException(hasResult, clientMessage, message, servConn, internalfunctionException);
} catch (Exception e) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ExecuteFunction_EXCEPTION_ON_SERVER_WHILE_EXECUTIONG_FUNCTION_0, function), e);
final String message = e.getMessage();
sendException(hasResult, clientMessage, message, servConn, e);
}
}
use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.
the class MemberFunctionExecutor method executeFunction.
@SuppressWarnings("unchecked")
private ResultCollector executeFunction(final Function function, ResultCollector resultCollector) {
final DM dm = this.ds.getDistributionManager();
final Set dest = new HashSet(this.members);
if (dest.isEmpty()) {
throw new FunctionException(LocalizedStrings.MemberFunctionExecutor_NO_MEMBER_FOUND_FOR_EXECUTING_FUNCTION_0.toLocalizedString(function.getId()));
}
validateExecution(function, dest);
setExecutionNodes(dest);
final InternalDistributedMember localVM = this.ds.getDistributionManager().getDistributionManagerId();
final LocalResultCollector<?, ?> localRC = getLocalResultCollector(function, resultCollector);
boolean remoteOnly = false;
boolean localOnly = false;
if (!dest.contains(localVM)) {
remoteOnly = true;
}
if (dest.size() == 1 && dest.contains(localVM)) {
localOnly = true;
}
final MemberFunctionResultSender resultSender = new MemberFunctionResultSender(dm, localRC, function, localOnly, remoteOnly, sender);
if (dest.contains(localVM)) {
// if member is local VM
dest.remove(localVM);
final FunctionContext context = new FunctionContextImpl(function.getId(), getArgumentsForMember(localVM.getId()), resultSender);
boolean isTx = false;
InternalCache cache = GemFireCacheImpl.getInstance();
if (cache != null) {
isTx = cache.getTxManager().getTXState() == null ? false : true;
}
executeFunctionOnLocalNode(function, context, resultSender, dm, isTx);
}
if (!dest.isEmpty()) {
HashMap<InternalDistributedMember, Object> memberArgs = new HashMap<InternalDistributedMember, Object>();
Iterator<DistributedMember> iter = dest.iterator();
while (iter.hasNext()) {
InternalDistributedMember recip = (InternalDistributedMember) iter.next();
memberArgs.put(recip, getArgumentsForMember(recip.getId()));
}
Assert.assertTrue(memberArgs.size() == dest.size());
MemberFunctionResultWaiter resultReciever = new MemberFunctionResultWaiter(this.ds, localRC, function, memberArgs, dest, resultSender);
ResultCollector reply = resultReciever.getFunctionResultFrom(dest, function, this);
return reply;
}
return localRC;
}
use of org.apache.geode.cache.execute.FunctionContext in project geode by apache.
the class PRClientServerRegionFunctionExecutionDUnitTest 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.FunctionContext in project geode by apache.
the class PRClientServerRegionFunctionExecutionDUnitTest method serverMultiKeyExecution_FunctionInvocationTargetException.
public static void serverMultiKeyExecution_FunctionInvocationTargetException() {
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);
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);
}
ResultCollector rc1 = null;
try {
rc1 = dataSet.withFilter(testKeysSet).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {
@Override
public void execute(FunctionContext context) {
if (((RegionFunctionContext) context).isPossibleDuplicate()) {
context.getResultSender().lastResult(new Integer(retryCount));
return;
}
if (context.getArguments() instanceof Boolean) {
throw new FunctionInvocationTargetException("I have been thrown from TestFunction");
}
}
@Override
public String getId() {
return getClass().getName();
}
@Override
public boolean hasResult() {
return true;
}
});
List list = (ArrayList) rc1.getResult();
assertEquals(list.get(0), 0);
} catch (Throwable e) {
e.printStackTrace();
Assert.fail("This is not expected Exception", e);
}
}
Aggregations