
@GridSpiInfo(author = "Your Name", version = "1.0")
@GridSpiMultipleInstancesSupport(true)
public class GridLowElectricityTopologySpi
extends GridSpiAdapter
implements GridTopologySpi {
/** Auto-inject logger. */
@GridLoggerResource
private GridLogger log = null;
/**
* Start SPI.
*/
public void spiStart(String gridName) {
if (log.isInfoEnabled() == true) {
log.info(startInfo());
}
}
/**
* Stop SPI.
*/
public void spiStop() {
if (log.isInfoEnabled() == true) {
log.info(stopInfo());
}
}
/**
* Given whole grid topology return only nodes
* selected for execution of this task.
*/
public Collection<GridNode> getTopology(
GridTaskSession ses,
Collection<GridNode> grid) {
int hour = Calendar.getInstance(Calendar.HOUR_OF_DAY);
List<GridNode> top = new ArrayList<GridNode>(grid.size());
for (GridNode node : grid) {
String segment = node.getAttribute("segment");
if (hour <= 12) {
if (segment.equals("A")) {
top.add(node);
}
}
else if (segment.equals("B")) {
top.add(node);
}
}
return top;
}
}
Now, the nodes from data center A need to have the following in their configuration (the configuration for data center B is analogous).
<bean
id="grid.cfg"
class="org.gridgain.grid.GridConfigurationAdapter"
scope="singleton">
...
<property name="userAttributes">
<map>
<entry key="segment" value="A"/>
</map>
</property>
...
</bean>
This is all that needs to be done to lower your electric bill. Now all tasks will be routed to the data center with lowest electricity cost.
Enjoy!
View comments