多进程协调(W2-b)第一步:新增 app/cluster——工作进程在库里登记、心跳续租约(属主随进程,进程没了它认领的行一起到期),调度当班的租约,发给属主进程的停止 / 记事件请求,PostgreSQL 的 LISTEN/NOTIFY 叫醒,跨进程的互斥锁(咨询锁),SQLite 单进程守卫(第二个进程拒绝启动);运行、问数据一轮、起草 / 剖析 / 探查 / 评测 / 影子起草、文档、发布单记 owner,启动整理改成只接手属主不在了的行,当班进程隔一会儿再接手一次;运行的并发名额改成库里按交互 / 应用 / 定时 / 系统四类发(各有全站上限和优先级,设置里可调),替掉进程内的信号量;同源队记在库里;运行停止、记事件跨进程交给属主;运行事件、问数据事件跨进程转发(库里按 seq 追,不落库的流式增量和保活心跳只转给在看的进程);每日花费账先占住那一行再读写;访问密钥限流、登录失败计数在 PostgreSQL 时记在库里;登录设置、对外地址、向量模型、能力快照、数据源连接池、应用页面缓存跨进程失效;上传版本库、记录表、按需探查、出具、召回、补算、按需视图、个人刷新、定时发起的锁换成跨进程的锁

This commit is contained in:
ThearchyHelios
2026-10-19 11:40:33 +08:00
parent 9a0d87aa6e
commit 87f0d62f9b
74 changed files with 3521 additions and 447 deletions
+1 -1
View File
@@ -120,7 +120,7 @@ async def test_shadow_drafting_writes_only_the_copy(source, monkeypatch):
assert snap.status in ("queued", "running")
# 不占数据源的队:同一个源照样能马上起草、剖析
await asyncio.sleep(0)
assert job_runtime.lanes.holders(source["id"]) == []
assert await job_runtime.lanes.holders(source["id"]) == []
done = await wait_done(snap.id)
assert done.status == "succeeded", (done.error, done.progress)
# 线上目录一项不动
+10 -9
View File
@@ -211,7 +211,7 @@ async def test_stop_keeps_what_was_read_and_restart_marks_interrupted(client, bi
async def test_introspect_queues_behind_profile_and_blocks_sync_writes(client, small, monkeypatch):
sid, _ = small
held = runtime.lanes.enter(sid, "profile", "someone-else")
held = await runtime.lanes.enter(sid, "profile", "someone-else")
try:
job = (await client.post(f"/api/datasources/{sid}/introspect")).json()["job"]
await asyncio.sleep(0.1)
@@ -220,7 +220,7 @@ async def test_introspect_queues_behind_profile_and_blocks_sync_writes(client, s
r = await client.post(f"/api/datasources/{sid}/introspect")
assert r.status_code == 409 and r.json()["code"] == "introspect_busy"
finally:
held.release()
await held.release()
await introspect_jobs.manager.wait(job["id"], timeout=30)
assert (await client.get(f"/api/datasources/{sid}/introspect-jobs/{job['id']}")).json()["job"]["status"] \
== "succeeded"
@@ -237,7 +237,8 @@ async def test_introspect_queues_behind_profile_and_blocks_sync_writes(client, s
job = (await client.post(f"/api/datasources/{sid}/introspect")).json()["job"]
for _ in range(100):
await asyncio.sleep(0.02)
if runtime.lanes.holders(sid) and runtime.lanes.holders(sid)[0].kind == "introspect":
holders = await runtime.lanes.holders(sid)
if holders and holders[0].kind == "introspect":
break
r = await client.post(f"/api/datasources/{sid}/catalog/draft", json={"tables": ["alpha"]})
assert r.status_code == 409 and r.json()["code"] == "introspect_busy" and "探查" in r.json()["detail"]
@@ -436,14 +437,14 @@ async def test_draft_and_profile_jobs_say_they_wait_for_introspect(small):
try:
for kind, job, waiting in (("profile", profile, catalog_profile_jobs.waiting_for),
("draft", draft, catalog_jobs.waiting_for)):
held = runtime.lanes.enter(sid, "introspect", "an-introspect-job")
mine = runtime.lanes.enter(sid, kind, job.id)
held = await runtime.lanes.enter(sid, "introspect", "an-introspect-job")
mine = await runtime.lanes.enter(sid, kind, job.id)
try:
async with SessionLocal() as session:
assert await waiting(session, job) == "introspect", kind
finally:
held.release()
mine.release()
await held.release()
await mine.release()
finally:
async with SessionLocal() as session:
for model, job_id in ((CatalogProfileJob, profile.id), (CatalogDraftJob, draft.id)):
@@ -644,7 +645,7 @@ async def test_tables_added_while_the_job_is_queued_stay_in_scope(client, small)
got = await so.add_to_scope(sid, ["eta"], trail=so.Trail(via=so.VIA_LOOK, actor="tester"))
assert got.added == ["eta"]
await _scope(client, sid, mode="rules", include=["alpha"], removed=["eta"])
held = runtime.lanes.enter(sid, "profile", "someone-else")
held = await runtime.lanes.enter(sid, "profile", "someone-else")
try:
job = (await client.post(f"/api/datasources/{sid}/introspect")).json()["job"]
assert job["status"] == "queued" and job["total"] == 1
@@ -655,7 +656,7 @@ async def test_tables_added_while_the_job_is_queued_stay_in_scope(client, small)
# 任务还没开始:范围还是上一次的(eta 还在),新加的两张已经进了表结构
assert set((await _cache(sid))["tables"]) == {"alpha", "beta", "gamma", "eta"}
finally:
held.release()
await held.release()
await introspect_jobs.manager.wait(job["id"], timeout=30)
assert (await client.get(f"/api/datasources/{sid}/introspect-jobs/{job['id']}")).json()["job"]["status"] \
== "succeeded"
+8 -8
View File
@@ -32,7 +32,7 @@ def test_an_unregistered_kind_is_refused():
async def go() -> None:
with pytest.raises(ValueError, match="没有登记"):
lanes.enter("src", "nonsense-kind")
await lanes.enter("src", "nonsense-kind")
asyncio.run(go())
@@ -52,16 +52,16 @@ def test_a_new_kind_queues_behind_the_existing_ones_on_the_same_source():
lanes = job_runtime.SourceLanes()
async def go() -> None:
first = lanes.enter("src-a", "draft", "job-1")
second = lanes.enter("src-a", "test-introspect", "job-2")
other = lanes.enter("src-b", "test-introspect", "job-3")
first = await lanes.enter("src-a", "draft", "job-1")
second = await lanes.enter("src-a", "test-introspect", "job-2")
other = await lanes.enter("src-b", "test-introspect", "job-3")
assert first.granted.is_set() and not second.granted.is_set()
assert other.granted.is_set(), "不同源并行"
assert lanes.ahead("src-a", "job-2") == ["draft"]
first.release()
assert await lanes.ahead("src-a", "job-2") == ["draft"]
await first.release()
await asyncio.wait_for(second.wait(), timeout=1)
second.release()
other.release()
await second.release()
await other.release()
asyncio.run(go())
+6 -6
View File
@@ -622,10 +622,10 @@ def _judged_args(run: Run) -> tuple:
async def test_a_judge_interleaved_with_a_post_seal_append_survives_a_restart(flaky, monkeypatch):
"""按需裁判记判定(_append_judged)的条件更新不带 last_seq 条件:它取了号、还没写,封存后追加抢先提交了,
它再提交时把 last_seq 写回自己那个更小的号。同一进程里序号计数只增不减,不会撞号;进程重启后计数从 0 起,
各处又按 last_seq 接号——裁判自己也是。所以启动时按库里实际的最后一条改正 last_seq:之后再裁判、继续运行,
都接在实际的最后一条后面。"""
"""按需裁判记判定(_append_judged)和封存后追加交错:裁判取了号、还没写,封存后追加抢先提交了。以前裁判的条件更新不带
last_seq 条件,照样提交、把 last_seq 写回自己那个更小的号(同一进程里计数只增不减,不撞号;多进程时两个进程各自的计数
会撞号)。现在同 append_events:last_seq 也要还是读到的那个,后到的裁判这一次不记(判定照样交回给点的人),last_seq
停在追加的那一条上。启动时照旧按库里实际的最后一条改正 last_seq:之后再裁判、继续运行,都接在实际的最后一条后面。"""
from app.api import evidence
from app.engine.run_append import append_events
@@ -662,9 +662,9 @@ async def test_a_judge_interleaved_with_a_post_seal_append_survives_a_restart(fl
await asyncio.sleep(0.01)
written = await append_events(first.id, [inquiry("这次为什么失败")], require="sealed")
held.set()
assert await judging == {"seq": first.last_seq + 1}
assert await judging is None, "追加先提交了:后到的裁判不记,免得两条撞号"
assert written is not None and written[0].seq == first.last_seq + 2
assert (await _row(first.id)).last_seq == first.last_seq + 1, "裁判后提交,last_seq 被写回了小的号"
assert (await _row(first.id)).last_seq == written[0].seq
monkeypatch.setattr(evidence, "SessionLocal", opened)
# 进程重启:内存里的序号计数没了,启动时 setup 照常执行一遍。只改落后的那条,别的运行原样不动
+3 -2
View File
@@ -243,7 +243,8 @@ async def test_background_jobs_follow_the_lane_order(client, shop):
await session.commit()
ids = {"introspect": intro.id, "draft": draft.id, "profile": profile.id}
# 队里是 起草(在做)→ 剖析(排着);探查刚建、还没进队:排在队里的后面
tickets = [runtime.lanes.enter(sid, "draft", ids["draft"]), runtime.lanes.enter(sid, "profile", ids["profile"])]
tickets = [await runtime.lanes.enter(sid, "draft", ids["draft"]),
await runtime.lanes.enter(sid, "profile", ids["profile"])]
try:
items = (await client.get(f"/api/datasources/{sid}/background-jobs")).json()["items"]
assert [i["kind"] for i in items] == ["draft", "profile", "introspect"], items
@@ -253,7 +254,7 @@ async def test_background_jobs_follow_the_lane_order(client, shop):
assert {"total", "done", "status"} <= set(items[0])
finally:
for t in tickets:
t.release()
await t.release()
async with SessionLocal() as session:
for model, key in ((IntrospectJob, "introspect"), (CatalogDraftJob, "draft"), (CatalogProfileJob, "profile")):
await session.execute(delete(model).where(model.id == ids[key]))