Skip to content

fix(listener): admission 不再卡住 message loop,派發保守計費並於 JobStarted 重新歸屬 (#192) - #199

Merged
ycpss91255 merged 10 commits into
mainfrom
fix/192-admission
Nov 30, 2026
Merged

ycpss91255 merged 10 commits into
mainfrom
fix/192-admission

Conversation

@ycpss91255

Copy link
Copy Markdown
Member

摘要

修正 #192:idle 的 JIT runner 佔住 admission 額度時,listener 的 message loop 停在 admission 等待,不再 GetMessage / AcquireJobs,整個 scale set 卡死。

做法照 issue 2026-11-30 的設計決定:

  1. admission 不阻塞:每個派發決策只探測一次,不放行就結束這一輪 reconcile,message loop 繼續輪詢,下一個 envelope(包含沒有 statistics 的)再試。
  2. 派發時保守計費:新 runner 依目前已知、尚未開始的 assignment 中宣告最大的 size 計費(clamp 後),沒有已知 assignment 時用 defaultJob。GPU 用同一個最大 VRAM 宣告選卡與計費。
  3. JobStarted 時重新歸屬:以 JIT runner 名稱找到 worker,把計費換成實際 job 的宣告 size;GPU 在已釘選的同一張卡上調整 VRAM。不認得的 runner 不改任何計費,只記 log,但該 job 不再算作等待中。

變更

  • 新增 ledger:以單一 mutex 管理已知 assignment、retired job id(最近 4096 個)、每個 runner 的計費;committed 一律由持有中的 worker 加總,不用增量計數,任何事件順序都不會變成負數或殘留。
  • 每次派發使用獨立的 runner 名稱;CPU 計費沿用通過 admission 時的大小。
  • 移除 settle window 與等待邏輯。
  • 文件:ADR-0005/0006/0007、listener README、架構圖、CHANGELOG 同步。

Red evidence

所有 red 都在 golang:1.25.3 container 內執行(go test 或 just coverage-go)。

卡死回歸(c1):go test -run TestRunKeepsPollingWhenAdmissionFull,未修正前:

--- FAIL: TestRunKeepsPollingWhenAdmissionFull (2.00s)
    listener_test.go:1457: message loop stalled: AcquireJobs for the third message never ran while admission was full

保守計費與重新歸屬(review r1):

charge_test.go:46: committed = {cores:1 gb:4}, want 4 cores 16 GiB
charge_test.go:77: committed cores = 1, want 4 from the retained assignment
charge_test.go:205: committed (card 0, card 1) = (24, 0), want (16, 16)
charge_test.go:90: committed after JobStarted = {cores:4 gb:16}, want 1 core 4 GiB

envelope 內順序、無 statistics 重試、GPU 卡未出現在探測(review r2):

charge_test.go:269: committed = {cores:4 gb:16}, want the default job {cores:1 gb:4} (the retired job is not known)
charge_test.go:296: in flight = 1, want 2
charge_test.go:321: card 0 committed = 2147483647, want 25 (its own budget, not the whole-GPU sentinel)

worker 結束順序(review r3):

--- FAIL: TestAssignmentKeyHeldUntilWorkerRecordGone (0.00s)
    charge_test.go:357: assignment key was released while the worker record was still held

TestLedgerOrderInsensitive 的 red 只到編譯失敗(ledger 型別尚不存在)。

延遲的 JobStarted 與計費大小(review r4):

round4_test.go:83: both dispatches minted the name "job"
round4_test.go:90: a delayed start of the first runner resized the replacement to 1 cores, want 4
round4_test.go:120: charged 3 cores, want the 7 that passed admission

加強後的測試(review r5):對 HEAD 刻意做 4 種破壞,各自讓新測試失敗後還原。

  • 繞過 CPU admission:
    charge_test.go:151: minted 2 runners while the budget was full, want 1
    listener_test.go:1479: minted runners for ["a" "b"], want only [a]: b must be denied while a holds the budget
    
  • 忽略 GPU gate:charge_test.go:210: minted [a-r1] onto a card with no room
  • 重新歸屬時額外要求 job id 相符(job swap):
    charge_test.go:99: committed after JobStarted = {cores:4 gb:16}, want 1 core 4 GiB
    charge_test.go:246: after JobStarted committed (card 0, card 1) = (16, 16), want (16, 8)
    
  • 未知 runner 的 JobStarted 沒有讓 job 離開等待清單:
    charge_test.go:397: in flight = 1, want 2
    charge_test.go:399: committed = {cores:4 gb:16}, want 6 cores 24 GiB (4 unchanged + 2 for the next runner)
    

測試

  • just check exit 0
  • just coverage-go exit 0,listener core 93.9%(下限 85%)
  • go vet、go test -race ./... 通過

備註

  • 合併後需要在空檔重新部署所有 listener。
  • round4 的測試仍在沒有 in-flight 時直接佔滿 worker slot,已在註解說明,沒有重構。

Closes #192

- 新增 listener/ledger.go:已知指派、已退場 job id、runner 計費記錄由同一把鎖持有,
  committed 的 CPU/RAM/VRAM 改為對持有中 runner 的加總,不再增減計數器
- 已 started 或 completed 的 job id 不會再回到已知指派;已持有的 runner 名稱拒絕再次 dispatch;
  exit 只釋放自己 token 的記錄
- worker 結束時先釋放計費記錄,再釋放指派鍵,避免重送指派在空窗內以同名取代
- 新增 TestLedgerOrderInsensitive 與 TestAssignmentKeyHeldUntilWorkerRecordGone;ADR-0006、ADR-0007 與 CHANGELOG 同步
- 輪詢回歸測試(CPU):第一個 runner 真的佔住整個 budget,第二個不會被開出,
  且第三個 message 的 AcquireJobs 照常進行。
- 輪詢回歸測試(GPU):卡已滿時不會開出 runner,也不會在卡上記帳。
- 釋放後再派發:第一個 runner 透過真正的結束流程釋放額度,不再直接改 ledger。
- 重新歸屬:JobStarted 的 job id 和 runner 被開出時的 assignment 不同,只有依
  JIT runner 名稱比對才會通過;補上 GPU 未知 runner 不改變記帳的斷言。
- 新增 message loop 層級的測試:未知 runner 的 JobStarted 不改變任何記帳,但該
  job 已開始,不再是已知 assignment,也不再影響下一個 runner 的保守計費。
- round4 測試改成等待准入探測,不再 sleep,結束時釋放 provisioner。
…ope 重試准入 (#192)

- retire 的 job id 只保留最近 4096 個,超過就忘掉最舊的;ledger 註解、ADR-0006、
  CHANGELOG 不再說「永遠不會再回到已知 assignment」。
- 未知 runner 的 JobStarted:不改變任何記帳,只記 log;但該 job 已開始,離開已
  知 assignment,不再影響下一個 runner 的保守計費。ledger、listener、ADR-0006、
  CHANGELOG、README 一致。
- Listen 註解:啟動、每個 message envelope(包含沒有 statistics 的)與空輪詢都
  依最新的 statistics 重試准入。
- dispatch 註解:重送的 assignment 由 reserveAssignment 擋下;每次派發都有獨立
  的 runner 名稱,ledger 拒絕同名只是防護。
- ADR-0006:派發時是保守估計,JobStarted 之後依 JIT runner 名稱歸屬實際 job;
  README 補上保守計費、同卡 GPU 重新歸屬與空輪詢重試。
- gofmt 整理 ledger.go、ledger_test.go、listener.go。
@ycpss91255
ycpss91255 enabled auto-merge November 30, 2026 11:19
@ycpss91255
ycpss91255 added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit 1455762 Nov 30, 2026
9 checks passed
@ycpss91255
ycpss91255 deleted the fix/192-admission branch November 30, 2026 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(listener): admission 卡住 message loop,idle runner 佔住額度造成 deadlock

1 participant