fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性
This commit is contained in:
@@ -98,6 +98,18 @@ def is_domain_fresh(
|
||||
)
|
||||
|
||||
|
||||
def lock_account_row(db: Session, account_id: int | None) -> None:
|
||||
"""写账号派生表前先对账号主键加 X 锁,避免子表外键 S 锁与后续 UPDATE accounts 互锁成死锁。"""
|
||||
if not account_id:
|
||||
return
|
||||
(
|
||||
db.query(Account.id)
|
||||
.filter(Account.id == int(account_id))
|
||||
.with_for_update()
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def _get_or_create_state(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -105,11 +117,15 @@ def _get_or_create_state(
|
||||
domain: str,
|
||||
platform: str = "douyu",
|
||||
) -> AccountSyncState:
|
||||
state = get_sync_state(
|
||||
db,
|
||||
account_id=account_id,
|
||||
domain=domain,
|
||||
platform=platform,
|
||||
state = (
|
||||
db.query(AccountSyncState)
|
||||
.filter(
|
||||
AccountSyncState.account_id == account_id,
|
||||
AccountSyncState.platform == platform,
|
||||
AccountSyncState.domain == domain,
|
||||
)
|
||||
.with_for_update()
|
||||
.first()
|
||||
)
|
||||
if state is None:
|
||||
state = AccountSyncState(
|
||||
@@ -129,6 +145,7 @@ def mark_domain_dirty(
|
||||
domain: str,
|
||||
reason: str = "",
|
||||
) -> AccountSyncState:
|
||||
lock_account_row(db, account_id)
|
||||
state = _get_or_create_state(db, account_id=account_id, domain=domain)
|
||||
state.status = "dirty"
|
||||
state.valid_until = None
|
||||
@@ -148,6 +165,7 @@ def start_sync_run(
|
||||
source: str,
|
||||
task_id: int | None = None,
|
||||
) -> AccountSyncRun:
|
||||
lock_account_row(db, account_id)
|
||||
moment = datetime.now(UTC)
|
||||
state = _get_or_create_state(db, account_id=account_id, domain=domain)
|
||||
state.last_attempt_at = moment
|
||||
@@ -322,25 +340,51 @@ def record_sync_success(
|
||||
) -> StorehomeSyncResult:
|
||||
"""登记一次已经完成的官方查询投影。
|
||||
|
||||
适用于探针已经拿到并完成归一化的结果,避免调用方为了同步记录再重复请求。
|
||||
适用于探针已经拿到并完成归一化的结果,避免调用方为了同步记录再重复请求,
|
||||
也避免同一次查询把 account_sync_states 先改成 running 再改成 success 的双倍锁更新。
|
||||
"""
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
run = start_sync_run(
|
||||
db,
|
||||
lock_account_row(db, account_id)
|
||||
now = datetime.now(UTC)
|
||||
moment = observed_at or now
|
||||
ttl_seconds = max(
|
||||
1,
|
||||
int(
|
||||
valid_for_seconds
|
||||
if valid_for_seconds is not None
|
||||
else capability_ttl_seconds()
|
||||
),
|
||||
)
|
||||
state = _get_or_create_state(db, account_id=account_id, domain=domain)
|
||||
state.last_attempt_at = now
|
||||
state.last_success_at = now
|
||||
state.status = "success"
|
||||
state.valid_until = now + timedelta(seconds=ttl_seconds)
|
||||
state.next_retry_at = state.valid_until
|
||||
state.error_code = ""
|
||||
state.error_message = ""
|
||||
state.source = source[:32]
|
||||
if raw_payload is not None:
|
||||
safe_payload = _json_safe(raw_payload)
|
||||
state.raw_payload_json = safe_payload
|
||||
state.payload_hash = _payload_hash(safe_payload)
|
||||
state.state_version = int(state.state_version or 0) + 1
|
||||
|
||||
run = AccountSyncRun(
|
||||
account_id=account_id,
|
||||
platform="douyu",
|
||||
domain=domain,
|
||||
source=source,
|
||||
task_id=task_id,
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="success",
|
||||
source=source[:32],
|
||||
item_count=int(item_count),
|
||||
changed_count=int(changed_count),
|
||||
duration_ms=0,
|
||||
official_observed_at=moment,
|
||||
)
|
||||
finish_sync_success(
|
||||
db,
|
||||
run,
|
||||
item_count=item_count,
|
||||
changed_count=changed_count,
|
||||
observed_at=moment,
|
||||
raw_payload=raw_payload,
|
||||
valid_for_seconds=valid_for_seconds,
|
||||
)
|
||||
db.add(run)
|
||||
db.flush()
|
||||
return StorehomeSyncResult(
|
||||
item_count=int(item_count),
|
||||
changed_count=int(changed_count),
|
||||
|
||||
Reference in New Issue
Block a user