Dispatch a request to the correct Data Plane core. Enqueues into the per-core weighted-fair queue keyed by `DatabaseId`, then flushes WFQ → physical ring. Returns `Err` when the WFQ itself is full (total capacity reached across all active databases on that core).
(&mut self, request: envelope::Request)
| 273 | /// then flushes WFQ → physical ring. Returns `Err` when the WFQ itself is |
| 274 | /// full (total capacity reached across all active databases on that core). |
| 275 | pub fn dispatch(&mut self, request: envelope::Request) -> crate::Result<()> { |
| 276 | let tenant_id = request.tenant_id.as_u64(); |
| 277 | let req_id = request.request_id.as_u64(); |
| 278 | let database_id = request.database_id.as_u64(); |
| 279 | |
| 280 | // Per-tenant fairness: reject if this tenant has too many in-flight requests. |
| 281 | if self.max_per_tenant_inflight > 0 { |
| 282 | let inflight = self.tenant_inflight.get(&tenant_id).copied().unwrap_or(0); |
| 283 | if inflight >= self.max_per_tenant_inflight { |
| 284 | return Err(crate::Error::Dispatch { |
| 285 | detail: format!( |
| 286 | "tenant {tenant_id}: queue full ({inflight}/{} in-flight)", |
| 287 | self.max_per_tenant_inflight |
| 288 | ), |
| 289 | }); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | let core_id = |
| 294 | self.router |
| 295 | .resolve(request.vshard_id) |
| 296 | .ok_or_else(|| crate::Error::Dispatch { |
| 297 | detail: format!("no core for vshard {}", request.vshard_id), |
| 298 | })?; |
| 299 | |
| 300 | let channel = &mut self.cores[core_id]; |
| 301 | |
| 302 | // Refresh priority for this DB in the WFQ. |
| 303 | let cls = self.priority_resolver.priority_for(database_id); |
| 304 | channel.wfq.set_priority(database_id, cls); |
| 305 | |
| 306 | // Check per-DB suspended state (≥95% of fair share). |
| 307 | if channel.wfq.is_suspended_for(database_id) { |
| 308 | return Err(crate::Error::Dispatch { |
| 309 | detail: format!( |
| 310 | "database {database_id}: virtual queue suspended (≥95% of fair share on core {core_id})" |
| 311 | ), |
| 312 | }); |
| 313 | } |
| 314 | |
| 315 | // Enqueue into the WFQ — returns Err if total capacity is full. |
| 316 | channel |
| 317 | .wfq |
| 318 | .try_enqueue(database_id, request) |
| 319 | .map_err(|_| crate::Error::Dispatch { |
| 320 | detail: format!("core {core_id}: total WFQ capacity exhausted"), |
| 321 | })?; |
| 322 | |
| 323 | // Update per-DB pressure. |
| 324 | channel.update_db_pressure(database_id); |
| 325 | |
| 326 | // Flush WFQ → physical ring. |
| 327 | channel.flush_wfq(); |
| 328 | |
| 329 | // Update global backpressure based on ring utilization. |
| 330 | let util = channel.request_tx.utilization(); |
| 331 | if let Some(new_state) = channel.backpressure.update(util) { |
| 332 | warn!( |