Skip to content
Open
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
63 changes: 48 additions & 15 deletions crates/apache-pulsar/tests/integration/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,25 +338,49 @@ fn failover(env: &Env, failover: Failover) {
let topic = format!("topic://{path}");
env.admin(&["scalable-topics", "create", "-s", "2", &path]);
let (failed_over, producing) = (AtomicBool::new(false), AtomicBool::new(true));
// How many sends completed on each segment; and why the topic couldn't fail over, if it couldn't.
let (completed, gave_up) = (Mutex::new(BTreeMap::<u64, usize>::new()), OnceLock::<String>::new());
// The producer's registrations on the two segments, before the unload.
let registered = OnceLock::new();
std::thread::scope(|scope| {
let (start, fail_over) = std::sync::mpsc::channel();
let (failed_over, namespace, path, registered) = (&failed_over, &namespace, &path, &registered);
let (completed, gave_up) = (&completed, &gave_up);
scope.spawn(move || {
fail_over.recv().unwrap();
// Whether `done` holds within `seconds`, asked every 200 ms.
let within = |seconds, done: &mut dyn FnMut() -> bool| {
let deadline = Instant::now() + Duration::from_secs(seconds);
while !done() {
if Instant::now() > deadline {
return false;
}
std::thread::sleep(Duration::from_millis(200));
}
true
};
// The keys go to both segments: the topic fails over once a send to each has completed.
if !within(120, &mut || completed.lock().unwrap().len() == 2) {
let completed = completed.lock().unwrap().clone();
let reason = format!("in 120 s, sends completed on {} of 2 segments: {completed:?}", completed.len());
gave_up.set(reason).unwrap();
return;
}
match failover {
Failover::Unload => {
// The keys go to both segments: wait for the producer on each.
let deadline = Instant::now() + Duration::from_secs(30);
let before = loop {
let before = producers(env, path);
if before.len() == 2 || Instant::now() > deadline {
break before;
}
std::thread::sleep(Duration::from_millis(200));
};
assert_eq!(before.len(), 2, "the producer on each segment: {before:?}");
// The producer's registration on each segment, as the topic's stats list them.
let mut before = BTreeMap::new();
let listed = within(60, &mut || {
before = producers(env, path);
before.len() == 2
});
if !listed {
let completed = completed.lock().unwrap().clone();
let listing = format!("the topic's stats listed its producers as {before:?}");
let reason = format!("sends completed on both segments ({completed:?}), but in 60 s {listing}");
gave_up.set(reason).unwrap();
return;
}
registered.set(before).unwrap();
env.admin(&["namespaces", "unload", namespace]);
}
Expand Down Expand Up @@ -399,25 +423,31 @@ fn failover(env: &Env, failover: Failover) {
// A send's message is queued when its future is first polled: poll the new one, and take the
// ones that completed, without waiting.
while let Some(Some(result)) = sends.next().now_or_never() {
result.expect("a send made across the failover");
let id = result.expect("a send made across the failover");
*completed.lock().unwrap().entry(segment_of(&id.to_string())).or_default() += 1;
}
if i == 20 {
start.send(()).unwrap();
}
after += usize::from(failed_over.load(Ordering::Acquire));
if after == 20 {
if after == 20 || gave_up.get().is_some() {
break;
}
compio::time::sleep(Duration::from_millis(50)).await;
}
while let Some(result) = sends.next().await {
result.expect("a send made across the failover");
// If the topic couldn't fail over, a send may never complete: the case fails without waiting for it.
if gave_up.get().is_none() {
while let Some(result) = sends.next().await {
result.expect("a send made across the failover");
}
}
producing.store(false, Ordering::Release);
};
let deadline = Instant::now() + Duration::from_secs(240);
let reading = |got: &[Message<String>]| {
(producing.load(Ordering::Acquire) || firsts(got) < total()) && Instant::now() < deadline
(producing.load(Ordering::Acquire) || firsts(got) < total())
&& Instant::now() < deadline
&& gave_up.get().is_none()
};
let wait = Duration::from_secs(1);
let read_queue = async {
Expand Down Expand Up @@ -449,6 +479,9 @@ fn failover(env: &Env, failover: Failover) {
};
let ((), from_queue, from_stream, from_checkpoint) =
futures_util::join!(produce, read_queue, read_stream, read_checkpoint);
if let Some(reason) = gave_up.get() {
panic!("the topic couldn't fail over: {reason}");
}
assert!(failed_over.load(Ordering::Acquire), "the failover did not complete");
if let Some(before) = registered.get() {
// The broker did let go of the producer: it is back on both segments, registered again.
Expand Down
17 changes: 9 additions & 8 deletions tests/conformance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ message once, from segments 0 to 4, and the Stream consumer receives every messa

Both cases run the same clients: a producer whose sends never time out, a Queue consumer, a Stream consumer and a
Checkpoint consumer, from the earliest position, on a topic of two segments in a namespace of the case's own. The
producer sends 20 messages over 5 keys, then a message every 50 ms while the topic fails over and 20 more once it is
back. The consumers receive meanwhile, the Queue and Stream consumers acknowledging. A message may come twice after a
failover; what counts is the first time each comes. *Passes* when no send fails, and each consumer receives every
message, the Stream and the Checkpoint consumers each key's in order.

**`failover-by-unload`** (C14, C7, C8, C9). The namespace is unloaded (`namespaces unload`): the broker closes the
topic's producers and consumers and gives the topic up, as when its ownership moves to another broker. *Passes* as
above, and the topic's stats show the producer registered again on both segments, each with a new `connectedSince`.
producer sends a message every 50 ms, over 5 keys. Once 20 are sent and a send to each segment has completed, the topic
fails over; the producer sends 20 more once it is back. The consumers receive meanwhile, the Queue and Stream consumers
acknowledging. A message may come twice after a failover; what counts is the first time each comes. *Passes* when no
send fails, and each consumer receives every message, the Stream and the Checkpoint consumers each key's in order.

**`failover-by-unload`** (C14, C7, C8, C9). Once the topic's stats list the producer on both segments, the namespace is
unloaded (`namespaces unload`): the broker closes the topic's producers and consumers and gives the topic up, as when
its ownership moves to another broker. *Passes* as above, and the topic's stats show the producer registered again on
both segments, each with a new `connectedSince`.
*This client:* `conformance::failover_by_unload`.

**`failover-by-broker-restart`** (C14, C7, C8, C9). The broker is killed, every connection dropping, and started
Expand Down
Loading