feat(processor): 同步心跳数据到 room_status 表

- 在 HeartbeatProcessor 中新增异步同步逻辑,在历史表写入成功后尝试更新 room_status 表
- 实现 DatabaseManager.upsertRoomStatus 方法,支持批量更新和自动分区创建
- 添加批次内去重逻辑,避免 PostgreSQL ON CONFLICT 冲突
- 新增相关文档:同步方案、测试报告和提案说明
This commit is contained in:
2026-02-06 15:15:03 +08:00
parent b72cdde8bf
commit e44cf10a82
5 changed files with 372 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
# Room Status Synchronization Implementation Test Results
Date: 2026-02-06
## 1. Test Overview
This document summarizes the validation tests performed for the Room Status Synchronization feature, specifically focusing on the `upsertRoomStatus` logic in `DatabaseManager`.
## 2. Test Cases & Results
### 2.1 Auto-Partitioning (自动分区)
- **Scenario**: Incoming heartbeat data contains a `hotel_id` (e.g., 3000) for which no partition exists in `room_status.room_status_moment`.
- **Expected Behavior**: The system should catch the "no partition of relation" error, dynamically create the partition `room_status_moment_h3000`, and retry the insertion successfully.
- **Result**: **PASSED**.
- Log observation: `[db] 检测到 room_status 分区缺失尝试自动创建分区hotelIds: 3000`
- Log observation: `[db] 成功创建 room_status 分区: room_status_moment_h3000`
- Data verification: Data was successfully inserted into the new partition.
### 2.2 In-Batch Deduplication (批次内去重)
- **Scenario**: A single batch of heartbeat events contains multiple records for the same device (`hotel_id`, `room_id`, `device_id`) but with different `ts_ms`.
- **Issue**: PostgreSQL `ON CONFLICT` clause raises an error/warning if the same row is proposed for update multiple times in the same statement: "ON CONFLICT DO UPDATE command cannot affect row a second time".
- **Solution**: Implemented application-level deduplication before SQL generation. Grouped by device key and kept the record with the largest `ts_ms`.
- **Result**: **PASSED**.
- The warning "ON CONFLICT DO UPDATE command cannot affect row a second time" is no longer observed in the logs.
- The database reflects the latest state (highest `ts_ms`).
### 2.3 System Startup & Stability
- **Scenario**: Full system startup with the new code changes.
- **Result**: **PASSED**.
- Service started successfully.
- Redis connection established.
- Database connection established.
- Partition maintenance task started.
- No regression observed in existing Heartbeat History (`heartbeat.heartbeat_events`) insertion.
## 3. Conclusion
The implementation of `upsertRoomStatus` is robust and handles dynamic partitioning and data duplication correctly. The system is ready for deployment.