Skip to content

Commit ec47a27

Browse files
committed
fix(llm): 收紧贴纸视觉投递领取查询
1 parent 8e8e9ea commit ec47a27

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

pallas/core/foundation/db/repository_pg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class BackgroundJobRow(Base):
170170
__table_args__ = (
171171
UniqueConstraint("idempotency_key", name="uq_background_job_idempotency"),
172172
Index("ix_background_job_claim", "status", "available_at", "leased_until", "id"),
173+
Index("ix_background_job_delivery_claim", "kind", "status", "finished_at"),
173174
)
174175

175176
id: Mapped[str] = mapped_column(Text, primary_key=True)
@@ -735,6 +736,18 @@ def _ensure_pg_context_answer_message_reply_index(connection) -> None:
735736
)
736737

737738

739+
def _ensure_pg_background_job_delivery_claim_index(connection) -> None:
740+
"""background_job 表补已完成视觉投递领取索引。"""
741+
insp = inspect(connection)
742+
if not insp.has_table("background_job"):
743+
return
744+
connection.execute(
745+
text(
746+
"CREATE INDEX IF NOT EXISTS ix_background_job_delivery_claim ON background_job (kind, status, finished_at)"
747+
)
748+
)
749+
750+
738751
def _ensure_pg_stat_statements_extension(connection) -> None:
739752
"""启用 pg_stat_statements(仅应在独立 autocommit 连接中调用)。
740753
@@ -840,6 +853,7 @@ async def get_session(*, read_only: bool = False):
840853
("ddl.message_timeline_metadata", _ensure_pg_message_timeline_metadata),
841854
("ddl.context_answer_reply_index", _ensure_pg_context_answer_reply_index),
842855
("ddl.context_answer_message_reply_index", _ensure_pg_context_answer_message_reply_index),
856+
("ddl.background_job_delivery_claim_index", _ensure_pg_background_job_delivery_claim_index),
843857
]
844858

845859

pallas/product/llm/sticker_vision.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,15 +436,22 @@ async def claim_sticker_vision_delivery(bot_ids: set[int]) -> dict[str, object]
436436
from pallas.core.foundation.db.runtime import is_postgresql_backend
437437

438438
if is_postgresql_backend():
439-
from sqlalchemy import select
439+
from sqlalchemy import String, cast, select
440440

441441
from pallas.core.foundation.db.repository_pg import BackgroundJobRow, get_session
442442

443443
async with get_session() as session:
444444
rows = (
445445
await session.execute(
446446
select(BackgroundJobRow)
447-
.where(BackgroundJobRow.kind == "sticker_vision.select", BackgroundJobRow.status == "done")
447+
.where(
448+
BackgroundJobRow.kind == "sticker_vision.select",
449+
BackgroundJobRow.status == "done",
450+
BackgroundJobRow.payload["delivery"]["state"].astext == "pending",
451+
cast(BackgroundJobRow.payload["delivery"]["bot_id"].astext, String).in_([
452+
str(bot_id) for bot_id in bot_ids
453+
]),
454+
)
448455
.order_by(BackgroundJobRow.finished_at)
449456
.limit(32)
450457
.with_for_update(skip_locked=True)

tests/common/test_repository_pg.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,37 @@ def execute(self, statement) -> None:
514514
]
515515

516516

517+
def test_background_job_rows_have_delivery_claim_index():
518+
from pallas.core.foundation.db.repository_pg import BackgroundJobRow
519+
520+
index_names = {idx.name for idx in BackgroundJobRow.__table__.indexes}
521+
522+
assert "ix_background_job_delivery_claim" in index_names
523+
524+
525+
def test_ensure_pg_background_job_delivery_claim_index(monkeypatch):
526+
from pallas.core.foundation.db import repository_pg as mod
527+
528+
executed: list[str] = []
529+
530+
class FakeInspector:
531+
def has_table(self, name: str) -> bool:
532+
return name == "background_job"
533+
534+
class FakeConnection:
535+
def execute(self, statement) -> None:
536+
executed.append(str(statement))
537+
538+
monkeypatch.setattr(mod, "inspect", lambda _connection: FakeInspector())
539+
540+
mod._ensure_pg_background_job_delivery_claim_index(FakeConnection())
541+
542+
assert executed == [
543+
"CREATE INDEX IF NOT EXISTS ix_background_job_delivery_claim "
544+
"ON background_job (kind, status, finished_at)"
545+
]
546+
547+
517548
@pytest.mark.asyncio
518549
async def test_find_by_keywords_for_reply_many_answers_no_in_overflow(pg_engine, monkeypatch):
519550
"""热词大量 Answer 时不得用超大 IN (...),接话 find 应成功且受 reply_answers_cap 限制。"""

0 commit comments

Comments
 (0)