如何在 hazelcast 中跨节点获取分布式映射的内存成本
How to get Memory cost of Distributed Map across node in hazelcast
我想获取 Hazelcast 中分布式映射的总内存成本。
我在下面尝试过,
LocalMapStats mapStatistics = cache.getLocalMapStats();
this.heapCost=mapStatistics.getHeapCost();
这仅给出本地节点的地图成本。
任何人都可以在这里帮助我,以获取 hazelcast 中所有节点的地图的总内存成本。
根据下面的评论,我已经尝试过 ExecutorService,
我的可调用 class 是,
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'
您可能想使用 ExecutorService (hazelcastInstance::getExecutorService
) 来 运行 所有节点上的操作并对结果求和,如果这有意义的话。
可调用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;
}
调用方法,
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;
}
我想获取 Hazelcast 中分布式映射的总内存成本。 我在下面尝试过,
LocalMapStats mapStatistics = cache.getLocalMapStats();
this.heapCost=mapStatistics.getHeapCost();
这仅给出本地节点的地图成本。 任何人都可以在这里帮助我,以获取 hazelcast 中所有节点的地图的总内存成本。 根据下面的评论,我已经尝试过 ExecutorService,
我的可调用 class 是,
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'
您可能想使用 ExecutorService (hazelcastInstance::getExecutorService
) 来 运行 所有节点上的操作并对结果求和,如果这有意义的话。
可调用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;
}
调用方法,
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;
}