Non-deterministic pagination in /api/v2/search when rows share the same timestamp

未关闭
#2,614 3 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
clickhouse, typescript
领域
api, backend, databases

调研方向

从 POST /api/v2/search 端点的实现开始,跟踪应用 Timestamp 排序和 LIMIT/OFFSET 分页的 ClickHouse 查询。验证 _block_number 和 _block_offset 的相关表设置,然后检查端点现有的测试,或补充针对相同 timestamps 的覆盖。连续页面具有确定性排序,且没有跳过或重复行,即表示完成。

由索引模型根据 Issue 内容生成。

描述

external
Problem

The POST /api/v2/search endpoint (added in [hyperdxio/hyperdx#2258](https://github.com/hyperdxio/hyperdx/pull/2258)) uses LIMIT/OFFSET pagination with results ordered by Timestamp. When multiple rows share the same timestamp, pagination becomes non-deterministic -- rows near a page boundary can be duplicated or skipped across pages.

Example: If rows 90-110 all have the same Timestamp value and the client requests maxResults=100, offset=0 followed by maxResults=100, offset=100, ClickHouse arbitrarily assigns which rows fall on which page. The client may see some rows twice and miss others entirely.

Why this happens in practice

While Timestamp is DateTime64(9) (nanosecond precision), duplicate timestamps are common in real-world OTEL pipelines:

  • Collector-assigned timestamps: When logs arrive at the OTEL collector without a timestamp, the collector assigns one. An entire batch receives the same timestamp.
  • Limited source precision: Many language runtimes only produce microsecond or millisecond precision. The remaining digits are zero-padded, reducing the effective keyspace.
  • High-throughput services: Multiple log lines from the same flush, HTTP request, or function call naturally share the same wall-clock time.

Adding search predicates reduces the likelihood (fewer matching rows means fewer ties), but the issue is still possible in any high-volume deployment.

Proposed solution

For tables with enable_block_number_column = 1 and enable_block_offset_column = 1 (which ClickStack already enables on otel_logs), add _block_number and _block_offset as tiebreaker columns in the ORDER BY clause:

ORDER BY Timestamp DESC, _block_number DESC, _block_offset DESC
LIMIT {maxResults} OFFSET {offset}

This produces a deterministic total ordering since (_block_number, _block_offset) uniquely identifies each row within a part. The additional columns should have negligible performance impact when using read_in_order.

Alternatives considered
  • Cursor-based pagination (filter by WHERE Timestamp < :last_seen): Avoids offset entirely but still non-deterministic when ties exist at the cursor boundary. Would also require a tiebreaker.
  • Nanosecond precision alone: Reduces probability but does not eliminate the issue, especially for collector-assigned timestamps.
主要语言
TypeScript
星标
10.1k
派生
527
平均合并
1 天 20 小时
30 天内合并 PR
186

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/ClickStack 的其他 Issue

查看 ClickHouse/ClickStack 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。