Skip to content

fix(kvdb)/避免将空白 USING 扇区误标记为 ERR_HDR - #440

Draft
brightness-810 wants to merge 5 commits into
armink:masterfrom
brightness-810:fix/kvdb-blank-using-sector
Draft

brightness-810 wants to merge 5 commits into
armink:masterfrom
brightness-810:fix/kvdb-blank-using-sector

Conversation

@brightness-810

@brightness-810 brightness-810 commented Nov 13, 2026 •

Copy link
Copy Markdown

What this changes

Fix three KVDB issues observed during power-loss recovery:

  • Recover invalid sector headers during initialization when at least one valid header remains. Precheck all headers before modifying flash, preserve valid-header sectors, and reject a fully invalid partition without writes or erases.
  • Avoid programming ERR_HDR into fully erased data areas during USING-sector space calculation and internal USING/FULL-sector iteration. Preserve the correct free address and capacity.
  • Advance the initial cached free address past a status-only first KV header to prevent overlapping KV status tables.

Recovery-policy change: invalid-header sectors are erased and reformatted even when automatic formatting is disabled. A sector with an invalid header may still contain unique data, so this recovery can lose data. Repairs completed before a later repair failure are not rolled back.

The blank-data guards and allocation-boundary adjustment reuse the existing FF-boundary helper and do not themselves add flash reads or writes. Existing ERR_HDR or DELETED records are not resurrected. The FF-boundary helper ignores underlying read errors, so these changes do not establish ECC/read-error safety or comprehensive GC safety.

Related issue

Closes #439

Test closure

  • Added or updated cases in tests/fdb_kvdb_tc.c or tests/fdb_tsdb_tc.c
  • New cases are registered in the UTEST_UNIT_RUN(...) list of testcase()
  • Cases cover boundaries (sector full / GC triggered / blob crossing write granularity / init check failure)
  • Bug fix: the case failed before the fix and passes after, and its name carries the issue number
  • No new case needed, reason: N/A; this is a behavior-changing bug fix and requires upstream regression tests

Validation currently uses a standalone host RAM-flash harness. The cases have not been ported to the upstream UTEST files or registered with UTEST_UNIT_RUN(...).

Local evidence:

  • A historical pre-fix replay reproduced successful initialization followed by a failed key lookup. Its complete 32,768-byte output matched the captured hardware failure image byte for byte.
  • Nine related tests passed against the local implementation with KV/sector caches disabled and enabled: 18 replay executions.
  • Coverage includes PRE_DELETE recovery into a blank USING sector, an EMPTY-sector control, blank-sector space calculation, internal lookup over a blank FULL sector, first-KV creation, a non-erased final byte, a partially programmed header, a later valid KV behind an erased first slot, and preservation of an already-failed snapshot.
  • Recovery checks include record status, value contents, application CRC, generated payload, write/erase counts, and readability after reopening.
  • An iterator-only comparison still programmed ERR_HDR during actual space recalculation and advanced the free address by one KV header. This supports retaining the space-calculation guard.

CI status at the current PR head:

  • The BSP builds succeeded, but the QEMU KVDB UTEST failed for all six FDB_WRITE_GRAN values. TSDB passed.
  • The available CI output does not include the failing assertion, so the failure has not been attributed to a specific commit.

Remaining work:

  • Validate the isolated PF-005 patch against the selected upstream base.
  • Port the required regressions to tests/fdb_kvdb_tc.c and register them in testcase().
  • Include the actual issue number in the new regression case name.
  • Investigate the KVDB UTEST failure before claiming CI validation.
  • Add coverage for actual GC triggering, allocation exhaustion, write-granularity crossing, and initialization I/O failures.

Write granularity covered

  • CI passed for FDB_WRITE_GRAN = 1 / 8 / 32 / 64 / 128 / 256
  • Local manual verification only, granularities: 128
  • Not verified, reason and risk: CI attempted all six values, but the KVDB UTEST failed for each. The local host tests use 128-bit granularity only; other granularities remain unverified.

Data layout and compatibility

  • KV/TSL node format unchanged
  • Node format changed: N/A
  • Public struct and API in inc/flashdb.h unchanged
  • API or config macro changed: N/A; no API or configuration changes were made

No changes to persistent status encoding, public structures, APIs, or configuration macros. No data migration is required. The fix does not convert existing ERR_HDR or DELETED records back to WRITE.

Power-loss semantics

  • Not on a write path
  • On a write path, covered by host fdb_kvdb_deinit() -> fdb_kvdb_init() simulation for the blank-sector recovery cases
  • On a write path, not coverable by UTEST: N/A

The guards affect allocation and startup recovery. Although the blank-data guards do not write flash themselves, they prevent read_kv() from programming ERR_HDR. The invalid-header recovery path does erase and rewrite flash.

The host reboot simulation does not establish safety for every interruption window, invalid-header recovery, or GC source erase. A post-fix hardware power-cut campaign remains outstanding.

掉电发生在扇区更新为 USING、首条 KV 尚未写入之间时,
可能留下扇区头有效但数据区全 FF 的状态。

恢复 PRE_DELETE 记录时,内部查找可能将已分配的空白目标
误判为损坏 KV 并写入 ERR_HDR。随后搬移仍在同一地址提交
WRITE,并将旧副本标记为 DELETED,导致该 key 无法正常查询。

在空间计算和内部 KV 遍历入口检查整个数据区:
- 空间计算遇到全 FF 数据区时,保留首个空闲地址和完整容量
- 内部遍历跳过数据区全 FF 的 USING/FULL 扇区
- 非空数据区保持原有处理,不改变存储格式或增加擦除
An interrupted first KV write can leave the trailing-FF boundary
inside its status table. Caching this boundary as free space allows
recovery to allocate a replacement inside the incomplete header.

A subsequent search can mark the residual header ERR_HDR at a byte
that also belongs to the replacement's DELETED slot. Committing WRITE
does not clear that marker, and deleting the source leaves no readable
copy.

Advance the initial cached free address past the complete first KV
header when the FF boundary falls within its status-table interval.
Keep the cache-hit condition, capacity calculation, and on-flash
format unchanged, without adding flash reads or writes.
@brightness-810 brightness-810 changed the title fix(kvdb): 避免将空白 USING 扇区误标记为 ERR_HDR fix(kvdb): improve power-loss recovery for sector headers and KV allocation Nov 13, 2026
The KVDB QEMU UTEST fails for all tested FDB_WRITE_GRAN values, but the
available CI output does not show the failing assertion. Revert only the
startup invalid-sector-header recovery so the earlier KVDB fixes remain
isolated while the failure is investigated.
@armink

armink commented Nov 16, 2026

Copy link
Copy Markdown
Owner

CI 看着一个都没过

@brightness-810

Copy link
Copy Markdown
Author

我分别提交测试下哪笔commit有问题

@brightness-810 brightness-810 changed the title fix(kvdb): improve power-loss recovery for sector headers and KV allocation fix(kvdb)/避免将空白 USING 扇区误标记为 ERR_HDR Nov 17, 2026
@brightness-810
brightness-810 marked this pull request as draft November 19, 2026 03:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[KVDB 掉电恢复] 空白 USING 扇区被误写 ERR_HDR,导致恢复后新旧副本均不可查询

2 participants