优化商品多规格探测边界
This commit is contained in:
1 parent
c30029557c
commit
ac57a354ee
5 files changed
+195
-16
No files matched your search
@@ -742,3 +742,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P1 账号删除生命周期 fencing 切片 | 已完成 | 删除流程新增账号级 `BeginStopping/EndStopping` fencing,停止窗口内拒绝新的 runtime 启动;删除前停止 runtime 改为受 5 秒请求 Context 约束的同步收束,超时返回明确冲突,不再创建游离 Stop goroutine;重新取得凭证锁后复核账号归属再删除,避免停止窗口内误删被替换账号;新增 Manager fencing 回归测试,Go/Server 定向测试、`make check` 和 Go/React 注释门禁通过。调度器 Context-aware 等待、删除任务 ID/状态查询、其他生命周期任务统一清单仍待治理,阶段 5 继续保持“进行中” |
|
||||
| 2026-08-15 | P2 订单详情分片事务写入切片 | 已完成 | 订单详情远端请求仍在锁外执行;每个详情分片先收集成功结果,再通过订单应用 `UnitOfWork` 在单事务内提交,避免逐订单独立事务和分片内部分提交;事务失败时整片结果统一失败并保留远端会话收束逻辑;订单刷新定向测试、`make check`、架构和中文注释门禁通过。尚未实现真正单 SQL 多行 UPSERT、游标分页和后台 Job 化,P2 批量同步仍保持“进行中” |
|
||||
| 2026-08-15 | P2 订单刷新复合游标扫描切片 | 已完成 | 订单刷新应用 Port 移除 OFFSET 分页入口,数据库新增 `created_at + order_id` 复合游标查询并统一扫描模型;刷新流程按游标读取每个账号订单,加入游标不推进保护;1001 条订单无重复扫描、订单/Server 定向测试和中文注释门禁通过。用户订单列表 HTTP 分页仍是页码语义;真正单 SQL 多行 UPSERT、后台 Job 化、SQLite/MySQL/Postgres 查询计划证据仍待完成,P2 批量同步保持“进行中” |
|
||||
| 2026-08-15 | P2 商品多规格探测批量与并发边界切片 | 已完成 | 商品同步先用 `MultiSpecFlags` 一次读取本地多规格标记,消除逐商品数据库查询;剩余远端详情探测使用 4 路有界并发,Session 过期可取消同批次探测,普通单项失败保留告警并继续;新增批量标记和并发上限回归测试,定向测试与中文注释门禁通过。跨请求缓存、跨批次去重、平台限流指标和 MySQL/Postgres 查询计划仍待完成,P2 批量同步保持“进行中” |
|
||||
@@ -88,6 +88,65 @@ func (i *Items) IsMultiSpec(ctx context.Context, cookieID, itemID string) bool {
|
||||
return v != 0
|
||||
}
|
||||
|
||||
// MultiSpecFlags 批量读取账号商品的多规格标记,避免同步时逐商品查询数据库。
|
||||
func (i *Items) MultiSpecFlags(ctx context.Context, cookieID string, itemIDs []string) (map[string]bool, error) {
|
||||
// uniqueIDs 保存去重后的商品标识,避免重复占用 SQL 参数。
|
||||
uniqueIDs := make([]string, 0, len(itemIDs))
|
||||
// seenIDs 保存已经加入查询的商品标识。
|
||||
seenIDs := make(map[string]struct{}, len(itemIDs))
|
||||
// itemID 表示当前待查询的商品标识。
|
||||
for _, itemID := range itemIDs {
|
||||
itemID = strings.TrimSpace(itemID)
|
||||
if itemID == "" {
|
||||
continue
|
||||
}
|
||||
// exists 表示当前商品标识是否已经加入查询集合。
|
||||
if _, exists := seenIDs[itemID]; exists {
|
||||
continue
|
||||
}
|
||||
seenIDs[itemID] = struct{}{}
|
||||
uniqueIDs = append(uniqueIDs, itemID)
|
||||
}
|
||||
// flags 保存已存在商品的多规格标记;缺失商品不会出现在结果中。
|
||||
flags := make(map[string]bool, len(uniqueIDs))
|
||||
if len(uniqueIDs) == 0 {
|
||||
return flags, nil
|
||||
}
|
||||
// placeholders 保存 IN 子句的参数占位符。
|
||||
placeholders := make([]string, len(uniqueIDs))
|
||||
// args 保存账号和商品查询参数。
|
||||
args := make([]any, 0, len(uniqueIDs)+1)
|
||||
args = append(args, cookieID)
|
||||
// index、itemID 分别表示占位符下标和当前商品标识。
|
||||
for index, itemID := range uniqueIDs {
|
||||
placeholders[index] = "?"
|
||||
args = append(args, itemID)
|
||||
}
|
||||
// rows、err 保存批量标记查询结果及错误。
|
||||
rows, err := i.DB.QueryContext(ctx,
|
||||
`SELECT item_id, is_multi_spec FROM item_info WHERE cookie_id=? AND deleted_at IS NULL AND item_id IN (`+strings.Join(placeholders, ",")+`)`, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
// itemID、isMultiSpec 保存当前商品标识及多规格值。
|
||||
var itemID string
|
||||
// isMultiSpec 保存数据库中的多规格整数标记。
|
||||
var isMultiSpec int
|
||||
// err 表示扫描当前商品标记时的数据库错误。
|
||||
if err := rows.Scan(&itemID, &isMultiSpec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flags[itemID] = isMultiSpec != 0
|
||||
}
|
||||
// err 表示遍历批量标记结果时的数据库错误。
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return flags, nil
|
||||
}
|
||||
|
||||
// MultiQuantityDelivery 是否开启多数量发货。
|
||||
func (i *Items) MultiQuantityDelivery(ctx context.Context, cookieID, itemID string) bool {
|
||||
// v 保存v,供当前处理流程使用
|
||||
|
||||
@@ -612,6 +612,15 @@ func TestItems_GetAndFlags(t *testing.T) {
|
||||
if !s.Items.MultiQuantityDelivery(ctx, cid, "i1") {
|
||||
t.Fatal("MultiQuantityDelivery 应 true")
|
||||
}
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := s.Items.Upsert(ctx, &ItemInfoRow{CookieID: cid, ItemID: "i2", ItemTitle: "普通商品"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// flags、err 保存批量多规格标记及错误。
|
||||
flags, err := s.Items.MultiSpecFlags(ctx, cid, []string{"i1", "i2", "missing", "i1"})
|
||||
if err != nil || !flags["i1"] || flags["i2"] || len(flags) != 2 {
|
||||
t.Fatalf("MultiSpecFlags=%v err=%v", flags, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCards_ConsumeBatchData data 类型卡券按行消费。
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -759,33 +760,88 @@ func (s *Server) syncSyncedItems(ctx context.Context, cookieID string, items []m
|
||||
return s.Store.Items.SyncFromRemote(ctx, cookieID, rows)
|
||||
}
|
||||
|
||||
// enrichSyncedItemMultiSpec 负责enrichSynced商品MultiSpec相关处理。
|
||||
// itemSpecProbeConcurrency 限制商品多规格远端探测的并发数,避免同步洪峰压垮平台接口。
|
||||
const itemSpecProbeConcurrency = 4
|
||||
|
||||
// enrichSyncedItemMultiSpec 批量读取本地标记并受限并发探测剩余商品的多规格状态。
|
||||
func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Client, cookies, cookieID string, items []mtop.ItemListItem) error {
|
||||
// fetcher、ok 保存fetcher、ok,供当前处理流程使用
|
||||
fetcher, ok := client.(mtop.ItemDetailFetcher)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
// index 表示当前遍历过程中的index
|
||||
// itemIDs 保存待批量读取本地多规格标记的商品标识。
|
||||
itemIDs := make([]string, 0, len(items))
|
||||
// item 表示当前同步返回的商品。
|
||||
for _, item := range items {
|
||||
itemIDs = append(itemIDs, item.ID)
|
||||
}
|
||||
// localFlags、flagsErr 保存本地多规格标记及批量查询错误。
|
||||
localFlags, flagsErr := s.Store.Items.MultiSpecFlags(ctx, cookieID, itemIDs)
|
||||
if flagsErr != nil && s.Logger != nil {
|
||||
s.Logger.Warn("批量读取商品多规格标记失败,将继续远端探测", "cookie_id", cookieID, "err", flagsErr)
|
||||
}
|
||||
// candidates 保存需要访问远端接口的商品下标。
|
||||
candidates := make([]int, 0, len(items))
|
||||
// index 表示当前商品在同步列表中的下标。
|
||||
// index 表示当前商品在同步列表中的下标。
|
||||
for index := range items {
|
||||
if items[index].IsMultiSpec || s.Store.Items.IsMultiSpec(ctx, cookieID, items[index].ID) {
|
||||
if items[index].IsMultiSpec || localFlags[items[index].ID] {
|
||||
items[index].IsMultiSpec = true
|
||||
continue
|
||||
}
|
||||
// isMultiSpec、err 保存isMultiSpec、err,供当前处理流程使用
|
||||
isMultiSpec, err := fetcher.DetectItemMultiSpec(ctx, cookies, items[index].ID)
|
||||
if err != nil {
|
||||
if mtop.IsSessionExpiredErr(err) {
|
||||
return err
|
||||
}
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("识别商品多规格状态失败", "cookie_id", cookieID, "item_id", items[index].ID, "err", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
items[index].IsMultiSpec = isMultiSpec
|
||||
candidates = append(candidates, index)
|
||||
}
|
||||
return nil
|
||||
if len(candidates) == 0 {
|
||||
return nil
|
||||
}
|
||||
// probeCtx、cancel 让 Session 过期时的其他探测尽快停止。
|
||||
probeCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
// semaphore 限制同时进行的远端商品详情请求数。
|
||||
semaphore := make(chan struct{}, itemSpecProbeConcurrency)
|
||||
// waitGroup 等待所有商品探测 goroutine 收束。
|
||||
var waitGroup sync.WaitGroup
|
||||
// errorMu 保护 sessionErr 的并发写入。
|
||||
var errorMu sync.Mutex
|
||||
// sessionErr 保存首个需要中断同步的 Session 过期错误。
|
||||
var sessionErr error
|
||||
// index 表示当前需要远端探测的商品下标。
|
||||
for _, index := range candidates {
|
||||
waitGroup.Add(1)
|
||||
go func(index int) {
|
||||
defer waitGroup.Done()
|
||||
select {
|
||||
case semaphore <- struct{}{}:
|
||||
case <-probeCtx.Done():
|
||||
return
|
||||
}
|
||||
defer func() { <-semaphore }()
|
||||
// isMultiSpec、err 保存当前商品远端探测结果及错误。
|
||||
isMultiSpec, err := fetcher.DetectItemMultiSpec(probeCtx, cookies, items[index].ID)
|
||||
if err != nil {
|
||||
if mtop.IsSessionExpiredErr(err) {
|
||||
errorMu.Lock()
|
||||
if sessionErr == nil {
|
||||
sessionErr = err
|
||||
cancel()
|
||||
}
|
||||
errorMu.Unlock()
|
||||
return
|
||||
}
|
||||
if probeCtx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("识别商品多规格状态失败", "cookie_id", cookieID, "item_id", items[index].ID, "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
items[index].IsMultiSpec = isMultiSpec
|
||||
}(index)
|
||||
}
|
||||
waitGroup.Wait()
|
||||
return sessionErr
|
||||
}
|
||||
|
||||
// listItemsByCookie 负责list商品列表By登录凭证相关处理。
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
@@ -631,6 +632,59 @@ func TestSyncItemsFromAccountDetectsMultiSpecFromDetail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestEnrichSyncedItemMultiSpecLimitsProbeConcurrency 验证商品多规格探测使用有界并发且覆盖全部候选商品。
|
||||
func TestEnrichSyncedItemMultiSpecLimitsProbeConcurrency(t *testing.T) {
|
||||
// srv、store、cleanup 保存用于商品同步测试的服务、数据库和清理函数。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// stateMu 保护远端探测并发计数。
|
||||
var stateMu sync.Mutex
|
||||
// active、maxActive 保存当前及观测到的最大远端探测并发数。
|
||||
active, maxActive := 0, 0
|
||||
// client 是带并发计数的商品详情测试客户端。
|
||||
client := withMTopTransport(roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if !strings.Contains(req.URL.String(), "mtop.taobao.idle.pc.detail") {
|
||||
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{}`)), Request: req}, nil
|
||||
}
|
||||
stateMu.Lock()
|
||||
active++
|
||||
if active > maxActive {
|
||||
maxActive = active
|
||||
}
|
||||
stateMu.Unlock()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
stateMu.Lock()
|
||||
active--
|
||||
stateMu.Unlock()
|
||||
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"ret":["SUCCESS::调用成功"],"data":{"multiSKU":true,"skuDO":{"skuList":[{"id":"a"}]}}}`)), Request: req}, nil
|
||||
}))
|
||||
// items 保存等待探测的商品列表。
|
||||
items := make([]mtop.ItemListItem, 8)
|
||||
// index 表示当前商品在测试列表中的下标。
|
||||
for index := range items {
|
||||
items[index].ID = fmt.Sprintf("probe-%d", index)
|
||||
}
|
||||
// err 表示批量多规格探测错误。
|
||||
err := srv.enrichSyncedItemMultiSpec(context.Background(), client, "unb=1; _m_h5_tk=t_1;", "acc1", items)
|
||||
if err != nil {
|
||||
t.Fatalf("enrich multi spec: %v", err)
|
||||
}
|
||||
if maxActive > itemSpecProbeConcurrency {
|
||||
t.Fatalf("probe concurrency=%d want <=%d", maxActive, itemSpecProbeConcurrency)
|
||||
}
|
||||
// index、item 分别表示商品下标和当前探测结果。
|
||||
for index, item := range items {
|
||||
if !item.IsMultiSpec {
|
||||
t.Fatalf("item %d was not marked multi spec", index)
|
||||
}
|
||||
}
|
||||
// flags、err 保存数据库批量标记查询结果及错误,确认本地查询未误报候选商品。
|
||||
flags, err := store.Items.MultiSpecFlags(context.Background(), "acc1", []string{"probe-0"})
|
||||
if err != nil || len(flags) != 0 {
|
||||
t.Fatalf("unexpected local flags=%v err=%v", flags, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSyncItemsFromAccountFail mtop 返回非成功 → 502。
|
||||
func TestSyncItemsFromAccountFail(t *testing.T) {
|
||||
// srv、cleanup 保存srv、cleanup,供当前处理流程使用
|
||||
|
||||
Reference in new issue
Block a user