unis-oa/backend/scripts/check_readcache.py

872 lines
44 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""守 ``app/readcache`` 这一层:它省的是数据库来回,赌的是作废规则没漏。
漏一次作废就是让人读到旧的组织架构——比慢严重得多,所以这个脚本一次钉两件事:
1. **缓存自己**:同键只问一次库;写对表作废、写别表不作废;别的 Session 写的一样作废;
写过之后回滚就清空;认不出目标的写(DDL、存储过程、多表写、``WITH ... UPDATE``)
一律全场作废;读语句与只读请求收尾那条 ROLLBACK 牵连不到任何人。
2. **接进来的每一个调用点**:挪部门、改角色成员、停用、改名、改表单、改通知渠道、
新建/删除人之后,同一个 Session 里再问一遍必须马上是新答案。
库里不留东西:所有动作都在事务里跑完就回滚::
PYTHONPATH=. python3 scripts/check_readcache.py
"""
from __future__ import annotations
import sys
from types import SimpleNamespace
from sqlalchemy import event, select, text
from sqlalchemy.orm import Session, joinedload
from app import models, readcache
from app.db import SessionLocal
from app.db import engine as db_engine
from app.engine import engine as engine_module
from app.service import autofill, channels, events, org, personref, titles
FAILS: list[str] = []
PASSED = 0
#: 数库上的来回用:证明"同一批账号在一次请求里问第二遍不再排队"
_STATEMENTS = {"n": 0}
@event.listens_for(db_engine, "before_cursor_execute")
def _count_statements(conn, cursor, statement, parameters, context, executemany) -> None: # noqa: ANN001
_STATEMENTS["n"] += 1
def asked(call) -> int:
"""跑一次给定的调用,返回它问了几条库;0 = 全在只读缓存里。"""
before = _STATEMENTS["n"]
call()
return _STATEMENTS["n"] - before
def check(label: str, ok: bool, detail: str = "") -> None:
"""报一条结果;``ok`` 为假就记进失败清单,最后决定退出码。"""
global PASSED
if ok:
PASSED += 1
print(f"[通过] {label}")
else:
FAILS.append(label)
print(f"[FAIL] {label}{f' -> {detail}' if detail else ''}")
class Counter:
"""数 loader 被叫了几次。"""
def __init__(self) -> None:
self.calls = 0
def loader(self, value):
def run():
self.calls += 1
return value
return run
def fire(statement: str, session: Session | None = None) -> None:
"""让 ``app/db`` 里那条作废逻辑只看这一条语句,看它认成哪张表。"""
from app import db as app_db
app_db._invalidate_for_statement(statement, session)
# ---------------------------------------------------------------- 缓存机制
def cache_rules(db: Session, other: Session) -> None:
counter = Counter()
first = readcache.get(db, ("users",), ("check", "one"), counter.loader(["x"]))
again = readcache.get(db, ("users",), ("check", "one"), counter.loader(["y"]))
check("同一个键在一次请求里只问一次库",
counter.calls == 1 and first == ["x"] and again == ["x"], f"calls={counter.calls}")
readcache.get(db, ("users",), ("check", "one"), counter.loader(None)).append("改了")
check("调用方改了返回的 list 不污染缓存",
readcache.get(db, ("users",), ("check", "one"), counter.loader(None)) == ["x"])
pair = Counter()
readcache.get(db, ("users",), ("甲", "同值"), pair.loader("A"))
readcache.get(db, ("users",), ("乙", "同值"), pair.loader("B"))
check("键自带调用点标签,两个调用点不共用一份", pair.calls == 2, f"calls={pair.calls}")
naked = Counter()
readcache.get(db, (), ("check", "没声明表"), naked.loader(1))
readcache.get(db, (), ("check", "没声明表"), naked.loader(1))
check("不声明依赖表就一次都不缓存", naked.calls == 2, f"calls={naked.calls}")
empty = Counter()
for key, value in (("none", None), ("empty", [])):
readcache.get(db, ("users",), ("check", key), empty.loader(value))
readcache.get(db, ("users",), ("check", key), empty.loader(value))
check("空结果(None / 空表)同样复用", empty.calls == 2, f"calls={empty.calls}")
readcache.note_write(db, "oa_unit")
readcache.get(db, ("users",), ("check", "one"), counter.loader(None))
check("写无关的那张表不影响命中", counter.calls == 1, f"calls={counter.calls}")
uid = db.execute(select(models.User.id).limit(1)).scalar_one()
db.execute(text("update users set status = status where id = :i"), {"i": uid})
readcache.get(db, ("users",), ("check", "one"), counter.loader(None))
check("裸 SQL 写了 users 之后作废", counter.calls == 2, f"calls={counter.calls}")
flushed = Counter()
person = db.execute(select(models.User).where(models.User.id == uid)).scalar_one()
readcache.get(db, ("users",), ("check", "orm"), flushed.loader("v"))
person.email = f"check-readcache-{uid}@probe.local"
db.flush()
readcache.get(db, ("users",), ("check", "orm"), flushed.loader("v"))
check("ORM 刷出去的一行(flush)同样触发作废", flushed.calls == 2, f"calls={flushed.calls}")
db.rollback()
cross = Counter()
readcache.get(db, ("users",), ("check", "cross"), cross.loader("v"))
other.execute(text("update users set status = status where id = :i"), {"i": uid})
other.rollback()
readcache.get(db, ("users",), ("check", "cross"), cross.loader("v"))
check("别的 Session 写了同一张表,本 Session 那份也算过期", cross.calls == 2, f"calls={cross.calls}")
before = readcache.generation()
readcache.invalidate_everywhere()
check("全场作废会把代号推一格", readcache.generation() > before)
uncacheable = []
for statement in (
"call some_procedure()",
"truncate table oa_work",
"alter table oa_work add column x int",
"create table t_probe (id int)",
"drop table if exists t_probe",
"load data infile 'x' into table oa_work",
"update oa_task a, oa_work b set a.x = 1 where b.id = a.work",
"delete from oa_task a using oa_work b where b.id = a.work",
"with c as (select 1) update oa_work set title = 'x' where id = 'y' ",
):
mark = readcache.generation()
fire(statement, session=db)
if readcache.generation() <= mark:
uncacheable.append(statement)
check("认不出目标的写一律全场作废(DDL / 存储过程 / 多表写 / CTE 写)", not uncacheable, str(uncacheable))
single, kept = Counter(), Counter()
readcache.get(db, ("users",), ("check", "单表"), single.loader("v"))
readcache.get(db, ("oa_unit",), ("check", "别表"), kept.loader("v"))
for statement in (
"insert into oa_unit (id) values (1)",
"update `oa_unit` set name = 'x' where id = 1",
"delete from OA_UNIT where id = 1",
"replace into oa_unit(id) values (1)",
"update oa_unit u set u.name = 'x' where u.id = 1",
):
fire(statement, session=db)
readcache.get(db, ("users",), ("check", "单表"), single.loader("v"))
readcache.get(db, ("oa_unit",), ("check", "别表"), kept.loader("v"))
check("认得出单表时只作废那张表(大小写、反引号、带别名都认)",
single.calls == 1 and kept.calls == 2, f"users={single.calls} unit={kept.calls}")
reads = Counter()
readcache.get(db, ("users",), ("check", "读"), reads.loader("v"))
for statement in ("select 1", " SELECT count(*) FROM users", "show tables", "explain select 1",
"commit", "set autocommit = 1"):
fire(statement, session=db)
readcache.get(db, ("users",), ("check", "读"), reads.loader("v"))
check("读语句与 COMMIT 一律不动缓存", reads.calls == 1, f"calls={reads.calls}")
clean = Counter()
readcache.get(other, ("users",), ("check", "干净回滚"), clean.loader("v"))
other.rollback()
readcache.get(other, ("users",), ("check", "干净回滚"), clean.loader("v"))
check("没写过的 Session 回滚不动任何人的缓存", clean.calls == 1, f"calls={clean.calls}")
rolled = Counter()
db.rollback()
readcache.get(db, ("users",), ("check", "回滚"), rolled.loader("v"))
db.execute(text("update users set status = status where id = :i"), {"i": uid})
readcache.get(db, ("users",), ("check", "回滚"), rolled.loader("v"))
db.rollback()
readcache.get(db, ("users",), ("check", "回滚"), rolled.loader("v"))
check("写过之后回滚:本 Session 那份清空(未提交值读进过缓存)", rolled.calls == 3, f"calls={rolled.calls}")
nested = Counter()
readcache.get(db, ("users",), ("check", "嵌套"), nested.loader("v"))
savepoint = db.begin_nested()
db.execute(text("update users set status = status where id = :i"), {"i": uid})
readcache.get(db, ("users",), ("check", "嵌套"), nested.loader("v"))
savepoint.rollback()
readcache.get(db, ("users",), ("check", "嵌套"), nested.loader("v"))
check("SAVEPOINT 回滚也一样清空", nested.calls == 3, f"calls={nested.calls}")
row = db.execute(select(models.User).where(models.User.id == uid)).scalar_one()
check("ORM 对象按原样返回(拷壳会坏掉身份映射)",
readcache.get(db, ("users",), ("check", "对象"), lambda: row) is row)
# ---------------------------------------------------------------- 调用点
def call_sites(db: Session) -> None:
"""每个接进来的调用点:改了数据,同一 Session 里立刻得看见新答案。"""
unit_a = db.execute(select(models.Unit).order_by(models.Unit.id)).scalars().first()
unit_b = db.execute(select(models.Unit).order_by(models.Unit.id.desc())).scalars().first()
person = db.execute(
select(models.User).where(models.User.status == models.UserStatus.ACTIVE).order_by(models.User.id)
).scalars().first()
if person is None or unit_a is None:
check("库里得有可用的人和部门", False, "找不到素材")
return
account = person.account
check("人员引用:认账号也认主键,认不出就不是人",
personref.account_of(db, account) == account and personref.account_of(db, "查无此人") == "")
probe_account = "zz.check.readcache"
db.query(models.User).filter(models.User.account == probe_account).delete(synchronize_session=False)
db.flush()
check("先确认库里没有这个账号(下一步才看得出多了人)",
personref.resolve(db, probe_account) is None)
ghost = models.User(account=probe_account, display_name="校验探针", password_hash="x",
organization_id=unit_a.id, role=models.ROLE_MEMBER, status=models.UserStatus.ACTIVE)
db.add(ghost)
db.flush()
check("新建的人:resolve / account_of / 通用解析立刻认得",
personref.account_of(db, probe_account) == probe_account
and (personref.resolve(db, probe_account) or models.User()).account == probe_account
and org._expand(db, probe_account) == [probe_account])
check("新建的人:批量换算也立刻认得",
personref.map_accounts(db, [probe_account, "查无此人"]) == {probe_account: probe_account})
db.delete(ghost)
db.flush()
check("删掉的人:立刻又认不出",
personref.account_of(db, probe_account) == "" and org._expand(db, probe_account) == [])
old_name = person.display_name
check("素材:按姓名解析得到这个人", org._expand(db, old_name) == [account], str(org._expand(db, old_name)))
person.display_name = f"{old_name}·校验探针"
db.flush()
check("改名:通用解析按新名字认得到、老名字认不到",
org._expand(db, person.display_name) == [account] and org._expand(db, old_name) == [],
f"新={org._expand(db, person.display_name)} 老={org._expand(db, old_name)}")
check("改名:人员引用里的姓名跟着走", org.ref_for(db, account).name == person.display_name)
person.display_name = old_name
db.flush()
check("改名还原:引用里的姓名也跟着还原", org.ref_for(db, account).name == person.display_name)
person.status = models.UserStatus.DISABLED
db.flush()
check("停用:审批人解析当场不再算他,但认人这条路照旧认(口径不许变)",
org._expand(db, account) == [] and personref.account_of(db, account) == account)
check("停用:「在职人员」筛选当场把他挡掉(这一份缓存按账号存)",
org.live_refs(db, [account]) == [] and org.live_refs(db, [account, "查无此人"]) == [])
check("停用:共用那份账号读当场改口(状态跟着库走)",
personref.account_status(db, [account]).get(account) == models.UserStatus.DISABLED)
person.status = models.UserStatus.ACTIVE
db.flush()
check("恢复在职:筛选当场又放行", org.live_refs(db, [account]) == [account],
str(org.live_refs(db, [account])))
check("恢复在职:共用那份账号读也当场跟着改回来",
personref.account_status(db, [account]).get(account) == models.UserStatus.ACTIVE)
check("库里没有的账号不进那份读,认到的那个带着状态",
personref.account_status(db, [account, "zz.check.没这个账号"]) == {account: models.UserStatus.ACTIVE},
str(personref.account_status(db, [account, "zz.check.没这个账号"])))
check("同一批账号在一次请求里问第二遍不再排队(按单个账号存的)",
asked(lambda: personref.account_status(db, [account])) == 0,
"第二次仍然产生了数据库往返")
if unit_b is not None and unit_b.id != unit_a.id:
home_id = person.organization_id
home = db.get(models.Unit, home_id) if home_id else None
person.organization_id = unit_b.id
db.flush()
moved = org.unit_by_identity(db, account)
check("挪部门:部门成员清单与「这个人在哪个部门」当场跟着变",
(moved or models.Unit()).id == unit_b.id
and account in org.expand_as(db, "unit", unit_b.path, contains_sub=False),
str(moved))
person.organization_id = home_id
db.flush()
back = org.unit_by_identity(db, account)
check("挪回原部门:口径立刻复原",
home is None or ((back or models.Unit()).id == home.id
and account in org.expand_as(db, "unit", home.path, contains_sub=False)),
str(back))
role = db.execute(select(models.Role).order_by(models.Role.id)).scalars().first()
if role is not None:
original_auth = list(role.list_auth or [])
role.list_auth = [account]
db.flush()
check("岗位角色换了成员:按角色解析审批人当场跟着变",
org.expand_as(db, "role", role.name) == [account]
and account in org._expand(db, role.name))
role.list_auth = []
db.flush()
check("岗位角色清空:当场一个人都派不出", org.expand_as(db, "role", role.name) == [])
role.list_auth = original_auth
db.flush()
# 批量预取这一路单独钉一遍:一次 IN 填进来的那一堆条目里,只被预取过、
# 从没单独解析过的那一条也要跟着写操作立刻作废,不然环节会按旧名单派活
other_role = db.execute(select(models.Role).order_by(models.Role.id.desc())).scalars().first()
if other_role is not None:
ghost = "校验探针-没有这个岗位角色"
org.prefetch_roles(db, [other_role.name, ghost])
members_before = [str(item) for item in (other_role.list_auth or [])]
check("批量预取:不存在的角色仍然如实是「没有这个岗位角色」",
org.role_members(db, ghost) is None
and org.resolve_as(db, "role", ghost) == ([], False))
other_role.list_auth = [account]
db.flush()
check("批量预取过的角色改了成员:当场就是新名单(不许吃预取那一份旧的)",
org.role_members(db, other_role.name) == [account]
and org.expand_as(db, "role", other_role.name) == [account],
str(org.role_members(db, other_role.name)))
other_role.list_auth = members_before
db.flush()
check("还原:预取过的那个角色也回到原名单",
org.role_members(db, other_role.name) == members_before,
str(org.role_members(db, other_role.name)))
# 按流程并趟取角色:只有当前这一环真的配了角色审批人才值得多问那一条 IN
stub = SimpleNamespace(process_id="check-log-timeline-process",
activities={"A": SimpleNamespace(config={
"taskValues": [{"type": "role", "data": ["某个角色"]}]})})
org.prefetch_roles_of(db, stub, {"taskValues": [{"type": "unit", "data": ["某个部门"]}]})
check("这一环没配角色:不预取(发起那一趟常常一个角色都不解析)",
"check-log-timeline-process" not in db.info.get(org._ROLE_SEED_KEY, set()))
org.prefetch_roles_of(db, stub, {"taskValues": [{"type": "role", "data": ["某个角色"]}]})
check("这一环配了角色:一张流程只并一次预取",
"check-log-timeline-process" in db.info.get(org._ROLE_SEED_KEY, set()))
org.prefetch_roles_of(db, stub, {"taskValues": [{"type": "role", "data": ["某个角色"]}]})
check("同一个 Session 里同一张流程不重复预取",
list(db.info.get(org._ROLE_SEED_KEY, set())).count("check-log-timeline-process") == 1)
# 引用里那份 DN 是从人员档案现算的:档案一改,缓存必须当场让路
name_before = person.display_name
dn_before = org.ref_for(db, account).distinguished_name
person.display_name = f"{name_before}·探针"
db.flush()
check("改了姓名:引用里那份 DN 当场跟着换",
org.ref_for(db, account).distinguished_name != dn_before)
person.display_name = name_before
db.flush()
check("还原姓名:DN 也复原", org.ref_for(db, account).distinguished_name == dn_before)
# 一人多身份下线之后,引用里的部门只剩 ``oa_unit`` 这一处真相:部门一改名,
# 缓存里那份引用(含 DN)必须当场让路,不然单据与环节会按旧部门名显示。
# 改的是部门行本身(与 ``PATCH /org/units/{id}`` 同一路径),不碰人员外键:
# 直接挪外键会让 Session 身份映射里那份 ``unit`` 变陈,测出来的就不是缓存了。
unit_person = person if person.organization_id else db.execute(
select(models.User)
.where(models.User.status == models.UserStatus.ACTIVE,
models.User.organization_id.is_not(None))
.order_by(models.User.id)
).scalars().first()
if unit_person is not None:
home_unit = db.get(models.Unit, unit_person.organization_id)
unit_name_before, unit_path_before = home_unit.name, home_unit.path
dn_before = org.ref_for(db, unit_person.account).distinguished_name
home_unit.name = f"{unit_name_before}·探针"
db.flush()
check("部门改名:引用里那份部门名当场跟着换(引用的依赖表含 oa_unit)",
org.ref_for(db, unit_person.account).unit_name == f"{unit_name_before}·探针")
# 接口改完名还会重算整棵子树的 path,DN 里那段部门路径就来自这一列
home_unit.path = f"{unit_path_before or unit_name_before}·探针"
db.flush()
check("部门改名连带重算 path:DN 里那段部门路径当场跟着换",
org.ref_for(db, unit_person.account).distinguished_name != dn_before)
home_unit.name, home_unit.path = unit_name_before, unit_path_before
db.flush()
check("还原部门名与 path:引用与 DN 一并复原",
org.ref_for(db, unit_person.account).distinguished_name == dn_before)
form = db.execute(select(models.FormElement).order_by(models.FormElement.id)).scalars().first()
if form is not None:
content = dict(form.content or {})
fields = list(content.get("fields") or [])
baseline = len(autofill.form_fields(db, form.id))
check("标题与自动带出读到同一张表单", len(titles._form_content(db, form.id).get("fields") or []) == baseline)
content["fields"] = fields + [{"type": "auto", "key": "checkReadcacheKey", "auto": "today"}]
form.content = content
db.flush()
got = autofill.form_fields(db, form.id)
check("改了表单元素:自动带出与标题当场看到新字段",
len(got) == baseline + 1
and any(str(item.get("key")) == "checkReadcacheKey" for item in got)
and len(titles._form_content(db, form.id).get("fields") or []) == baseline + 1,
f"{baseline} -> {len(got)}")
content["fields"] = fields
form.content = content
db.flush()
check("表单还原:字段数复原", len(autofill.form_fields(db, form.id)) == baseline)
# 只挑**启用的**那条:channels_for 本来就只回答启用的渠道,开发库里只剩一条
# 停用的 hub 渠道时,拿它当现成素材问出来的必然是空,于是自己临时补一条。
channel = db.execute(
select(models.NotifyChannel).where(models.NotifyChannel.enabled.is_(True))
.order_by(models.NotifyChannel.code)
).scalars().first()
probe_channel = channel is None
if probe_channel:
channel = models.NotifyChannel(
code="check-readcache-channel", name="校验探针渠道", kind=models.NotifyChannel.KIND_WEBHOOK,
enabled=True, events=["task"], config={"url": "http://127.0.0.1:1"},
)
db.add(channel)
db.flush()
event_name = str((channel.events or ["task"])[0])
check("通知渠道:临时配的渠道当场就生效", channel.code in [row.code for row in channels.channels_for(db, event_name)])
channel.enabled = False
db.flush()
check("通知渠道停用:当场不再发", channel.code not in [row.code for row in channels.channels_for(db, event_name)])
channel.enabled = True
db.flush()
if probe_channel:
db.delete(channel)
db.flush()
check("通知渠道删掉:当场消失", channel.code not in [row.code for row in channels.channels_for(db, event_name)])
check("webhook 订阅者清单也走同一套作废",
isinstance(events.enabled_webhooks(db), list) and isinstance(events.subscriptions(db, "task"), list))
admin_before = engine_module._fallback_admin_ref(db)
admin = db.execute(
select(models.User).where(models.User.role == models.ROLE_ADMIN,
models.User.status == models.UserStatus.ACTIVE)
.order_by(models.User.id)
).scalars().first()
if admin is not None:
admin.role = models.ROLE_MEMBER
db.flush()
check("「空审批人交给管理员」:改了管理员名单当场跟着变",
engine_module._fallback_admin_ref(db) != admin_before or admin_before == "")
admin.role = models.ROLE_ADMIN
db.flush()
check("管理员名单还原:兜底对象也复原", engine_module._fallback_admin_ref(db) == admin_before)
def completed_rows(db: Session) -> None:
"""引擎里那份已办清单(``_completed_rows``):读一次够用,写过当场重来。
一次审批里「上一环节处理人」与抄送上下文合起来要问同一张单的同一批已办五趟,
这一层把五趟并成一趟。省的前提是:本次请求自己办结写过这张表之后,那份旧清单
必须立刻作废,否则「上一环节处理人」会算成办结之前的人。
"""
src = db.execute(
select(models.TaskCompleted).where(models.TaskCompleted.completed.is_(True))
.order_by(models.TaskCompleted.id)
).scalars().first()
if src is None:
check("素材:库里得有已办记录(已办清单这一格的素材)", False, "oa_task_completed 是空的")
return
probe = "zz.check.readcache"
hits: list[str] = []
def spy(conn, cur, statement, params, context, executemany): # noqa: ANN001
if "oa_task_completed" in statement:
hits.append(" ".join(statement.split())[:70])
event.listen(db_engine, "before_cursor_execute", spy)
try:
readcache.ENABLED = False
fresh = [row.id for row in engine_module._completed_rows(db, src.work)]
readcache.ENABLED = True
check("已办清单:缓存给的那份与现查一字不差(行数、顺序都一样)",
[row.id for row in engine_module._completed_rows(db, src.work)] == fresh,
f"现查 {len(fresh)} 行")
check("已办清单:素材那张单有已办可挑",
bool(engine_module._recent_identities(db, SimpleNamespace(id=src.work))))
before_first = engine_module._recent_identities(db, SimpleNamespace(id=src.work))[0].identity
readcache.invalidate_everywhere(db) # 上面几行已经把这一份读进缓存了,先清干净再数
del hits[:]
engine_module._completed_rows(db, src.work)
first = len(hits)
engine_module._completed_rows(db, src.work)
engine_module._recent_identities(db, SimpleNamespace(id=src.work))
total = len(hits)
check("已办清单:同一次请求里问四遍只问一次库", first == 1 and total == 1,
f"第一遍 {first} 条,四遍合计 {total} 条")
ghost = models.TaskCompleted(job=src.job, work=src.work, process=src.process,
activity="zz-check", activity_name="校验探针",
identity=probe, completed=True, processing_type="normal",
completed_time=models.localnow())
db.add(ghost)
db.flush()
now_first = engine_module._recent_identities(db, SimpleNamespace(id=src.work))
check("刚办结的那一条:「上一环节处理人」当场算成新的人",
[row.identity for row in now_first][:1] == [probe],
str([row.identity for row in now_first]))
latest = next((row for row in engine_module._completed_rows(db, src.work)
if row.processing_type == "normal"), None)
check("「最后一条正常办结」也从同一份清单里挑得到", latest is not None
and latest.processing_type == "normal")
db.delete(ghost)
db.flush()
back = engine_module._recent_identities(db, SimpleNamespace(id=src.work))
check("删掉那条:「上一环节处理人」又变回原来那个人",
[row.identity for row in back][:1] == [before_first],
str([row.identity for row in back]))
# 口径等价:拿库里每一张单对一遍改前那两句 SQL(原来一次审批要问五趟的就是它们)
differs = 0
work_ids = [row[0] for row in db.execute(select(models.TaskCompleted.work).distinct()).all()]
for work_id in work_ids:
for limit in (1, 2, 3, 5):
limited = db.execute(
select(models.TaskCompleted)
.where(models.TaskCompleted.work == work_id,
models.TaskCompleted.completed.is_(True))
.order_by(models.TaskCompleted.completed_time.desc())
.limit(limit * 10)
).scalars().all()
picked: list[str] = []
for row in limited:
if row.identity not in picked:
picked.append(row.identity)
if len(picked) >= limit:
break
if picked != [row.identity for row in
engine_module._recent_identities(db, SimpleNamespace(id=work_id), limit)]:
differs += 1
newest = db.execute(
select(models.TaskCompleted)
.where(models.TaskCompleted.work == work_id,
models.TaskCompleted.processing_type == "normal")
.order_by(models.TaskCompleted.completed_time.desc())
.limit(1)
).scalar_one_or_none()
derived = next((row for row in engine_module._completed_rows(db, work_id)
if row.processing_type == "normal"), None)
if (newest.id if newest else "") != (derived.id if derived else ""):
differs += 1
check(f"改前改后挑人一致:库里 {len(work_ids)} 张单逐张对(limit 1/2/3/5 + 最后一条正常办结)",
differs == 0, f"{differs} 张单对不上")
finally:
event.remove(db_engine, "before_cursor_execute", spy)
readcache.ENABLED = True
def probe_rules(db: Session, other: Session) -> None:
"""守 ``app/service/org.py`` 里那枚选择器探针。
它替通用兜底口径省掉的是"试错的那几趟":一个五档全不命中的值(配置里还压着的
历史 DN 就是这种),从白问五趟变成合起来问一趟。赌的是**"探针说这一档肯定不中"
与"这一档真的解析不出东西"永远一致**。这里一次钉四件事:
1. 开与关对同一串值答案一字不差(口径不许因为探针而变);
2. 不分大小写的账号列上探针也得跟着不分——判定必须留在库里做,
改成 Python 比字符串就会把该试的档跳掉;
3. 一批值里每个值各拿各的答案(预探不是"这批里有一个就算大家都可能");
4. 写掉组织那四张表任何一张,探过的答案当场作废。
"""
person = db.execute(
select(models.User).where(models.User.status == models.UserStatus.ACTIVE)
.order_by(models.User.id)
).scalars().first()
unit = db.execute(select(models.Unit).order_by(models.Unit.id)).scalars().first()
role = db.execute(select(models.Role).order_by(models.Role.id)).scalars().first()
group = db.execute(select(models.Group).order_by(models.Group.id)).scalars().first()
if person is None or unit is None:
check("库里得有可用的人与部门(探针验收的素材)", False, "找不到素材")
return
account = person.account
values = [
account, account.upper(), person.display_name, person.display_name.upper(),
str(person.id), unit.name, unit.path, (unit.path or "").rstrip(chr(92)),
"zz.查无此人", "部门\\姓名\\身份名\\旧DN", "", " ", "0", "-1",
]
if role is not None:
values.append(role.name)
if group is not None:
values.append(group.name)
def both(value: str) -> tuple[list[str], list[str]]:
"""同一个值:开着探针一份、关掉探针一份(各用一个 Session,免得读缓存盖住差异)。"""
org.PROBE_ENABLED = True
quick = org._expand(db, value)
org.PROBE_ENABLED = False
full = org._expand(other, value)
org.PROBE_ENABLED = True
return quick, full
diffs = []
for value in values:
quick, full = both(value)
if quick != full:
diffs.append(f"{value!r} 开={quick} 关={full}")
check("探针开与关:同一串值解析结果一字不差",
not diffs, ";".join(diffs[:4]))
if account.upper() != account:
quick, full = both(account.upper())
check("账号列不分大小写时探针也跟着不分(判定留在库里做)",
quick == full, f"开={quick} 关={full}")
person_kinds = org._probe_kinds(db, account, allow_query=True) or frozenset()
check("探针答案按值存:真账号至少认得到人与人员两档,查无此值一档都不认",
{"identity", "person"} <= person_kinds
and org._probe_kinds(db, "zz.查无此人", allow_query=True) == frozenset(),
f"{account!r} -> {sorted(person_kinds)}")
org.probe_selectors(db, [account, "zz.查无此人"])
quick_one = org._probe_kinds(db, account, allow_query=False)
check("一批值合起来只问一趟,答案仍按值各归各",
quick_one is not None and "identity" in quick_one
and org._probe_kinds(db, "zz.查无此人", allow_query=False) == frozenset())
# 作废:探过"没有这个人",紧接着把人建出来,下一句就得认
probe_account = "zz.check.probe"
db.query(models.User).filter(models.User.account == probe_account).delete(synchronize_session=False)
db.flush()
org.PROBE_ENABLED = True
org.probe_selectors(db, [probe_account, "zz.另一个查无"])
check("素材:预探确认库里没有这个账号",
org._expand(db, probe_account) == [])
ghost = models.User(account=probe_account, display_name="探针素材", password_hash="x",
organization_id=unit.id, role=models.ROLE_MEMBER,
status=models.UserStatus.ACTIVE)
db.add(ghost)
db.flush()
check("新建的人:预探过的答案当场作废,批量解析立刻认得",
org.resolve_identity_names(db, [probe_account, "zz.另一个查无"]) == [probe_account],
str(org.resolve_identity_names(db, [probe_account])))
db.delete(ghost)
db.flush()
org.probe_selectors(db, [probe_account])
check("删掉的人:重新预探之后又认不出",
org._expand(db, probe_account) == [])
if role is not None:
original = list(role.list_auth or [])
# 先在角色上挂一个还不存在的成员,探过"这个角色派不出人",再把人建出来:
# 这一趟考的是"角色成员那一串预探过的答案,会不会因为建了人而作废"
role.list_auth = [probe_account]
db.flush()
org.probe_selectors(db, [role.name, probe_account])
check("角色挂着一个还不存在的成员:当场派不出人(探针也认不出)",
org.expand_as(db, "role", role.name) == [] and org._expand(db, role.name) == [],
str(org.expand_as(db, "role", role.name)))
ghost = models.User(account=probe_account, display_name="探针素材", password_hash="x",
organization_id=unit.id, role=models.ROLE_MEMBER,
status=models.UserStatus.ACTIVE)
db.add(ghost)
db.flush()
check("角色那个成员建出来了:按角色解析当场就是他,不必等下一次请求",
org.expand_as(db, "role", role.name) == [probe_account]
and org._expand(db, role.name) == [probe_account],
str(org.expand_as(db, "role", role.name)))
db.delete(ghost)
db.flush()
check("成员又删掉:这个角色又派不出人了",
org.expand_as(db, "role", role.name) == [])
role.list_auth = [account]
db.flush()
check("岗位角色改成在职的人:解析与通用口径当场一致",
org.expand_as(db, "role", role.name) == [account]
and account in org._expand(db, role.name),
str(org.expand_as(db, "role", role.name)))
role.list_auth = original
db.flush()
check("岗位角色还原:成员清单也复原",
org.expand_as(db, "role", role.name) == [str(x) for x in org.resolve_identity_names(other, original)])
# ------------------------------------------------------ 认人共用的那一份人员行
def person_rows(db: Session) -> None:
"""``personref.rows_for``:认人那五处共用一份人员行,同一个人一次请求只问一趟。
以前 ``account_of`` / ``resolve`` / ``map_accounts`` / ``account_status`` 与
``org.refs_for`` 各认各的,量的时候一次审批光 ``users`` 就排三四趟队(本部署一趟
9~19ms)。并成一份之后这里钉住四件事:第二遍零往返、认不出也记一笔、登录那一趟
认过的人不必再问、写了 ``users`` 立刻重来。
"""
person = db.execute(
select(models.User).where(models.User.status == models.UserStatus.ACTIVE)
.order_by(models.User.id)
).scalars().first()
if person is None:
check("库里得有可用的人", False, "找不到素材")
return
account = str(person.account)
def in_fresh(call):
"""新开一个 Session(= 新一次请求)跑一段代码,数它问了几条库。"""
session = SessionLocal()
try:
return asked(lambda: call(session))
finally:
session.rollback()
session.close()
def five_sites(session: Session) -> bool:
personref.account_of(session, account)
personref.resolve(session, account)
personref.map_accounts(session, [account])
personref.account_status(session, [account])
org.refs_for(session, [account])
return True
check("冷缓存:一个账号一趟问完",
in_fresh(lambda session: personref.rows_for(session, [account])) == 1)
sites = in_fresh(five_sites)
check("认人的五个调用点合起来只问一趟", sites == 1, f"问了 {sites} 趟")
check("一批人一次问完:三个人也只一趟",
in_fresh(lambda session: personref.rows_for(
session, [account, account, str(person.id)])) == 1)
ghost = "zz.check.personrows"
session = SessionLocal()
try:
first = asked(lambda: personref.resolve(session, ghost))
second = asked(lambda: personref.account_of(session, ghost))
check("认不出也记一笔:换个调用点再问就不去库里绕",
first == 1 and second == 0, f"{first} / {second}")
check("认不出的人不进结果", personref.rows_for(session, [ghost]) == {})
finally:
session.rollback()
session.close()
session = SessionLocal()
try:
# 与 ``deps.current_user`` 同一形状:那一趟本来就连着部门与权限角色一起问
row = session.get(models.User, int(person.id), options=[joinedload(models.User.unit)])
personref.note_row(session, row)
later = asked(lambda: (personref.resolve(session, account),
org.refs_for(session, [account])))
check("登录那一趟认过的人报给共用装载器:后面各处零往返", later == 0, f"问了 {later} 趟")
finally:
session.rollback()
session.close()
session = SessionLocal()
try:
personref.rows_for(session, [account])
# 不真去 UPDATE:前面那几段在同一个事务里改过人,会撞行锁。这里要钉的是
# "连接层认得出这是写 users 的语句",交给 fire() 只看这一条语句。
fire("update users set status = status where id = 1", session)
again = asked(lambda: personref.account_status(session, [account]))
check("写了 users:共用那一份当场作废,下一回重新问库", again == 1, f"问了 {again} 趟")
finally:
session.rollback()
session.close()
keyed = str(person.id)
clash = db.execute(select(models.User).where(models.User.account == keyed)).first() is not None
if not clash: # 有人账号就叫这个数字时这一项没有意义,跳过
check("状态只按账号问:值写成 users.id 不回状态,但 account_of 照认(历史列)",
personref.account_status(db, [keyed]) == {}
and personref.account_of(db, keyed) == account)
def task_rows(db: Session) -> None:
"""引擎里那份待办名单(``_task_rows``):两问共用一份,写过当场重来。
「本环节有哪些人要办」(写 ``work.manual_identity_list``)与「这些人里谁手上还有活」
(抄送去重)问的是同一批行,一次审批里落在同一个库状态上。省的前提是:本请求自己
新增/删掉待办之后,那份名单必须立刻作废——不然抄送去重会按旧名单挡人或者漏挡人。
"""
src = db.execute(
select(models.Task).where(models.Task.status == "pending").order_by(models.Task.id)
).scalars().first()
if src is None:
check("素材:库里得有在途待办(待办名单这一格的素材)", False, "oa_task 是空的")
return
probe = "zz.check.readcache"
hits: list[str] = []
def spy(conn, cur, statement, params, context, executemany): # noqa: ANN001
if "FROM oa_task" in statement and "oa_task.work" in statement:
hits.append(" ".join(statement.split())[:70])
event.listen(db_engine, "before_cursor_execute", spy)
try:
readcache.ENABLED = False
fresh = engine_module._task_rows(db, src.work)
readcache.ENABLED = True
check("待办名单:缓存给的那份与现查一字不差(账号、姓名、状态、环节都对上)",
engine_module._task_rows(db, src.work) == fresh, f"现查 {len(fresh)} 行")
readcache.invalidate_everywhere(db) # 上面已经把这一份读进缓存了,先清干净再数
del hits[:]
engine_module._task_rows(db, src.work)
first = len(hits)
engine_module._task_rows(db, src.work)
engine_module._busy_identities(db, SimpleNamespace(id=src.work), src.activity)
engine_module._busy_identities(db, SimpleNamespace(id=src.work))
total = len(hits)
check("待办名单:名单 + 两遍抄送去重一共只问一次库(这一格省的就是这条)",
first == 1 and total == 1, f"第一遍 {first} 条,四遍合计 {total} 条")
before = engine_module._busy_identities(db, SimpleNamespace(id=src.work), src.activity)
ghost = models.Task(job=src.job, work=src.work, process=src.process,
activity=src.activity, activity_name="校验探针",
identity=probe, identity_name="校验探针", status="pending")
db.add(ghost)
db.flush()
check("新增一条待办:抄送去重当场把它算成「手上还有活」",
probe in engine_module._busy_identities(db, SimpleNamespace(id=src.work), src.activity),
str(sorted(engine_module._busy_identities(db, SimpleNamespace(id=src.work)))))
check("新增一条待办:本环节名单也当场多这个人",
probe in [row[0] for row in engine_module._task_rows(db, src.work)])
db.delete(ghost)
db.flush()
check("删掉那条:名单与去重又回到原来那批人",
engine_module._busy_identities(db, SimpleNamespace(id=src.work), src.activity) == before,
str(sorted(engine_module._busy_identities(db, SimpleNamespace(id=src.work), src.activity))))
# 口径等价:拿库里每一张单对一遍改前那两句 SQL(改前一次审批要各问一遍的就是它们)
differs = 0
work_ids = [row[0] for row in db.execute(select(models.Task.work).distinct()).all()]
activities = {row[0]: row[1] for row in db.execute(
select(models.Task.work, models.Task.activity).distinct()).all()}
for work_id in work_ids:
listed = db.execute(
select(models.Task.identity, models.Task.identity_name)
.where(models.Task.work == work_id)
).all()
if [str(row[0]) for row in listed] != [row[0] for row in
engine_module._task_rows(db, work_id)]:
differs += 1
for activity_id in {str(activities.get(work_id) or ""), ""}:
stmt = select(models.Task.identity).where(
models.Task.work == work_id, models.Task.status == "pending")
if activity_id:
stmt = stmt.where(models.Task.activity == activity_id)
direct = set(db.execute(stmt).scalars().all())
if direct != engine_module._busy_identities(
db, SimpleNamespace(id=work_id), activity_id):
differs += 1
check(f"改前改后逐张单对得上:库里 {len(work_ids)} 张单 × (本环节 / 全环节) 两种问法",
differs == 0, f"{differs} 处对不上")
finally:
event.remove(db_engine, "before_cursor_execute", spy)
readcache.ENABLED = True
def main(argv: list[str]) -> int:
db, other = SessionLocal(), SessionLocal()
try:
cache_rules(db, other)
call_sites(db)
completed_rows(db)
task_rows(db)
probe_rules(db, other)
person_rows(db)
finally:
db.rollback()
other.rollback()
db.close()
other.close()
if FAILS:
print(f"共 {len(FAILS)} 项不通过:{';'.join(FAILS)}")
return 1
print(f"通过:{PASSED} 项(缓存作废规则与每个调用点的即时性)")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))