Tuesday, March 25, 2014

Five Easy Clustering Tips With GridGain



Before diving deeper into what it means to easily cluster an application, let's start from defining what  a cluster really is. Wikipedia has a pretty good explanation of clustering here, which is a high level definition that covers fault tolerance, load balancing, scheduling, etc. However, the real magic behind clustering is in making these complex distributed operations seem easy.


From development standpoint ability to cluster an application in most cases can be reduced to being able to easily perform the following functions:
  1. Get list of all currently alive cluster nodes
  2. Ability to create sub-groups of nodes within cluster at will
  3. Exchange messages between any nodes within cluster
  4. Listen to events from any node within a specified group
  5. Easily compute and share data on any of the cluster nodes
Here are the coding examples on how to achieve the above in GridGain. I hope the code is simple enough to understand, but would be interesting to get some feedback on it. Feel free to comment.

Let's start from getting list of all cluster nodes:

Collection<GridNode> nodes = GridGain.grid().nodes();

Now, let's create different sub-groups of nodes within cluster:
// Remote nodes (all nodes, excluding local)
GridProjection rmtNodes = grid.forRemotes();

// Random remote node.
GridProjection rmtRandomNode = rmtNodes.forRandom();

// Current CPU load of remote random node.
double cpu = rmtRandomNode.node().metrics().getCurrentCpuLoad();

// All nodes on the same physical host as remote random node.
GridProjection hostNodes = grid.forHost(rmtRandomNode.node());

// All nodes marked by user as worker nodes.
GridProjeciton workers = grid.forAttribute("worker", "true");

Here is an example of message exchange between cluster nodes in GridGain cluster:
// User-defined message topics.
private enum TOPIC { MYTOPIC }
 
// Get message instance to provide messaging functionality 
// over a projection of remote nodes.
GridMessaging msg = grid.forRemotes().message();
 
// Register message listeners on all remote grid nodes.
msg.remoteListen(MYTOPIC, new GridBiPredicate<UUID, String>() {
    @Override public boolean apply(UUID sndrNodeId, String msg) {
        System.out.println("Received message: " + msg");
         
        return true; // Return true to continue listening.
    }
}).get();
 
msg.send(MYTOPIC, "Hello World");

This example shows how to subscribe an event listener on all grid nodes:
// This optional local callback is called for each event notification
// that passed remote predicate listener.
GridBiPredicate<UUID, GridCacheEvent> locLsnr = new GridBiPredicate<UUID, GridCacheEvent>() {
    @Override public boolean apply(UUID uuid, GridCacheEvent evt) {
        System.out.println("Received event: " + evt.name());

        return true; // Continue listening.
    }
};

// Remote listener which only accepts events for keys that are
// greater or equal than 10 and if event node is primary caching node for this key.
GridPredicate<GridCacheEvent> rmtLsnr = new GridPredicate<GridCacheEvent>() {
    @Override public boolean apply(GridCacheEvent evt) {
        System.out.println("Cache event: " + evt.name());

        int key = evt.key(); // Cache key.

        return key >= 10 && cache.affinity().isPrimary(grid.localNode(), key);
    }
};

// Subscribe to specified cache events on all nodes that have "myCache" running.
grid.forCache("myCache").events().remoteListen(locLsnr, rmtLsnr, EVT_CACHE_OBJECT_PUT).get();

And finally, an example that distributes computations to the nodes where the data is cached:
for (int i = 0; i < KEY_CNT; i++) {
    final int key = i;

    // This runnable will execute on the remote node where
    // the data with the given key is cached. 
    GridRunnable run = new GridRunnable() {
        @Override public void run() {
            System.out.println("Computing [key= " + key + ", value=" + cache.peek(key) + ']');
        }
    };

    grid.compute().affinityRun("myCache", key, run).get();
}

No comments:

Post a Comment