Repository navigation
Description
problem
LibvirtGetVolumeStatsCommandWrapper calls KVMStoragePoolManager.getStoragePool() inside its per-volume loop. One GetVolumeStatsCommand with N volumes therefore does N full pool lookups. Each lookup calls storagePoolLookupByUUIDString, parses the pool XML, calls StoragePool.getInfo() four times and runs addPoolDetails. Loop · Per-volume lookup · Pool lookup
This gets worse in two ways:
- Lock contention. Since Introduce new backup provider (KBOSS) #12758,
KVMStoragePoolManager.getStoragePool(type, uuid, refreshInfo, addDetails)issynchronizedon the manager. AGetStorageStatsCommandasks forrefreshInfo = true, so it holds that monitor for the whole libvirt pool refresh. On a large RBD pool that refresh takes seconds. Every per-volume lookup of a runningGetVolumeStatsCommandthen waits behind it, together with any other agent command that needs a pool. synchronized · refreshInfo = true - All or nothing. If one volume cannot be resolved, the wrapper returns a failed answer with no stats, so the stats of every other volume in the command are lost. Early return
On RBD before #14188, getPhysicalDisk() also fell back to a full pool refresh when a volume was missing from the host's libvirt cache, which is common for volumes created by other hosts. #14188 (on 4.22 and main, first release 4.22.2.0) removes that refresh by looking RBD volumes up through librbd. It does not change the per-volume pool lookup or the all-or-nothing answer, which this issue is about.
On the management server, every node runs VolumeStatsTask independently, without an owner check. Each Up host in the cluster therefore gets one GetVolumeStatsCommand per pool from every management server, every volume.stats.interval. stats.timeout only bounds the wait on the management server; the agent keeps running the command after the management server gives up. Scheduling · Per-host dispatch
Observed impact (KVM, Ceph RBD primary storage with about 120 images, 3 management servers, volume.stats.interval = 600 s): an idle virsh pool-refresh of the RBD pool takes about 1.9 s. On one host, a single GetVolumeStatsCommand for 22 volumes logged Trying to fetch storage pool <uuid> from libvirt 22 times. Under load, commands lasted 40 minutes or more, all agent workers (workers=15) were busy, pings arrived late, and the host was repeatedly marked Disconnected.
versions
- Observed on: CloudStack 4.21, KVM hosts, Ceph RBD primary storage, 3 management servers.
- Code checked at tag
4.21.0.0, branch4.22, andmainat85ca17a4d7(24.0.0-SNAPSHOT). The per-volume lookup and the all-or-nothing answer are present in all three. Thesynchronizedlookup is in 4.23 and later.
The steps to reproduce the bug
- Use a KVM cluster with Ceph RBD primary storage holding about 100 or more images, and put about 20 RBD volumes (VMs) on one host.
- Keep the default
storage.stats.intervaland setvolume.stats.intervalto a low value to make it easier to observe. - On that host, enable DEBUG logging for the agent and watch one
GetVolumeStatsCommand:Trying to fetch storage pool <uuid> from libvirtis logged once per volume in the command. - On 4.21 or 4.22.1.x, with volumes created by other hosts, also look for
Refreshing storage pool(DEBUG), which is a full pool refresh per cache miss. - Add a volume path that does not exist in the pool to the command, for example a volume deleted out of band: the answer comes back failed and no volume in the command gets stats.
What to do about it?
- In
LibvirtGetVolumeStatsCommandWrapper, look the pool up once per command, before the loop, and callpool.getPhysicalDisk()for each volume. - Catch failures per volume, log them, and return the stats that were collected.
UserVmManagerImpl.getVolumeStatisticsalready merges whatevergetVolumeStats()returns, so partial results need no change on the management server. - Add a unit test that checks
getStoragePool()is called once per command and that one failing volume does not drop the others. - Optional follow-ups: backport kvm: look RBD volumes up through librbd instead of refreshing the whole pool #14188 to supported branches that do not have it, and reuse one rados connection per command for RBD instead of one per volume.
Suggested shape of the loop:
final KVMStoragePool pool = storagePoolMgr.getStoragePool(cmd.getPoolType(), cmd.getPoolUuid());
if (pool == null) {
return new GetVolumeStatsAnswer(cmd, "Storage pool " + cmd.getPoolUuid() + " not found", null);
}
final Map<String, VolumeStatsEntry> stats = new HashMap<>();
final List<String> failed = new ArrayList<>();
for (String volumeUuid : cmd.getVolumeUuids()) {
try {
KVMPhysicalDisk disk = pool.getPhysicalDisk(volumeUuid);
if (disk == null) {
failed.add(volumeUuid);
continue;
}
stats.put(volumeUuid, new VolumeStatsEntry(volumeUuid, disk.getSize(), disk.getVirtualSize()));
} catch (CloudRuntimeException e) {
logger.debug("Can't get stats for volume {} on pool {}: {}", volumeUuid, cmd.getPoolUuid(), e.getMessage());
failed.add(volumeUuid);
}
}
return new GetVolumeStatsAnswer(cmd, failed.isEmpty() ? "" : "Stats unavailable for " + failed, stats);AI assistance
This issue was written with the help of an AI assistant (Claude). I reviewed it and checked the code paths and links against the source at the commits listed above. The measurements come from a production environment. The suggested code above is a sketch: it has not been compiled or tested, and there is no pull request yet.
Activity