Skip to content

KVM: GetVolumeStatsCommand looks up the storage pool once per volume and discards all stats when one volume fails #14874

Backlog

Description

@jtolelo

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:

  1. Lock contention. Since Introduce new backup provider (KBOSS) #12758, KVMStoragePoolManager.getStoragePool(type, uuid, refreshInfo, addDetails) is synchronized on the manager. A GetStorageStatsCommand asks for refreshInfo = 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 running GetVolumeStatsCommand then waits behind it, together with any other agent command that needs a pool. synchronized · refreshInfo = true
  2. 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, branch 4.22, and main at 85ca17a4d7 (24.0.0-SNAPSHOT). The per-volume lookup and the all-or-nothing answer are present in all three. The synchronized lookup is in 4.23 and later.

The steps to reproduce the bug

  1. 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.
  2. Keep the default storage.stats.interval and set volume.stats.interval to a low value to make it easier to observe.
  3. On that host, enable DEBUG logging for the agent and watch one GetVolumeStatsCommand: Trying to fetch storage pool <uuid> from libvirt is logged once per volume in the command.
  4. 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.
  5. 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?

  1. In LibvirtGetVolumeStatsCommandWrapper, look the pool up once per command, before the loop, and call pool.getPhysicalDisk() for each volume.
  2. Catch failures per volume, log them, and return the stats that were collected. UserVmManagerImpl.getVolumeStatistics already merges whatever getVolumeStats() returns, so partial results need no change on the management server.
  3. Add a unit test that checks getStoragePool() is called once per command and that one failing volume does not drop the others.
  4. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions