FP in Java – Finding Total CPUs Number in the Grid

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:

  • F which is typedef for GridFunc – GridGain’s “global scope” for Functional APIs
  • F.fold which is classic functional fold-left operation on the collection
  • C2 which is a typedef for GridClosure2
  • GridProjection class 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: