refactor(video): 计数改由业务事务维护并删除热度第二条写路径

点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。
热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。
删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。
删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。
DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。
likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。
验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
This commit is contained in:
2026-11-01 15:15:15 +08:00
parent c0210b61ba
commit 1b47c9f027
26 changed files with 924 additions and 626 deletions

View File

@@ -32,11 +32,6 @@ func TestTopologyConstants_WireNamesAreStable(t *testing.T) {
{"SocialFollowRK", SocialFollowRK, "social.follow"},
{"SocialUnfollowRK", SocialUnfollowRK, "social.unfollow"},
{"PopularityExchange", PopularityExchange, "video.popularity.events"},
{"PopularityQueue", PopularityQueue, "video.popularity.events"},
{"PopularityBindingKey", PopularityBindingKey, "video.popularity.*"},
{"PopularityUpdateRK", PopularityUpdateRK, "video.popularity.update"},
{"TimelineExchange", TimelineExchange, "video.timeline.events"},
{"TimelineQueue", TimelineQueue, "video.timeline.update.queue"},
{"TimelineBindingKey", TimelineBindingKey, "video.timeline.*"},

View File

@@ -40,8 +40,8 @@ func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
return &LikeMQ{mgr: base}, nil
}
// Like / Unlike 的 intentKey 语义见 PopularityMQ.Update:同一个键会同时
// 出现在点赞事件与热度事件的 event_id 里,让两条投递链路共享一次判重身份。
// Like / Unlike 的 intentKey 只作为 LikeEvent.EventID:调用方用它把日志与
// 一条用户意图对齐,判重仍由 likes 表的唯一索引负责(见 ApplyLike)。
func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint, intentKey string) error {
return l.publish(ctx, "like", LikeLikeRK, userID, videoID, intentKey)
}

View File

@@ -1,66 +0,0 @@
package rabbitmq
import (
"context"
"errors"
"time"
)
// PopularityMQ 通过连接管理器发布热度事件(不再持有长生命周期 channel)。
type PopularityMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
const (
PopularityExchange = "video.popularity.events"
PopularityQueue = "video.popularity.events"
PopularityBindingKey = "video.popularity.*"
PopularityUpdateRK = "video.popularity.update"
)
type PopularityEvent struct {
EventID string `json:"event_id"`
VideoID uint `json:"video_id"`
Change int64 `json:"change"`
OccurredAt time.Time `json:"occurred_at"`
}
func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
base.RegisterTopology(Topology{Exchange: PopularityExchange, Queue: PopularityQueue, BindingKey: PopularityBindingKey})
return &PopularityMQ{mgr: base}, nil
}
// Update 发布一条热度事件。
//
// intentKey 是"这次用户意图"的稳定标识,会写进 event_id:调用方(如点赞)
// 用同一个键同时发布点赞事件与热度事件,于是两条消息无论重复投递多少次,
// 消费端都能认出"这是同一次意图"(P0-4)。intentKey 为空时退化为随机 id
// ——注意此时消费端无法去重,所以生产路径必须传键。
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64, intentKey string) error {
if p == nil || p.mgr == nil {
return errors.New("popularity mq is not initialized")
}
if videoID == 0 || change == 0 {
return errors.New("videoID and change are required")
}
id := intentKey
if id == "" {
var err error
id, err = newEventID(16)
if err != nil {
return err
}
}
event := PopularityEvent{
EventID: id,
VideoID: videoID,
Change: change,
OccurredAt: time.Now().UTC(),
}
return p.mgr.PublishJSON(ctx, PopularityExchange, PopularityUpdateRK, event)
}