Skip to content

fix(ai): review calls through the gateway no longer stall for about 190 s on ollama, with the cause measured first #1069

Closed

Description

@dan-petty

Problem

Under Ollama, a fifth to a third of qwen3-coder calls through the gateway took about 190–215 s, or a multiple of that, whatever their size. They accounted for most of a review's model time.

Measured in S9 (20261003-021501, release/v0.2.25, 356 files, 371 calls, 2,152.7 s; spend.db read-only):

Tier Calls Calls of about 190 s or more Share of call-seconds Median of the others
48gib-fast 189 35 (19%) 80% 8.5 s
64gib-standard 122 28 (23%) 81% 13.6 s
48gib-slow 23 7 (30%) 81% 22.8 s

The measurement this item first asked for has been made (the ten-run loop S12–S21 of 2026-10-03, read from the gateway and Ollama logs):

  • The ~190 s stall is the ~125 s cut that the Cloudflare edge applies to a long-held request, plus a ~60 s resend by the router or the client.
  • Verification wall time follows the slowest chat (ρ 0.90).
  • Every review call took the edge path (5,010/5,010 rows on 2026-10-03), although the gateway is reachable on the LAN through its NodePort.

Routing makes the queue wait. The router-wide routing_strategy: simple-shuffle with num_retries: 5 (k8s/llm/gateway/configmap.yaml:171-172) spreads devops-review over three one-slot tiers with weights 9/6/6 (configmap.yaml:79-94). With weighted shuffle, three in-flight requests land on three different tiers only 21% of the time, so requests queue behind each other on one Ollama while another tier idles. The client allows 16 review requests in flight (config/defaults.py:433), and /model/info reports no slot count (ai/gateway_tune.py:116-132). Ollama queues up to 512 requests by default, so the gateway's 429s never fire. Queue wait was 82–87.5% of Ollama time in S11 and the loop.

This item is phase P0 of the evidence-first review (#1137). Its body was rewritten on 2026-10-03 so the acceptance criteria stay current; the title is unchanged.

Acceptance criteria

  • devops ai gateway connect saves the gateway's NodePort URL from node_port_address (k8s/node_port.py), probing only /health/liveliness and /model/info, so review calls take the LAN path instead of the edge.
  • The gateway sets cancel_on_disconnect: true, and review requests carry num_retries: 0, so a cut request is neither finished unseen nor resent behind the client's back.
  • The router-wide routing_strategy changes from simple-shuffle to least-busy (configmap.yaml:171). Simulated queue-wait share at a cap of 3 is 39% under shuffle and 0% under least-busy [estimate].
  • The client cap comes from a slots table. Review in-flight requests are capped at the sum of slots in a config table (distinct api_base × OLLAMA_NUM_PARALLEL; 3 for devops-review today). This replaces the constant 16 (config/defaults.py:433), because /model/info carries no slot count.
  • A configmap test pins cancel_on_disconnect, the routing strategy and the review group's settings, in the style of tests/test_k8s_llm_gateway.py.
  • A recorded measurement procedure, fixed LogQL and PromQL queries for edge cuts, backend chats and queue-wait share, is stored in the task file. A person-run step saves its output into the review session directory.
  • (person-run, other clients quiesced with devops ai quiesce) On one fixed-input review: 0 edge cuts; backend chats equal ledger attempts (needs fix(ai): the spend ledger records every attempt the gateway failed, including the retry transport's, and its gap to the gateway's count is measured first #1019); queue-wait share at most 10%; LAN path 100%.
  • Tests run offline, call no gateway, Ollama or model, and each runs in under 1 s.
  • changelog.d/1069.md records the fix under ### Fixed. CHANGELOG.md is not edited.
  • uv run devops ci passes.

Key questions

Strategic alignment

Amended 2026-10-04: the client retries only what can succeed, and only the call that failed

Measured during an owner-run evaluation on 2026-10-04 (.data/ai-eval/iter-20261004T024327Z; review sessions 20261004-024421 to 20261004-033712):

  • A 400 re-ran a whole page. In the slowest review (991.7 s total; the typical review took about 545 s), one file got an HTTP 400 and a retry. Its persona stage took 748 s, against 306 s and 340 s in the two runs before it. The retry loop counts every AIClientError as temporary, a 4xx included (ai/review/pipeline.py:1638-1644). One failure therefore re-sends all five persona calls for the page: 74 calls instead of 70 in that run.

  • One call stalled before it reached the gateway. A qwen3-coder call took 432.8 s (.data/ai/spend.db: finish_reason stop, 6,522 prompt and 94 completion tokens). From 03:42:33 to 03:49:16 the gateway and every Ollama tier were idle: in-flight requests stayed at their idle value, and no completion was recorded. The time was lost between the client and the gateway, on the edge path this item moves off. No client record shows the attempt, its status or the 400 body; per-attempt records are fix(ai): the spend ledger records every attempt the gateway failed, including the retry transport's, and its gap to the gateway's count is measured first #1019.

  • The edge path cut chat requests too. In the chat benchmark arm, the client ran one worker, yet gateway in-flight requests rose from 4 to 10 over 45 minutes, with 9 completions in 40 minutes. devops-chat generations on the ollama-16gib tier ran 16–25 minutes each, one ending in a 500 at 25 minutes, and every one was cut at the edge after about 125 s and resent. The output-token cap that stops those runaway generations belongs to fix(ai): sampling settings reach the model or are refused, review sessions record them, and the reply cache key includes them #1070. This item's LAN path, cancel_on_disconnect and least-busy routing cover the pile-up.

  • Only retryable statuses are retried. The review client retries 408, 429 and 5xx replies and connection errors. A 4xx reply, a 400 included, is not retried: it is reported with its status and the start of its body. Test: a fake transport answers 400 once; the persona call fails without a second attempt, and the report shows the status.

  • A failed persona call retries alone. A retry re-sends only the persona call that failed, never the page's other persona calls. Test: with five personas on one page and one transient 503, the fake transport sees six calls, not ten.

  • The person-run measurement above also counts persona calls per page, and that count equals the number of personas when no call failed.

  • One retry layer. Today a chat call can be sent up to 25 times. _retry_chat_dispatch (ai/client/unified.py:610-640) retries any exception up to DEFAULT_AI_MAX_RETRIES + 1 = 5 times. Each of those attempts goes through the tenacity retry transport (ai/client/base.py:53-60), which retries up to 5 times on its own. Every send can wait out the 3,600 s read timeout. In the chat arm on 2026-10-04, this kept one benchmark task resending into a backlog for over 45 minutes. Exactly one layer retries, with the status rules above. Test: a fake transport that always answers 503 sees exactly max_retries + 1 sends per call.

  • Abandoned connections are closed. After the edge cut its requests, the benchmark process still held two sockets in CLOSE-WAIT to the gateway. A cancelled or failed attempt closes its connection. Test: after a cut request, the client's connection pool holds no closed-by-peer connection.

Activity

  1. added
    type/bug
    scope/ai
    priority/p1-high
    on Oct 3, 2026
  2. added this to the v0.2.28 milestone on Oct 6, 2026
  3. dan-petty-bot commented on Oct 6, 2026

    @dan-petty-bot
    Collaborator

    Moved to v0.2.28: v0.2.28 started with fewer than its 12 items and was topped up to 12 from Ready items, critical fixes and P0 features first. (devops roadmap reprioritize)

  4. added a commit that references this issue on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aitype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions