Files
ewoooc/services
OoO e862a9040c
All checks were successful
CD Pipeline / deploy (push) Successful in 9m14s
fix(ollama): 解「111 關機 → GCP 也斷」三主機 retry 鏈 + lazy host
統帥 2026-05-04 反饋:「111 關機後,兩台 GCP Ollama 也跟著斷線不可用」

根因(雙 bug 連動):
  1. OllamaService.__init__ 凍結 self.host:
     `self.host = host or resolve_ollama_host()` 是 instance 級凍結
     容器啟動時若 GCP cold start 觸發 fallback 111 → self.host 永遠卡 111
     即使 cache 過期,instance 不會重新 resolve
  2. generate() 失敗無 retry 鏈:
     原邏輯只 mark_unhealthy(self.host) + return failure
     沒有「換下一台主機再試」邏輯 → 111 死則全死

修補(雙管齊下):
  A. self.host 改 @property:每次存取走 resolve_ollama_host()
     - caller 顯式指定(_explicit_host)才凍結
     - 內部 _RESOLVE_TTL 120s cache 控制 HTTP probe 成本
  B. generate() 三主機 retry 鏈:
     for attempt in range(3):
         current_host = self.host  # property lazy resolve
         if attempted → break (避免無限迴圈)
         post → success ? return : mark_unhealthy + retry
     mark_unhealthy 自動清空 resolve cache,下次 self.host 取新主機

行為對比:
  戰前:GCP cold start 卡 111 → 111 關機 → 全部失敗
  戰後:GCP cold start 卡 111 → 111 關機 → mark_unhealthy(111) →
        self.host 重 resolve → GCP Primary(已暖機)→ 成功

generate_embedding 同類修補延後(caller 多走 explicit host 路徑風險較低)

regression: 36 unit tests 全綠(test_ollama_resolve + test_ai_call_logger)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 08:42:52 +08:00
..