VM and volume stats cleanup runs on every management server
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 48/100
研究方向
Start by reading StatsCollector, VmStatsCleaner, VolumeStatsCleaner, and the VmDiskStatsUpdaterTask locking pattern mentioned in the issue. Check the cleaner scheduling and database deletion paths, including the related issues #14397 and #14398; the issue proposes both single-server scheduling and a faster deletion strategy. Done means preventing duplicate cleanup work across management servers and validating that cleanup preserves the intended retention behavior.
由索引模型根據 Issue 內容生成。
描述
problem
StatsCollector schedules VmStatsCleaner and VolumeStatsCleaner every 60 seconds on every management server.
A cluster with three management servers runs the same retention DELETE three times a minute against the same database, leading to unnecessary DB load.
vm_stats has no index on timestamp, so each pass is a full table scan (indexes were discussed in #8737). With 30-day retention our table is around 3M rows, and each pass takes 2-3 seconds. Only the first pass each minute finds anything to delete:
ms1 Removed a total of [0] vm_stats rows older than [Sun Oct 11 20:16:49 UTC 2026].
ms3 Removed a total of [0] vm_stats rows older than [Sun Oct 11 20:16:54 UTC 2026].
ms2 Removed a total of [0] vm_stats rows older than [Sun Oct 11 20:17:29 UTC 2026].
ms1 Removed a total of [0] vm_stats rows older than [Sun Oct 11 20:17:52 UTC 2026].
ms3 Removed a total of [0] vm_stats rows older than [Sun Oct 11 20:17:56 UTC 2026].
(We recently switched to 30 days of retention, which is why we're seeing 0 removed on each pass)
versions
ACS 4.22, three management servers, MariaDB Galera cluster, KVM.
The steps to reproduce the bug
- Run two or more management servers against the same database.
- Set vm.stats.max.retention.time > 0 (the default is 720).
- Watch the management server logs at INFO, or the database slow log. Each management server logs "Removed a total of [N] vm_stats rows" once a minute.
What to do about it?
I think the cleaners should run on one management server only. VmDiskStatsUpdaterTask in the same class already does this: it takes a GlobalLock and runs only on the Up management server with the lowest id.
Wrapping both cleaners the same way would cut the work to one pass a minute. Related to #14397. I'm happy to raise a PR.
For discussion:
I did some digging with some AI help and there might be other ways to run this that might be more efficient.
Option 1: delete by id
The cleaner could find the first row in id order that is newer than the cutoff, then run DELETE ... WHERE id < <that id>.
Both steps use the primary key so a pass only reads the expired rows plus one and doesn't need a new index.
This approach relies on id order closely following timestamp order. On our 3M-row table about 0.5% of rows are out of order by up to 4 minutes, however these rows were rows that were older than the cutoff, not newer, so those rows can stay a few minutes past expiry.
Rows newer than the cutoff can't be matched and deleted, because the lookup stops at the first one it finds. I confirmed this against our table by testing a cutoff every 10 seconds across 28 days, and none of them matched a newer row.
Option 2: delete by id, plus a 10-minute window
This adds a second step to catch the out-of-order rows from Option 1. The cleaner also finds the first row newer than the cutoff plus 10 minutes, then runs DELETE ... WHERE id >= <first id> AND id < <second id> AND timestamp < <cutoff>.
The second step only reads about 10 minutes of rows by primary key, so it stays cheap. On our table a 5-minute window caught every leftover row, so 10 minutes leaves some headroom.
Estimated per pass at 1,000 VMs, one management server writing stats (PR #14398), 30-day retention (43.2M rows, ~1,000 expiring each minute):
| Current | Option 1 | Option 2 | |
|---|---|---|---|
| Rows read and locked | 43.2M | ~1,000 | ~11,000 |
| Read + lock | ~37 s | ~1 ms | ~9 ms |
| Delete 1,000 rows | ~25 ms | ~25 ms | ~25 ms |
| Total | ~37 s | ~26 ms | ~35 ms |
I therefore recommend Option 2. It is significantly faster than the current delete, which means less lock time and less load on the DB, and on our table it deleted exactly the same rows as the current delete.
To confirm this I compared Option 2 against the current delete on our table (3.08M rows) at 88 cutoffs: one per day for 28 days, 40 random cutoffs, and the 20 cutoffs where Option 1 left the most rows behind (30 rows each). At every cutoff Option 2 deleted exactly the same rows as the current delete.
| Cutoff times tested | Cutoffs | Rows where Option 2 and current deletes differ | Expired rows Option 1 left for a later pass |
|---|---|---|---|
| One per day, 1-28 days ago | 28 | 0 | 0 |
| Random times | 40 | 0 | 0 |
| Times where Option 1 left the most rows | 20 | 0 | 30 each |
If anyone wants to run this on their own DB to cross-check, set @cutoff to any time inside your data and run the following. It only reads, inside a read-only transaction, and takes about a second per 3M rows. rows_where_option2_and_current_differ should be 0.
START TRANSACTION READ ONLY;
SET @cutoff = UTC_TIMESTAMP() - INTERVAL 1 DAY; -- any cutoff inside your data
SET @first_newer_id = NULL, @window_end_id = NULL;
SELECT MAX(id) + 1 INTO @max_id FROM vm_stats;
SELECT id INTO @first_newer_id FROM vm_stats WHERE timestamp >= @cutoff ORDER BY id LIMIT 1;
SELECT id INTO @window_end_id FROM vm_stats WHERE timestamp >= @cutoff + INTERVAL 10 MINUTE ORDER BY id LIMIT 1;
SET @window_end_id = COALESCE(@window_end_id, @max_id);
SELECT SUM(timestamp < @cutoff) AS current_deletes,
SUM(id < @first_newer_id OR (id < @window_end_id AND timestamp < @cutoff)) AS option2_deletes,
SUM((timestamp < @cutoff) <> (id < @first_newer_id OR (id < @window_end_id AND timestamp < @cutoff))) AS rows_where_option2_and_current_differ,
SUM(id >= @first_newer_id AND timestamp < @cutoff) AS expired_rows_option1_leaves_for_later
FROM vm_stats;
COMMIT;
- 主要語言
- Java
- 星號
- 3.1k
- 分支
- 1.4k
- 平均合併
- 7 天 16 小時
- 30 天內合併 PR
- 15
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
apache/cloudstack 的其他 Issue
-
resizeVolume of a running system VM's volume is accepted but the VM never sees the new size可能已有人在做 @bhouse-nexthop 於 3 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
apache/cloudstack#14645 · 1 則留言 ·
維護者通常 1 天內回覆
-
VR reports a load balancer change as applied when haproxy.cfg cannot be replaced可能已有人在做 @bhouse-nexthop 於 3 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
apache/cloudstack#14633 · 1 則留言 ·
維護者通常 1 天內回覆
-
restartNetwork reports a ResourceUnavailableException as an unexpected error (530), unlike restartVPC (534)可能已有人在做 @bhouse-nexthop 於 3 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
apache/cloudstack#14631 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
apache/cloudstack#14625 · 1 則留言 ·
維護者通常 1 天內回覆
-
Remote access VPN users are disconnected on every LB, ACL, port forward or firewall change (xl2tpd restarted on every iptables pass)可能已有人在做 @willbennett-devops 於 12 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
apache/cloudstack#14545 · 1 則留言 ·
維護者通常 1 天內回覆
查看 apache/cloudstack 的全部 Issue
相似的 Issue
-
[Bug]: Nullable implicit operator on string extensible enums still throws for a null string可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉bug emitter:client:csharp
難度 2/5 1-3 小時 新手友好度 84/100
microsoft/typespec#12572 · 1 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
[BUG]: Java SDK MountRequest always sends update-optional fields, resetting stored mount settings未關閉bug
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)未關閉
難度 2/5 1-3 小時 新手友好度 73/100
維護者通常 1 天內回覆
-
[Bug] RemotingHelper.connect swaps the socket send and receive buffer configuration可能已有人在做 @amalsaidov 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
維護者通常 1 天內回覆
-
needs-area-label
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 2 天內回覆