Cache nodes

You can logically group together nodes that are storing cache data or all client nodes accessing the data cache. We will look at affinity and cache grouping in Chapter 4Exploring the Compute Grid and Query API.

Create a client to access cache data in an infinite loop:

package com.packt;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;

public class ClientAccessCache {
public static void main(String[] args) throws InterruptedException{
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);

//Client mode is ON
cfg.setClientMode(true);

try (Ignite ignite = Ignition.start(cfg)) {
//Access the 'data' cache
IgniteCache<Long, String> cache = ignite.getOrCreateCache("data");

//infinite get/put cache operation
while(true) {
long longValue = System.currentTimeMillis();
cache.put(longValue, String.valueOf(longValue));

System.out.println(cache.get(longValue));
Thread.sleep(1000);
}
}
}
}

Start the program; it will keep putting objects into the cache data:

Write a program to send compute to the nodes where:

  • The 'data' cache is deployed
  • The data nodes responsible for 'data' cache
  • The client nodes accessing the 'data' cache

The program will get three ClusterGroups for cache nodes, data nodes, and client nodes:

public class CacheGrouping {
public static void main(String[] args) {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (Ignite ignite = Ignition.start(cfg)) {
IgniteCluster cluster = ignite.cluster();

The following code snippet asks the cluster to return the nodes where the 'data' cache is deployed:

// All nodes on which cache with name "data" is deployed,
// either in client or server mode.
ClusterGroup cacheGroup = cluster.forCacheNodes("data");

The following code snippet asks the cluster to return the nodes where elements of the 'data' cache are kept:

// All data nodes responsible for caching data for "data".
ClusterGroup dataGroup = cluster.forDataNodes("data");

The following code snippet asks the cluster to return the client nodes accessing the 'data' cache:

// All client nodes that access "data".
ClusterGroup clientGroup = cluster.forClientNodes("data");

The following code snippet gets the reference of IgniteCompute for the previous three ClusterGroups:

 IgniteCompute cacheGroupCompute = ignite.compute(cacheGroup);
IgniteCompute dataGroupCompute = ignite.compute(dataGroup);
IgniteCompute clientGroupCompute = ignite.compute(clientGroup);

The following code broadcasts the computation to the cache group nodes:

 cacheGroupCompute.broadcast(() -> {
System.out.println("******** Cache Group Only ***********");
});

The following code broadcasts the computation to the data group nodes:

 dataGroupCompute.broadcast(() -> {
System.out.println("********* Data Group Only ************");
});

The following code broadcasts the computation to the client group nodes:

     clientGroupCompute.broadcast(() -> {
System.out.println("********* Client Group Only *******");
});
}
}
}

Now, run the program; it will print the client group message to the client code console. The client node keeps the local cache, so it is also a Cache Group node, hence the console will print two messages:

The CacheGrouping code's console will print Cache Group Only and Data Group Only. As we started the program as a server node, it's a cache group and a data group (data is being distributed in this node):

When you look at the Apache Ignite server's console, it will print Cache Group and Data Group:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.154.252