重构:为核心组件实现 Reset 方法优化重置机制

为所有核心组件添加 Reset() 方法:
- LogWriter.Reset(): 删除并重新创建日志文件,保持 index 和 wbuf 引用不变
- RecordIndex.Reset(): 清空索引数据并重新创建索引文件
- RecordQuery.Reset(): 关闭并重新打开日志文件
- ProcessCursor.Reset(): 删除位置文件并重置游标位置
- LogTailer.Reset(): 重置内部 channel 状态

优化 TopicProcessor.Reset() 实现:
- 不再销毁和重建组件对象
- 通过调用各组件的 Reset() 方法重置状态
- 保持组件间引用关系稳定
- 减少代码行数约 20 行
- 避免空指针风险和内存分配开销

代码改进:
- LogWriter 添加 path 字段用于重置
- 移除 topic_processor.go 中未使用的 os import
- 职责分离更清晰,每个组件管理自己的重置逻辑

测试结果:
- TestTopicReset: PASS
- TestTopicResetWithPendingRecords: PASS
- 所有 TopicProcessor 相关测试通过

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-04 21:58:54 +08:00
parent bcc328b129
commit 6fb0731935
6 changed files with 184 additions and 54 deletions

View File

@@ -147,3 +147,19 @@ func (t *LogTailer) GetStartIndex() int {
func (t *LogTailer) GetEndIndex() int {
return t.cursor.EndIndex()
}
// Reset 重置 tailer 的内部状态
// 注意:调用前必须确保 tailer 已停止
func (t *LogTailer) Reset() error {
// 重新创建 channel确保没有遗留的信号
t.stopCh = make(chan struct{})
t.doneCh = make(chan struct{})
// 清空配置 channel
select {
case <-t.configCh:
default:
}
return nil
}