149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
|
|
// Command reconcile 是热度相关派生数据的运维入口:重算计数器、核对热榜、
|
|||
|
|
// 重建热榜分钟桶。
|
|||
|
|
//
|
|||
|
|
// 三个动作分开放,因为风险不同:Reconcile 是行锁 + 重算,可重入;Verify 只读,
|
|||
|
|
// 可以高频跑;Rebuild 的 Del+ZAdd 与并发增量有竞态,只能人工确认后执行。
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"flag"
|
|||
|
|
"log"
|
|||
|
|
"os"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"feedsystem_video_go/internal/config"
|
|||
|
|
"feedsystem_video_go/internal/db"
|
|||
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
|||
|
|
"feedsystem_video_go/internal/video"
|
|||
|
|
|
|||
|
|
"github.com/joho/godotenv"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func main() {
|
|||
|
|
counters := flag.Bool("counters", false, "重算 videos.likes_count / videos.popularity(写操作)")
|
|||
|
|
verify := flag.Bool("verify", false, "逐桶核对热榜窗口与 DB 计数")
|
|||
|
|
rebuild := flag.Bool("rebuild-hot", false, "用 DB 重写热榜分钟桶(会与并发增量竞态)")
|
|||
|
|
from := flag.String("from", "", "窗口起点 RFC3339(默认 now-60m)")
|
|||
|
|
to := flag.String("to", "", "窗口终点 RFC3339,开区间(默认本分钟起点,即含上一个闭合分钟)")
|
|||
|
|
limit := flag.Int("limit", 200, "counters 每轮修正的视频数")
|
|||
|
|
flag.Parse()
|
|||
|
|
|
|||
|
|
if !*counters && !*verify && !*rebuild {
|
|||
|
|
flag.Usage()
|
|||
|
|
os.Exit(2)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ctx := context.Background()
|
|||
|
|
gormDB := mustDB(ctx)
|
|||
|
|
// Redis 只在核对与重建时需要:counters 是纯 DB 操作。
|
|||
|
|
var cache *rediscache.Client
|
|||
|
|
if *verify || *rebuild {
|
|||
|
|
cache = mustCache(ctx)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fromT, toT := resolveWindow(*from, *to, time.Now())
|
|||
|
|
failed := false
|
|||
|
|
|
|||
|
|
if *counters {
|
|||
|
|
checked, fixed, err := video.NewCounterReconciler(gormDB).Reconcile(ctx, *limit)
|
|||
|
|
log.Printf("reconcile: checked=%d fixed=%d err=%v", checked, fixed, err)
|
|||
|
|
if err != nil || fixed > 0 {
|
|||
|
|
failed = true
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if *verify || *rebuild {
|
|||
|
|
admin := video.NewHotWindowAdmin(gormDB, cache)
|
|||
|
|
if *verify {
|
|||
|
|
buckets, drift, err := admin.Verify(ctx, fromT, toT)
|
|||
|
|
log.Printf("verify: buckets=%d drift=%d err=%v", buckets, drift, err)
|
|||
|
|
if err != nil || drift > 0 {
|
|||
|
|
failed = true
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if *rebuild {
|
|||
|
|
buckets, members, err := admin.Rebuild(ctx, fromT, toT)
|
|||
|
|
log.Printf("rebuild: buckets=%d members=%d err=%v", buckets, members, err)
|
|||
|
|
if err != nil {
|
|||
|
|
failed = true
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if failed {
|
|||
|
|
os.Exit(1)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// resolveWindow 解析 [from, to) 窗口。空串取默认值:最近 60 分钟,
|
|||
|
|
// 右端是本分钟起点——这一分钟还在增长,不能当桶(N6)。
|
|||
|
|
func resolveWindow(from, to string, now time.Time) (fromT, toT time.Time) {
|
|||
|
|
now = now.UTC()
|
|||
|
|
fromT = now.Truncate(time.Minute).Add(-time.Duration(video.HotWindowMinutes) * time.Minute)
|
|||
|
|
toT = now.Truncate(time.Minute)
|
|||
|
|
|
|||
|
|
if from != "" {
|
|||
|
|
if t, err := time.Parse(time.RFC3339, from); err == nil {
|
|||
|
|
fromT = t.UTC()
|
|||
|
|
} else {
|
|||
|
|
log.Fatalf("invalid -from %q: %v", from, err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if to != "" {
|
|||
|
|
if t, err := time.Parse(time.RFC3339, to); err == nil {
|
|||
|
|
toT = t.UTC()
|
|||
|
|
} else {
|
|||
|
|
log.Fatalf("invalid -to %q: %v", to, err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if !toT.After(fromT) {
|
|||
|
|
log.Fatalf("empty window: from=%s to=%s", fromT.Format(time.RFC3339), toT.Format(time.RFC3339))
|
|||
|
|
}
|
|||
|
|
return fromT, toT
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func mustDB(ctx context.Context) *gorm.DB {
|
|||
|
|
if err := godotenv.Load(); err != nil {
|
|||
|
|
log.Println(".env not found; continuing")
|
|||
|
|
}
|
|||
|
|
configPath := os.Getenv("CONFIG_PATH")
|
|||
|
|
if configPath == "" {
|
|||
|
|
configPath = "configs/config.yaml"
|
|||
|
|
}
|
|||
|
|
cfg, usedDefault, err := config.LoadLocalDev(configPath)
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to load config: %v", err)
|
|||
|
|
}
|
|||
|
|
if usedDefault {
|
|||
|
|
log.Printf("Config file %s not found, using default local config", configPath)
|
|||
|
|
}
|
|||
|
|
gormDB, err := db.NewDB(cfg.Database)
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to connect MySQL: %v", err)
|
|||
|
|
}
|
|||
|
|
return gormDB
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func mustCache(ctx context.Context) *rediscache.Client {
|
|||
|
|
configPath := os.Getenv("CONFIG_PATH")
|
|||
|
|
if configPath == "" {
|
|||
|
|
configPath = "configs/config.yaml"
|
|||
|
|
}
|
|||
|
|
cfg, _, err := config.LoadLocalDev(configPath)
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to load config: %v", err)
|
|||
|
|
}
|
|||
|
|
cache, err := rediscache.NewFromEnv(&cfg.Redis)
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to connect Redis: %v", err)
|
|||
|
|
}
|
|||
|
|
pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|||
|
|
defer cancel()
|
|||
|
|
if err := cache.Ping(pingCtx); err != nil {
|
|||
|
|
log.Fatalf("Redis not available: %v", err)
|
|||
|
|
}
|
|||
|
|
return cache
|
|||
|
|
}
|