Skip to content

Commit db5341c

Browse files
slavkapPearl Dsilva
authored andcommitted
fix NPE in volumes statistics (apache#4388)
1 parent abe16eb commit db5341c

4 files changed

Lines changed: 30 additions & 37 deletions

File tree

server/src/main/java/com/cloud/api/query/ViewResponseHelper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,10 @@ public static List<VolumeResponse> createVolumeResponse(ResponseView view, Volum
288288
vrDataList.put(vr.getId(), vrData);
289289

290290
VolumeStats vs = null;
291-
if (vr.getFormat() == ImageFormat.QCOW2) {
292-
vs = ApiDBUtils.getVolumeStatistics(vrData.getId());
293-
}
294-
else if (vr.getFormat() == ImageFormat.VHD){
291+
if (vr.getFormat() == ImageFormat.VHD || vr.getFormat() == ImageFormat.QCOW2) {
295292
vs = ApiDBUtils.getVolumeStatistics(vrData.getPath());
296293
}
297-
else if (vr.getFormat() == ImageFormat.OVA){
294+
else if (vr.getFormat() == ImageFormat.OVA) {
298295
if (vrData.getChainInfo() != null) {
299296
vs = ApiDBUtils.getVolumeStatistics(vrData.getChainInfo());
300297
}

server/src/main/java/com/cloud/server/StatsCollector.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -931,13 +931,8 @@ protected void runInContext() {
931931

932932
for (StoragePoolVO pool : pools) {
933933
List<VolumeVO> volumes = _volsDao.findByPoolId(pool.getId(), null);
934-
List<String> volumeLocators = new ArrayList<String>();
935934
for (VolumeVO volume : volumes) {
936-
if (volume.getFormat() == ImageFormat.QCOW2 || volume.getFormat() == ImageFormat.VHD) {
937-
volumeLocators.add(volume.getPath());
938-
} else if (volume.getFormat() == ImageFormat.OVA) {
939-
volumeLocators.add(volume.getChainInfo());
940-
} else {
935+
if (volume.getFormat() != ImageFormat.QCOW2 && volume.getFormat() != ImageFormat.VHD && volume.getFormat() != ImageFormat.OVA) {
941936
s_logger.warn("Volume stats not implemented for this format type " + volume.getFormat());
942937
break;
943938
}
@@ -946,15 +941,14 @@ protected void runInContext() {
946941
Map<String, VolumeStatsEntry> volumeStatsByUuid;
947942
if (pool.getScope() == ScopeType.ZONE) {
948943
volumeStatsByUuid = new HashMap<>();
949-
for (final Cluster cluster : _clusterDao.listByZoneId(pool.getDataCenterId())) {
950-
final Map<String, VolumeStatsEntry> volumeStatsForCluster = _userVmMgr.getVolumeStatistics(cluster.getId(), pool.getUuid(), pool.getPoolType(),
951-
volumeLocators, StatsTimeout.value());
944+
for (final Cluster cluster : _clusterDao.listClustersByDcId(pool.getDataCenterId())) {
945+
final Map<String, VolumeStatsEntry> volumeStatsForCluster = _userVmMgr.getVolumeStatistics(cluster.getId(), pool.getUuid(), pool.getPoolType(), StatsTimeout.value());
952946
if (volumeStatsForCluster != null) {
953947
volumeStatsByUuid.putAll(volumeStatsForCluster);
954948
}
955949
}
956950
} else {
957-
volumeStatsByUuid = _userVmMgr.getVolumeStatistics(pool.getClusterId(), pool.getUuid(), pool.getPoolType(), volumeLocators, StatsTimeout.value());
951+
volumeStatsByUuid = _userVmMgr.getVolumeStatistics(pool.getClusterId(), pool.getUuid(), pool.getPoolType(), StatsTimeout.value());
958952
}
959953
if (volumeStatsByUuid != null) {
960954
for (final Map.Entry<String, VolumeStatsEntry> entry : volumeStatsByUuid.entrySet()) {

server/src/main/java/com/cloud/vm/UserVmManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public interface UserVmManager extends UserVmService {
8686

8787
HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, String hostName, List<Long> vmIds);
8888

89-
HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, List<String> volumeLocator, int timout);
89+
HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, int timout);
9090

9191
boolean deleteVmGroup(long groupId);
9292

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Map.Entry;
32+
import java.util.Objects;
3233
import java.util.Set;
3334
import java.util.UUID;
3435
import java.util.concurrent.ConcurrentHashMap;
@@ -1954,45 +1955,46 @@ public HashMap<Long, VmStatsEntry> getVirtualMachineStatistics(long hostId, Stri
19541955
}
19551956

19561957
@Override
1957-
public HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, List<String> volumeLocators, int timeout) {
1958+
public HashMap<String, VolumeStatsEntry> getVolumeStatistics(long clusterId, String poolUuid, StoragePoolType poolType, int timeout) {
19581959
List<HostVO> neighbors = _resourceMgr.listHostsInClusterByStatus(clusterId, Status.Up);
19591960
StoragePoolVO storagePool = _storagePoolDao.findPoolByUUID(poolUuid);
1960-
for (HostVO neighbor : neighbors) {
1961-
// apply filters:
1962-
// - managed storage
1963-
// - local storage
1964-
if (storagePool.isManaged() || storagePool.isLocal()) {
1965-
1966-
volumeLocators = getVolumesByHost(neighbor, storagePool);
1961+
HashMap<String, VolumeStatsEntry> volumeStatsByUuid = new HashMap<>();
19671962

1968-
}
1963+
for (HostVO neighbor : neighbors) {
19691964

19701965
// - zone wide storage for specific hypervisortypes
1971-
if (ScopeType.ZONE.equals(storagePool.getScope()) && storagePool.getHypervisor() != neighbor.getHypervisorType()) {
1966+
if ((ScopeType.ZONE.equals(storagePool.getScope()) && storagePool.getHypervisor() != neighbor.getHypervisorType())) {
19721967
// skip this neighbour if their hypervisor type is not the same as that of the store
19731968
continue;
19741969
}
19751970

1976-
GetVolumeStatsCommand cmd = new GetVolumeStatsCommand(poolType, poolUuid, volumeLocators);
1971+
List<String> volumeLocators = getVolumesByHost(neighbor, storagePool);
1972+
if (!CollectionUtils.isEmpty(volumeLocators)) {
19771973

1978-
if (timeout > 0) {
1979-
cmd.setWait(timeout/1000);
1980-
}
1974+
GetVolumeStatsCommand cmd = new GetVolumeStatsCommand(poolType, poolUuid, volumeLocators);
19811975

1982-
Answer answer = _agentMgr.easySend(neighbor.getId(), cmd);
1976+
if (timeout > 0) {
1977+
cmd.setWait(timeout/1000);
1978+
}
1979+
1980+
Answer answer = _agentMgr.easySend(neighbor.getId(), cmd);
19831981

1984-
if (answer instanceof GetVolumeStatsAnswer){
1985-
GetVolumeStatsAnswer volstats = (GetVolumeStatsAnswer)answer;
1986-
return volstats.getVolumeStats();
1982+
if (answer instanceof GetVolumeStatsAnswer){
1983+
GetVolumeStatsAnswer volstats = (GetVolumeStatsAnswer)answer;
1984+
if (volstats.getVolumeStats() != null) {
1985+
volumeStatsByUuid.putAll(volstats.getVolumeStats());
1986+
}
1987+
}
19871988
}
19881989
}
1989-
return null;
1990+
return volumeStatsByUuid.size() > 0 ? volumeStatsByUuid : null;
19901991
}
19911992

19921993
private List<String> getVolumesByHost(HostVO host, StoragePool pool){
1993-
List<UserVmVO> vmsPerHost = _vmDao.listByHostId(host.getId());
1994+
List<VMInstanceVO> vmsPerHost = _vmInstanceDao.listByHostId(host.getId());
19941995
return vmsPerHost.stream()
1995-
.flatMap(vm -> _volsDao.findByInstanceIdAndPoolId(vm.getId(),pool.getId()).stream().map(vol -> vol.getPath()))
1996+
.flatMap(vm -> _volsDao.findByInstanceIdAndPoolId(vm.getId(),pool.getId()).stream().map(vol ->
1997+
vol.getState() == Volume.State.Ready ? (vol.getFormat() == ImageFormat.OVA ? vol.getChainInfo() : vol.getPath()) : null).filter(Objects::nonNull))
19961998
.collect(Collectors.toList());
19971999
}
19982000

0 commit comments

Comments
 (0)