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:
@@ -4,9 +4,13 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"feedsystem_video_go/internal/apierror"
|
||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
)
|
||||
|
||||
type fakeVideoExister struct {
|
||||
@@ -25,6 +29,13 @@ type fakeCommentStore struct {
|
||||
lastComment *Comment
|
||||
lastDelta int64
|
||||
|
||||
// 删除路径:byID 是 GetByID 的返回,deleteRow/deleted 是 DeleteWithPopularity 的返回。
|
||||
byID *Comment
|
||||
deleteCalls int
|
||||
deleteRow *Comment
|
||||
deleted bool
|
||||
deleteErr error
|
||||
|
||||
findNames []string
|
||||
ids map[string]uint
|
||||
findErr error
|
||||
@@ -40,11 +51,14 @@ func (f *fakeCommentStore) CreateIdempotentWithPopularity(_ context.Context, c *
|
||||
return f.created, f.createErr
|
||||
}
|
||||
|
||||
func (f *fakeCommentStore) GetByID(_ context.Context, _ uint) (*Comment, error) { return nil, nil }
|
||||
func (f *fakeCommentStore) GetByID(_ context.Context, _ uint) (*Comment, error) {
|
||||
return f.byID, nil
|
||||
}
|
||||
|
||||
func (f *fakeCommentStore) DeleteComment(_ context.Context, _ *Comment) error { return nil }
|
||||
|
||||
func (f *fakeCommentStore) DeleteByID(_ context.Context, _ uint) (bool, error) { return false, nil }
|
||||
func (f *fakeCommentStore) DeleteWithPopularity(_ context.Context, _ uint) (*Comment, bool, error) {
|
||||
f.deleteCalls++
|
||||
return f.deleteRow, f.deleted, f.deleteErr
|
||||
}
|
||||
|
||||
func (f *fakeCommentStore) GetAllComments(_ context.Context, _ uint) ([]Comment, error) {
|
||||
return nil, nil
|
||||
@@ -63,6 +77,7 @@ func (f *fakeCommentStore) FindAccountIDs(_ context.Context, names []string) (ma
|
||||
type fakeCommentPublisher struct {
|
||||
events []rabbitmq.CommentEvent
|
||||
pubErr error
|
||||
delErr error
|
||||
}
|
||||
|
||||
func (f *fakeCommentPublisher) Publish(_ context.Context, evt rabbitmq.CommentEvent) error {
|
||||
@@ -70,50 +85,45 @@ func (f *fakeCommentPublisher) Publish(_ context.Context, evt rabbitmq.CommentEv
|
||||
return f.pubErr
|
||||
}
|
||||
|
||||
func (f *fakeCommentPublisher) Delete(_ context.Context, _ uint, _ string) error { return nil }
|
||||
|
||||
type popularityCall struct {
|
||||
videoID uint
|
||||
change int64
|
||||
key string
|
||||
}
|
||||
|
||||
type fakePopularityPublisher struct {
|
||||
calls []popularityCall
|
||||
pubErr error
|
||||
}
|
||||
|
||||
func (f *fakePopularityPublisher) Update(_ context.Context, videoID uint, change int64, intentKey string) error {
|
||||
f.calls = append(f.calls, popularityCall{videoID: videoID, change: change, key: intentKey})
|
||||
return f.pubErr
|
||||
}
|
||||
func (f *fakeCommentPublisher) Delete(_ context.Context, _ uint, _ string) error { return f.delErr }
|
||||
|
||||
type commentFixture struct {
|
||||
svc *CommentService
|
||||
store *fakeCommentStore
|
||||
comments *fakeCommentPublisher
|
||||
pop *fakePopularityPublisher
|
||||
cache *rediscache.Client
|
||||
mr *miniredis.Miniredis
|
||||
}
|
||||
|
||||
func newCommentFixture(t *testing.T) *commentFixture {
|
||||
t.Helper()
|
||||
store := &fakeCommentStore{created: true, ids: map[string]uint{}}
|
||||
comments := &fakeCommentPublisher{}
|
||||
pop := &fakePopularityPublisher{}
|
||||
cache, _ := newTestCache(t)
|
||||
svc := NewCommentService(store, &fakeVideoExister{exists: true}, cache, comments, pop)
|
||||
return &commentFixture{svc: svc, store: store, comments: comments, pop: pop}
|
||||
cache, mr := newTestCache(t)
|
||||
svc := NewCommentService(store, &fakeVideoExister{exists: true}, cache, comments)
|
||||
return &commentFixture{svc: svc, store: store, comments: comments, cache: cache, mr: mr}
|
||||
}
|
||||
|
||||
// hotScoreAt 返回某个视频在某一分钟桶里的分数;桶不存在时返回 0。
|
||||
func (f *commentFixture) hotScoreAt(t *testing.T, videoID uint, at time.Time) float64 {
|
||||
t.Helper()
|
||||
score, err := f.mr.ZScore(f.cache.Key("hot:video:1m:%s", HotBucket(at)), HotMember(videoID))
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func newComment(videoID, authorID uint, content string) *Comment {
|
||||
return &Comment{VideoID: videoID, AuthorID: authorID, Username: "alice", Content: content}
|
||||
}
|
||||
|
||||
// 两条事件都发出去之后,API 绝不能再写一次库:否则"MQ 正常"的常规路径
|
||||
// 评论事件发出去之后,API 绝不能再写一次库:否则"MQ 正常"的常规路径
|
||||
// 会稳定产生两条评论——这正是 P0-4 的核心症状之一。
|
||||
func TestCommentService_Publish_BothEnqueued_SkipsLocalWrite(t *testing.T) {
|
||||
func TestCommentService_Publish_Enqueued_SkipsLocalWriteAndHot(t *testing.T) {
|
||||
f := newCommentFixture(t)
|
||||
if err := f.svc.Publish(context.Background(), newComment(7, 1, "hello")); err != nil {
|
||||
c := newComment(7, 1, "hello")
|
||||
if err := f.svc.Publish(context.Background(), c); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
if f.store.createCalls != 0 {
|
||||
@@ -122,12 +132,9 @@ func TestCommentService_Publish_BothEnqueued_SkipsLocalWrite(t *testing.T) {
|
||||
if len(f.comments.events) != 1 || f.comments.events[0].EventID == "" {
|
||||
t.Fatalf("评论事件 = %+v, want 1 条且带 event_id", f.comments.events)
|
||||
}
|
||||
if len(f.pop.calls) != 1 || f.pop.calls[0].key == "" {
|
||||
t.Fatalf("热度事件 = %+v, want 1 条且带意图键", f.pop.calls)
|
||||
}
|
||||
if f.comments.events[0].EventID != f.pop.calls[0].key {
|
||||
t.Fatalf("两条事件必须共享同一个意图键: comment=%q popularity=%q",
|
||||
f.comments.events[0].EventID, f.pop.calls[0].key)
|
||||
// 热榜由 worker 在事务提交后写:API 提前加一次就是双计。
|
||||
if got := f.hotScoreAt(t, 7, c.CreatedAt); got != 0 {
|
||||
t.Fatalf("入队成功的路径不得改热榜,实际分数 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +144,8 @@ func TestCommentService_Publish_CommentPublishFails_WritesLocallyWithIntentKey(t
|
||||
f := newCommentFixture(t)
|
||||
f.comments.pubErr = errors.New("broker down")
|
||||
|
||||
if err := f.svc.Publish(context.Background(), newComment(7, 1, "hello")); err != nil {
|
||||
c := newComment(7, 1, "hello")
|
||||
if err := f.svc.Publish(context.Background(), c); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
if f.store.createCalls != 1 {
|
||||
@@ -147,25 +155,80 @@ func TestCommentService_Publish_CommentPublishFails_WritesLocallyWithIntentKey(t
|
||||
if written == nil || *written == "" {
|
||||
t.Fatal("兜底写入的评论必须带上意图键,否则 worker 侧无法判重")
|
||||
}
|
||||
if got := f.pop.calls[0].key; got != *written {
|
||||
t.Fatalf("兜底写入的键 %q 与热度事件的键 %q 不一致", *written, got)
|
||||
}
|
||||
if f.store.lastDelta != 1 {
|
||||
t.Fatalf("热度增量 = %d, want 1", f.store.lastDelta)
|
||||
}
|
||||
// 事务真的插入了行,热榜才 +1,且落在评论的 created_at 分钟。
|
||||
if got := f.hotScoreAt(t, 7, f.store.lastComment.CreatedAt); got != 1 {
|
||||
t.Fatalf("兜底路径热榜分数 = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 评论事件入队成功、热度发布失败:这是旧代码返回 500 的那条路径。
|
||||
// 现在只降级更新热榜,库由 worker 写,接口返回成功。
|
||||
func TestCommentService_Publish_PopularityPublishFails_AppliesHotCacheOnce(t *testing.T) {
|
||||
// 兜底写入命中唯一索引(created=false)说明这一行早就存在,计数与热榜
|
||||
// 都已经被那次写驱动过:此时再加一次就是重复。
|
||||
func TestCommentService_Publish_FallbackDedupeHit_DoesNotTouchHot(t *testing.T) {
|
||||
f := newCommentFixture(t)
|
||||
f.pop.pubErr = errors.New("broker down")
|
||||
f.comments.pubErr = errors.New("broker down")
|
||||
f.store.created = false
|
||||
|
||||
if err := f.svc.Publish(context.Background(), newComment(9, 1, "hello")); err != nil {
|
||||
t.Fatalf("Publish 不应失败: %v", err)
|
||||
c := newComment(7, 1, "hello")
|
||||
if err := f.svc.Publish(context.Background(), c); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
if f.store.createCalls != 0 {
|
||||
t.Fatalf("评论已入队,不应直写,实际调用 %d 次", f.store.createCalls)
|
||||
if f.store.createCalls != 1 {
|
||||
t.Fatalf("兜底写入 %d 次, want 1", f.store.createCalls)
|
||||
}
|
||||
if got := f.hotScoreAt(t, 7, c.CreatedAt); got != 0 {
|
||||
t.Fatalf("判重命中时热榜不得变化,实际分数 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 删除入队成功时 API 不碰库:热度回退由 worker 在同一事务后做。
|
||||
func TestCommentService_Delete_Enqueued_SkipsLocalWrite(t *testing.T) {
|
||||
f := newCommentFixture(t)
|
||||
f.store.byID = &Comment{ID: 5, VideoID: 7, AuthorID: 1}
|
||||
|
||||
if err := f.svc.Delete(context.Background(), 5, 1); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
if f.store.deleteCalls != 0 {
|
||||
t.Fatalf("删除已入队,不得直写库,实际调用 %d 次", f.store.deleteCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// 删除事件发不出去时走兜底:删行与热度 -1 都发生,且 -1 落回评论创建的那一分钟
|
||||
// (P2-20:旧实现只删行、不减热度)。
|
||||
func TestCommentService_Delete_FallbackRevertsHotAtOriginalBucket(t *testing.T) {
|
||||
f := newCommentFixture(t)
|
||||
f.comments.delErr = errors.New("broker down")
|
||||
createdAt := time.Now().UTC().Add(-20 * time.Minute).Truncate(time.Minute)
|
||||
f.store.byID = &Comment{ID: 5, VideoID: 7, AuthorID: 1}
|
||||
f.store.deleted = true
|
||||
f.store.deleteRow = &Comment{ID: 5, VideoID: 7, AuthorID: 1, CreatedAt: createdAt}
|
||||
|
||||
if err := f.svc.Delete(context.Background(), 5, 1); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
if f.store.deleteCalls != 1 {
|
||||
t.Fatalf("兜底删除 %d 次, want 1", f.store.deleteCalls)
|
||||
}
|
||||
if got := f.hotScoreAt(t, 7, createdAt); got != -1 {
|
||||
t.Fatalf("原桶分数 = %v, want -1(回退必须落回创建那一分钟)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 重复删除是空操作:deleted=false 时不能再减一次热度。
|
||||
func TestCommentService_Delete_RepeatIsNoop(t *testing.T) {
|
||||
f := newCommentFixture(t)
|
||||
f.comments.delErr = errors.New("broker down")
|
||||
f.store.byID = &Comment{ID: 5, VideoID: 7, AuthorID: 1}
|
||||
f.store.deleted = false
|
||||
|
||||
if err := f.svc.Delete(context.Background(), 5, 1); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
if got := f.hotScoreAt(t, 7, time.Now()); got != 0 {
|
||||
t.Fatalf("重复删除不得改热榜,实际分数 %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user