Summary: This page describes two ways to determine the partition load in a Gigaspaces grid programmatically. Author: Joe Ottinger, Technology Evangelist, GigaSpaces Recently tested with GigaSpaces version: XAP 7.1.2
When using a GigaSpaces cluster as a task queue there are times you will need to determine how the space instances are loaded. This could be to determine where you can route the next task (minimum load partition/instance) or where you want to launch more processors (heavily loaded partition/instance).
The first example shows an implementation of DistributedTask that can be used in scenarios where you want to run ad-hoc queries. Usage instructions are similar to the Task Executors Example. Business logic does not have to be on the cluster, it is dynamically transported to the server side and executed remotely.
Example is trying to find a partition with least number of objects and uses GigaSpaces SpaceRuntimeInfo API to get the count of objects. This API is lot faster compared to the count API and is preferred way of getting object counts.
Another example shows an implementation using Executor Service. This approach should be used when this functionality is intrinsic part of the system and not needed on ad-hoc basis. Usage instructions are similar to the Executor Service Example.
In this example, we're trying to find the partition with the least number of objects, using GigaSpaces' SpaceRuntimeInfo API to get the count of objects. This API is lot faster than the count API and is the preferred way of getting object counts.
The Service Interface includes only one method, used to invoke the Service method in Synchronous mode:
The Result Reducer applies the additional logic (finding the partition with the least number of objects, in this case).
import org.openspaces.remoting.RemoteResultReducer;
import org.openspaces.remoting.SpaceRemotingInvocation;
import org.openspaces.remoting.SpaceRemotingResult;
public class DataProcessorServiceReducer implements RemoteResultReducer<PartitionCount, PartitionCount >{
public PartitionCount reduce(SpaceRemotingResult<PartitionCount>[] results,
SpaceRemotingInvocation sri) throws Exception {
PartitionCount minPart = null;
for (SpaceRemotingResult<PartitionCount> result : results) {
if (result.getException() != null) {
System.out.println("Error in one of the partitions");
}
if (minPart != null) {
if (minPart.getCount() > result.getResult().getCount()) {
minPart = result.getResult();
}
}
} else {
minPart = result.getResult();
}
}
return minPart;
}
}
This is how a client might invoke the service:
space = new UrlSpaceConfigurer("jini://*/*/space").space();
gigaSpace = new GigaSpaceConfigurer(space).gigaSpace();
dataProcessor = new ExecutorRemotingProxyConfigurer<IDataProcessor>(
gigaSpace, IDataProcessor.class).broadcast(
new DataProcessorServiceReducer()).proxy();
PartitionCount result = dataProcessor.processData();