1. As I mentioned in my previous blog, GridGain 3.0 practically changed the way we think about cloud programming. GridGain has always been simple and natural to use, but after the last 2.1 release this just was not enough anymore. We kept thinking on how to make our product even more natural and more powerful. Well, the addition of Data Grid component in GridGain 3.0 definitely helped, but I think the biggest and most powerful change for us was a significant paradigm shift towards Functional Programming (FP) with rich and powerful APIs. The functional approach for APIs just fits so naturally, I am surprised we have not thought about it before (well... reading books on Scala definitely helped :). In GridGain 3.0 the API's got rich, and the code got terse.

    Take a look for example at how you can *recursively* calculate Fibonacci sequence for number 10 in GridGain 3.0 (this is not the most effective implementation, but bare with me for now):
    final Grid g = G.start(); // Start grid.
    
    int fib = g.call(UNICAST, new GridClosureX<Integer, Integer>() {
      @Override public Integer applyx(Integer n) throws GridException {
        return n == 0 ? 0 : n <= 2 ? 1 : 
          g.call(UNICAST, this, n - 1) + g.call(UNICAST, this, n - 2);
      }
    }, 10);
    Things to notice in the above coding snippet:
    1. GridClosureX is just a function which will be executed on the remote grid or cloud (suffix 'X' means that it throws GridException).
    2. There is no deployment step - GridGain auto-deploys your code on participating nodes on-demand (pretty cool).
    3. We are reusing the same grid variable "g" in local and remote code (even cooler, but it gets better).
    4. Note how we are re-executing the same closure from remote nodes *recursively* by passing "this" into "g.call(..)" method!!!
    I hope you are already intrigued, but the above example, as pretty as it is, is not very useful or effective. Nodes may recursively calculate Fibonacci for the same number more than once throughout the same execution. Also, the method returns an integer which is not very practical as Fibonacci numbers grow very large very quickly.

    Let's get a little fancier and introduce caching of calculated Fibonacci numbers on remote nodes. Also let's switch to using BigInteger so we can handle really large numbers:
    BigInteger fib = g.call(UNICAST, new GridClosureX<Long, BigInteger>() {
      @Override public BigInteger applyx(Long n) throws GridException {
        System.out.println("Starting fibonacci execution for number: " + n);
    
        // Make sure n is not negative.
        n = Math.abs(n);
    
        if (n == 0) {
          return BigInteger.ZERO;
        }
    
        if (n <= 2) {
          return BigInteger.ONE;
        }            
        
        // Node-local storage is provided by Grid.nodeLocal() method.
        GridNodeLocal<Long, BigInteger> nodeLocal = g.nodeLocal();
    
        // Check if value is cached in node-local store first.
        BigInteger n1 = nodeLocal.get(n - 1);
    
        // If value is not cached in node-local store, then
        // compute it and cache it.
        if (n1 == null) {
          // Nested recursive distributed execution on the grid.
          nodeLocal.putIfAbsent(n - 1, n1 = g.call(UNICAST, this, n - 1, p));
        }
    
        // Check if value is cached in node-local store first.
        BigInteger n2 = nodeLocal.get(n - 2);
    
        // If value is not cached in node-local store, then
        // compute it and cache it.
        if (n2 == null) {
          // Nested recursive distributed execution on the grid.
          nodeLocal.putIfAbsent(n - 2, n2 = g.call(UNICAST, this, n - 2, p));
        }
    
        return n1.add(n2);
      }
    }, 100);
    This code snippet is pretty similar to the first one, except that it caches already calculated values directly on remote nodes, so it gets smarter as it progresses. If you run it twice for the same value, no recursion will happen the second time at all, and you will get result immediately from node-local store. All we need to do now is just startup a few grid nodes and give it a go. The result for Fibonacci(100) is '354224848179261915075' by the way.

    Now I want you to stop for a second and think about what we have been able to achieve just in a few lines of code above.

    This example, along with many others, is shipped with GridGain. I invite you to download it and see it for yourself.
    0

    Add a comment

  2. After many sleepless nights and long hours I am pleased to announce that we have released GridGain 3.0 Beta. With this release we took quite a leap from our previous version and significantly enhanced our APIs and functionality with post-functional approach. I invite you to download the release it and see it for yourself.

    As a little taste, here is how "Hello World" example now looks on GridGain 3.0:
    grid.run(UNICAST, F.println(), "Hello World");
    The example above will take the string "Hello World" and print it on some remote node. F.println() is just a predefined function (closure) that simply prints out the argument passed in. As usual you get the benefit of GridGain's automatic class loading, so you don't need to deploy anything at all - this code will just auto-deploy on remote nodes.

    Now, let's get a little fancier and have each word from the passed in phrase print on a separate node.
    grid.run(SPREAD, F.yield("Say Hello Functional Style".split(" "), F.println()));
    Here is what happens: 'F.yield(..)' simply takes every element from a collection or array and passes it to the given function. Mode 'SPREAD' will take every function and spread them one-at-a-time to each node - hence the end result is that remote nodes will print individual words.

    Here is how the same example looks in Scalar - native Scala counterpart for GridGain, which is also part of GridGain 3.0 release:
    grid !!~ 
    (for (w <- "Say Hello Functional Style".split(" ")) yield () => println(w))
    In coming days I will blog more about many new features in GridGain 3.0, post-functional approach, Scalar, and a major data grid component that we have added in GridGain 3.0.

    Stay tuned!
    0

    Add a comment

About me
About me
- Antoine de Saint-Exupery -
- Antoine de Saint-Exupery -
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
Blog Archive
Blogs I frequent
Loading
Dynamic Views theme. Powered by Blogger.