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 8, 2023
1 parent 7ea068c commit 7f31309
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import javax.annotation.Nonnull;

import org.apache.cloudstack.storage.datastore.util.LinstorUtil;
import org.apache.cloudstack.utils.qemu.QemuImg;
import org.apache.cloudstack.utils.qemu.QemuImgException;
import org.apache.cloudstack.utils.qemu.QemuImgFile;
Expand Down Expand Up @@ -489,39 +490,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 @@ -550,7 +520,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 @@ -586,7 +556,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 @@ -72,7 +72,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 7f31309

Please sign in to comment.