Skip to content

fix: reuse budgeted dataset event sources (#222) - #225

Merged
codeasier merged 5 commits into
mainfrom
fix/222-source-reuse
Oct 10, 2026
Merged

codeasier merged 5 commits into
mainfrom
fix/222-source-reuse

Conversation

@codeasier

@codeasier codeasier commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

依赖

依赖 #224。为运行面向 main 的完整 CI,暂以 main 为目标,因此当前差异包含 #224;建议先合并 #224,再复核本 PR。

变更

在单次查询操作内复用已计入预算的 EventSource,避免 callstack 分析、排序和结果输出重复读取同一事件。v2 堆栈文本在所属分片解析;帧读取和不同来源记录继续计入实际工作预算。

验证

  • 84 项已有 global/attribution/projection 测试及 7 项新增复用测试通过。
  • fixture provenance 7 项通过;basedpyright 0 errors(1173 warnings);相关 Ruff、Black、diff 检查通过。
  • 60k 事件的堆栈分析及按堆栈排序 limit=1,预算行数为 60001(含输出)。
  • 49,999 active blocks 的 99999 行通过;50k 的 100001 行仍按真实工作量拒绝。3k 长堆栈通过,5k 仍触发字节预算。
  • 全套本地测试和远端 CI 尚在运行,结果稍后补充。远端 tree 与已测试提交一致。

未修改 snapshot 源码,无需追加 snapshot provenance。
Closes #222
Related to #217

检视修正

  • 缓存命中保留精确 int 校验,拒绝 bool/float 与整数键相等造成的别名;源行所属分片在冷、热缓存下均 fail-closed。
  • 明确操作级源缓存的生命周期和序列化预算不是 RSS 上限;扫描投影从调用方所需列派生。
  • 增加 v1/native v2、ASC/DESC 堆栈排序分页与 standalone 的真实事件范围差分,确认不泄露 callstackId。
  • 云端固定源码:fixture provenance 7/7,相关回归 54/54,Ruff/Black/diff 通过,basedpyright 0 errors(1168 informational warnings);完整最终套件 1950/1950 通过(286.75s);wheel 构建、审计与安装验收通过。安装验收使用仅依赖目录的临时 venv shim,不改变产品来源检查。
    未修改 snapshot 源码或新增可执行 fixture。后续集成 PR 需同步这些修正并重新验收。

Copy link
Copy Markdown
Owner Author

最终验证更新:eda49166421dcab5c73fe7d107504122d701928c 的完整 CI 已通过。对应云端源代码全套 1942 项测试通过,Ruff/Black 通过,basedpyright 为 0 errors(1168 warnings)。

审查中补充了两项回归保护:ID 排序 event 仍只为选中页读取堆栈;按堆栈排序时,ordered frames 延迟至选中页,并在单次操作内复用分片 schema 检查。10k × 8KiB 堆栈的 event(limit=1) 为 10,002 行 / 1,245,073 字节;600 事件的 ordered-frame 排序页为 610 行,未免除真实帧和输出的预算。依赖 #224,继续保持草稿等待整组验收。

@codeasier codeasier left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本轮检视未发现 High/Medium 问题,核心改动(按列投影、操作级缓存、仅对选中页加载帧)与代码一致。以下为 Low/Info 建议,均为非阻断项。

Comment thread src/pt_snap_cli/core/dataset_sources.py Outdated
result: dict[int, EventSource] = {}
for event in sorted(set(event_ids)):
self.budget.remaining()
if event in self._events:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] 缓存命中检查位于 owner() 的类型校验之前。由于 hash(True) == hash(1) 且 True == 1,同一个 resolver 中先 events([1]) 再 events([True]) 会返回事件 1 的 source,而原逻辑返回 {};1.0 同理。CLI/API 因 QueryService 拒绝 bool 端点暂不可达,但直接调用 resolver 的代码会受影响。建议只对精确 int 命中缓存:

Suggested change
if event in self._events:
if type(event) is int and event in self._events:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 973843032d61a999ec9d00b544f8bb89d1ff49e5。

已修正缓存命中前的精确 int 校验。新增冷/热缓存 True 与 1.0 回归,确认返回空结果且不产生额外读取;整数 1 的复用保持不变。

验证:相关回归 54/54、provenance 7/7,Ruff/Black/diff 与 type check(0 errors)通过。最终完整套件 1950/1950,wheel 构建/审计/安装验收通过。远端提交与 CI 另行补充。

local_stack_id=local_id,
)
fresh = {event: source for event, source in result.items() if event not in self._events}
self._events.update(fresh)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] events_from_rows 把每个扫描行写入 self._events,并保留到整个操作结束。旧的 _stacks 每批结果处理后即释放。预算上限是序列化字节(注释也说明不是 RSS 上限),Python 对象开销会使实际内存高于计费字节。去除双读的取舍是合理的,但建议在注释中说明,或在多分片 v1 数据集上测一下峰值内存。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 973843032d61a999ec9d00b544f8bb89d1ff49e5。

