I just wrote a pretty interesting piece of code that actually demonstrates nicely the power of the Functional Java APIs introduced in GridGain 3.0. I needed to calculate the total number of CPUs in the topology (grid or cloud).
Here’s the full source code of this method as I wrote it just now:
private int getTotalCpus() {
return F.fold(ctx.grid().neighborhood(), 0, new C2<GridProjection, Integer, Integer>() {
@Override public Integer apply(GridProjection p, Integer cnt) {
return cnt + F.first(p.nodes()).metrics().getAvailableProcessors();
}
});
}
In a few lines of Java code we use:
Fwhich istypedefforGridFunc– GridGain’s “global scope” for Functional APIsF.foldwhich is classic functionalfold-leftoperation on the collectionC2which is atypedefforGridClosure2GridProjectionclass that gives you monad over arbitrary set of grid nodes- Method
neighborhood()that gives you collection of GridProject where each projection has nodes from the same physical computer (coming up in 3.0.5) - Method
F.first()from our functional framework that gives you simply the first element in the collection - Node
metrics()that gives you over 40 dynamically updated metrics for any GridGain node in the topology
Enjoy!