Skip to content

Commit bba554b

Browse files
authored
linstor: Fix possible NPE if Linstor storage-pool data missing (#8319)
If Linstor doesn't return storage pool info, certain values are null. Now we assume the values are 0 if we get null values.
1 parent 7eb3636 commit bba554b

File tree

2 files changed

+8
-35
lines changed

2 files changed

+8
-35
lines changed

plugins/storage/volume/linstor/src/main/java/com/cloud/hypervisor/kvm/storage/LinstorStorageAdaptor.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import javax.annotation.Nonnull;
3030

31+
import org.apache.cloudstack.storage.datastore.util.LinstorUtil;
3132
import org.apache.cloudstack.utils.qemu.QemuImg;
3233
import org.apache.cloudstack.utils.qemu.QemuImgException;
3334
import org.apache.cloudstack.utils.qemu.QemuImgFile;
@@ -489,39 +490,8 @@ public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String templateFileP
489490
}
490491

491492
public long getCapacity(LinstorStoragePool pool) {
492-
DevelopersApi linstorApi = getLinstorAPI(pool);
493493
final String rscGroupName = pool.getResourceGroup();
494-
try {
495-
List<ResourceGroup> rscGrps = linstorApi.resourceGroupList(
496-
Collections.singletonList(rscGroupName),
497-
null,
498-
null,
499-
null);
500-
501-
if (rscGrps.isEmpty()) {
502-
final String errMsg = String.format("Linstor: Resource group '%s' not found", rscGroupName);
503-
s_logger.error(errMsg);
504-
throw new CloudRuntimeException(errMsg);
505-
}
506-
507-
List<StoragePool> storagePools = linstorApi.viewStoragePools(
508-
Collections.emptyList(),
509-
rscGrps.get(0).getSelectFilter().getStoragePoolList(),
510-
null,
511-
null,
512-
null
513-
);
514-
515-
final long capacity = storagePools.stream()
516-
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
517-
.mapToLong(sp -> sp.getTotalCapacity() != null ? sp.getTotalCapacity() : 0)
518-
.sum() * 1024; // linstor uses kiB
519-
s_logger.debug("Linstor: GetCapacity() -> " + capacity);
520-
return capacity;
521-
} catch (ApiException apiEx) {
522-
s_logger.error(apiEx.getMessage());
523-
throw new CloudRuntimeException(apiEx.getBestMessage(), apiEx);
524-
}
494+
return LinstorUtil.getCapacityBytes(pool.getSourceHost(), rscGroupName);
525495
}
526496

527497
public long getAvailable(LinstorStoragePool pool) {
@@ -550,7 +520,7 @@ public long getAvailable(LinstorStoragePool pool) {
550520

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

555525
s_logger.debug("Linstor: getAvailable() -> " + free);
556526
return free;
@@ -586,7 +556,9 @@ public long getUsed(LinstorStoragePool pool) {
586556

587557
final long used = storagePools.stream()
588558
.filter(sp -> sp.getProviderKind() != ProviderKind.DISKLESS)
589-
.mapToLong(sp -> sp.getTotalCapacity() - sp.getFreeCapacity()).sum() * 1024; // linstor uses Kib
559+
.mapToLong(sp -> sp.getTotalCapacity() != null && sp.getFreeCapacity() != null ?
560+
sp.getTotalCapacity() - sp.getFreeCapacity() : 0L)
561+
.sum() * 1024; // linstor uses Kib
590562
s_logger.debug("Linstor: getUsed() -> " + used);
591563
return used;
592564
} catch (ApiException apiEx) {

plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/datastore/util/LinstorUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public static long getCapacityBytes(String linstorUrl, String rscGroupName) {
7272

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

0 commit comments

Comments
 (0)