Skip to content

Commit 30b707c

Browse files
James PeruJames Peru
authored andcommitted
Fix NPE in NASBackupProvider when no running KVM host is available
ResourceManager.findOneRandomRunningHostByHypervisor() can return null when no KVM host in the zone has status=Up (e.g. during management server startup, brief agent disconnections, or host state transitions). NASBackupProvider.syncBackupStorageStats() and deleteBackup() call host.getId() without a null check, causing a NullPointerException that crashes the entire BackupSyncTask background job every sync interval. This adds null checks in both methods: - syncBackupStorageStats: log a warning and return early - deleteBackup: throw CloudRuntimeException with a descriptive message
1 parent 27bce46 commit 30b707c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ public boolean deleteBackup(Backup backup, boolean forced) {
471471
} else {
472472
host = resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, backup.getZoneId());
473473
}
474+
if (host == null) {
475+
throw new CloudRuntimeException(String.format("Unable to find a running KVM host in zone %d to delete backup %s", backup.getZoneId(), backup.getUuid()));
476+
}
474477

475478
DeleteBackupCommand command = new DeleteBackupCommand(backup.getExternalId(), backupRepository.getType(),
476479
backupRepository.getAddress(), backupRepository.getMountOptions());
@@ -552,7 +555,14 @@ public Pair<Long, Long> getBackupStorageStats(Long zoneId) {
552555
@Override
553556
public void syncBackupStorageStats(Long zoneId) {
554557
final List<BackupRepository> repositories = backupRepositoryDao.listByZoneAndProvider(zoneId, getName());
558+
if (CollectionUtils.isEmpty(repositories)) {
559+
return;
560+
}
555561
final Host host = resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId);
562+
if (host == null) {
563+
logger.warn("Unable to find a running KVM host in zone {} to sync backup storage stats", zoneId);
564+
return;
565+
}
556566
for (final BackupRepository repository : repositories) {
557567
GetBackupStorageStatsCommand command = new GetBackupStorageStatsCommand(repository.getType(), repository.getAddress(), repository.getMountOptions());
558568
BackupStorageStatsAnswer answer;

0 commit comments

Comments
 (0)