Send a reconnect's index replay to the new worker
Browse files at this commitc1d26746bfa1a9643f1d9b529ce3e6400cc97b47Parent: 1a7d5c3- fix
- send a reconnect's index replay to the new worker
2 changed files +35 -4
- src/ai/registry.rs +21 -3
- src/constant.rs +14 -1
src/ai/registry.rsmodified+21 -3
| @@ -191,9 +191,13 @@ impl<C> Registry<C> { | |||
| 191 | let chosen = offering | 191 | let chosen = offering |
| 192 | .filter(|w| w.inflight() < w.max_concurrent) | 192 | .filter(|w| w.inflight() < w.max_concurrent) |
| 193 | .min_by(|a, b| { | 193 | .min_by(|a, b| { |
| 194 | - a.inflight() | 194 | + a.inflight().cmp(&b.inflight()).then_with(|| { |
| 195 | - .cmp(&b.inflight()) | 195 | + // A reconnect keeps the dying connection in the registry |
| 196 | - .then_with(|| a.id.cmp(&b.id)) | 196 | + // until its idle timeout. Equal load must prefer the |
| 197 | + // newer id, or the replay that fires on register is sent | ||
| 198 | + // to the connection that is about to time out. | ||
| 199 | + b.id.cmp(&a.id) | ||
| 200 | + }) | ||
| 197 | }) | 201 | }) |
| 198 | .ok_or_else(|| AiError::Busy(capability.to_string()))?; | 202 | .ok_or_else(|| AiError::Busy(capability.to_string()))?; |
| 199 | chosen.inflight.fetch_add(1, Ordering::Relaxed); | 203 | chosen.inflight.fetch_add(1, Ordering::Relaxed); |
| @@ -336,6 +340,20 @@ mod tests { | |||
| 336 | reg.pick("ocr.extract").expect("slot came back"); | 340 | reg.pick("ocr.extract").expect("slot came back"); |
| 337 | } | 341 | } |
| 338 | 342 | ||
| 343 | + #[tokio::test] | ||
| 344 | + async fn equal_load_prefers_the_newer_worker() { | ||
| 345 | + // A reconnect leaves the dying connection registered until its idle | ||
| 346 | + // timeout. The replay that fires on the new Hello must not be handed | ||
| 347 | + // to that older id. | ||
| 348 | + let reg = registry_with(&[ | ||
| 349 | + ("01a0cafe-old", &["rag.index"], 4), | ||
| 350 | + ("01a0cb0f-new", &["rag.index"], 4), | ||
| 351 | + ]); | ||
| 352 | + let lease = reg.pick("rag.index").unwrap(); | ||
| 353 | + assert_eq!(lease.worker().id, "01a0cb0f-new"); | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | + | ||
| 339 | #[tokio::test] | 357 | #[tokio::test] |
| 340 | async fn picks_spread_across_workers_by_least_inflight() { | 358 | async fn picks_spread_across_workers_by_least_inflight() { |
| 341 | let reg = registry_with(&[("w1", &["ocr.extract"], 4), ("w2", &["ocr.extract"], 4)]); | 359 | let reg = registry_with(&[("w1", &["ocr.extract"], 4), ("w2", &["ocr.extract"], 4)]); |
src/constant.rsmodified+14 -1
| @@ -559,6 +559,10 @@ pub const AI_DEFAULT_CONCURRENT_PER_WORKER: usize = 8; | |||
| 559 | /// well under the idle timeout so an idle-but-healthy service is never dropped | 559 | /// well under the idle timeout so an idle-but-healthy service is never dropped |
| 560 | /// for being quiet; a service that actually died is deregistered within the | 560 | /// for being quiet; a service that actually died is deregistered within the |
| 561 | /// idle window without any application-level heartbeat frame. | 561 | /// idle window without any application-level heartbeat frame. |
| 562 | +/// | ||
| 563 | +/// Must stay strictly above [`AI_BLOB_WRITE_STALL_SECS`]. A stalled blob write | ||
| 564 | +/// resets that stream; if the two bounds are equal, a busy runtime lets the | ||
| 565 | +/// idle timeout win and the whole connection dies as `TimedOut` instead. | ||
| 562 | pub const AI_IDLE_TIMEOUT_SECS: u64 = 30; | 566 | pub const AI_IDLE_TIMEOUT_SECS: u64 = 30; |
| 563 | pub const AI_KEEPALIVE_SECS: u64 = 10; | 567 | pub const AI_KEEPALIVE_SECS: u64 = 10; |
| 564 | 568 | ||
| @@ -568,7 +572,16 @@ pub const AI_KEEPALIVE_SECS: u64 = 10; | |||
| 568 | /// takes, while a service that opens a blob stream and never reads it stops | 572 | /// takes, while a service that opens a blob stream and never reads it stops |
| 569 | /// pinning a task and an open file descriptor once the QUIC stream window | 573 | /// pinning a task and an open file descriptor once the QUIC stream window |
| 570 | /// fills. A wall-clock deadline on the transfer could not tell the two apart. | 574 | /// fills. A wall-clock deadline on the transfer could not tell the two apart. |
| 571 | -pub const AI_BLOB_WRITE_STALL_SECS: u64 = 30; | 575 | +/// |
| 576 | +/// Strictly under [`AI_IDLE_TIMEOUT_SECS`]: the stream reset has to land | ||
| 577 | +/// before the connection idle timeout, or a stalled reader kills every other | ||
| 578 | +/// stream on that connection. | ||
| 579 | +pub const AI_BLOB_WRITE_STALL_SECS: u64 = 15; | ||
| 580 | + | ||
| 581 | +const _: () = assert!( | ||
| 582 | + AI_BLOB_WRITE_STALL_SECS + 5 < AI_IDLE_TIMEOUT_SECS, | ||
| 583 | + "a stalled blob write must reset the stream before the connection idle timeout" | ||
| 584 | +); | ||
| 572 | 585 | ||
| 573 | /// The capability an AI service declares to answer chatbot turns. The bridge | 586 | /// The capability an AI service declares to answer chatbot turns. The bridge |
| 574 | /// routes a chat request to any worker carrying it; with none registered the | 587 | /// routes a chat request to any worker carrying it; with none registered the |