219 lines
8.8 KiB
Python
219 lines
8.8 KiB
Python
#!/usr/bin/env python3
|
||
"""守 :mod:`app.designcache`:省的是往返,口径与"改了设计立刻生效"一条都不许动。
|
||
|
||
设计(``oa_process`` / ``oa_activity`` / ``oa_route``)从每一次审批、每一张单据详情里
|
||
各排三次队,于是整条链进 Redis 共享缓存。这个脚本一次钉六条:
|
||
|
||
1. **命中与现查一字不差**:同一张流程,缓存里那份与回库现查那份,拼出来的
|
||
:class:`ProcessDefinition` 逐字段相等;命中的那一趟**一条 SQL 都不许多问**;
|
||
2. **改环节配置当场生效**:同一个 Session 里改了设计,下一趟 ``load`` 必须读到自己刚写的
|
||
那一份(不能拿命中缓存糊过去);
|
||
3. **没提交的设计不许进缓存**:上面那一步之后,Redis 里必须还是空的——回滚之后
|
||
别人读到的是库里那一版,而不是这版根本没存在过的设计;
|
||
4. **提交之后别的 Session 立刻看得见**:另开一个 Session 提交一次设计写,
|
||
再开一个 Session 现读,看到的必须是新版;
|
||
5. **不相关的写不许牵连**:写一张单据(``oa_work``)之后,那份设计键必须还在;
|
||
6. **整层关掉照跑**:带 ``OA_DESIGN_CACHE=off`` 再起一个进程,同样的流程读出同一份定义。
|
||
|
||
库里不留东西:所有设计改动跑在事务里,最后整体回滚;Redis 里这一层的键脚本结束清干净。::
|
||
|
||
PYTHONPATH=. python3 scripts/check_design_cache.py
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import dataclasses
|
||
import os
|
||
import subprocess
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from sqlalchemy import event, select, text
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app import designcache, models
|
||
from app.db import SessionLocal, engine
|
||
from app.engine import definition
|
||
|
||
FAILS: list[str] = []
|
||
PASSED = 0
|
||
|
||
STATEMENTS: list[str] = []
|
||
|
||
|
||
@event.listens_for(engine, "before_cursor_execute")
|
||
def _spy(conn, cursor, statement, parameters, context, executemany) -> None: # noqa: ANN001
|
||
STATEMENTS.append(statement)
|
||
|
||
|
||
def check(label: str, ok: bool, detail: str = "") -> None:
|
||
global PASSED
|
||
if ok:
|
||
PASSED += 1
|
||
print(f"[通过] {label}")
|
||
else:
|
||
FAILS.append(label)
|
||
print(f"[FAIL] {label}{f' -> {detail}' if detail else ''}")
|
||
|
||
|
||
def same(a: definition.ProcessDefinition, b: definition.ProcessDefinition) -> bool:
|
||
return dataclasses.asdict(a) == dataclasses.asdict(b)
|
||
|
||
|
||
def pick_process(db: Session) -> str | None:
|
||
"""挑一张环节最多的流程:环节多,逐字段比对才有东西可比。"""
|
||
rows = db.execute(select(models.Activity.process).limit(2000)).scalars().all()
|
||
counts: dict[str, int] = {}
|
||
for pid in rows:
|
||
counts[pid] = counts.get(pid, 0) + 1
|
||
return max(counts, key=lambda pid: counts[pid]) if counts else None
|
||
|
||
|
||
def hit_and_equivalence(db: Session, pid: str) -> None:
|
||
designcache.clear()
|
||
fresh = definition.load(db, pid) # 未命中:问库 + 回填
|
||
check("未命中时确实回填了一份", designcache.get(pid) is not None)
|
||
STATEMENTS.clear()
|
||
cached = definition.load(db, pid) # 应命中:一条都不许多问
|
||
other = SessionLocal()
|
||
checked = definition.load(other, pid) # 换个 Session 现查一遍做对照
|
||
check("命中与现查逐字段一模一样", same(fresh, cached) and same(fresh, checked))
|
||
check("命中那一趟一条 SQL 都没多问", not STATEMENTS, f"{len(STATEMENTS)} 条")
|
||
other.close()
|
||
|
||
|
||
def uncommitted_write(db: Session, pid: str) -> None:
|
||
activity = db.execute(
|
||
select(models.Activity).where(models.Activity.process == pid).limit(1)
|
||
).scalars().first()
|
||
if activity is None:
|
||
check("素材:这张流程得有环节", False, "没有环节可改")
|
||
return
|
||
definition.load(db, pid) # 先把这一份放进缓存,下面才看得出写一次就作废
|
||
original = dict(activity.config or {})
|
||
activity.config = {**original, "checkDesignCache": 1}
|
||
db.flush()
|
||
after = definition.load(db, pid)
|
||
check("改了环节配置:同一 Session 下一趟 load 立刻是自己刚写的那一份",
|
||
after.activities[activity.id].config.get("checkDesignCache") == 1,
|
||
str(after.activities[activity.id].config.get("checkDesignCache")))
|
||
check("没提交的设计不许进缓存:Redis 里这一份必须还是空的",
|
||
designcache.get(pid) is None, "回填没有被「刚改过」标记挡住")
|
||
activity.config = original
|
||
db.flush()
|
||
|
||
|
||
def committed_write_elsewhere(pid: str) -> None:
|
||
"""另开 Session 真提交一次设计写,再看新 Session 读到的是不是新版。"""
|
||
writer = SessionLocal()
|
||
row = writer.execute(
|
||
text("SELECT id FROM oa_activity WHERE `process` = :p ORDER BY id LIMIT 1"), {"p": pid}
|
||
).first()
|
||
if row is None:
|
||
writer.close()
|
||
check("素材:这张流程得有环节(提交那一条)", False)
|
||
return
|
||
activity_id = row[0]
|
||
before = definition.load(SessionLocal(), pid)
|
||
before_config = dict(before.activities[activity_id].config)
|
||
writer.execute(
|
||
text("UPDATE oa_activity SET config = :c WHERE id = :i"),
|
||
{"c": '{"checkDesignCacheCommit": 1}', "i": activity_id},
|
||
)
|
||
writer.commit()
|
||
writer.close()
|
||
reader = SessionLocal()
|
||
after = definition.load(reader, pid)
|
||
check("别处提交了一次设计写:下一个 Session 读到的就是新版",
|
||
after.activities[activity_id].config.get("checkDesignCacheCommit") == 1,
|
||
str(after.activities[activity_id].config))
|
||
reader.close()
|
||
restorer = SessionLocal()
|
||
restorer.execute(
|
||
text("UPDATE oa_activity SET config = :c WHERE id = :i"),
|
||
{"c": __import__("json").dumps(before_config, ensure_ascii=False), "i": activity_id},
|
||
)
|
||
restorer.commit()
|
||
restorer.close()
|
||
back = SessionLocal()
|
||
check("还原之后读回来的是原来那份设计",
|
||
same(before, definition.load(back, pid)))
|
||
back.close()
|
||
|
||
|
||
def unrelated_write_does_not_invalidate(pid: str) -> None:
|
||
db = SessionLocal()
|
||
designcache.clear()
|
||
definition.load(db, pid)
|
||
work = db.execute(select(models.Work).limit(1)).scalars().first()
|
||
if work is None:
|
||
check("素材:得有一张单据做「不相关写」的探针", False)
|
||
db.close()
|
||
return
|
||
title_before = work.title
|
||
work.title = f"{title_before}·探针"
|
||
db.flush()
|
||
check("写单据不牵连设计缓存:那一份键还在", designcache.get(pid) is not None)
|
||
work.title = title_before
|
||
db.rollback()
|
||
db.close()
|
||
|
||
|
||
def disabled_layer(pid: str) -> None:
|
||
"""整层关掉:另起进程读同一张流程,结果必须与开着时一字不差。"""
|
||
fresh = definition.load(SessionLocal(), pid)
|
||
code = (
|
||
"import dataclasses, json, sys;"
|
||
"from app.db import SessionLocal;"
|
||
"from app.engine import definition;"
|
||
f"db = SessionLocal();d = definition.load(db, {pid!r});"
|
||
"print(dataclasses.asdict(d))"
|
||
)
|
||
env = {**os.environ, "PYTHONPATH": ".", "OA_AUTO_CREATE_SCHEMA": "false",
|
||
"OA_DESIGN_CACHE": "off", "OA_SCHEDULER": "off"}
|
||
try:
|
||
out = subprocess.run([sys.executable, "-c", code], cwd=str(Path(__file__).resolve().parents[1]),
|
||
env=env, capture_output=True, text=True, timeout=180)
|
||
except Exception as exc: # noqa: BLE001
|
||
check("关掉这一层也能起进程", False, str(exc))
|
||
return
|
||
if out.returncode != 0:
|
||
check("关掉这一层也能起进程", False, (out.stderr or "")[-300:])
|
||
return
|
||
try:
|
||
off = eval(out.stdout.strip()) # noqa: S307 - 读回来的是自己打印的 dataclass dict
|
||
except Exception as exc: # noqa: BLE001
|
||
check("关掉这一层也能起进程", False, str(exc))
|
||
return
|
||
check("``OA_DESIGN_CACHE=off``:与开着时读出同一份定义",
|
||
dataclasses.asdict(fresh) == off)
|
||
|
||
|
||
def main() -> int:
|
||
db = SessionLocal()
|
||
pid = pick_process(db)
|
||
if pid is None:
|
||
print("跳过:库里没有流程可当素材")
|
||
return 0
|
||
print(f"素材:流程 {pid} 共享缓存:{'参与' if designcache.cache.configured() else '没参与'}")
|
||
try:
|
||
hit_and_equivalence(db, pid)
|
||
uncommitted_write(db, pid)
|
||
finally:
|
||
db.rollback()
|
||
db.close()
|
||
if designcache.cache.configured():
|
||
committed_write_elsewhere(pid)
|
||
unrelated_write_does_not_invalidate(pid)
|
||
disabled_layer(pid)
|
||
removed = designcache.clear()
|
||
print(f"清理:这一层清掉 {removed} 个键")
|
||
if FAILS:
|
||
print(f"\n共 {len(FAILS)} 项不通过:" + ";".join(FAILS))
|
||
return 1
|
||
print(f"\n通过:{PASSED} 项(设计缓存的等价性、作废即时性与关掉这一层的样子)")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|