DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Create Asynchronous and Retryable Methods With Failover Support
  • What's New With ActiveMQ v5.16.4
  • Mule Server, Server Group and Cluster
  • Redis-Based Tomcat Session Management

Trending

  • Microservices With Apache Camel and Quarkus
  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  • How To Simplify Multi-Cluster Istio Service Mesh Using Admiral
  • What Technical Skills Can You Expect To Gain From a DevOps Course Syllabus?
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Five Easy Clustering Tips With GridGain

Five Easy Clustering Tips With GridGain

Dmitriy Setrakyan user avatar by
Dmitriy Setrakyan
·
Apr. 02, 14 · Interview
Like (0)
Save
Tweet
Share
4.81K Views

Join the DZone community and get the full member experience.

Join For Free

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();
}
clustering

Published at DZone with permission of Dmitriy Setrakyan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Create Asynchronous and Retryable Methods With Failover Support
  • What's New With ActiveMQ v5.16.4
  • Mule Server, Server Group and Cluster
  • Redis-Based Tomcat Session Management

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: