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.
This commit is contained in:
Peinthor Rene 2023-12-08 12:32:18 +01:00 committed by GitHub
parent 7eb36367c9
commit bba554bcc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 35 deletions

View File

@ -28,6 +28,7 @@ import java.util.StringJoiner;
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;
@ -489,39 +490,8 @@ public class LinstorStorageAdaptor implements StorageAdaptor {
}
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) {
@ -550,7 +520,7 @@ public class LinstorStorageAdaptor implements StorageAdaptor {
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;
@ -586,7 +556,9 @@ public class LinstorStorageAdaptor implements StorageAdaptor {
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) {

View File

@ -72,7 +72,8 @@ public class LinstorUtil {
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);