Why Segment Nodes?
Often in deployments you need to segment your grid nodes into several groups, having each group perform one or more subsets of jobs only. For example, let's say you have a scenario where you have some nodes only submitting jobs to grid (masters), and other groups of nodes only executing these jobs (workers). Then you would segment your grid into 2 groups, masters and workers, and have each group do only what it is supposed to do.
Multiple Sub-Grids
Node segmentation allows you to create multiple sub-grids within your grid. Every sub-grid may have it's own static physical characteristics and logical responsibilities. All node characteristics, physical or logical, if they are static, can be specified in Spring Configuration and used in your Topology SPI or GridTask.map(..) ![]()
Note, that based on its attributes, every node can participate in one or multiple segments.
Dynamic Sub-Grids
You may also wish to segment your grid based on dynamic characteristics, not static. For example, what if you only want to include nodes that have less than 50% CPU utilization. In GridGain you can achieve this by using dynamic GridNodeMetrics ![]()
![]()
![]()
Node Segmentation Example
This example shows how you can segment your grid into static segments using GridGain. In GridGain such segmentation can be easily achieved with node attributes (see GridNode.getAttribute(String) ![]()
Every node at startup should get a certain number of attributes assigned to it. Here is how this can be done from Spring XML configuration:
<bean id="grid.cfg" class="org.gridgain.grid.GridConfigurationAdapter" scope="singleton"> ... <property name="userAttributes"> <map> <!-- In our example, segment value can be either 'segment.master', 'segment.worker1', or 'segment.worker2'. --> <entry key="segment.worker1" value="true"/> </map> </property> ... </bean>
Then in your GridTask.map(..) ![]()
![]()
public class FooBarGridTask extends GridTaskAdapter<String, String> { ... public Map<GridJob, GridNode> map(List<GridNode> topology, String arg) { Map<GridJob, GridNode> jobs = new HashMap<GridJob, GridNode>(topology.size()); for (GridNode node : topology) { String worker1Attr = node.getAttribute("segment.worker1"); String worker2Attr = node.getAttribute("segment.worker2"); if (worker1Attr != null && worker1Attr.equals("true")) { // This type of job should only execute on 'segment.worker1' segment. jobs.put(new FooBarWorker1Job(arg), node); } else if (worker2Attr != null && worker2Attr.equals("true")) { // This type of job should only execute on 'segment.worker2' segment. jobs.put(new FooBarWorker2Job(arg), node); } } return jobs; } ... }
You can also restrict the topology passed into map(..) method by properly configuring GridAttributesTopologySpi to only include nodes from segments 'segment.worker1' and 'segment.worker2' and always exclude nodes belonging to 'segment.master' segment. Here is an example:
<bean id="grid.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true"> ... <property name="topologySpi"> <bean class="org.gridgain.grid.spi.topology.attributes.GridAttributesTopologySpi"> <property name="attributes"> <map> <entry key="segment.worker1" value="true"/> <entry key="segment.worker2" value="true"/> </map> </property> </bean> </property> ... </bean>
