@timecho/node-red-node-iotdb 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +97 -2
- package/bridge/README.md +15 -0
- package/bridge/build.js +17 -0
- package/bridge/src/IotdbSubscriptionBridge.java +277 -0
- package/bridge/target/iotdb-bridge.jar +0 -0
- package/docs/compatibility.md +27 -0
- package/docs/operations.md +44 -0
- package/docs/production-readiness.md +36 -0
- package/docs/research/subscription-recovery-contract.md +82 -0
- package/docs/validation-report.md +89 -0
- package/examples/01-sql-query.json +8 -0
- package/examples/02-manual-ack.json +8 -0
- package/lib/bridge-client.js +128 -0
- package/lib/consumer.js +117 -0
- package/lib/errors.js +29 -0
- package/lib/sdk-compat.js +62 -0
- package/lib/sql-client.js +113 -0
- package/lib/sql-worker.js +68 -0
- package/lib/subscription-state.js +47 -0
- package/lib/values.js +43 -0
- package/nodes/iotdb.html +87 -0
- package/nodes/iotdb.js +123 -0
- package/package.json +62 -4
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# 部署与运行
|
|
2
|
+
|
|
3
|
+
每个 server 配置拥有独立 SQL Worker(128 MiB V8 old-generation 上限)和有界串行队列。每个 consumer 拥有独立 Java 进程(最大堆 128 MiB);系统总内存还包括线程栈、native buffers 和 Node-RED。根据节点实例数量测量容量,不能把 JVM 堆上限等同于 RSS 上限。
|
|
4
|
+
|
|
5
|
+
## 安装与安全
|
|
6
|
+
|
|
7
|
+
1. 在测试过的 Node.js / Node-RED 版本安装固定 tarball,核对 SHA-256。
|
|
8
|
+
2. 配置 Node-RED adminAuth / HTTPS 或可信反向代理;编辑器只开放给获授权的维护人员。开发环境使用 loopback + SSH 转发。
|
|
9
|
+
3. 显式配置并备份 Node-RED credentialSecret;数据库用户名、密码填写在 server credentials,导出 Flow 不携带密码。以最小权限服务账户运行,Java SDK 和可执行路径只能由管理员配置。
|
|
10
|
+
4. 原生订阅部署匹配 SDK;不要把企业版数据库 JAR 放入公开 npm 产物。可预编译桥接再用 JRE 运行。
|
|
11
|
+
5. TLS SQL 连接必须验证服务端证书;订阅桥接不支持原生 TLS,跨不可信网络需加密隧道。
|
|
12
|
+
|
|
13
|
+
## 数据与确认
|
|
14
|
+
|
|
15
|
+
统一把 INT64 / 时间戳输出为字符串。timestampPrecision 是用户提供的服务端精度标注,必须与实例一致,不负责单位转换。默认 SQL 上限是 10,000 行 / 8 MiB,订阅上限是每 poll 批次 10,000 行 / 8 MiB、最多 4 个未确认批次。
|
|
16
|
+
|
|
17
|
+
手动 ACK 必须在业务实际成功后回送。当前停止保护限制同一 Topic/组同时使用一个 consumer;独立业务使用不同组。批次没有全局事件时间排序保证。在存活会话内,未确认数据可能重投;使用业务主键或可重建的事件键去重,不以短期 ACK token 作为持久业务主键。
|
|
18
|
+
|
|
19
|
+
**本次实测服务端会在关闭或心跳超时后取消订阅关系。** 同组名重建不等于保留进度。插件关闭时先尝试 NACK 在途消息并释放资源,但这不能阻止服务端取消订阅。不得将未确认重投的存活会话测试当作持久恢复证明。
|
|
20
|
+
|
|
21
|
+
## 故障与恢复
|
|
22
|
+
|
|
23
|
+
- SQL 超时或断连:Worker 被回收,下个请求重建连接;原请求不自动重试,尤其写入可能已经成功。根据幂等设计或查询结果判断后处理。
|
|
24
|
+
- 订阅网络错误、Java 退出或超时:关闭旧桥接、使旧 token 失效,报告 `RECOVERY_REQUIRED` 并停止;不自动重新订阅。
|
|
25
|
+
- SDK 可能把断网 poll 转为空结果。桥接层会检查 SDK 可用连接,在暂停和等待 ACK 时也检查;连接失效时报告 `RECOVERY_REQUIRED`。不能以进程存活或持续空批次判断订阅健康。
|
|
26
|
+
- 配置、认证、权限、禁用订阅、Topic 不存在、批次超限、格式不支持:停止消费并报错。若已经开始连接尝试,重部署仍会遇到恢复保护,因为失败结果可能不明确。
|
|
27
|
+
- ACK 失败:Catch 接收错误;同一存活连接中尚存的待确认批次可以显式重试 ACK。连接丢失后停止并先恢复数据,不复用旧 token。
|
|
28
|
+
- 服务端 WAL 保留范围不足可能导致无法恢复旧进度;根据业务恢复窗口配置保留策略并监测存储。客户端不承诺无限期重放。
|
|
29
|
+
|
|
30
|
+
将 Catch、Status、Complete 接入自己的告警与指标流程。观测待确认数、队列拒绝、查询上限错误、重连次数、处理延迟、RSS 与事件循环延迟。订阅业务成功应以 ACK 完成为边界,不能仅以消费者输出数量统计。
|
|
31
|
+
|
|
32
|
+
## 恢复状态处理
|
|
33
|
+
|
|
34
|
+
首次连接前,以 `host + port + model + topic + groupId` 计算状态文件名,在 `<Node-RED userDir>/iotdb-subscriptions/` 中以独占创建、文件同步(Linux 同时同步目录)的方式保存标记。标记不含密码,正常关闭也保留。状态目录必须持久化;换 userDir、主机地址别名或移走状态目录会绕过此本机保护,因此不能据此构建多机故障转移。
|
|
35
|
+
|
|
36
|
+
遇到 `RECOVERY_REQUIRED` 时,保留状态文件和下游业务处理记录,确定最后完成的业务边界,核对数据库中的遗漏范围。当前版本没有自动补数工具;应使用已验证的 SQL/备份导出或由数据库维护人员确认的 WAL 重放流程,按业务幂等规则补齐。只有已完成核对补数,或明确接受重新开始的业务,才可在停止该节点后由管理员归档对应标记文件,显式开始新的一次订阅。不要将删标记、换组名或循环重部署作为恢复手段。
|
|
37
|
+
|
|
38
|
+
研究脚本中的 `positions`/`seekAfter` 在已有位点且 WAL 保留的受控场景中可重放停机数据。首次空位点、新 Region、WAL 清理、跨组并发和异常退出需要分别核对;研究逻辑未接入节点的自动恢复流程。`npm run test:acceptance` 覆盖重开后的消息重投与 ACK 行为。
|
|
39
|
+
|
|
40
|
+
## Ubuntu 开发环境
|
|
41
|
+
|
|
42
|
+
Ubuntu 开发目录为 `/home/<user>/node-red-node-iotdb`;工具和运行目录位于独立用户目录。Node-RED 编辑器绑定 127.0.0.1:1880;SSH 转发将测试主机的 127.0.0.1:6667 接至 TimechoDB。数据库无需改动监听地址或防火墙。
|
|
43
|
+
|
|
44
|
+
开发隧道由 `tools/dev-tunnel.py` 通过已安装 ssh-skill 客户端创建,只使用现有密钥和已验证 host key。结束本地隧道会切断数据库连接;这条隧道不是无人值守生产网络部署方案。
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 验证范围与运行行为
|
|
2
|
+
|
|
3
|
+
本文件归纳 `@timecho/node-red-node-iotdb@0.0.2` 的功能验证范围。该版本更新发布说明和仓库元数据,运行代码沿用 `0.0.1`。
|
|
4
|
+
|
|
5
|
+
## 已验证功能
|
|
6
|
+
|
|
7
|
+
- 注册 `iotdb-server`、`iotdb-operator`、`iotdb-consumer` 三种节点,并通过真实 Node-RED 流程执行 SQL 和原生订阅。
|
|
8
|
+
- TimechoDB 2.0.12.1 树、表模型的 SQL 查询、写入、数据类型转换、Topic snapshot/incremental 订阅和存活会话手动 ACK。
|
|
9
|
+
- Apache IoTDB 2.0.11 的树、表 SQL;该发行构建未启用原生订阅时,节点返回明确错误。
|
|
10
|
+
- SQL 队列、行数/字节上限、连接超时、错误脱敏、Flow 凭据存储和消息上下文。
|
|
11
|
+
- 订阅实际断网时检测连接状态;暂停或等待 ACK 时也检查桥接连接健康度。
|
|
12
|
+
|
|
13
|
+
详细版本范围见[兼容矩阵](compatibility.md),自动化结果见[验证报告](validation-report.md)。
|
|
14
|
+
|
|
15
|
+
## 订阅生命周期行为
|
|
16
|
+
|
|
17
|
+
- TimechoDB 2.0.12.1 在 Consumer CLOSE 或不活跃清理时取消订阅关系并清理组进度文件。相同 Topic 和消费组重新订阅时,未确认记录及停机期间写入是否重新投递取决于服务端的进度保留行为;本版本实测的同组重开窗口内未收到这些记录。
|
|
18
|
+
- 插件首次连接尝试前,会在 Node-RED userDir 写入持久化标记。检测到连接丢失后,consumer 报告 `RECOVERY_REQUIRED`、使旧 ACK token 失效并停止拉取;再次部署同一 Topic/组时,已有标记会阻止直接建立新的消费起点。
|
|
19
|
+
- 该标记只记录启动尝试身份和时间,不保存消息或消费位点。重启前应核对下游处理记录与数据库数据,并按[运行说明](operations.md)完成数据核对或补数。
|
|
20
|
+
- `positions`/`seekAfter` 在已有位点且 WAL 保留的受控场景中重放过停机数据。空位点、新 Region、WAL 清理边界及并发 ACK 需要分别验证;桥接目前不会把这项研究流程作为自动恢复步骤。
|
|
21
|
+
- 连接健康检查用于识别 SDK 空 poll 掩盖的网络故障。它会让失效会话明确停下,恢复连接后仍需按上述步骤核对状态。
|
|
22
|
+
|
|
23
|
+
## 验证命令
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm ci
|
|
27
|
+
npm test
|
|
28
|
+
npm run check
|
|
29
|
+
npm run test:integration
|
|
30
|
+
npm run test:acceptance
|
|
31
|
+
npm pack --dry-run
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
集成测试使用独立的随机测试命名空间、Topic 和消费组。`test:acceptance` 覆盖 Consumer 关闭后使用同一 Topic/组重新启动、检查未确认及离线记录的投递并执行 ACK。运行数据库测试前,应配置与待测服务端版本相匹配的测试地址、凭据和订阅 SDK。
|
|
35
|
+
|
|
36
|
+
首次连接尝试会创建持久化标记。重新运行同一 Topic/组的测试前,应先查看[恢复状态处理说明](operations.md),不要为了重新运行而自动删除标记。
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# TimechoDB 2.0.12.1 订阅恢复接口核验
|
|
2
|
+
|
|
3
|
+
核验时间:2026-11-08。本文记录 TimechoDB 2.0.12.1 原生订阅在关闭、超时和同组重开场景中的接口行为,以及插件的连接保护和恢复状态处理方式。
|
|
4
|
+
|
|
5
|
+
## 环境与可复现版本
|
|
6
|
+
|
|
7
|
+
- 服务端与 Java SDK:TimechoDB 2.0.12.1 / c05111d;树、表两种模型,单 DataNode,原生订阅启用,incremental + SubscriptionRecordHandler。
|
|
8
|
+
- 客户端:Ubuntu 26.04、Java 17.0.20.1、Node.js 22.22.1;通过 loopback SSH 转发访问 Windows 本机数据库。
|
|
9
|
+
- `timechodb-server-2.0.12.1.jar` SHA-256:`5225a14db10b46f17fe11bd4e2309bd8e75f31b3cb3d725b6db4c82e7ef63e6b`。
|
|
10
|
+
- `common-dependency.jar` SHA-256:`59502da20ee798349b7122b36dd18f1829c905768e1dcce5e6c3120f7deab848`。
|
|
11
|
+
- 测试工具只创建随机 `nr_test_` 资源并清理自身资源;没有更改服务端监听、配置、用户数据或服务进程。凭据由测试运行器通过环境或私有管道提供,不放入参数或报告。
|
|
12
|
+
|
|
13
|
+
## 发现一:关闭连接与取消订阅共享清理路径
|
|
14
|
+
|
|
15
|
+
用例:原生 Pull Consumer 订阅、收到一行但不 ACK、正常 close;停机期间写入第二行;以相同 groupId 和 consumerId 重开并 poll。树、表模型均未收到这两行。SQL 仍能查到这些写入。
|
|
16
|
+
|
|
17
|
+
服务端日志记录 CLOSE 前执行 `unsubscribe all subscribed topics ... before close consumer`。字节码进一步确认:
|
|
18
|
+
|
|
19
|
+
1. `SubscriptionReceiverV1.closeConsumer` 取消该 Consumer 的订阅关系。
|
|
20
|
+
2. `ConsensusSubscriptionSetupHandler.teardownConsensusSubscriptions` 解除相应预取队列,并调用 `ConsensusSubscriptionCommitManager.removeAllStatesForTopic`。
|
|
21
|
+
3. `removeAllStatesForTopic` 删除对应组/Topic 的内存状态,并调用 `deleteTopicProgressFiles` 删除持久化进度文件。
|
|
22
|
+
|
|
23
|
+
官方 Java API 文档也说明,不活跃断开会同步触发取消订阅。这是当前版本的已观察行为,不能仅因与插件的持久消费需求不符就断言违反现有文档。需要确认是否有受支持的持久消费组机制,或由客户端承担全部恢复责任的正式契约。Topic `retention.ms` 与组进度持久性是不同问题。
|
|
24
|
+
|
|
25
|
+
原始诊断用例位于 `test/server-contract/close-progress.test.js`。它验证服务端行为,不替代插件端到端验收;客户端若能通过其他可靠机制恢复,不要求这个原生用例必须通过。
|
|
26
|
+
|
|
27
|
+
## 发现二:seek 成功不证明请求位点实际存在
|
|
28
|
+
|
|
29
|
+
`tools/recovery-lab.js` 通过真实消费得到单 Region、单 Writer 的 `TopicProgress`,当前 localSeq 为 1。使用官方序列化格式,仅将 localSeq 改成尚未产生的 1,000,001,然后调用 `seekAfter`,再调用 `positions`。
|
|
30
|
+
|
|
31
|
+
| 模型 | seek 是否被接受 | positions 是否返回所请求位点 | 明确的无效位点错误 |
|
|
32
|
+
| --- | --- | --- | --- |
|
|
33
|
+
| 树 | 是 | 是 | 无 |
|
|
34
|
+
| 表 | 是 | 是 | 无 |
|
|
35
|
+
|
|
36
|
+
因此,`seekAfter` 成功及随后 `positions` 相等不能用来证明目标记录仍在 WAL 中。这项实验使用的是不存在的未来位点,**不是实际删除或过期 WAL 的实验**;不能把它表述为已实测“过期 WAL 一定静默丢数据”。它排除了此前考虑的客户端位点有效性检查方式。
|
|
37
|
+
|
|
38
|
+
其他已验证边界:首次 `seekToEnd` 后 `positions` 可以为空;对空 TopicProgress 调用 `seekAfter` 不会回放之前的记录。字节码显示,`ConsensusSubscriptionBroker.seekQueueToRegionProgress` 对缺失或空 RegionProgress 直接返回。已有真实位点且 WAL 仍保留时,树/表 `seekAfter` 重放停机记录通过;这只证明该受控场景。
|
|
39
|
+
|
|
40
|
+
## 发现三:SDK 可持续返回空批次而不向调用方报告连接失效
|
|
41
|
+
|
|
42
|
+
使用单独的官方 SDK 探针,连接测试专用 TCP 代理。先收到并 ACK counter=2,然后断开代理全部连接并拒绝新连接 22 秒,期间直接向数据库写入 counter=3;恢复代理后写入 counter=4。探针使用 SDK 文档中的 120 秒端点同步周期,整个网络观察小于该周期,以降低绕过代理的风险;测试同时记录断网期间收到的数据。
|
|
43
|
+
|
|
44
|
+
| 模型 | 断网期间空 poll 次数 | 22 秒后原订阅关系存在 | SDK 抛出的异常 | 本轮收到的 counter |
|
|
45
|
+
| --- | ---: | --- | --- | --- |
|
|
46
|
+
| 树 | 53 | 否 | 无 | 仅 2 |
|
|
47
|
+
| 表 | 54 | 否 | 无 | 仅 2 |
|
|
48
|
+
|
|
49
|
+
恢复代理后额外观察约 27.5 秒,探针进程仍存活,但没有收到 3/4。断网期间未收到数据,代理共观察到 7 次连接尝试。该结果证明“进程存活 + poll 返回空”不能作为健康订阅状态;不将其夸大为 SDK 重新订阅后已静默跳过所有数据。
|
|
50
|
+
|
|
51
|
+
插件连接保护:桥接使用已验证的 SDK protected 扩展点 `getAvailableDataNodeIds()` 检查连接可用性,poll 前后和 ACK 前检查;Consumer 在暂停、等待 ACK 时也请求健康状态。树/表断网回归中,连接失效会报 `RECOVERY_REQUIRED` 并停止;该状态用于要求调用方核对消费进度。
|
|
52
|
+
|
|
53
|
+
## 需要明确的恢复契约
|
|
54
|
+
|
|
55
|
+
下列信息决定能否在当前发行版上安全实现客户端 checkpoint;仅提供成功调用 seek 的示例不足以证明完整性:
|
|
56
|
+
|
|
57
|
+
1. CLOSE / 超时后,是否有受支持的选项保留组订阅及已提交位点?若没有,官方建议的持久化与重开流程是什么?
|
|
58
|
+
2. 如何查询每个 Region/Writer 可重放的最早和最晚位置,并区分合法的过滤间隙、尚未产生的位置和已被清理的位置?目标位置不可恢复时,哪一个公开 API/错误码能可靠地报告?
|
|
59
|
+
3. 没有活跃 Consumer 时,Topic 保留策略怎样约束 WAL 清理?时间、字节及磁盘压力之间的优先级是什么?
|
|
60
|
+
4. 首次空位点、停机期间出现的新 Region、Topic 删除重建,如何建立不会跳过写入的恢复边界,并验证订阅实例身份?
|
|
61
|
+
5. 同组多消费者与 ACK 乱序时,怎样取得可以原子保存、恢复且不会越过未确认消息的组 checkpoint?seek 的作用范围及并发隔离条件是什么?
|
|
62
|
+
6. SDK 在失去连接、订阅被回收或位点失效后,哪些异常保证向应用传播,哪些会被转为空 poll?
|
|
63
|
+
|
|
64
|
+
若实现自动恢复,应先依据上述接口边界验证每个 Region/Writer 的位点与 WAL 可用范围,再将 `positions` 保存和 `seekAfter` 接入恢复流程。当前节点保留连接状态检查、流控和重开保护;研究脚本用于复核特定的 WAL 保留场景。
|
|
65
|
+
|
|
66
|
+
## 复现命令与验收范围
|
|
67
|
+
|
|
68
|
+
在已授权的开发数据库环境,使用现有私有测试凭据运行器:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
node tools/run-ubuntu-tests.js tools/recovery-lab.js
|
|
72
|
+
node tools/run-ubuntu-tests.js --test --test-concurrency=1 test/server-contract/*.test.js
|
|
73
|
+
node tools/run-ubuntu-tests.js --test --test-concurrency=1 test/acceptance/*.test.js
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
实验 JSON:Ubuntu `.dev/recovery-lab.json`;Windows 工作区副本 `.dev/recovery-lab-20261108.json`。网络研究 Java 探针在 `tools/SubscriptionRecoveryProbe.java`,不随 npm 插件分发。
|
|
77
|
+
|
|
78
|
+
重开场景测试位于 `test/acceptance/durable-recovery.test.js`:使用插件 `Consumer`,关闭后写入,再创建新的 Consumer,检查原未确认记录与停机期间记录是否重新投递并能 ACK。TimechoDB 2.0.12.1 的树、表用例重开后报告 `RECOVERY_REQUIRED`;该结果与当前节点的重开保护行为一致。
|
|
79
|
+
|
|
80
|
+
官网发布历史截至本次查询列出的最新版本为 V2.0.12.1(2026-10-29),下载说明为联系 Timecho 工作人员;未找到公开列出的更高修复版本。
|
|
81
|
+
|
|
82
|
+
官方依据:[Java 订阅 API](https://www.timecho.com/docs/zh/UserGuide/latest/API/Programming-Data-Subscription_timecho.html)、[订阅手册](https://www.timecho.com/docs/zh/UserGuide/latest/User-Manual/Data-subscription_timecho.html)、[发布历史](https://www.timecho.com/docs/zh/UserGuide/latest/IoTDB-Introduction/Release-history_timecho.html)。
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# 验证报告:2026-11-08 至 2026-11-19
|
|
2
|
+
|
|
3
|
+
功能验证针对 `node-red-node-iotdb@0.1.0-rc.2` 实现;2026-11-20 将相同运行时代码打为包版本 `0.0.1`。`0.0.2` 更新文档和仓库元数据,没有修改插件运行逻辑。SQL 和存活会话中的订阅闭环按下列范围进行过验证;断连、关闭及同组重开行为单独记录,避免与存活会话结果混为一谈。
|
|
4
|
+
|
|
5
|
+
## 环境与版本
|
|
6
|
+
|
|
7
|
+
- 开发与测试:Ubuntu 26.04 LTS,Node.js 22.22.1,Java 17.0.20.1,Node-RED 5.0.9。
|
|
8
|
+
- 主数据库:Windows 本机 TimechoDB 2.0.12.1 / c05111d,ms 精度,原生订阅已启用。
|
|
9
|
+
- Apache 对照:同一 Windows 机器上的隔离 Apache IoTDB 2.0.11 / 7c0de06,RPC 16667,ns 精度。使用独立配置、端口和数据目录;未覆盖用户原有安装的数据。
|
|
10
|
+
- Ubuntu 通过 loopback SSH 转发连接数据库。结果包含该网络路径的开销,不能当作本机纯数据库性能。
|
|
11
|
+
- SQL SDK 固定 `@iotdb/client@2.0.11`;订阅使用 TimechoDB 2.0.12.1 配套官方 Java SDK。SDK 不包含在 npm 产物中。
|
|
12
|
+
|
|
13
|
+
## 自动化检查
|
|
14
|
+
|
|
15
|
+
| 检查 | 实际结果 | 证据 |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| 单元与真实 Node-RED helper 运行时 | 21/21 通过,0 跳过 | `npm test`,Ubuntu `.dev/unit-tests.log` |
|
|
18
|
+
| 数据库集成 | 18/18 通过,0 跳过 | `test/integration/*.test.js`,Ubuntu `.dev/integration-rc2.log` |
|
|
19
|
+
| 插件重开行为 | 重启同一 Topic/组时报告 `RECOVERY_REQUIRED`,不会直接建立新的消费起点 | `test/acceptance/durable-recovery.test.js` |
|
|
20
|
+
| 服务端原生 close 行为 | CLOSE/不活跃清理会取消订阅;同组重开观察窗口内未返回关闭前未确认及停机期间写入的记录 | `test/server-contract/close-progress.test.js` |
|
|
21
|
+
| 实际 SQL 网络故障 | 通过:丢弃已执行写入的响应,SQL 仅转发一次;随后重连查询确认已写入 | `test/integration/faults.test.js` |
|
|
22
|
+
| 实际凭据错误 | SQL 与订阅均拒绝,返回错误不含所用密码 | 同上 |
|
|
23
|
+
| 数据完整性 | INT64 上下界、ns 时间戳、NULL、Unicode、重复列、BOOL/BLOB/DATE 通过 | 数据库、Apache 和边界集成用例 |
|
|
24
|
+
| 订阅停止保护 | Java 强杀、跨 12 秒停机、旧 token、重部署阻止重新连接通过 | `test/integration/recovery.test.js` |
|
|
25
|
+
| 订阅实际断网 | 树/表在等待 ACK 时断开代理,明确报告 RECOVERY_REQUIRED;SDK 空 poll 不再被当作健康空闲 | `test/integration/subscription-network.test.js` |
|
|
26
|
+
| 受控重放研究 | 树/表在已有位点且 WAL 保留的用例中,`seekAfter` 可重放停机数据;超限批次可用 `seekToBeginning` 再处理 | recovery / subscription-edges;边界见接口调查 |
|
|
27
|
+
| UI 与实际 Flow | 三类型注册、SQL 操作模式、密码控件、树表选项、示例查询已测 | `tools/editor-smoke.js`、`tools/install-demo.js` |
|
|
28
|
+
|
|
29
|
+
测试用例按真实服务端行为调整后,分别记录插件 Consumer 重开与原生 SDK close/reopen 的观测结果;测试框架重复注册、null/undefined 断言及服务端异步清理等运行问题已修正,结果只统计完整结束的测试运行。
|
|
30
|
+
|
|
31
|
+
## 有界持续运行
|
|
32
|
+
|
|
33
|
+
通过真实 Node-RED 流程写入数据库,原生 consumer 输出后回送手动 ACK。每个模型目标负载为每秒一批、每批 200 行。以下 300 秒记录来自上一候选版本,不把它当作 rc.2 已做相同时长测试的证据。
|
|
34
|
+
|
|
35
|
+
| 300 秒运行 | 树模型 | 表模型 |
|
|
36
|
+
| --- | ---: | ---: |
|
|
37
|
+
| 写入行数 | 59,800 | 59,800 |
|
|
38
|
+
| 收到不同记录数 | 59,800 | 59,800 |
|
|
39
|
+
| 总投递记录数 | 59,800 | 59,800 |
|
|
40
|
+
| 已确认批次数 | 297 | 296 |
|
|
41
|
+
| 写入延迟 p50 | 9.60 ms | 9.46 ms |
|
|
42
|
+
| 写入延迟 p95 | 24.53 ms | 25.71 ms |
|
|
43
|
+
| 最大写入延迟 | 1,931.62 ms | 1,931.18 ms |
|
|
44
|
+
|
|
45
|
+
本轮流程错误为 0,测试记录未观察到缺失或重复。测试进程还维护全量记录 ID 集合用于核对,采样 RSS 约 140–177 MiB;单个 Java 进程 RSS 约 210–232 MiB,高于配置的 128 MiB Java 堆上限。堆配置与进程 RSS 是不同指标;该次 5 分钟观测只描述当次采样。
|
|
46
|
+
|
|
47
|
+
加入停止保护后又进行 60 秒复测,树/表各 11,400 行全部收到并确认,错误为 0。该轮与其他集成测试同时运行,写入 p95 约 1.62 秒;这是资源竞争下的观测值,不与前一轮混合汇报。
|
|
48
|
+
|
|
49
|
+
原始报告由开发工作区保存为 `.dev/soak-report-20261108.json` 和 `.dev/soak-guard-report-20261108.json`。本报告的持续运行观测时长为 5 分钟、60 秒及独立 60 秒;日级运行、大规模设备数、多 DataNode、故障转移和容量场景不在这些记录范围内。
|
|
50
|
+
|
|
51
|
+
`rc.2` 增加连接健康检查后完成独立 60 秒复测:树、表各写入并收到 11,600 条,分别 ACK 53 批,无缺失、重复或流程错误。该轮同时执行集成测试,写入 p50 约 12.6/12.2 ms、p95 约 1.45 s,不用于承诺生产性能。原始记录为 `.dev/soak-rc2-report-20261108.json`。
|
|
52
|
+
|
|
53
|
+
## 本轮恢复接口研究
|
|
54
|
+
|
|
55
|
+
真实 SDK 在 22 秒代理断网中持续返回空 poll,没有向调用方抛出异常;树/表分别观察到 53/54 次空 poll,服务端订阅关系消失。恢复网络后继续观察约 27.5 秒,进程仍存活但没有收到后续测试记录。此结果用于实现并验证 rc.2 的连接状态检查;该用例观察的是健康检测行为,不包含自动重订阅流程。
|
|
56
|
+
|
|
57
|
+
将真实序列号 1 的 checkpoint 改成未产生的 1,000,001 后,树/表 `seekAfter` 均被接受,`positions` 返回请求值。该实验不是实际 WAL 过期测试;它证明 seek 成功不能用作位点有效性的验证。详细步骤、证据及需明确的恢复契约见[接口调查](research/subscription-recovery-contract.md)。
|
|
58
|
+
|
|
59
|
+
## Docker 数据库重开场景:2026-11-19
|
|
60
|
+
|
|
61
|
+
使用 dev2 独立 Node-RED 容器连接用户指定的 TimechoDB `192.168.99.26:6667`。实测版本为 `2.0.12.1 / c05111d`,时间精度 `ms`;容器内插件为 `0.1.0-rc.2`。数据库凭据只从容器 secrets 文件加载,没有写入脚本或报告。
|
|
62
|
+
|
|
63
|
+
| 模型 | 插件 consumer 重开 | 原生 SDK 同组重开 |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| 树模型 | 首条未确认记录已收到,写入离线记录后重开触发 `RECOVERY_REQUIRED`;重开流程未输出这两条记录 | 重开后约 17.9 秒内 37 次 poll 均为空;观察窗口内未输出未确认记录或离线记录 |
|
|
66
|
+
| 表模型 | 首条未确认记录已收到,写入离线记录后重开触发 `RECOVERY_REQUIRED`;重开流程未输出这两条记录 | 重开后约 17.9 秒内 37 次 poll 均为空;观察窗口内未输出未确认记录或离线记录 |
|
|
67
|
+
|
|
68
|
+
原生对照绕过插件 Consumer 的持久化标记,使用插件内 `BridgeClient` 调用匹配版本 Java 订阅 SDK;关闭前两模型均收到 counter `1`,关闭后各写入 counter `2`,然后以相同 Topic、消费组及 Consumer ID 重开。该次观察窗口内,原生 SDK 未返回 counter `1` 或 `2`;插件 Consumer 的 `RECOVERY_REQUIRED` 表示需要对照业务记录核对消费进度。
|
|
69
|
+
|
|
70
|
+
首轮尝试使用新建数据库时,树/表在 SchemaRegionGroups 创建阶段返回错误,未进入订阅步骤,因此不纳入订阅场景数据。复测使用已有 `root.ai_demo` 和 `node-red` 数据库。四个测试对象的 Topic 和 schema 均在结束后由独立只读查询确认不存在;Node-RED 容器最后检查为 `running healthy`。
|
|
71
|
+
|
|
72
|
+
本机脱敏证据位于 `.dev/dev2-retests/`;插件报告为 `durable-recovery-retest-1795083734706.json`,原生 SDK 对照为 `native-close-progress-1795084344877.json`。这组记录描述 TimechoDB 2.0.12.1 上的树、表同组重开观测。
|
|
73
|
+
|
|
74
|
+
## 安装包与依赖
|
|
75
|
+
|
|
76
|
+
`tools/verify-package.js` 会打包、检查文件白名单、在新目录安装 tarball、审计运行依赖、启动独立 Node-RED,再执行包内示例的真实版本查询并校验凭据存储。每个交付包另附 `package-verification.json` 和 SHA-256;报告字段逐项记录对应检查结果。
|
|
77
|
+
|
|
78
|
+
插件自身生产依赖审计为零漏洞。Node-RED 开发运行环境固定补丁后无 high/critical,仍有 35 条 moderate 依赖链,根问题为 npm bundled `glob` 与 `minimatch`:
|
|
79
|
+
|
|
80
|
+
- [GHSA-3g3m-2fjr-6pw4](https://github.com/advisories/GHSA-3g3m-2fjr-6pw4)
|
|
81
|
+
- [GHSA-g3hv-75wg-m249](https://github.com/advisories/GHSA-g3hv-75wg-m249)
|
|
82
|
+
|
|
83
|
+
开发编辑器仅监听 loopback,关闭 palette 安装/更新/上传及外部模块自动安装;这些限制降低暴露面,但不会消除依赖中的问题。
|
|
84
|
+
|
|
85
|
+
## 订阅恢复场景记录范围
|
|
86
|
+
|
|
87
|
+
后续订阅恢复场景可覆盖:未确认记录和停机写入在重启、超时、重部署及数据库故障后的处理;WAL 位点的可用范围与错误返回;首次空位点、新 Region、ACK 乱序/并发、进度文件故障,以及恢复过程中再次崩溃。测试应保留稳定 groupId,并校验每个业务记录的投递和 ACK 结果。
|
|
88
|
+
|
|
89
|
+
依据:[TimechoDB 官方 Java 订阅 API(连接超时与资源清理)](https://www.timecho.com/docs/zh/UserGuide/latest/API/Programming-Data-Subscription_timecho.html)、2.0.12.1 字节码、服务端日志以及对应的插件与原生 SDK 测试用例。官网文档与实测构建分别记录,不外推到其他版本。
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
[
|
|
2
|
+
{"id":"iotdb-example-sql","type":"tab","label":"IoTDB SQL example","disabled":false,"info":"Configure the server credentials, then click Inject. This example only queries the server version."},
|
|
3
|
+
{"id":"iotdb-example-server","type":"iotdb-server","name":"Local TimechoDB / IoTDB","host":"127.0.0.1","port":6667,"model":"tree","timestampPrecision":"ms","timeoutMs":15000,"connectTimeoutMs":10000,"maxQueue":32},
|
|
4
|
+
{"id":"iotdb-example-inject","type":"inject","z":"iotdb-example-sql","name":"Query version","props":[{"p":"topic","vt":"str"}],"topic":"SHOW VERSION","repeat":"","once":false,"x":150,"y":120,"wires":[["iotdb-example-query"]]},
|
|
5
|
+
{"id":"iotdb-example-query","type":"iotdb-operator","z":"iotdb-example-sql","server":"iotdb-example-server","name":"IoTDB SQL","mode":"query","sql":"","maxRows":10000,"maxBytes":8388608,"x":380,"y":120,"wires":[["iotdb-example-result"]]},
|
|
6
|
+
{"id":"iotdb-example-result","type":"debug","z":"iotdb-example-sql","name":"Result","active":true,"tosidebar":true,"console":false,"complete":"true","x":620,"y":120,"wires":[]},
|
|
7
|
+
{"id":"iotdb-example-catch","type":"catch","z":"iotdb-example-sql","scope":["iotdb-example-query"],"uncaught":false,"x":380,"y":220,"wires":[["iotdb-example-result"]]}
|
|
8
|
+
]
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
[
|
|
2
|
+
{"id":"iotdb-example-sub","type":"tab","label":"IoTDB subscription / manual ACK","disabled":true,"info":"Development preview. Durable recovery is not certified on TimechoDB 2.0.12.1; connection loss or redeploy requires manual reconciliation. Read docs/operations.md before enabling. Create a matching record Topic, configure credentials/SDK and unique IDs. Replace the Function with real processing; ACK only on success."},
|
|
3
|
+
{"id":"iotdb-sub-server","type":"iotdb-server","name":"Subscription server","host":"127.0.0.1","port":6667,"model":"tree","timestampPrecision":"ms","timeoutMs":15000,"connectTimeoutMs":10000,"maxQueue":32,"sdkClasspath":""},
|
|
4
|
+
{"id":"iotdb-example-consumer","type":"iotdb-consumer","z":"iotdb-example-sub","server":"iotdb-sub-server","name":"Manual ACK","topic":"example_topic","groupId":"example_business","consumerId":"worker_1","ackMode":"manual","pollMs":1000,"maxInFlight":4,"maxRows":10000,"maxBytes":8388608,"x":180,"y":120,"wires":[["iotdb-example-business","iotdb-sub-debug"]]},
|
|
5
|
+
{"id":"iotdb-example-business","type":"function","z":"iotdb-example-sub","name":"Process, then ACK","func":"// Replace with real processing. Return only after success.\n// Preserve msg.iotdb.ackToken. Join all required branches first.\nmsg.iotdb.action = 'ack';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":120,"wires":[["iotdb-example-consumer"]]},
|
|
6
|
+
{"id":"iotdb-sub-debug","type":"debug","z":"iotdb-example-sub","name":"Data / errors","active":true,"tosidebar":true,"console":false,"complete":"true","x":470,"y":220,"wires":[]},
|
|
7
|
+
{"id":"iotdb-sub-catch","type":"catch","z":"iotdb-example-sub","scope":["iotdb-example-consumer","iotdb-example-business"],"uncaught":false,"x":180,"y":240,"wires":[["iotdb-sub-debug"]]}
|
|
8
|
+
]
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { spawn } = require('node:child_process');
|
|
3
|
+
const { EventEmitter } = require('node:events');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const os = require('node:os');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const { IotdbError, safeError } = require('./errors');
|
|
8
|
+
|
|
9
|
+
class BridgeClient extends EventEmitter {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super();
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.pending = new Map();
|
|
14
|
+
this.sequence = 0;
|
|
15
|
+
this.child = null;
|
|
16
|
+
this.stopped = false;
|
|
17
|
+
this.buffer = '';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async open() {
|
|
21
|
+
const config = this.config;
|
|
22
|
+
const sdk = config.sdkClasspath || process.env.IOTDB_SUBSCRIPTION_CLASSPATH;
|
|
23
|
+
if (!sdk) throw new IotdbError('SDK_REQUIRED', 'Configure the matching Java subscription SDK classpath (IOTDB_SUBSCRIPTION_CLASSPATH)');
|
|
24
|
+
if (config.tls) throw new IotdbError('UNSUPPORTED_TLS', 'This subscription bridge does not support native RPC TLS; use a protected network or SSH tunnel');
|
|
25
|
+
this.directory = fs.mkdtempSync(path.join(os.tmpdir(), 'iotdb-consumer-'));
|
|
26
|
+
const jar = path.join(__dirname, '../bridge/target/iotdb-bridge.jar');
|
|
27
|
+
const compiled = fs.existsSync(jar);
|
|
28
|
+
const args = ['-Xms16m', '-Xmx128m', '-Dfile.encoding=UTF-8', '-cp', compiled ? `${jar}${path.delimiter}${sdk}` : sdk,
|
|
29
|
+
compiled ? 'IotdbSubscriptionBridge' : path.join(__dirname, '../bridge/src/IotdbSubscriptionBridge.java')];
|
|
30
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => /^(PATH|JAVA_HOME|SYSTEMROOT|WINDIR|TEMP|TMP|TMPDIR|HOME|LANG|LC_ALL|TZ)$/i.test(key)));
|
|
31
|
+
const child = spawn(config.javaPath || 'java', args, { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true, env });
|
|
32
|
+
this.child = child;
|
|
33
|
+
this.exited = new Promise(resolve => child.once('close', resolve));
|
|
34
|
+
child.stdout.setEncoding('utf8');
|
|
35
|
+
child.stdout.on('data', text => this.receive(text));
|
|
36
|
+
child.stderr.resume(); // SDK diagnostics can include credentials; never relay them.
|
|
37
|
+
child.stdin.on('error', () => this.abort(new IotdbError('BRIDGE_PIPE', 'Java bridge input closed', true)));
|
|
38
|
+
child.on('error', () => this.abort(new IotdbError('JAVA_UNAVAILABLE', 'Cannot start Java; check the configured executable and SDK classpath')));
|
|
39
|
+
child.on('exit', () => {
|
|
40
|
+
this.stopped = true;
|
|
41
|
+
this.lastError ||= new IotdbError('BRIDGE_EXIT', this.initialized ? 'Java subscription process exited' : 'Java bridge exited during startup; verify Java 17+ and the matching SDK classpath', this.initialized === true);
|
|
42
|
+
this.rejectPending(this.lastError);
|
|
43
|
+
});
|
|
44
|
+
const result = await this.request('init', [config.host, config.port, config.username, config.password, config.model,
|
|
45
|
+
config.topic, config.groupId, config.consumerId, config.pollMs, config.maxInFlight,
|
|
46
|
+
config.maxRows, config.maxBytes, config.connectTimeoutMs, this.directory], config.connectTimeoutMs + 15000);
|
|
47
|
+
this.initialized = true;
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
receive(text) {
|
|
52
|
+
this.buffer += text;
|
|
53
|
+
if (Buffer.byteLength(this.buffer) > this.config.maxBytes + 65536) return this.abort(new IotdbError('PROTOCOL_LIMIT', 'Java response exceeds the configured byte limit'));
|
|
54
|
+
let index;
|
|
55
|
+
while ((index = this.buffer.indexOf('\n')) >= 0) {
|
|
56
|
+
const line = this.buffer.slice(0, index);
|
|
57
|
+
this.buffer = this.buffer.slice(index + 1);
|
|
58
|
+
let message;
|
|
59
|
+
try { message = JSON.parse(line); } catch { return this.abort(new IotdbError('PROTOCOL_ERROR', 'Invalid Java bridge response')); }
|
|
60
|
+
const request = this.pending.get(message.id);
|
|
61
|
+
if (!request) continue;
|
|
62
|
+
this.pending.delete(message.id);
|
|
63
|
+
clearTimeout(request.timer);
|
|
64
|
+
if (message.error) {
|
|
65
|
+
const error = safeError(message.error, [this.config.password]);
|
|
66
|
+
const terminal = /BATCH_LIMIT|UNSUPPORTED|UNKNOWN_ACK|INVALID_|not enabled|disabled|not exist|does not exist|authentication|password|permission|privilege|not support/i.test(error.message);
|
|
67
|
+
error.retryable = !terminal && /connect|timeout|timed out|transport|socket|broken|closed|unavailable|network/i.test(error.message);
|
|
68
|
+
request.reject(error);
|
|
69
|
+
} else request.resolve(message.result);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
request(action, fields = [], timeout = this.config.timeoutMs) {
|
|
74
|
+
if (this.stopped || !this.child) return Promise.reject(new IotdbError('BRIDGE_CLOSED', 'Subscription bridge is closed', true));
|
|
75
|
+
if (this.pending.size >= 128) return Promise.reject(new IotdbError('QUEUE_FULL', 'Subscription command queue is full'));
|
|
76
|
+
const id = ++this.sequence;
|
|
77
|
+
const line = [id, action, ...fields.map(value => Buffer.from(String(value ?? ''), 'utf8').toString('base64'))].join('\t') + '\n';
|
|
78
|
+
if (Buffer.byteLength(line) > 65536) return Promise.reject(new IotdbError('REQUEST_LIMIT', 'Subscription configuration is too large'));
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
const timer = setTimeout(() => this.abort(new IotdbError('BRIDGE_TIMEOUT', 'Subscription operation timed out; commit outcome may be unknown', true)), timeout);
|
|
81
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
82
|
+
this.child.stdin.write(line);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
poll() { return this.request('poll', [], this.config.timeoutMs + this.config.pollMs); }
|
|
87
|
+
health() { return this.request('health', [], this.config.timeoutMs + this.config.pollMs); }
|
|
88
|
+
ack(token) { return this.request('ack', [token]); }
|
|
89
|
+
|
|
90
|
+
rejectPending(error) {
|
|
91
|
+
for (const pending of this.pending.values()) { clearTimeout(pending.timer); pending.reject(error); }
|
|
92
|
+
this.pending.clear();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
abort(error) {
|
|
96
|
+
this.stopped = true;
|
|
97
|
+
this.lastError = error;
|
|
98
|
+
this.rejectPending(error);
|
|
99
|
+
this.child?.kill();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async close() {
|
|
103
|
+
if (this.closing) return this.closing;
|
|
104
|
+
this.closing = (async () => {
|
|
105
|
+
if (this.child && !this.stopped) {
|
|
106
|
+
try { await this.request('close', [], 2500); } catch { /* termination below */ }
|
|
107
|
+
}
|
|
108
|
+
this.stopped = true;
|
|
109
|
+
this.rejectPending(new IotdbError('BRIDGE_CLOSED', 'Subscription bridge is closing'));
|
|
110
|
+
if (this.child) {
|
|
111
|
+
this.child.stdin.end();
|
|
112
|
+
const killTimer = setTimeout(() => this.child.kill('SIGKILL'), 1500);
|
|
113
|
+
this.child.kill();
|
|
114
|
+
await this.exited;
|
|
115
|
+
clearTimeout(killTimer);
|
|
116
|
+
}
|
|
117
|
+
if (this.directory) {
|
|
118
|
+
const directory = path.resolve(this.directory);
|
|
119
|
+
if (path.dirname(directory) === path.resolve(os.tmpdir()) && path.basename(directory).startsWith('iotdb-consumer-')) {
|
|
120
|
+
await fs.promises.rm(directory, { recursive: true, force: true, maxRetries: 2 }).catch(() => {});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
})();
|
|
124
|
+
return this.closing;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = { BridgeClient };
|
package/lib/consumer.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { EventEmitter } = require('node:events');
|
|
3
|
+
const { randomUUID } = require('node:crypto');
|
|
4
|
+
const { BridgeClient } = require('./bridge-client');
|
|
5
|
+
const { IotdbError } = require('./errors');
|
|
6
|
+
const { SubscriptionState } = require('./subscription-state');
|
|
7
|
+
|
|
8
|
+
class Consumer extends EventEmitter {
|
|
9
|
+
constructor(config, factory = config => new BridgeClient(config)) {
|
|
10
|
+
super();
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.factory = factory;
|
|
13
|
+
this.closed = false;
|
|
14
|
+
this.paused = false;
|
|
15
|
+
this.terminal = false;
|
|
16
|
+
this.inFlight = new Map();
|
|
17
|
+
this.committed = new Set();
|
|
18
|
+
this.acks = new Map();
|
|
19
|
+
this.generation = randomUUID();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
start() { if (!this.loop) this.loop = this.tick(); }
|
|
23
|
+
status(state) { this.emit('status', { state, inFlight: this.inFlight.size }); }
|
|
24
|
+
|
|
25
|
+
schedule(ms) {
|
|
26
|
+
if (!this.closed && !this.terminal) this.timer = setTimeout(() => { this.loop = this.tick(); }, ms);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async tick() {
|
|
30
|
+
if (this.closed || this.terminal) return;
|
|
31
|
+
try {
|
|
32
|
+
if (!this.bridge) {
|
|
33
|
+
this.status('connecting');
|
|
34
|
+
// The marker is created before any attempt that could subscribe. Even
|
|
35
|
+
// a failed/ambiguous handshake requires reconciliation on restart.
|
|
36
|
+
this.state ||= new SubscriptionState(this.config);
|
|
37
|
+
this.state.begin();
|
|
38
|
+
const bridge = this.factory(this.config);
|
|
39
|
+
this.bridge = bridge;
|
|
40
|
+
await bridge.open();
|
|
41
|
+
if (this.closed) { await bridge.close(); return; }
|
|
42
|
+
}
|
|
43
|
+
if (this.bridge.stopped) throw this.bridge.lastError || new IotdbError('BRIDGE_CLOSED', 'Subscription connection closed', true);
|
|
44
|
+
// SDK poll() may swallow transport errors. Check its provider state even
|
|
45
|
+
// while paused or waiting for downstream ACK, when no new poll is sent.
|
|
46
|
+
await this.bridge.health?.();
|
|
47
|
+
if (this.paused) { this.status('paused'); this.schedule(200); return; }
|
|
48
|
+
if (this.inFlight.size >= this.config.maxInFlight) { this.status('waiting for ACK'); this.schedule(100); return; }
|
|
49
|
+
this.status('subscribed');
|
|
50
|
+
const batch = await this.bridge.poll();
|
|
51
|
+
if (this.closed) return;
|
|
52
|
+
if (batch.token) {
|
|
53
|
+
const token = `${this.generation}:${batch.token}`;
|
|
54
|
+
this.inFlight.set(token, batch.token);
|
|
55
|
+
const message = { topic: this.config.topic, payload: batch.rows,
|
|
56
|
+
iotdb: { model: this.config.model, consumerGroupId: this.config.groupId, consumerId: this.config.consumerId,
|
|
57
|
+
ackToken: token, schemas: batch.schemas, messageCount: batch.messageCount,
|
|
58
|
+
timestampPrecision: this.config.timestampPrecision, acknowledgement: this.config.ackMode } };
|
|
59
|
+
this.emit('data', message);
|
|
60
|
+
if (this.config.ackMode === 'delivery') await this.ack(token);
|
|
61
|
+
}
|
|
62
|
+
this.schedule(batch.empty || batch.blocked ? 100 : 0);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (this.closed) return;
|
|
65
|
+
const bridge = this.bridge;
|
|
66
|
+
this.bridge = null;
|
|
67
|
+
await bridge?.close();
|
|
68
|
+
this.inFlight.clear();
|
|
69
|
+
this.committed.clear();
|
|
70
|
+
this.generation = randomUUID();
|
|
71
|
+
const failure = error.retryable ? new IotdbError('RECOVERY_REQUIRED',
|
|
72
|
+
'Subscription connection was lost. Automatic resubscription is stopped because the server may have discarded group progress. Reconcile and replay before restarting.') : error;
|
|
73
|
+
this.terminal = true;
|
|
74
|
+
this.emit('failure', failure);
|
|
75
|
+
this.status('error');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async ack(token) {
|
|
80
|
+
if (this.closed) throw new IotdbError('CLOSED', 'Consumer is closing');
|
|
81
|
+
if (typeof token !== 'string' || token.length > 200) throw new IotdbError('INVALID_ACK', 'Provide msg.iotdb.ackToken from this consumer');
|
|
82
|
+
if (this.committed.has(token)) return { duplicate: true };
|
|
83
|
+
if (this.acks.has(token)) return this.acks.get(token);
|
|
84
|
+
const raw = this.inFlight.get(token);
|
|
85
|
+
if (!raw || !this.bridge) throw new IotdbError('UNKNOWN_ACK', 'ACK token is expired or belongs to another consumer');
|
|
86
|
+
const bridge = this.bridge;
|
|
87
|
+
const operation = (async () => {
|
|
88
|
+
const result = await bridge.ack(raw);
|
|
89
|
+
if (bridge !== this.bridge) throw new IotdbError('UNKNOWN_ACK', 'Subscription connection changed during ACK; wait for redelivery');
|
|
90
|
+
this.inFlight.delete(token);
|
|
91
|
+
this.committed.add(token);
|
|
92
|
+
if (this.committed.size > 1024) this.committed.delete(this.committed.values().next().value);
|
|
93
|
+
return result;
|
|
94
|
+
})();
|
|
95
|
+
this.acks.set(token, operation);
|
|
96
|
+
try { return await operation; } finally { this.acks.delete(token); }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pause() { this.paused = true; this.status('paused'); }
|
|
100
|
+
resume() { this.paused = false; if (!this.terminal) this.status('subscribed'); }
|
|
101
|
+
|
|
102
|
+
async close() {
|
|
103
|
+
if (this.closed) return this.closing;
|
|
104
|
+
this.closed = true;
|
|
105
|
+
clearTimeout(this.timer);
|
|
106
|
+
this.closing = (async () => {
|
|
107
|
+
await this.bridge?.close();
|
|
108
|
+
await this.loop?.catch(() => {});
|
|
109
|
+
this.inFlight.clear();
|
|
110
|
+
this.committed.clear();
|
|
111
|
+
this.status('closed');
|
|
112
|
+
})();
|
|
113
|
+
return this.closing;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
module.exports = { Consumer };
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class IotdbError extends Error {
|
|
4
|
+
constructor(code, message, retryable = false) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'IotdbError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.retryable = retryable;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function safeError(error, secrets = []) {
|
|
13
|
+
let message = String(error?.message || error || 'Unknown database error');
|
|
14
|
+
for (const secret of secrets) {
|
|
15
|
+
if (typeof secret === 'string' && secret.length) message = message.split(secret).join('[redacted]');
|
|
16
|
+
}
|
|
17
|
+
message = message.replace(/[\r\n\t]+/g, ' ').slice(0, 600);
|
|
18
|
+
return new IotdbError(error?.code || 'DATABASE_ERROR', message, error?.retryable === true);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function integer(value, fallback, min, max, name) {
|
|
22
|
+
const n = value === undefined || value === '' ? fallback : Number(value);
|
|
23
|
+
if (!Number.isInteger(n) || n < min || n > max) {
|
|
24
|
+
throw new IotdbError('INVALID_CONFIG', `${name} must be an integer between ${min} and ${max}`);
|
|
25
|
+
}
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = { IotdbError, safeError, integer };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { IotdbError } = require('./errors');
|
|
3
|
+
const { uniqueColumns, valueToJson } = require('./values');
|
|
4
|
+
|
|
5
|
+
// Version-gated adapter for the pinned SDK. Its public iterator converts tree
|
|
6
|
+
// time to Number and exposes physical rather than logical column order. Keep
|
|
7
|
+
// all internal access here, fail closed on shape changes, and regression-test
|
|
8
|
+
// against actual server results before changing the SDK dependency.
|
|
9
|
+
function datasetShape(dataset) {
|
|
10
|
+
if (require('@iotdb/client/package.json').version !== '2.0.11' ||
|
|
11
|
+
typeof dataset.ignoreTimeStamp !== 'boolean' ||
|
|
12
|
+
!Array.isArray(dataset.columnIndex2TsBlockColumnIndexList)) {
|
|
13
|
+
throw new IotdbError('SDK_INCOMPATIBLE', 'Unsupported SDK result shape; upgrade the compatibility adapter before using this SDK');
|
|
14
|
+
}
|
|
15
|
+
const columns = uniqueColumns(dataset.getColumnNames(), dataset.getColumnTypes());
|
|
16
|
+
const hasTime = !dataset.ignoreTimeStamp;
|
|
17
|
+
const timeKey = columns.some(c => c.key === 'Time') ? '$time' : 'Time';
|
|
18
|
+
return { columns, hasTime, timeKey, outputColumns: hasTime ? [{ name: 'Time', key: timeKey, type: 'TIMESTAMP' }, ...columns] : columns };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function readRow(dataset, shape) {
|
|
22
|
+
const raw = dataset.currentRows?.[dataset.currentRowIndex];
|
|
23
|
+
if (!Array.isArray(raw)) throw new IotdbError('SDK_INCOMPATIBLE', 'Unsupported SDK row layout');
|
|
24
|
+
const mapping = dataset.columnIndex2TsBlockColumnIndexList;
|
|
25
|
+
const entries = shape.columns.map((column, i) => {
|
|
26
|
+
const index = mapping[i];
|
|
27
|
+
if (!Number.isInteger(index) || index < -1) throw new IotdbError('SDK_INCOMPATIBLE', 'Unsupported SDK column mapping');
|
|
28
|
+
const value = index === -1 ? (shape.hasTime ? raw[0] : undefined) : raw[index + Number(shape.hasTime)];
|
|
29
|
+
if (value === undefined) throw new IotdbError('SDK_INCOMPATIBLE', 'SDK omitted a mapped column; refusing a partial result');
|
|
30
|
+
return [column.key, valueToJson(value, column.type)];
|
|
31
|
+
});
|
|
32
|
+
if (shape.hasTime) entries.unshift([shape.timeKey, valueToJson(raw[0], 'TIMESTAMP')]);
|
|
33
|
+
dataset.next();
|
|
34
|
+
return Object.fromEntries(entries);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function installBinaryCompatibility(session) {
|
|
38
|
+
const original = session.parseQueryResult.bind(session);
|
|
39
|
+
session.parseQueryResult = (blocks, count, types, ignoreTime, mapping) => {
|
|
40
|
+
const binaryIndexes = new Set();
|
|
41
|
+
types.forEach((type, i) => {
|
|
42
|
+
if (type === 'BLOB') binaryIndexes.add(mapping?.length ? mapping[i] : i);
|
|
43
|
+
});
|
|
44
|
+
if (!binaryIndexes.size) return original(blocks, count, types, ignoreTime, mapping);
|
|
45
|
+
const corrected = blocks.map(block => {
|
|
46
|
+
const buffer = Buffer.from(block);
|
|
47
|
+
const physicalCount = buffer.readInt32BE(0);
|
|
48
|
+
if (physicalCount < 0 || physicalCount > buffer.length - 4) throw new IotdbError('SDK_INCOMPATIBLE', 'Invalid TsBlock column header');
|
|
49
|
+
// The server may encode logical BLOB as the common binary/TEXT wire type.
|
|
50
|
+
// Preserve those bytes before the SDK's binary decoder converts TEXT to UTF-8.
|
|
51
|
+
for (const i of binaryIndexes) {
|
|
52
|
+
if (!Number.isInteger(i) || i < 0 || i >= physicalCount) throw new IotdbError('SDK_INCOMPATIBLE', 'Invalid BLOB column mapping');
|
|
53
|
+
if (![5, 10, 11].includes(buffer[4 + i])) throw new IotdbError('SDK_INCOMPATIBLE', 'Unsupported BLOB wire type');
|
|
54
|
+
buffer[4 + i] = 10;
|
|
55
|
+
}
|
|
56
|
+
return buffer;
|
|
57
|
+
});
|
|
58
|
+
return original(corrected, count, types, ignoreTime, mapping);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = { datasetShape, readRow, installBinaryCompatibility };
|