apache / apache/cloudstack

GenericDaoBase.persist() leaves the caller's transaction unbalanced when an insert throws

未關閉
#13,905 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug
主要語言
Java
星號
3.1k
分支
1.4k
平均合併
7 天 14 小時
30 天內合併 PR
31

描述

### 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:

```java
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

1. On 4.22.1.0 with the Usage Server enabled, deploy an instance. Its ROOT volume produces a `VOLUME.CREATE` usage event carrying `vm_id`.
2. `UsageManagerImpl.createVolumeHelperEvent()` performs two `persist()` calls sharing `(volume_id, created)`; the second violates `usage_volume`'s unique key (see #13399).
3. `createHelperRecord()` catches the resulting `EntityExistsException` and logs a warning, intending to continue.
4. Observe `txn: Commit called when it is not a transaction` shortly afterwards, and that the `processed` flags set for that batch of events in `cloud_usage.usage_event` were 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.

貢獻指南

開啟貢獻指南

研究方向

先追蹤 framework/db 中的 GenericDaoBase.persist() 以及 TransactionLegacy 的巢狀與 commit 行為,接著檢查 UsageManagerImpl.createHelperRecord() 和 createVolumeHelperEvent() 這條重現路徑。確認 insert 失敗不會讓呼叫端的交易處於不平衡狀態,也不會讓其後續 commit 靜默變成 no-op,同時處理 EntityExistsException 的呼叫端能夠取得預期的失敗行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
backend, database
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
活躍
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。