Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/cluster/chunk_log_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<(std::sync::Mutex<usize>, std::sync::Condvar)>>,
) -> Self {
use prost::Message as _;
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
Expand Down Expand Up @@ -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 {
Expand All @@ -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)?;
Expand Down
89 changes: 85 additions & 4 deletions src/cluster/published_tail_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogOpReply, LogCallError>; 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<LogCallError>; 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<Result<crate::cluster::message::ReplicaReply, ObjectError>>;
LOG_REPLICAS] = std::array::from_fn(|_| None);
{
let mut pending: [Option<crate::cluster::client::PendingReplicaReply<'_>>;
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,
Expand Down Expand Up @@ -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 {
Expand All @@ -148,7 +229,7 @@ impl ChunkLogReader {
}
(reply.epoch == epoch
&& reply
.initialized_publication_prefix(*initialization)
.initialized_publication_prefix(initialization)
.is_some())
.then_some(reply)
}
Expand Down
84 changes: 84 additions & 0 deletions src/cluster/published_tail_reader_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Loading