I have a Geode system with one locator, three cache servers and one client. Inside these servers, there is a partitioned region which my data is in. What I am trying to do is that run a function on each server to get the data on that specific server. I wrote the function below for beginning:
public void execute(FunctionContext fc) {
Cache cache = CacheFactory.getAnyInstance();
// testRegion is a partitioned region
Region<Object, Object> region = cache.getRegion("/testRegion");
Set<Object> keys = region.keySet();
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) {
fc.getResultSender().sendResult((Serializable) region.get(k));
}
Object lastResult = keysIterator.next();
fc.getResultSender().lastResult((Serializable) region.get(lastResult));
}
There are 30 entries in the partitoned region. When I call the function above like this:
public void runFunction() {
MyFunction function = new MyFunction();
FunctionService.registerFunction(function);
Execution execution = FunctionService.onServers(cache);
ResultCollector collector = execution.execute(new MyFunction());
List result = (List) collector.getResult();
System.out.println(result);
}
The result
list's size is 60 (duplicate entries). When I use FunctionService.onServer()
method instead, than the list size is 30 which is all the data I have pushed. So is there a way to get only the data on a specific server? Like when I run the function it should return something like:
Data on Server1: ....
Data on Server2: ....
Data on Server3: ....
You need to use PartitionRegionHelper in your function body to get the local data. Something like:
@Override
public void execute(FunctionContext context) {
RegionFunctionContext rfc = (RegionFunctionContext) context;
Region r = PartitionRegionHelper.getLocalDataForContext(rfc);
...
}