Skip to content

Commit

Permalink
linstor: Fix possible NPE if Linstor storage-pool data missing
Browse files Browse the repository at this point in the history
If Linstor doesn't return storage pool info, certain values are null.
Now we assume the values are 0 if we get null values.
  • Loading branch information
rp- committed Dec 6, 2023
1 parent b00fd6b commit 459867f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,39 +493,8 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
}

public long getCapacity(LinstorStoragePool pool) {
DevelopersApi linstorApi = getLinstorAPI(pool);
final String rscGroupName = pool.getResourceGroup();
try {
List<ResourceGroup> rscGrps = linstorApi.resourceGroupList(
Collections.singletonList(rscGroupName),
null,
null,
null);

if (rscGrps.isEmpty()) {
final String errMsg = String.format("Linstor: Resource group '%s' not found", rscGroupName);
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}

List<StoragePool> storagePools = linstorApi.viewStoragePools(
Collections.emptyList(),
rscGrps.get(0).getSelectFilter().getStoragePoolList(),
null,
null,
null
);

final long capacity = storagePools.stream()
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
.mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0)
.sum() * 1024; // linstor uses kiB
s_logger.debug("Linstor: GetCapacity() -> " + capacity);
return capacity;
} catch (ApiException apiEx) {
s_logger.error(apiEx.getMessage());
throw new CloudRuntimeException(apiEx.getBestMessage(), apiEx);
}
return LinstorUtil.getCapacityBytes(pool.getSourceHost(), rscGroupName);
}

public long getAvailable(LinstorStoragePool pool) {
Expand Down Expand Up @@ -554,7 +523,7 @@ public long getAvailable(LinstorStoragePool pool) {

final long free = storagePools.stream()
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
.mapToLong(StoragePool::getFreeCapacity).sum() * 1024; // linstor uses KiB
.mapToLong(sp -> sp.getFreeCapacity() != null ? sp.getFreeCapacity() : 0L).sum() * 1024; // linstor uses KiB

s_logger.debug("Linstor: getAvailable() -> " + free);
return free;
Expand Down Expand Up @@ -590,7 +559,9 @@ public long getUsed(LinstorStoragePool pool) {

final long used = storagePools.stream()
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
.mapToLong(sp -> sp.getTotalCapacity() - sp.getFreeCapacity()).sum() * 1024; // linstor uses Kib
.mapToLong(sp -> sp.getTotalCapacity() != null && sp.getFreeCapacity() != null ?
sp.getTotalCapacity() - sp.getFreeCapacity() : 0L)
.sum() * 1024; // linstor uses Kib
s_logger.debug("Linstor: getUsed() -> " + used);
return used;
} catch (ApiException apiEx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public static long getCapacityBytes(String linstorUrl, String rscGroupName) {

return storagePools.stream()
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
.mapToLong(StoragePool::getTotalCapacity).sum() * 1024; // linstor uses kiB
.mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0L)
.sum() * 1024; // linstor uses kiB
} catch (ApiException apiEx) {
s_logger.error(apiEx.getMessage());
throw new CloudRuntimeException(apiEx);
Expand Down

0 comments on commit 459867f

Please sign in to comment.