如何在hazelcast中获取跨节点的分布式地图的内存成本

时间:2017-05-09 12:10:51

标签: java hazelcast distributed-caching distributed-cache hazelcast-imap

我想获得Hazelcast中分布式地图的总内存成本。 我试过下面的,

    LocalMapStats mapStatistics = cache.getLocalMapStats();
        this.heapCost=mapStatistics.getHeapCost();

这仅提供来自本地节点的Map的成本。 任何人都可以帮助我,在hazelcast中获取地图的所有节点的总内存成本。 根据以下评论我已经尝试过ExecutorService,

我的Callable类是,

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;
}

和调用方法是,

    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);
            // ...
       }
    } 

但在跑步时低于错误

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'

2 个答案:

答案 0 :(得分:1)

您可能希望使用ExecutorService(hazelcastInstance::getExecutorService)在所有节点上运行操作并总结结果,如果这是有意义的。

答案 1 :(得分:0)

可调用类:

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;
}

调用方法,

    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;
    }