fix: 修复商务汇总任务超时并验证计划启用

This commit is contained in:
Yvan
2026-11-04 15:46:46 +08:00
parent 7764b03906
commit 34535c90c3
9 changed files with 260 additions and 36 deletions
@@ -3,6 +3,8 @@ package business
import (
"strings"
"testing"
"kra/internal/biz/business"
)
func TestMySQLBusinessQueryIndexesAreAdditiveAndIdempotent(t *testing.T) {
@@ -48,3 +50,69 @@ func TestMySQLBusinessQueryIndexesAreAdditiveAndIdempotent(t *testing.T) {
t.Fatalf("must reject a conflicting existing index: %v", err)
}
}
func TestMySQLReportBuildIndexesCoverInputsAndPreserveIdentities(t *testing.T) {
db := openRehearsalMySQL(t)
for _, sql := range []string{
"CREATE TABLE tab_user(id INT,source_db VARCHAR(10),puid INT,promote_id INT,idcard VARCHAR(20),payload VARCHAR(1000),PRIMARY KEY(id,source_db)) ENGINE=InnoDB",
"CREATE TABLE tab_user_play_info(id INT PRIMARY KEY,play_time INT,user_id INT,source_db VARCHAR(10),promote_id INT,game_id INT,server_id VARCHAR(30),payload VARCHAR(1000),KEY(play_time)) ENGINE=InnoDB",
} {
if err := db.Exec(sql).Error; err != nil {
t.Fatal(err)
}
}
users, plays := []map[string]any{}, []map[string]any{}
known := map[business.ReportUserKey]bool{}
ids := []int64{}
for id := int64(1); id <= 4000; id++ {
for _, source := range []string{"lx", "hw"} {
users = append(users, map[string]any{"id": id, "source_db": source, "puid": 0, "promote_id": id, "idcard": source, "payload": strings.Repeat("x", 300)})
}
plays = append(plays, map[string]any{"id": id, "play_time": id, "user_id": id, "source_db": "lx", "promote_id": id, "game_id": 1, "server_id": " 001 ", "payload": strings.Repeat("x", 300)})
if id <= 501 {
known[business.ReportUserKey{SourceDB: "lx", ID: id}] = true
ids = append(ids, id)
}
}
for table, rows := range map[string][]map[string]any{"tab_user": users, "tab_user_play_info": plays} {
if err := db.Table(table).CreateInBatches(rows, 200).Error; err != nil {
t.Fatal(err)
}
}
for attempt := 0; attempt < 2; attempt++ {
if err := migrateReportBuildQueryIndexes(db); err != nil {
t.Fatal(err)
}
}
loaded, err := loadReportUsers(db, known, map[int64]bool{501: true})
if err != nil || len(loaded) != 502 {
t.Fatalf("分批读取或跨平台歧义匹配错误: rows=%d error=%v", len(loaded), err)
}
for key, user := range loaded {
if user.IDCard != key.SourceDB || user.PromoteID == nil || *user.PromoteID != key.ID {
t.Fatalf("迁移改变用户归属或身份: %+v", key)
}
}
for _, query := range []struct {
sql, index string
args []any
}{
{"SELECT id,source_db,puid,promote_id,idcard FROM tab_user WHERE source_db=? AND id IN ?", "idx_sw_user_report_identity", []any{"lx", ids}},
{"SELECT id,source_db,puid,promote_id,idcard FROM tab_user WHERE id IN ?", "idx_sw_user_report_identity", []any{ids}},
{"SELECT user_id,source_db,promote_id,game_id,server_id FROM tab_user_play_info WHERE server_id<>'' AND play_time BETWEEN ? AND ?", "idx_sw_play_report_active", []any{1, 501}},
} {
var plan []struct{ Key, Type, Extra string }
if err := db.Raw("EXPLAIN "+query.sql, query.args...).Scan(&plan).Error; err != nil {
t.Fatal(err)
}
if len(plan) != 1 || plan[0].Key != query.index || plan[0].Type != "range" || !strings.Contains(plan[0].Extra, "Using index") {
t.Fatalf("任务查询未使用覆盖范围索引: %+v", plan)
}
}
if err := db.Exec("ALTER TABLE tab_user DROP INDEX idx_sw_user_report_identity, ADD INDEX idx_sw_user_report_identity(id)").Error; err != nil {
t.Fatal(err)
}
if err := migrateReportBuildQueryIndexes(db); err == nil || !strings.Contains(err.Error(), "idx_sw_user_report_identity") {
t.Fatalf("同名但列不符的索引必须拒绝: %v", err)
}
}