Files
go-trustlog/api/adapter/subscriber.go
ryan 88f80ffa5e feat: 新增数据库持久化模块(Persistence),实现 Cursor + Retry 双层架构
## 核心功能

### 1. 数据库持久化支持
- 新增完整的 Persistence 模块 (api/persistence/)
- 支持三种持久化策略:
  * StrategyDBOnly - 仅落库,不存证
  * StrategyDBAndTrustlog - 既落库又存证(推荐)
  * StrategyTrustlogOnly - 仅存证,不落库
- 支持多数据库:PostgreSQL, MySQL, SQLite

### 2. Cursor + Retry 双层架构
- CursorWorker:第一道防线,快速发现新记录并尝试存证
  * 增量扫描 operation 表(基于时间戳游标)
  * 默认 10 秒扫描间隔,批量处理 100 条
  * 成功更新状态,失败转入重试队列
- RetryWorker:第二道防线,处理失败记录
  * 指数退避重试(1m → 2m → 4m → 8m → 16m)
  * 默认最多重试 5 次
  * 超限自动标记为死信

### 3. 数据库表设计
- operation 表:存储操作记录,支持可空 IP 字段
- trustlog_cursor 表:Key-Value 模式,支持多游标
- trustlog_retry 表:重试队列,支持指数退避

### 4. 异步最终一致性
- 应用调用立即返回(仅落库)
- CursorWorker 异步扫描并存证
- RetryWorker 保障失败重试
- 完整的监控和死信处理机制

## 修改文件

### 核心代码(11个文件)
- api/persistence/cursor_worker.go - Cursor 工作器(新增)
- api/persistence/repository.go - 数据仓储层(新增)
- api/persistence/schema.go - 数据库 Schema(新增)
- api/persistence/strategy.go - 策略管理器(新增)
- api/persistence/client.go - 客户端封装(新增)
- api/persistence/retry_worker.go - Retry 工作器(新增)
- api/persistence/config.go - 配置管理(新增)

### 修复内部包引用(5个文件)
- api/adapter/publisher.go - 修复 internal 包引用
- api/adapter/subscriber.go - 修复 internal 包引用
- api/model/envelope.go - 修复 internal 包引用
- api/model/operation.go - 修复 internal 包引用
- api/model/record.go - 修复 internal 包引用

