Skip to content

fix(tsdb): fix invalid cur_sector after reboot when all sectors are full - #447

Open
paxtonbacon wants to merge 2 commits into
armink:masterfrom
paxtonbacon:fix/tsdb-all-full-sectors-reboot
Open

paxtonbacon wants to merge 2 commits into
armink:masterfrom
paxtonbacon:fix/tsdb-all-full-sectors-reboot

Conversation

@paxtonbacon

@paxtonbacon paxtonbacon commented Nov 26, 2026 •

Copy link
Copy Markdown

What this changes

我们设置rollover为false进行使用,在使用过程中所有扇区被写满。当我们再次尝试append时,最后一个sector.status从USING变为FULL,此时所有扇区状态全部为FULL。
此时进行重启,执行fdb_tsdb_deinit,设置值后执行fdb_tsdb_init。
由于fdb_tsdb_init中db->rollover始终设置为true,其值本函数中不进行修改,所以永远无法访问下面的else分支:

/* default rollover flag is true */
db->rollover = true;
if (db->rollover) {
  latest_addr = db->cur_sec.addr;
} else {
  latest_addr = db->cur_sec.addr = db_max_size(db) - db_sec_size(db);
}

事实上,这个if-else实际上是区分没有空扇区时的两种状态:

  • 一种是其中有一个扇区状态是USING,其他扇区全为FULL
  • 另一种是所有扇区全为FULL(只有当rollover为false的时候,写满后添加,USING不会回环后移,导致所有扇区全为FULL)。

第二种情况时db->cur_sec不会被下面check_sec_hdr_cb回调赋值,保留db->cur_sec.addr = FDB_DATA_UNUSED;初始化时的无意义值(因为该回调中没有处理FULL的分支)

sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, &check_sec_arg, NULL, check_sec_hdr_cb, true);

所以修改条件区分上述两种状态,使其可达即可,对cur_sec.addr准确赋值,修改如下:

if (db->cur_sec.addr != FDB_DATA_UNUSED) {
  /* There is no empty sector, but one is using. */
  latest_addr = db->cur_sec.addr;
} else {
  /* There is no empty sector and all are full (only when not rollover). */
  latest_addr = db->cur_sec.addr = db_max_size(db) - db_sec_size(db);
}

同时fdb_tsdb_init后续对于last_time的恢复也存在未考虑rollover为false时cur_sec.status为FULL的情况,所以在if-else if中补充一个else if,修改(添加)如下:

else if (db->cur_sec.status == FDB_SECTOR_STORE_FULL) {
  /* when not rollover, current sector may be full */
  db->last_time = db->cur_sec.end_time;
}

在tests/fdb_tsdb_tc.c中已添加测试用例并已注册。

Related issue

Closes #291
Closes #207
Closes #148

Test closure

  • Added or updated cases in 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: (pure comment / docs / formatting, or a behaviour-preserving refactor already covered)

Write granularity covered

  • CI passed for FDB_WRITE_GRAN = 1 / 8 / 32 / 64 / 128 / 256
  • Local manual verification only, granularities: 1 / 8 / 32 / 64 / 128 / 256
    CI-equivalent local run: same source-copy step, scons --global-macros
    build and qemu_runner.py flow as .github/workflows/main.yml, run twice
    (PR source and unfixed parent source) at all six granularities. Not the
    GitHub Actions runner: local ARM GCC 14.2.1 instead of the CI's GCC 10, and
    the BSP snapshot carries local compatibility flags for GCC 14.
  • Not verified, reason and risk: real hardware power loss; optional read-cache
    configuration. Risk limited to those two paths.

Data layout and compatibility

  • KV/TSL node format unchanged
  • Node format changed: version bumped, old data migrates by
  • Public struct or API in inc/flashdb.h changed, compatibility impact:
  • API or config macro changed, and docs/ plus docs/zh-cn/ are updated

Power-loss semantics

  • Not on a write path
  • On a write path, covered by the fdb_tsdb_deinit -> fdb_tsdb_init reboot simulation
  • On a write path, not coverable by utest, verification method:

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