frotmo opened a new pull request, #11576:
URL: https://github.com/apache/rocketmq/pull/11576
### Which Issue(s) This PR Fixes
- Fixes #11574
### Brief Description
This PR prevents the RocksDB transaction check path from deleting a half
record when the half message body cannot be read.
#### Background
`TransactionalMessageRocksDBService#checkTransRecordsStatus` currently treats
`getMessage() == null` as "the message does not exist" and sets
`halfRecord.setDelete(true)`. However, `TransMessageRocksDBStore#getMessage`
retries three times, and `StoreUtil.getMessage` returns null when:
- `messageStore.getData(...)` returns false (commitLog/mmap read failure,
truncated file, invalid offset, ...);
- `MessageDecoder.decode(...)` throws (partial/corrupt bytes);
- any exception occurs while reading.
So null is an ambiguous "could not read/validate" result, not a proof of
absence. Deleting the record on the first null removes the only durable
handle
used for transaction check-back. The old
`TransactionalMessageServiceImpl` path retries/advances and only calls
`resolveDiscardMsg` after `needDiscard`/`needSkip`; the default listener
moves
the message to `TRANS_CHECK_MAXTIME_TOPIC` instead of silently deleting it.
Scope: this only affects deployments with `transRocksDBEnable=true`; the
default path is unchanged.
#### Change
When `getMessage` returns null:
1. If the record is invalid (negative offset, non-positive size, or size >
`maxMessageSize`), keep the existing cleanup behavior: mark it deleted.
2. If the record is older than the commitLog min physical offset, the body
has
definitely been purged; keep the existing cleanup behavior: mark it
deleted.
3. Otherwise, treat the result as transient/unknown: do not delete the
record,
do not increment `checkTimes`, log a warning, and retry on the next
`transactionCheckInterval`.
Key part of the diff:
```java
if (null == msgExt) {
if (isInvalidHalfRecord(halfRecord) || isExpiredHalfRecord(halfRecord)) {
halfRecord.setDelete(true);
updateList.add(halfRecord);
} else {
log.warn("... keep record for next check ...");
}
continue;
}
```
```java
private boolean isInvalidHalfRecord(TransRocksDBRecord halfRecord) {
return halfRecord.getOffsetPy() < 0L
|| halfRecord.getSizePy() <= 0
|| halfRecord.getSizePy() >
messageStore.getMessageStoreConfig().getMaxMessageSize();
}
private boolean isExpiredHalfRecord(TransRocksDBRecord halfRecord) {
return halfRecord.getOffsetPy() < messageStore.getMinPhyOffset();
}
```
#### Why this design
- It does not assume that null means "permanently missing"; it distinguishes
"definitely invalid/purged" from "unknown".
- `minPhyOffset` is a conservative discriminator: below it, the commitLog
file
containing the record is already gone; otherwise the message may still be
valid and the read may succeed on the next scan.
- `checkTimes` is left untouched on a read failure. It is the transaction
check-back counter, not a read-failure counter; incrementing it would let a
transient outage exhaust `transactionCheckMax` and indirectly delete the
record.
- The existing max-check behavior for successfully read messages is
unchanged.
#### Alternatives considered
- **Typed read result**: change `getMessage` to return
`FOUND / NOT_FOUND / READ_ERROR / INVALID`. Cleaner long term, but a larger
API change; happy to follow up if maintainers prefer this direction.
- **Retain every null**: simplest, but permanent orphans would accumulate and
log every interval; the invalid/expired checks avoid that for the obvious
cases.
- **Keep current behavior**: avoids the orphan-retention concern, but loses
the
recovery handle on transient read failures.
#### Known limitation / follow-up
A record whose offset is still within `[minPhyOffset, maxPhyOffset]` but
whose
body is permanently corrupt/unreadable will be retained and logged on each
scan. This PR intentionally favors data safety over silent deletion. A
follow-up can add an age/quarantine policy or a dedicated read-failure metric
without conflating it with `checkTimes`.
This PR is intentionally opened as a draft to confirm the null/read-failure
contract first. If maintainers prefer a typed `getMessage` result, I can
rework this PR around that design instead.
### How Did You Test This Change?
Added `TransactionalMessageRocksDBServiceNullReadRegressionTest` with four
cases:
1. valid offset, null read: record retained, `checkTimes` unchanged, no
`updateRecordsForTrans` call;
2. offset == minPhyOffset, null read: record retained (boundary);
3. offset < minPhyOffset, null read: record deleted as expired;
4. invalid size, null read: record deleted as invalid.
The two "retain" cases fail against the unmodified code (`delete=true`) and
pass with this change. The expired/invalid cases keep the previous cleanup
behavior. The existing `TransactionalMessageRocksDBServiceTest` also passes.
Commands:
```bash
mvn -pl broker \
-Dtest=TransactionalMessageRocksDBServiceNullReadRegressionTest,TransactionalMessageRocksDBServiceTest
\
test
```
Result:
```text
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[KEEP] offsetPy=123 sizePy=100 checkTimes=0 delete=false
BUILD SUCCESS
```
`checkstyle: check` passed and `spotbugs:check` reported `BugInstance size
is 0`.
The tests use reflection to invoke the private `checkTransRecordsStatus`
method, following the existing `TransactionalMessageRocksDBServiceTest`
pattern. No production API or configuration is changed.
--
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]