perf(fulfillment): 修复 Outbox 状态版本去重、跨进程实时推送与数据库默认配置

This commit is contained in:
yml
2026-11-28 18:52:42 +08:00
parent f23ef42e67
commit d03eeaee18
22 changed files with 751 additions and 72 deletions
@@ -21,6 +21,8 @@ from __future__ import annotations
import json
import os
import threading
import time
from datetime import UTC, datetime, timedelta
from typing import Any
@@ -40,6 +42,48 @@ INTERNAL_SHOP_ID = "91kaquan"
_ENGINE: Engine | None = None
_INITIALIZED = False
_LOOKUP_LOCK = threading.Lock()
_LOOKUP_SUMMARY_INTERVAL_SECONDS = 60.0
_LOOKUP_SUMMARY_STEP = 50
_CROSS_DB_LOOKUP_COUNTS: dict[str, int] = {
"lookup_order": 0,
"list_pending_manual": 0,
"item_lookup": 0,
}
_LAST_LOOKUP_SUMMARY_AT = 0.0
_LAST_LOOKUP_SUMMARY_TOTAL = 0
def _record_cross_db_lookup(path: str, detail: str) -> None:
global _LAST_LOOKUP_SUMMARY_AT, _LAST_LOOKUP_SUMMARY_TOTAL
now_mono = time.monotonic()
should_summarize = False
snapshot: dict[str, int] | None = None
with _LOOKUP_LOCK:
_CROSS_DB_LOOKUP_COUNTS[path] = _CROSS_DB_LOOKUP_COUNTS.get(path, 0) + 1
path_total = _CROSS_DB_LOOKUP_COUNTS[path]
grand_total = sum(_CROSS_DB_LOOKUP_COUNTS.values())
if (
grand_total - _LAST_LOOKUP_SUMMARY_TOTAL >= _LOOKUP_SUMMARY_STEP
or now_mono - _LAST_LOOKUP_SUMMARY_AT >= _LOOKUP_SUMMARY_INTERVAL_SECONDS
):
_LAST_LOOKUP_SUMMARY_AT = now_mono
_LAST_LOOKUP_SUMMARY_TOTAL = grand_total
should_summarize = True
snapshot = dict(_CROSS_DB_LOOKUP_COUNTS)
logger.debug(
"order_site 跨库只读回查 path={} path_hits={} detail={}",
path,
path_total,
detail,
)
if should_summarize and snapshot is not None:
logger.info("order_site 跨库只读回查累计计数 counts={}", snapshot)
def get_cross_db_lookup_counts() -> dict[str, int]:
with _LOOKUP_LOCK:
return dict(_CROSS_DB_LOOKUP_COUNTS)
class OrderLookupUnavailable(Exception):
@@ -144,6 +188,7 @@ def _fetch_rows(order_no: str) -> list[dict[str, Any]]:
"""
)
try:
_record_cross_db_lookup("lookup_order", f"order_no={order_no}")
with engine.connect() as conn:
return [
dict(row)
@@ -222,6 +267,7 @@ def _fetch_pending_manual_fulfillments(
"""
)
try:
_record_cross_db_lookup("list_pending_manual", f"limit={limit}")
with engine.connect() as conn:
params: dict[str, Any] = {
"limit": max(1, min(limit, 500)),
@@ -341,6 +387,7 @@ def lookup_pending_manual_fulfillment(
"""
)
try:
_record_cross_db_lookup("item_lookup", f"source_order_item_id={item_id}")
with _get_engine().connect() as conn:
row = (
conn.execute(sql, {"source_order_item_id": int(item_id)})