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

@@ -16,7 +16,7 @@ import (
"gorm.io/gorm/logger"
)
// 评论消费者"只写一次评论、不再碰热度"这条不变式只能在真实 MySQL 上验证:
// 评论消费者"只写一次评论、热度跟着事务走"这条不变式只能在真实 MySQL 上验证:
// 判重落在唯一索引 (author_id, idempotency_key) 上,没有数据库就没有被测对象。
//
// MYSQL_TEST_DSN='root:123456@tcp(127.0.0.1:3306)/feedsystem_test?charset=utf8mb4&parseTime=True&loc=Local' \
@@ -37,10 +37,10 @@ func newCommentWorkerTestDB(t *testing.T) *gorm.DB {
if err != nil {
t.Fatalf("connect mysql: %v", err)
}
if err := db.Migrator().DropTable(&video.Comment{}, &video.Video{}); err != nil {
if err := db.Migrator().DropTable(&video.Comment{}, &video.Like{}, &video.Video{}); err != nil {
t.Fatalf("drop tables: %v", err)
}
if err := db.AutoMigrate(&video.Video{}, &video.Comment{}); err != nil {
if err := db.AutoMigrate(&video.Video{}, &video.Comment{}, &video.Like{}); err != nil {
t.Fatalf("migrate: %v", err)
}
return db
@@ -67,12 +67,11 @@ func seedCommentVideo(t *testing.T, db *gorm.DB) *video.Video {
//
// 两个断言分别钉住两件不同的事:行数 = 1 证明唯一索引吸收了重复;热度 = 1
// (而不是 2)证明"加热度"与"插入评论"在同一个事务里、且以 created 为门控。
// 热度写在这里而不是 Redis,是因为 videos.popularity 与 comments 必须原子
// 提交——Redis 那条轨由 popularity 事件单独负责。
// 热度写在这里而不是 Redis,是因为 videos.popularity 是事实,热榜只是它的滑窗副本。
func TestCommentWorker_ApplyPublish_DuplicateEventWritesOneRow(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), "comment.events")
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), nil, "comment.events")
evt := &rabbitmq.CommentEvent{
EventID: "evt-comment-1",
Action: "publish",
@@ -110,7 +109,7 @@ func TestCommentWorker_ApplyPublish_DuplicateEventWritesOneRow(t *testing.T) {
func TestCommentWorker_ApplyPublish_EmptyEventIDStillWrites(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), "comment.events")
w := NewCommentWorker(nil, nil, video.NewCommentRepository(db), video.NewVideoRepository(db), nil, "comment.events")
evt := &rabbitmq.CommentEvent{
Action: "publish",
@@ -137,10 +136,10 @@ func TestCommentWorker_ApplyDelete_DuplicateIsNoop(t *testing.T) {
db := newCommentWorkerTestDB(t)
v := seedCommentVideo(t, db)
repo := video.NewCommentRepository(db)
w := NewCommentWorker(nil, nil, repo, video.NewVideoRepository(db), "comment.events")
w := NewCommentWorker(nil, nil, repo, video.NewVideoRepository(db), nil, "comment.events")
c := &video.Comment{Username: "alice", VideoID: v.ID, AuthorID: 1, Content: "x"}
if _, err := repo.CreateIdempotentWithPopularity(context.Background(), c, 0); err != nil {
if _, err := repo.CreateIdempotentWithPopularity(context.Background(), c, 1); err != nil {
t.Fatalf("seed comment: %v", err)
}
@@ -158,4 +157,12 @@ func TestCommentWorker_ApplyDelete_DuplicateIsNoop(t *testing.T) {
if count != 0 {
t.Fatalf("评论行数 = %d, want 0", count)
}
// P2-20:删除必须回退热度,且只回退一次(第二次删除是空操作)。
var got video.Video
if err := db.First(&got, v.ID).Error; err != nil {
t.Fatalf("reload video: %v", err)
}
if got.Popularity != 0 {
t.Fatalf("popularity = %d, want 0(删除要回退热度,重复删除不得再减)", got.Popularity)
}
}