perf(fulfillment): 优化本地能力选号查询与索引
This commit is contained in:
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
|||||||
PROJECT_ROOT = TESTS_DIR.parent
|
PROJECT_ROOT = TESTS_DIR.parent
|
||||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||||
|
|
||||||
HEAD_REVISION = "20261123_0122"
|
HEAD_REVISION = "20261123_0123"
|
||||||
|
|
||||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||||
EXTRA_INDEXES = (
|
EXTRA_INDEXES = (
|
||||||
|
|||||||
@@ -1153,6 +1153,35 @@ class TestSnapshotSync(_Base):
|
|||||||
|
|
||||||
|
|
||||||
class TestAccountSelection(_Base):
|
class TestAccountSelection(_Base):
|
||||||
|
def test_capability_candidate_query_selects_only_selection_columns(self):
|
||||||
|
query = account_selector._capability_candidate_query(
|
||||||
|
self.session,
|
||||||
|
commodity_id=COMMODITY,
|
||||||
|
required_points=1280,
|
||||||
|
now=NOW,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert query is not None
|
||||||
|
selected = {
|
||||||
|
str(description["name"])
|
||||||
|
for description in query.column_descriptions
|
||||||
|
}
|
||||||
|
assert {
|
||||||
|
"account_id",
|
||||||
|
"account_username",
|
||||||
|
"account_points",
|
||||||
|
"account_elite_opened",
|
||||||
|
"state_offer_state",
|
||||||
|
"state_observed_at",
|
||||||
|
} <= selected
|
||||||
|
assert {
|
||||||
|
"account_cookie",
|
||||||
|
"account_overview_snapshot",
|
||||||
|
"account_ck_check_result",
|
||||||
|
"state_raw_json",
|
||||||
|
"state_schedule_json",
|
||||||
|
}.isdisjoint(selected)
|
||||||
|
|
||||||
def test_manual_allocation_request_only_marks_task_due(self):
|
def test_manual_allocation_request_only_marks_task_due(self):
|
||||||
task = self.task()
|
task = self.task()
|
||||||
old_retry_at = task.next_allocate_at
|
old_retry_at = task.next_allocate_at
|
||||||
|
|||||||
@@ -489,6 +489,10 @@
|
|||||||
"20261123_0122_execution_jobs_recharge_guard.py": {
|
"20261123_0122_execution_jobs_recharge_guard.py": {
|
||||||
"revision": "20261123_0122",
|
"revision": "20261123_0122",
|
||||||
"sha256": "e3549ea76a9da00d1e6e01e11d25fbb6fca3cce64b26223a8f0352a6fbec4c7d"
|
"sha256": "e3549ea76a9da00d1e6e01e11d25fbb6fca3cce64b26223a8f0352a6fbec4c7d"
|
||||||
|
},
|
||||||
|
"20261123_0123_capability_candidate_index.py": {
|
||||||
|
"revision": "20261123_0123",
|
||||||
|
"sha256": "4fc8ed0659f92a141ff383b5a5925e8b3e73aff7540b523198742971dc9b628b"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""add covering index for fulfillment capability candidates.
|
||||||
|
|
||||||
|
Revision ID: 20261123_0123
|
||||||
|
Revises: 20261123_0122
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision: str = "20261123_0123"
|
||||||
|
down_revision: str | None = "20261123_0122"
|
||||||
|
branch_labels: str | None = None
|
||||||
|
depends_on: str | None = None
|
||||||
|
|
||||||
|
INDEX_NAME = "ix_account_commodity_candidate"
|
||||||
|
REPLACED_INDEX_NAME = "ix_account_commodity_eligibility"
|
||||||
|
|
||||||
|
|
||||||
|
def _indexes() -> set[str]:
|
||||||
|
return {
|
||||||
|
str(index["name"])
|
||||||
|
for index in sa.inspect(op.get_bind()).get_indexes(
|
||||||
|
"account_commodity_states"
|
||||||
|
)
|
||||||
|
if index.get("name")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
indexes = _indexes()
|
||||||
|
if INDEX_NAME not in indexes:
|
||||||
|
op.create_index(
|
||||||
|
INDEX_NAME,
|
||||||
|
"account_commodity_states",
|
||||||
|
[
|
||||||
|
"platform",
|
||||||
|
"scope",
|
||||||
|
"commodity_id",
|
||||||
|
"account_eligibility",
|
||||||
|
"offer_state",
|
||||||
|
"valid_until",
|
||||||
|
"account_id",
|
||||||
|
"last_exchange_at",
|
||||||
|
"observed_at",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
if REPLACED_INDEX_NAME in indexes:
|
||||||
|
op.drop_index(
|
||||||
|
REPLACED_INDEX_NAME,
|
||||||
|
table_name="account_commodity_states",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
indexes = _indexes()
|
||||||
|
if REPLACED_INDEX_NAME not in indexes:
|
||||||
|
op.create_index(
|
||||||
|
REPLACED_INDEX_NAME,
|
||||||
|
"account_commodity_states",
|
||||||
|
[
|
||||||
|
"platform",
|
||||||
|
"scope",
|
||||||
|
"commodity_id",
|
||||||
|
"account_eligibility",
|
||||||
|
"offer_state",
|
||||||
|
"valid_until",
|
||||||
|
"account_id",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
if INDEX_NAME in indexes:
|
||||||
|
op.drop_index(INDEX_NAME, table_name="account_commodity_states")
|
||||||
@@ -2118,7 +2118,7 @@ class AccountCommodityState(Base):
|
|||||||
"account_id",
|
"account_id",
|
||||||
),
|
),
|
||||||
Index(
|
Index(
|
||||||
"ix_account_commodity_eligibility",
|
"ix_account_commodity_candidate",
|
||||||
"platform",
|
"platform",
|
||||||
"scope",
|
"scope",
|
||||||
"commodity_id",
|
"commodity_id",
|
||||||
@@ -2126,6 +2126,8 @@ class AccountCommodityState(Base):
|
|||||||
"offer_state",
|
"offer_state",
|
||||||
"valid_until",
|
"valid_until",
|
||||||
"account_id",
|
"account_id",
|
||||||
|
"last_exchange_at",
|
||||||
|
"observed_at",
|
||||||
),
|
),
|
||||||
Index(
|
Index(
|
||||||
"ix_account_commodity_account",
|
"ix_account_commodity_account",
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ from .douyu_goods_service import (
|
|||||||
)
|
)
|
||||||
from .file_service import build_file_url
|
from .file_service import build_file_url
|
||||||
from .platform_fulfillment.account_selector import (
|
from .platform_fulfillment.account_selector import (
|
||||||
|
CapabilityAccount,
|
||||||
acquire_account_lease,
|
acquire_account_lease,
|
||||||
active_lease_for_account,
|
active_lease_for_account,
|
||||||
capability_candidate_accounts,
|
capability_candidate_accounts,
|
||||||
@@ -651,7 +652,7 @@ def recommend_accounts(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
now = _now()
|
now = _now()
|
||||||
candidates_by_id: dict[int, Account] = {}
|
candidates_by_id: dict[int, Account | CapabilityAccount] = {}
|
||||||
if cid:
|
if cid:
|
||||||
for account in capability_candidate_accounts(
|
for account in capability_candidate_accounts(
|
||||||
db,
|
db,
|
||||||
|
|||||||
@@ -64,8 +64,38 @@ class AccountCandidate:
|
|||||||
class CapabilityCandidate:
|
class CapabilityCandidate:
|
||||||
"""一个新鲜能力快照及其对应账号,供本地零外部请求选号。"""
|
"""一个新鲜能力快照及其对应账号,供本地零外部请求选号。"""
|
||||||
|
|
||||||
account: Account
|
account: CapabilityAccount
|
||||||
state: AccountCommodityState
|
state: CapabilityState
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class CapabilityAccount:
|
||||||
|
"""能力选号实际需要的账号字段,不加载 CK / JSON 等大列。"""
|
||||||
|
|
||||||
|
id: int
|
||||||
|
username: str
|
||||||
|
nickname: str
|
||||||
|
uid: str
|
||||||
|
points: int | None
|
||||||
|
gold_balance: int | None
|
||||||
|
points_expire_at: datetime | None
|
||||||
|
role_change_available_at: datetime | None
|
||||||
|
elite_opened: bool | None
|
||||||
|
role_bind_status: str
|
||||||
|
ck_check_status: str
|
||||||
|
game_name: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class CapabilityState:
|
||||||
|
"""选号排序之后才需要的能力快照详情。"""
|
||||||
|
|
||||||
|
account_eligibility: str
|
||||||
|
offer_state: str
|
||||||
|
observed_at: datetime | None
|
||||||
|
available_from: datetime | None
|
||||||
|
valid_until: datetime | None
|
||||||
|
blocking_reason: str
|
||||||
|
|
||||||
|
|
||||||
def _now(now: datetime | None = None) -> datetime:
|
def _now(now: datetime | None = None) -> datetime:
|
||||||
@@ -251,7 +281,24 @@ def _capability_candidate_query(
|
|||||||
allow_before_open=allow_before_open,
|
allow_before_open=allow_before_open,
|
||||||
)
|
)
|
||||||
return (
|
return (
|
||||||
db.query(Account, AccountCommodityState)
|
db.query(
|
||||||
|
Account.id.label("account_id"),
|
||||||
|
Account.username.label("account_username"),
|
||||||
|
Account.nickname.label("account_nickname"),
|
||||||
|
Account.uid.label("account_uid"),
|
||||||
|
Account.points.label("account_points"),
|
||||||
|
Account.gold_balance.label("account_gold_balance"),
|
||||||
|
Account.points_expire_at.label("account_points_expire_at"),
|
||||||
|
Account.role_change_available_at.label(
|
||||||
|
"account_role_change_available_at"
|
||||||
|
),
|
||||||
|
Account.elite_opened.label("account_elite_opened"),
|
||||||
|
Account.role_bind_status.label("account_role_bind_status"),
|
||||||
|
Account.ck_check_status.label("account_ck_check_status"),
|
||||||
|
Account.game_name.label("account_game_name"),
|
||||||
|
AccountCommodityState.offer_state.label("state_offer_state"),
|
||||||
|
AccountCommodityState.observed_at.label("state_observed_at"),
|
||||||
|
)
|
||||||
.join(
|
.join(
|
||||||
AccountCommodityState,
|
AccountCommodityState,
|
||||||
AccountCommodityState.account_id == Account.id,
|
AccountCommodityState.account_id == Account.id,
|
||||||
@@ -315,10 +362,67 @@ def capability_candidate_rows(
|
|||||||
)
|
)
|
||||||
if query is None:
|
if query is None:
|
||||||
return []
|
return []
|
||||||
return [
|
rows = query.all()
|
||||||
CapabilityCandidate(account=account, state=state)
|
if not rows:
|
||||||
for account, state in query.all()
|
return []
|
||||||
]
|
account_ids = [int(row.account_id) for row in rows]
|
||||||
|
state_rows = (
|
||||||
|
db.query(
|
||||||
|
AccountCommodityState.account_id,
|
||||||
|
AccountCommodityState.account_eligibility,
|
||||||
|
AccountCommodityState.offer_state,
|
||||||
|
AccountCommodityState.observed_at,
|
||||||
|
AccountCommodityState.available_from,
|
||||||
|
AccountCommodityState.valid_until,
|
||||||
|
AccountCommodityState.blocking_reason,
|
||||||
|
)
|
||||||
|
.filter(
|
||||||
|
AccountCommodityState.account_id.in_(account_ids),
|
||||||
|
AccountCommodityState.platform == "douyu",
|
||||||
|
AccountCommodityState.scope == scope,
|
||||||
|
AccountCommodityState.commodity_id == commodity_id.strip(),
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
states = {
|
||||||
|
int(row.account_id): CapabilityState(
|
||||||
|
account_eligibility=str(row.account_eligibility or ""),
|
||||||
|
offer_state=str(row.offer_state or ""),
|
||||||
|
observed_at=row.observed_at,
|
||||||
|
available_from=row.available_from,
|
||||||
|
valid_until=row.valid_until,
|
||||||
|
blocking_reason=str(row.blocking_reason or ""),
|
||||||
|
)
|
||||||
|
for row in state_rows
|
||||||
|
}
|
||||||
|
candidates: list[CapabilityCandidate] = []
|
||||||
|
for row in rows:
|
||||||
|
account_id = int(row.account_id)
|
||||||
|
state = states.get(account_id)
|
||||||
|
if state is None:
|
||||||
|
continue
|
||||||
|
candidates.append(
|
||||||
|
CapabilityCandidate(
|
||||||
|
account=CapabilityAccount(
|
||||||
|
id=account_id,
|
||||||
|
username=str(row.account_username or ""),
|
||||||
|
nickname=str(row.account_nickname or ""),
|
||||||
|
uid=str(row.account_uid or ""),
|
||||||
|
points=row.account_points,
|
||||||
|
gold_balance=row.account_gold_balance,
|
||||||
|
points_expire_at=row.account_points_expire_at,
|
||||||
|
role_change_available_at=(
|
||||||
|
row.account_role_change_available_at
|
||||||
|
),
|
||||||
|
elite_opened=row.account_elite_opened,
|
||||||
|
role_bind_status=str(row.account_role_bind_status or ""),
|
||||||
|
ck_check_status=str(row.account_ck_check_status or ""),
|
||||||
|
game_name=str(row.account_game_name or ""),
|
||||||
|
),
|
||||||
|
state=state,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return candidates
|
||||||
|
|
||||||
|
|
||||||
def capability_candidate_accounts(
|
def capability_candidate_accounts(
|
||||||
@@ -330,7 +434,7 @@ def capability_candidate_accounts(
|
|||||||
limit: int = 20,
|
limit: int = 20,
|
||||||
now: datetime | None = None,
|
now: datetime | None = None,
|
||||||
allow_before_open: bool = False,
|
allow_before_open: bool = False,
|
||||||
) -> list[Account]:
|
) -> list[CapabilityAccount]:
|
||||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||||
return [
|
return [
|
||||||
candidate.account
|
candidate.account
|
||||||
@@ -680,6 +784,7 @@ __all__ = [
|
|||||||
"EXCHANGE_COOLDOWN_DAYS",
|
"EXCHANGE_COOLDOWN_DAYS",
|
||||||
"VALIDATION_LEASE_MINUTES",
|
"VALIDATION_LEASE_MINUTES",
|
||||||
"AccountCandidate",
|
"AccountCandidate",
|
||||||
|
"CapabilityAccount",
|
||||||
"acquire_account_lease",
|
"acquire_account_lease",
|
||||||
"active_lease_for_account",
|
"active_lease_for_account",
|
||||||
"capability_candidate_accounts",
|
"capability_candidate_accounts",
|
||||||
|
|||||||
Reference in New Issue
Block a user