i want to get the total memory cost of Distributed Map in Hazelcast. i have tried below ,
LocalMapStats mapStatistics = cache.getLocalMapStats();
this.heapCost=mapStatistics.getHeapCost();
This gives cost of Map from Local Node only. Can anyone help me here,to get the total memory cost of the map across all nodes in hazelcast. AS per below comment i have tried ExecutorService,
My Callable class is,
public class DistrubutedMapStats implements Callable<String>, Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
String cacheMapName = null;
// 0 means no heap Cost.
/** The heap Cost. */
protected long heapCost = 0;
private String instanceName;
transient HazelcastInstance hazelcastInstance;
public DistrubutedMapStats() {
}
public DistrubutedMapStats(String cacheMapName,String instanceName) {
this.cacheMapName = cacheMapName;
this.instanceName=instanceName;
hazelcastInstance=Hazelcast.getHazelcastInstanceByName(instanceName);
}
public String call() {
System.out.println("HazelcastInstance Details="+hazelcastInstance.getName());
LocalMapStats mapStatistics = hazelcastInstance.getMap(cacheMapName).getLocalMapStats();
heapCost = mapStatistics.getHeapCost();
System.out.println("CacheName="+cacheMapName+" HeapCost="+heapCost);
return ""+heapCost;
}
and calling method is,
private void getHeapCostFromMembers(String cacheName, Set<Member> members) throws Exception {
IExecutorService executorService = hazelcastInstance.getExecutorService("default");
DistrubutedMapStats distrubutedMapStats=new DistrubutedMapStats(cacheName,hazelcastInstance.getName());
Map<Member, Future<String>> futures = executorService.submitToMembers(distrubutedMapStats, members);
for (Future<String> future : futures.values()) {
String echoResult = future.get();
System.out.println("HEAP COST="+echoResult);
// ...
}
}
but getting below error while running,
java.util.concurrent.ExecutionException: java.lang.NullPointerException: while trying to invoke the method com.hazelcast.core.HazelcastInstance.getName() of a null object loaded from field DistrubutedMapStats.hazelcastInstance of an object loaded from local variable 'this'
Callable class:
public class DistrubutedMapStats implements Callable<String>, Serializable,HazelcastInstanceAware{
private static final long serialVersionUID = 1L;
String cacheMapName = null;
// 0 means no heap Cost.
/** The heap Cost. */
protected long heapCost = 0;
protected long totalHeapCost = 0;
protected long backupHeapCost = 0;
public transient HazelcastInstance hazelcastInstance;
public DistrubutedMapStats() {
}
public DistrubutedMapStats(String cacheMapName) {
this.cacheMapName = cacheMapName;
}
public String call() {
LocalMapStats mapStatistics = hazelcastInstance.getMap(cacheMapName).getLocalMapStats();
heapCost = mapStatistics.getHeapCost();
backupHeapCost=mapStatistics.getBackupEntryMemoryCost();
totalHeapCost=heapCost-backupHeapCost;
System.out.println("CacheName="+cacheMapName+" Total Cost="+heapCost+" HeapCost="+totalHeapCost+" BackupHeapCost="+backupHeapCost+" from Member");
return ""+totalHeapCost;
}
@Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
// TODO Auto-generated method stub
this.hazelcastInstance=hazelcastInstance;
}
calling method,
private long getHeapCostFromMembers(String cacheName, Set<Member> members) throws Exception {
long totalCacheHeapCost=0;
members=hazelcastInstance.getCluster().getMembers();
IExecutorService executorService = hazelcastInstance.getExecutorService("default");
DistrubutedMapStats distrubutedMapStats=new DistrubutedMapStats(cacheName);
distrubutedMapStats.setHazelcastInstance(hazelcastInstance);
System.out.println("Total Members in Cloud="+members.size());
Map<Member, Future<String>> futures = executorService.submitToMembers(distrubutedMapStats, members);
int i=0;
for (Future<String> future : futures.values())
{
i++;
String heapCostFromMembers = future.get();
System.out.println("HEAP COST "+"For Cache "+cacheName+" is"+" of Member="+i+" is "+heapCostFromMembers);
if(!heapCostFromMembers.isEmpty())
{
totalCacheHeapCost+=Long.parseLong(heapCostFromMembers);
}
// ...
}
System.out.println("Total HEAP COST "+"For Cache "+cacheName+" is"+" of Members="+members.size()+" is "+totalCacheHeapCost);
return totalCacheHeapCost;
}