已在 events_from_rows 文档中明确:源对象保留到当前 resolver 操作结束,序列化值预算不包含 Python 对象开销,也不是 RSS 上限。本次未将其表述为峰值内存保证。

验证:相关回归 54/54、provenance 7/7,Ruff/Black/diff 与 type check(0 errors)通过。最终完整套件 1950/1950,wheel 构建/审计/安装验收通过。远端提交与 CI 另行补充。

Comment thread src/pt_snap_cli/core/dataset_global.py Outdated
for item in items:
trace = f'"trace_entry_{sources.device.device_id}"'
if with_sources:
projection = (

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] with_sources=True 分支会覆盖上方根据 columns 生成的 projection,传入的 columns 在该分支被静默忽略,列表也与 _events 中的字段知识重复。当前两处集合一致,没有 bug,但 METRICS 或事件列变化时可能分叉。建议由 columns 加上 callstack 项派生这个投影。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 973843032d61a999ec9d00b544f8bb89d1ff49e5。

已删除 with_sources 分支的重复列清单,投影直接使用调用方 columns 后追加布局对应的堆栈字段;callstack_analysis 显式传入完整源行所需列。既有预算及 source reuse 回归通过。

验证:相关回归 54/54、provenance 7/7,Ruff/Black/diff 与 type check(0 errors)通过。最终完整套件 1950/1950,wheel 构建/审计/安装验收通过。远端提交与 CI 另行补充。



@pytest.mark.parametrize("direction", ["ASC", "DESC"])
def test_event_projection_preserves_callstack_sort_and_paging(tmp_path, direction):

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] 测试覆盖缺口:按 callstack 排序的 event 路径只在 v1 fixture 上测试。tests/core/test_dataset_global.py 的 standalone parity 用例也从未设置 order_by=callstack。建议补充 v2 的 callstack 排序分页用例,并在 parity 比较中加入 order_by=callstack(两个方向)。已核对:返回行由缓存的、已弹出 callstackId 的副本构造,不会泄露该字段。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 973843032d61a999ec9d00b544f8bb89d1ff49e5。

已增加 v1 与 native v2 的 callstack ASC/DESC 排序分页差分,使用真实事件范围与 standalone 比较行、总数和分页标志,同时断言不泄露 callstackId;覆盖多设备。

验证:相关回归 54/54、provenance 7/7,Ruff/Black/diff 与 type check(0 errors)通过。最终完整套件 1950/1950,wheel 构建/审计/安装验收通过。远端提交与 CI 另行补充。

Comment thread src/pt_snap_cli/core/dataset_sources.py Outdated
result[event_id] = self._events[event_id]
continue
if self.owner(event_id) != item:
raise QueryExecutionError("Source event does not belong to the selected shard.")

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] 这是防御性检查(契约校验已保证 owner 与分片一致),目前没有任何测试覆盖它。另外这是行为变化:旧路径会从真实所属分片重新解析,新路径直接抛出 QueryExecutionError。仅影响手工伪造且 hash 也被改写的数据集,fail-closed 合理。建议补一个直接调用 events_from_rows 传入其他分片行的单元测试,或去掉该检查;并在 PR 描述中注明这一行为变化。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 973843032d61a999ec9d00b544f8bb89d1ff49e5。

已补充错误分片源行的冷、热缓存回归,并将所属分片检查移到缓存命中之前,避免热缓存绕过 fail-closed 检查。PR 描述同步注明这一直接输入边界行为。

验证:相关回归 54/54、provenance 7/7,Ruff/Black/diff 与 type check(0 errors)通过。最终完整套件 1950/1950,wheel 构建/审计/安装验收通过。远端提交与 CI 另行补充。

Copy link
Copy Markdown
Owner Author

为使本轮检视修正可运行 PR CI,追加了仅调整祖先关系的 merge commit 2ad2976c1d9d2f1d91933f9d54fe01642ef0f0d4。

已核对 main 的 #224 squash 提交 7aaca357 与原依赖 c5ed2a8d 具有相同父提交和完全相同源码 tree 681b0a72acc0bf3dcbf903a805df65eeab912a88。因此将当前 main 作为额外父提交,保留本 PR 已验证的 tree 0b98bdabb2c2dbcda38b5f43bec79f46dea34b51 不变,无源码覆盖、force-push 或合并 PR 操作。新 head 的 CI 另行核验。

Copy link
Copy Markdown
Owner Author

本轮检视修正的最新提交 2ad2976c1d9d2f1d91933f9d54fe01642ef0f0d4 已通过完整 CI。已验证新提交本身,不沿用旧 head 的结果。PR 仍保留草稿,未执行合并;评审回复留待复核。

@codeasier
codeasier marked this pull request as ready for review October 10, 2026 12:57
@codeasier
codeasier merged commit b3eef3e into main Oct 10, 2026
6 checks passed
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.

[H5] callstack_analysis / active_*_at_event 重复取数,有效预算减半

1 participant