jtolelo opened a new issue, #14874:
URL: https://github.com/apache/cloudstack/issues/14874

   ### 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](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java#L50-L57)
 · [Per-volume 
lookup](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java#L65)
 · [Pool 
lookup](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java#L558-L645)
   
   This gets worse in two ways:
   
   1. **Lock contention.** Since #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](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStoragePoolManager.java#L283)
 · [refreshInfo = 
true](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetStorageStatsCommandWrapper.java#L39)
   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](https://github.com/apache/cloudstack/blob/85ca17a4d7/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVolumeStatsCommandWrapper.java#L52-L55)
   
   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](https://github.com/apache/cloudstack/blob/85ca17a4d7/server/src/main/java/com/cloud/server/StatsCollector.java#L550-L552)
 · [Per-host 
dispatch](https://github.com/apache/cloudstack/blob/85ca17a4d7/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java#L2431-L2474)
   
   **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 #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:
   
   ```java
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to