diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a79bc968..6a37783a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2376,8 +2376,8 @@ jobs: run: | set -euo pipefail cargo test --locked --lib cluster::chunk_log_tests::published_reader_tests -- --test-threads=1 2>&1 | tee /tmp/published-tail-reads.log - grep -q '^running 9 tests$' /tmp/published-tail-reads.log - grep -q '^test result: ok. 9 passed' /tmp/published-tail-reads.log + grep -q '^running 11 tests$' /tmp/published-tail-reads.log + grep -q '^test result: ok. 11 passed' /tmp/published-tail-reads.log - name: guarded publication wire writes preserve durable prefixes run: | set -euo pipefail diff --git a/src/cluster/chunk_log_tests.rs b/src/cluster/chunk_log_tests.rs index 37460e2f..33394415 100644 --- a/src/cluster/chunk_log_tests.rs +++ b/src/cluster/chunk_log_tests.rs @@ -635,6 +635,14 @@ pub(super) struct FaultProxy { } impl FaultProxy { pub(super) fn new(target: String, operation: u32, drop_reply: bool) -> Self { + Self::with_probe_gate(target, operation, drop_reply, None) + } + fn with_probe_gate( + target: String, + operation: u32, + drop_reply: bool, + probe_gate: Option, std::sync::Condvar)>>, + ) -> Self { use prost::Message as _; let listener = TcpListener::bind("127.0.0.1:0").unwrap(); listener.set_nonblocking(true).unwrap(); @@ -666,6 +674,7 @@ impl FaultProxy { } let fault = Arc::clone(&fault); let recorded = Arc::clone(&recorded); + let probe_gate = probe_gate.clone(); scope.spawn(move || { let mut transfer = || -> std::io::Result<()> { loop { @@ -678,6 +687,17 @@ impl FaultProxy { let seen = (header[1] == crate::cluster::wire::RequestCode::LogOp as u8) .then(|| LogOpRequest::decode(body.as_slice()).ok().map(|request| request.op)).flatten(); if let Some(op) = seen { recorded.lock().unwrap().push(op); } + if seen == Some(super::log_op::OP_TAIL) { + if let Some(gate) = &probe_gate { + let mut count = gate.0.lock().unwrap(); + *count += 1; + gate.1.notify_all(); + let (count, _) = gate.1.wait_timeout_while( + count, Duration::from_secs(3), |count| *count < 3, + ).unwrap(); + if *count < 3 { return Err(std::io::ErrorKind::TimedOut.into()); } + } + } let selected = seen == Some(operation) && fault.swap(false, Ordering::SeqCst); if selected && !drop_reply { return Ok(()); } upstream.write_all(&header)?; diff --git a/src/cluster/published_tail_reader.rs b/src/cluster/published_tail_reader.rs index be28f4db..c0eda8a6 100644 --- a/src/cluster/published_tail_reader.rs +++ b/src/cluster/published_tail_reader.rs @@ -108,6 +108,87 @@ impl ChunkLogReader { Ok(reply) } + // Start every qualified member's read-only probe before waiting for any reply. + // Still collect all replies: a later member may report a newer writer epoch. + fn publication_probes(&mut self) -> [Result; LOG_REPLICAS] { + let request = LogOpRequest::publication_probe(self.volume, self.chunk, self.generation); + let frame = std::sync::Arc::new(crate::cluster::message::encode_request( + &crate::cluster::message::ReplicaRequest::LogOp(request.clone()), + )); + let reused: [bool; LOG_REPLICAS] = std::array::from_fn(|i| self.clients[i].is_some()); + let mut failures: [Option; LOG_REPLICAS] = std::array::from_fn(|_| None); + for (member, failure) in failures.iter_mut().enumerate() { + if let Err(error) = self.client(member) { + *failure = Some(error); + } + } + let mut replies: [Option>; + LOG_REPLICAS] = std::array::from_fn(|_| None); + { + let mut pending: [Option>; + LOG_REPLICAS] = std::array::from_fn(|_| None); + for (member, client) in self.clients.iter_mut().enumerate() { + if failures[member].is_some() { + continue; + } + match client.as_mut().unwrap().begin_encoded_before( + crate::cluster::wire::RequestCode::LogOp, + frame.clone(), + self.options.deadline, + ) { + Ok(reply) => pending[member] = Some(reply), + Err(error) => replies[member] = Some(Err(error)), + } + } + while pending.iter().any(Option::is_some) { + let mut progress = false; + for (member, slot) in pending.iter_mut().enumerate() { + let result = slot.as_mut().and_then(|reply| { + let before = reply.transferred_bytes(); + let result = reply.poll(); + progress |= result.is_some() || reply.transferred_bytes() != before; + result + }); + if let Some(result) = result { + replies[member] = Some(result); + *slot = None; + } + } + if !progress { + crate::cluster::replica_set::wait_for_pending(&pending); + } + } + } + std::array::from_fn(|member| { + if let Some(error) = failures[member].take() { + return Err(error); + } + let result = match replies[member].take().unwrap() { + Ok(crate::cluster::message::ReplicaReply::LogOp(reply)) => Ok(reply), + Ok(_) => Err(LogCallError::Refused(ObjectError::Corruption( + "unexpected publication probe reply".into(), + ))), + Err(error) => Err(LogCallError::from_object( + error, + self.clients[member] + .as_ref() + .unwrap() + .needs_standalone_reconnect(), + )), + }; + if matches!(result, Err(LogCallError::Transport(_))) { + self.clients[member] = None; + if reused[member] { + // Match read_call: retry an idle connection once, only for this read. + return self + .client(member) + .and_then(|client| client.log_call(request.clone())); + } + } + result + }) + } + fn stabilize_publication_inner( &mut self, epoch: u64, @@ -137,9 +218,9 @@ impl ChunkLogReader { } } let mut answers = Vec::with_capacity(LOG_REPLICAS); - for (member, initialization) in initializations.iter().enumerate() { - let request = LogOpRequest::publication_probe(self.volume, self.chunk, self.generation); - let answer = match self.read_call(member, |c| c.log_call(request.clone())) { + for (member, result) in self.publication_probes().into_iter().enumerate() { + let initialization = initializations[member]; + let answer = match result { Ok(reply) => { if reply.epoch > epoch { return Err(ChunkLogError::StaleEpoch { @@ -148,7 +229,7 @@ impl ChunkLogReader { } (reply.epoch == epoch && reply - .initialized_publication_prefix(*initialization) + .initialized_publication_prefix(initialization) .is_some()) .then_some(reply) } diff --git a/src/cluster/published_tail_reader_tests.rs b/src/cluster/published_tail_reader_tests.rs index 5cb2b46d..c14040ea 100644 --- a/src/cluster/published_tail_reader_tests.rs +++ b/src/cluster/published_tail_reader_tests.rs @@ -214,3 +214,87 @@ fn published_read_never_exposes_a_durable_unpublished_suffix() { .read_published(1, &[PUB_INIT; 3], 0, 4, || Ok(())) .is_err()); } + +#[test] +fn published_read_starts_every_probe_before_waiting_for_any_reply() { + let (_dir, nodes) = publication_cluster(); + pub_init(&nodes); + for node in &nodes { + pub_append(node, 0, b"complete", 1, 8); + } + let gate = Arc::new((std::sync::Mutex::new(0usize), std::sync::Condvar::new())); + let proxies: Vec<_> = nodes + .iter() + .map(|node| { + FaultProxy::with_probe_gate(node.addr.clone(), 0, false, Some(Arc::clone(&gate))) + }) + .collect(); + let endpoints: Vec<_> = proxies.iter().map(|proxy| proxy.addr.clone()).collect(); + let mut config = options(); + config.read_timeout = Duration::from_secs(5); + let mut reader = ChunkLogReader::new(&endpoints, V, C, 1, config); + assert_eq!( + reader + .read_published(1, &[PUB_INIT; 3], 0, 8, || Ok(())) + .unwrap(), + b"complete" + ); + assert_eq!(*gate.0.lock().unwrap(), 3); + let operations: Vec<_> = proxies + .iter() + .flat_map(|p| p.operations.lock().unwrap().clone()) + .collect(); + assert_eq!( + operations + .iter() + .filter(|&&op| op == super::super::log_op::OP_TAIL) + .count(), + 3 + ); + assert_eq!( + operations + .iter() + .filter(|&&op| op == super::super::log_op::OP_READ) + .count(), + 1 + ); + assert_eq!(operations.len(), 4); +} + +#[test] +fn published_probe_retries_only_a_reused_failed_connection() { + let (_dir, nodes) = publication_cluster(); + pub_init(&nodes); + for node in &nodes { + pub_append(node, 0, b"complete", 1, 8); + } + let proxy = FaultProxy::new(nodes[2].addr.clone(), super::super::log_op::OP_TAIL, false); + let mut eps = endpoints(&nodes); + eps[2] = proxy.addr.clone(); + proxy.armed.store(false, Ordering::SeqCst); + let mut reader = reader(&eps); + assert_eq!( + reader + .stabilize_publication(1, &[PUB_INIT; 3], || Ok(())) + .unwrap(), + 8 + ); + proxy.operations.lock().unwrap().clear(); + proxy.armed.store(true, Ordering::SeqCst); + assert_eq!( + reader + .stabilize_publication(1, &[PUB_INIT; 3], || Ok(())) + .unwrap(), + 8 + ); + assert_eq!( + *proxy.operations.lock().unwrap(), + vec![super::super::log_op::OP_TAIL, super::super::log_op::OP_TAIL,] + ); + assert_eq!( + reader + .stabilize_publication(1, &[PUB_INIT; 3], || Ok(())) + .unwrap(), + 8 + ); +}