feat(api-pull): 完善全悉设备接入并修复 NDR 边界重复采集

- 增加全悉设备 API 日志拉取及消费处理
- 完善全悉日志解析、字段映射和标准化逻辑
- NDR 采集窗口统一采用左闭右开规则
- 对设备秒级查询范围向外取整,并在本地精确过滤
- 区分接口原始返回数和实际接收数,保证分页偏移正确推进
- 补充时间窗口、边界过滤和分页推进测试
- 更新设备 API 拉取及对接说明文档
This commit is contained in:
王绍宏
2026-09-07 18:05:27 +08:00
parent 957f7193a3
commit 0837dd116a
23 changed files with 1337 additions and 36 deletions
@@ -117,7 +117,7 @@ public class ApiPullRunner {
try {
// safetyDelay 之后的数据仍可能在设备端持续变化,只处理已经闭合的逻辑窗口。
Instant start = ApiPullTime.toInstant(checkpoint.getCommittedWindowEnd());
Instant now = Instant.now();
Instant now = Instant.now().truncatedTo(java.time.temporal.ChronoUnit.MILLIS);
Instant closedAt = now.minusSeconds(config.getSafetyDelaySeconds());
Instant plannedEnd = min(start.plusSeconds(config.getWindowSizeSeconds()), closedAt);
if (!plannedEnd.isAfter(start)) {
@@ -220,6 +220,9 @@ public class ApiPullRunner {
long offset = 0;
Long firstTotal = null;
boolean complete = false;
String datasetFingerprint = firstPage.getDatasetFingerprint();
java.util.Set<String> seenExternalIds = adapter.requiresDistinctExternalIds()
? new java.util.HashSet<>() : null;
// 同一任务严格串行分页,offset 只在整页进入 WAL 后递增。
for (int pageNo = 1; pageNo <= config.getMaxPagesPerRun(); pageNo++) {
@@ -247,14 +250,27 @@ public class ApiPullRunner {
// offset 分页依赖稳定数据集;total 漂移时继续翻页可能静默漏数或重复。
if (firstTotal == null) firstTotal = page.getTotal();
else if (firstTotal.longValue() != page.getTotal())
throw new AdapterException("PAGINATION_DATASET_CHANGED", "固定窗口内 NDR total 发生变化");
if (page.getEvents().isEmpty()) {
throw new AdapterException("PAGINATION_DATASET_CHANGED", "固定窗口内分页 total 发生变化");
if (!java.util.Objects.equals(datasetFingerprint, page.getDatasetFingerprint()))
throw new AdapterException("PAGINATION_DATASET_CHANGED", "固定窗口内分页元数据发生变化");
int sourceRecordCount = page.getSourceRecordCount();
if (sourceRecordCount < 0 || sourceRecordCount > config.getPageSize())
throw new AdapterException("PAGINATION_INCONSISTENT", "设备单页返回条数超出请求 limit");
if (seenExternalIds != null) {
for (PulledEvent event : page.getEvents()) {
if (seenExternalIds.size() >= 100000)
throw new AdapterException("PAGINATION_ID_LIMIT", "分页去重保护上限 100000");
if (!seenExternalIds.add(event.getExternalEventId()))
throw new AdapterException("PAGINATION_DUPLICATE_DOCUMENT", "固定窗口出现重复文档 ID");
}
}
if (sourceRecordCount == 0) {
if (offset >= firstTotal) {
complete = true;
logSlowPage(config, run, page, pageOffset, 0, pageStarted);
break;
}
throw new AdapterException("PAGINATION_INCONSISTENT", "NDR 在 total 之前返回空页");
throw new AdapterException("PAGINATION_INCONSISTENT", "设备在 total 之前返回空页");
}
run.setRecordsFetched(run.getRecordsFetched() + page.getEvents().size());
List<CompletionStage<com.haobang.wal.DurableReceipt>> receipts = new ArrayList<>();
@@ -298,9 +314,11 @@ public class ApiPullRunner {
run.setRecordsDurable(run.getRecordsDurable() + 1L);
}
long walMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - walStarted);
offset += page.getEvents().size();
// 适配器可能为了把厂商闭区间转换成平台半开区间而过滤边界记录。offset 必须按设备
// 原始返回数推进,不能按过滤后的 WAL 写入数推进,否则会重复请求同一页。
offset += sourceRecordCount;
if (offset > firstTotal)
throw new AdapterException("PAGINATION_INCONSISTENT", "NDR 返回条数超过首次 total");
throw new AdapterException("PAGINATION_INCONSISTENT", "设备返回条数超过首次 total");
mapper.updateRun(run);
logSlowPage(config, run, page, pageOffset, walMs, pageStarted);
if (offset >= firstTotal) {
@@ -449,10 +467,11 @@ public class ApiPullRunner {
long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started);
if (total < properties.getSlowPageThresholdMs()) return;
// 一条结构化日志覆盖网络、解析和 durable 三段耗时,便于定位千万级吞吐瓶颈。
double rate = p.getEvents().isEmpty() ? 0d : p.getEvents().size() * 1000d / Math.max(1, total);
double rate =
p.getEvents().isEmpty() ? 0d : p.getEvents().size() * 1000d / Math.max(1, total);
log.warn(
"设备 API 慢页: taskId={}, runId={}, adapter={}, stream={}, windowStart={}, windowEnd={},"
+ " offset={}, limit={}, count={}, responseBytes={}, httpMs={}, parseMs={},"
+ " offset={}, limit={}, sourceCount={}, acceptedCount={}, responseBytes={}, httpMs={}, parseMs={},"
+ " walDurableMs={}, totalMs={}, recordsPerSecond={}",
c.getCollectTaskId(),
r.getRunId(),
@@ -462,6 +481,7 @@ public class ApiPullRunner {
r.getLogicalWindowEnd(),
offset,
c.getPageSize(),
p.getSourceRecordCount(),
p.getEvents().size(),
p.getResponseBytes(),
p.getHttpDurationMs(),