/******************************************************************************* * ___ _ ____ ____ * / _ \ _ _ ___ ___| |_| _ \| __ ) * | | | | | | |/ _ \/ __| __| | | | _ \ * | |_| | |_| | __/\__ \ |_| |_| | |_) | * \__\_\\__,_|\___||___/\__|____/|____/ * * Copyright (c) 2014-2019 Appsicle * Copyright (c) 2019-2025 QuestDB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ******************************************************************************/ //! Sync QWP/WebSocket (RFC 6455) sender. Hand-rolled HTTP/1.1 upgrade and binary //! framing — no external WebSocket dependency. See QWP_SPECIFICATION §2 (transport), //! §3 (version negotiation) and §13 (response format). use std::collections::VecDeque; use std::io::{ErrorKind, Read, Write}; use std::net::{Shutdown, SocketAddr, TcpStream}; use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use std::sync::{Arc, Condvar, Mutex, TryLockError}; use std::thread; use std::time::{Duration, Instant}; use rand::RngCore; use crate::error; use crate::ingress::SyncProtocolHandler; use crate::ingress::buffer::{QwpWsColumnarBuffer, QwpWsFramePlan, QwpWsNoFramePlan}; use crate::ingress::conf::{ DurableAckTiers, QwpWsConfig, QwpWsEndpoint, QwpWsInitialConnectMode, SfDurability, }; use crate::ingress::tls::{TlsSettings, configure_tls}; use crate::ws::frame::{ OPCODE_BINARY, OPCODE_CLOSE, OPCODE_CONTINUATION, OPCODE_PING, OPCODE_PONG, OPCODE_TEXT, Opcode, }; use crate::ws::nosigpipe::NoSigpipeTcp; use super::qwp_ws_codec::{self as codec, MAX_INBOUND_FRAME_BYTES}; #[cfg(test)] use super::qwp_ws_driver::QwpWsCoreTestHarness; use super::qwp_ws_driver::{ BlockingQwpWsTransport, CatchUpDriveError, CloseOutcome, DriveOutcome, DriverError, DriverEvent, PublicationLifecycle, PublicationLog, PublicationState, QwpWsCoreTransport, QwpWsCounters, QwpWsHotResponseProgress, QwpWsHotSendProgress, QwpWsPublicationStore, QwpWsReconnectState, QwpWsReconnectStep, QwpWsSendCore, QwpWsTransportFailureAction, ReconnectPolicy, ReconnectReason, TransportFailure, TransportPoll, TransportResponse, reconnect_error_is_foreground_terminal, reconnect_error_is_terminal, reconnect_error_is_terminal_when_established, reconnect_sleep_duration, retry_budget_exhausted_error, }; use super::qwp_ws_orphan::{ ManualOrphanDrainers, OrphanDrainerConfig, OrphanDrainerPool, is_candidate_orphan, scan_orphan_slots, }; use super::qwp_ws_ownership::QwpWsSenderError; use super::qwp_ws_publisher::{ QwpWsFlushEncoder, QwpWsReplayEncoder, qwp_ws_encoded_message_size_error, }; use super::qwp_ws_queue::{OutboundFrame, SentFrame}; use super::qwp_ws_sfa_queue::{ SfaMemoryQueueOptions, SfaProducer, SfaProgressView, SfaQueueError, segment_payload_capacity, two_frame_segment_payload_capacity, }; use super::qwp_ws_sfa_slot::{SfaSlotOptions, SfaSlotQueue}; use super::qwp_ws_sfa_symbol_dict::PersistedSymbolDict; // ---------- transport ---------- type TlsStream = rustls::StreamOwned; const QWP_WS_TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(5); const QWP_WS_DEFAULT_BOUNDED_CONNECT_TIMEOUT: Duration = Duration::from_secs(15); const QWP_WS_RUNNER_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(30); #[cfg(test)] thread_local! { static FAIL_NEXT_RECOVERED_DICT_COPY: std::cell::Cell = const { std::cell::Cell::new(false) }; } #[cfg(test)] pub(crate) fn fail_next_recovered_dict_copy_for_test() { FAIL_NEXT_RECOVERED_DICT_COPY.with(|fail| fail.set(true)); } #[cfg(test)] fn should_fail_recovered_dict_copy_for_test() -> bool { FAIL_NEXT_RECOVERED_DICT_COPY.with(|fail| fail.replace(false)) } const QWP_WS_RUNNER_SHUTDOWN_POLL: Duration = Duration::from_millis(1); /// Cold-path handle used by the runner owner to break blocking socket I/O. /// /// The duplicated descriptor prevents fd-reuse races, and `shut` is sticky so /// a reconnect that installs a socket after close has started is interrupted /// immediately. The mutex is never taken by frame send/receive. #[derive(Default)] pub(crate) struct TrafficGate { state: Mutex, } #[derive(Default)] struct TrafficGateState { current: Option, /// Raw handle of the ORIGINAL socket, not the `current` dup: a shutdown /// through the `try_clone` (WSADuplicateSocket) dup can leave later /// `recv()`s on the original blocking. Only valid while `current` is /// `Some`: every close of the original is mutex-ordered behind /// `clear()`/re-register, so the handle cannot be recycled while the /// gate still holds it. #[cfg(windows)] original: Option, shut: bool, } // Clears a newly registered socket if TLS or WebSocket setup returns early. #[cfg(all(test, unix))] type TestRawSocket = std::os::fd::RawFd; #[cfg(all(test, windows))] type TestRawSocket = std::os::windows::io::RawSocket; struct TrafficRegistration<'a> { gate: &'a TrafficGate, armed: bool, #[cfg(all(test, any(unix, windows)))] original: TestRawSocket, } #[cfg(all(test, unix))] fn test_socket_is_open(socket: TestRawSocket) -> bool { // SAFETY: F_GETFD only inspects the descriptor value and dereferences no pointers. unsafe { libc::fcntl(socket, libc::F_GETFD) != -1 } } #[cfg(all(test, windows))] fn test_socket_is_open(socket: TestRawSocket) -> bool { use windows_sys::Win32::Networking::WinSock::{SO_TYPE, SOCKET, SOL_SOCKET, getsockopt}; let Ok(socket) = SOCKET::try_from(socket) else { return false; }; let mut socket_type = 0i32; let mut option_len = std::mem::size_of_val(&socket_type) as i32; // SAFETY: the output buffer is a live i32 and option_len describes its exact size. unsafe { getsockopt( socket, SOL_SOCKET, SO_TYPE, (&mut socket_type as *mut i32).cast(), &mut option_len, ) == 0 } } impl TrafficGate { fn register(&self, stream: &TcpStream) -> std::io::Result> { let shutdown_handle = stream.try_clone()?; let mut state = self.state.lock().unwrap_or_else(|err| err.into_inner()); if state.shut { drop(state); shutdown_socket(&shutdown_handle)?; return Err(std::io::Error::new( ErrorKind::Interrupted, "QWP/WebSocket traffic is shutting down", )); } state.current = Some(shutdown_handle); #[cfg(windows)] { use std::os::windows::io::AsRawSocket; state.original = Some(stream.as_raw_socket()); } Ok(TrafficRegistration { gate: self, armed: true, #[cfg(all(test, unix))] original: { use std::os::fd::AsRawFd; stream.as_raw_fd() }, #[cfg(all(test, windows))] original: { use std::os::windows::io::AsRawSocket; stream.as_raw_socket() }, }) } pub(super) fn clear(&self) { let mut state = self.state.lock().unwrap_or_else(|err| err.into_inner()); state.current = None; #[cfg(windows)] { state.original = None; } } fn is_shutdown(&self) -> bool { self.state .lock() .unwrap_or_else(|err| err.into_inner()) .shut } pub(super) fn shutdown(&self) -> std::io::Result<()> { // The syscalls run under the gate mutex on purpose: every close of // the original socket is mutex-ordered behind `clear()`, so while // `current` is `Some` the original handle is still open and // `CancelIoEx` cannot hit a recycled handle. Cold path only. let mut state = self.state.lock().unwrap_or_else(|err| err.into_inner()); state.shut = true; #[cfg(windows)] if let Some(original) = state.original.take() { use std::os::windows::io::FromRawSocket; use windows_sys::Win32::Foundation::HANDLE; // Shutdown does not wake an in-flight recv(), so CancelIoEx // follows it; the reverse order lets a recv() slip in between. // SAFETY: `original` is open while `current` is `Some`. let original_stream = std::mem::ManuallyDrop::new(unsafe { TcpStream::from_raw_socket(original) }); let shut = shutdown_socket(&original_stream); unsafe { windows_sys::Win32::System::IO::CancelIoEx(original as HANDLE, std::ptr::null()); } state.current = None; return shut; } if let Some(stream) = state.current.take() { shutdown_socket(&stream)?; } Ok(()) } } impl TrafficRegistration<'_> { fn keep(mut self) { self.armed = false; } } impl Drop for TrafficRegistration<'_> { fn drop(&mut self) { if self.armed { #[cfg(all(test, any(unix, windows)))] { let state = self .gate .state .lock() .unwrap_or_else(|err| err.into_inner()); if state.current.is_some() { assert!( test_socket_is_open(self.original), "traffic gate registration outlived its original socket" ); } } self.gate.clear(); } } } fn shutdown_socket(stream: &TcpStream) -> std::io::Result<()> { match stream.shutdown(Shutdown::Both) { Ok(()) => Ok(()), Err(err) if err.kind() == ErrorKind::NotConnected => Ok(()), Err(err) => Err(err), } } /// Which lifecycle owns a shared QWP/WebSocket connect walk. /// /// `Foreground` is a caller-thread initial connect: the user is synchronously /// waiting and no bounded-shutdown deadline depends on the dial returning. /// `ForegroundBounded` is foreground work whose connection setup a bounded /// wait depends on -- I/O-runner reconnects and the async initial-connect /// retry loop (both raced by the 30s runner shutdown), and manual-mode /// reconnects inside `drive_once`. Only orphan-slot drainers use /// `BackgroundDrainer`, matching Java's background-connect policy; a transport /// stores the kind of its lifecycle (`Foreground`/`BackgroundDrainer`) and /// derives the bounded variant per reconnect via [`Self::for_reconnect`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum QwpWsConnectKind { Foreground, ForegroundBounded, BackgroundDrainer, } impl QwpWsConnectKind { fn is_background(self) -> bool { self == Self::BackgroundDrainer } fn bounded_dial(self) -> bool { self != Self::Foreground } pub(crate) fn for_reconnect(self) -> Self { match self { Self::Foreground | Self::ForegroundBounded => Self::ForegroundBounded, Self::BackgroundDrainer => Self::BackgroundDrainer, } } } /// Extends Java's `effectiveConnectTimeoutMs`: caller-thread initial connects /// preserve the configured value verbatim (`None` keeps the OS-default dial), /// while an unset timeout on any bounded dial receives a finite fallback so a /// bounded shutdown cannot remain parked until the OS TCP-connect deadline. /// Java instead cancels an in-flight dial with `shutdown(2)` on a pre-published /// fd (`ConnectCancellation`); that fd does not exist before /// `TcpStream::connect` returns, and mid-`SYN_SENT` shutdown wake is /// Linux-specific, so bounding the dial is the portable equivalent. fn effective_connect_timeout(bounded_dial: bool, configured: Option) -> Option { if bounded_dial && configured.is_none() { Some(QWP_WS_DEFAULT_BOUNDED_CONNECT_TIMEOUT) } else { configured } } pub(crate) enum WsStream { Plain(NoSigpipeTcp), Tls(Box), } impl WsStream { pub(crate) fn set_timeouts( &self, read: Option, write: Option, ) -> std::io::Result<()> { let sock = self.tcp_stream(); sock.set_read_timeout(read)?; sock.set_write_timeout(write)?; Ok(()) } pub(crate) fn read_nonblocking_once(&mut self, out: &mut [u8]) -> std::io::Result { let guard = NonblockingModeGuard::new(self.tcp_stream())?; let read = self.read(out); let restore = guard.restore(); match (read, restore) { (_, Err(err)) => Err(err), (result, Ok(())) => result, } } fn tcp_stream(&self) -> &TcpStream { match self { WsStream::Plain(sock) => sock.tcp(), WsStream::Tls(stream) => stream.get_ref().tcp(), } } /// Emit a TLS `close_notify` and try to flush it. No-op for plain /// sockets. `rustls::ClientConnection` does NOT auto-send /// `close_notify` on `Drop`, so callers issuing a clean shutdown /// (after writing the WS Close frame) must invoke this explicitly to /// satisfy RFC 8446 §6.1 and avoid server-side truncation warnings. pub(crate) fn shutdown_tls(&mut self) { if let WsStream::Tls(stream) = self { stream.conn.send_close_notify(); let _ = stream.conn.complete_io(&mut stream.sock); } } } struct NonblockingModeGuard { sock: TcpStream, armed: bool, } impl NonblockingModeGuard { fn new(sock: &TcpStream) -> std::io::Result { let guard_sock = sock.try_clone()?; sock.set_nonblocking(true)?; Ok(Self { sock: guard_sock, armed: true, }) } fn restore(mut self) -> std::io::Result<()> { self.armed = false; self.sock.set_nonblocking(false) } } impl Drop for NonblockingModeGuard { fn drop(&mut self) { if self.armed { let _ = self.sock.set_nonblocking(false); } } } impl Read for WsStream { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { match self { WsStream::Plain(s) => s.read(buf), WsStream::Tls(s) => s.read(buf), } } } impl Write for WsStream { fn write(&mut self, buf: &[u8]) -> std::io::Result { match self { WsStream::Plain(s) => s.write(buf), WsStream::Tls(s) => s.write(buf), } } fn flush(&mut self) -> std::io::Result<()> { match self { WsStream::Plain(s) => s.flush(), WsStream::Tls(s) => s.flush(), } } } // ---------- handler state ---------- struct QwpWsConnectedParts { encoder: QwpWsReplayEncoder, store: QwpWsPublicationStore, send_core: QwpWsSendCore, /// Delta symbol-dict mode for the slot (memory always; file iff the side-file /// opened). Drives both the encoder and the driver mirror. delta_dict_enabled: bool, /// Recovered symbol-dict entries (`[len][utf8]...`) + count, to seed the /// producer dict and the driver mirror on file-mode recovery / orphan-drain. /// Empty otherwise. recovered_dict_entries: Vec, recovered_dict_count: u32, /// The slot's persisted symbol dictionary (file mode) for the foreground's /// write-ahead. `None` in memory mode / on open failure. persisted_symbol_dict: Option, } pub(crate) struct SyncQwpWsHandlerState { encoder: QwpWsReplayEncoder, runner: SyncQwpWsRunner, pub(crate) server_max_batch_size: Arc, pub(crate) request_durable_ack: DurableAckTiers, /// Retained for a pending ACK wait's callback guard. Reconnects use the /// same provider through the runner; a warning handler may be waiting on /// this sender while that runner joins the handler's token worker. pub(crate) token_provider: Option, /// Hard cap on a single store-and-forward frame's payload: `sf_max_segment_bytes` /// minus the segment and frame headers. The queue rejects anything larger, /// so publishers must size-check and split against THIS bound — /// `max_buf_size` (100 MiB default) sits far above it and never binds /// first. pub(crate) sfa_frame_payload_cap: usize, /// Split target for oversize flushes: the largest payload that still packs /// two frames per segment, so split output amortizes segment rotation /// instead of forcing a fresh segment per near-cap frame. pub(crate) sfa_frame_split_target: usize, orphan_pool: Option, close_drain_timeout: Duration, /// Whether the background driver enabled its symbol-dict catch-up mirror /// (memory mode always; file mode iff the persisted side-file opened). The /// column foreground reads this so it emits delta frames on exactly the same /// condition — the two must stay in lockstep. pub(crate) delta_dict_enabled: bool, /// Symbols recovered from the slot's persisted dictionary on a file-mode /// reconnect/adopt, in id order (empty in memory mode / on a fresh slot). /// Seeds whichever foreground dictionary owns this state so newly ingested /// symbols continue above the recovered ids. pub(crate) recovered_dict_entries: Vec, pub(crate) recovered_dict_count: u32, /// The slot's persisted symbol dictionary (file mode) for foreground /// write-ahead. Routed to whichever foreground owns this state — the /// standalone replay encoder in [`connect_qwp_ws`] or the pooled core in /// [`super::column_sender::PooledSenderCore::new_store_and_forward`]; the two are /// mutually exclusive. `None` in memory mode / on side-file open failure. pub(crate) persisted_symbol_dict: Option, /// Whether a split chunk of a column sender may defer its non-final /// frames' commit. A row buffer does not ask: see /// [`flush_qwp_ws_through`]. /// /// Memory mode only: with `sf_dir` every frame of a split chunk commits /// by itself. Frames that wait for their commit on disk need a slot /// that, when it is opened, removes the ones whose committing frame /// never came. A slot does that for the frames of a row buffer /// (`remove_unfinished_flush` in `qwp_ws_sfa_queue`); the split of a /// chunk has not been moved onto it. pub(crate) sfa_split_deferral_enabled: bool, } impl SyncQwpWsHandlerState { /// Whether the encoder of a `Sender` sends its symbols as a delta: see /// [`QwpWsFlushEncoder::delta_dict_enabled`]. pub(crate) fn encoder_delta_dict_enabled(&self) -> bool { self.encoder.delta_dict_enabled() } /// Releases the recovered dictionary seeded into the standalone replay /// encoder at connect. The pooled core uses its own dictionary and never /// touches this encoder, so that seed is dead weight once the pooled core /// claims the state; called from [`PooledSenderCore::new_store_and_forward`]. /// /// [`PooledSenderCore::new_store_and_forward`]: super::column_sender::PooledSenderCore::new_store_and_forward pub(crate) fn release_dormant_encoder_dict(&mut self) { self.encoder.release_dormant_dict(); } } pub(crate) struct ManualQwpWsHandlerState { encoder: QwpWsReplayEncoder, store: QwpWsPublicationStore, send_core: QwpWsSendCore, pub(crate) server_max_batch_size: Arc, pub(crate) request_durable_ack: DurableAckTiers, pub(crate) token_provider: Option, /// Hard cap on a single queued frame's payload, as on /// [`SyncQwpWsHandlerState::sfa_frame_payload_cap`]. pub(crate) sfa_frame_payload_cap: usize, /// The size that the frames of a flush above the frame cap are cut for, /// as [`SyncQwpWsHandlerState::sfa_frame_split_target`]. pub(crate) sfa_frame_split_target: usize, /// The flush that left the sender closed for publications, if one did: /// see [`ManualQwpWsPublisher`]. aborted_flush: Option, orphan_drainers: Option, append_deadline: Duration, close_drain_timeout: Duration, /// Whether the queue is a slot under `sf_dir`, where the frames that a /// close drain leaves unconfirmed stay for the next sender on the slot. has_sf_dir: bool, } impl ManualQwpWsHandlerState { /// As [`SyncQwpWsHandlerState::encoder_delta_dict_enabled`]. pub(crate) fn encoder_delta_dict_enabled(&self) -> bool { self.encoder.delta_dict_enabled() } } pub(crate) struct SyncQwpWsRunner { shared: Arc>>, sender_errors_pending: Arc, producer: Option, lifecycle: PublicationLifecycle, /// The flush that left the sender closed for publications, if one did: /// see [`Self::abort_flush`]. aborted_flush: Option, /// As [`ManualQwpWsHandlerState::has_sf_dir`]; `false` until the sender /// that opens the runner sets it. has_sf_dir: bool, backpressure: Arc, ok_completed_upper: Arc, append_deadline: Duration, stop: Arc, traffic_gate: Arc, shutdown_timeout: Duration, thread: Option>, } /// A flush that queued some of its frames and could not queue the rest. struct AbortedFlush { /// The last frame published before the flush, if there was one. published_before: Option, /// The error that the flush returned, which every later publication /// gets too. error: crate::Error, } impl AbortedFlush { /// A flush that queued its frames from `first_fsn` on and could not /// queue the rest, because of `cause`. Its error keeps what the cause /// carried, and is never in doubt: nothing of the flush is committed, /// below the server's `qwp.max.uncommitted.rows`, which the message says. fn new(first_fsn: u64, cause: crate::Error) -> Self { let msg = format!( "Could not flush buffer: the flush stopped part way: {}. Nothing of this flush is \ committed and the buffer still holds its rows. Only a flush with more rows of one \ table than the server's qwp.max.uncommitted.rows may be committed in part, and \ flushing it again then stores that part twice unless the table has DEDUP keys. \ This sender takes no more flushes and still delivers the flushes before this one: \ once they are confirmed, close it or return it to its pool, and flush the buffer \ again through a new sender.", cause.msg().trim_end_matches('.') ); Self { published_before: first_fsn.checked_sub(1), error: cause .reclassified(crate::ErrorCode::FlushAborted, msg) .with_in_doubt(false), } } /// Whether `err`, the error that stopped the sender, is about the frames /// of this flush alone: the server's rejection of frames that all come /// after the frames before the flush, once those are confirmed /// (`completed`). The error of the flush already says that nothing of it /// is committed, so the calls about the frames before it do not fail on /// such a rejection, which stays readable as a sender error. fn is_rejection_of_its_frames(&self, err: &crate::Error, completed: Option) -> bool { // `None` orders below every `Some`. completed >= self.published_before && err .qwp_ws_rejection() .is_some_and(|rejection| Some(rejection.from_fsn) > self.published_before) } } /// `Err(err)`, for `err` the error that stopped a sender, unless it is about /// the frames of the flush that failed part way alone: see /// [`AbortedFlush::is_rejection_of_its_frames`]. fn stopping_error( aborted_flush: Option<&AbortedFlush>, err: crate::Error, completed: Option, ) -> crate::Result<()> { match aborted_flush { Some(flush) if flush.is_rejection_of_its_frames(&err, completed) => Ok(()), _ => Err(err), } } /// [`check_store_error`] for a sender whose flush may have failed part way: /// see [`stopping_error`]. fn check_store_error_after( store: &QwpWsPublicationStore, aborted_flush: Option<&AbortedFlush>, ) -> crate::Result<()> { store .check_durability() .map_err(driver_error_to_error_without_state)?; match check_store_terminal_error(store) { Err(err) => stopping_error(aborted_flush, err, store.completed_fsn()), ok => ok, } } /// A confirmed position `fsn` as callers see it: after a flush that failed /// part way, no higher than the last frame published before it, which /// callers see as the last one published. A server that commits a part of /// that flush by itself (more rows of one table than its /// `qwp.max.uncommitted.rows`) confirms frames of it, which are not /// reported. fn confirmed_as_reported(aborted_flush: Option<&AbortedFlush>, fsn: Option) -> Option { match aborted_flush { // `None` orders below every `Some`. Some(flush) => fsn.min(flush.published_before), None => fsn, } } /// What a flush publishes through: the runner of a sender in background /// progress mode, or the store and the connection of a sender in manual /// progress mode ([`ManualQwpWsPublisher`]). trait QwpWsFlushPublisher { /// The producer of the queue, which tells what room frames need. `None` /// once the sender has given it up to close its queue. fn producer(&self) -> Option<&SfaProducer>; /// Waits until the queue has room for frames with payloads of /// `payload_lens` bytes, all at once, as long as a single frame waits /// for the room of its own. Queues nothing. fn await_room(&mut self, payload_lens: &[usize]) -> crate::Result<()>; /// Queues one frame, and returns its number. fn publish(&mut self, payload: &[u8]) -> crate::Result; /// [`Self::publish`] for a frame after the first of a flush in frames, /// which has had its room. With `sf_dir` the queue may still refuse the /// frame while storage work of the network thread is in flight, which /// ends by itself: the frame waits for it beyond the append deadline. /// In manual progress mode that work runs on the flushing thread, so /// none is in flight here and this is `publish`. fn publish_later_frame(&mut self, payload: &[u8]) -> crate::Result; /// Leaves a flush that queued its frames from `first_fsn` on and could /// not queue the rest, because of `cause`: the sender takes no more /// publications. Returns the error of that flush. fn abort(&mut self, first_fsn: u64, cause: crate::Error) -> crate::Error; } struct SyncQwpWsRunnerCore { send_core: QwpWsSendCore, progress: SfaProgressView, cold_effects: VecDeque, backpressure: Arc, ok_completed_upper: Arc, lifecycle: PublicationLifecycle, } struct SyncQwpWsPendingRunnerCore { connected: Option>, pending_connect: QwpWsPendingConnect, progress: SfaProgressView, backpressure: Arc, ok_completed_upper: Arc, lifecycle: PublicationLifecycle, } struct QwpWsPendingConnect { host: String, port: String, use_tls: bool, tls_settings: Option, qwp_ws: QwpWsConfig, auth_header: Option, reconnect_policy: ReconnectPolicy, durable_ack: DurableAckTiers, server_max_batch_size: Arc, traffic_gate: Arc, /// Whether the I/O thread enables its symbol-dict catch-up mirror once /// connected (memory mode always; file mode iff the side-file opened). delta_dict_enabled: bool, /// Symbols recovered from the slot's persisted dictionary (file mode), in id /// order, used to seed the catch-up mirror when the send core is first built. /// Empty in memory mode / on a fresh slot. recovered_dict_entries: Vec, recovered_dict_count: u32, #[cfg(test)] terminal_error_for_test: Option, } #[derive(Debug, Clone, Copy)] enum RunnerColdEffect { Sent { frame: SentFrame, replayed: bool }, Event(DriverEvent), CompletedThrough { fsn: u64, wire_seq: u64 }, } /// What a test runs on the network thread of a sender in background progress /// mode each time that thread has stored a confirmation of the server in the /// queue and not yet written it to the watermark of the slot, which waits for /// the store lock. It gets the confirmed position that the queue holds. #[cfg(test)] pub(crate) type ConfirmationStoredHook = Arc) + Send + Sync>; #[cfg(test)] thread_local! { /// The hook of the senders started on this thread, which their network /// threads take over: see [`ConfirmationStoredHookGuard`]. static CONFIRMATION_STORED_HOOK: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; } /// Gives the senders started on this thread while it lives a /// [`ConfirmationStoredHook`], and restores the one before when dropped. #[cfg(test)] pub(crate) struct ConfirmationStoredHookGuard(Option); #[cfg(test)] impl ConfirmationStoredHookGuard { pub(crate) fn new(hook: ConfirmationStoredHook) -> Self { Self(set_confirmation_stored_hook(Some(hook))) } } #[cfg(test)] impl Drop for ConfirmationStoredHookGuard { fn drop(&mut self) { set_confirmation_stored_hook(self.0.take()); } } /// Sets the hook of this thread, and gives the one it replaces. #[cfg(test)] fn set_confirmation_stored_hook( hook: Option, ) -> Option { CONFIRMATION_STORED_HOOK.with(|current| current.replace(hook)) } #[cfg(test)] fn confirmation_stored_hook() -> Option { CONFIRMATION_STORED_HOOK.with(|current| current.borrow().clone()) } #[derive(Debug)] struct BackpressureNotifier { lock: Mutex<()>, available: Condvar, generation: AtomicU64, } #[cfg(test)] const DEFAULT_APPEND_DEADLINE: Duration = Duration::from_secs(30); const BACKPRESSURE_PARK: Duration = Duration::from_micros(50); /// How long a later frame of a flush in frames waits for storage work of /// the network thread before it looks again: see /// [`SyncQwpWsRunner::wait_for_storage_work`]. const STORAGE_WORK_POLL: Duration = Duration::from_millis(100); impl SyncQwpWsRunner where Q: PublicationLog + Send + 'static, { #[cfg(test)] fn start(driver: QwpWsCoreTestHarness) -> Self where T: QwpWsCoreTransport + Send + 'static, { Self::start_driver_with_append_deadline(driver, DEFAULT_APPEND_DEADLINE) } #[cfg(test)] fn start_driver_with_append_deadline( driver: QwpWsCoreTestHarness, append_deadline: Duration, ) -> Self where T: QwpWsCoreTransport + Send + 'static, { let (store, send_core) = driver.into_parts(); Self::start_with_append_deadline( store, send_core, append_deadline, Arc::new(TrafficGate::default()), ) } fn start_with_append_deadline( mut store: QwpWsPublicationStore, send_core: QwpWsSendCore, append_deadline: Duration, traffic_gate: Arc, ) -> Self where T: QwpWsCoreTransport + Send + 'static, { let lifecycle = store.lifecycle(); let progress = store.progress_view(); let producer = store.take_producer(); let sender_errors_pending = store.sender_errors_pending(); let shared = Arc::new(Mutex::new(store)); let backpressure = Arc::new(BackpressureNotifier::new()); let ok_completed_upper = Arc::new(AtomicU64::new(0)); let stop = Arc::new(AtomicBool::new(false)); let thread_shared = Arc::clone(&shared); let thread_backpressure = Arc::clone(&backpressure); let thread_ok_completed_upper = Arc::clone(&ok_completed_upper); let thread_stop = Arc::clone(&stop); let thread_lifecycle = lifecycle.clone(); #[cfg(test)] let thread_confirmation_stored_hook = confirmation_stored_hook(); let thread = thread::spawn(move || { #[cfg(test)] set_confirmation_stored_hook(thread_confirmation_stored_hook); let mut core = SyncQwpWsRunnerCore { send_core, progress, cold_effects: VecDeque::new(), backpressure: thread_backpressure, ok_completed_upper: thread_ok_completed_upper, lifecycle: thread_lifecycle, }; while !thread_stop.load(Ordering::Acquire) { match core.drive_step(&thread_shared, &thread_stop) { RunnerStep::Idle => thread::sleep(Duration::from_micros(50)), RunnerStep::Continue => {} RunnerStep::Stop => break, }; } }); Self { shared, sender_errors_pending, producer, lifecycle, aborted_flush: None, has_sf_dir: false, backpressure, ok_completed_upper, append_deadline, stop, traffic_gate, shutdown_timeout: QWP_WS_RUNNER_SHUTDOWN_TIMEOUT, thread: Some(thread), } } fn start_pending_connect( queue: Q, pending_connect: QwpWsPendingConnect, append_deadline: Duration, event_capacity: usize, rejection_sink: Option>, ) -> Self { let mut store = QwpWsPublicationStore::new(queue, event_capacity); store.set_rejection_sink(rejection_sink); let lifecycle = store.lifecycle(); let progress = store.progress_view(); let producer = store.take_producer(); let sender_errors_pending = store.sender_errors_pending(); let shared = Arc::new(Mutex::new(store)); let backpressure = Arc::new(BackpressureNotifier::new()); let ok_completed_upper = Arc::new(AtomicU64::new(0)); let stop = Arc::new(AtomicBool::new(false)); let traffic_gate = Arc::clone(&pending_connect.traffic_gate); let thread_shared = Arc::clone(&shared); let thread_backpressure = Arc::clone(&backpressure); let thread_ok_completed_upper = Arc::clone(&ok_completed_upper); let thread_stop = Arc::clone(&stop); let thread_lifecycle = lifecycle.clone(); #[cfg(test)] let thread_confirmation_stored_hook = confirmation_stored_hook(); let thread = thread::spawn(move || { #[cfg(test)] set_confirmation_stored_hook(thread_confirmation_stored_hook); let mut core = SyncQwpWsPendingRunnerCore { connected: None, pending_connect, progress, backpressure: thread_backpressure, ok_completed_upper: thread_ok_completed_upper, lifecycle: thread_lifecycle, }; while !thread_stop.load(Ordering::Acquire) { match core.drive_step(&thread_shared, &thread_stop) { RunnerStep::Idle => thread::sleep(Duration::from_micros(50)), RunnerStep::Continue => {} RunnerStep::Stop => break, }; } }); Self { shared, sender_errors_pending, producer, lifecycle, aborted_flush: None, has_sf_dir: false, backpressure, ok_completed_upper, append_deadline, stop, traffic_gate, shutdown_timeout: QWP_WS_RUNNER_SHUTDOWN_TIMEOUT, thread: Some(thread), } } fn publish_replay_payload(&mut self, payload: &[u8]) -> crate::Result { self.publish_frame(payload, false) } /// [`Self::publish_replay_payload`], or, with `later_frame`, the /// publication of a frame after the first of a flush in frames: see /// [`QwpWsFlushPublisher::publish_later_frame`]. fn publish_frame(&mut self, payload: &[u8], later_frame: bool) -> crate::Result { self.check_publication_open()?; if let Some(producer) = self.producer.as_mut() && let Some(receipt) = producer .try_submit_active(payload) .map_err(DriverError::from) .map_err(driver_error_to_error_without_state)? { return Ok(receipt.fsn); } // Start the wait budget before rotation or backpressure, not on every // successful append to an already allocated segment. let deadline = Instant::now().checked_add(self.append_deadline); loop { let backpressure_generation = self.backpressure.generation(); self.check_publication_open()?; if let Some(producer) = self.producer.as_mut() { match producer.try_submit(payload) { Ok(receipt) => return Ok(receipt.fsn), Err(err) => { let err: DriverError = err.into(); let backpressure = driver_error_backpressure_queue(&err); if backpressure.is_some() { if !self.wait_after_refusal( backpressure, backpressure_generation, deadline, later_frame, )? { return Err(submit_timeout_error( backpressure, self.reconnect_failure().as_ref(), )); } continue; } return Err(driver_error_to_error_without_state(err)); } } } let submit_result = { let mut store = self.lock_shared()?; check_store_error(&store)?; store.try_submit(payload) }; match submit_result { Ok(receipt) => return Ok(receipt.fsn), Err(err) if driver_error_is_backpressure(&err) => { let backpressure = driver_error_backpressure_queue(&err); if !self.wait_after_refusal( backpressure, backpressure_generation, deadline, later_frame, )? { return Err(submit_timeout_error( backpressure, self.reconnect_failure().as_ref(), )); } } Err(err) => return Err(driver_error_to_error_without_state(err)), } } } /// Waits after the queue refused a frame for `backpressure`, and returns /// whether to try the frame again: whether the back-pressure changed /// after `generation` before `deadline`. /// /// A `later_frame` of a flush in frames that storage work of the network /// thread keeps out (`StorageSpareNotReady`) waits for that work without /// the deadline: it returns `true` after each wait, with or without a /// change, and fails once that thread has stopped. See /// [`Self::wait_for_storage_work`]. fn wait_after_refusal( &self, backpressure: Option, generation: u64, deadline: Option, later_frame: bool, ) -> crate::Result { use super::qwp_ws_queue::QueueError; if later_frame && matches!(backpressure, Some(QueueError::StorageSpareNotReady { .. })) { self.wait_for_storage_work(generation)?; return Ok(true); } Ok(self.wait_for_publication_capacity(generation, deadline)) } /// Waits up to [`STORAGE_WORK_POLL`] for the back-pressure to change /// after `generation`, for a frame that storage work of the network /// thread in flight keeps out of the queue: a spare segment that it /// makes, the manifest write of a trim, or a periodic sync. That work /// ends by itself, so the caller tries the frame again either way, with /// no deadline. /// /// Fails once the network thread has ended: a thread that panicked /// leaves the sender open and its storage work marked as in flight, and /// notifies nobody. fn wait_for_storage_work(&self, generation: u64) -> crate::Result<()> { // A slice that ends without a change is tried again too: a storage // step can clear its flag without a task that notifies, as a trim // whose manifest write failed does. let slice_end = Instant::now() + STORAGE_WORK_POLL; if !self.wait_for_publication_capacity(generation, Some(slice_end)) && self .thread .as_ref() .is_none_or(thread::JoinHandle::is_finished) { return Err(error::fmt!( SocketError, "QWP/WebSocket Store-and-Forward append failed: the network thread of the \ sender has stopped" )); } Ok(()) } /// Waits until the queue has room for frames with payloads of /// `payload_lens` bytes, all at once: see [`SfaProducer::has_room_for`]. /// Returns at once when it has. /// /// The room comes when the server confirms frames that the queue holds /// and the runner frees their segments. The wait is the one of a single /// frame in [`Self::publish_replay_payload`]: it ends at the append /// deadline with the same error, and at once when the sender stops /// taking publications. It queues nothing, so a flush that gives up here /// has published nothing. fn wait_for_room(&self, producer: &SfaProducer, payload_lens: &[usize]) -> crate::Result<()> { if producer.has_room_for(payload_lens) { return Ok(()); } let deadline = Instant::now().checked_add(self.append_deadline); loop { let backpressure_generation = self.backpressure.generation(); self.check_publication_open()?; if producer.has_room_for(payload_lens) { return Ok(()); } if !self.wait_for_publication_capacity(backpressure_generation, deadline) { return Err(submit_timeout_error( Some(producer.no_room_error()), self.reconnect_failure().as_ref(), )); } } } fn check_publication_open(&self) -> crate::Result<()> { match self.lifecycle.load() { PublicationState::Open => Ok(()), PublicationState::Closing => Err(match &self.aborted_flush { Some(flush) => flush.error.clone(), None => driver_error_to_error_without_state(DriverError::Closing), }), PublicationState::Terminal => { let store = self.lock_shared()?; let err = driver_error_to_error_from_store(&store, DriverError::Terminal); // A rejection of the frames of the flush that failed part // way does not replace the error of that flush. Err(match &self.aborted_flush { Some(flush) if flush.is_rejection_of_its_frames(&err, store.completed_fsn()) => { flush.error.clone() } _ => err, }) } } } /// Leaves a flush that queued its frames from `first_fsn` on and could /// not queue the rest, because of `cause`. Returns the error of that /// flush. /// /// The queued frames hold rows that only a later frame could commit, so /// nothing may be published after them: the sender takes no more /// publications, and each one gets the error of the flush. The network /// thread is not told: it goes on sending what the queue holds, the /// frames of this flush among them, and reading the server's answers, so /// the flushes before this one are still delivered. The server answers /// none of the frames of this flush: it holds their rows for a frame /// that commits them, and drops them when the connection ends. So /// callers are told that the sender has published what it had before /// this flush, and wait for nothing more: see [`Self::published_fsn`] /// and [`Self::drain_to_deadline`]. /// /// A sender that has stopped for a reason of its own goes on reporting /// that reason, here and at every later call. fn abort_flush(&mut self, first_fsn: u64, cause: crate::Error) -> crate::Error { let mut flush = AbortedFlush::new(first_fsn, cause); if let Err(err) = self.check_publication_open() { flush.error = err; } let error = flush.error.clone(); self.aborted_flush = Some(flush); self.lifecycle.begin_close(); error } fn check_error(&self) -> crate::Result<()> { if let Some(producer) = self.producer.as_ref() { producer .check_durability() .map_err(DriverError::from) .map_err(driver_error_to_error_without_state)?; } else { let store = self.lock_shared()?; check_store_error_after(&store, self.aborted_flush.as_ref())?; } if self.lifecycle.is_terminal() { let store = self.lock_shared()?; return stopping_error( self.aborted_flush.as_ref(), driver_error_to_error_from_store(&store, DriverError::Terminal), store.completed_fsn(), ); } Ok(()) } /// Why the runner's latest attempt to (re)connect failed, while it has no /// connection. fn reconnect_failure(&self) -> Option { self.lock_shared() .ok() .and_then(|store| store.reconnect_failure().cloned()) } /// The last frame published, or the last one before the flush that /// failed part way, if one did: see [`Self::abort_flush`]. The queue /// keeps its own position, which the network thread sends up to. fn published_fsn(&self) -> crate::Result> { self.check_error()?; if let Some(flush) = &self.aborted_flush { return Ok(flush.published_before); } if let Some(producer) = self.producer.as_ref() { return Ok(producer.published_fsn()); } let store = self.lock_shared()?; check_store_error(&store)?; Ok(store.published_fsn()) } /// The confirmed position, after a flush that failed part way no higher /// than [`Self::published_fsn`]: see [`confirmed_as_reported`]. fn acked_fsn(&self) -> crate::Result> { self.check_error()?; Ok(confirmed_as_reported( self.aborted_flush.as_ref(), self.queue_completed_fsn()?, )) } /// The position that the queue has confirmed, frames of a flush that /// failed part way included. fn queue_completed_fsn(&self) -> crate::Result> { if let Some(producer) = self.producer.as_ref() { return Ok(producer.completed_fsn()); } let store = self.lock_shared()?; check_store_error_after(&store, self.aborted_flush.as_ref())?; Ok(store.completed_fsn()) } fn ok_fsn(&self) -> crate::Result> { self.check_error()?; let completed_upper = if let Some(producer) = self.producer.as_ref() { producer .completed_fsn() .map_or(0, |fsn| fsn.saturating_add(1)) } else { let store = self.lock_shared()?; check_store_error_after(&store, self.aborted_flush.as_ref())?; store.completed_fsn().map_or(0, |fsn| fsn.saturating_add(1)) }; let upper = self .ok_completed_upper .load(Ordering::Acquire) .max(completed_upper); Ok(confirmed_as_reported( self.aborted_flush.as_ref(), upper.checked_sub(1), )) } fn poll_sender_error(&self) -> crate::Result> { if !self.sender_errors_pending.load(Ordering::Acquire) && !self.shared.is_poisoned() { return Ok(None); } let mut store = self.lock_shared()?; Ok(store.poll_sender_error()) } fn poll_sender_error_notification(&self) -> crate::Result> { if !self.sender_errors_pending.load(Ordering::Acquire) && !self.shared.is_poisoned() { return Ok(None); } let mut store = self.lock_shared()?; Ok(store.poll_sender_error_notification()) } fn terminal_sender_error(&self) -> crate::Result> { let store = self.lock_shared()?; Ok(store.terminal_sender_error().cloned()) } fn lifecycle_is_terminal(&self) -> bool { self.lifecycle.is_terminal() } /// Whether a flush failed part way: see [`Self::abort_flush`]. fn has_aborted_flush(&self) -> bool { self.aborted_flush.is_some() } /// Whether the sender stopped for a reason that is not about the frames /// of a flush that failed part way alone: see [`stopping_error`]. fn stopped_for_good(&self) -> bool { self.lifecycle.is_terminal() && self.lock_shared().map_or(true, |store| { stopping_error( self.aborted_flush.as_ref(), driver_error_to_error_from_store(&store, DriverError::Terminal), store.completed_fsn(), ) .is_err() }) } fn sender_errors_dropped_total(&self) -> crate::Result { let store = self.lock_shared()?; Ok(store.sender_errors_dropped_total()) } fn counters(&self) -> crate::Result { let store = self.lock_shared()?; Ok(store.counters()) } fn close_drain(&mut self, timeout: Duration) -> crate::Result<()> { self.begin_close(); self.producer.take(); if timeout.is_zero() { return Ok(()); } self.drain_to_deadline(Instant::now().checked_add(timeout)) } /// Stop accepting new publications and wake the background runner so it /// flushes whatever is already queued. Non-blocking and idempotent; pairs /// with [`Self::drain_to_deadline`]. The unified sender pool calls this on /// every connection being retired *before* waiting on any of them, so a /// multi-connection close drains in parallel rather than serially. fn begin_close(&self) { self.lifecycle.begin_close(); self.backpressure.notify_all(); } /// Block until every published frame has resolved its receipt, then close /// the queue; give up with a `SocketError` once `deadline` passes (an /// already-elapsed deadline fails fast, which is what bounds a batched pool /// close). While reconnecting keeps failing, that error names the latest /// failure, as the manual close drain's does. `deadline == None` waits /// indefinitely. Assumes [`Self::begin_close`] has already run. /// /// After a flush that failed part way ([`Self::abort_flush`]) the wait /// is for the frames before that flush: its own frames are never /// confirmed, and go with the queue: see /// [`Self::close_after_aborted_flush`]. The server's rejection of its /// own frames does not fail the wait: see [`stopping_error`]. fn drain_to_deadline(&mut self, deadline: Option) -> crate::Result<()> { // No publication can pass the Closing lifecycle latch. Release the // detached producer's active-segment mapping before queue.close() // starts the ordered unlink protocol. self.producer.take(); loop { let backpressure_generation = self.backpressure.generation(); { let mut store = self.lock_shared()?; // A periodic-checkpoint failure is recoverable: the runner // keeps retrying it, and close_queue() performs its own // covering sync before tearing down an undrained queue. Do // not let the public durability latch prevent close from // reaching either recovery path. if let Err(err) = check_store_terminal_error(&store) { stopping_error(self.aborted_flush.as_ref(), err, store.completed_fsn())?; } // The frames of a flush that failed part way are never // confirmed: the wait ends at the frame before them. let drained = match &self.aborted_flush { Some(flush) => store.completed_fsn() >= flush.published_before, None => store.all_published_receipts_resolved(), }; if drained { // Storage maintenance performs file work off the // publication-store lock. A task may own segment mappings, // create a hot-spare file, or unlink a retired segment, so // wait for the entire task (including deferred cleanup) to // retire before close tears down the slot. if !store .storage_maintenance_in_flight() .map_err(driver_error_to_error_without_state)? { if self.aborted_flush.is_some() { drop(store); return self.close_after_aborted_flush(deadline); } store .close_queue() .map_err(driver_error_to_error_without_state)?; return Ok(()); } } } if !self.wait_for_publication_capacity(backpressure_generation, deadline) { let unconfirmed = unconfirmed_at_close(&*self.lock_shared()?, self.aborted_flush.as_ref()); return Err(close_drain_timeout_error( unconfirmed, self.reconnect_failure().as_ref(), self.has_sf_dir, )); } } } /// Closes the queue after a flush that failed part way, once the frames /// before it are confirmed. The network thread is stopped first: that /// ends the connection, so the server drops the rows that it holds of /// the flush, and the thread can neither send from the slot nor store a /// confirmation any more. The queue then closes as fully drained: it /// writes the confirmed position, which may be ahead of the watermark /// that the thread wrote, gives up the frames of the flush, and removes /// the files of the slot, the symbol file too. The slot is given up only /// then. If the thread is not gone by `deadline`, or within the bound of /// [`Drop`] when that comes first, nothing is closed: the drop of the /// sender waits for the thread again. /// /// The server's rejection of frames of the flush does not stop the close, /// whenever the thread recorded it: [`Self::drain_to_deadline`] lets it /// pass once the frames before the flush are confirmed (see /// [`stopping_error`]). Neither does another terminal error that the /// thread records once the drain found those frames confirmed, which /// this does not look at: it can only be of the end of the connection, /// and nothing of the flush is committed either way. fn close_after_aborted_flush(&mut self, deadline: Option) -> crate::Result<()> { let wait = deadline.map_or(self.shutdown_timeout, |deadline| { deadline .saturating_duration_since(Instant::now()) .min(self.shutdown_timeout) }); if !self.stop_network_thread(wait)? { return Err(if wait < self.shutdown_timeout { close_drain_timeout_after_aborted_flush_error(self.has_sf_dir) } else { error::fmt!( SocketError, "QWP/WebSocket runner did not stop within {:?}", self.shutdown_timeout ) }); } let mut store = self.lock_shared()?; // The thread finishes a storage task within one step, so none is in // flight once it is gone. if store .storage_maintenance_in_flight() .map_err(driver_error_to_error_without_state)? { return Err(error::fmt!( SocketError, "QWP/WebSocket runner stopped with storage maintenance in flight" )); } store .close_queue_abandoning_unresolved() .map_err(driver_error_to_error_without_state) } /// Stops the network thread and waits up to `wait` until it is gone. /// Whether it is gone. fn stop_network_thread(&mut self, wait: Duration) -> crate::Result { self.stop.store(true, Ordering::Release); if let Err(err) = self.traffic_gate.shutdown() { log::warn!("could not shut down QWP/WebSocket runner traffic: {err}"); } let Some(thread) = self.thread.as_ref() else { return Ok(true); }; if !wait_for_runner_exit(thread, wait) { return Ok(false); } if let Some(thread) = self.thread.take() && thread.join().is_err() { return Err(error::fmt!( SocketError, "QWP/WebSocket runner thread panicked" )); } Ok(true) } fn lock_shared(&self) -> crate::Result>> { self.shared .lock() .map_err(|_| error::fmt!(SocketError, "QWP/WebSocket runner state lock is poisoned")) } /// Whether the queue has room for a deferred frame AND the frame that will /// commit it, or `None` when the runner has no producer and cannot read the /// queue without blocking on the driver. fn has_deferred_commit_headroom(&self) -> Option { if let Some(producer) = self.producer.as_ref() { return Some(producer.has_deferred_commit_headroom()); } // Only reached once `take_producer` has run (close drain), so this is // not the publishing path. try_lock, not lock: the answer only informs // a defer/commit choice and both answers are safe, so blocking the // foreground on the driver thread to learn it is the worse trade. let store = self.shared.try_lock().ok()?; Some(store.progress_view().has_deferred_commit_headroom()) } fn wait_for_publication_capacity(&self, generation: u64, deadline: Option) -> bool { self.backpressure.wait_for_change(generation, deadline) } } impl BackpressureNotifier { fn new() -> Self { Self { lock: Mutex::new(()), available: Condvar::new(), generation: AtomicU64::new(0), } } fn generation(&self) -> u64 { self.generation.load(Ordering::Acquire) } fn notify_all(&self) { let _guard = self.lock(); self.generation.fetch_add(1, Ordering::Release); self.available.notify_all(); } fn wait_for_change(&self, generation: u64, deadline: Option) -> bool { let mut guard = self.lock(); while self.generation.load(Ordering::Acquire) == generation { let Some(deadline) = deadline else { guard = match self.available.wait(guard) { Ok(guard) => guard, Err(poisoned) => poisoned.into_inner(), }; continue; }; if backpressure_deadline_expired(Some(deadline)) { return false; } let wait_for = deadline.saturating_duration_since(Instant::now()); if wait_for.is_zero() { return false; } guard = match self.available.wait_timeout(guard, wait_for) { Ok((guard, _)) => guard, Err(poisoned) => { let (guard, _) = poisoned.into_inner(); guard } }; } true } fn lock(&self) -> std::sync::MutexGuard<'_, ()> { match self.lock.lock() { Ok(guard) => guard, Err(poisoned) => poisoned.into_inner(), } } } fn update_atomic_max(cell: &AtomicU64, value: u64) { let mut current = cell.load(Ordering::Acquire); while value > current { match cell.compare_exchange_weak(current, value, Ordering::AcqRel, Ordering::Acquire) { Ok(_) => return, Err(observed) => current = observed, } } } impl QwpWsPendingConnect { #[allow(clippy::too_many_arguments)] fn new( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, durable_ack: DurableAckTiers, server_max_batch_size: Arc, traffic_gate: Arc, delta_dict_enabled: bool, recovered_dict_entries: Vec, recovered_dict_count: u32, ) -> Self { Self { host: host.to_string(), port: port.to_string(), use_tls, tls_settings, qwp_ws: qwp_ws.clone(), auth_header, reconnect_policy: ReconnectPolicy::bounded( *qwp_ws.reconnect_max_duration, *qwp_ws.reconnect_initial_backoff, *qwp_ws.reconnect_max_backoff, ), durable_ack, server_max_batch_size, traffic_gate, delta_dict_enabled, recovered_dict_entries, recovered_dict_count, #[cfg(test)] terminal_error_for_test: None, } } /// `on_failure` sees each failed attempt that will be retried. fn connect_with_retry( &self, stop: &AtomicBool, on_failure: &dyn Fn(&crate::Error), ) -> Result, crate::Error> { #[cfg(test)] if let Some(err) = &self.terminal_error_for_test { return Err(err.clone()); } let started = Instant::now(); let deadline = started.checked_add(self.reconnect_policy.max_duration()); let endpoints = qwp_ws_configured_endpoints(&self.host, &self.port, &self.qwp_ws); let mut tracker = QwpWsHostHealthTracker::new(endpoints.len()); let mut previous_idx = None; let mut backoff = self.reconnect_policy.initial_backoff(); let mut attempts = 0usize; let mut last_error = None; while !stop.load(Ordering::Acquire) && deadline.is_none_or(|deadline| Instant::now() < deadline) { attempts += 1; match connect_qwp_ws_endpoint_round( &endpoints, &mut tracker, &mut previous_idx, None, self.use_tls, self.tls_settings.clone(), // Runner-owned: the 30s shutdown wait races this dial, so an // unset connect_timeout must not fall back to the OS deadline. QwpWsConnectKind::ForegroundBounded, &self.qwp_ws, self.auth_header.as_deref(), self.qwp_ws.conn_events.as_deref(), Some(&self.traffic_gate), ) { Ok(connected) => { return Ok(Some(BlockingQwpWsTransport::from_connected( Arc::clone(&endpoints), tracker, self.use_tls, self.tls_settings.clone(), QwpWsConnectKind::Foreground, self.qwp_ws.clone(), self.auth_header.clone(), Arc::clone(&self.server_max_batch_size), Some(Arc::clone(&self.traffic_gate)), connected, ))); } Err(err) if reconnect_error_is_terminal(&err) => return Err(err), Err(err) => { on_failure(&err); let role_reject = is_qwp_ws_role_reject_error(&err); last_error = Some(err); let sleep_for = reconnect_sleep_duration( role_reject, self.reconnect_policy.initial_backoff(), backoff, ); if !sleep_before_runner_reconnect(deadline, sleep_for, stop) { break; } backoff = if role_reject { self.reconnect_policy.initial_backoff() } else { double_duration(backoff).min(self.reconnect_policy.max_backoff()) }; } } } if stop.load(Ordering::Acquire) { Ok(None) } else { Err(retry_budget_exhausted_error( "QWP/WebSocket async initial connect", attempts, started, last_error, )) } } } impl SyncQwpWsPendingRunnerCore { fn drive_step( &mut self, shared: &Arc>>, stop: &AtomicBool, ) -> RunnerStep where Q: PublicationLog, { if let Some(connected) = self.connected.as_mut() { return connected.drive_step(shared, stop); } // An ACK wait that times out before the first connection reports why // connecting keeps failing, exactly as after a later drop. Wake the // ACK waiters as after a later drop's failed attempt: see // `SyncQwpWsRunnerCore::record_reconnect_failure`. let record_failure = |err: &crate::Error| { if let Ok(mut store) = shared.lock() { store.record_reconnect_failure(err); } self.backpressure.notify_all(); }; match self .pending_connect .connect_with_retry(stop, &record_failure) { Ok(Some(transport)) => { if let Ok(mut store) = shared.lock() { store.clear_reconnect_failure(); } let mut send_core = QwpWsSendCore::new_with_durable_ack_and_rejection_limit( transport, self.pending_connect.reconnect_policy, self.pending_connect.durable_ack, *self.pending_connect.qwp_ws.max_frame_rejections, *self.pending_connect.qwp_ws.poison_min_escalation_window, ); // Enable the symbol-dict catch-up mirror on the same condition the // foreground delta-encodes (memory mode always; file mode iff the // side-file opened), seeding it from any recovered dictionary so // the mirror's count matches the producer's baseline. The two must // stay in lockstep. if self.pending_connect.delta_dict_enabled { // Take the recovered entries so the buffer is freed after // seeding the mirror rather than living dead in // `pending_connect` (a permanent runner field) for the // connection's life. let recovered = std::mem::take(&mut self.pending_connect.recovered_dict_entries); send_core.enable_delta_dict_owned( recovered, self.pending_connect.recovered_dict_count, ); } self.connected = Some(SyncQwpWsRunnerCore { send_core, progress: self.progress.clone(), cold_effects: VecDeque::new(), backpressure: Arc::clone(&self.backpressure), ok_completed_upper: Arc::clone(&self.ok_completed_upper), lifecycle: self.lifecycle.clone(), }); RunnerStep::Continue } Ok(None) => RunnerStep::Stop, Err(err) => { if !reconnect_error_is_terminal(&err) { thread::sleep(self.pending_connect.reconnect_policy.initial_backoff()); return RunnerStep::Continue; } let mut store = match shared.lock() { Ok(store) => store, Err(_) => { self.lifecycle.terminalize(); self.backpressure.notify_all(); return RunnerStep::Stop; } }; store.mark_terminal(Some(err)); self.backpressure.notify_all(); RunnerStep::Stop } } } } impl SyncQwpWsRunnerCore where T: QwpWsCoreTransport, { fn drive_step( &mut self, shared: &Arc>>, stop: &AtomicBool, ) -> RunnerStep where Q: PublicationLog, { if stop.load(Ordering::Acquire) { return RunnerStep::Stop; } if self.lifecycle.is_terminal() { return RunnerStep::Stop; } if self.flush_cold_effects(shared) == RunnerStep::Stop { return RunnerStep::Stop; } if self.send_core.has_pending_reconnect() { return self.finish_pending_reconnect(shared, stop, Duration::ZERO, None); } // Re-register the whole symbol dictionary via a catch-up frame before the // first replay frame, when a reconnect armed it (delta mode). The manual // send path does the same in QwpWsSendCore::drive_send_available. match self.send_core.drive_catch_up() { Ok(()) => {} Err(CatchUpDriveError::Transport(failure)) => { return self.apply_transport_failure(shared, stop, failure); } Err(CatchUpDriveError::RetryConnection { error, pace }) => { return self.reconnect_with_policy( shared, stop, ReconnectReason::RetryableFailure, error, pace, ); } Err(CatchUpDriveError::Terminal(err)) => { // A terminal catch-up failure (the dictionary cannot be // re-registered -- resend required) is a storage/data-integrity // outcome, not a transport drop, so classify it the same as the // sibling torn-dictionary guard below. return self.store_shared_driver_error(shared, DriverError::Storage(err)); } } // Bounded send burst: ready frames are handed to the transport back to // back and reach the socket in grouped writes when the burst closes. let budget = self.send_core.begin_send_batch(); let mut bytes = 0usize; let mut send_step = RunnerStep::Idle; while bytes < budget { if stop.load(Ordering::Acquire) { return RunnerStep::Stop; } let outbound = match self.send_core.next_outbound_sfa_frame(&self.progress) { Ok(Some(outbound)) => outbound, Ok(None) => break, Err(err) => return self.store_shared_driver_error(shared, err), }; // Torn-dictionary guard (file mode): a replayed delta frame whose // base exceeds the re-registered dictionary is unrecoverable -- // fail loudly rather than send the server a frame it cannot // decode. Checked per frame even within a burst. if let Err(err) = outbound.with_view(|view| self.send_core.guard_dict_not_torn(view.payload)) { return self.store_shared_driver_error(shared, DriverError::Storage(err)); } bytes = bytes .saturating_add(outbound.sent_frame().payload_len) .saturating_add(14); send_step = self.finish_send(shared, stop, outbound); if send_step == RunnerStep::Stop { return RunnerStep::Stop; } if self.send_core.send_batch_interrupted() { return send_step; } } if let Err(failure) = self.send_core.finish_send_batch() { return self.apply_transport_failure(shared, stop, failure); } let receive_step = self.drain_receive_ready(shared, stop); if receive_step == RunnerStep::Stop { return RunnerStep::Stop; } let storage_step = self.finish_storage_maintenance(shared); if storage_step == RunnerStep::Stop { return RunnerStep::Stop; } if send_step == RunnerStep::Continue || receive_step == RunnerStep::Continue || storage_step == RunnerStep::Continue { RunnerStep::Continue } else { self.finish_keepalive(shared, stop) } } fn finish_send( &mut self, shared: &Arc>>, stop: &AtomicBool, outbound: OutboundFrame, ) -> RunnerStep where Q: PublicationLog, { let (frame, send_result) = self.send_core.send_frame(outbound); match send_result { Ok(send_result) => match self.send_core.finish_send_result_hot(frame, send_result) { Ok(QwpWsHotSendProgress::NoResponse { frame, replayed }) => { self.cold_effects .push_back(RunnerColdEffect::Sent { frame, replayed }); if self.flush_cold_effects(shared) == RunnerStep::Stop { return RunnerStep::Stop; } let step = step_from_drive_outcome(DriveOutcome::Sent(frame)); self.backpressure.notify_all(); step } Ok(QwpWsHotSendProgress::Response { frame, replayed, response, }) => { self.cold_effects .push_back(RunnerColdEffect::Sent { frame, replayed }); self.finish_response(shared, stop, response) } Ok(QwpWsHotSendProgress::TransportFailure { frame, replayed, failure, }) => { self.cold_effects .push_back(RunnerColdEffect::Sent { frame, replayed }); if self.flush_cold_effects(shared) == RunnerStep::Stop { return RunnerStep::Stop; } self.apply_transport_failure(shared, stop, failure) } Err(err) => self.store_shared_driver_error(shared, err), }, Err(failure) => self.apply_transport_failure(shared, stop, failure), } } fn finish_receive( &mut self, shared: &Arc>>, stop: &AtomicBool, ) -> RunnerStep where Q: PublicationLog, { match self.send_core.try_poll_response() { Ok(TransportPoll::Response(response)) => self.finish_response(shared, stop, response), Ok(TransportPoll::Progress) => RunnerStep::Continue, Ok(TransportPoll::Idle) => RunnerStep::Idle, Err(failure) => self.apply_transport_failure(shared, stop, failure), } } fn finish_response( &mut self, shared: &Arc>>, stop: &AtomicBool, response: TransportResponse, ) -> RunnerStep where Q: PublicationLog, { match response { TransportResponse::Ack { wire_seq } => { match self .send_core .finish_ack_response_sfa(&self.progress, wire_seq) { Ok(progress) => self.finish_hot_response_progress(shared, progress), Err(err) => self.store_shared_driver_error(shared, err), } } TransportResponse::DurableOk { wire_seq, table_seq_txns, } => { match self.send_core.finish_durable_ok_response_sfa( &self.progress, wire_seq, table_seq_txns, ) { Ok(progress) => self.finish_hot_response_progress(shared, progress), Err(err) => self.store_shared_driver_error(shared, err), } } TransportResponse::DurableAck { table_seq_txns, local, } => { match self.send_core.finish_durable_ack_response_sfa( &self.progress, table_seq_txns, local, ) { Ok(progress) => self.finish_hot_response_progress(shared, progress), Err(err) => self.store_shared_driver_error(shared, err), } } response @ TransportResponse::Reject { .. } => { let outcome = { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.flush_cold_effects_locked(&mut store); match self .send_core .finish_response_defer_reconnect(&mut store, response) { Ok(outcome) => outcome, Err(err) => return self.store_driver_error(&mut store, err), } }; match outcome { DriveOutcome::ReconnectDelay { sleep_for, deadline, } => self.finish_pending_reconnect(shared, stop, sleep_for, deadline), DriveOutcome::Idle => { self.backpressure.notify_all(); RunnerStep::Continue } outcome => { let step = step_from_drive_outcome(outcome); self.backpressure.notify_all(); step } } } } } fn finish_hot_response_progress( &mut self, shared: &Arc>>, progress: QwpWsHotResponseProgress, ) -> RunnerStep where Q: PublicationLog, { if let Some(fsn) = progress.ok_fsn { update_atomic_max(&self.ok_completed_upper, fsn.saturating_add(1)); } self.enqueue_hot_response_events(progress.events); #[cfg(test)] if let Some(hook) = confirmation_stored_hook() && self .cold_effects .iter() .any(|effect| matches!(effect, RunnerColdEffect::CompletedThrough { .. })) { hook(self.progress.completed_fsn()); } if self.flush_cold_effects(shared) == RunnerStep::Stop { return RunnerStep::Stop; } let step = if progress.outcome == DriveOutcome::Idle { RunnerStep::Continue } else { step_from_drive_outcome(progress.outcome) }; self.backpressure.notify_all(); step } fn enqueue_hot_response_events(&mut self, events: Vec) { for event in events { match event { DriverEvent::CompletedThrough { fsn, wire_seq } => { self.cold_effects .push_back(RunnerColdEffect::CompletedThrough { fsn, wire_seq }); } event => self.cold_effects.push_back(RunnerColdEffect::Event(event)), } } } fn drain_receive_ready( &mut self, shared: &Arc>>, stop: &AtomicBool, ) -> RunnerStep where Q: PublicationLog, { let mut step = RunnerStep::Idle; loop { match self.finish_receive(shared, stop) { RunnerStep::Continue => step = RunnerStep::Continue, RunnerStep::Idle => return step, RunnerStep::Stop => return RunnerStep::Stop, } } } fn finish_keepalive( &mut self, shared: &Arc>>, stop: &AtomicBool, ) -> RunnerStep where Q: PublicationLog, { if self.lifecycle.is_terminal() { return RunnerStep::Stop; } let durable_ack_pending = self.send_core.has_pending_durable_ack(); match self .send_core .send_durable_ack_keepalive_if_due(durable_ack_pending) { Ok(true) => RunnerStep::Idle, Ok(false) => RunnerStep::Idle, Err(failure) => self.apply_transport_failure(shared, stop, failure), } } fn finish_storage_maintenance( &mut self, shared: &Arc>>, ) -> RunnerStep where Q: PublicationLog, { let task = { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.flush_cold_effects_locked(&mut store); if store.is_terminal() { return RunnerStep::Stop; } match store.take_storage_maintenance_step() { Ok(task) => task, Err(err) => return self.store_driver_error(&mut store, err), } }; let Some(task) = task else { return RunnerStep::Idle; }; let changed_before_io = task.changes_queue_before_io(); if changed_before_io { self.backpressure.notify_all(); } let result = match task.perform() { Ok(result) => result, Err(err) => { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; if let Err(completion_err) = store.complete_storage_maintenance() { return self.store_driver_error(&mut store, completion_err); } return self.store_driver_error(&mut store, err.into()); } }; let finish = { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.flush_cold_effects_locked(&mut store); match store.finish_storage_maintenance(result) { Ok(finish) => finish, Err(err) => { if let Err(completion_err) = store.complete_storage_maintenance() { return self.store_driver_error(&mut store, completion_err); } return self.store_driver_error(&mut store, err); } } }; let changed = changed_before_io || finish.did_change(); if changed { self.backpressure.notify_all(); } let cleanup_failure = finish.into_cleanup().and_then(|cleanup| cleanup.perform()); let terminal = { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; if let Some(failure) = cleanup_failure && let Err(err) = store.record_storage_cleanup_failure(failure) { if let Err(completion_err) = store.complete_storage_maintenance() { return self.store_driver_error(&mut store, completion_err); } return self.store_driver_error(&mut store, err); } if let Err(err) = store.complete_storage_maintenance() { return self.store_driver_error(&mut store, err); } store.is_terminal() }; // Closing may be waiting solely for the task-wide maintenance lease, // including a successful task that made no queue-visible change. self.backpressure.notify_all(); if terminal { RunnerStep::Stop } else if changed { RunnerStep::Continue } else { RunnerStep::Idle } } fn apply_transport_failure( &mut self, shared: &Arc>>, stop: &AtomicBool, failure: TransportFailure, ) -> RunnerStep where Q: PublicationLog, { let action = { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.flush_cold_effects_locked(&mut store); self.send_core.transport_failure_action(&mut store, failure) }; match action { QwpWsTransportFailureAction::Reconnect { reason, initial_error, pace, } => self.reconnect_with_policy(shared, stop, reason, initial_error, pace), QwpWsTransportFailureAction::Terminal(_error) => { self.backpressure.notify_all(); RunnerStep::Stop } } } fn reconnect_with_policy( &mut self, shared: &Arc>>, stop: &AtomicBool, reason: ReconnectReason, initial_error: crate::Error, pace: Duration, ) -> RunnerStep where Q: PublicationLog, { let mut reconnect = self .send_core .begin_reconnect("QWP/WebSocket reconnect", reason, initial_error) .with_pace(pace); self.finish_reconnect_state(shared, stop, &mut reconnect, Duration::ZERO, None) } fn finish_pending_reconnect( &mut self, shared: &Arc>>, stop: &AtomicBool, sleep_for: Duration, deadline: Option, ) -> RunnerStep where Q: PublicationLog, { let Some(mut reconnect) = self.send_core.take_pending_reconnect() else { return RunnerStep::Continue; }; self.finish_reconnect_state(shared, stop, &mut reconnect, sleep_for, deadline) } fn finish_reconnect_state( &mut self, shared: &Arc>>, stop: &AtomicBool, reconnect: &mut QwpWsReconnectState, mut sleep_for: Duration, mut deadline: Option, ) -> RunnerStep where Q: PublicationLog, { while !stop.load(Ordering::Acquire) { if let Some(pace) = reconnect.take_first_attempt_pace() { sleep_for = pace; deadline = reconnect.deadline(); } if !sleep_before_runner_reconnect( deadline.or_else(|| reconnect.deadline()), sleep_for, stop, ) && stop.load(Ordering::Acquire) { break; } // Bump the cumulative attempt counter before each call. // Brief lock — the reconnect path is the slow one (network // I/O, backoff sleeps), so this lock isn't on the hot path // and won't perceptibly contend with publishers. { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; store.record_reconnect_attempt(); } match self.send_core.reconnect_once(reconnect) { Ok(QwpWsReconnectStep::Reconnected { reason }) => { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.flush_cold_effects_locked(&mut store); let outcome = self.send_core.finish_reconnect_success(&mut store, reason); return step_from_drive_outcome(outcome); } Ok(QwpWsReconnectStep::RetryAfter { sleep_for: retry_sleep, }) => { if let Some(step) = self.record_reconnect_failure(shared, reconnect.last_error()) { return step; } deadline = reconnect.deadline(); sleep_for = retry_sleep; } Ok(QwpWsReconnectStep::Terminal(err)) => { return self.mark_store_terminal(shared, err); } Ok(QwpWsReconnectStep::BudgetExhausted(err)) => { if reconnect_error_is_terminal_when_established(&err) { return self.mark_store_terminal(shared, err); } // As in the manual driver: a budget spent before its // first attempt has no reconnect failure to report. if reconnect.attempts() > 0 && let Some(step) = self.record_reconnect_failure(shared, &err) { return step; } sleep_for = reconnect.initial_backoff(); *reconnect = reconnect.next_after_retryable_terminal(err); deadline = reconnect.deadline(); } Err(err) => { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; return self.store_driver_error(&mut store, err); } } } if stop.load(Ordering::Acquire) { RunnerStep::Stop } else { RunnerStep::Continue } } /// Latch why this reconnect attempt failed, for an ACK wait that times out /// while the transport is down, and wake the ACK waiters: the attempt's /// token pull may have just found that a wait inside an auth callback can /// never complete (`TokenProvider::callback_wait_error`), and such a wait /// must be rejected now, not at its deadline (never, with none). `Some` /// only when the store lock was poisoned and the runner must stop. fn record_reconnect_failure( &self, shared: &Arc>>, err: &crate::Error, ) -> Option where Q: PublicationLog, { match shared.lock() { Ok(mut store) => { store.record_reconnect_failure(err); self.backpressure.notify_all(); None } Err(_) => Some(self.handle_poisoned_lock()), } } fn store_driver_error( &self, store: &mut QwpWsPublicationStore, err: DriverError, ) -> RunnerStep where Q: PublicationLog, { let err = driver_error_to_error_from_store(store, err); store.mark_terminal(Some(err)); self.backpressure.notify_all(); RunnerStep::Stop } fn flush_cold_effects(&mut self, shared: &Arc>>) -> RunnerStep where Q: PublicationLog, { if self.cold_effects.is_empty() { return RunnerStep::Idle; } match shared.try_lock() { Ok(mut store) => { self.flush_cold_effects_locked(&mut store); RunnerStep::Continue } Err(TryLockError::WouldBlock) => RunnerStep::Idle, Err(TryLockError::Poisoned(_)) => self.handle_poisoned_lock(), } } fn flush_cold_effects_locked(&mut self, store: &mut QwpWsPublicationStore) where Q: PublicationLog, { while let Some(effect) = self.cold_effects.pop_front() { match effect { RunnerColdEffect::Sent { frame, replayed } => { store.record_sent_event(frame, replayed); } RunnerColdEffect::Event(event) => store.record_driver_event(event), RunnerColdEffect::CompletedThrough { fsn, wire_seq } => { store.record_completed_through_event(fsn, wire_seq); } } } } fn store_shared_driver_error( &self, shared: &Arc>>, err: DriverError, ) -> RunnerStep where Q: PublicationLog, { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; self.store_driver_error(&mut store, err) } fn mark_store_terminal( &self, shared: &Arc>>, err: crate::Error, ) -> RunnerStep where Q: PublicationLog, { let mut store = match shared.lock() { Ok(store) => store, Err(_) => return self.handle_poisoned_lock(), }; store.mark_terminal(Some(err)); self.backpressure.notify_all(); RunnerStep::Stop } /// Handle a poisoned shared-store lock. /// /// Flips the lifecycle to `Terminal` via its own atomic, and wakes condvar /// waiters so any `publish_replay_payload` blocked on append-deadline /// backpressure observes the terminal state on its next loop iteration /// instead of timing out per submit. The store's `terminal_error` is left /// unset; publishers see a generic terminal error rather than a /// runner-specific one, but they fail fast. fn handle_poisoned_lock(&self) -> RunnerStep { self.lifecycle.terminalize(); self.backpressure.notify_all(); RunnerStep::Stop } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum RunnerStep { Idle, Continue, Stop, } fn step_from_drive_outcome(outcome: DriveOutcome) -> RunnerStep { match outcome { DriveOutcome::Idle => RunnerStep::Idle, DriveOutcome::Terminal => RunnerStep::Stop, DriveOutcome::Sent(_) | DriveOutcome::Acked { .. } | DriveOutcome::Rejected { .. } | DriveOutcome::Reconnected { .. } | DriveOutcome::ReconnectDelay { .. } | DriveOutcome::Progress => RunnerStep::Continue, } } fn check_store_error(store: &QwpWsPublicationStore) -> crate::Result<()> { store .check_durability() .map_err(driver_error_to_error_without_state)?; check_store_terminal_error(store) } fn check_store_terminal_error( store: &QwpWsPublicationStore, ) -> crate::Result<()> { if let Some(err) = store.terminal_error() { return Err(err.clone()); } if store.is_terminal() { return Err(error::fmt!(SocketError, "QWP/WebSocket sender is terminal")); } Ok(()) } fn driver_error_to_error_from_store( store: &QwpWsPublicationStore, err: DriverError, ) -> crate::Error { match err { DriverError::Terminal => store .terminal_error() .cloned() .unwrap_or_else(|| error::fmt!(SocketError, "QWP/WebSocket sender is terminal")), DriverError::SubmitTimedOut { backpressure } => { submit_timeout_error(backpressure, store.reconnect_failure()) } err => driver_error_to_error_without_state(err), } } fn runner_reconnect_deadline_expired(deadline: Option) -> bool { deadline.is_some_and(|deadline| Instant::now() >= deadline) } fn backpressure_deadline_expired(deadline: Option) -> bool { deadline.is_some_and(|deadline| Instant::now() >= deadline) } fn sleep_before_runner_reconnect( deadline: Option, backoff: Duration, stop: &AtomicBool, ) -> bool { if backoff.is_zero() { return !stop.load(Ordering::Acquire) && !runner_reconnect_deadline_expired(deadline); } let sleep_for = match deadline { Some(deadline) => { let remaining = deadline.saturating_duration_since(Instant::now()); if remaining.is_zero() { return false; } backoff.min(remaining) } None => backoff, }; let sleep_started = Instant::now(); while !stop.load(Ordering::Acquire) { let remaining = sleep_for.saturating_sub(sleep_started.elapsed()); if remaining.is_zero() { break; } thread::sleep(remaining.min(Duration::from_millis(50))); } !stop.load(Ordering::Acquire) && !runner_reconnect_deadline_expired(deadline) } impl Drop for SyncQwpWsRunner { fn drop(&mut self) { self.stop.store(true, Ordering::Release); if let Err(err) = self.traffic_gate.shutdown() { log::warn!("could not shut down QWP/WebSocket runner traffic: {err}"); } if let Some(thread) = self.thread.take() { if wait_for_runner_exit(&thread, self.shutdown_timeout) { if thread.join().is_err() { log::error!("QWP/WebSocket runner thread panicked during shutdown"); } } else { log::error!( "QWP/WebSocket runner did not stop within {:?}; detaching it while its \ publication store and slot lock remain retained by the worker", self.shutdown_timeout ); } } } } fn wait_for_runner_exit(thread: &thread::JoinHandle<()>, timeout: Duration) -> bool { let started = Instant::now(); while !thread.is_finished() { let remaining = timeout.saturating_sub(started.elapsed()); if remaining.is_zero() { return false; } thread::sleep(remaining.min(QWP_WS_RUNNER_SHUTDOWN_POLL)); } true } fn open_configured_qwp_ws_queue(qwp_ws: &QwpWsConfig) -> crate::Result { if matches!( *qwp_ws.sf_durability, SfDurability::Flush | SfDurability::Append ) { let durability = qwp_ws.sf_durability.as_conf_value(); return Err(error::fmt!( ConfigError, "sf_durability={durability} is not yet supported (use sf_durability=memory or periodic)" )); } let max_bytes = usize_from_config("sf_max_total_bytes", qwp_ws.sf_max_total_bytes())?; let periodic_sync_interval = qwp_ws.periodic_sync_interval(); if let Some(sf_dir) = qwp_ws.sf_dir.as_ref() { return SfaSlotQueue::open(SfaSlotOptions { sf_dir: sf_dir.clone(), sender_id: qwp_ws.sender_id.to_string(), segment_size_bytes: *qwp_ws.sf_max_segment_bytes, max_bytes, periodic_sync_interval, }) .map_err(|err| match err { SfaQueueError::SlotInUse { slot_dir, holder } => crate::Error::new( if qwp_ws.pool_managed_slot { crate::ErrorCode::ConfigError } else { crate::ErrorCode::SocketError }, format!( "QWP/WebSocket store-and-forward slot is already in use \ [slot={}, holder={}]. Another process or pool holds this \ slot; use a unique sender_id per producer.", slot_dir.display(), holder ), ), // Intentionally propagate once; the next construction revalidates // the healed chain. SfaQueueError::SanitizedResidue { path } => error::fmt!( SocketError, "QWP/WebSocket store-and-forward recovery repaired sealed residue at {}; retry sender construction once. A repeated recovery error requires operator intervention.", path.display() ), err => error::fmt!( SocketError, "Could not open QWP/WebSocket Store-and-Forward queue: {:?}", err ), }); } if periodic_sync_interval.is_some() { return Err(error::fmt!( ConfigError, "sf_durability=periodic requires sf_dir" )); } SfaSlotQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: *qwp_ws.sf_max_segment_bytes, max_bytes, }) .map_err(|err| { error::fmt!( ConfigError, "Invalid QWP/WebSocket memory SFA queue configuration: {:?}", err ) }) } fn usize_from_config(name: &str, value: u64) -> crate::Result { usize::try_from(value).map_err(|_| { error::fmt!( ConfigError, "{name} value is too large for this platform [value={value}]" ) }) } // ---------- minimal SHA-1 for Sec-WebSocket-Accept ---------- // ---------- frame I/O ---------- fn random_mask() -> [u8; 4] { let mut mask = [0u8; 4]; rand::rng().fill_bytes(&mut mask); mask } /// Write a single binary frame (FIN=1, opcode=0x2). Mask-bit set as required for /// client → server frames. `out` is a scratch buffer; the frame is then written /// to `stream` in one or more `write_all` calls. pub(crate) fn write_binary_frame( stream: &mut W, out: &mut Vec, payload: &[u8], ) -> std::io::Result<()> { codec::write_frame_to_buf(out, Opcode::Binary, payload, random_mask()); stream.write_all(out) } pub(crate) fn write_ping_frame( stream: &mut W, out: &mut Vec, payload: &[u8], ) -> std::io::Result<()> { codec::write_frame_to_buf(out, Opcode::Ping, payload, random_mask()); stream.write_all(out) } /// Read one full WebSocket message into `out`. Reassembles fragmented frames. /// Replies to PING with PONG and treats CLOSE as an error. Returns the opcode /// of the first data frame (text/binary). #[cfg(test)] pub(crate) fn read_message( stream: &mut S, scratch: &mut Vec, out: &mut Vec, ) -> crate::Result { read_message_with_close(stream, scratch, out).map_err(WsMessageError::into_error) } #[derive(Debug)] pub(crate) struct WsCloseFrame { pub(crate) code: Option, pub(crate) reason: String, } impl WsCloseFrame { pub(crate) fn is_orderly(&self) -> bool { matches!(self.code, Some(1000 | 1001)) } pub(crate) fn into_error(self) -> crate::Error { error::fmt!( SocketError, "WebSocket connection closed by server{}", self.display_suffix() ) } fn display_suffix(&self) -> String { match (self.code, self.reason.is_empty()) { (Some(code), true) => format!(" (code={code})"), (Some(code), false) => format!(" (code={code}, reason={})", self.reason), (None, _) => String::new(), } } } #[derive(Debug)] pub(crate) enum WsMessageError { Close(WsCloseFrame), ProtocolViolation(String), Error(crate::Error), } impl WsMessageError { #[cfg(test)] pub(crate) fn into_error(self) -> crate::Error { match self { Self::Close(close) => close.into_error(), Self::ProtocolViolation(reason) => { error::fmt!(SocketError, "WebSocket protocol violation: {reason}") } Self::Error(err) => err, } } } #[cfg(test)] pub(crate) fn read_message_with_close( stream: &mut S, scratch: &mut Vec, out: &mut Vec, ) -> Result { out.clear(); let mut first_opcode: Option = None; let mut control_payload = [0u8; MAX_CONTROL_FRAME_PAYLOAD_BYTES]; loop { let header = read_frame_header(stream)?; match header.opcode { OPCODE_PING => { let payload = read_control_frame_payload(stream, header, &mut control_payload)?; codec::write_frame_to_buf(scratch, Opcode::Pong, payload, random_mask()); stream.write_all(scratch).map_err(|io| { WsMessageError::Error(error::fmt!( SocketError, "Could not send WebSocket PONG: {}", io )) })?; continue; } OPCODE_PONG => { read_control_frame_payload(stream, header, &mut control_payload)?; continue; } OPCODE_CLOSE => { let payload = read_control_frame_payload(stream, header, &mut control_payload)?; return match codec::parse_ws_close_payload(payload) { Ok((code, reason)) => Err(WsMessageError::Close(WsCloseFrame { code, reason })), Err(reason) => Err(WsMessageError::ProtocolViolation(reason)), }; } OPCODE_TEXT | OPCODE_BINARY => { if first_opcode.is_some() { return Err(WsMessageError::ProtocolViolation( "Unexpected new data frame mid-message".to_string(), )); } first_opcode = Some(header.opcode); read_payload_into(stream, out, header.payload_len)?; } OPCODE_CONTINUATION => { if first_opcode.is_none() { return Err(WsMessageError::ProtocolViolation( "Continuation frame without prior data frame".to_string(), )); } read_payload_into(stream, out, header.payload_len)?; } other => { return Err(WsMessageError::ProtocolViolation(format!( "Unknown WebSocket opcode: 0x{:x}", other ))); } } if header.fin { return Ok(first_opcode.unwrap()); } } } pub(crate) struct WsFrameReader { input: Vec, read_pos: usize, fragment_opcode: Option, message: Vec, #[cfg(test)] max_message_bytes: usize, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum WsFrameRead { Message { opcode: u8 }, Progress, Idle, } impl WsFrameReader { pub(crate) fn new() -> Self { Self { input: Vec::with_capacity(16 * 1024), read_pos: 0, fragment_opcode: None, message: Vec::with_capacity(16 * 1024), #[cfg(test)] max_message_bytes: MAX_INBOUND_MESSAGE_BYTES, } } pub(crate) fn with_initial_input(input: Vec) -> Self { let mut reader = Self::new(); if !input.is_empty() { reader.input = input; } reader } #[cfg(test)] fn with_max_message_bytes_for_test(max_message_bytes: usize) -> Self { let mut reader = Self::new(); reader.max_message_bytes = max_message_bytes; reader } pub(crate) fn message(&self) -> &[u8] { &self.message } pub(crate) fn clear_message(&mut self) { self.message.clear(); } pub(crate) fn try_read_one( &mut self, stream: &mut WsStream, scratch: &mut Vec, ) -> Result { match self.try_parse_buffered_one(stream, scratch)? { WsFrameRead::Idle => {} read => return Ok(read), } let mut read_buf = [0u8; 8192]; match stream.read_nonblocking_once(&mut read_buf) { Ok(0) => { return Err(WsMessageError::Error(error::fmt!( SocketError, "WebSocket connection closed by server" ))); } Ok(n) => self.input.extend_from_slice(&read_buf[..n]), Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { return Ok(WsFrameRead::Idle); } Err(err) => { return Err(WsMessageError::Error(error::fmt!( SocketError, "Could not read WebSocket frame: {}", err ))); } } self.try_parse_buffered_one(stream, scratch) } #[cfg(test)] fn append_input_for_test(&mut self, input: &[u8]) { self.input.extend_from_slice(input); } #[cfg(test)] fn try_read_buffered_for_test( &mut self, writer: &mut W, scratch: &mut Vec, ) -> Result { self.try_parse_buffered_one(writer, scratch) } fn try_parse_buffered_one( &mut self, writer: &mut W, scratch: &mut Vec, ) -> Result { let Some(header) = self.peek_buffered_frame_header()? else { return Ok(WsFrameRead::Idle); }; match header.opcode { OPCODE_PING => { let payload = self.payload_slice(header); codec::write_frame_to_buf(scratch, Opcode::Pong, payload, random_mask()); writer.write_all(scratch).map_err(|io| { WsMessageError::Error(error::fmt!( SocketError, "Could not send WebSocket PONG: {}", io )) })?; self.consume_frame(header.frame_end); Ok(WsFrameRead::Progress) } OPCODE_PONG => { self.consume_frame(header.frame_end); Ok(WsFrameRead::Progress) } OPCODE_CLOSE => { let parse_result = codec::parse_ws_close_payload(self.payload_slice(header)); self.consume_frame(header.frame_end); match parse_result { Ok((code, reason)) => Err(WsMessageError::Close(WsCloseFrame { code, reason })), Err(reason) => Err(WsMessageError::ProtocolViolation(reason)), } } OPCODE_TEXT | OPCODE_BINARY => { if self.fragment_opcode.is_some() { return Err(WsMessageError::ProtocolViolation( "Unexpected new data frame mid-message".to_string(), )); } self.message.clear(); self.append_message_payload(header)?; self.consume_frame(header.frame_end); if header.fin { Ok(WsFrameRead::Message { opcode: header.opcode, }) } else { self.fragment_opcode = Some(header.opcode); Ok(WsFrameRead::Progress) } } OPCODE_CONTINUATION => { let Some(opcode) = self.fragment_opcode else { return Err(WsMessageError::ProtocolViolation( "Continuation frame without prior data frame".to_string(), )); }; self.append_message_payload(header)?; self.consume_frame(header.frame_end); if header.fin { self.fragment_opcode = None; Ok(WsFrameRead::Message { opcode }) } else { Ok(WsFrameRead::Progress) } } other => Err(WsMessageError::ProtocolViolation(format!( "Unknown WebSocket opcode: 0x{:x}", other ))), } } fn peek_buffered_frame_header(&self) -> Result, WsMessageError> { let available = self.input.len().saturating_sub(self.read_pos); if available < 2 { return Ok(None); } let frame = &self.input[self.read_pos..]; let fin = (frame[0] & 0x80) != 0; let opcode = frame[0] & 0x0F; if frame[0] & WS_RSV_MASK != 0 { return Err(WsMessageError::ProtocolViolation( "WebSocket RSV bits are set but no extensions were negotiated".to_string(), )); } let masked = (frame[1] & 0x80) != 0; if masked { return Err(WsMessageError::ProtocolViolation( "WebSocket server frame must not be masked".to_string(), )); } let len_short = frame[1] & 0x7F; let (header_len, payload_len) = match len_short { 126 => { if available < 4 { return Ok(None); } (4, u16::from_be_bytes([frame[2], frame[3]]) as u64) } 127 => { if available < 10 { return Ok(None); } ( 10, u64::from_be_bytes([ frame[2], frame[3], frame[4], frame[5], frame[6], frame[7], frame[8], frame[9], ]), ) } n => (2, n as u64), }; validate_payload_length_encoding(len_short, payload_len)?; if payload_len > MAX_INBOUND_FRAME_BYTES { return Err(WsMessageError::ProtocolViolation(format!( "WebSocket frame too large: {} bytes", payload_len ))); } let payload_len = usize::try_from(payload_len).map_err(|_| { WsMessageError::ProtocolViolation(format!( "WebSocket frame too large for this platform: {} bytes", payload_len )) })?; validate_control_frame_header(fin, opcode, payload_len)?; let payload_start = self.read_pos + header_len; let frame_end = payload_start.checked_add(payload_len).ok_or_else(|| { WsMessageError::ProtocolViolation("WebSocket frame too large".to_string()) })?; if self.input.len() < frame_end { return Ok(None); } Ok(Some(BufferedFrameHeader { fin, opcode, payload_start, frame_end, })) } fn payload_slice(&self, header: BufferedFrameHeader) -> &[u8] { &self.input[header.payload_start..header.frame_end] } fn append_message_payload( &mut self, header: BufferedFrameHeader, ) -> Result<(), WsMessageError> { let payload = &self.input[header.payload_start..header.frame_end]; let new_len = checked_message_len(self.message.len(), payload.len(), self.max_message_bytes())?; self.message.extend_from_slice(payload); debug_assert_eq!(self.message.len(), new_len); Ok(()) } fn max_message_bytes(&self) -> usize { #[cfg(test)] { self.max_message_bytes } #[cfg(not(test))] { MAX_INBOUND_MESSAGE_BYTES } } fn consume_frame(&mut self, frame_end: usize) { self.read_pos = frame_end; if self.read_pos == self.input.len() { self.input.clear(); self.read_pos = 0; } else if self.read_pos >= 4096 && self.read_pos * 2 >= self.input.len() { self.input.drain(..self.read_pos); self.read_pos = 0; } } } #[derive(Debug, Clone, Copy)] struct BufferedFrameHeader { fin: bool, opcode: u8, payload_start: usize, frame_end: usize, } #[derive(Debug, Clone, Copy)] #[cfg(test)] struct FrameHeader { fin: bool, opcode: u8, payload_len: usize, } const MAX_CONTROL_FRAME_PAYLOAD_BYTES: usize = 125; const MAX_INBOUND_MESSAGE_BYTES: usize = MAX_INBOUND_FRAME_BYTES as usize; const WS_RSV_MASK: u8 = 0x70; fn checked_message_len( current_len: usize, payload_len: usize, max_message_bytes: usize, ) -> Result { let new_len = current_len.checked_add(payload_len).ok_or_else(|| { WsMessageError::ProtocolViolation("WebSocket message too large".to_string()) })?; if new_len > max_message_bytes { return Err(WsMessageError::ProtocolViolation(format!( "WebSocket message too large: {} bytes", new_len ))); } Ok(new_len) } fn validate_payload_length_encoding(len_short: u8, payload_len: u64) -> Result<(), WsMessageError> { match len_short { 126 if payload_len <= 125 => Err(WsMessageError::ProtocolViolation(format!( "WebSocket frame length uses non-minimal 16-bit encoding: {} bytes", payload_len ))), 127 if payload_len <= 0xffff => Err(WsMessageError::ProtocolViolation(format!( "WebSocket frame length uses non-minimal 64-bit encoding: {} bytes", payload_len ))), _ => Ok(()), } } fn validate_control_frame_header( fin: bool, opcode: u8, payload_len: usize, ) -> Result<(), WsMessageError> { if !matches!(opcode, OPCODE_PING | OPCODE_PONG | OPCODE_CLOSE) { return Ok(()); } if !fin { return Err(WsMessageError::ProtocolViolation( "WebSocket control frame must not be fragmented".to_string(), )); } if payload_len > MAX_CONTROL_FRAME_PAYLOAD_BYTES { return Err(WsMessageError::ProtocolViolation(format!( "WebSocket control frame too large: {} bytes", payload_len ))); } if opcode == OPCODE_CLOSE && payload_len == 1 { return Err(WsMessageError::ProtocolViolation( "WebSocket close frame payload length must be 0 or at least 2 bytes".to_string(), )); } Ok(()) } #[cfg(test)] fn read_frame_header(stream: &mut R) -> Result { let mut hdr = [0u8; 2]; read_exact_io(stream, &mut hdr, "WebSocket frame header").map_err(WsMessageError::Error)?; let fin = (hdr[0] & 0x80) != 0; let opcode = hdr[0] & 0x0F; if hdr[0] & WS_RSV_MASK != 0 { return Err(WsMessageError::ProtocolViolation( "WebSocket RSV bits are set but no extensions were negotiated".to_string(), )); } let masked = (hdr[1] & 0x80) != 0; if masked { return Err(WsMessageError::ProtocolViolation( "WebSocket server frame must not be masked".to_string(), )); } let len_short = hdr[1] & 0x7F; let payload_len: u64 = match len_short { 126 => { let mut b = [0u8; 2]; read_exact_io(stream, &mut b, "WebSocket frame length") .map_err(WsMessageError::Error)?; u16::from_be_bytes(b) as u64 } 127 => { let mut b = [0u8; 8]; read_exact_io(stream, &mut b, "WebSocket frame length") .map_err(WsMessageError::Error)?; u64::from_be_bytes(b) } n => n as u64, }; validate_payload_length_encoding(len_short, payload_len)?; if payload_len > MAX_INBOUND_FRAME_BYTES { return Err(WsMessageError::ProtocolViolation(format!( "WebSocket frame too large: {} bytes", payload_len ))); } let payload_len = usize::try_from(payload_len).map_err(|_| { WsMessageError::ProtocolViolation(format!( "WebSocket frame too large for this platform: {} bytes", payload_len )) })?; validate_control_frame_header(fin, opcode, payload_len)?; Ok(FrameHeader { fin, opcode, payload_len, }) } #[cfg(test)] fn read_payload_into( stream: &mut R, out: &mut Vec, payload_len: usize, ) -> Result<(), WsMessageError> { let start = out.len(); let end = checked_message_len(start, payload_len, MAX_INBOUND_MESSAGE_BYTES)?; out.resize(end, 0); if payload_len > 0 { read_exact_io(stream, &mut out[start..end], "WebSocket frame payload") .map_err(WsMessageError::Error)?; } Ok(()) } #[cfg(test)] fn read_control_frame_payload<'a, R: Read>( stream: &mut R, header: FrameHeader, payload: &'a mut [u8; MAX_CONTROL_FRAME_PAYLOAD_BYTES], ) -> Result<&'a [u8], WsMessageError> { let payload = &mut payload[..header.payload_len]; if !payload.is_empty() { read_exact_io(stream, payload, "WebSocket control frame payload") .map_err(WsMessageError::Error)?; } Ok(payload) } #[cfg(test)] fn read_exact_io(stream: &mut R, buf: &mut [u8], what: &str) -> crate::Result<()> { stream .read_exact(buf) .map_err(|io| error::fmt!(SocketError, "Could not read {}: {}", what, io)) } // ---------- HTTP/1.1 upgrade ---------- // // The actual RFC 6455 §4 client handshake (request build, response read, // Sec-WebSocket-Accept validation) lives in `crate::ws::handshake`. The // connect paths below drive `crate::ws::handshake::upgrade` directly and // then apply the QWP-specific overlay (X-QWP-Version negotiation, // durable-ack echo, role-reject classification) via the helpers in // `codec::{qwp_extra_headers, validate_qwp_handshake_headers, // handshake_error_to_ingress}`. /// Test-only convenience wrapper used by the probes in `crate::tests::qwp_ws_*`: /// the inline upgrade sequence the connect paths use below, in a single call, /// without the extras-builder + validate-headers + error-mapper boilerplate. Returns /// the version, the bytes read past the response, and the server's frame limit (0: none). #[cfg(all(test, feature = "_sender-http"))] #[allow(clippy::too_many_arguments)] pub(crate) fn perform_upgrade( stream: &mut S, host_header: &str, auth_header: Option<&str>, max_version: u32, client_id: Option<&str>, request_durable_ack: DurableAckTiers, ) -> crate::Result<(u8, Vec, usize)> { let extras = codec::qwp_extra_headers(auth_header, max_version, client_id, request_durable_ack); let handshake = crate::ws::handshake::upgrade(stream, host_header, codec::WS_PATH, &extras) .map_err(codec::handshake_error_to_ingress)?; let result = codec::validate_qwp_handshake_headers( &handshake.headers, max_version, request_durable_ack, )?; let frame_limit = result.server_max_batch_size; Ok((result.version, handshake.leftover, frame_limit)) } // ---------- connect ---------- fn complete_qwp_ws_tls_handshake( conn: &mut rustls::ClientConnection, tcp: &mut NoSigpipeTcp, tls_timeout: Duration, ) -> crate::Result<()> { while conn.wants_write() || conn.is_handshaking() { conn.complete_io(tcp).map_err(|io| { if matches!(io.kind(), ErrorKind::TimedOut | ErrorKind::WouldBlock) { error::fmt!( TlsError, "Failed to complete TLS handshake: timed out waiting for server response after {:?}.", tls_timeout ) } else { error::fmt!(TlsError, "Failed to complete TLS handshake: {}", io) } })?; } Ok(()) } fn resolve_qwp_ws_addrs(host: &str, port: &str) -> crate::Result> { use std::net::ToSocketAddrs; let port = port .parse::() .map_err(|_| error::fmt!(ConfigError, "Invalid port: {:?}", port))?; let addrs = (host, port) .to_socket_addrs() .map_err(|io| { error::fmt!( CouldNotResolveAddr, "Could not resolve {}:{}: {}", host, port, io ) })? .collect::>(); if addrs.is_empty() { return Err(error::fmt!( CouldNotResolveAddr, "No address found for {}:{}", host, port )); } Ok(addrs) } fn connect_qwp_ws_tcp( host: &str, port: &str, request_timeout: Duration, connect_timeout: Option, traffic_gate: Option<&TrafficGate>, ) -> crate::Result { let addrs = resolve_qwp_ws_addrs(host, port)?; connect_tcp_to_any_addr( host, port, &addrs, request_timeout, connect_timeout, traffic_gate, ) } fn connect_tcp_to_any_addr( host: &str, port: &str, addrs: &[SocketAddr], request_timeout: Duration, connect_timeout: Option, traffic_gate: Option<&TrafficGate>, ) -> crate::Result { let mut failures = Vec::new(); // Stays true only while *every* attempted address failed specifically with // a connect timeout — lets us surface a distinct, retryable `ConnectTimeout` // rather than burying it under the generic `SocketError`. let mut all_timed_out = true; for addr in addrs { // A dial in flight cannot be interrupted (the fd only reaches the gate // after connect returns), so the timeout is per address: stop between // addresses once shutdown is requested rather than finishing the walk. if traffic_gate.is_some_and(TrafficGate::is_shutdown) { return Err(error::fmt!( SocketError, "QWP/WebSocket runner is shutting down during connect" )); } // `connect_timeout = None` keeps the OS-default blocking dial; `Some` // uses the native non-blocking connect + poll + SO_ERROR check that // `connect_timeout` implements per platform, bounded per address. let res = match connect_timeout { Some(d) => TcpStream::connect_timeout(addr, d), None => TcpStream::connect(addr), }; match res { Ok(tcp) => { tcp.set_nodelay(true).ok(); tcp.set_read_timeout(Some(request_timeout)).ok(); tcp.set_write_timeout(Some(request_timeout)).ok(); // macOS only: its default send buffer (~128KB, autotune ramp) // is smaller than a typical QWP chunk, so `write_all` blocks // mid-frame; an explicit 4MiB is honored there (maxsockbuf // 8MiB) and skips the ramp. On Linux an explicit SO_SNDBUF / // SO_RCVBUF is silently clamped to net.core.{w,r}mem_max // (208KiB stock, granted 2x = ~416KiB) and DISABLES kernel // autotuning, which left alone grows the buffers to // tcp_{w,r}mem[2] (>= the 4MiB requested here) — the explicit // set caps per-connection throughput at ~426KB/RTT on any // real-latency path. #[cfg(target_os = "macos")] { let sock = socket2::SockRef::from(&tcp); sock.set_send_buffer_size(4 * 1024 * 1024).ok(); sock.set_recv_buffer_size(4 * 1024 * 1024).ok(); } match NoSigpipeTcp::new(tcp) { Ok(wrapped) => return Ok(wrapped), Err(err) => { all_timed_out = false; failures.push(format!("{addr}: SO_NOSIGPIPE setup failed: {err}")); continue; } } } Err(io) => { if io.kind() != std::io::ErrorKind::TimedOut { all_timed_out = false; } failures.push(format!("{addr}: {io}")); } } } let msg = format!( "Could not connect to {}:{}; tried {}", host, port, failures.join(", ") ); // A dial that burned its `connect_timeout` on every candidate surfaces as a // distinct `ConnectTimeout` (still retryable in the reconnect loop) so // callers can tell a timed-out dial apart from refused / reset. Err(if connect_timeout.is_some() && all_timed_out { error::fmt!(ConnectTimeout, "{}", msg) } else { error::fmt!(SocketError, "{}", msg) }) } #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum QwpWsHostState { Healthy, HealthUnknown, TransientReject, FailedThisRound, RoleRejected, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum QwpWsZoneTier { Same, Unknown, Other, } #[derive(Debug, Clone)] pub(crate) struct QwpWsHostHealthTracker { states: Vec, attempted_this_round: Vec, zone_tiers: Vec, last_success_epoch: Vec, success_epoch: u64, } impl QwpWsHostHealthTracker { pub(crate) fn new(host_count: usize) -> Self { assert!(host_count > 0, "host_count must be > 0"); Self { states: vec![QwpWsHostState::HealthUnknown; host_count], attempted_this_round: vec![false; host_count], zone_tiers: vec![QwpWsZoneTier::Same; host_count], last_success_epoch: vec![0; host_count], success_epoch: 0, } } pub(super) fn begin_round(&mut self, forget_classifications: bool) { let mut sticky_idx = None; if forget_classifications { let mut best_epoch = 0; for idx in 0..self.states.len() { if self.states[idx] == QwpWsHostState::Healthy && self.zone_tiers[idx] == QwpWsZoneTier::Same && self.last_success_epoch[idx] > best_epoch { best_epoch = self.last_success_epoch[idx]; sticky_idx = Some(idx); } } } self.attempted_this_round.fill(false); if forget_classifications { for idx in 0..self.states.len() { if Some(idx) != sticky_idx { self.states[idx] = QwpWsHostState::HealthUnknown; } } } } pub(super) fn is_round_exhausted(&self) -> bool { self.attempted_this_round.iter().all(|attempted| *attempted) } pub(super) fn pick_next(&self) -> Option { const STATE_ORDER: [QwpWsHostState; 5] = [ QwpWsHostState::Healthy, QwpWsHostState::HealthUnknown, QwpWsHostState::TransientReject, QwpWsHostState::FailedThisRound, QwpWsHostState::RoleRejected, ]; const ZONE_ORDER: [QwpWsZoneTier; 3] = [ QwpWsZoneTier::Same, QwpWsZoneTier::Unknown, QwpWsZoneTier::Other, ]; for state in STATE_ORDER { for zone in ZONE_ORDER { for idx in 0..self.states.len() { if !self.attempted_this_round[idx] && self.states[idx] == state && self.zone_tiers[idx] == zone { return Some(idx); } } } } None } /// Like [`Self::pick_next`] but also marks the chosen endpoint /// `attempted_this_round` before returning. The pool drives the connect /// round through [`QwpWsHealthAccess`], releasing the health lock during /// the blocking `establish_connection`; claiming the endpoint while the /// lock is still held means concurrent borrows rotate to a *different* /// peer instead of piling onto the same slow / black-holed endpoint. The /// real outcome is still recorded by the matching `record_*` call once the /// attempt finishes (those also set `attempted_this_round`, so the early /// claim is idempotent). pub(super) fn pick_next_and_claim(&mut self) -> Option { let idx = self.pick_next()?; self.attempted_this_round[idx] = true; Some(idx) } pub(super) fn pick_next_and_claim_connect_round( &mut self, reset_if_exhausted: bool, ) -> Option { if reset_if_exhausted && self.is_round_exhausted() { self.begin_round(true); } self.pick_next_and_claim() } pub(super) fn record_success(&mut self, idx: usize) { self.states[idx] = QwpWsHostState::Healthy; self.attempted_this_round[idx] = true; self.success_epoch = self.success_epoch.saturating_add(1); self.last_success_epoch[idx] = self.success_epoch; } pub(super) fn record_transport_error(&mut self, idx: usize) { self.states[idx] = QwpWsHostState::FailedThisRound; self.attempted_this_round[idx] = true; } pub(super) fn record_role_reject(&mut self, idx: usize, transient: bool) { self.states[idx] = if transient { QwpWsHostState::TransientReject } else { QwpWsHostState::RoleRejected }; self.attempted_this_round[idx] = true; } pub(crate) fn record_mid_stream_failure( &mut self, idx: usize, reason: Option, ) { if reason == Some(ReconnectReason::NotWritable) { self.states[idx] = QwpWsHostState::RoleRejected; } else if self.states[idx] == QwpWsHostState::Healthy { self.states[idx] = QwpWsHostState::FailedThisRound; } } pub(super) fn record_zone(&mut self, _idx: usize, _zone: Option<&str>) { // Rust is still client-zone-blind, which matches the spec's zone-unset // path: every endpoint remains in the Same tier. } } /// How [`connect_qwp_ws_endpoint_round`] reaches the health tracker. /// /// Every tracker mutation/read goes through [`Self::with_tracker`], so an /// implementation backed by a `Mutex` (the connection pool) acquires and /// releases the lock *per operation*. The blocking `establish_connection` /// between those operations therefore runs with **no** health lock held — /// the fix for the pool's head-of-line blocking, where a single slow / /// black-holed connect used to stall every other borrow and every dead-sender /// return that needed the same lock. Single-connection drivers that own the /// tracker by value implement this as a direct in-place mutation with no /// locking. pub(crate) trait QwpWsHealthAccess { fn with_tracker(&mut self, f: impl FnOnce(&mut QwpWsHostHealthTracker) -> R) -> R; } /// Owned-tracker accessor: the single-connection background / SFA / reconnect /// drivers hold the tracker by value, so there is no lock to take. impl QwpWsHealthAccess for &mut QwpWsHostHealthTracker { fn with_tracker(&mut self, f: impl FnOnce(&mut QwpWsHostHealthTracker) -> R) -> R { f(self) } } /// Pool accessor: locks the shared tracker for the duration of a single /// tracker operation only, never across the blocking connect. pub(crate) struct LockedQwpWsHealth<'a> { mutex: &'a Mutex, } impl<'a> LockedQwpWsHealth<'a> { pub(crate) fn new(mutex: &'a Mutex) -> Self { Self { mutex } } } impl QwpWsHealthAccess for LockedQwpWsHealth<'_> { fn with_tracker(&mut self, f: impl FnOnce(&mut QwpWsHostHealthTracker) -> R) -> R { // Poison-tolerant like the pool's other lock helpers: a panic in // another thread's short locked region must not turn every subsequent // borrow/return into a panic. let mut guard = self.mutex.lock().unwrap_or_else(|e| e.into_inner()); f(&mut guard) } } pub(crate) struct QwpWsConnectRoundSuccess { pub(crate) endpoint_idx: usize, pub(crate) stream: WsStream, pub(crate) negotiated_version: u8, pub(crate) server_max_batch_size: usize, pub(crate) leftover: Vec, } pub(crate) fn qwp_ws_configured_endpoints( host: &str, port: &str, qwp_ws: &QwpWsConfig, ) -> Arc<[QwpWsEndpoint]> { if qwp_ws.endpoints.is_empty() { Arc::from([QwpWsEndpoint::new(host.to_string(), port.to_string())]) } else { Arc::from(qwp_ws.endpoints.to_vec()) } } pub(crate) fn is_qwp_ws_role_reject_error(err: &crate::Error) -> bool { err.qwp_ws_role_reject().is_some() } fn qwp_ws_role_reject_is_transient(err: &crate::Error) -> bool { err.qwp_ws_role_reject() .is_some_and(|role_reject| role_reject.is_transient()) } fn qwp_ws_role_reject_zone(err: &crate::Error) -> Option<&str> { err.qwp_ws_role_reject() .and_then(|role_reject| role_reject.zone.as_deref()) } fn qwp_ws_all_endpoints_unreachable_error( endpoints: &[QwpWsEndpoint], last_failure: Option<(usize, crate::Error)>, ) -> crate::Error { match last_failure { Some((idx, err)) => { let msg = format!( "QWP/WebSocket all endpoints unreachable; last endpoint {}:{} failed: {}", endpoints[idx].host, endpoints[idx].port, err ); err.reclassified(crate::ErrorCode::SocketError, msg) } None => error::fmt!(SocketError, "QWP/WebSocket all endpoints unreachable"), } } /// Establish a fresh QWP/WebSocket connection: TCP → optional TLS → HTTP /// upgrade. Returns the connected stream, the version the server picked, and any /// WebSocket bytes read after the HTTP upgrade response. #[allow(clippy::too_many_arguments)] pub(crate) fn establish_connection( host: &str, port: &str, use_tls: bool, tls_settings: Option, connect_kind: QwpWsConnectKind, qwp_ws: &QwpWsConfig, auth_header: Option<&str>, traffic_gate: Option<&TrafficGate>, ) -> crate::Result<(WsStream, codec::QwpWsHandshakeResult, Vec)> { let auth_timeout = *qwp_ws.auth_timeout; let request_timeout = *qwp_ws.request_timeout; let connect_timeout = effective_connect_timeout(connect_kind.bounded_dial(), *qwp_ws.connect_timeout); // Windows safety invariant: while the gate stores tcp's raw socket, // traffic_registration must drop before tcp. Keep tcp in this outer scope // until every fallible TLS/WebSocket setup step has completed. let mut tcp = connect_qwp_ws_tcp(host, port, request_timeout, connect_timeout, traffic_gate)?; let traffic_registration = traffic_gate .map(|gate| gate.register(tcp.tcp())) .transpose() .map_err(|io| { error::fmt!( SocketError, "Could not register QWP/WebSocket traffic shutdown handle: {}", io ) })?; // RFC 9110: an IPv6 literal in a Host header must be bracketed. let header_host = if host.contains(':') { format!("[{host}]") } else { host.to_string() }; let host_header = if (use_tls && port == "443") || (!use_tls && port == "80") { header_host } else { format!("{header_host}:{port}") }; let max_version = *qwp_ws.max_protocol_version; let client_id = qwp_ws.client_id.as_deref(); let request_durable_ack = *qwp_ws.request_durable_ack; let (stream, handshake_result, leftover) = if use_tls { let tls = tls_settings.ok_or_else(|| { error::fmt!(ConfigError, "TLS settings missing for QWP/WebSocket Secure") })?; let cfg: Arc = configure_tls(tls)?; let server_name = host .to_string() .try_into() .map_err(|e| error::fmt!(TlsError, "Invalid TLS server name {:?}: {}", host, e))?; let mut conn = rustls::ClientConnection::new(cfg, server_name) .map_err(|e| error::fmt!(TlsError, "TLS handshake setup failed: {}", e))?; tcp.tcp() .set_read_timeout(Some(QWP_WS_TLS_HANDSHAKE_TIMEOUT)) .ok(); tcp.tcp() .set_write_timeout(Some(QWP_WS_TLS_HANDSHAKE_TIMEOUT)) .ok(); complete_qwp_ws_tls_handshake(&mut conn, &mut tcp, QWP_WS_TLS_HANDSHAKE_TIMEOUT)?; tcp.tcp().set_read_timeout(Some(request_timeout)).ok(); tcp.tcp().set_write_timeout(Some(request_timeout)).ok(); // The shared `upgrade()` does both the request write and the // response read in one call. Switch SO_RCVTIMEO to `auth_timeout` // first: the write happens immediately (doesn't depend on // read_timeout), and the response read is what auth_timeout bounds. tcp.tcp().set_read_timeout(Some(auth_timeout)).ok(); let extras = codec::qwp_extra_headers(auth_header, max_version, client_id, request_durable_ack); let mut tls_stream = rustls::Stream::new(&mut conn, &mut tcp); let handshake = crate::ws::handshake::upgrade(&mut tls_stream, &host_header, codec::WS_PATH, &extras) .map_err(codec::handshake_error_to_ingress)?; let handshake_result = codec::validate_qwp_handshake_headers( &handshake.headers, max_version, request_durable_ack, )?; let leftover = handshake.leftover; ( WsStream::Tls(Box::new(rustls::StreamOwned::new(conn, tcp))), handshake_result, leftover, ) } else { tcp.tcp().set_read_timeout(Some(auth_timeout)).ok(); let extras = codec::qwp_extra_headers(auth_header, max_version, client_id, request_durable_ack); let handshake = crate::ws::handshake::upgrade(&mut tcp, &host_header, codec::WS_PATH, &extras) .map_err(codec::handshake_error_to_ingress)?; let handshake_result = codec::validate_qwp_handshake_headers( &handshake.headers, max_version, request_durable_ack, )?; let leftover = handshake.leftover; (WsStream::Plain(tcp), handshake_result, leftover) }; stream .set_timeouts(Some(request_timeout), Some(request_timeout)) .ok(); if let Some(registration) = traffic_registration { registration.keep(); } Ok((stream, handshake_result, leftover)) } /// Whether a provider acquisition failed only because the transport's own /// traffic gate shut down (the user closed or dropped the sender): a normal /// close, not a credential outage, so it must not be narrated as one. fn provider_cancelled_by_shutdown(err: &crate::Error, traffic_gate: Option<&TrafficGate>) -> bool { traffic_gate.is_some_and(TrafficGate::is_shutdown) && crate::token_provider::is_provider_shutdown_error(err) } fn acquire_qwp_ws_provider_header( provider: &crate::token_provider::TokenProvider, connect_kind: QwpWsConnectKind, traffic_gate: Option<&TrafficGate>, ) -> crate::Result { // Without a traffic gate (manual progress) the pull runs on the thread // driving progress, which may be an ACK wait inside one of the auth's // callbacks: record a failed pull there so that wait can be rejected. match traffic_gate { Some(gate) if connect_kind.bounded_dial() => { provider.bearer_header_isolated_until(|| gate.is_shutdown()) } // A manual-progress reconnect, or a manual orphan drainer's connect, // runs inside the caller's `drive_once`, `wait`, `close_drain` or a // `flush` waiting for queue space. Their deadlines are checked only // between attempts, so a refreshing provider's cold wait (30 s by // default) would overrun them. Fail the attempt at once instead: the // provider's refresher keeps fetching, and a later attempt pulls again. None if connect_kind.bounded_dial() => provider.bearer_header_on_caller_without_waiting(), _ => provider.bearer_header_on_caller(), } } #[allow(clippy::too_many_arguments)] pub(crate) fn connect_qwp_ws_endpoint_round( endpoints: &Arc<[QwpWsEndpoint]>, mut health: A, previous_idx: &mut Option, previous_failure: Option, use_tls: bool, tls_settings: Option, connect_kind: QwpWsConnectKind, qwp_ws: &QwpWsConfig, auth_header: Option<&str>, events: Option<&crate::ingress::conn_events::ConnectionEventSource>, traffic_gate: Option<&TrafficGate>, ) -> crate::Result { // Background (orphan-drainer) walks never narrate: reporting their // failures would claim an outage against an endpoint the foreground // may be healthily using. Mirrors the Java sender's `if (!background)` // guards; health tracking is recorded either way. let events = if connect_kind.is_background() { None } else { events }; if let Some(idx) = previous_idx.take() { health.with_tracker(|t| t.record_mid_stream_failure(idx, previous_failure)); } // A token provider (e.g. OIDC) is pulled fresh on every (re)connect round, // overriding the static basic/token header, so a long-lived sender keeps a // valid Bearer as the token rotates. Provider acquisition/validation failures // are normally retryable SocketErrors: the next invocation may recover, and // accepted store-and-forward frames must remain drainable. An InvalidApiCall // from the callback is a caller-contract violation and arrives re-coded as a // terminal ConfigError (`classify_provider_error`). Server authentication // rejections happen later in the handshake and end the round with an // AuthError: terminal before the first connection, retried by a sender that // has connected (`reconnect_error_is_terminal_when_established`). let mut provided_header = match qwp_ws.token_provider.as_ref() { Some(provider) => { let acquired = acquire_qwp_ws_provider_header(provider, connect_kind, traffic_gate); // Narrate before propagating. This returns above the endpoint loop, // so `auth_failed` (inside it) and `all_endpoints_unreachable` // (after it) are both unreachable from here: without this the round // produces no event and no log at all. Combined with a retryable // provider error and a reconnect budget that restarts each round, // the caller would otherwise observe an indefinite silent stall // whose first symptom is unrelated store backpressure. Some(acquired.map_err(|err| { if provider_cancelled_by_shutdown(&err, traffic_gate) { log::debug!( "questdb: QWP/WebSocket token provider acquisition \ abandoned because the transport is shutting down" ); return err; } match events { // Foreground rounds narrate; background (orphan-drainer) // walks deliberately do not, so as not to claim an outage // against an endpoint the foreground may be using happily. Some(events) => { events.token_provider_failed(None, &err, events.next_attempt()); log::warn!( "questdb: QWP/WebSocket token provider failed before \ dialling any endpoint; abandoning this connection \ round: {err}" ); } None => log::debug!( "questdb: QWP/WebSocket token provider failed on a \ background walk: {err}" ), } err })?) } None => None, }; let static_auth_header = auth_header; let mut last_role_mismatch = None; let mut last_transport_failure: Option<(usize, crate::Error)> = None; let mut role_reject_count = 0usize; let mut version_reject_count = 0usize; let mut last_version_reject = None; // Pick + claim under the lock, then drop it for the blocking connect, then // re-acquire only to record the outcome. The lock is never held across // `establish_connection`. // // Reset and claim under one tracker lock. Otherwise a single-endpoint // contender can claim the just-reset round before this caller does. // Do this only for the first pick so failed attempts terminate normally. let mut first_pick = true; let mut auth_rotation_retry_used = false; while let Some(idx) = health.with_tracker(|t| t.pick_next_and_claim_connect_round(first_pick)) { first_pick = false; let endpoint = &endpoints[idx]; let mut connected = establish_connection( &endpoint.host, &endpoint.port, use_tls, tls_settings.clone(), connect_kind, qwp_ws, provided_header.as_deref().or(static_auth_header), traffic_gate, ); // A credential can expire during DNS/TCP/TLS setup. Resolve once more // after a definite 401 and replay this endpoint only when the rotating // provider actually supplies a changed header. Static credentials, 403, // unchanged values, and a second 401 end the round, as does a 401 // whose Bearer challenge names an error other than `invalid_token` // (spec §8.2): a new token cannot fix `insufficient_scope`. if matches!(&connected, Err(err) if err.upgrade_401_allows_token_refresh()) && !auth_rotation_retry_used && let Some(provider) = qwp_ws.token_provider.as_ref() { auth_rotation_retry_used = true; // Name the rejected token, so a cache-first provider such as // `OidcDeviceAuth` refreshes it rather than handing it back. let rotated = match crate::token_provider::with_rejected_credential( provided_header.as_deref(), || acquire_qwp_ws_provider_header(provider, connect_kind, traffic_gate), ) { Ok(rotated) => rotated, Err(err) => { // A credential was already presented to this endpoint. Keep // the provider's retryable/terminal classification and OIDC // detail, but do not report a pre-dial failure or lose the // server rejection that prompted the second acquisition. let code = err.code(); let message = format!( "QWP/WebSocket credential rejected with HTTP 401 at {}:{}; \ token provider failed to refresh it: {}", endpoint.host, endpoint.port, err.msg() ); let shutdown = provider_cancelled_by_shutdown(&err, traffic_gate); let err = err.reclassified(code, message); if let Some(events) = events.filter(|_| !shutdown) { events.token_provider_failed( Some((&endpoint.host, &endpoint.port)), &err, events.next_attempt(), ); } return Err(err); } }; if Some(rotated.as_str()) != provided_header.as_deref() { provided_header = Some(rotated); connected = establish_connection( &endpoint.host, &endpoint.port, use_tls, tls_settings.clone(), connect_kind, qwp_ws, provided_header.as_deref(), traffic_gate, ); } } if traffic_gate.is_some_and(TrafficGate::is_shutdown) { return Err(match connected { Ok(_) => error::fmt!( SocketError, "QWP/WebSocket runner is shutting down during connect" ), Err(err) => err, }); } match connected { Ok((stream, handshake_result, leftover)) => { health.with_tracker(|t| t.record_success(idx)); *previous_idx = Some(idx); return Ok(QwpWsConnectRoundSuccess { endpoint_idx: idx, stream, negotiated_version: handshake_result.version, server_max_batch_size: handshake_result.server_max_batch_size, leftover, }); } Err(err) if err.code() == crate::ErrorCode::AuthError => { if let Some(events) = events { events.auth_failed(&endpoint.host, &endpoint.port, &err, events.next_attempt()); } return Err(err); } Err(err) if is_qwp_ws_role_reject_error(&err) => { role_reject_count += 1; let role_reject = err.qwp_ws_role_reject().cloned(); let zone = qwp_ws_role_reject_zone(&err).map(str::to_string); let transient = qwp_ws_role_reject_is_transient(&err); health.with_tracker(|t| { t.record_zone(idx, zone.as_deref()); t.record_role_reject(idx, transient); }); let mut err = error::fmt!( RoleMismatch, "QWP/WebSocket role mismatch at {}:{}: {}", endpoint.host, endpoint.port, err ); if let Some(role_reject) = role_reject { err = err.with_qwp_ws_role_reject(role_reject); } if let Some(events) = events { events.connect_attempt_failed( &endpoint.host, &endpoint.port, &err, events.next_attempt(), ); } last_role_mismatch = Some(err); } Err(err) => { health.with_tracker(|t| t.record_transport_error(idx)); if let Some(events) = events { events.connect_attempt_failed( &endpoint.host, &endpoint.port, &err, events.next_attempt(), ); } if err.code() == crate::ErrorCode::ProtocolVersionError { version_reject_count += 1; last_version_reject = Some(err); } else { last_transport_failure = Some((idx, err)); } } } } // ProtocolVersionError is terminal, so it wins only when every configured // endpoint failed with it in THIS round -- counted against endpoints.len() // because rounds can be partial (a reconnect round skips the endpoint whose // mid-stream failure triggered it). Anything less -- a rolling upgrade // where the rest of the fleet is transiently down -- must stay retryable. if version_reject_count == endpoints.len() && let Some(err) = last_version_reject { return Err(err); } if qwp_ws.request_durable_ack.is_enabled() && role_reject_count == endpoints.len() { let role_reject = last_role_mismatch .as_ref() .and_then(|err| err.qwp_ws_role_reject().cloned()); let mut err = error::fmt!( ProtocolVersionError, "WebSocket upgrade failed: server did not enable durable ACK; all endpoints rejected by role" ); if let Some(role_reject) = role_reject { err = err.with_qwp_ws_role_reject(role_reject); } return Err(err); } if let Some(err) = last_role_mismatch { return Err(err); } let err = qwp_ws_all_endpoints_unreachable_error(endpoints, last_transport_failure); if let Some(events) = events { events.all_endpoints_unreachable(&err); } Err(err) } #[allow(clippy::too_many_arguments)] pub(crate) fn connect_qwp_ws( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, ) -> crate::Result { let mut state = connect_qwp_ws_background_state(host, port, use_tls, tls_settings, qwp_ws, auth_header)?; // Row background sender: the encoder is the delta encode surface, so it owns // the slot's persisted symbol dictionary for write-ahead. (The column sender // claims it in new_store_and_forward instead; the two are mutually exclusive.) let persisted = state.persisted_symbol_dict.take(); state.encoder.set_persisted_symbol_dict(persisted); // The row encoder was already seeded from the recovered entries inside // connect_qwp_ws_background_state (and the driver mirror seeds from the send // core / pending_connect, not from here), so the copy kept in the handler // state is dead weight for the row path -- free it. let _ = std::mem::take(&mut state.recovered_dict_entries); Ok(SyncProtocolHandler::SyncQwpWs(Box::new(state))) } /// Fallible copy of a recovered-dictionary byte region. A ~2 GiB recovery must /// surface an error, not abort the host via an infallible `to_vec`/`clone` -- that /// would defeat the fallible `try_reserve`/`MAX_FILE_LEN` guard the side-file /// reader already applies. pub(super) fn try_dup_recovered(src: &[u8]) -> crate::Result> { #[cfg(test)] if should_fail_recovered_dict_copy_for_test() { return Err(error::fmt!( SocketError, "injected recovered symbol dictionary allocation failure" )); } let mut v = Vec::new(); v.try_reserve_exact(src.len()).map_err(|_| { error::fmt!( SocketError, "recovered symbol dictionary is too large to allocate ({} bytes)", src.len() ) })?; v.extend_from_slice(src); Ok(v) } /// Give one sender its own token-provider attachment: shared acquisition, but /// a private callback-blocked state, so senders cloned from one pool or one /// options object never see each other's failed reconnects. Orphan drainers /// keep the unattached provider: nothing waits on their ACKs. fn attach_qwp_ws_token_provider(qwp_ws: &QwpWsConfig) -> Option { let provider = qwp_ws.token_provider.as_ref()?; let mut attached = qwp_ws.clone(); attached.token_provider = Some(provider.for_attachment()); Some(attached) } pub(crate) fn connect_qwp_ws_background_state( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, ) -> crate::Result { let orphan_config = OrphanDrainerConfig::new( host, port, use_tls, tls_settings.clone(), qwp_ws, auth_header.clone(), ); let attached = attach_qwp_ws_token_provider(qwp_ws); let qwp_ws = attached.as_ref().unwrap_or(qwp_ws); let server_max_batch_size = Arc::new(AtomicUsize::new(0)); let traffic_gate = Arc::new(TrafficGate::default()); let ( mut runner, encoder, delta_dict_enabled, recovered_dict_entries, recovered_dict_count, persisted_symbol_dict, ) = if *qwp_ws.initial_connect_retry == QwpWsInitialConnectMode::Async { let mut queue = open_configured_qwp_ws_queue(qwp_ws)?; // Pull the slot's delta-dict state out of the queue before it moves into // the runner: whether delta is on, the recovered entries (to seed the // foreground dict + the I/O thread's catch-up mirror), and the side-file // handle (routed to the owning foreground for write-ahead). let delta_dict_enabled = queue.is_delta_dict_enabled(); let recovered_dict_entries = try_dup_recovered(queue.recovered_symbol_dict_entries())?; let recovered_dict_count = queue.recovered_symbol_dict_count(); let persisted_symbol_dict = queue.take_persisted_symbol_dict(); // Async connect builds the send core lazily on the I/O thread; the driver // enables its catch-up mirror there (see drive_step). The encoder (used by // standalone ingress sender; dormant for the pooled core) delta-encodes on the same // condition, seeded from the recovered dictionary so new ids continue above // it. The side-file is left in the handler state for the owning foreground // to claim (standalone: connect_qwp_ws; pooled: new_store_and_forward). // // Validate the recovered dictionary SYNCHRONOUSLY here, *before* the runner // is spawned. `seed_global_dict` is the fallible validator // (`SymbolGlobalDict::seed`: duplicate / torn-tail rejection), and the // catch-up mirror the runner arms on connect seeds the SAME recovered bytes // verbatim (`SentDictMirror::seed` does not re-validate). A corrupt // dictionary must be rejected now -- before the I/O thread connects and // replays queued frames -- so the caller's `StoreResendRequired` // (`in_doubt == false`, "re-ingest from source") cannot surface only after // frames were already committed on the server, which would duplicate them. // This mirrors the pre-arm validation the orphan drainer already performs // (`qwp_ws_orphan.rs`) -- but note the two diverge on what they do with a // failure: this one fails construction, the drainer discards the rejected // entries and arms its mirror EMPTY so the drain bootstraps from the // stored frames' own delta sections (neither degrades to dense). // That asymmetry is deliberate and argued in `SymbolGlobalDict::seed`'s // docs; the short of it is that this path has a PRODUCER that would // otherwise mint ids the stored frames already reference, while // replay-only has none. `seed` can fail `SymbolDictFull` here as well as // `StoreResendRequired` -- a well-formed side-file written by a // higher-capped client -- and it leaves the dictionary partially seeded // either way, so continuing with it is never an option. Failing now // leaves the slot intact on disk for a client that can hold it // (`store_and_forward_file_mode_recovery_fails_when_the_recovered_dict_ // exceeds_the_cap`). let mut encoder = QwpWsReplayEncoder::new(1); encoder.set_delta_dict_enabled(delta_dict_enabled); if delta_dict_enabled { encoder.seed_global_dict(&recovered_dict_entries, recovered_dict_count)?; } let pending_connect = QwpWsPendingConnect::new( host, port, use_tls, tls_settings, qwp_ws, auth_header, *qwp_ws.request_durable_ack, Arc::clone(&server_max_batch_size), Arc::clone(&traffic_gate), delta_dict_enabled, try_dup_recovered(&recovered_dict_entries)?, recovered_dict_count, ); let runner = SyncQwpWsRunner::start_pending_connect( queue, pending_connect, *qwp_ws.sf_append_deadline, *qwp_ws.error_inbox_capacity, qwp_ws.rejection_sink.clone(), ); ( runner, encoder, delta_dict_enabled, recovered_dict_entries, recovered_dict_count, persisted_symbol_dict, ) } else { let mut parts = open_qwp_ws_parts( host, port, use_tls, tls_settings, qwp_ws, auth_header, Arc::clone(&server_max_batch_size), Some(Arc::clone(&traffic_gate)), )?; // Delta symbol dictionaries: memory mode always (the in-process ring is // replayed and the I/O thread re-registers the whole dictionary via a // catch-up frame on reconnect); file mode iff the persisted side-file // opened (its recovered entries seed the encoder dict + driver mirror so // ids continue above them). This branch runs for the standalone // ingress sender and for pool borrows whose initial connect is not // forced async — any non-lazy pool, including the default Off mode; // the encoder is live only for the standalone sender and stays // dormant for the pooled core. // Encoder and mirror enable together to stay in lockstep. let delta_dict_enabled = parts.delta_dict_enabled; parts.encoder.set_delta_dict_enabled(delta_dict_enabled); if delta_dict_enabled { parts .encoder .seed_global_dict(&parts.recovered_dict_entries, parts.recovered_dict_count)?; parts .send_core .enable_delta_dict(&parts.recovered_dict_entries, parts.recovered_dict_count); } let runner = SyncQwpWsRunner::start_with_append_deadline( parts.store, parts.send_core, *qwp_ws.sf_append_deadline, traffic_gate, ); ( runner, parts.encoder, delta_dict_enabled, parts.recovered_dict_entries, parts.recovered_dict_count, parts.persisted_symbol_dict, ) }; runner.has_sf_dir = qwp_ws.sf_dir.is_some(); let orphan_candidates = orphan_candidates(qwp_ws); let orphan_pool = OrphanDrainerPool::start( orphan_candidates, *qwp_ws.max_background_drainers, orphan_config, ); Ok(SyncQwpWsHandlerState { encoder, runner, server_max_batch_size, request_durable_ack: *qwp_ws.request_durable_ack, token_provider: qwp_ws.token_provider.clone(), sfa_frame_payload_cap: segment_payload_capacity(*qwp_ws.sf_max_segment_bytes), sfa_frame_split_target: two_frame_segment_payload_capacity(*qwp_ws.sf_max_segment_bytes), orphan_pool, close_drain_timeout: *qwp_ws.close_flush_timeout, delta_dict_enabled, recovered_dict_entries, recovered_dict_count, persisted_symbol_dict, sfa_split_deferral_enabled: qwp_ws.sf_dir.is_none(), }) } fn orphan_candidates(qwp_ws: &QwpWsConfig) -> Vec { let Some(sf_dir) = qwp_ws.sf_dir.as_ref() else { return Vec::new(); }; let mut candidates = if *qwp_ws.drain_orphans { scan_orphan_slots( sf_dir, qwp_ws.sender_id.as_str(), &qwp_ws.orphan_exclude_managed_slots, ) } else { Vec::new() }; let own_slot = sf_dir.join(qwp_ws.sender_id.as_str()); for slot in &qwp_ws.orphan_extra_slots { if slot != &own_slot && is_candidate_orphan(slot) && !candidates.iter().any(|s| s == slot) { candidates.push(slot.clone()); } } candidates } #[allow(clippy::too_many_arguments)] pub(crate) fn open_manual_qwp_ws( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, ) -> crate::Result { let orphan_config = OrphanDrainerConfig::new( host, port, use_tls, tls_settings.clone(), qwp_ws, auth_header.clone(), ); let attached = attach_qwp_ws_token_provider(qwp_ws); let qwp_ws = attached.as_ref().unwrap_or(qwp_ws); let server_max_batch_size = Arc::new(AtomicUsize::new(0)); let mut parts = open_qwp_ws_parts( host, port, use_tls, tls_settings, qwp_ws, auth_header, Arc::clone(&server_max_batch_size), None, )?; // Delta symbol dictionaries: the encoder ships only ids new since the dict // last grew, and the driver re-registers the whole dictionary via a catch-up // frame on reconnect. Enabled in memory mode always, and in file mode once the // persisted side-file opened -- then the recovered entries seed the encoder // dict + driver mirror (ids continue above them) and the encoder owns the // side-file for write-ahead. Manual progress is row-only, so the encoder is // the live surface. Encoder and mirror enable together to stay in lockstep. let delta_dict_enabled = parts.delta_dict_enabled; parts.encoder.set_delta_dict_enabled(delta_dict_enabled); if delta_dict_enabled { parts .encoder .seed_global_dict(&parts.recovered_dict_entries, parts.recovered_dict_count)?; parts .encoder .set_persisted_symbol_dict(parts.persisted_symbol_dict.take()); parts .send_core .enable_delta_dict(&parts.recovered_dict_entries, parts.recovered_dict_count); } let orphan_drainers = ManualOrphanDrainers::new( orphan_candidates(qwp_ws), *qwp_ws.max_background_drainers, orphan_config, ); Ok(ManualQwpWsHandlerState { encoder: parts.encoder, store: parts.store, send_core: parts.send_core, server_max_batch_size, request_durable_ack: *qwp_ws.request_durable_ack, token_provider: qwp_ws.token_provider.clone(), sfa_frame_payload_cap: segment_payload_capacity(*qwp_ws.sf_max_segment_bytes), sfa_frame_split_target: two_frame_segment_payload_capacity(*qwp_ws.sf_max_segment_bytes), aborted_flush: None, orphan_drainers, append_deadline: *qwp_ws.sf_append_deadline, close_drain_timeout: *qwp_ws.close_flush_timeout, has_sf_dir: qwp_ws.sf_dir.is_some(), }) } #[allow(clippy::too_many_arguments)] fn open_qwp_ws_parts( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, server_max_batch_size: Arc, traffic_gate: Option>, ) -> crate::Result { let mut queue = open_configured_qwp_ws_queue(qwp_ws)?; let transport = connect_blocking_transport( host, port, use_tls, tls_settings, qwp_ws, auth_header, Arc::clone(&server_max_batch_size), traffic_gate, )?; let negotiated_version = transport.negotiated_version(); // Extract the slot's delta-dict state before the queue moves into the store: // whether delta is on, the recovered entries (to seed the producer dict + the // driver mirror), and the side-file handle (for the foreground's write-ahead). let delta_dict_enabled = queue.is_delta_dict_enabled(); // Fallible copy: a large recovered dictionary (up to ~2 GiB for a crafted // CRC-valid side-file) must surface an error, not abort the host via an // infallible `to_vec` -- exactly the guard `try_dup_recovered` documents and the // async connect path already applies. let recovered_dict_entries = try_dup_recovered(queue.recovered_symbol_dict_entries())?; let recovered_dict_count = queue.recovered_symbol_dict_count(); let persisted_symbol_dict = queue.take_persisted_symbol_dict(); let mut store = QwpWsPublicationStore::new(queue, *qwp_ws.error_inbox_capacity); store.set_rejection_sink(qwp_ws.rejection_sink.clone()); let send_core = QwpWsSendCore::new_with_durable_ack_and_rejection_limit( transport, ReconnectPolicy::bounded( *qwp_ws.reconnect_max_duration, *qwp_ws.reconnect_initial_backoff, *qwp_ws.reconnect_max_backoff, ), *qwp_ws.request_durable_ack, *qwp_ws.max_frame_rejections, *qwp_ws.poison_min_escalation_window, ); Ok(QwpWsConnectedParts { encoder: QwpWsReplayEncoder::new(negotiated_version), store, send_core, delta_dict_enabled, recovered_dict_entries, recovered_dict_count, persisted_symbol_dict, }) } #[allow(clippy::too_many_arguments)] fn connect_blocking_transport( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, server_max_batch_size: Arc, traffic_gate: Option>, ) -> crate::Result { match *qwp_ws.initial_connect_retry { QwpWsInitialConnectMode::Off => BlockingQwpWsTransport::connect( host, port, use_tls, tls_settings, QwpWsConnectKind::Foreground, qwp_ws.clone(), auth_header, server_max_batch_size, traffic_gate, ), QwpWsInitialConnectMode::Sync | QwpWsInitialConnectMode::Async => { connect_blocking_transport_with_retry( host, port, use_tls, tls_settings, qwp_ws, auth_header, server_max_batch_size, traffic_gate, ) } } } #[allow(clippy::too_many_arguments)] fn connect_blocking_transport_with_retry( host: &str, port: &str, use_tls: bool, tls_settings: Option, qwp_ws: &QwpWsConfig, auth_header: Option, server_max_batch_size: Arc, traffic_gate: Option>, ) -> crate::Result { let started = Instant::now(); let deadline = started.checked_add(*qwp_ws.reconnect_max_duration); let endpoints = qwp_ws_configured_endpoints(host, port, qwp_ws); let mut tracker = QwpWsHostHealthTracker::new(endpoints.len()); let mut previous_idx = None; let mut attempts = 0usize; let mut backoff = *qwp_ws.reconnect_initial_backoff; let mut last_error = None; // Always run one full endpoint round: a very short connect budget can // already be used up by the time setup finishes. while attempts == 0 || deadline.is_none_or(|deadline| Instant::now() < deadline) { attempts += 1; match connect_qwp_ws_endpoint_round( &endpoints, &mut tracker, &mut previous_idx, None, use_tls, tls_settings.clone(), QwpWsConnectKind::Foreground, qwp_ws, auth_header.as_deref(), qwp_ws.conn_events.as_deref(), traffic_gate.as_deref(), ) { Ok(connected) => { return Ok(BlockingQwpWsTransport::from_connected( Arc::clone(&endpoints), tracker, use_tls, tls_settings, QwpWsConnectKind::Foreground, qwp_ws.clone(), auth_header, server_max_batch_size, traffic_gate.clone(), connected, )); } Err(err) if reconnect_error_is_foreground_terminal(&err) => return Err(err), Err(err) => { let role_reject = is_qwp_ws_role_reject_error(&err); last_error = Some(err); let sleep_for = reconnect_sleep_duration( role_reject, *qwp_ws.reconnect_initial_backoff, backoff, ); let Some(deadline) = deadline else { thread::sleep(sleep_for); backoff = if role_reject { *qwp_ws.reconnect_initial_backoff } else { double_duration(backoff).min(*qwp_ws.reconnect_max_backoff) }; continue; }; let remaining = deadline.saturating_duration_since(Instant::now()); if remaining.is_zero() { break; } thread::sleep(sleep_for.min(remaining)); backoff = if role_reject { *qwp_ws.reconnect_initial_backoff } else { double_duration(backoff).min(*qwp_ws.reconnect_max_backoff) }; } } } Err(retry_budget_exhausted_error( "QWP/WebSocket initial connect", attempts, started, last_error, )) } // ---------- send / receive ---------- /// Public flush entry point: publish through the replay queue and return once /// the buffer is locally accepted. A sender-owned runner advances WebSocket I/O. /// See [`flush_qwp_ws_through`]. pub(crate) fn flush_qwp_ws( state: &mut SyncQwpWsHandlerState, buffer: &QwpWsColumnarBuffer, frame_cap: usize, max_buf_size: usize, server_frame_limit_known: bool, ) -> Result, QwpWsFlushError> { let split_target = server_frame_limit_known.then_some(state.sfa_frame_split_target); flush_qwp_ws_through( &mut state.encoder, &mut state.runner, buffer, frame_cap, max_buf_size, split_target, ) } /// [`flush_qwp_ws`] for a sender of a pool, which has no encoder of its own: /// its row and column flushes share `encoder`. See [`flush_qwp_ws_through`]. pub(crate) fn flush_qwp_ws_pooled( state: &mut SyncQwpWsHandlerState, encoder: &mut impl QwpWsFlushEncoder, buffer: &QwpWsColumnarBuffer, frame_cap: usize, max_buf_size: usize, server_frame_limit_known: bool, ) -> Result, QwpWsFlushError> { let split_target = server_frame_limit_known.then_some(state.sfa_frame_split_target); flush_qwp_ws_through( encoder, &mut state.runner, buffer, frame_cap, max_buf_size, split_target, ) } /// Ends the refusal of a buffer that is above the frame cap and that would /// go out in several frames, had the server told its frame limit: /// `max_buf_size` is not what the buffer is held against. /// /// A server tells its frame limit since the release that can commit several /// frames together, so the frames of a buffer are never cut for a server /// that tells none. A sender that is not connected yet does not know its /// server, and cannot be told from one whose server tells no limit. pub(crate) const NO_FRAME_LIMIT_TOLD: &str = " It is not sent in several frames, because the \ server has not told its frame limit: either the sender is not connected yet (flush again \ once it is), or the server is too old to commit several frames together (upgrade it, or \ flush smaller batches)."; /// Ends the refusal of a buffer that is above the frame cap and that would /// go out in several frames, had the sender a symbol file in the slot of its /// `sf_dir`: it could not make the file when it started, or could not take /// the symbols of a frame out of it again. Without the file it sends every /// symbol with every frame, and the frames of a flush are not cut for that. pub(crate) const NO_SYMBOL_FILE: &str = " It is not sent in several frames, because this \ sender could not write the symbol file of its store-and-forward slot: flush smaller \ batches, or close the sender, see to the disk of sf_dir, and build a new one."; /// Publishes a buffer through `publisher`: for a sender in either progress /// mode, and for a sender of a pool. /// /// A buffer goes out as one frame, which is refused when it is larger than /// `frame_cap`. When the server has told its frame limit (`split_target`, /// the size its frames are then cut for) and `encoder` sends its symbols as /// a delta, a buffer whose one frame is above `frame_cap` goes out in /// several frames that the server commits together instead. Such a flush /// waits until the queue has room for all its frames, as a flush of one /// frame waits for the room of its frame, and queues nothing before. /// /// The encoded frame decides, not the size bound of the buffer /// ([`QwpWsColumnarBuffer::len`]), which takes more than the frame for /// symbols: a buffer whose frame fits `frame_cap` goes out as that one /// frame, as it would from a sender that cuts no frames, whatever its bound. /// Only a buffer whose bound is above `frame_cap` and whose frame may still /// fit, by [`QwpWsColumnarBuffer::one_frame_min_len`], is encoded as one /// frame first; that encoding is thrown away when the frame is too large. /// /// A buffer that cannot be cut into frames goes the way of one frame, whose /// encoder gives a value that cannot be sent the error of that value. What /// the bound keeps from frames may fit one frame all the same: a buffer /// above `max_buf_size`, a buffer whose frames the queue cannot hold /// together, and a buffer with a row that fits no frame by its bound. Such a /// buffer is sent when it fits one frame. When it does not, it is refused /// with an error that names `max_buf_size`, the queue, or the row. fn flush_qwp_ws_through( encoder: &mut impl QwpWsFlushEncoder, publisher: &mut impl QwpWsFlushPublisher, buffer: &QwpWsColumnarBuffer, frame_cap: usize, max_buf_size: usize, split_target: Option, ) -> Result, QwpWsFlushError> { // What keeps the buffer from being cut into frames by its size bound, // when one thing does. let mut not_in_frames = None; // The error of the one frame, when it was tried and is too large. let mut one_frame_too_large = None; if let Some(split_target) = split_target.filter(|_| encoder.delta_dict_enabled()) { let len = buffer.len(); if len > frame_cap && len > max_buf_size { // A buffer that needs several frames is held against // `max_buf_size` by its size. not_in_frames = Some(QwpWsNotInFrames::AboveMaxBufSize { len, max_buf_size }); } else if len > frame_cap { // The bound takes more than the frame for symbols: a buffer // whose one frame may fit the cap goes out as that frame, with // room in the queue or without, and is cut into frames only // when that frame is too large. let known = encoder.known_symbols_max_encoded_len(); if buffer.one_frame_min_len(known) <= frame_cap { match publish_qwp_ws_buffer(encoder, buffer, frame_cap, |payload| { publisher.publish(payload) }) { Err(err) if err.code() == crate::ErrorCode::BatchTooLarge => { one_frame_too_large = Some(err); } published => return published.map_err(QwpWsFlushError::Failed), } } let target = frame_cap.min(split_target); match buffer.plan_frames(target, frame_cap) { Ok(plan) => { if let Some(producer) = publisher.producer() && let Some(min_total_bytes) = producer.min_total_bytes_for(plan.frame_sizes()) { let frame_sizes = plan.frame_sizes(); let max_total_bytes = producer.max_total_bytes(); if min_total_bytes > max_total_bytes { not_in_frames = Some(QwpWsNotInFrames::AboveQueue { frame_count: frame_sizes.len(), min_total_bytes, max_total_bytes, }); } else { // Room that the queue has now stays until the // frames are queued, so they wait for no room // then. With `sf_dir` a later frame may still // wait for storage work of the network thread, // which ends by itself. if !producer.has_room_for(frame_sizes) { // The queue can hold the frames together, so // the room comes once the server has // confirmed what the queue holds now. publisher .await_room(frame_sizes) .map_err(QwpWsFlushError::Failed)?; } return publish_qwp_ws_frames( encoder, publisher, buffer, &plan, frame_cap, ) .map_err(QwpWsFlushError::Failed); } } } Err(QwpWsNoFramePlan::RowAboveCap { table_idx, frame_size, .. }) => { not_in_frames = Some(QwpWsNotInFrames::RowAboveCap { table_idx, frame_size, }) } Err( QwpWsNoFramePlan::UnsendableValue | QwpWsNoFramePlan::NoRows | QwpWsNoFramePlan::NoSizeBound, ) => {} } } } match one_frame_too_large { Some(err) => Err(err), None => publish_qwp_ws_buffer(encoder, buffer, frame_cap, |payload| { publisher.publish(payload) }), } .map_err(|err| match not_in_frames { // The buffer is too large for one frame too: the error names what // kept it from going out in several. Some(not_in_frames) if err.code() == crate::ErrorCode::BatchTooLarge => { not_in_frames.error(buffer, frame_cap) } _ => QwpWsFlushError::Failed(err), }) } /// What keeps a buffer above the frame cap from being cut into frames, by /// the size bound of the buffer or of its frames. The bound takes more than /// the encoded frame for symbols, so such a buffer goes out as one frame /// when that frame fits, and gets the error of what kept it from frames /// when it does not. enum QwpWsNotInFrames { /// The buffer is larger than `max_buf_size`. AboveMaxBufSize { len: usize, max_buf_size: usize }, /// The queue cannot hold the `frame_count` frames of the plan together, /// whatever the server confirms: they need `min_total_bytes`. AboveQueue { frame_count: usize, min_total_bytes: u64, max_total_bytes: u64, }, /// A row of table `table_idx` fits no frame: a frame that holds it alone /// takes `frame_size` bytes. RowAboveCap { table_idx: usize, frame_size: usize }, } impl QwpWsNotInFrames { /// The error of a buffer that is too large for one frame too. fn error(self, buffer: &QwpWsColumnarBuffer, frame_cap: usize) -> QwpWsFlushError { match self { Self::AboveMaxBufSize { len, max_buf_size } => { QwpWsFlushError::Refused(buffer_exceeds_max_buf_size_error(len, max_buf_size)) } Self::AboveQueue { frame_count, min_total_bytes, max_total_bytes, } => QwpWsFlushError::Refused(flush_exceeds_queue_error( frame_count, min_total_bytes, max_total_bytes, )), Self::RowAboveCap { table_idx, frame_size, } => QwpWsFlushError::Failed(row_exceeds_frame_cap_error( &buffer.table_name(table_idx), frame_size, frame_cap, )), } } } /// What ends the refusal of a buffer above the frame cap, when one thing /// keeps a sender from cutting it into frames: the server's silence about /// its frame limit, or a slot without a symbol file. Empty when the buffer /// is held against `max_buf_size` itself (`frame_cap_is_max_buf_size`), and /// when such a buffer is cut. pub(crate) fn qwp_ws_not_in_frames_hint( frame_cap_is_max_buf_size: bool, server_frame_limit_known: bool, delta_dict_enabled: bool, ) -> &'static str { if frame_cap_is_max_buf_size { "" } else if !server_frame_limit_known { NO_FRAME_LIMIT_TOLD } else if !delta_dict_enabled { NO_SYMBOL_FILE } else { "" } } /// Why [`flush_qwp_ws`] or [`flush_qwp_ws_manual`] did not publish a buffer. #[derive(Debug)] pub(crate) enum QwpWsFlushError { /// The buffer is refused with an error that names the limit in the way /// and what to do about it, which is not the frame cap. Refused(crate::Error), /// Any other error. Among these are the two that name the frame cap, to /// which `Sender` adds what sets it: the size error of a buffer that goes /// out as one frame, and the refusal of a row that fits no frame. Another /// is the error of a flush that waited in vain for room in the queue. Failed(crate::Error), } /// The refusal of a buffer that is larger than `max_buf_size`. fn buffer_exceeds_max_buf_size_error(len: usize, max_buf_size: usize) -> crate::Error { error::fmt!( BatchTooLarge, "Could not flush buffer: its size is {len} bytes, and max_buf_size is {max_buf_size} \ bytes. Set max_buf_size to at least {len}, or flush more often." ) } /// The refusal of a buffer with a row that fits no frame: by its size bound, /// a frame that holds the row alone takes `frame_size` bytes. fn row_exceeds_frame_cap_error(table: &str, frame_size: usize, frame_cap: usize) -> crate::Error { error::fmt!( BatchTooLarge, "Could not flush buffer: a row of table '{table}' needs a frame of {frame_size} bytes, \ and the per-frame cap is {frame_cap} bytes. Make the row smaller, or raise the cap." ) } /// The refusal of a flush whose frames the queue cannot hold together, /// whatever the server confirms: see [`SfaProducer::min_total_bytes_for`]. fn flush_exceeds_queue_error( frame_count: usize, min_total_bytes: u64, max_total_bytes: u64, ) -> crate::Error { error::fmt!( BatchTooLarge, "Could not flush buffer: its {frame_count} frames need a store-and-forward queue of \ {min_total_bytes} bytes, and sf_max_total_bytes is {max_total_bytes} bytes. Set \ sf_max_total_bytes to at least {min_total_bytes}, or flush more often." ) } pub(crate) fn publish_qwp_ws_payload_background( state: &mut SyncQwpWsHandlerState, payload: &[u8], max_buf_size: usize, ) -> crate::Result { if payload.is_empty() { return Err(error::fmt!( InvalidApiCall, "Could not flush buffer: QWP/WebSocket encoded message is empty." )); } if payload.len() > max_buf_size { return Err(qwp_ws_encoded_message_size_error( payload.len(), max_buf_size, )); } state.runner.publish_replay_payload(payload) } /// Whether a deferred frame may be published without stranding the frame that /// will commit it. /// /// A deferred frame is never acked, so the queue storage it occupies is never /// reclaimed. Publishing one that consumes the slot's last byte budget would /// leave the committing frame unable to publish at all: it would block on /// back-pressure until the append deadline and then fail, with the deferred /// prefix durably queued and nothing able to commit it. This is the /// store-and-forward counterpart of the direct backend's /// `Conn::has_sync_commit_slot`. /// /// Normally answered from the foreground's own producer handle, which takes the /// engine's state lock briefly. The `try_lock` fallback only applies once the /// producer has been handed off (close drain), and answers conservatively /// (`false`) there: committing a frame that could have been deferred costs /// throughput, deferring one that should have committed costs liveness. pub(crate) fn sfa_has_deferred_commit_slot(state: &SyncQwpWsHandlerState) -> bool { state.sfa_split_deferral_enabled && state.runner.has_deferred_commit_headroom().unwrap_or(false) } /// [`flush_qwp_ws`] for a sender in manual progress mode, where nobody sends /// or reads the server's answers unless the caller drives: the flush only /// queues its frames, and where it waits for room in the queue it drives the /// connection itself. See [`flush_qwp_ws_through`]. pub(crate) fn flush_qwp_ws_manual( state: &mut ManualQwpWsHandlerState, buffer: &QwpWsColumnarBuffer, frame_cap: usize, max_buf_size: usize, server_frame_limit_known: bool, ) -> Result, QwpWsFlushError> { let split_target = server_frame_limit_known.then_some(state.sfa_frame_split_target); let mut publisher = ManualQwpWsPublisher { store: &mut state.store, send_core: &mut state.send_core, aborted_flush: &mut state.aborted_flush, append_deadline: state.append_deadline, }; flush_qwp_ws_through( &mut state.encoder, &mut publisher, buffer, frame_cap, max_buf_size, split_target, ) } /// The store and the connection of a sender in manual progress mode, as what /// a flush publishes through. /// /// After a flush that failed part way ([`QwpWsFlushPublisher::abort`]) the /// sender is in the state of [`SyncQwpWsRunner::abort_flush`]: the store is /// closed for publications, each of which gets the error of that flush; /// what the caller drives goes on sending the queue, the frames of that /// flush among them, and reading the answers; and callers are told that the /// sender has published what it had before that flush: see /// [`qwp_ws_published_fsn_manual`] and [`qwp_ws_close_drain_manual`]. struct ManualQwpWsPublisher<'a> { store: &'a mut QwpWsPublicationStore, send_core: &'a mut QwpWsSendCore, aborted_flush: &'a mut Option, append_deadline: Duration, } impl ManualQwpWsPublisher<'_> { /// The error of a publication that the store did not take. fn error_of(&self, err: DriverError) -> crate::Error { match (err, &*self.aborted_flush) { (DriverError::Closing, Some(flush)) => flush.error.clone(), (err, Some(flush)) => { let err = driver_error_to_error_from_store(self.store, err); // A rejection of the frames of that flush does not replace // its error. if flush.is_rejection_of_its_frames(&err, self.store.completed_fsn()) { flush.error.clone() } else { err } } (err, None) => driver_error_to_error_from_store(self.store, err), } } } impl QwpWsFlushPublisher for ManualQwpWsPublisher<'_> { fn producer(&self) -> Option<&SfaProducer> { self.store.producer() } fn await_room(&mut self, payload_lens: &[usize]) -> crate::Result<()> { let has_room = |store: &mut QwpWsPublicationStore| match store.producer() { Some(producer) if !producer.has_room_for(payload_lens) => { Err(match store.lifecycle().load() { PublicationState::Open => DriverError::Queue(producer.no_room_error()), _ => DriverError::Closing, }) } _ => Ok(()), }; manual_drive_until_taken(self.store, self.send_core, self.append_deadline, has_room) .map_err(|err| self.error_of(err)) } fn publish(&mut self, payload: &[u8]) -> crate::Result { let submit = |store: &mut QwpWsPublicationStore| { store.try_submit(payload).map(|receipt| receipt.fsn) }; manual_drive_until_taken(self.store, self.send_core, self.append_deadline, submit) .map_err(|err| self.error_of(err)) } fn publish_later_frame(&mut self, payload: &[u8]) -> crate::Result { self.publish(payload) } fn abort(&mut self, first_fsn: u64, cause: crate::Error) -> crate::Error { let flush = AbortedFlush::new(first_fsn, cause); let error = flush.error.clone(); *self.aborted_flush = Some(flush); self.store.set_closing(); // Nothing drives the connection between two frames of a flush that // had its room, so the sender has not stopped for a reason of its // own in between, as one in background progress mode can have. error } } impl QwpWsFlushPublisher for SyncQwpWsRunner where Q: PublicationLog + Send + 'static, { fn producer(&self) -> Option<&SfaProducer> { self.producer.as_ref() } fn await_room(&mut self, payload_lens: &[usize]) -> crate::Result<()> { match self.producer.as_ref() { Some(producer) => self.wait_for_room(producer, payload_lens), None => Ok(()), } } fn publish(&mut self, payload: &[u8]) -> crate::Result { self.publish_replay_payload(payload) } fn publish_later_frame(&mut self, payload: &[u8]) -> crate::Result { self.publish_frame(payload, true) } fn abort(&mut self, first_fsn: u64, cause: crate::Error) -> crate::Error { self.abort_flush(first_fsn, cause) } } fn publish_qwp_ws_buffer( encoder: &mut impl QwpWsFlushEncoder, buffer: &QwpWsColumnarBuffer, max_buf_size: usize, publish: impl FnOnce(&[u8]) -> crate::Result, ) -> crate::Result> { // `encode_and_publish` rolls the dict + side-file back if `publish` fails, so a // recoverable publish failure (e.g. `SubmitTimedOut` back-pressure) does not // leave the encoder dict ahead of the driver's send mirror and trip the // torn-dict guard on the next frame (which would abandon all queued data). encoder .encode_and_publish(buffer, max_buf_size, publish) .map(Some) } /// Publishes the frames of `plan` back to back, as one flush that the server /// commits once: every frame but the last asks the server to hold its rows, /// and the last one commits them all. Returns the number of the last frame. /// /// When the first frame cannot be published, nothing of the flush is queued /// and the sender stays usable. When a later one cannot, the sender takes no /// more publications: see [`SyncQwpWsRunner::abort_flush`]. fn publish_qwp_ws_frames( encoder: &mut impl QwpWsFlushEncoder, publisher: &mut impl QwpWsFlushPublisher, buffer: &QwpWsColumnarBuffer, plan: &QwpWsFramePlan, frame_cap: usize, ) -> crate::Result> { let mut frames = plan.frames().peekable(); let mut first_fsn = None; let mut last_fsn = None; while let Some(blocks) = frames.next() { let defer_commit = frames.peek().is_some(); let published = encoder.encode_and_publish_frame(buffer, blocks, defer_commit, frame_cap, |payload| { match first_fsn { None => publisher.publish(payload), Some(_) => publisher.publish_later_frame(payload), } }); match (published, first_fsn) { (Ok(fsn), _) => { first_fsn.get_or_insert(fsn); last_fsn = Some(fsn); } (Err(err), None) => return Err(err), (Err(err), Some(first_fsn)) => return Err(publisher.abort(first_fsn, err)), } } Ok(last_fsn) } pub(crate) fn qwp_ws_drive_once(state: &mut ManualQwpWsHandlerState) -> crate::Result { let outcome = state.send_core.drive_once(&mut state.store); let foreground_progress = match outcome { Ok(DriveOutcome::Idle) => Ok(false), Ok(DriveOutcome::Terminal) | Err(DriverError::Terminal) => { qwp_ws_manual_stopping_error(state).map(|()| false) } Ok(DriveOutcome::ReconnectDelay { sleep_for, deadline, }) => { sleep_before_manual_reconnect(deadline, sleep_for, None); Ok(true) } Ok( DriveOutcome::Sent(_) | DriveOutcome::Acked { .. } | DriveOutcome::Rejected { .. } | DriveOutcome::Reconnected { .. } | DriveOutcome::Progress, ) => Ok(true), Err(err) => Err(driver_error_to_error_from_store(&state.store, err)), }?; let orphan_progress = state .orphan_drainers .as_mut() .is_some_and(ManualOrphanDrainers::drive_once); Ok(foreground_progress || orphan_progress) } pub(crate) fn qwp_ws_published_fsn_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.published_fsn() } pub(crate) fn qwp_ws_acked_fsn_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.acked_fsn() } pub(crate) fn qwp_ws_ok_fsn_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.ok_fsn() } /// Why the background runner's latest attempt to (re)connect failed, while it /// has no connection. pub(crate) fn qwp_ws_reconnect_failure_background( state: &SyncQwpWsHandlerState, ) -> Option { state.runner.reconnect_failure() } /// Why the latest manually driven attempt to (re)connect failed, while there /// is no connection. pub(crate) fn qwp_ws_reconnect_failure_manual( state: &ManualQwpWsHandlerState, ) -> Option { state.store.reconnect_failure().cloned() } /// Take before checking a completion watermark or error condition. pub(crate) fn qwp_ws_progress_generation_background(state: &SyncQwpWsHandlerState) -> u64 { state.runner.backpressure.generation() } /// Wait for runner progress or the absolute deadline. The caller must recheck /// its watermark/error predicate: sends, storage maintenance and failed /// reconnect attempts also notify. pub(crate) fn qwp_ws_wait_for_progress_background( state: &SyncQwpWsHandlerState, generation: u64, deadline: Option, ) { state .runner .backpressure .wait_for_change(generation, deadline); } pub(crate) fn qwp_ws_published_fsn_manual( state: &ManualQwpWsHandlerState, ) -> crate::Result> { check_manual_driver_error(state)?; Ok(match &state.aborted_flush { Some(flush) => flush.published_before, None => state.store.published_fsn(), }) } /// The position that the queue of a sender has confirmed, frames of a flush /// that failed part way included: what [`confirmed_as_reported`] bounds. #[cfg(test)] pub(crate) fn qwp_ws_queue_completed_fsn_for_test_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.queue_completed_fsn() } /// [`qwp_ws_queue_completed_fsn_for_test_background`] in manual progress /// mode. #[cfg(test)] pub(crate) fn qwp_ws_queue_completed_fsn_for_test_manual( state: &ManualQwpWsHandlerState, ) -> Option { state.store.completed_fsn() } pub(crate) fn qwp_ws_acked_fsn_manual( state: &ManualQwpWsHandlerState, ) -> crate::Result> { check_manual_driver_error(state)?; Ok(confirmed_as_reported( state.aborted_flush.as_ref(), state.store.completed_fsn(), )) } /// Manual-mode OK watermark. Manual progress keeps no separate OK tracker, /// so this is the completed watermark: with durable ACKs requested it advances /// on the durable ACK the sender trims on, not on the server's ordinary OK. pub(crate) fn qwp_ws_ok_fsn_manual(state: &ManualQwpWsHandlerState) -> crate::Result> { check_manual_driver_error(state)?; Ok(confirmed_as_reported( state.aborted_flush.as_ref(), state.store.completed_fsn(), )) } pub(crate) fn qwp_ws_check_error_background(state: &SyncQwpWsHandlerState) -> crate::Result<()> { state.runner.check_error() } pub(crate) fn qwp_ws_check_error_manual(state: &ManualQwpWsHandlerState) -> crate::Result<()> { check_manual_driver_error(state) } pub(crate) fn qwp_ws_poll_sender_error_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.poll_sender_error() } pub(crate) fn qwp_ws_poll_sender_error_notification_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.poll_sender_error_notification() } pub(crate) fn qwp_ws_poll_sender_error_manual( state: &mut ManualQwpWsHandlerState, ) -> crate::Result> { Ok(state.store.poll_sender_error()) } pub(crate) fn qwp_ws_poll_sender_error_notification_manual( state: &mut ManualQwpWsHandlerState, ) -> crate::Result> { Ok(state.store.poll_sender_error_notification()) } pub(crate) fn qwp_ws_terminal_sender_error_background( state: &SyncQwpWsHandlerState, ) -> crate::Result> { state.runner.terminal_sender_error() } pub(crate) fn qwp_ws_terminal_sender_error_manual( state: &ManualQwpWsHandlerState, ) -> crate::Result> { Ok(state.store.terminal_sender_error().cloned()) } pub(crate) fn qwp_ws_is_terminal_background(state: &SyncQwpWsHandlerState) -> bool { state.runner.lifecycle_is_terminal() } pub(crate) fn qwp_ws_has_aborted_flush_background(state: &SyncQwpWsHandlerState) -> bool { state.runner.has_aborted_flush() } /// Whether a sender in background progress mode stopped for a reason that is /// not about the frames of a flush that failed part way alone: see /// [`stopping_error`]. pub(crate) fn qwp_ws_stopped_for_good_background(state: &SyncQwpWsHandlerState) -> bool { state.runner.stopped_for_good() } pub(crate) fn qwp_ws_is_terminal_manual(state: &ManualQwpWsHandlerState) -> bool { state.store.is_terminal() } pub(crate) fn qwp_ws_sender_errors_dropped_background( state: &SyncQwpWsHandlerState, ) -> crate::Result { state.runner.sender_errors_dropped_total() } pub(crate) fn qwp_ws_sender_errors_dropped_manual( state: &ManualQwpWsHandlerState, ) -> crate::Result { Ok(state.store.sender_errors_dropped_total()) } pub(crate) fn qwp_ws_counters_background( state: &SyncQwpWsHandlerState, ) -> crate::Result { state.runner.counters() } pub(crate) fn qwp_ws_counters_manual( state: &ManualQwpWsHandlerState, ) -> crate::Result { Ok(state.store.counters()) } pub(crate) fn qwp_ws_close_drain_background( state: &mut SyncQwpWsHandlerState, ) -> crate::Result<()> { let result = state.runner.close_drain(state.close_drain_timeout); if let Some(mut orphan_pool) = state.orphan_pool.take() { orphan_pool.close(); } result } /// Non-blocking: stop accepting new publications and wake the runner to flush /// what is queued. The unified sender pool signals every connection being /// retired before waiting on any, so a batched close drains in parallel. pub(crate) fn qwp_ws_begin_close_background(state: &SyncQwpWsHandlerState) { state.runner.begin_close(); } /// Wait (until `deadline`) for the background runner to deliver every published /// frame, then close the queue and the orphan-drainer pool. A `None` deadline /// waits indefinitely; an already-elapsed deadline fails fast. Pairs with /// [`qwp_ws_begin_close_background`]. pub(crate) fn qwp_ws_drain_to_deadline_background( state: &mut SyncQwpWsHandlerState, deadline: Option, ) -> crate::Result<()> { let result = state.runner.drain_to_deadline(deadline); if let Some(mut orphan_pool) = state.orphan_pool.take() { orphan_pool.close(); } result } /// Closes the queue of a sender in manual progress mode whose flush failed /// part way, once the frames before that flush are confirmed: the frames of /// the flush go with the queue, which closes as fully drained. fn qwp_ws_close_manual_after_aborted_flush( state: &mut ManualQwpWsHandlerState, ) -> crate::Result<()> { state .send_core .close_publication_queue_abandoning_unresolved(&mut state.store) .map_err(driver_error_to_error_without_state) } pub(crate) fn qwp_ws_close_drain_manual(state: &mut ManualQwpWsHandlerState) -> crate::Result<()> { if state.close_drain_timeout.is_zero() { state.store.set_closing(); return Ok(()); } let deadline = Instant::now().checked_add(state.close_drain_timeout); loop { // The frames of a flush that failed part way are never confirmed: // the wait ends at the frame before them, and they go with the queue, // which closes as fully drained and leaves an empty slot. Nothing // sends from the queue once it is closed, so the connection may stay // until the sender is dropped: the server holds the rows of the flush // for a frame that never comes, and drops them then. if let Some(flush) = &state.aborted_flush && !state.store.is_terminal() && state.store.completed_fsn() >= flush.published_before { return qwp_ws_close_manual_after_aborted_flush(state); } match state.send_core.close_drain_ready_once(&mut state.store) { Ok(CloseOutcome::Drained) => return Ok(()), // The server's rejection of frames of that flush, once the // frames before it are confirmed, closes as above: see // [`stopping_error`]. Ok(CloseOutcome::Terminal) => { qwp_ws_manual_stopping_error(state)?; return qwp_ws_close_manual_after_aborted_flush(state); } Ok(CloseOutcome::Waiting { sleep_for, deadline: reconnect_deadline, }) => { // A retryable provider/connect failure keeps the driver in // `Waiting`. Once the outer close budget expires, the sleep // helper returns immediately; without this check the loop // drives reconnect continuously, spins a core and never lets // close (or a language runtime's Ctrl-C handling) return. if backpressure_deadline_expired(deadline) { return Err(qwp_ws_manual_close_timeout_error(state)); } sleep_before_manual_reconnect(reconnect_deadline, sleep_for, deadline); } Ok(CloseOutcome::Timeout) => { if backpressure_deadline_expired(deadline) { return Err(qwp_ws_manual_close_timeout_error(state)); } thread::sleep(BACKPRESSURE_PARK); } Err(err) => return Err(driver_error_to_error_from_store(&state.store, err)), } } } /// Asks `queue` until it takes what the caller has for it. While it answers /// that it has no room, drives the connection, which nobody else does in /// manual progress mode: the room comes when the server confirms frames that /// the queue holds. Gives up at the append deadline. fn manual_drive_until_taken( store: &mut QwpWsPublicationStore, send_core: &mut QwpWsSendCore, append_deadline: Duration, queue: impl Fn(&mut QwpWsPublicationStore) -> Result, ) -> Result where Q: PublicationLog, T: QwpWsCoreTransport, { let deadline = Instant::now().checked_add(append_deadline); loop { if store.is_terminal() { return Err(DriverError::Terminal); } match queue(store) { Ok(taken) => return Ok(taken), Err(err) => { let Some(backpressure) = driver_error_backpressure_queue(&err) else { return Err(err); }; if backpressure_deadline_expired(deadline) { return Err(DriverError::SubmitTimedOut { backpressure: Some(backpressure), }); } match send_core.drive_once(store)? { DriveOutcome::Idle => sleep_until_backpressure_deadline(deadline), DriveOutcome::ReconnectDelay { sleep_for, deadline: reconnect_deadline, } => sleep_before_manual_reconnect(reconnect_deadline, sleep_for, deadline), _ => {} } } } } } fn sleep_until_backpressure_deadline(deadline: Option) { let sleep_for = match deadline { Some(deadline) => deadline .saturating_duration_since(Instant::now()) .min(BACKPRESSURE_PARK), None => BACKPRESSURE_PARK, }; if !sleep_for.is_zero() { thread::sleep(sleep_for); } } fn sleep_before_manual_reconnect( reconnect_deadline: Option, sleep_for: Duration, outer_deadline: Option, ) { let mut sleep_for = sleep_for; if let Some(deadline) = reconnect_deadline { let remaining = deadline.saturating_duration_since(Instant::now()); if remaining.is_zero() { return; } sleep_for = sleep_for.min(remaining); } if let Some(deadline) = outer_deadline { let remaining = deadline.saturating_duration_since(Instant::now()); if remaining.is_zero() { return; } sleep_for = sleep_for.min(remaining); } if !sleep_for.is_zero() { thread::sleep(sleep_for); } } fn check_manual_driver_error(state: &ManualQwpWsHandlerState) -> crate::Result<()> { check_store_error_after(&state.store, state.aborted_flush.as_ref()) } /// The error that stopped a sender in manual progress mode, as its calls /// report it: see [`stopping_error`]. fn qwp_ws_manual_stopping_error(state: &ManualQwpWsHandlerState) -> crate::Result<()> { stopping_error( state.aborted_flush.as_ref(), driver_error_to_error_from_store(&state.store, DriverError::Terminal), state.store.completed_fsn(), ) } fn qwp_ws_manual_close_timeout_error(state: &ManualQwpWsHandlerState) -> crate::Error { close_drain_timeout_error( unconfirmed_at_close(&state.store, state.aborted_flush.as_ref()), state.store.reconnect_failure(), state.has_sf_dir, ) } /// How many of the frames that a close drain waits for the server has not /// confirmed: of all that were published, or of those before a flush that /// failed part way. fn unconfirmed_at_close( store: &QwpWsPublicationStore, aborted_flush: Option<&AbortedFlush>, ) -> u64 { store.unconfirmed_through(match aborted_flush { Some(flush) => flush.published_before, None => store.published_fsn(), }) } /// The error of a close drain, in either progress mode, whose deadline passed /// before every published frame was acknowledged. It says how many frames /// are not confirmed, names the latest reconnect failure if there is one, /// says where those frames go, which depends on `has_sf_dir`, and says what /// a caller can do. As for an ACK-wait timeout, the failure's diagnostics are /// kept except a server-rejection diagnostic, which marks a terminal error. fn close_drain_timeout_error( unconfirmed: u64, reconnect_failure: Option<&crate::Error>, has_sf_dir: bool, ) -> crate::Error { let not_confirmed = match unconfirmed { 1 => "1 of them is not confirmed".to_string(), frames => format!("{frames} of them are not confirmed"), }; let what_to_do = if has_sf_dir { "They stay in the slot under sf_dir and the next sender on the slot sends them: to \ deliver them with this sender, call wait before closing or raise \ close_flush_timeout_millis." } else { "Without sf_dir they are dropped with the sender: call wait before closing, raise \ close_flush_timeout_millis, or use sf_dir." }; let msg = |cause: &str| { format!( "QWP/WebSocket close drain timed out before all published frames were \ acknowledged: {not_confirmed}{cause}. {what_to_do}" ) }; match reconnect_failure { Some(cause) => cause.clone().without_qwp_ws_rejection().reclassified( crate::ErrorCode::SocketError, msg(&format!(" (the latest reconnect failed: {})", cause.msg())), ), None => error::fmt!(SocketError, "{}", msg("")), } } /// The error of a close drain in background progress mode whose deadline /// passed while the network thread was stopping after a flush that failed /// part way, with the flushes before it confirmed. Nothing is closed, and /// where the frames of the failed flush go depends on `has_sf_dir`. fn close_drain_timeout_after_aborted_flush_error(has_sf_dir: bool) -> crate::Error { let what_to_do = if has_sf_dir { "Drop the sender: the next sender on the slot under sf_dir removes that flush." } else { "Drop the sender: the frames of that flush go with it." }; error::fmt!( SocketError, "QWP/WebSocket close drain timed out while the sender was closing its connection after \ the flush that stopped part way. The flushes before it are confirmed, and nothing of \ that flush is committed: only a flush with more rows of one table than the server's \ qwp.max.uncommitted.rows may be committed in part. {what_to_do}" ) } fn double_duration(duration: Duration) -> Duration { duration.checked_mul(2).unwrap_or(Duration::MAX) } fn driver_error_to_error_without_state(err: DriverError) -> crate::Error { match err { DriverError::Transport(err) | DriverError::Storage(err) => err, DriverError::Queue(err) => error::fmt!( InvalidApiCall, "QWP/WebSocket queue rejected publication: {:?}", err ), DriverError::SubmitTimedOut { backpressure } => submit_timeout_error(backpressure, None), DriverError::Terminal => error::fmt!(SocketError, "QWP/WebSocket sender is terminal"), DriverError::Closing => error::fmt!(InvalidApiCall, "QWP/WebSocket sender is closing"), DriverError::UnknownReceipt { fsn } => error::fmt!( InvalidApiCall, "QWP/WebSocket receipt is unknown [fsn={fsn}]" ), } } fn driver_error_is_backpressure(err: &DriverError) -> bool { driver_error_backpressure_queue(err).is_some() } fn driver_error_backpressure_queue(err: &DriverError) -> Option { use super::qwp_ws_queue::QueueError; match err { DriverError::Queue( err @ (QueueError::FrameCapacityFull { .. } | QueueError::ByteCapacityFull { .. } | QueueError::StorageSpareNotReady { .. } | QueueError::StorageSegmentCapFull { .. }), ) => Some(*err), _ => None, } } /// The error of a publication that waited out its append deadline on /// store-and-forward back-pressure. /// /// While the connection is down and reconnecting keeps failing, that failure is /// why no acknowledgement frees queue space, so the error names it instead of /// advising a larger `sf_max_total_bytes`, which would send the caller to fix /// the wrong thing. As for an ACK-wait timeout, the failure's diagnostics are /// kept except a server-rejection diagnostic, which marks a terminal error. fn submit_timeout_error( backpressure: Option, reconnect_failure: Option<&crate::Error>, ) -> crate::Error { use super::qwp_ws_queue::QueueError; if let Some(cause) = reconnect_failure { let waited_for = match backpressure { Some(QueueError::StorageSpareNotReady { segment_size_bytes, allocated_segment_bytes, max_total_bytes, }) => format!( "a prepared segment spare [segment_size_bytes={segment_size_bytes}, allocated_segment_bytes={allocated_segment_bytes}, max_total_bytes={max_total_bytes}]" ), Some(QueueError::StorageSegmentCapFull { segment_size_bytes, allocated_segment_bytes, max_total_bytes, }) => format!( "ACK-driven segment trim [segment_size_bytes={segment_size_bytes}, allocated_segment_bytes={allocated_segment_bytes}, max_total_bytes={max_total_bytes}]" ), _ => "local queue capacity".to_string(), }; let msg = format!( "QWP/WebSocket Store-and-Forward append timed out waiting for {waited_for}; the \ connection is down and reconnecting keeps failing: {}. The frames already \ published remain queued and the sender keeps reconnecting: retry the flush once \ that is resolved, or close the sender to give up.", cause.msg() ); return cause .clone() .without_qwp_ws_rejection() .reclassified(crate::ErrorCode::SocketError, msg); } match backpressure { Some(QueueError::StorageSpareNotReady { segment_size_bytes, allocated_segment_bytes, max_total_bytes, }) => error::fmt!( SocketError, "QWP/WebSocket Store-and-Forward append timed out waiting for a prepared segment spare [segment_size_bytes={segment_size_bytes}, allocated_segment_bytes={allocated_segment_bytes}, max_total_bytes={max_total_bytes}]" ), Some(QueueError::StorageSegmentCapFull { segment_size_bytes, allocated_segment_bytes, max_total_bytes, }) => error::fmt!( SocketError, "QWP/WebSocket Store-and-Forward append timed out waiting for ACK-driven segment trim; increase sf_max_total_bytes or drive pending acknowledgements [segment_size_bytes={segment_size_bytes}, allocated_segment_bytes={allocated_segment_bytes}, max_total_bytes={max_total_bytes}]" ), _ => error::fmt!( SocketError, "QWP/WebSocket flush timed out waiting for local queue capacity" ), } } #[cfg(test)] mod tests { use super::super::qwp_ws_driver::{ DriverError, DriverEvent, FakeOrderedServer, ReconnectReason, TableSeqTxn, TransportFailure, TransportPoll, TransportResponse, TransportSendResult, }; use super::super::qwp_ws_queue::{OutboundFrameView, SentFrame}; use super::super::qwp_ws_sfa_queue::{ SfaFrameQueue, SfaMemoryQueueOptions, SfaQueueOptions, SfaStorageStep, }; use super::*; use crate::ingress::Buffer; use crate::tests::qwp_ws_server_model::{ QwpWsModelConnection, QwpWsServerModel, buffer_of, rows_of, }; use std::sync::{Arc, mpsc}; use tempfile::TempDir; fn memory_queue(max_bytes: usize) -> SfaFrameQueue { SfaFrameQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: 256, max_bytes, }) .unwrap() } #[cfg(feature = "_oidc")] #[test] fn endpoint_aggregation_preserves_oidc_detail() { let endpoints = [QwpWsEndpoint::new("localhost".into(), "9000".into())]; let original = crate::Error::from(crate::oidc::OidcError::interaction_required("sign in")); let err = qwp_ws_all_endpoints_unreachable_error(&endpoints, Some((0, original))); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!(err.msg().contains("localhost:9000")); assert_eq!( err.oidc_error().map(crate::oidc::OidcError::kind), Some(crate::oidc::OidcErrorKind::InteractionRequired) ); } #[cfg(feature = "_oidc")] #[test] fn retry_budget_exhaustion_preserves_oidc_detail() { let original = crate::Error::from(crate::oidc::OidcError::interaction_required("sign in")) .reclassified(crate::ErrorCode::SocketError, "token provider failed"); let err = retry_budget_exhausted_error( "QWP/WebSocket initial connect", 3, Instant::now(), Some(original), ); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!(err.msg().contains("retry budget exhausted")); assert_eq!( err.oidc_error().map(crate::oidc::OidcError::kind), Some(crate::oidc::OidcErrorKind::InteractionRequired) ); } fn retriable_server_rejection(msg: &str) -> crate::Error { crate::Error::new(crate::ErrorCode::ServerRejection, msg).with_qwp_ws_rejection( crate::ingress::QwpWsSenderError { category: crate::ingress::QwpWsErrorCategory::WriteError, applied_policy: crate::ingress::QwpWsErrorPolicy::Retriable, status: Some(0x09), message: Some(msg.to_string()), message_sequence: Some(0), from_fsn: 0, to_fsn: 0, }, ) } /// A sender error that names the frames `from_fsn..=to_fsn`. fn rejection_of(from_fsn: u64, to_fsn: u64) -> crate::Error { crate::Error::new(crate::ErrorCode::ServerRejection, "schema mismatch") .with_qwp_ws_rejection(crate::ingress::QwpWsSenderError { category: crate::ingress::QwpWsErrorCategory::SchemaMismatch, applied_policy: crate::ingress::QwpWsErrorPolicy::Terminal, status: Some(0x03), message: Some("schema mismatch".to_string()), message_sequence: Some(from_fsn), from_fsn, to_fsn, }) } /// After a flush that failed part way at frame 5, calls do not fail on a /// rejection of frames of that flush once frames 0 to 4 are confirmed. /// They fail on any other error that stopped the sender, and on that /// rejection too while frames before the flush are not confirmed, as with /// durable acknowledgement the confirmation can come after the server /// answered them. #[test] fn only_a_rejection_of_the_frames_of_a_failed_flush_does_not_fail_the_calls_about_the_frames_before() { let cause = crate::Error::new(crate::ErrorCode::SymbolDictFull, "dictionary full"); let flush = AbortedFlush::new(5, cause.clone()); let after = |completed| stopping_error(Some(&flush), rejection_of(6, 6), completed); assert!(after(Some(4)).is_ok()); assert!(after(Some(7)).is_ok()); assert!(after(Some(3)).is_err()); assert!(after(None).is_err()); // Frames of the flush before it, or a range that starts there. assert!(stopping_error(Some(&flush), rejection_of(4, 6), Some(4)).is_err()); assert!(stopping_error(Some(&flush), rejection_of(5, 5), Some(4)).is_ok()); // An error that names no frames. let transport = crate::Error::new(crate::ErrorCode::SocketError, "connection lost"); assert!(stopping_error(Some(&flush), transport, Some(4)).is_err()); // No failed flush. assert!(stopping_error(None, rejection_of(6, 6), Some(4)).is_err()); // A failed flush that was the first of its sender. let first = AbortedFlush::new(0, cause); assert!(stopping_error(Some(&first), rejection_of(0, 0), None).is_ok()); } #[test] fn close_drain_timeout_carries_no_server_rejection_diagnostic() { // A close-drain timeout is not a terminal error, so it must not carry // the structured rejection that marks one (`line_sender_error_qwpws_get_view`). let cause = retriable_server_rejection("write failed"); let err = close_drain_timeout_error(3, Some(&cause), false); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert_eq!( err.msg(), "QWP/WebSocket close drain timed out before all published frames were \ acknowledged: 3 of them are not confirmed (the latest reconnect failed: write \ failed). Without sf_dir they are dropped with the sender: call wait before \ closing, raise close_flush_timeout_millis, or use sf_dir." ); assert!(err.qwp_ws_rejection().is_none()); let err = close_drain_timeout_error(1, None, false); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert_eq!( err.msg(), "QWP/WebSocket close drain timed out before all published frames were \ acknowledged: 1 of them is not confirmed. Without sf_dir they are dropped with \ the sender: call wait before closing, raise close_flush_timeout_millis, or use sf_dir." ); assert!(err.qwp_ws_rejection().is_none()); let err = close_drain_timeout_error(2, None, true); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert_eq!( err.msg(), "QWP/WebSocket close drain timed out before all published frames were \ acknowledged: 2 of them are not confirmed. They stay in the slot under sf_dir and \ the next sender on the slot sends them: to deliver them with this sender, call \ wait before closing or raise close_flush_timeout_millis." ); assert!(err.qwp_ws_rejection().is_none()); } #[test] fn append_timeout_names_a_failing_reconnect_instead_of_the_buffer_size() { use super::super::qwp_ws_queue::QueueError; let cap_full = Some(QueueError::StorageSegmentCapFull { segment_size_bytes: 512, allocated_segment_bytes: 1024, max_total_bytes: 1024, }); // Connected: the queue really is too small for what the server acks. let err = submit_timeout_error(cap_full, None); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!(err.msg().contains("increase sf_max_total_bytes"), "{err}"); // Disconnected: the failing reconnect is why nothing is acked. let cause = crate::Error::new( crate::ErrorCode::AuthError, "WebSocket upgrade authentication failed: HTTP status 401", ) .with_ws_http_status(401); let err = submit_timeout_error(cap_full, Some(&cause)); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!( err.msg() .starts_with("QWP/WebSocket Store-and-Forward append timed out"), "{err}" ); assert!( err.msg().contains( "reconnecting keeps failing: WebSocket upgrade authentication failed: HTTP status 401" ), "{err}" ); assert!(err.msg().contains("max_total_bytes=1024"), "{err}"); assert!(!err.msg().contains("increase sf_max_total_bytes"), "{err}"); assert!(!err.in_doubt()); let err = submit_timeout_error(cap_full, Some(&retriable_server_rejection("rejected"))); assert!(err.qwp_ws_rejection().is_none(), "{err}"); } #[cfg(any(unix, windows))] fn periodic_qwp_ws_config(sf_dir: &std::path::Path, sender_id: &str) -> QwpWsConfig { crate::ingress::SenderBuilder::from_conf(format!( "ws::addr=127.0.0.1:1;sf_dir={};sender_id={sender_id};\ sf_max_segment_bytes=256;sf_max_total_bytes=1024;\ sf_durability=periodic;sf_sync_interval_millis=3600000;", sf_dir.display() )) .unwrap() .qwp_ws .unwrap() } #[cfg(any(unix, windows))] struct BlockedProviderControl { started: mpsc::Receiver<()>, release: mpsc::Sender<()>, finished: mpsc::Receiver<()>, } #[cfg(any(unix, windows))] impl BlockedProviderControl { fn wait_until_started(&self) { self.started .recv_timeout(Duration::from_secs(5)) .expect("token provider did not start"); } fn release_and_wait(self) { self.release.send(()).unwrap(); self.finished .recv_timeout(Duration::from_secs(5)) .expect("token provider did not finish after release"); } } #[cfg(any(unix, windows))] fn qwp_ws_config_with_blocked_provider( sf_dir: &std::path::Path, sender_id: &str, ) -> (QwpWsConfig, BlockedProviderControl) { let (started_tx, started_rx) = mpsc::channel(); let (release_tx, release_rx) = mpsc::channel(); let (finished_tx, finished_rx) = mpsc::channel(); let release_rx = Mutex::new(release_rx); let qwp_ws = crate::ingress::SenderBuilder::from_conf(format!( "ws::addr=127.0.0.1:1;sf_dir={};sender_id={sender_id};\ sf_max_segment_bytes=4096;sf_max_total_bytes=8192;\ initial_connect_retry=async;", sf_dir.display() )) .unwrap() .qwp_ws_token_provider(move || { let _ = started_tx.send(()); release_rx.lock().unwrap().recv().unwrap(); let _ = finished_tx.send(()); Ok::<_, crate::Error>("eventual-token".to_string()) }) .unwrap() .qwp_ws .unwrap(); ( qwp_ws, BlockedProviderControl { started: started_rx, release: release_tx, finished: finished_rx, }, ) } #[test] fn ws_close_frame_orderly_detection() { assert!( WsCloseFrame { code: Some(1000), reason: String::new(), } .is_orderly() ); assert!( WsCloseFrame { code: Some(1001), reason: String::new(), } .is_orderly() ); assert!( !WsCloseFrame { code: Some(1002), reason: String::new(), } .is_orderly() ); assert!( !WsCloseFrame { code: None, reason: String::new(), } .is_orderly() ); } #[test] fn host_tracker_picks_by_state_zone_then_configured_order() { let mut tracker = QwpWsHostHealthTracker::new(3); tracker.zone_tiers[0] = QwpWsZoneTier::Other; tracker.zone_tiers[1] = QwpWsZoneTier::Same; tracker.zone_tiers[2] = QwpWsZoneTier::Unknown; assert_eq!(tracker.pick_next(), Some(1)); tracker.record_transport_error(1); assert_eq!(tracker.pick_next(), Some(2)); tracker.record_transport_error(2); assert_eq!(tracker.pick_next(), Some(0)); } #[test] fn host_tracker_demotes_midstream_failure_without_marking_attempted() { let mut tracker = QwpWsHostHealthTracker::new(2); tracker.record_success(0); tracker.begin_round(true); tracker.record_mid_stream_failure(0, Some(ReconnectReason::RetryableFailure)); assert_eq!(tracker.states[0], QwpWsHostState::FailedThisRound); assert!(!tracker.attempted_this_round[0]); assert_eq!(tracker.pick_next(), Some(1)); } #[test] fn host_tracker_marks_midstream_not_writable_as_role_reject_without_marking_attempted() { let mut tracker = QwpWsHostHealthTracker::new(2); tracker.record_success(0); tracker.begin_round(true); tracker.record_mid_stream_failure(0, Some(ReconnectReason::NotWritable)); assert_eq!(tracker.states[0], QwpWsHostState::RoleRejected); assert!(!tracker.attempted_this_round[0]); assert_eq!(tracker.pick_next(), Some(1)); } #[test] fn host_tracker_round_reset_preserves_only_latest_same_zone_healthy_state() { let mut tracker = QwpWsHostHealthTracker::new(4); tracker.record_success(0); tracker.record_success(2); tracker.record_success(1); tracker.zone_tiers[1] = QwpWsZoneTier::Other; tracker.record_role_reject(3, false); tracker.begin_round(true); assert_eq!(tracker.states[0], QwpWsHostState::HealthUnknown); assert_eq!(tracker.states[1], QwpWsHostState::HealthUnknown); assert_eq!(tracker.states[2], QwpWsHostState::Healthy); assert_eq!(tracker.states[3], QwpWsHostState::HealthUnknown); assert_eq!(tracker.pick_next(), Some(2)); } #[test] fn locked_health_access_never_holds_lock_between_operations() { // The pool's head-of-line fix relies on this: the connect round only // touches the shared tracker inside `with_tracker`, and the blocking // `establish_connection` runs *between* those calls. So the lock must // be fully released the instant each `with_tracker` returns — a // returning sender recording a transport failure, or a concurrent // cold-start borrow, must never stall behind one slow / black-holed // connect. let mutex = std::sync::Mutex::new(QwpWsHostHealthTracker::new(2)); let mut access = LockedQwpWsHealth::new(&mutex); let idx = access.with_tracker(|t| t.pick_next_and_claim()).unwrap(); assert!( mutex.try_lock().is_ok(), "health lock must be released after pick+claim, not held across the connect" ); access.with_tracker(|t| t.record_success(idx)); assert!( mutex.try_lock().is_ok(), "health lock must be released after recording the outcome" ); } #[test] fn locked_health_access_lets_another_thread_lock_between_operations() { // Concrete proof that a second actor (a returning sender / another // borrow) can take `inner.health` while a connect round is mid-flight // between its pick and its record steps. let mutex = Arc::new(std::sync::Mutex::new(QwpWsHostHealthTracker::new(2))); let mut access = LockedQwpWsHealth::new(&mutex); let idx = access.with_tracker(|t| t.pick_next_and_claim()).unwrap(); // Simulates the gap during which `establish_connection` blocks: no // health lock is held here, so another thread grabs it freely. let other = Arc::clone(&mutex); std::thread::spawn(move || { other .lock() .unwrap_or_else(|e| e.into_inner()) .record_mid_stream_failure(1, Some(ReconnectReason::RetryableFailure)); }) .join() .unwrap(); access.with_tracker(|t| t.record_transport_error(idx)); } #[test] fn pick_next_and_claim_rotates_concurrent_picks_off_the_same_endpoint() { // Claiming the endpoint *while the lock is still held* means a second // borrow that picks before the first has recorded its outcome rotates // to a different peer instead of piling onto the same slow endpoint. let mut tracker = QwpWsHostHealthTracker::new(2); let a = tracker.pick_next_and_claim().unwrap(); let b = tracker.pick_next_and_claim().unwrap(); assert_ne!(a, b, "concurrent claims must target distinct endpoints"); assert!(tracker.is_round_exhausted()); assert_eq!(tracker.pick_next_and_claim(), None); // A fresh round re-probes from scratch. tracker.begin_round(true); assert!(tracker.pick_next_and_claim().is_some()); } #[test] fn connect_round_start_resets_and_claims_single_endpoint_atomically() { let mut tracker = QwpWsHostHealthTracker::new(1); tracker.record_success(0); assert!(tracker.is_round_exhausted()); assert_eq!(tracker.pick_next_and_claim(), None); assert_eq!(tracker.pick_next_and_claim_connect_round(true), Some(0)); assert!(tracker.is_round_exhausted()); tracker.record_transport_error(0); assert_eq!(tracker.pick_next_and_claim_connect_round(false), None); assert_eq!(tracker.pick_next_and_claim_connect_round(true), Some(0)); } #[test] fn role_reject_helpers_use_structured_error_not_message_prefix() { let string_only = crate::Error::new( crate::ErrorCode::SocketError, "QWP/WebSocket upgrade rejected by role=PRIMARY_CATCHUP zone=az-a", ); assert!(!is_qwp_ws_role_reject_error(&string_only)); assert!(!qwp_ws_role_reject_is_transient(&string_only)); assert_eq!(qwp_ws_role_reject_zone(&string_only), None); let structured = crate::Error::new(crate::ErrorCode::SocketError, "unrelated message") .with_qwp_ws_role_reject(crate::ingress::QwpWsRoleReject::new( "primary_catchup", Some("az-a"), )); assert!(is_qwp_ws_role_reject_error(&structured)); assert!(qwp_ws_role_reject_is_transient(&structured)); assert_eq!(qwp_ws_role_reject_zone(&structured), Some("az-a")); } #[test] fn backpressure_notifier_remembers_progress_before_wait() { let notifier = BackpressureNotifier::new(); let generation = notifier.generation(); notifier.notify_all(); assert!(notifier.wait_for_change(generation, Some(Instant::now()))); } #[test] fn backpressure_notifier_deadline_expires_without_progress() { let notifier = BackpressureNotifier::new(); let generation = notifier.generation(); assert!(!notifier.wait_for_change(generation, Some(Instant::now()))); } #[test] fn pending_connect_terminal_failure_with_poisoned_store_wakes_waiters() { let qwp_ws = crate::ingress::SenderBuilder::from_conf("ws::addr=127.0.0.1:1;") .unwrap() .qwp_ws .unwrap(); let mut pending_connect = QwpWsPendingConnect::new( "127.0.0.1", "1", false, None, &qwp_ws, None, DurableAckTiers::Off, Arc::new(AtomicUsize::new(0)), Arc::new(TrafficGate::default()), false, Vec::new(), 0, ); // Fail before any real socket operation, through the production // pending-connect error branch. pending_connect.terminal_error_for_test = Some(crate::Error::new( crate::ErrorCode::AuthError, "injected initial-connect failure", )); let mut store = QwpWsPublicationStore::new(memory_queue(1024), 32); store.try_submit(b"waiting").unwrap(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let poison_target = Arc::clone(&shared); assert!( thread::spawn(move || { let _guard = poison_target.lock().unwrap(); panic!("injected store-lock poison"); }) .join() .is_err() ); let notifier = Arc::new(BackpressureNotifier::new()); let generation = notifier.generation(); let mut core = SyncQwpWsPendingRunnerCore { connected: None, pending_connect, progress, backpressure: Arc::clone(¬ifier), ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle: lifecycle.clone(), }; assert_eq!( core.drive_step(&shared, &AtomicBool::new(false)), RunnerStep::Stop ); assert!(lifecycle.is_terminal()); assert_ne!(notifier.generation(), generation); assert!( notifier.wait_for_change(generation, None), "even an unlimited wait must observe the poisoned initial-connect failure" ); } fn pooled_boundary_wait_sender( transport: T, durable: DurableAckTiers, ) -> ( crate::ingress::column_sender::PooledSenderCore, Arc, ) { let queue = SfaSlotQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: 4096, max_bytes: 8192, }) .unwrap(); let mut driver = QwpWsCoreTestHarness::from_queue_with_reconnect_policy( queue, transport, ReconnectPolicy::bounded(Duration::MAX, Duration::ZERO, Duration::ZERO), durable, ); driver.try_submit(b"boundary").unwrap(); let runner = SyncQwpWsRunner::start(driver); let notifier = Arc::clone(&runner.backpressure); let state = SyncQwpWsHandlerState { encoder: QwpWsReplayEncoder::new(1), runner, server_max_batch_size: Arc::new(AtomicUsize::new(4096)), request_durable_ack: durable, token_provider: None, sfa_frame_payload_cap: segment_payload_capacity(4096), sfa_frame_split_target: two_frame_segment_payload_capacity(4096), orphan_pool: None, close_drain_timeout: Duration::ZERO, delta_dict_enabled: false, recovered_dict_entries: Vec::new(), recovered_dict_count: 0, persisted_symbol_dict: None, sfa_split_deferral_enabled: true, }; let sender = crate::ingress::column_sender::PooledSenderCore::new_store_and_forward( state, 4096, durable, Duration::from_secs(5), ) .unwrap(); (sender, notifier) } #[test] fn pooled_boundary_wait_unrelated_notifications_do_not_extend_timeout() { let (mut sender, notifier) = pooled_boundary_wait_sender(FakeOrderedServer::no_response(), DurableAckTiers::Off); let (stop_tx, stop_rx) = mpsc::channel(); let (result_tx, result_rx) = mpsc::channel(); std::thread::scope(|scope| { let notifying = scope.spawn(move || { while matches!( stop_rx.recv_timeout(Duration::from_millis(5)), Err(mpsc::RecvTimeoutError::Timeout) ) { notifier.notify_all(); } }); let waiting = scope.spawn(move || { let result = sender.wait(crate::ingress::AckLevel::Ok, Duration::from_millis(40)); let _ = result_tx.send(result); }); let result = result_rx.recv_timeout(Duration::from_secs(5)); let _ = stop_tx.send(()); notifying.join().unwrap(); waiting.join().unwrap(); let err = result .expect("unrelated notifications must not renew the no-progress deadline") .unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::FailoverRetry); assert!(err.msg().contains("no ack progress")); }); } #[test] fn pooled_boundary_wait_wakes_for_ack_and_terminal_error() { for terminal in [false, true] { let (sent_tx, sent_rx) = mpsc::channel(); let (ack_tx, ack_rx) = mpsc::channel(); let (terminal_tx, terminal_rx) = mpsc::channel(); let transport = SignalResponseTransport { sent_frame: sent_tx, ack: ack_rx, terminal: terminal_rx, sent_frames: Vec::new(), }; let (mut sender, notifier) = pooled_boundary_wait_sender(transport, DurableAckTiers::Off); sent_rx.recv_timeout(Duration::from_secs(5)).unwrap(); let (waiting_tx, waiting_rx) = mpsc::channel(); let (result_tx, result_rx) = mpsc::channel(); std::thread::scope(|scope| { let waiting = scope.spawn(move || { waiting_tx.send(()).unwrap(); let result = sender.wait(crate::ingress::AckLevel::Ok, Duration::from_secs(10)); let _ = result_tx.send(result); }); waiting_rx.recv_timeout(Duration::from_secs(5)).unwrap(); if terminal { terminal_tx.send(()).unwrap(); } else { ack_tx.send(()).unwrap(); } let result = result_rx.recv_timeout(Duration::from_secs(2)); if result.is_err() { // Rescue the waiter before asserting a missing notification; // reaching its own deadline must not make this test pass. notifier.notify_all(); } waiting.join().unwrap(); let result = result.expect("ACK and terminal errors must wake a boundary wait"); if terminal { let err = result.unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!(err.msg().contains("synthetic terminal failure")); } else { result.unwrap(); } }); } } #[test] fn pooled_boundary_wait_keeps_ok_and_durable_notifications_distinct() { let ack_ready = Arc::new(AtomicBool::new(false)); let transport = DurableAckWhenReadyTransport { ack_ready: Arc::clone(&ack_ready), sent_frames: Vec::new(), }; let (mut sender, _) = pooled_boundary_wait_sender(transport, DurableAckTiers::LegacyReplicated); sender .wait(crate::ingress::AckLevel::Ok, Duration::from_secs(5)) .unwrap(); assert_eq!(sender.acked_fsn().unwrap(), None); let err = sender .wait(crate::ingress::AckLevel::Durable, Duration::from_millis(20)) .unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::FailoverRetry); ack_ready.store(true, Ordering::Release); sender .wait(crate::ingress::AckLevel::Durable, Duration::from_secs(5)) .unwrap(); assert_eq!(sender.acked_fsn().unwrap(), Some(0)); } #[test] fn effective_connect_timeout_defaults_only_for_bounded_dials() { let explicit = Some(Duration::from_millis(250)); assert_eq!( effective_connect_timeout(true, None), Some(Duration::from_secs(15)) ); assert_eq!(effective_connect_timeout(true, explicit), explicit); assert_eq!(effective_connect_timeout(false, None), None); assert_eq!(effective_connect_timeout(false, explicit), explicit); } #[test] fn connect_kind_bounds_reconnects_and_background_dials() { assert!(!QwpWsConnectKind::Foreground.bounded_dial()); assert!(QwpWsConnectKind::ForegroundBounded.bounded_dial()); assert!(QwpWsConnectKind::BackgroundDrainer.bounded_dial()); assert_eq!( QwpWsConnectKind::Foreground.for_reconnect(), QwpWsConnectKind::ForegroundBounded ); assert_eq!( QwpWsConnectKind::ForegroundBounded.for_reconnect(), QwpWsConnectKind::ForegroundBounded ); assert_eq!( QwpWsConnectKind::BackgroundDrainer.for_reconnect(), QwpWsConnectKind::BackgroundDrainer ); } /// A refreshing provider whose every fetch stalls until `release` fires. fn stalled_refreshing_provider( cold_wait: Duration, ) -> ( crate::token_provider::RefreshingTokenProvider, mpsc::Sender<()>, ) { let (release, released) = mpsc::channel::<()>(); let released = Mutex::new(released); let provider = crate::token_provider::RefreshingTokenProvider::builder( move || -> crate::Result { let _ = released .lock() .unwrap() .recv_timeout(Duration::from_secs(30)); Err(crate::Error::token_unavailable( true, None, "stalled source", )) }, ) .name("stalled") .cold_wait(cold_wait) .build() .unwrap(); (provider, release) } #[test] fn a_manual_reconnect_does_not_wait_for_a_refreshing_provider() { // A manual-progress reconnect, and a manual orphan drainer's connect, // run inside a caller's close_drain, wait or flush: waiting out the // cold wait there overran the caller's deadline. let (refreshing, release) = stalled_refreshing_provider(Duration::from_secs(20)); let provider = crate::token_provider::TokenProvider::new(refreshing.clone()); for kind in [ QwpWsConnectKind::ForegroundBounded, QwpWsConnectKind::BackgroundDrainer, ] { let started = Instant::now(); let err = acquire_qwp_ws_provider_header(&provider, kind, None) .expect_err("no token is held"); assert!( started.elapsed() < Duration::from_secs(5), "{kind:?} waited {:?} for the provider", started.elapsed() ); assert_eq!(err.code(), crate::ErrorCode::SocketError, "{err}"); assert!(err.is_provider_failure(), "{err}"); assert!(err.msg().contains("no usable token"), "{err}"); } let _ = release.send(()); refreshing.close(); // A connect the caller waits for still waits for the provider's fetch. let (refreshing, release) = stalled_refreshing_provider(Duration::from_millis(300)); let provider = crate::token_provider::TokenProvider::new(refreshing.clone()); let started = Instant::now(); acquire_qwp_ws_provider_header(&provider, QwpWsConnectKind::Foreground, None) .expect_err("no token is held"); assert!( started.elapsed() >= Duration::from_millis(250), "a waited-for connect gave up after {:?}", started.elapsed() ); let _ = release.send(()); refreshing.close(); } #[test] fn traffic_gate_shutdown_is_sticky_across_socket_registration() { use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let client = TcpStream::connect(listener.local_addr().unwrap()).unwrap(); let (mut peer, _) = listener.accept().unwrap(); peer.set_read_timeout(Some(Duration::from_secs(1))).unwrap(); let gate = TrafficGate::default(); gate.register(&client).unwrap().keep(); gate.shutdown().unwrap(); let mut byte = [0u8; 1]; assert_eq!(peer.read(&mut byte).unwrap(), 0); let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let client = TcpStream::connect(listener.local_addr().unwrap()).unwrap(); let (mut peer, _) = listener.accept().unwrap(); peer.set_read_timeout(Some(Duration::from_secs(1))).unwrap(); let err = match gate.register(&client) { Ok(_) => panic!("a socket registered after shutdown must be interrupted"), Err(err) => err, }; assert_eq!(err.kind(), ErrorKind::Interrupted); assert_eq!(peer.read(&mut byte).unwrap(), 0); } fn reject_websocket_upgrade(stream: &mut (impl Read + Write)) { let mut request = Vec::new(); let mut chunk = [0u8; 256]; while !request.windows(4).any(|window| window == b"\r\n\r\n") { let read = stream.read(&mut chunk).unwrap(); assert!(read > 0, "client closed before sending the upgrade request"); request.extend_from_slice(&chunk[..read]); } stream .write_all( b"HTTP/1.1 101 Switching Protocols\r\n\ Upgrade: websocket\r\n\ Connection: Upgrade\r\n\ Sec-WebSocket-Accept: invalid\r\n\ \r\n", ) .unwrap(); stream.flush().unwrap(); } fn test_tls_server_config() -> Arc { use rustls_pki_types::pem::PemObject; use rustls_pki_types::{CertificateDer, PrivateKeyDer}; use std::fs::File; let mut certs_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); certs_dir.pop(); certs_dir.push("tls_certs"); let cert_file = File::open(certs_dir.join("server.crt")).unwrap(); let key_file = File::open(certs_dir.join("server.key")).unwrap(); let certs = CertificateDer::pem_reader_iter(cert_file) .collect::, _>>() .unwrap(); let key = PrivateKeyDer::from_pem_reader(key_file).unwrap(); Arc::new( rustls::ServerConfig::builder() .with_no_client_auth() .with_single_cert(certs, key) .unwrap(), ) } #[test] fn failed_upgrade_clears_traffic_gate_before_original_socket_closes() { use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let server = std::thread::spawn(move || { let (mut peer, _) = listener.accept().unwrap(); peer.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); reject_websocket_upgrade(&mut peer); }); let qwp_ws = crate::ingress::SenderBuilder::from_conf(format!("ws::addr=127.0.0.1:{port};")) .unwrap() .qwp_ws .unwrap(); let gate = TrafficGate::default(); let result = establish_connection( "127.0.0.1", &port.to_string(), false, None, QwpWsConnectKind::Foreground, &qwp_ws, None, Some(&gate), ); assert!(result.is_err()); server.join().unwrap(); } #[test] fn failed_tls_upgrade_clears_traffic_gate_before_original_socket_closes() { use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let server_config = test_tls_server_config(); let server = std::thread::spawn(move || { let (mut peer, _) = listener.accept().unwrap(); peer.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); let mut conn = rustls::ServerConnection::new(server_config).unwrap(); let mut tls_stream = rustls::Stream::new(&mut conn, &mut peer); reject_websocket_upgrade(&mut tls_stream); }); let mut root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); root.pop(); root.push("tls_certs/server_rootCA.pem"); let builder = crate::ingress::SenderBuilder::from_conf(format!( "wss::addr=localhost:{port};tls_roots={};", root.display() )) .unwrap(); let (use_tls, tls_settings, qwp_ws, auth_header) = builder.resolve_qwp_ws_ingredients().unwrap(); let gate = TrafficGate::default(); let result = establish_connection( "localhost", &port.to_string(), use_tls, tls_settings, QwpWsConnectKind::Foreground, &qwp_ws, auth_header.as_deref(), Some(&gate), ); assert!(result.is_err()); server.join().unwrap(); } #[test] fn connect_tcp_to_any_addr_falls_back_after_refused_ipv6() { use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let accepted = std::thread::spawn(move || listener.accept().unwrap()); let bad_v6 = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, port, 0, 0)); let good_v4 = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)); let tcp = connect_tcp_to_any_addr( "localhost", &port.to_string(), &[bad_v6, good_v4], Duration::from_secs(1), None, None, ) .unwrap(); drop(tcp); let _ = accepted.join().unwrap(); } #[test] fn connect_tcp_to_any_addr_times_out_against_blackhole() { use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; // 192.0.2.1 is RFC 5737 TEST-NET-1: globally unrouted, so the SYN is // dropped and the dial blocks until connect_timeout fires (instead of // the OS default, which is tens of seconds). let blackhole = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 19009)); let start = std::time::Instant::now(); let err = match connect_tcp_to_any_addr( "192.0.2.1", "19009", &[blackhole], Duration::from_secs(1), Some(Duration::from_millis(300)), None, ) { Err(e) => e, Ok(_) => panic!("dialing a blackhole endpoint must not succeed"), }; assert_eq!( err.code(), crate::ErrorCode::ConnectTimeout, "expected ConnectTimeout, got {:?}: {}", err.code(), err.msg() ); assert!( start.elapsed() < Duration::from_secs(5), "the dial must be bounded by connect_timeout (~300ms)" ); } #[test] fn connect_tcp_to_any_addr_stops_between_addresses_after_gate_shutdown() { use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; let gate = TrafficGate::default(); gate.shutdown().unwrap(); let blackhole = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 19009)); let start = std::time::Instant::now(); let err = match connect_tcp_to_any_addr( "192.0.2.1", "19009", &[blackhole], Duration::from_secs(1), Some(Duration::from_millis(300)), Some(&gate), ) { Err(e) => e, Ok(_) => panic!("a shut gate must stop the dial walk"), }; assert!( err.msg().contains("shutting down during connect"), "unexpected error: {}", err.msg() ); assert!( start.elapsed() < Duration::from_millis(100), "a shut gate must skip the dial entirely" ); } #[test] fn connect_tcp_sets_post_connect_io_timeout() { use std::net::{Ipv4Addr, TcpListener}; let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let accepted = std::thread::spawn(move || listener.accept().unwrap()); let io_timeout = Duration::from_millis(250); let tcp = connect_qwp_ws_tcp("127.0.0.1", &port.to_string(), io_timeout, None, None).unwrap(); assert_eq!(tcp.tcp().read_timeout().unwrap(), Some(io_timeout)); assert_eq!(tcp.tcp().write_timeout().unwrap(), Some(io_timeout)); drop(tcp); let _ = accepted.join().unwrap(); } #[test] fn frame_short_payload_is_masked() { let mut out = Vec::new(); let payload = b"hello"; codec::write_frame_to_buf(&mut out, Opcode::Binary, payload, [0; 4]); assert_eq!(out[0], 0x82); // FIN | binary assert_eq!(out[1] & 0x80, 0x80); // masked assert_eq!(out[1] & 0x7F, 5); // length } #[test] fn write_ping_frame_masks_client_ping() { let mut stream = InMemoryWs::new(Vec::new()); let mut scratch = Vec::new(); write_ping_frame(&mut stream, &mut scratch, b"da").unwrap(); assert_eq!(stream.written[0], 0x80 | OPCODE_PING); assert_eq!(stream.written[1] & 0x80, 0x80); assert_eq!(stream.written[1] & 0x7f, 2); } #[test] fn masked_server_frame_is_protocol_error() { let mut masked_frame = Vec::new(); codec::write_frame_to_buf(&mut masked_frame, Opcode::Binary, b"hello", [0; 4]); let err = read_message( &mut std::io::Cursor::new(masked_frame), &mut Vec::new(), &mut Vec::new(), ) .unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!( err.msg().contains("must not be masked"), "got: {}", err.msg() ); } #[test] fn frame_reader_rejects_rsv_bits_without_extension() { let mut frame = Vec::new(); append_server_frame(&mut frame, true, OPCODE_BINARY, b"hello"); frame[0] |= 0x40; let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame); let mut written = Vec::new(); let mut scratch = Vec::new(); let err = reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap_err(); assert_protocol_violation_contains(err, "RSV bits"); } #[test] fn frame_reader_rejects_one_byte_close_payload() { let mut frame = Vec::new(); append_server_frame(&mut frame, true, OPCODE_CLOSE, &[0x03]); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame); let mut written = Vec::new(); let mut scratch = Vec::new(); let err = reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap_err(); assert_protocol_violation_contains(err, "payload length must be 0 or at least 2 bytes"); } #[test] fn frame_reader_rejects_non_minimal_16_bit_payload_length() { let frame = [0x80 | OPCODE_BINARY, 126, 0, 125]; let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame); let mut written = Vec::new(); let mut scratch = Vec::new(); let err = reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap_err(); assert_protocol_violation_contains(err, "non-minimal 16-bit encoding"); } #[test] fn frame_reader_rejects_non_minimal_64_bit_payload_length() { let mut frame = vec![0x80 | OPCODE_BINARY, 127]; frame.extend_from_slice(&0xffffu64.to_be_bytes()); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame); let mut written = Vec::new(); let mut scratch = Vec::new(); let err = reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap_err(); assert_protocol_violation_contains(err, "non-minimal 64-bit encoding"); } struct InMemoryWs { read: std::io::Cursor>, written: Vec, } impl InMemoryWs { fn new(read: Vec) -> Self { Self { read: std::io::Cursor::new(read), written: Vec::new(), } } } impl std::io::Read for InMemoryWs { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { std::io::Read::read(&mut self.read, buf) } } impl std::io::Write for InMemoryWs { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.written.extend_from_slice(buf); Ok(buf.len()) } fn flush(&mut self) -> std::io::Result<()> { Ok(()) } } struct UpgradeResponseWithFrame { written: Vec, read: std::io::Cursor>, payload: Vec, response_built: bool, } impl UpgradeResponseWithFrame { fn new(payload: &[u8]) -> Self { Self { written: Vec::new(), read: std::io::Cursor::new(Vec::new()), payload: payload.to_vec(), response_built: false, } } fn build_response(&mut self) { let request = std::str::from_utf8(&self.written).unwrap(); let key = request .split("\r\n") .find_map(|line| { let (name, value) = line.split_once(':')?; name.eq_ignore_ascii_case("sec-websocket-key") .then(|| value.trim()) }) .unwrap(); let accept = crate::ws::crypto::compute_accept(key); let mut response = format!( "HTTP/1.1 101 Switching Protocols\r\n\ Upgrade: websocket\r\n\ Connection: Upgrade\r\n\ Sec-WebSocket-Accept: {accept}\r\n\ X-QWP-Version: 1\r\n\ \r\n" ) .into_bytes(); append_server_frame(&mut response, true, OPCODE_BINARY, &self.payload); self.read = std::io::Cursor::new(response); self.response_built = true; } } impl std::io::Read for UpgradeResponseWithFrame { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { if !self.response_built { self.build_response(); } std::io::Read::read(&mut self.read, buf) } } impl std::io::Write for UpgradeResponseWithFrame { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.written.extend_from_slice(buf); Ok(buf.len()) } fn flush(&mut self) -> std::io::Result<()> { Ok(()) } } fn append_server_frame(out: &mut Vec, fin: bool, opcode: u8, payload: &[u8]) { let fin_bit = if fin { 0x80 } else { 0x00 }; out.push(fin_bit | (opcode & 0x0F)); let plen = payload.len(); if plen <= 125 { out.push(plen as u8); } else if plen <= 0xFFFF { out.push(126); out.extend_from_slice(&(plen as u16).to_be_bytes()); } else { out.push(127); out.extend_from_slice(&(plen as u64).to_be_bytes()); } out.extend_from_slice(payload); } #[test] fn perform_upgrade_preserves_coalesced_websocket_frame() { // Server coalesces a WS data frame onto the tail of the upgrade // response. The shared `handshake::upgrade` MUST surface those // bytes via `Handshake.leftover` so the ingress frame reader can // consume them without losing the leading frame. let mut stream = UpgradeResponseWithFrame::new(b"\x02\x00"); let extras = codec::qwp_extra_headers(None, 1, None, DurableAckTiers::Off); let handshake = crate::ws::handshake::upgrade(&mut stream, "localhost:9000", codec::WS_PATH, &extras) .unwrap(); let result = codec::validate_qwp_handshake_headers(&handshake.headers, 1, DurableAckTiers::Off) .unwrap(); let leftover = handshake.leftover; assert_eq!(result.version, 1); let mut reader = WsFrameReader::with_initial_input(leftover); let mut written = Vec::new(); let mut scratch = Vec::new(); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Message { opcode: OPCODE_BINARY } ); assert_eq!(reader.message(), b"\x02\x00"); } fn assert_protocol_violation_contains(err: WsMessageError, needle: &str) { match err { WsMessageError::ProtocolViolation(reason) => { assert!(reason.contains(needle), "got: {reason}"); } other => panic!("expected protocol violation, got {other:?}"), } } #[test] fn normal_close_is_reconnectable_role_movement_shape() { let mut frames = Vec::new(); append_server_frame(&mut frames, true, OPCODE_CLOSE, &1000u16.to_be_bytes()); let mut stream = InMemoryWs::new(frames); let mut scratch = Vec::new(); let mut out = Vec::new(); let err = read_message_with_close(&mut stream, &mut scratch, &mut out).unwrap_err(); match err { WsMessageError::Close(close) => { assert_eq!(close.code, Some(1000)); assert!(close.reason.is_empty()); } other => panic!("expected normal close, got {other:?}"), } } #[test] fn read_message_preserves_fragment_across_ping() { let mut frames = Vec::new(); append_server_frame(&mut frames, false, OPCODE_BINARY, b"hello "); append_server_frame(&mut frames, true, OPCODE_PING, b"p"); append_server_frame(&mut frames, true, OPCODE_CONTINUATION, b"world"); let mut stream = InMemoryWs::new(frames); let mut scratch = Vec::new(); let mut out = Vec::new(); let opcode = read_message_with_close(&mut stream, &mut scratch, &mut out).unwrap(); assert_eq!(opcode, OPCODE_BINARY); assert_eq!(out, b"hello world"); assert!(!stream.written.is_empty()); } #[test] fn frame_reader_rejects_fragmented_message_over_aggregate_limit() { let mut frames = Vec::new(); append_server_frame(&mut frames, false, OPCODE_BINARY, b"abc"); append_server_frame(&mut frames, true, OPCODE_CONTINUATION, b"def"); let mut reader = WsFrameReader::with_max_message_bytes_for_test(5); reader.append_input_for_test(&frames); let mut written = Vec::new(); let mut scratch = Vec::new(); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Progress ); let err = reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap_err(); assert_protocol_violation_contains(err, "WebSocket message too large: 6 bytes"); assert_eq!(reader.message(), b"abc"); } #[test] fn frame_reader_consumes_pong_then_buffered_binary_message() { let mut frames = Vec::new(); append_server_frame(&mut frames, true, OPCODE_PONG, b""); append_server_frame(&mut frames, true, OPCODE_BINARY, b"\x02\x00"); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frames); let mut written = Vec::new(); let mut scratch = Vec::new(); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Progress ); assert!(written.is_empty()); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Message { opcode: OPCODE_BINARY } ); assert_eq!(reader.message(), b"\x02\x00"); } #[test] fn frame_reader_answers_ping_and_returns_progress() { let mut frame = Vec::new(); append_server_frame(&mut frame, true, OPCODE_PING, b"p"); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame); let mut written = Vec::new(); let mut scratch = Vec::new(); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Progress ); assert_eq!(written[0], 0x80 | OPCODE_PONG); assert_eq!(written[1] & 0x80, 0x80); assert_eq!(written[1] & 0x7f, 1); } #[test] fn frame_reader_preserves_incomplete_buffered_payload_until_complete() { let mut frame = Vec::new(); append_server_frame(&mut frame, true, OPCODE_BINARY, b"ok"); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frame[..3]); let mut written = Vec::new(); let mut scratch = Vec::new(); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Idle ); reader.append_input_for_test(&frame[3..]); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Message { opcode: OPCODE_BINARY } ); assert_eq!(reader.message(), b"ok"); } #[test] fn frame_reader_compacts_consumed_small_frames() { let mut frames = Vec::new(); for _ in 0..2500 { append_server_frame(&mut frames, true, OPCODE_PONG, b""); } append_server_frame(&mut frames, true, OPCODE_BINARY, b"done"); let original_len = frames.len(); let mut reader = WsFrameReader::new(); reader.append_input_for_test(&frames); let mut written = Vec::new(); let mut scratch = Vec::new(); for _ in 0..2500 { assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Progress ); } assert!( reader.input.len() < original_len, "reader did not compact consumed frames" ); assert_eq!( reader .try_read_buffered_for_test(&mut written, &mut scratch) .unwrap(), WsFrameRead::Message { opcode: OPCODE_BINARY } ); assert_eq!(reader.message(), b"done"); } /// Run with: /// `cargo test --features sync-sender-qwp-ws --lib read_message_zero_alloc_after_warmup -- --ignored --test-threads=1` #[test] #[ignore] fn read_message_zero_alloc_after_warmup() { use crate::alloc_counter; let mut frame = Vec::new(); append_server_frame(&mut frame, true, OPCODE_BINARY, b"\x00\x00"); let mut scratch = Vec::with_capacity(64); let mut out = Vec::with_capacity(64); let mut warmup = InMemoryWs::new(frame.clone()); assert_eq!( read_message_with_close(&mut warmup, &mut scratch, &mut out).unwrap(), OPCODE_BINARY ); assert_eq!(out, b"\x00\x00"); let mut counted = InMemoryWs::new(frame); alloc_counter::start_counting(); assert_eq!( read_message_with_close(&mut counted, &mut scratch, &mut out).unwrap(), OPCODE_BINARY ); let alloc_count = alloc_counter::stop_counting(); assert_eq!(out, b"\x00\x00"); assert_eq!( alloc_count, 0, "Expected zero allocations for warmed QWP/WebSocket inbound message read, got {alloc_count}" ); } #[derive(Debug)] struct BlockingFirstSendTransport { send_started: mpsc::Sender<()>, release_send: mpsc::Receiver<()>, should_block_send: bool, sent_frames: Vec, } impl QwpWsCoreTransport for BlockingFirstSendTransport { fn try_poll_response(&mut self) -> Result { Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { let sent_frame = frame.sent_frame(); if self.should_block_send { self.should_block_send = false; self.send_started.send(()).unwrap(); self.release_send .recv_timeout(Duration::from_secs(5)) .unwrap(); } self.sent_frames.push(sent_frame); Ok(TransportSendResult::NoResponse) } } #[derive(Debug)] struct AckWhenReadyTransport { ack_ready: Arc, sent_frames: Vec, } impl QwpWsCoreTransport for AckWhenReadyTransport { fn try_poll_response(&mut self) -> Result { if self.ack_ready.swap(false, Ordering::AcqRel) { Ok(TransportPoll::Response(TransportResponse::Ack { wire_seq: 0, })) } else { Ok(TransportPoll::Idle) } } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { self.sent_frames.push(frame.sent_frame()); Ok(TransportSendResult::NoResponse) } } #[derive(Debug)] struct DurableAckWhenReadyTransport { ack_ready: Arc, sent_frames: Vec, } impl DurableAckWhenReadyTransport { fn table_seq_txns() -> Vec { vec![TableSeqTxn { table: "trades".to_string(), seq_txn: 10, }] } } impl QwpWsCoreTransport for DurableAckWhenReadyTransport { fn try_poll_response(&mut self) -> Result { if self.ack_ready.swap(false, Ordering::AcqRel) { Ok(TransportPoll::Response(TransportResponse::DurableAck { table_seq_txns: Self::table_seq_txns(), local: false, })) } else { Ok(TransportPoll::Idle) } } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { self.sent_frames.push(frame.sent_frame()); Ok(TransportSendResult::Response( TransportResponse::DurableOk { wire_seq: 0, table_seq_txns: Self::table_seq_txns(), }, )) } } #[test] fn threaded_runner_accepts_publication_while_transport_send_is_blocked() { let (send_started_tx, send_started_rx) = mpsc::channel(); let (release_send_tx, release_send_rx) = mpsc::channel(); let queue = memory_queue(1024); let transport = BlockingFirstSendTransport { send_started: send_started_tx, release_send: release_send_rx, should_block_send: true, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(queue, transport); let mut runner = SyncQwpWsRunner::start(driver); runner.publish_replay_payload(b"first").unwrap(); send_started_rx .recv_timeout(Duration::from_secs(5)) .unwrap(); std::thread::scope(|scope| { let (published_tx, published_rx) = mpsc::channel(); let runner = &mut runner; let publish_thread = scope.spawn(move || { let result = runner.publish_replay_payload(b"second"); let _ = published_tx.send(result.map(|_| ())); }); let publish_result = match published_rx.recv_timeout(Duration::from_secs(2)) { Ok(result) => result, Err(err) => { let _ = release_send_tx.send(()); publish_thread.join().unwrap(); panic!("publication waited for blocked transport send: {err:?}"); } }; publish_result.unwrap(); publish_thread.join().unwrap(); }); release_send_tx.send(()).unwrap(); drop(runner); } #[cfg(any(unix, windows))] #[test] fn runner_shutdown_timeout_keeps_slot_locked_until_worker_exit() { let dir = TempDir::new().unwrap(); let slot_options = SfaSlotOptions { sf_dir: dir.path().to_path_buf(), sender_id: "blocked-runner".to_string(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }; let queue = SfaSlotQueue::open(slot_options.clone()).unwrap(); let (send_started_tx, send_started_rx) = mpsc::channel(); let (release_send_tx, release_send_rx) = mpsc::channel(); let transport = BlockingFirstSendTransport { send_started: send_started_tx, release_send: release_send_rx, should_block_send: true, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(queue, transport); let mut runner = SyncQwpWsRunner::start(driver); runner.shutdown_timeout = Duration::from_millis(50); runner.publish_replay_payload(b"blocked").unwrap(); send_started_rx .recv_timeout(Duration::from_secs(5)) .unwrap(); let started = Instant::now(); drop(runner); assert!( started.elapsed() < Duration::from_secs(1), "runner drop exceeded its shutdown backstop" ); match SfaSlotQueue::open(slot_options.clone()) { Err(SfaQueueError::SlotInUse { .. }) => {} Err(err) => panic!("expected the detached worker to retain the slot lock: {err:?}"), Ok(_) => panic!("detached worker released its slot before exiting"), } release_send_tx.send(()).unwrap(); let deadline = Instant::now() + Duration::from_secs(5); loop { match SfaSlotQueue::open(slot_options.clone()) { Ok(queue) => { // The unacked frame must survive the detached worker's // exit and replay to the next adopter of the slot. let mut driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::ack_each_send()); assert_eq!( driver.drive_once().unwrap(), DriveOutcome::Acked { wire_seq: 0 } ); assert_eq!( driver.poll_event(), Some(DriverEvent::Sent { fsn: 0, wire_seq: 0 }) ); break; } Err(SfaQueueError::SlotInUse { .. }) if Instant::now() < deadline => { thread::sleep(Duration::from_millis(10)); } Err(err) => panic!("slot did not become reusable after worker exit: {err:?}"), } } } #[cfg(any(unix, windows))] #[test] fn async_initial_connect_shutdown_releases_slot_while_provider_is_blocked() { let dir = TempDir::new().unwrap(); let (qwp_ws, provider) = qwp_ws_config_with_blocked_provider(dir.path(), "blocked-initial-provider"); let queue = open_configured_qwp_ws_queue(&qwp_ws).unwrap(); let traffic_gate = Arc::new(TrafficGate::default()); let pending_connect = QwpWsPendingConnect::new( "127.0.0.1", "1", false, None, &qwp_ws, None, *qwp_ws.request_durable_ack, Arc::new(AtomicUsize::new(0)), Arc::clone(&traffic_gate), false, Vec::new(), 0, ); let mut runner = SyncQwpWsRunner::start_pending_connect( queue, pending_connect, Duration::from_secs(1), *qwp_ws.error_inbox_capacity, None, ); runner.shutdown_timeout = Duration::from_millis(100); provider.wait_until_started(); let started = Instant::now(); drop(runner); let drop_elapsed = started.elapsed(); let reopened = open_configured_qwp_ws_queue(&qwp_ws); provider.release_and_wait(); assert!( drop_elapsed < Duration::from_secs(1), "runner shutdown waited for the blocked token provider: {drop_elapsed:?}" ); drop(reopened.unwrap_or_else(|err| { panic!("blocked initial-connect provider retained the SFA slot after shutdown: {err}") })); } #[cfg(any(unix, windows))] #[test] fn reconnect_shutdown_releases_slot_while_provider_is_blocked() { use std::net::{Ipv4Addr, TcpListener}; let dir = TempDir::new().unwrap(); let (qwp_ws, provider) = qwp_ws_config_with_blocked_provider(dir.path(), "blocked-reconnect-provider"); let queue = open_configured_qwp_ws_queue(&qwp_ws).unwrap(); // Seed the transport with an already-connected socket, then close its // peer. The first publication drives the real blocking transport into // its reconnect path, where token acquisition deliberately blocks. let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let client = TcpStream::connect(listener.local_addr().unwrap()).unwrap(); client .set_read_timeout(Some(Duration::from_secs(1))) .unwrap(); client .set_write_timeout(Some(Duration::from_secs(1))) .unwrap(); let (peer, _) = listener.accept().unwrap(); drop(peer); let traffic_gate = Arc::new(TrafficGate::default()); let endpoints = qwp_ws_configured_endpoints("127.0.0.1", &port.to_string(), &qwp_ws); let transport = BlockingQwpWsTransport::from_connected( Arc::clone(&endpoints), QwpWsHostHealthTracker::new(endpoints.len()), false, None, QwpWsConnectKind::Foreground, qwp_ws.clone(), None, Arc::new(AtomicUsize::new(0)), Some(Arc::clone(&traffic_gate)), QwpWsConnectRoundSuccess { endpoint_idx: 0, stream: WsStream::Plain(NoSigpipeTcp::new(client).unwrap()), negotiated_version: 1, server_max_batch_size: 0, leftover: Vec::new(), }, ); let driver = QwpWsCoreTestHarness::from_queue(queue, transport); let (store, send_core) = driver.into_parts(); let mut runner = SyncQwpWsRunner::start_with_append_deadline( store, send_core, Duration::from_secs(1), traffic_gate, ); runner.shutdown_timeout = Duration::from_millis(100); runner.publish_replay_payload(b"blocked reconnect").unwrap(); provider.wait_until_started(); let started = Instant::now(); drop(runner); let drop_elapsed = started.elapsed(); let reopened = open_configured_qwp_ws_queue(&qwp_ws); provider.release_and_wait(); assert!( drop_elapsed < Duration::from_secs(1), "runner shutdown waited for the blocked reconnect provider: {drop_elapsed:?}" ); drop(reopened.unwrap_or_else(|err| { panic!("blocked reconnect provider retained the SFA slot after shutdown: {err}") })); } #[cfg(any(unix, windows))] #[test] fn runner_drives_periodic_sync_from_config() { let dir = TempDir::new().unwrap(); let qwp_ws = periodic_qwp_ws_config(dir.path(), "periodic-driver"); let queue = open_configured_qwp_ws_queue(&qwp_ws).unwrap(); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let (mut store, send_core) = driver.into_parts(); store.try_submit(b"periodic").unwrap(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let mut core = SyncQwpWsRunnerCore { send_core, progress, cold_effects: VecDeque::new(), backpressure: Arc::new(BackpressureNotifier::new()), ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle, }; assert_eq!( core.drive_step(&shared, &AtomicBool::new(false)), RunnerStep::Continue ); let mut store = shared.lock().unwrap(); assert!(!store.storage_maintenance_in_flight().unwrap()); assert!( store.take_storage_maintenance_step().unwrap().is_none(), "the runner must finish the initial periodic sync and arm its configured cadence" ); store.close_queue().unwrap(); } #[cfg(any(unix, windows))] #[test] fn runner_periodic_sync_failure_notifies_completion_waiters() { use super::super::qwp_ws_sfa_segment::fail_sync_after_for_test; let dir = TempDir::new().unwrap(); let qwp_ws = periodic_qwp_ws_config(dir.path(), "periodic-wait-error"); let queue = open_configured_qwp_ws_queue(&qwp_ws).unwrap(); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let (mut store, send_core) = driver.into_parts(); store.try_submit(b"periodic").unwrap(); let producer = store.take_producer().unwrap(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let notifier = Arc::new(BackpressureNotifier::new()); let mut core = SyncQwpWsRunnerCore { send_core, progress, cold_effects: VecDeque::new(), backpressure: Arc::clone(¬ifier), ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle, }; let generation = notifier.generation(); producer.check_durability().unwrap(); fail_sync_after_for_test(0); assert_eq!( core.finish_storage_maintenance(&shared), RunnerStep::Continue ); assert!( notifier.wait_for_change(generation, Some(Instant::now())), "a periodic failure must be observable even if it precedes the wait" ); assert!(matches!( producer.check_durability(), Err(SfaQueueError::Durability(_)) )); assert!( !core.lifecycle.is_terminal(), "periodic sync failure is retryable" ); drop(producer); shared.lock().unwrap().close_queue().unwrap(); } #[cfg(any(unix, windows))] #[test] fn threaded_runner_waits_for_in_flight_periodic_sync_before_close() { let dir = TempDir::new().unwrap(); let sender_id = "periodic-runner"; let qwp_ws = periodic_qwp_ws_config(dir.path(), sender_id); let slot_options = SfaSlotOptions { sf_dir: dir.path().to_path_buf(), sender_id: sender_id.to_owned(), segment_size_bytes: 256, max_bytes: 1024, periodic_sync_interval: Some(Duration::from_secs(3600)), }; let mut queue = open_configured_qwp_ws_queue(&qwp_ws).unwrap(); let receipt = PublicationLog::try_publish(&mut queue, b"periodic").unwrap(); PublicationLog::progress_view(&queue) .complete_through_fsn(receipt.fsn) .unwrap(); PublicationLog::persist_completed_fsn(&mut queue, receipt.fsn); // Hold the real sync step instead of racing a filesystem call: taking // it sets the production in-flight flag until finish is reported. let sync_step = PublicationLog::take_storage_maintenance_step(&mut queue, false) .unwrap() .expect("periodic queue must offer its initial sync step"); assert!(matches!(sync_step, SfaStorageStep::SyncPublished(_))); assert!(PublicationLog::storage_maintenance_in_flight(&queue).unwrap()); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let mut runner = SyncQwpWsRunner::start(driver); runner.begin_close(); // An already-expired deadline probes the gate without sleeping. let err = runner .drain_to_deadline(Some(Instant::now())) .expect_err("close must wait for the held periodic sync step"); assert!(err.msg().contains("close drain timed out")); assert!(matches!( SfaSlotQueue::open(slot_options.clone()), Err(SfaQueueError::SlotInUse { .. }) )); let sync_result = sync_step.perform().unwrap(); runner .shared .lock() .unwrap() .finish_storage_maintenance(sync_result) .unwrap(); runner .shared .lock() .unwrap() .complete_storage_maintenance() .unwrap(); runner.drain_to_deadline(None).unwrap(); SfaSlotQueue::open(slot_options).unwrap(); } #[cfg(any(unix, windows))] #[test] fn threaded_runner_waits_for_in_flight_hot_spare_creation_before_close() { let dir = TempDir::new().unwrap(); let slot_options = SfaSlotOptions { sf_dir: dir.path().to_path_buf(), sender_id: "hot-spare-runner".to_string(), segment_size_bytes: 38, max_bytes: 114, periodic_sync_interval: None, }; let mut queue = SfaSlotQueue::open(slot_options.clone()).unwrap(); PublicationLog::try_publish(&mut queue, b"first").unwrap(); let last = PublicationLog::try_publish(&mut queue, b"second").unwrap(); PublicationLog::progress_view(&queue) .complete_through_fsn(last.fsn) .unwrap(); PublicationLog::persist_completed_fsn(&mut queue, last.fsn); // Retire the sealed segment first so the next maintenance task is the // hot-spare creation involved in the close race. let trim_step = PublicationLog::take_storage_maintenance_step(&mut queue, false) .unwrap() .expect("completed sealed segment must schedule trim"); assert!(matches!(trim_step, SfaStorageStep::Trim(_))); let trim_result = trim_step.perform().unwrap(); PublicationLog::finish_storage_maintenance(&mut queue, trim_result, false).unwrap(); PublicationLog::complete_storage_maintenance(&mut queue).unwrap(); // Hold the real task after taking it. This is the precise window where // its off-lock file creation used to race a fully drained close. let create_step = PublicationLog::take_storage_maintenance_step(&mut queue, true) .unwrap() .expect("queue without a spare must schedule creation"); assert!(matches!(create_step, SfaStorageStep::CreateHotSpare { .. })); assert!(PublicationLog::storage_maintenance_in_flight(&queue).unwrap()); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let mut runner = SyncQwpWsRunner::start(driver); runner.begin_close(); let err = runner .drain_to_deadline(Some(Instant::now())) .expect_err("close must wait for the held hot-spare creation"); assert!(err.msg().contains("close drain timed out")); assert!(matches!( SfaSlotQueue::open(slot_options.clone()), Err(SfaQueueError::SlotInUse { .. }) )); let create_result = create_step.perform().unwrap(); let cleanup = runner .shared .lock() .unwrap() .finish_storage_maintenance(create_result) .unwrap() .into_cleanup() .expect("closing queue must abandon the created spare"); assert!(cleanup.perform().is_none()); runner .shared .lock() .unwrap() .complete_storage_maintenance() .unwrap(); runner.drain_to_deadline(None).unwrap(); SfaSlotQueue::open(slot_options).unwrap(); } #[test] fn empty_sender_error_polls_do_not_wait_for_storage_maintenance() { let driver = QwpWsCoreTestHarness::from_queue(memory_queue(8192), FakeOrderedServer::no_response()); let runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); let shared = Arc::clone(&runner.shared); let guard = shared.lock().unwrap(); std::thread::scope(|scope| { let (tx, rx) = mpsc::channel(); let runner = &runner; let polling = scope.spawn(move || { tx.send(( runner.poll_sender_error(), runner.poll_sender_error_notification(), )) .unwrap(); }); let result = rx.recv_timeout(Duration::from_secs(2)); drop(guard); polling.join().unwrap(); let (error, notification) = result.expect("empty polls must not wait on the store"); assert_eq!(error.unwrap(), None); assert_eq!(notification.unwrap(), None); }); } #[test] fn threaded_sfa_publication_does_not_take_shared_store_mutex() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); let shared = Arc::clone(&runner.shared); let guard = shared.lock().unwrap(); std::thread::scope(|scope| { let (published_tx, published_rx) = mpsc::channel(); let runner = &mut runner; let publish_thread = scope.spawn(move || { let result = runner.publish_replay_payload(b"sfa-first"); let _ = published_tx.send(result); }); assert_eq!( published_rx .recv_timeout(Duration::from_secs(2)) .unwrap() .unwrap(), 0 ); publish_thread.join().unwrap(); }); drop(guard); drop(runner); } #[test] fn threaded_sfa_send_progress_does_not_take_shared_store_mutex() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let (send_started_tx, send_started_rx) = mpsc::channel(); let (release_send_tx, release_send_rx) = mpsc::channel(); let transport = BlockingFirstSendTransport { send_started: send_started_tx, release_send: release_send_rx, should_block_send: true, sent_frames: Vec::new(), }; let mut driver = QwpWsCoreTestHarness::from_queue(queue, transport); let receipt = driver.try_submit(b"sfa-first").unwrap(); assert_eq!(receipt.fsn, 0); let (store, send_core) = driver.into_parts(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let mut guard = Some(shared.lock().unwrap()); let backpressure = Arc::new(BackpressureNotifier::new()); let mut core = SyncQwpWsRunnerCore { send_core, progress, cold_effects: VecDeque::new(), backpressure, ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle, }; let stop = AtomicBool::new(false); let shared_for_step = Arc::clone(&shared); std::thread::scope(|scope| { let step_thread = scope.spawn(|| core.drive_step(&shared_for_step, &stop)); match send_started_rx.recv_timeout(Duration::from_secs(2)) { Ok(()) => {} Err(err) => { drop(guard.take()); let _ = send_started_rx.recv_timeout(Duration::from_secs(2)); let _ = release_send_tx.send(()); step_thread.join().unwrap(); panic!("send progress waited for shared-store mutex: {err:?}"); } } release_send_tx.send(()).unwrap(); drop(guard.take()); assert_eq!(step_thread.join().unwrap(), RunnerStep::Continue); }); } #[test] fn threaded_sfa_ack_progress_does_not_take_shared_store_mutex() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let ack_ready = Arc::new(AtomicBool::new(false)); let transport = AckWhenReadyTransport { ack_ready: Arc::clone(&ack_ready), sent_frames: Vec::new(), }; let mut driver = QwpWsCoreTestHarness::from_queue(queue, transport); let receipt = driver.try_submit(b"sfa-first").unwrap(); assert_eq!(receipt.fsn, 0); let (store, send_core) = driver.into_parts(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let backpressure = Arc::new(BackpressureNotifier::new()); let mut core = SyncQwpWsRunnerCore { send_core, progress: progress.clone(), cold_effects: VecDeque::new(), backpressure, ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle, }; let stop = AtomicBool::new(false); assert_eq!(core.drive_step(&shared, &stop), RunnerStep::Continue); assert_eq!(progress.completed_fsn(), None); let mut guard = Some(shared.lock().unwrap()); ack_ready.store(true, Ordering::Release); let shared_for_step = Arc::clone(&shared); std::thread::scope(|scope| { let step_thread = scope.spawn(|| core.drive_step(&shared_for_step, &stop)); let deadline = Instant::now() + Duration::from_secs(2); while Instant::now() < deadline { if progress.completed_fsn() == Some(0) { drop(guard.take()); assert_eq!(step_thread.join().unwrap(), RunnerStep::Continue); return; } thread::sleep(Duration::from_millis(10)); } drop(guard.take()); let _ = step_thread.join(); panic!("ACK progress waited for shared-store mutex"); }); } #[test] fn threaded_sfa_durable_ack_progress_does_not_take_shared_store_mutex() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let ack_ready = Arc::new(AtomicBool::new(false)); let transport = DurableAckWhenReadyTransport { ack_ready: Arc::clone(&ack_ready), sent_frames: Vec::new(), }; let mut driver = QwpWsCoreTestHarness::from_queue_with_reconnect_policy( queue, transport, ReconnectPolicy::bounded(Duration::MAX, Duration::ZERO, Duration::ZERO), DurableAckTiers::LegacyReplicated, ); let receipt = driver.try_submit(b"sfa-first").unwrap(); assert_eq!(receipt.fsn, 0); let (store, send_core) = driver.into_parts(); let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let backpressure = Arc::new(BackpressureNotifier::new()); let ok_completed_upper = Arc::new(AtomicU64::new(0)); let mut core = SyncQwpWsRunnerCore { send_core, progress: progress.clone(), cold_effects: VecDeque::new(), backpressure, ok_completed_upper: Arc::clone(&ok_completed_upper), lifecycle, }; let stop = AtomicBool::new(false); assert_eq!(core.drive_step(&shared, &stop), RunnerStep::Continue); assert_eq!(ok_completed_upper.load(Ordering::Acquire), 1); assert_eq!(progress.completed_fsn(), None); let mut guard = Some(shared.lock().unwrap()); ack_ready.store(true, Ordering::Release); let shared_for_step = Arc::clone(&shared); std::thread::scope(|scope| { let step_thread = scope.spawn(|| core.drive_step(&shared_for_step, &stop)); let deadline = Instant::now() + Duration::from_secs(2); while Instant::now() < deadline { if progress.completed_fsn() == Some(0) { drop(guard.take()); assert_eq!(step_thread.join().unwrap(), RunnerStep::Continue); return; } thread::sleep(Duration::from_millis(10)); } drop(guard.take()); let _ = step_thread.join(); panic!("durable ACK progress waited for shared-store mutex"); }); } #[test] fn threaded_sfa_ok_fsn_uses_completed_fsn_lower_bound() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let mut driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let receipt = driver.try_submit(b"sfa-completed").unwrap(); let (store, send_core) = driver.into_parts(); let progress = store.progress_view(); progress.complete_through_fsn(receipt.fsn).unwrap(); let runner = SyncQwpWsRunner::start_with_append_deadline( store, send_core, Duration::from_secs(5), Arc::new(TrafficGate::default()), ); assert_eq!(runner.ok_fsn().unwrap(), Some(receipt.fsn)); } #[test] fn threaded_sfa_watermark_reads_do_not_take_shared_store_mutex() { let dir = TempDir::new().unwrap(); let queue = SfaFrameQueue::open(SfaQueueOptions { slot_dir: dir.path().to_path_buf(), segment_size_bytes: 4096, max_bytes: 8192, periodic_sync_interval: None, }) .unwrap(); let driver = QwpWsCoreTestHarness::from_queue(queue, FakeOrderedServer::no_response()); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); let fsn = runner.publish_replay_payload(b"sfa-first").unwrap(); let shared = Arc::clone(&runner.shared); let mut guard = Some(shared.lock().unwrap()); std::thread::scope(|scope| { let (watermarks_tx, watermarks_rx) = mpsc::channel(); let runner = &runner; scope.spawn(move || { let result = (runner.published_fsn(), runner.ok_fsn(), runner.acked_fsn()); let _ = watermarks_tx.send(result); }); let (published, ok, acked) = match watermarks_rx.recv_timeout(Duration::from_secs(2)) { Ok(result) => result, Err(err) => { drop(guard.take()); let _ = watermarks_rx.recv_timeout(Duration::from_secs(2)); panic!("watermark read waited for shared-store mutex: {err:?}"); } }; assert_eq!(published.unwrap(), Some(fsn)); assert_eq!(ok.unwrap(), None); assert_eq!(acked.unwrap(), None); }); drop(guard); drop(runner); } #[derive(Debug)] struct BlockingReconnectTransport { send_started: mpsc::Sender<()>, reconnect_started: mpsc::Sender<()>, release_reconnect: mpsc::Receiver<()>, should_fail_send: bool, sent_frames: Vec, } impl QwpWsCoreTransport for BlockingReconnectTransport { fn try_poll_response(&mut self) -> Result { Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { if self.should_fail_send { self.should_fail_send = false; self.send_started.send(()).unwrap(); return Err(TransportFailure::Disconnect(crate::Error::new( crate::ErrorCode::SocketError, "fake disconnect before reconnect", ))); } self.sent_frames.push(frame.sent_frame()); Ok(TransportSendResult::NoResponse) } fn restart_connection(&mut self, _reason: ReconnectReason) -> Result<(), DriverError> { self.reconnect_started.send(()).unwrap(); self.release_reconnect .recv_timeout(Duration::from_secs(5)) .unwrap(); Ok(()) } } #[test] fn threaded_runner_accepts_publication_while_reconnect_is_blocked() { let (send_started_tx, send_started_rx) = mpsc::channel(); let (reconnect_started_tx, reconnect_started_rx) = mpsc::channel(); let (release_reconnect_tx, release_reconnect_rx) = mpsc::channel(); let queue = memory_queue(1024); let transport = BlockingReconnectTransport { send_started: send_started_tx, reconnect_started: reconnect_started_tx, release_reconnect: release_reconnect_rx, should_fail_send: true, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(queue, transport); let mut runner = SyncQwpWsRunner::start(driver); runner.publish_replay_payload(b"first").unwrap(); send_started_rx .recv_timeout(Duration::from_secs(5)) .unwrap(); reconnect_started_rx .recv_timeout(Duration::from_secs(5)) .unwrap(); std::thread::scope(|scope| { let (published_tx, published_rx) = mpsc::channel(); let runner = &mut runner; let publish_thread = scope.spawn(move || { let result = runner.publish_replay_payload(b"second"); let _ = published_tx.send(result.map(|_| ())); }); let publish_result = match published_rx.recv_timeout(Duration::from_secs(2)) { Ok(result) => result, Err(err) => { let _ = release_reconnect_tx.send(()); publish_thread.join().unwrap(); panic!("publication waited for blocked reconnect: {err:?}"); } }; publish_result.unwrap(); publish_thread.join().unwrap(); }); release_reconnect_tx.send(()).unwrap(); drop(runner); } #[derive(Debug)] struct SignalResponseTransport { sent_frame: mpsc::Sender, ack: mpsc::Receiver<()>, terminal: mpsc::Receiver<()>, sent_frames: Vec, } impl QwpWsCoreTransport for SignalResponseTransport { fn try_poll_response(&mut self) -> Result { if self.terminal.try_recv().is_ok() { return Err(TransportFailure::Terminal(crate::Error::new( crate::ErrorCode::SocketError, "synthetic terminal failure", ))); } if self.ack.try_recv().is_ok() { return Ok(TransportPoll::Response(TransportResponse::Ack { wire_seq: 0, })); } Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { let sent_frame = frame.sent_frame(); self.sent_frames.push(sent_frame); self.sent_frame.send(sent_frame).unwrap(); Ok(TransportSendResult::NoResponse) } } // Two 10-byte frames fill the two-segment byte budget; a third publish then // backpressures until acks trim an earlier frame's segment. fn byte_backpressured_queue() -> SfaFrameQueue { SfaFrameQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: 48, max_bytes: 96, }) .unwrap() } #[test] fn threaded_runner_waits_for_ack_when_sfa_publication_is_backpressured() { let (sent_tx, sent_rx) = mpsc::channel(); let (ack_tx, ack_rx) = mpsc::channel(); let (_terminal_tx, terminal_rx) = mpsc::channel(); let transport = SignalResponseTransport { sent_frame: sent_tx, ack: ack_rx, terminal: terminal_rx, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(byte_backpressured_queue(), transport); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); runner.publish_replay_payload(b"aaaaaaaaaa").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 0); runner.publish_replay_payload(b"bbbbbbbbbb").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 1); std::thread::scope(|scope| { let (published_tx, published_rx) = mpsc::channel(); let runner = &mut runner; let publish_thread = scope.spawn(move || { let result = runner.publish_replay_payload(b"cccccccccc"); let _ = published_tx.send(result.map(|_| ())); }); assert!( published_rx .recv_timeout(Duration::from_millis(50)) .is_err() ); ack_tx.send(()).unwrap(); published_rx .recv_timeout(Duration::from_secs(5)) .unwrap() .unwrap(); publish_thread.join().unwrap(); }); } #[test] fn threaded_runner_times_out_when_sfa_publication_stays_backpressured() { let (sent_tx, sent_rx) = mpsc::channel(); let (_ack_tx, ack_rx) = mpsc::channel(); let (_terminal_tx, terminal_rx) = mpsc::channel(); let transport = SignalResponseTransport { sent_frame: sent_tx, ack: ack_rx, terminal: terminal_rx, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(byte_backpressured_queue(), transport); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_millis(20)); runner.publish_replay_payload(b"aaaaaaaaaa").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 0); runner.publish_replay_payload(b"bbbbbbbbbb").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 1); let err = runner.publish_replay_payload(b"cccccccccc").unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!( err.msg() .contains("timed out waiting for ACK-driven segment trim"), "got: {}", err.msg() ); } #[test] fn threaded_runner_wakes_backpressured_publication_on_terminal_error() { let (sent_tx, sent_rx) = mpsc::channel(); let (_ack_tx, ack_rx) = mpsc::channel(); let (terminal_tx, terminal_rx) = mpsc::channel(); let transport = SignalResponseTransport { sent_frame: sent_tx, ack: ack_rx, terminal: terminal_rx, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(byte_backpressured_queue(), transport); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); runner.publish_replay_payload(b"aaaaaaaaaa").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 0); runner.publish_replay_payload(b"bbbbbbbbbb").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 1); std::thread::scope(|scope| { let (published_tx, published_rx) = mpsc::channel(); let runner = &mut runner; let publish_thread = scope.spawn(move || { let result = runner.publish_replay_payload(b"cccccccccc"); let _ = published_tx.send(result.map(|_| ())); }); assert!( published_rx .recv_timeout(Duration::from_millis(50)) .is_err() ); terminal_tx.send(()).unwrap(); let err = published_rx .recv_timeout(Duration::from_secs(5)) .unwrap() .unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!( err.msg().contains("synthetic terminal failure"), "got: {}", err.msg() ); publish_thread.join().unwrap(); }); } #[test] fn threaded_runner_wakes_a_wait_for_room_on_terminal_error() { let (sent_tx, sent_rx) = mpsc::channel(); let (_ack_tx, ack_rx) = mpsc::channel(); let (terminal_tx, terminal_rx) = mpsc::channel(); let transport = SignalResponseTransport { sent_frame: sent_tx, ack: ack_rx, terminal: terminal_rx, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(byte_backpressured_queue(), transport); let mut runner = SyncQwpWsRunner::start_driver_with_append_deadline(driver, Duration::from_secs(5)); runner.publish_replay_payload(b"aaaaaaaaaa").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 0); runner.publish_replay_payload(b"bbbbbbbbbb").unwrap(); assert_eq!(sent_rx.recv_timeout(Duration::from_secs(5)).unwrap().fsn, 1); std::thread::scope(|scope| { let (waited_tx, waited_rx) = mpsc::channel(); let runner = &mut runner; let wait_thread = scope.spawn(move || { // One more frame needs a segment that the queue does not // have while the two before are not confirmed. let producer = runner.producer.as_ref().unwrap(); let _ = waited_tx.send(runner.wait_for_room(producer, &[10])); }); assert!(waited_rx.recv_timeout(Duration::from_millis(50)).is_err()); terminal_tx.send(()).unwrap(); let err = waited_rx .recv_timeout(Duration::from_secs(2)) .expect("the wait ends when the sender stops, before its deadline") .unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert!( err.msg().contains("synthetic terminal failure"), "got: {}", err.msg() ); wait_thread.join().unwrap(); }); } #[derive(Debug)] struct ImmediateFailureReconnectTransport { reconnect_started: mpsc::Sender<()>, release_reconnect: mpsc::Receiver<()>, should_fail_send: bool, sent_frames: Vec, } impl QwpWsCoreTransport for ImmediateFailureReconnectTransport { fn try_poll_response(&mut self) -> Result { Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { let sent_frame = frame.sent_frame(); self.sent_frames.push(sent_frame); if self.should_fail_send { self.should_fail_send = false; return Ok(TransportSendResult::Failure(TransportFailure::Disconnect( crate::Error::new(crate::ErrorCode::SocketError, "fake disconnect after send"), ))); } Ok(TransportSendResult::NoResponse) } fn restart_connection(&mut self, _reason: ReconnectReason) -> Result<(), DriverError> { self.reconnect_started.send(()).unwrap(); self.release_reconnect .recv_timeout(Duration::from_secs(5)) .unwrap(); Ok(()) } } #[test] fn threaded_runner_records_immediate_transport_failure_as_sent() { let (reconnect_started_tx, reconnect_started_rx) = mpsc::channel(); let (release_reconnect_tx, release_reconnect_rx) = mpsc::channel(); let queue = memory_queue(1024); let transport = ImmediateFailureReconnectTransport { reconnect_started: reconnect_started_tx, release_reconnect: release_reconnect_rx, should_fail_send: true, sent_frames: Vec::new(), }; let driver = QwpWsCoreTestHarness::from_queue(queue, transport); let mut runner = SyncQwpWsRunner::start(driver); runner.publish_replay_payload(b"first").unwrap(); reconnect_started_rx .recv_timeout(Duration::from_secs(5)) .unwrap(); // The sent event is flushed before reconnect I/O starts. Pin the // background-runner counter path while reconnect is deliberately // blocked so no replay can race this assertion. { let store = runner.lock_shared().unwrap(); let counters = store.counters(); assert_eq!(counters.total_frames_sent, 1); assert_eq!(counters.total_frames_replayed, 0); } release_reconnect_tx.send(()).unwrap(); let events = { let mut store = runner.lock_shared().unwrap(); let mut events = Vec::new(); while let Some(event) = store.poll_event() { events.push(event); } events }; assert_eq!( events.first(), Some(&DriverEvent::Sent { fsn: 0, wire_seq: 0, }) ); if let Some(reconnect_idx) = events .iter() .position(|event| matches!(event, DriverEvent::Reconnected { .. })) { assert!( reconnect_idx > 0, "immediate transport failure was not recorded as sent before reconnect: {events:?}" ); } drop(runner); } /// Records which frames each bounded send burst hands to the transport. #[derive(Debug)] struct BurstRecordingTransport { open: bool, current: Vec, bursts: Arc>>>, } impl QwpWsCoreTransport for BurstRecordingTransport { fn begin_send_batch(&mut self) -> usize { self.open = true; 64 * 1024 } fn finish_send_batch(&mut self) -> Result<(), TransportFailure> { assert!(self.open, "finish_send_batch without begin_send_batch"); self.open = false; let burst = std::mem::take(&mut self.current); if !burst.is_empty() { self.bursts.lock().unwrap().push(burst); } Ok(()) } fn try_poll_response(&mut self) -> Result { Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { assert!(self.open, "background sends must happen inside a burst"); self.current.push(frame.sent_frame().fsn); Ok(TransportSendResult::NoResponse) } fn restart_connection(&mut self, _reason: ReconnectReason) -> Result<(), DriverError> { Ok(()) } } #[test] fn runner_step_sends_every_ready_frame_in_one_burst() { let bursts = Arc::new(Mutex::new(Vec::new())); let transport = BurstRecordingTransport { open: false, current: Vec::new(), bursts: Arc::clone(&bursts), }; let driver = QwpWsCoreTestHarness::from_queue(memory_queue(4096), transport); let (mut store, send_core) = driver.into_parts(); for payload in [b"first".as_slice(), b"second", b"third"] { store.try_submit(payload).unwrap(); } let progress = store.progress_view(); let lifecycle = store.lifecycle(); let shared = Arc::new(Mutex::new(store)); let mut core = SyncQwpWsRunnerCore { send_core, progress, cold_effects: VecDeque::new(), backpressure: Arc::new(BackpressureNotifier::new()), ok_completed_upper: Arc::new(AtomicU64::new(0)), lifecycle, }; assert_eq!( core.drive_step(&shared, &AtomicBool::new(false)), RunnerStep::Continue ); assert_eq!(*bursts.lock().unwrap(), [vec![0, 1, 2]]); let mut store = shared.lock().unwrap(); let counters = store.counters(); assert_eq!(counters.total_frames_sent, 3); let mut sent = Vec::new(); while let Some(event) = store.poll_event() { if let DriverEvent::Sent { fsn, wire_seq } = event { sent.push((fsn, wire_seq)); } } assert_eq!(sent, [(0, 0), (1, 1), (2, 2)]); } #[derive(Debug)] struct FailFirstBatchFlushTransport { fail_next_flush: bool, unflushed: bool, sent_fsns: Arc>>, } impl QwpWsCoreTransport for FailFirstBatchFlushTransport { fn begin_send_batch(&mut self) -> usize { 64 * 1024 } fn finish_send_batch(&mut self) -> Result<(), TransportFailure> { if std::mem::take(&mut self.unflushed) && std::mem::take(&mut self.fail_next_flush) { return Err(TransportFailure::Disconnect(crate::Error::new( crate::ErrorCode::SocketError, "fake batch flush failure", ))); } Ok(()) } fn try_poll_response(&mut self) -> Result { Ok(TransportPoll::Idle) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { self.unflushed = true; self.sent_fsns.lock().unwrap().push(frame.sent_frame().fsn); Ok(TransportSendResult::NoResponse) } fn restart_connection(&mut self, _reason: ReconnectReason) -> Result<(), DriverError> { Ok(()) } } #[test] fn threaded_runner_replays_frames_whose_batch_write_failed() { let sent_fsns = Arc::new(Mutex::new(Vec::new())); let transport = FailFirstBatchFlushTransport { fail_next_flush: true, unflushed: false, sent_fsns: Arc::clone(&sent_fsns), }; let driver = QwpWsCoreTestHarness::from_queue(memory_queue(1024), transport); let mut runner = SyncQwpWsRunner::start(driver); runner.publish_replay_payload(b"first").unwrap(); let replayed = || { runner .lock_shared() .unwrap() .counters() .total_frames_replayed }; let deadline = Instant::now() + Duration::from_secs(5); while replayed() == 0 && Instant::now() < deadline { thread::sleep(Duration::from_millis(1)); } assert_eq!(replayed(), 1); assert_eq!(*sent_fsns.lock().unwrap(), [0, 0]); drop(runner); } // ---------- a flush of several frames ---------- const FLUSH_TABLE: &str = "flush_rows"; /// A segment this small holds two frames of the flushes below. const SMALL_SEGMENT: u64 = 1024; /// What the connection of a [`ServerModelTransport`] saw. #[derive(Default)] struct ModelWire { /// Each frame sent, and whether it has the defer flag. sent: Mutex>, /// The server keeps its answers to itself while this is set. answers_held: AtomicBool, /// The transport is gone, as a connection is once its runner thread /// has ended. closed: AtomicBool, } /// The connection of a runner to a [`QwpWsServerModel`]: the model takes /// every frame the runner sends, and its answer is the next response the /// runner reads. struct ServerModelTransport { connection: QwpWsModelConnection, answers: VecDeque>, wire: Arc, /// The frame at which the connection fails for good. fails_at_fsn: Option, /// The frame that the server rejects for good, as one whose columns /// disagree with the table. It takes no frame after it. rejects_at_fsn: Option, } impl QwpWsCoreTransport for ServerModelTransport { fn try_poll_response(&mut self) -> Result { if self.wire.answers_held.load(Ordering::Acquire) { return Ok(TransportPoll::Idle); } let Some(answer) = self.answers.pop_front() else { return Ok(TransportPoll::Idle); }; match codec::parse_pipelined_response(&answer) { Ok(codec::PipelinedResponse::Ok { sequence }) => { Ok(TransportPoll::Response(TransportResponse::Ack { wire_seq: sequence, })) } Ok(codec::PipelinedResponse::Error(error)) => { Ok(TransportPoll::Response(TransportResponse::Reject { wire_seq: error.sequence, error: crate::ingress::sender::qwp_ws_driver::QwpServerError::from(error), })) } _ => panic!("the server model answers with an OK or an error"), } } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { const QWP_FLAGS_OFFSET: usize = 5; const QWP_FLAG_DEFER_COMMIT: u8 = 0x01; if self.fails_at_fsn == Some(frame.fsn) { return Err(TransportFailure::Terminal(crate::Error::new( crate::ErrorCode::SocketError, "synthetic terminal failure", ))); } let defer_commit = frame.payload[QWP_FLAGS_OFFSET] & QWP_FLAG_DEFER_COMMIT != 0; self.wire .sent .lock() .unwrap() .push((frame.sent_frame(), defer_commit)); if self.rejects_at_fsn == Some(frame.fsn) { self.answers.push_back(self.connection.reject(0x03)); return Ok(TransportSendResult::NoResponse); } // The model panics at a frame it does not take: that stops the // sender, so the test fails at its next call instead of waiting. let answer = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { self.connection.receive(frame.payload) })) .map_err(|_| { TransportFailure::Terminal(crate::Error::new( crate::ErrorCode::SocketError, format!("the server model refused frame {}", frame.fsn), )) })?; self.answers.extend(answer); Ok(TransportSendResult::NoResponse) } } impl Drop for ServerModelTransport { fn drop(&mut self) { self.wire.closed.store(true, Ordering::Release); } } /// A runner on a memory queue with its connection to `server`, and the /// encoder of its sender. fn runner_on_model( server: &QwpWsServerModel, queue: SfaMemoryQueueOptions, append_deadline: Duration, fails_at_fsn: Option, ) -> ( SyncQwpWsRunner, QwpWsReplayEncoder, Arc, ) { runner_on_model_rejecting(server, queue, append_deadline, fails_at_fsn, None) } /// [`runner_on_model`] whose server rejects frame `rejects_at_fsn` for /// good. fn runner_on_model_rejecting( server: &QwpWsServerModel, queue: SfaMemoryQueueOptions, append_deadline: Duration, fails_at_fsn: Option, rejects_at_fsn: Option, ) -> ( SyncQwpWsRunner, QwpWsReplayEncoder, Arc, ) { let wire = Arc::new(ModelWire::default()); let transport = ServerModelTransport { connection: server.connect(), answers: VecDeque::new(), wire: Arc::clone(&wire), fails_at_fsn, rejects_at_fsn, }; let driver = QwpWsCoreTestHarness::from_queue(SfaFrameQueue::open_memory(queue).unwrap(), transport); // As for a sender that keeps its queue in memory: a frame brings the // symbols that are new to the connection, and the driver keeps what // it has sent, to bring it again on a new connection. let (store, mut send_core) = driver.into_parts(); send_core.enable_delta_dict(&[], 0); let runner = SyncQwpWsRunner::start_with_append_deadline( store, send_core, append_deadline, Arc::new(TrafficGate::default()), ); let mut encoder = QwpWsReplayEncoder::new(1); encoder.set_delta_dict_enabled(true); (runner, encoder, wire) } fn small_segments(segment_count: u64) -> SfaMemoryQueueOptions { SfaMemoryQueueOptions { segment_size_bytes: SMALL_SEGMENT, max_bytes: (segment_count * SMALL_SEGMENT) as usize, } } /// The largest frame a sender with segments of [`SMALL_SEGMENT`] sends. fn small_frame_cap() -> usize { segment_payload_capacity(SMALL_SEGMENT) } /// The frames such a sender cuts `buffer` into. fn small_frames_plan(buffer: &Buffer) -> QwpWsFramePlan { buffer .as_qwp_ws() .unwrap() .plan_frames( two_frame_segment_payload_capacity(SMALL_SEGMENT), small_frame_cap(), ) .unwrap() } /// Flushes `buffer` as the frames of `plan`. fn publish_frames( encoder: &mut QwpWsReplayEncoder, runner: &mut SyncQwpWsRunner, buffer: &Buffer, plan: &QwpWsFramePlan, ) -> crate::Result> { let rows = buffer.as_qwp_ws().unwrap(); publish_qwp_ws_frames(encoder, runner, rows, plan, small_frame_cap()) } /// Flushes `buffer` as one frame, the way every flush goes today. fn publish_as_one_frame( encoder: &mut QwpWsReplayEncoder, runner: &mut SyncQwpWsRunner, buffer: &Buffer, ) -> u64 { publish_as_one_frame_within(encoder, runner, buffer, small_frame_cap()) } fn publish_as_one_frame_within( encoder: &mut QwpWsReplayEncoder, runner: &mut SyncQwpWsRunner, buffer: &Buffer, frame_cap: usize, ) -> u64 { let rows = buffer.as_qwp_ws().unwrap(); publish_qwp_ws_buffer(encoder, rows, frame_cap, |payload| { runner.publish_replay_payload(payload) }) .unwrap() .unwrap() } /// Waits until the server has confirmed every frame up to `fsn`. fn wait_for_ack(runner: &SyncQwpWsRunner, fsn: u64) { let deadline = Instant::now() + Duration::from_secs(5); while runner.acked_fsn().unwrap() != Some(fsn) { assert!(Instant::now() < deadline, "frame {fsn} was not confirmed"); thread::sleep(Duration::from_millis(1)); } } /// One flush of many frames, between two flushes of one frame each. The /// server must hold the rows of the three, in three commits. Returns the /// bytes that the frames of the three flushes take in the queue. fn assert_a_flush_of_many_frames_is_one_commit(queue: SfaMemoryQueueOptions) -> u64 { let server = QwpWsServerModel::new(); let (mut runner, mut encoder, wire) = runner_on_model(&server, queue, Duration::from_secs(5), None); let before = buffer_of(FLUSH_TABLE, 0..3); let buffer = buffer_of(FLUSH_TABLE, 3..403); let after = buffer_of(FLUSH_TABLE, 403..406); let plan = small_frames_plan(&buffer); let frame_count = plan.frames().count() as u64; assert!(frame_count > 8, "{frame_count} frames"); assert_eq!(publish_as_one_frame(&mut encoder, &mut runner, &before), 0); // Frame 0 is the flush before, so this flush is frames 1 to // `frame_count`. let fsn = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap(); assert_eq!(fsn, Some(frame_count)); assert_eq!(runner.published_fsn().unwrap(), fsn); assert_eq!( publish_as_one_frame(&mut encoder, &mut runner, &after), frame_count + 1 ); wait_for_ack(&runner, frame_count + 1); let sent = wire.sent.lock().unwrap().clone(); assert_eq!(sent.len() as u64, frame_count + 2); for (idx, (frame, defer_commit)) in sent.iter().enumerate() { let idx = idx as u64; assert_eq!((frame.fsn, frame.wire_seq), (idx, idx)); assert_eq!( *defer_commit, (1..frame_count).contains(&idx), "frame {idx}: of the flush in frames, every frame but the last has the flag" ); } let table = server.table(FLUSH_TABLE).unwrap(); assert_eq!(table.commits, 3, "one commit for each flush"); let rows: Vec = [&before, &buffer, &after] .into_iter() .flat_map(|flushed| rows_of(FLUSH_TABLE, flushed)) .collect(); assert_eq!(table.rows_as_text(), rows); // In a segment a frame takes its payload and a header of 8 bytes. sent.iter() .map(|(frame, _)| frame.payload_len as u64 + 8) .sum() } #[test] fn a_flush_of_many_frames_is_one_commit() { let segment_size_bytes = 64 * SMALL_SEGMENT; let queued = assert_a_flush_of_many_frames_is_one_commit(SfaMemoryQueueOptions { segment_size_bytes, max_bytes: 2 * segment_size_bytes as usize, }); // A segment starts with a header of 24 bytes. assert!( queued + 24 <= segment_size_bytes, "{queued} bytes of frames must fit the first segment" ); } #[test] fn a_flush_of_many_frames_is_one_commit_when_it_fills_several_segments() { let queued = assert_a_flush_of_many_frames_is_one_commit(small_segments(64)); assert!(queued > 4 * SMALL_SEGMENT, "{queued} bytes of frames"); } #[test] fn a_flush_whose_first_frame_is_refused_leaves_the_sender_usable() { let server = QwpWsServerModel::new(); let (mut runner, mut encoder, wire) = runner_on_model(&server, small_segments(64), Duration::from_secs(5), None); let buffer = buffer_of(FLUSH_TABLE, 0..400); let plan = small_frames_plan(&buffer); let rows = buffer.as_qwp_ws().unwrap(); // No frame of the flush is as small as 64 bytes. let err = publish_qwp_ws_frames(&mut encoder, &mut runner, rows, &plan, 64).unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::BatchTooLarge); assert!( err.msg().contains("exceeds the per-frame cap of 64 bytes"), "got: {}", err.msg() ); assert!(!runner.lifecycle_is_terminal()); assert_eq!(runner.published_fsn().unwrap(), None); assert!(wire.sent.lock().unwrap().is_empty()); let fsn = publish_frames(&mut encoder, &mut runner, &buffer, &plan) .unwrap() .unwrap(); wait_for_ack(&runner, fsn); let table = server.table(FLUSH_TABLE).unwrap(); assert_eq!(table.commits, 1); assert_eq!(table.rows_as_text(), rows_of(FLUSH_TABLE, &buffer)); } #[test] fn a_flush_whose_first_frame_is_refused_gives_its_symbols_back() { let server = QwpWsServerModel::new(); let (mut runner, mut encoder, wire) = runner_on_model(&server, small_segments(64), Duration::from_secs(5), None); // A new symbol in every row: every frame brings some. let mut buffer = Buffer::new_qwp_ws(); for id in 0..400 { buffer.table(FLUSH_TABLE).unwrap(); buffer.symbol("sym", format!("s{id}")).unwrap(); buffer.column_i64("id", id).unwrap(); buffer.at_now().unwrap(); } let plan = small_frames_plan(&buffer); assert!(plan.frames().count() > 8); let rows = buffer.as_qwp_ws().unwrap(); // No frame of the flush is as small as 64 bytes. let err = publish_qwp_ws_frames(&mut encoder, &mut runner, rows, &plan, 64).unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::BatchTooLarge); assert!(!runner.lifecycle_is_terminal()); assert!(wire.sent.lock().unwrap().is_empty()); // The symbols of the refused frame did not stay with the sender: the // server model refuses a frame whose symbols start behind those that // its connection holds. let fsn = publish_frames(&mut encoder, &mut runner, &buffer, &plan) .unwrap() .unwrap(); wait_for_ack(&runner, fsn); let table = server.table(FLUSH_TABLE).unwrap(); assert_eq!(table.commits, 1); assert_eq!(table.rows_as_text(), rows_of(FLUSH_TABLE, &buffer)); } #[test] fn a_flush_that_cannot_queue_a_later_frame_closes_the_sender_for_more() { let server = QwpWsServerModel::new(); // Two segments hold the flush before and three frames of the flush. // The server confirms none of them before the last frame of the // flush, so their segments are never freed and the fourth frame // finds no room. let (mut runner, mut encoder, wire) = runner_on_model(&server, small_segments(2), Duration::from_millis(50), None); let before = buffer_of(FLUSH_TABLE, 0..3); let buffer = buffer_of(FLUSH_TABLE, 3..403); let plan = small_frames_plan(&buffer); assert!(plan.frames().count() > 8); // The server takes the flush before and keeps its answer to itself. wire.answers_held.store(true, Ordering::Release); assert_eq!(publish_as_one_frame(&mut encoder, &mut runner, &before), 0); let err = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::FlushAborted); for part in [ "Could not flush buffer: the flush stopped part way: ", "timed out waiting for ACK-driven segment trim", "Nothing of this flush is committed and the buffer still holds its rows.", "This sender takes no more flushes and still delivers the flushes before this one: \ once they are confirmed, close it or return it to its pool, and flush the buffer \ again through a new sender.", ] { assert!(err.msg().contains(part), "no {part:?} in: {}", err.msg()); } assert!(!err.in_doubt()); // The sender is not stopped. It says that it has published the flush // before and no more, while its queue holds the frames of the failed // flush too, for the network thread to send. assert!(!runner.lifecycle_is_terminal()); assert_eq!(runner.published_fsn().unwrap(), Some(0)); assert_eq!(runner.lock_shared().unwrap().published_fsn(), Some(3)); assert_eq!(runner.acked_fsn().unwrap(), None); // Every later publication is refused with the same error. let refused = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap_err(); assert_eq!((refused.code(), refused.msg()), (err.code(), err.msg())); let refused = runner.publish_replay_payload(b"more").unwrap_err(); assert_eq!((refused.code(), refused.msg()), (err.code(), err.msg())); assert_eq!(runner.lock_shared().unwrap().published_fsn(), Some(3)); // The close drain waits for the answer to the flush before, and for // nothing else: the server never answers a frame of the failed flush. thread::scope(|scope| { let drain = scope.spawn(|| runner.close_drain(Duration::from_secs(5))); let deadline = Instant::now() + Duration::from_secs(5); while wire.sent.lock().unwrap().len() < 4 { assert!(Instant::now() < deadline, "the queued frames were not sent"); thread::sleep(Duration::from_millis(1)); } thread::sleep(Duration::from_millis(100)); assert!( !drain.is_finished(), "the close drain did not wait for the flush before" ); wire.answers_held.store(false, Ordering::Release); drain.join().unwrap().unwrap(); }); assert_eq!(runner.acked_fsn().unwrap(), Some(0)); // The server has committed the flush before. It took the three // frames of the failed flush and holds their rows. let sent = wire.sent.lock().unwrap().clone(); let flags: Vec = sent.iter().map(|(_, defer)| *defer).collect(); assert_eq!(flags, [false, true, true, true]); let table = server.table(FLUSH_TABLE).unwrap(); assert_eq!(table.commits, 1); assert_eq!(table.rows_as_text(), rows_of(FLUSH_TABLE, &before)); // What the error tells the user to do: a new sender, the same // buffer. The connection of the old one goes with it, and with the // connection the rows that the server held. drop(runner); assert!(wire.closed.load(Ordering::Acquire)); let (mut runner, mut encoder, _wire) = runner_on_model(&server, small_segments(64), Duration::from_secs(5), None); let fsn = publish_frames(&mut encoder, &mut runner, &buffer, &plan) .unwrap() .unwrap(); wait_for_ack(&runner, fsn); let table = server.table(FLUSH_TABLE).unwrap(); assert_eq!(table.commits, 2); let mut rows = rows_of(FLUSH_TABLE, &before); rows.extend(rows_of(FLUSH_TABLE, &buffer)); assert_eq!(table.rows_as_text(), rows); } /// After a flush that failed part way, the close drain stops the network /// thread before it closes the queue. It waits for the thread until its /// deadline, and never longer than the drop of the runner does: what ends /// the wait first names the error. Here the thread is parked with the /// confirmation of the flush before stored in the queue. #[test] fn a_close_drain_after_a_flush_that_failed_part_way_waits_for_the_thread_until_its_deadline_or_the_drop_bound() { const SHORT: Duration = Duration::from_millis(100); let parked = Arc::new(AtomicBool::new(false)); let held = Arc::new(AtomicBool::new(false)); let _hook = { let (parked, held) = (Arc::clone(&parked), Arc::clone(&held)); ConfirmationStoredHookGuard::new(Arc::new(move |_| { while held.load(Ordering::Acquire) { parked.store(true, Ordering::Release); thread::sleep(Duration::from_millis(1)); } })) }; let timed_out = "QWP/WebSocket close drain timed out while the sender was closing its \ connection after the flush that stopped part way. The flushes before \ it are confirmed, and nothing of that flush is committed: only a \ flush with more rows of one table than the server's \ qwp.max.uncommitted.rows may be committed in part. Drop the sender: \ the frames of that flush go with it."; for (shutdown_timeout, close_timeout, expected) in [ (Duration::from_secs(5), SHORT, timed_out), ( SHORT, Duration::from_secs(5), "QWP/WebSocket runner did not stop within 100ms", ), ] { let server = QwpWsServerModel::new(); let (mut runner, mut encoder, wire) = runner_on_model(&server, small_segments(2), Duration::from_millis(50), None); runner.shutdown_timeout = shutdown_timeout; let before = buffer_of(FLUSH_TABLE, 0..3); let buffer = buffer_of(FLUSH_TABLE, 3..403); let plan = small_frames_plan(&buffer); wire.answers_held.store(true, Ordering::Release); assert_eq!(publish_as_one_frame(&mut encoder, &mut runner, &before), 0); let err = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::FlushAborted); parked.store(false, Ordering::Release); held.store(true, Ordering::Release); wire.answers_held.store(false, Ordering::Release); let deadline = Instant::now() + Duration::from_secs(5); while !parked.load(Ordering::Acquire) { assert!(Instant::now() < deadline, "the network thread did not park"); thread::sleep(Duration::from_millis(1)); } let started = Instant::now(); let err = runner.close_drain(close_timeout).unwrap_err(); let took = started.elapsed(); held.store(false, Ordering::Release); assert!( took >= SHORT && took < SHORT + Duration::from_secs(2), "close drain took {took:?}" ); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert_eq!(err.msg(), expected); drop(runner); } } #[test] fn a_sender_that_turns_terminal_during_a_flush_reports_its_own_error() { let server = QwpWsServerModel::new(); // The flush cannot queue its fifth frame, as above, and waits for // room. The connection fails for good at the second frame. let (mut runner, mut encoder, _wire) = runner_on_model(&server, small_segments(2), Duration::from_secs(5), Some(1)); let buffer = buffer_of(FLUSH_TABLE, 0..400); let plan = small_frames_plan(&buffer); let err = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap_err(); assert_eq!(err.code(), crate::ErrorCode::SocketError); assert_eq!(err.msg(), "synthetic terminal failure"); assert!(runner.lifecycle_is_terminal()); let refused = runner.publish_replay_payload(b"more").unwrap_err(); assert_eq!((refused.code(), refused.msg()), (err.code(), err.msg())); } /// The server rejects a frame of a flush in frames while the flush still /// queues its frames, so the sender stops for that rejection before the /// flush gives up: the flush and every later publication get the /// rejection itself. The flush before it is confirmed: the calls about /// it do not fail, and the close drain succeeds. #[test] fn a_flush_that_the_server_rejects_while_it_is_queued_fails_with_the_rejection() { let server = QwpWsServerModel::new(); // The flush cannot queue all its frames at once and waits for room, // as above. The server rejects its second frame, frame 2. let (mut runner, mut encoder, _wire) = runner_on_model_rejecting( &server, small_segments(2), Duration::from_secs(5), None, Some(2), ); let before = buffer_of(FLUSH_TABLE, 0..3); assert_eq!(publish_as_one_frame(&mut encoder, &mut runner, &before), 0); wait_for_ack(&runner, 0); let buffer = buffer_of(FLUSH_TABLE, 3..403); let plan = small_frames_plan(&buffer); let err = publish_frames(&mut encoder, &mut runner, &buffer, &plan).unwrap_err(); assert_eq!( err.code(), crate::ErrorCode::ServerRejection, "{}", err.msg() ); let rejection = err.qwp_ws_rejection().unwrap(); assert_eq!((rejection.from_fsn, rejection.to_fsn), (2, 2)); assert!(runner.lifecycle_is_terminal()); let refused = runner.publish_replay_payload(b"more").unwrap_err(); assert_eq!((refused.code(), refused.msg()), (err.code(), err.msg())); assert_eq!(runner.published_fsn().unwrap(), Some(0)); assert_eq!(runner.acked_fsn().unwrap(), Some(0)); runner.close_drain(Duration::from_secs(5)).unwrap(); assert_eq!(server.table(FLUSH_TABLE).unwrap().commits, 1); } #[test] fn the_error_of_an_aborted_flush_keeps_what_its_cause_says() { let server = QwpWsServerModel::new(); let (mut runner, _encoder, _wire) = runner_on_model(&server, small_segments(2), Duration::from_secs(5), None); // A cause that could not tell whether its frame had gone out. let cause = crate::Error::new( crate::ErrorCode::AuthError, "WebSocket upgrade authentication failed: HTTP status 401.", ) .with_ws_http_status(401) .with_in_doubt(true); let err = runner.abort_flush(0, cause); assert_eq!(err.code(), crate::ErrorCode::FlushAborted); assert_eq!( err.msg(), "Could not flush buffer: the flush stopped part way: WebSocket upgrade \ authentication failed: HTTP status 401. Nothing of this flush is committed and \ the buffer still holds its rows. Only a flush with more rows of one table than \ the server's qwp.max.uncommitted.rows may be committed in part, and flushing it \ again then stores that part twice unless the table has DEDUP keys. This sender \ takes no more flushes and still delivers the flushes before this one: once they \ are confirmed, close it or return it to its pool, and flush the buffer again \ through a new sender." ); assert_eq!(err.ws_http_status(), Some(401)); // Whatever its cause, nothing of the flush can be committed. assert!(!err.in_doubt()); assert!(!runner.lifecycle_is_terminal()); // The flush was the first of its sender: nothing was published // before it, and the close drain has nothing to wait for. assert_eq!(runner.published_fsn().unwrap(), None); let refused = runner.publish_replay_payload(b"more").unwrap_err(); assert_eq!((refused.code(), refused.msg()), (err.code(), err.msg())); assert!(!refused.in_doubt()); runner.close_drain(Duration::from_secs(5)).unwrap(); } /// A server that reads no rows, so that a flush is timed without it. It /// confirms each frame without the defer flag, or none at all. struct SinkTransport { confirms: bool, confirmed: VecDeque, } impl QwpWsCoreTransport for SinkTransport { fn try_poll_response(&mut self) -> Result { Ok(match self.confirmed.pop_front() { Some(wire_seq) => TransportPoll::Response(TransportResponse::Ack { wire_seq }), None => TransportPoll::Idle, }) } fn send_frame( &mut self, frame: OutboundFrameView<'_>, ) -> Result { if self.confirms && frame.payload[5] & 0x01 == 0 { self.confirmed.push_back(frame.wire_seq); } Ok(TransportSendResult::NoResponse) } } /// Times the `flush` of a large buffer in frames (plan, encode and copy /// into the queue) next to the same rows flushed as buffers of one frame /// each, into a memory queue of the default size. Run with: /// `cargo test --release --manifest-path questdb-rs/Cargo.toml --features sync-sender-qwp-ws qwp_ws_flush_in_frames_benchmark --lib -- --ignored --nocapture --test-threads=1` #[test] #[ignore = "performance benchmark"] fn qwp_ws_flush_in_frames_benchmark() { const SEGMENT: u64 = 4 * 1024 * 1024; const QUEUE: usize = 128 * 1024 * 1024; // The frame limit a server advertises by default. const SERVER_FRAME_LIMIT: usize = 2_097_138; let frame_cap = SERVER_FRAME_LIMIT.min(segment_payload_capacity(SEGMENT)); let target = frame_cap.min(two_frame_segment_payload_capacity(SEGMENT)); let runner_and_encoder = |confirms: bool| { let queue = SfaFrameQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: SEGMENT, max_bytes: QUEUE, }) .unwrap(); let transport = SinkTransport { confirms, confirmed: VecDeque::new(), }; let runner = SyncQwpWsRunner::start(QwpWsCoreTestHarness::from_queue(queue, transport)); let mut encoder = QwpWsReplayEncoder::new(1); encoder.set_delta_dict_enabled(true); (runner, encoder) }; let ms = |elapsed: Duration| elapsed.as_secs_f64() * 1e3; println!( "MiB,frames,single_frames,plan_ms,in_frames_ms,single_frames_ms,single_frames_unconfirmed_ms" ); for mib in [10usize, 50, 100] { // The same rows twice: in one buffer, and in buffers of one frame. let mut large = Buffer::new_qwp_ws(); let mut small = vec![Buffer::new_qwp_ws()]; let mut id = 0i64; while large.len() < mib << 20 { for buffer in [&mut large, small.last_mut().unwrap()] { buffer.table("flush_bench").unwrap(); for column in ["a", "b", "c", "d", "e", "f"] { buffer.column_i64(column, id).unwrap(); } buffer .at(crate::ingress::TimestampNanos::new( 1_700_000_000_000_000_000 + id, )) .unwrap(); } id += 1; if small.last().unwrap().len() + 256 > target { small.push(Buffer::new_qwp_ws()); } } small.retain(|buffer| !buffer.is_empty()); let rows = large.as_qwp_ws().unwrap(); for _ in 0..7 { let (mut runner, mut encoder) = runner_and_encoder(true); let started = Instant::now(); let plan = rows.plan_frames(target, frame_cap).unwrap(); let planned = started.elapsed(); let fsn = publish_qwp_ws_frames(&mut encoder, &mut runner, rows, &plan, frame_cap) .unwrap() .unwrap(); let in_frames = started.elapsed(); wait_for_ack(&runner, fsn); drop(runner); let mut single_frames = [Duration::ZERO; 2]; for (confirms, elapsed) in [true, false].into_iter().zip(&mut single_frames) { let (mut runner, mut encoder) = runner_and_encoder(confirms); let started = Instant::now(); let mut fsn = 0; for buffer in &small { fsn = publish_as_one_frame_within( &mut encoder, &mut runner, buffer, frame_cap, ); } *elapsed = started.elapsed(); if confirms { wait_for_ack(&runner, fsn); } drop(runner); } println!( "{mib},{},{},{:.2},{:.1},{:.1},{:.1}", plan.frames().count(), small.len(), ms(planned), ms(in_frames), ms(single_frames[0]), ms(single_frames[1]), ); } } } /// Times the `flush` of a buffer that fits one frame, for rows of /// `columns` long columns and a timestamp: the call alone, on a buffer /// that is cleared and filled again before every call, as a caller's is. /// The queue is a memory queue of the default size, the server reads no /// rows and confirms every frame. Run with: /// `cargo test --release --manifest-path questdb-rs/Cargo.toml --features sync-sender-qwp-ws qwp_ws_flush_single_frame_benchmark --lib -- --ignored --nocapture --test-threads=1` #[test] #[ignore = "performance benchmark"] fn qwp_ws_flush_single_frame_benchmark() { const SEGMENT: u64 = 4 * 1024 * 1024; const QUEUE: usize = 128 * 1024 * 1024; // The frame limit a server advertises by default. const SERVER_FRAME_LIMIT: usize = 2_097_138; const MAX_BUF_SIZE: usize = 100 * 1024 * 1024; let frame_cap = SERVER_FRAME_LIMIT.min(segment_payload_capacity(SEGMENT)); let samples = std::env::var("QWP_WS_FLUSH_BENCH_SAMPLES") .ok() .and_then(|samples| samples.parse().ok()) .unwrap_or(9); println!("rows,columns,bytes,flushes,ns_per_flush"); for (rows, columns) in [ (1usize, 6usize), (1, 100), (1, 1000), (100, 6), (10_000, 6), (100, 100), (10, 1000), ] { let queue = SfaSlotQueue::open_memory(SfaMemoryQueueOptions { segment_size_bytes: SEGMENT, max_bytes: QUEUE, }) .unwrap(); let transport = SinkTransport { confirms: true, confirmed: VecDeque::new(), }; let mut encoder = QwpWsReplayEncoder::new(1); encoder.set_delta_dict_enabled(true); let mut state = SyncQwpWsHandlerState { encoder, runner: SyncQwpWsRunner::start(QwpWsCoreTestHarness::from_queue(queue, transport)), server_max_batch_size: Arc::new(AtomicUsize::new(SERVER_FRAME_LIMIT)), request_durable_ack: DurableAckTiers::Off, token_provider: None, sfa_frame_payload_cap: segment_payload_capacity(SEGMENT), sfa_frame_split_target: two_frame_segment_payload_capacity(SEGMENT), orphan_pool: None, close_drain_timeout: Duration::ZERO, delta_dict_enabled: true, recovered_dict_entries: Vec::new(), recovered_dict_count: 0, persisted_symbol_dict: None, sfa_split_deferral_enabled: true, }; let names: Vec = (0..columns).map(|column| format!("c{column}")).collect(); let flushes = (4_000_000 / (rows * (columns + 1))).clamp(20, 100_000); let mut buffer = Buffer::new_qwp_ws(); for _ in 0..samples { let mut in_flush = Duration::ZERO; for _ in 0..flushes { buffer.clear(); for row in 0..rows as i64 { buffer.table("flush_bench").unwrap(); for name in &names { buffer.column_i64(name.as_str(), row).unwrap(); } buffer .at(crate::ingress::TimestampNanos::new( 1_700_000_000_000_000_000 + row, )) .unwrap(); } let rows_of_buffer = buffer.as_qwp_ws().unwrap(); let started = Instant::now(); let fsn = flush_qwp_ws(&mut state, rows_of_buffer, frame_cap, MAX_BUF_SIZE, true); in_flush += started.elapsed(); assert!(fsn.unwrap().is_some()); } println!( "{rows},{columns},{},{flushes},{}", buffer.len(), in_flush.as_nanos() / flushes as u128 ); } } } }