Tuesday, March 11, 2014

Grid-Enable Your Local Operations Across Cluster


As you may already know, GridGain went open source last week. Going open source was a lot more involved than simply opening up our code. We put significant amount of thought into simplifying our APIs and making our development process as community friendly as possible.

As an example, take a look at how in GridGain you can take any local operation and distribute it across the cluster. Let's take GridCache interface. In addition to distributed methods, like get(...) or put(...), many APIs on this interface are local. For example, method size() will return number of entries locally cached, or method containsValue(...) will check if value is cached on local node. These APIs are made local on purpose - we anticipated that for certain methods providing local information would be safer and more useful. However, what if you need to see if value is contained across the whole cache, not just local node cache.

In GridGain, to make any operation distributed you need to execute it across multiple nodes using GridCompute functionality. Here is how the global contains method would look like:
public boolean contains(final V val) {
    // Not all nodes in the grid may participate in caching data.
    // We want to make sure that we send our computation only to caching nodes.
    GridProjection cacheNodes = GridGain.grid().forCache("myCache");

    Collection<Boolean> results = cacheNodes.compute().broadcast(
        new GridCallable<Boolean>() {
            @Override public Boolean call() {
                return GridGain.grid().cache("myCache").containsValue(val);
            }
        }
    ).get();
    
    return results.contains(true); 
}
You can do the same for any other operation as well. For example, if you need to find out total cache size across all cache nodes, you would simply broadcast a computation that returned cache.size() from all nodes, and them add them up to get a total value.

No comments:

Post a Comment