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:
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"feedsystem_video_go/internal/apierror"
|
||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||
@@ -20,34 +21,31 @@ type CommentPublisher interface {
|
||||
Delete(ctx context.Context, commentID uint, intentKey string) error
|
||||
}
|
||||
|
||||
type PopularityPublisher interface {
|
||||
Update(ctx context.Context, videoID uint, change int64, intentKey string) error
|
||||
}
|
||||
|
||||
// videoExister 由 *VideoRepository 满足,因此 router.go 无需改动。
|
||||
type videoExister interface {
|
||||
IsExist(ctx context.Context, id uint) (bool, error)
|
||||
}
|
||||
|
||||
type CommentService struct {
|
||||
store CommentStore
|
||||
videos videoExister
|
||||
cache *rediscache.Client
|
||||
commentMQ CommentPublisher
|
||||
popularityMQ PopularityPublisher
|
||||
store CommentStore
|
||||
videos videoExister
|
||||
cache *rediscache.Client
|
||||
commentMQ CommentPublisher
|
||||
}
|
||||
|
||||
func NewCommentService(store CommentStore, videos videoExister, cache *rediscache.Client, commentMQ CommentPublisher, popularityMQ PopularityPublisher) *CommentService {
|
||||
func NewCommentService(store CommentStore, videos videoExister, cache *rediscache.Client, commentMQ CommentPublisher) *CommentService {
|
||||
return &CommentService{
|
||||
store: store,
|
||||
videos: videos,
|
||||
cache: cache,
|
||||
commentMQ: commentMQ,
|
||||
popularityMQ: popularityMQ,
|
||||
store: store,
|
||||
videos: videos,
|
||||
cache: cache,
|
||||
commentMQ: commentMQ,
|
||||
}
|
||||
}
|
||||
|
||||
// Publish 用同一个意图键编排两条路径:MQ 成功则不直写,失败则直写。
|
||||
// Publish 先入队,入队失败才直写。
|
||||
//
|
||||
// 热度不再单独发布:它只由"这次插入是否真的落库"驱动,所以两条轨不会
|
||||
// 各自成功一半。
|
||||
func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
|
||||
if comment == nil {
|
||||
return errors.New("comment is nil")
|
||||
@@ -71,6 +69,9 @@ func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
|
||||
return fmt.Errorf("%w: video not found", apierror.ErrValidation)
|
||||
}
|
||||
|
||||
// 行时间只在"直写"分支用到;worker 分支用它自己消息里的 occurred_at。
|
||||
comment.CreatedAt = time.Now().UTC()
|
||||
|
||||
commentEnqueued := false
|
||||
if err := s.commentMQ.Publish(ctx, rabbitmq.CommentEvent{
|
||||
EventID: key,
|
||||
@@ -85,13 +86,6 @@ func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
|
||||
log.Printf("%s enqueue comment failed, fallback to direct write: %v", span, err)
|
||||
}
|
||||
|
||||
popularityEnqueued := false
|
||||
if err := s.popularityMQ.Update(ctx, comment.VideoID, 1, key); err == nil {
|
||||
popularityEnqueued = true
|
||||
} else {
|
||||
log.Printf("%s enqueue popularity failed, fallback to hot cache: %v", span, err)
|
||||
}
|
||||
|
||||
// 入队成功时不直写,避免正常路径上稳定双写。
|
||||
if !commentEnqueued {
|
||||
created, err := s.store.CreateIdempotentWithPopularity(ctx, comment, 1)
|
||||
@@ -99,12 +93,11 @@ func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
|
||||
return fmt.Errorf("fallback create comment: %w", err)
|
||||
}
|
||||
log.Printf("%s fallback write dedupe=%s", span, dedupeLabel(created))
|
||||
}
|
||||
|
||||
// 热度与评论是两次独立发布,失败只降级。
|
||||
if !popularityEnqueued {
|
||||
if err := UpdatePopularityCache(ctx, s.cache, comment.VideoID, 1, key); err != nil {
|
||||
log.Printf("%s hot cache update failed (degraded): %v", span, err)
|
||||
// 只有真的插入了才加热度:重复意图已经被唯一索引挡掉。
|
||||
if created {
|
||||
if err := ApplyHotDelta(ctx, s.cache, comment.VideoID, 1, comment.CreatedAt); err != nil {
|
||||
log.Printf("%s hot window update failed (degraded): %v", span, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,14 +116,31 @@ func (s *CommentService) Delete(ctx context.Context, commentID uint, accountID u
|
||||
if comment.AuthorID != accountID {
|
||||
return apierror.ErrUnauthorized
|
||||
}
|
||||
key, keyErr := NewIntentKey()
|
||||
if keyErr != nil {
|
||||
key = ""
|
||||
|
||||
if s.commentMQ != nil {
|
||||
key, keyErr := NewIntentKey()
|
||||
if keyErr != nil {
|
||||
key = ""
|
||||
}
|
||||
err := s.commentMQ.Delete(ctx, commentID, key)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
log.Printf("comment_id=%d stage=comment.delete enqueue failed, fallback to direct write: %v", commentID, err)
|
||||
}
|
||||
if err := s.commentMQ.Delete(ctx, commentID, key); err == nil {
|
||||
|
||||
deleted, ok, err := s.store.DeleteWithPopularity(ctx, commentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
// 已经被删除(重复投递或另一条路径先到):空操作,热度不能再减。
|
||||
return nil
|
||||
}
|
||||
return s.store.DeleteComment(ctx, comment)
|
||||
if err := ApplyHotDelta(ctx, s.cache, deleted.VideoID, -1, deleted.CreatedAt); err != nil {
|
||||
log.Printf("comment_id=%d stage=comment.delete hot window update failed (degraded): %v", commentID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *CommentService) GetAll(ctx context.Context, videoID uint) ([]Comment, error) {
|
||||
|
||||
Reference in New Issue
Block a user