Running the Function
In this section you create an Execution
object and use its methods to define and run the function. To run a function with high availability, you call getResult
from the results collector returned from the execute
method.
Configuring and Running a Function
You specify the members that run the function and, optionally, the data set over which the functions run.
- Servers. Execute the function in a single server or a set of servers, specified by the server pool. To specify data sets for this type of function, pass arguments in to the function.
- Data set. Specify a region and possibly a set of keys on which to run.
In every client where you want to execute the function and process the results:
Use one of the
FunctionService on*
methods to create anExecution
object. Theon*
methods,onRegion
,onServer
andonServers
, define the highest level where the function is run. If you useonRegion
you can further narrow your run scope by setting key filters. The function run usingonRegion
is a data dependent function – the others are data-independent functions.You can run a data dependent function against custom partitioned and colocated partitioned regions. From the client, provide the appropriate key sets to the function call.
Use the
Execution
object as needed for additional function configuration. You can:- Provide a set of data keys to
withFilter
to narrow the execution scope. This works only foronRegion Execution
objects. - Provide function arguments to
withArgs
. - Define a custom
ResultCollector
towithCollector
. See Programming to Get Function Results.
- Provide a set of data keys to
Call the
Execution
object execute method to run the function.To run a function with high availability, call
getResult
from the results collector returned fromexecute
. Calling a highly available function without usinggetResult
disables the high availability functionality.
Running a Function on a Region (C++)
regPtr0 = initRegion();
ExecutionPtr exc = FunctionService::onRegion(regPtr0);
CacheableVectorPtr routingObj = CacheableVector::create();
char buf[128];
bool getResult = true;
sprintf(buf, "VALUE--%d", 10);
CacheablePtr value(buf);
sprintf(buf, "KEY--%d", 100);
CacheableKeyPtr key = CacheableKey::create(buf);
regPtr0->put(key, value);
sprintf(buf, "KEY--%d", 100);
CacheableKeyPtr key1 = CacheableKey::create(buf);
routingObj->push_back(key1);
CacheablePtr args = routingObj;
CacheableVectorPtr executeFunctionResult = exc->withFilter(routingObj)->
withArgs(args)->execute(func)->getResult();
Running a Function on a Server Pool (C++)
pptr = PoolManager::find(poolName);
ExecutionPtr exc = FunctionService::onServer(cache);
CacheableVectorPtr routingObj = CacheableVector::create();
char buf[128];
bool getResult = true;
sprintf(buf, "VALUE--%d", 10);
CacheablePtr value(buf);
sprintf(buf, "KEY--%d", 100);
CacheableKeyPtr key = CacheableKey::create(buf);
regPtr0->put(key, value);
sprintf(buf, "KEY--%d", 100);
CacheableKeyPtr key1 = CacheableKey::create(buf);
routingObj->push_back(key1);
CacheablePtr args = routingObj;
CacheableVectorPtr executeFunctionResult =
exc->withArgs(args)->execute(func)->getResult();
Running a Function on a Region (C# .NET)
IRegion<string, string> fregion =
regionFactory.Create<string, string>("exampleRegion");
for (int i = 0; i < 34; i++)
{
fregion.Put("KEY--" + i, "VALUE--" + i, null);
}
object[] routingObj = new object[17];
int j = 0;
for (int i = 0; i < 34; i++)
{
if (i % 2 == 0) continue;
routingObj[j] = "KEY--" + i;
j++;
}
object args0 = true;
Boolean getResult = true;
// data dependent function execution -- get function with result
Execution<object> exc =
Generic.FunctionService.OnRegion<string, string, object>(fregion);
Generic.IResultCollector rc =
exc.WithArgs((IGeodeSerializable)args0).WithFilter(
(IGeodeSerializable[])routingObj).Execute(getFuncName);
object[] executeFunctionResult = rc.GetResult();
Running a Function on a Server Pool (C# .NET)
exc = Generic.FunctionService.OnServer<object>(cache);
List<object> args1 = new List<object>();
for (int i = 0; i < routingObj.Length; i++)
{
Console.WriteLine("routingObj[{0}]={1}.", i, (routingObj[i] as string));
args1.Add(routingObj[i]);
}
rc = exc.WithArgs((IGeodeSerializable)args1).Execute(getFuncIName);
executeFunctionResult = rc.GetResult();
Console.WriteLine("on one server: result count= {0}.",
executeFunctionResult.Length);