Files
junhong_cmp_fiber/internal/application/cardobservation/traffic_collection.go
邹进 1214181e78
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 16m2s
feat: 轮询观测入库异步化,调网关与入库分离,采集记录经消息攒批写入
- 轮询步骤异步模式下只调网关并发布 card.ingest 入库消息,入库消费者固定并发 30、按采集 ID 去重、可重试错误不限次数退避重投
- 采集记录经 card.collection 攒批写入,失败整批重投,新增 (observation_id, observed_at) 唯一索引
- 网络、实名观测增加乱序保护;轮询读查询走缓存,套餐状态查询各自计时且失败不缓存
- 入库积压告警与反压;系统配置 polling.ingest.mode 默认 sync 可回退;测试环境 nsqd 消息全部落盘

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 16:33:29 +09:00

168 lines
6.4 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 cardobservation
import (
"context"
"encoding/json"
"time"
"unicode/utf8"
"go.uber.org/zap"
"gorm.io/datatypes"
cardflow "github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
domain "github.com/break/junhong_cmp_fiber/internal/domain/cardobservation"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// rawResponseMaxBytes 是原始响应体保存上限,超过时截断并标记。
const rawResponseMaxBytes = 4096
// collectionRecord 是一次采集的留痕内容;HasPackage 在观测事务内判断(只对成功读数有意义)。
type collectionRecord struct {
model.CardTrafficCollection
HasPackage bool
failure *domain.CollectionFailure
iccid string
}
func newCollectionRecord(observation domain.TrafficObservation) *collectionRecord {
record := &collectionRecord{CardTrafficCollection: model.CardTrafficCollection{
IotCardID: observation.CardID, ObservedAt: observation.Metadata.ObservedAt,
ObservationID: observation.Metadata.ObservationID, Source: observation.Metadata.Source,
Scene: observation.Metadata.Scene, Result: domain.ResolveReadingResult(observation),
CollectionRaw: buildCollectionRaw(observation.Raw), CarrierID: observation.CarrierID,
}, failure: observation.Failure, iccid: observation.ICCID}
if record.Result == constants.TrafficCollectionResultOK {
reading := observation.GatewayReadingMB
record.VendorUsedMB = &reading
}
return record
}
func (r *collectionRecord) setQueue(points []cardflow.QueuePoint) {
if encoded, err := json.Marshal(points); err == nil {
r.QueuePoints = encoded
}
}
func (r *collectionRecord) applyOutcome(outcome *flowOutcome) {
r.Decision = outcome.Decision
if !outcome.Written {
return
}
monthUsed, delta, stale := outcome.MonthUsedMB, outcome.DeltaMB, outcome.StaleGuarded
r.MonthUsedMB, r.DeltaMB, r.StaleGuarded, r.EventID = &monthUsed, &delta, &stale, outcome.EventID
}
// writeCollectionRecord 在观测事务之外写采集记录:成功读数只为采集时刻有套餐的卡写成功表;
// 失败读数不判断有无套餐,写失败表。注入发布器时发布到批量写入消息,由消费者攒批写库并失败重投;
// 未注入或发布失败时同步写库。
func (s *Service) writeCollectionRecord(ctx context.Context, record *collectionRecord, _ time.Time) {
if record.Result != constants.TrafficCollectionResultOK {
s.writeCollectionFailure(ctx, record)
return
}
if !record.HasPackage {
return
}
row := &record.CardTrafficCollection
if s.collectionSink != nil {
err := s.collectionSink.PublishCollection(ctx, row, nil)
if err == nil {
return
}
s.logTrafficWarn("发布卡流量采集记录消息失败,改为同步写库", zap.Uint("card_id", record.IotCardID),
zap.String("observation_id", record.ObservationID), zap.Error(err))
}
writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), constants.PollingIngestSyncCollectionTimeout)
defer cancel()
if err := postgres.NewCardTrafficCollectionStore(s.db).Create(writeCtx, row); err != nil {
s.logTrafficWarn("写卡流量采集记录失败", zap.Uint("card_id", record.IotCardID),
zap.String("observation_id", record.ObservationID), zap.Error(err))
}
}
func (s *Service) writeCollectionFailure(ctx context.Context, record *collectionRecord) {
row := &model.CardTrafficCollectionFailure{
IotCardID: record.IotCardID, ObservedAt: record.ObservedAt, ObservationID: record.ObservationID,
Source: record.Source, Scene: record.Scene, Result: record.Result, CollectionRaw: record.CollectionRaw,
ICCID: record.iccid, CarrierID: record.CarrierID,
}
if failure := record.failure; failure != nil {
row.ErrorCategory, row.ProviderCode = failure.ErrorCategory, failure.ProviderCode
row.ProviderMessage, row.DurationMS = truncateText(failure.ProviderMessage, 500), failure.DurationMS
}
writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), constants.PollingIngestSyncCollectionTimeout)
defer cancel()
if row.ICCID == "" || row.CarrierID == 0 {
var card model.IotCard
if err := s.db.WithContext(writeCtx).Unscoped().Select("iccid", "carrier_id").Where("id = ?", record.IotCardID).Take(&card).Error; err == nil {
row.ICCID, row.CarrierID = card.ICCID, card.CarrierID
}
}
if s.collectionSink != nil {
err := s.collectionSink.PublishCollection(ctx, nil, row)
if err == nil {
return
}
s.logTrafficWarn("发布卡流量采集失败记录消息失败,改为同步写库", zap.Uint("card_id", record.IotCardID),
zap.String("observation_id", record.ObservationID), zap.Error(err))
}
if err := postgres.NewCardTrafficCollectionFailureStore(s.db).Create(writeCtx, row); err != nil {
s.logTrafficWarn("写卡流量采集失败记录失败", zap.Uint("card_id", record.IotCardID),
zap.String("observation_id", record.ObservationID), zap.Error(err))
}
}
// buildCollectionRaw 把网关原始数据转为留存字段:响应可解析为 JSON 且不超过 4KB 时存 JSON,否则存文本并按 4KB 截断。
func buildCollectionRaw(raw *domain.GatewayRaw) model.CollectionRaw {
if raw == nil {
return model.CollectionRaw{}
}
out := model.CollectionRaw{
GatewayCode: raw.GatewayCode, GatewayMsg: truncateText(raw.GatewayMsg, 500),
GatewayTraceID: truncateText(raw.TraceID, 128), GatewayAttempts: raw.Attempts,
}
if raw.HTTPStatus > 0 {
status := raw.HTTPStatus
out.HTTPStatus = &status
}
if len(raw.Request) > 0 && json.Valid(raw.Request) {
out.RawRequest = datatypes.JSON(raw.Request)
}
switch {
case len(raw.Response) == 0:
case len(raw.Response) > rawResponseMaxBytes:
out.RawResponseText, out.RawTruncated = validUTF8Prefix(raw.Response, rawResponseMaxBytes), true
case json.Valid(raw.Response):
out.RawResponse = datatypes.JSON(raw.Response)
default:
out.RawResponseText = validUTF8Prefix(raw.Response, rawResponseMaxBytes)
}
return out
}
// validUTF8Prefix 返回不超过 limit 字节且不截断多字节字符的前缀,并替换非法 UTF-8(PostgreSQL text 不接受)。
func validUTF8Prefix(data []byte, limit int) string {
if len(data) > limit {
data = data[:limit]
for len(data) > 0 && !utf8.Valid(data) {
data = data[:len(data)-1]
}
}
if !utf8.Valid(data) {
return string([]rune(string(data)))
}
return string(data)
}
func truncateText(value string, limit int) string {
runes := []rune(value)
if len(runes) <= limit {
return value
}
return string(runes[:limit])
}