feat(reconcile): 新增计数重算与热榜核对重建命令
三个动作分开放,因为风险不同。 Reconcile 重算 likes_count 与 popularity。它先锁 videos 行再 COUNT,可安全重跑。 Verify 逐桶比对 ZSET 与 DB。它只读,可以高频跑。 Rebuild 用 DB 重写分钟桶。Del 与 ZAdd 会与并发增量竞态,因此只能手工跑。 新增 cmd/reconcile,旗标为 -counters、-verify、-rebuild-hot、-from、-to、-limit。 发现漂移或修正了行时以退出码 1 结束。cron 可据此告警。 验证:go build -buildvcs=false ./... 通过。
This commit is contained in:
74
backend/internal/video/counters_reconcile.go
Normal file
74
backend/internal/video/counters_reconcile.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CounterReconciler struct{ db *gorm.DB }
|
||||
|
||||
func NewCounterReconciler(db *gorm.DB) *CounterReconciler { return &CounterReconciler{db: db} }
|
||||
|
||||
// Reconcile 找出计数与源行不一致的视频,逐条重算。
|
||||
//
|
||||
// 为什么先 SELECT ... FOR UPDATE 再 COUNT:点赞/评论事务的顺序都是
|
||||
// "插行 → UPDATE videos",而 UPDATE videos 会拿 videos 的行锁。因此只要
|
||||
// 我们先锁住这一行,就不可能有新的业务行在 COUNT 期间提交(它提交不了
|
||||
// 的 UPDATE 被我们挡住),COUNT 得到的是一个稳定值;随后我们把计数写成
|
||||
// 这个值,等锁释放后业务事务再在正确值上 +1。
|
||||
func (r *CounterReconciler) Reconcile(ctx context.Context, limit int) (checked, fixed int, err error) {
|
||||
const q = `
|
||||
SELECT v.id FROM videos v
|
||||
LEFT JOIN (SELECT video_id, COUNT(*) c FROM likes GROUP BY video_id) l ON l.video_id = v.id
|
||||
LEFT JOIN (SELECT video_id, COUNT(*) c FROM comments GROUP BY video_id) c ON c.video_id = v.id
|
||||
WHERE v.likes_count <> COALESCE(l.c, 0)
|
||||
OR v.popularity <> COALESCE(l.c, 0) + COALESCE(c.c, 0)
|
||||
ORDER BY v.id
|
||||
LIMIT ?`
|
||||
var ids []uint
|
||||
if err := r.db.WithContext(ctx).Raw(q, limit).Scan(&ids).Error; err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
for _, id := range ids {
|
||||
checked++
|
||||
ok, err := r.fixOne(ctx, id)
|
||||
if err != nil {
|
||||
return checked, fixed, err
|
||||
}
|
||||
if ok {
|
||||
fixed++
|
||||
}
|
||||
}
|
||||
return checked, fixed, nil
|
||||
}
|
||||
|
||||
func (r *CounterReconciler) fixOne(ctx context.Context, id uint) (bool, error) {
|
||||
fixed := false
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var locked uint
|
||||
if err := tx.Raw("SELECT id FROM videos WHERE id = ? FOR UPDATE", id).Scan(&locked).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if locked == 0 {
|
||||
return nil // 视频在这期间被删了
|
||||
}
|
||||
var likes, comments int64
|
||||
if err := tx.Model(&Like{}).Where("video_id = ?", id).Count(&likes).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&Comment{}).Where("video_id = ?", id).Count(&comments).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
res := tx.Model(&Video{}).Where("id = ?", id).UpdateColumns(map[string]any{
|
||||
"likes_count": likes,
|
||||
"popularity": likes + comments,
|
||||
})
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
fixed = res.RowsAffected > 0
|
||||
return nil
|
||||
})
|
||||
return fixed, err
|
||||
}
|
||||
Reference in New Issue
Block a user