fix NPE in volumes statistics (#4388)

This commit is contained in:
slavkap 2020-10-30 17:53:05 +02:00 committed by GitHub
parent 963d603ede
commit 8afb451c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 37 deletions

View File

@ -282,13 +282,10 @@ public class ViewResponseHelper {
vrDataList.put(vr.getId(), vrData);
VolumeStats vs = null;
if (vr.getFormat() == ImageFormat.QCOW2) {
vs = ApiDBUtils.getVolumeStatistics(vrData.getId());
}
else if (vr.getFormat() == ImageFormat.VHD){
if (vr.getFormat() == ImageFormat.VHD || vr.getFormat() == ImageFormat.QCOW2) {
vs = ApiDBUtils.getVolumeStatistics(vrData.getPath());
}
else if (vr.getFormat() == ImageFormat.OVA){
else if (vr.getFormat() == ImageFormat.OVA) {
if (vrData.getChainInfo() != null) {
vs = ApiDBUtils.getVolumeStatistics(vrData.getChainInfo());
}

View File

@ -928,13 +928,8 @@ public class StatsCollector extends ManagerBase implements ComponentMethodInterc
for (StoragePoolVO pool : pools) {
List<VolumeVO> volumes = _volsDao.findByPoolId(pool.getId(), null);
List<String> volumeLocators = new ArrayList<String>();
for (VolumeVO volume : volumes) {
if (volume.getFormat() == ImageFormat.QCOW2 || volume.getFormat() == ImageFormat.VHD) {
volumeLocators.add(volume.getPath());
} else if (volume.getFormat() == ImageFormat.OVA) {
volumeLocators.add(volume.getChainInfo());
} else {
if (volume.getFormat() != ImageFormat.QCOW2 && volume.getFormat() != ImageFormat.VHD && volume.getFormat() != ImageFormat.OVA) {
s_logger.warn("Volume stats not implemented for this format type " + volume.getFormat());
break;
}
@ -943,15 +938,14 @@ public class StatsCollector extends ManagerBase implements ComponentMethodInterc
Map<String, VolumeStatsEntry> volumeStatsByUuid;
if (pool.getScope() == ScopeType.ZONE) {
volumeStatsByUuid = new HashMap<>();
for (final Cluster cluster : _clusterDao.listByZoneId(pool.getDataCenterId())) {
final Map<String, VolumeStatsEntry> volumeStatsForCluster = _userVmMgr.getVolumeStatistics(cluster.getId(), pool.getUuid(), pool.getPoolType(),
volumeLocators, StatsTimeout.value());
for (final Cluster cluster : _clusterDao.listClustersByDcId(pool.getDataCenterId())) {
final Map<String, VolumeStatsEntry> volumeStatsForCluster = _userVmMgr.getVolumeStatistics(cluster.getId(), pool.getUuid(), pool.getPoolType(), StatsTimeout.value());
if (volumeStatsForCluster != null) {
volumeStatsByUuid.putAll(volumeStatsForCluster);
}
}
} else {
volumeStatsByUuid = _userVmMgr.getVolumeStatistics(pool.getClusterId(), pool.getUuid(), pool.getPoolType(), volumeLocators, StatsTimeout.value());
volumeStatsByUuid = _userVmMgr.getVolumeStatistics(pool.getClusterId(), pool.getUuid(), pool.getPoolType(), StatsTimeout.value());
}
if (volumeStatsByUuid != null) {
for (final Map.Entry<String, VolumeStatsEntry> entry : volumeStatsByUuid.entrySet()) {

View File

@ -85,7 +85,7 @@ public interface UserVmManager extends UserVmService {
HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, String hostName, List<Long> vmIds);
HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, List<String> volumeLocator, int timout);
HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, int timout);
boolean deleteVmGroup(long groupId);

View File

@ -27,6 +27,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -96,6 +97,7 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
@ -1882,45 +1884,46 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
@Override
public HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, List<String> volumeLocators, int timeout) {
public HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, int timeout) {
List<HostVO> neighbors = _resourceMgr.listHostsInClusterByStatus(clusterId, Status.Up);
StoragePoolVO storagePool = _storagePoolDao.findPoolByUUID(poolUuid);
HashMap<String, VolumeStatsEntry> volumeStatsByUuid = new HashMap<>();
for (HostVO neighbor : neighbors) {
// apply filters:
// - managed storage
// - local storage
if (storagePool.isManaged() || storagePool.isLocal()) {
volumeLocators = getVolumesByHost(neighbor, storagePool);
}
// - zone wide storage for specific hypervisortypes
if (ScopeType.ZONE.equals(storagePool.getScope()) && storagePool.getHypervisor() != neighbor.getHypervisorType()) {
if ((ScopeType.ZONE.equals(storagePool.getScope()) && storagePool.getHypervisor() != neighbor.getHypervisorType())) {
// skip this neighbour if their hypervisor type is not the same as that of the store
continue;
}
GetVolumeStatsCommand cmd = new GetVolumeStatsCommand(poolType, poolUuid, volumeLocators);
List<String> volumeLocators = getVolumesByHost(neighbor, storagePool);
if (!CollectionUtils.isEmpty(volumeLocators)) {
if (timeout > 0) {
cmd.setWait(timeout/1000);
}
GetVolumeStatsCommand cmd = new GetVolumeStatsCommand(poolType, poolUuid, volumeLocators);
Answer answer = _agentMgr.easySend(neighbor.getId(), cmd);
if (timeout > 0) {
cmd.setWait(timeout/1000);
}
if (answer instanceof GetVolumeStatsAnswer){
GetVolumeStatsAnswer volstats = (GetVolumeStatsAnswer)answer;
return volstats.getVolumeStats();
Answer answer = _agentMgr.easySend(neighbor.getId(), cmd);
if (answer instanceof GetVolumeStatsAnswer){
GetVolumeStatsAnswer volstats = (GetVolumeStatsAnswer)answer;
if (volstats.getVolumeStats() != null) {
volumeStatsByUuid.putAll(volstats.getVolumeStats());
}
}
}
}
return null;
return volumeStatsByUuid.size() > 0 ? volumeStatsByUuid : null;
}
private List<String> getVolumesByHost(HostVO host, StoragePool pool){
List<UserVmVO> vmsPerHost = _vmDao.listByHostId(host.getId());
List<VMInstanceVO> vmsPerHost = _vmInstanceDao.listByHostId(host.getId());
return vmsPerHost.stream()
.flatMap(vm -> _volsDao.findByInstanceIdAndPoolId(vm.getId(),pool.getId()).stream().map(vol -> vol.getPath()))
.flatMap(vm -> _volsDao.findByInstanceIdAndPoolId(vm.getId(),pool.getId()).stream().map(vol ->
vol.getState() == Volume.State.Ready ? (vol.getFormat() == ImageFormat.OVA ? vol.getChainInfo() : vol.getPath()) : null).filter(Objects::nonNull))
.collect(Collectors.toList());
}