### 单元测试(8个文件)
- api/persistence/*_test.go - 完整的单元测试
- 测试覆盖率:28.5%
- 测试通过率:49/49 (100%)

### SQL 脚本(4个文件)
- api/persistence/sql/postgresql.sql - PostgreSQL 建表脚本
- api/persistence/sql/mysql.sql - MySQL 建表脚本
- api/persistence/sql/sqlite.sql - SQLite 建表脚本
- api/persistence/sql/test_data.sql - 测试数据

### 文档(2个文件)
- README.md - 更新主文档,新增 Persistence 使用指南
- api/persistence/README.md - 完整的 Persistence 文档
- api/persistence/sql/README.md - SQL 脚本说明

## 技术亮点

1. **充分利用 Cursor 游标表**
   - 作为任务发现队列,非简单的位置记录
   - Key-Value 模式,支持多游标并发扫描
   - 时间戳天然有序,增量扫描高效

2. **双层保障机制**
   - Cursor:正常流程,快速处理
   - Retry:异常流程,可靠重试
   - 职责分离,监控清晰

3. **可空 IP 字段支持**
   - ClientIP 和 ServerIP 使用 *string 类型
   - 支持 NULL 值,符合数据库最佳实践
   - 使用 sql.NullString 正确处理

4. **完整的监控支持**
   - 未存证记录数监控
   - Cursor 延迟监控
   - 重试队列长度监控
   - 死信队列监控

## 测试结果

-  单元测试:49/49 通过 (100%)
-  代码覆盖率:28.5%
-  编译状态:无错误
-  支持数据库:PostgreSQL, MySQL, SQLite

## Breaking Changes

无破坏性变更。Persistence 模块作为可选功能,不影响现有代码。

## 版本信息

- 版本:v2.1.0
- Go 版本要求:1.21+
- 更新日期:2025-12-23
2025-12-23 18:59:43 +08:00

274 lines
6.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package adapter
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/apache/pulsar-client-go/pulsar"
"go.yandata.net/iod/iod/go-trustlog/api/logger"
)
const (
SubNameKey contextKey = "subName"
ReceiverQueueSizeKey contextKey = "receiverQueueSize"
IndexKey contextKey = "index"
ReceiverQueueSizeDefault = 1000
SubNameDefault = "subName"
TimeOutDefault = time.Second * 10
defaultMessageChannelSize = 10
)
type contextKey string
var _ message.Subscriber = &Subscriber{}
// SubscriberConfig is the configuration to create a subscriber.
type SubscriberConfig struct {
// URL is the URL to the broker
URL string
// SubscriberName is the name of the subscription.
SubscriberName string
// SubscriberType is the type of the subscription.
SubscriberType pulsar.SubscriptionType
// TLSTrustCertsFilePath is the path to the CA certificate file for verifying the server certificate.
// If empty, TLS verification will be disabled.
TLSTrustCertsFilePath string
// TLSCertificateFilePath is the path to the client certificate file for mTLS authentication.
// If empty, mTLS authentication will be disabled.
TLSCertificateFilePath string
// TLSKeyFilePath is the path to the client private key file for mTLS authentication.
// If empty, mTLS authentication will be disabled.
TLSKeyFilePath string
// TLSAllowInsecureConnection allows insecure TLS connections (not recommended for production).
TLSAllowInsecureConnection bool
}
// Subscriber provides the pulsar implementation for watermill subscribe operations.
type Subscriber struct {
conn pulsar.Client
logger logger.Logger
subsLock sync.RWMutex
// Change to map with composite key: topic + subscriptionName + subName
subs map[string]pulsar.Consumer
closed bool
closing chan struct{}
SubscribersCount int
clientID string
config SubscriberConfig
}
// NewSubscriber creates a new Subscriber.
func NewSubscriber(config SubscriberConfig, adapter logger.Logger) (*Subscriber, error) {
clientOptions := pulsar.ClientOptions{
URL: config.URL,
// Logger: 使用 Pulsar 默认 loggerinternal 包引用已移除)
}
// Configure TLS/mTLS
if err := configureTLSForClient(&clientOptions, config, adapter); err != nil {
return nil, errors.Join(err, errors.New("failed to configure TLS"))
}
conn, err := pulsar.NewClient(clientOptions)
if err != nil {
return nil, errors.Join(err, errors.New("cannot connect to Pulsar"))
}
return NewSubscriberWithPulsarClient(conn, config, adapter)
}
// NewSubscriberWithPulsarClient creates a new Subscriber with the provided pulsar client.
func NewSubscriberWithPulsarClient(
conn pulsar.Client,
config SubscriberConfig,
logger logger.Logger,
) (*Subscriber, error) {
return &Subscriber{
conn: conn,
logger: logger,
closing: make(chan struct{}),
clientID: watermill.NewULID(),
subs: make(map[string]pulsar.Consumer),
config: config,
}, nil
}
// Subscribe subscribes messages from Pulsar.
func (s *Subscriber) Subscribe(ctx context.Context, topic string) (<-chan *message.Message, error) {
output := make(chan *message.Message)
s.subsLock.Lock()
subName, ok := ctx.Value(SubNameKey).(string)
if !ok {
subName = SubNameDefault
}
index, ok := ctx.Value(IndexKey).(int)
if !ok {
index = 0
}
receiverQueueSize, ok := ctx.Value(ReceiverQueueSizeKey).(int)
if !ok {
receiverQueueSize = ReceiverQueueSizeDefault
}
subscriptionName := fmt.Sprintf("%s-%s", topic, s.clientID)
if s.config.SubscriberName != "" {
subscriptionName = s.config.SubscriberName
}
sn := fmt.Sprintf("%s_%s", subscriptionName, subName)
n := fmt.Sprintf("%s_%d", sn, index)
sub, found := s.subs[n]
if !found {
subscribeCtx, cancel := context.WithTimeout(ctx, TimeOutDefault)
defer cancel()
done := make(chan struct{})
var sb pulsar.Consumer
var err error
go func() {
defer close(done)
sb, err = s.conn.Subscribe(pulsar.ConsumerOptions{
Topic: topic,
Name: n,
SubscriptionName: sn,
Type: s.config.SubscriberType,
MessageChannel: make(chan pulsar.ConsumerMessage, defaultMessageChannelSize),
ReceiverQueueSize: receiverQueueSize,
})
}()
select {
case <-subscribeCtx.Done():
s.subsLock.Unlock()
return nil, fmt.Errorf("subscription timeout: %w", subscribeCtx.Err())
case <-done:
if err != nil {
s.subsLock.Unlock()
return nil, fmt.Errorf("subscription failed: %w", err)
}
}
s.subs[n] = sb
sub = sb
}
s.subsLock.Unlock()
// 创建本地引用以避免竞态条件
localSub := sub
go func() {
for {
select {
case <-s.closing:
s.logger.InfoContext(ctx, "subscriber is closing")
return
case <-ctx.Done():
s.logger.InfoContext(ctx, "exiting on context closure")
return
case m, msgOk := <-localSub.Chan():
if !msgOk {
// Channel closed, exit the loop
s.logger.InfoContext(ctx, "consumer channel closed")
return
}
go s.processMessage(ctx, output, m, localSub)
}
}
}()
return output, nil
}
func (s *Subscriber) processMessage(
ctx context.Context,
output chan *message.Message,
m pulsar.Message,
sub pulsar.Consumer,
) {
if s.isClosed() {
return
}
s.logger.DebugContext(ctx, "Received message", "key", m.Key())
ctx, cancelCtx := context.WithCancel(ctx)
defer cancelCtx()
msg := message.NewMessage(m.Key(), m.Payload())
select {
case <-s.closing:
s.logger.DebugContext(ctx, "Closing, message discarded", "key", m.Key())
return
case <-ctx.Done():
s.logger.DebugContext(ctx, "Context cancelled, message discarded")
return
// if this is first can risk 'send on closed channel' errors
case output <- msg:
s.logger.DebugContext(ctx, "Message sent to consumer")
}
select {
case <-msg.Acked():
err := sub.Ack(m)
if err != nil {
s.logger.DebugContext(ctx, "Message Ack Failed")
}
s.logger.DebugContext(ctx, "Message Acked")
case <-msg.Nacked():
sub.Nack(m)
s.logger.DebugContext(ctx, "Message Nacked")
case <-s.closing:
s.logger.DebugContext(ctx, "Closing, message discarded before ack")
return
case <-ctx.Done():
s.logger.DebugContext(ctx, "Context cancelled, message discarded before ack")
return
}
}
// Close closes the publisher and the underlying connection. It will attempt to wait for in-flight messages to complete.
func (s *Subscriber) Close() error {
s.subsLock.Lock()
defer s.subsLock.Unlock()
if s.closed {
return nil
}
s.closed = true
s.logger.DebugContext(context.Background(), "Closing subscriber")
defer s.logger.InfoContext(context.Background(), "Subscriber closed")
close(s.closing)
for _, sub := range s.subs {
sub.Close()
}
s.conn.Close()
return nil
}
func (s *Subscriber) isClosed() bool {
s.subsLock.RLock()
defer s.subsLock.RUnlock()
return s.closed
}