GenericDaoBase.persist() leaves the caller's transaction unbalanced when an insert throws
还没有人认领这个 Issue。
评估
调研方向
先跟踪 framework/db 中的 GenericDaoBase.persist() 以及 TransactionLegacy 的嵌套和 commit 行为,然后检查 UsageManagerImpl.createHelperRecord() 和 createVolumeHelperEvent() 这一复现路径。确认 insert 失败不会使调用方的事务处于不平衡状态,也不会使其后续 commit 静默变成 no-op,同时处理 EntityExistsException 的调用方能够获得预期的失败行为。
由索引模型根据 Issue 内容生成。
描述
problem
Split out of #13399 at @DaanHoogland's request.
GenericDaoBase.persist() does not own a transaction, it joins the caller's via TransactionLegacy.currentTxn() and calls txn.start(), which pushes a START_TXN nesting level. On the SQLException path it never reaches txn.commit(), and there is no finally, so that nesting level is leaked:
final TransactionLegacy txn = TransactionLegacy.currentTxn(); // the CALLER's transaction
try {
txn.start(); // pushes a START_TXN nesting level
...
pstmt.executeUpdate(); // throws
...
txn.commit(); // never reached
} catch (final SQLException e) {
logger.error("DB Exception on: " + pstmt, e);
handleEntityExistsException(e); // throws EntityExistsException
throw new CloudRuntimeException("Unable to persist on DB, due to: " + e.getLocalizedMessage());
}
// no finally, the pushed nesting level is never released
Consequence: the caller's own commit() then finds the transaction unbalanced and silently no-ops, logging only:
WARN [db.Transaction.Transaction] txn: Commit called when it is not a transaction:
(TransactionLegacy.commit() — if (!_txn) { LOGGER.warn(...); return false; })
Everything in that transaction is discarded while the caller believes it committed.
Why it matters beyond one call site: callers that deliberately catch EntityExistsException in order to log-and-continue cannot actually continue, because the enclosing transaction is already unrecoverable. UsageManagerImpl.createHelperRecord() is one such caller, and in #13399 this is what converts a single constraint violation into permanent usage-aggregation failure rather than one skipped record.
versions
Observed on CloudStack 4.22.1.0 (EL9 packages), MySQL 8.x / InnoDB.
This is a code-level defect in framework/db rather than an environment-specific one; the code path is not version-specific and hypervisor/storage/network are not relevant.
The steps to reproduce the bug
- On 4.22.1.0 with the Usage Server enabled, deploy an instance. Its ROOT volume produces a
VOLUME.CREATEusage event carryingvm_id. UsageManagerImpl.createVolumeHelperEvent()performs twopersist()calls sharing(volume_id, created); the second violatesusage_volume's unique key (see #13399).createHelperRecord()catches the resultingEntityExistsExceptionand logs a warning, intending to continue.- Observe
txn: Commit called when it is not a transactionshortly afterwards, and that theprocessedflags set for that batch of events incloud_usage.usage_eventwere never committed.
Any caller that hits a constraint violation inside a transaction it owns should show the same behaviour, #13399 is simply a case where it happens on every VM deployment.
What to do about it?
Release the nesting level in a finally, and/or mark the transaction rollback-only so callers receive a real failure instead of a silent no-op.
Either way this needs someone familiar with TransactionLegacy's nesting semantics, since GenericDaoBase backs every DAO in the codebase. I'm raising it rather than proposing a patch.
- 主要语言
- Java
- 星标
- 3.1k
- 派生
- 1.4k
- 平均合并
- 7 天 5 小时
- 30 天内合并 PR
- 28
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/cloudstack 的其他 Issue
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
apache/cloudstack#14222 ·
-
bug component:kubernetes
难度 1/5 1 小时以内 新手友好度 88/100
apache/cloudstack#14180 ·
-
bug component:projects component:UI
难度 1/5 1 小时以内 新手友好度 88/100
apache/cloudstack#14070 · 5 条评论 ·
-
component:backup
难度 2/5 1-3 小时 新手友好度 76/100
apache/cloudstack#14013 ·
-
KVM agent fails to connect to Ceph RBD storage pool after upgrading Ceph client to Tentacle 20.2.4 未关闭bug component:ceph
难度 2/5 1-3 小时 新手友好度 78/100
apache/cloudstack#13989 · 3 条评论 ·
查看 apache/cloudstack 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
-
1.0.0-alpha2 Type/Improvement
难度 2/5 1-3 小时 新手友好度 68/100
wso2/dpdp-accelerator#272 ·
-
bug
难度 2/5 1-3 小时 新手友好度 88/100
apache/rocketmq-dashboard#4860 · 1 条评论 ·
-
agent-audit bug
难度 2/5 1-3 小时 新手友好度 88/100
Vault-Web/cloud-page#144 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
infinispan/infinispan#18150 ·