mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-03 22:01:50 +00:00
fix(service/sending): Restart crashed sender workers and resend queued backlogs
Signed-off-by: Erwan Leboucher <erwanleboucher@gmail.com>
This commit is contained in:
1 parent
919fea9b97
commit
cf45dcffbb
12 files changed
+415
-85
No files matched your search
@@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
fmt::Debug,
|
||||
panic::AssertUnwindSafe,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicU64, AtomicUsize, Ordering},
|
||||
@@ -55,6 +56,9 @@ use serde_json::value::{RawValue as RawJsonValue, to_raw_value};
|
||||
|
||||
use super::{Destination, EduBuf, EduVec, Msg, SendingEvent, Service, data::QueueItem};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum TransactionStatus {
|
||||
Running,
|
||||
@@ -78,12 +82,18 @@ pub const EDU_LIMIT: usize = 100;
|
||||
|
||||
impl Service {
|
||||
#[tracing::instrument(skip(self), level = "debug")]
|
||||
pub(super) async fn sender(self: Arc<Self>, id: usize) -> Result {
|
||||
pub(super) async fn sender(self: Arc<Self>, id: usize, restarted: bool) -> Result {
|
||||
let mut statuses: CurTransactionStatus = CurTransactionStatus::new();
|
||||
let mut futures: SendingFutures<'_> = FuturesUnordered::new();
|
||||
|
||||
// The previous worker of this shard may have dropped requests it had
|
||||
// taken from the channel.
|
||||
if restarted {
|
||||
self.dispatch_queued(Some(id)).await;
|
||||
}
|
||||
|
||||
info!(shard = id, "Running startup netburst");
|
||||
self.startup_netburst(id, &mut futures, &mut statuses)
|
||||
self.startup_netburst(id, restarted, &mut futures, &mut statuses)
|
||||
.boxed()
|
||||
.await;
|
||||
info!(shard = id, "Finished startup netburst");
|
||||
@@ -141,7 +151,17 @@ impl Service {
|
||||
statuses: &mut CurTransactionStatus,
|
||||
) {
|
||||
match response {
|
||||
| Ok(dest) => self.handle_response_ok(&dest, futures, statuses).await,
|
||||
| Ok(dest) => {
|
||||
if let Destination::Federation(server_name) = &dest {
|
||||
self.services.federation.mark_healthy(server_name);
|
||||
}
|
||||
self.handle_response_ok(&dest, futures, statuses).await;
|
||||
},
|
||||
// A bug on our side, not the remote's fault, and resending the same
|
||||
// events would panic again: drop them and move on.
|
||||
| Err((dest, e)) if e.is_panic() => {
|
||||
self.handle_response_ok(&dest, futures, statuses).await;
|
||||
},
|
||||
| Err((dest, e)) => {
|
||||
if let Destination::Federation(dest) = &dest {
|
||||
if self.services.federation.should_mark_stale(&e)
|
||||
@@ -188,9 +208,6 @@ impl Service {
|
||||
) {
|
||||
let _cork = self.db.db.cork();
|
||||
self.db.delete_all_active_requests_for(dest).await;
|
||||
if let Destination::Federation(server_name) = dest {
|
||||
self.services.federation.mark_healthy(server_name);
|
||||
}
|
||||
|
||||
// Find events that have been added since starting the last request
|
||||
let new_events = self
|
||||
@@ -274,7 +291,8 @@ impl Service {
|
||||
}
|
||||
|
||||
/// Performs startup netburst, which sends any previously queued up but not
|
||||
/// yet sent requests immediately.
|
||||
/// yet sent requests immediately. A restarted worker always resends its
|
||||
/// shard's requests in flight, without trimming them.
|
||||
#[tracing::instrument(
|
||||
name = "netburst",
|
||||
level = "debug",
|
||||
@@ -285,6 +303,7 @@ impl Service {
|
||||
async fn startup_netburst<'a>(
|
||||
&'a self,
|
||||
id: usize,
|
||||
restarted: bool,
|
||||
futures: &mut SendingFutures<'a>,
|
||||
statuses: &mut CurTransactionStatus,
|
||||
) {
|
||||
@@ -299,8 +318,9 @@ impl Service {
|
||||
}
|
||||
|
||||
let entry = txns.entry(dest.clone()).or_default();
|
||||
let over_budget =
|
||||
self.server.config.startup_netburst_keep >= 0 && entry.len() >= keep;
|
||||
let over_budget = !restarted
|
||||
&& self.server.config.startup_netburst_keep >= 0
|
||||
&& entry.len() >= keep;
|
||||
|
||||
if over_budget && !self.survives_netburst_trim(&event).await {
|
||||
warn!(
|
||||
@@ -315,8 +335,12 @@ impl Service {
|
||||
}
|
||||
}
|
||||
|
||||
if !restarted && !self.server.config.startup_netburst {
|
||||
return;
|
||||
}
|
||||
|
||||
for (dest, events) in txns {
|
||||
if self.server.config.startup_netburst && !events.is_empty() {
|
||||
if !events.is_empty() {
|
||||
statuses.insert(dest.clone(), TransactionStatus::Running);
|
||||
futures.push(self.send_events(dest, events));
|
||||
}
|
||||
@@ -772,6 +796,7 @@ impl Service {
|
||||
});
|
||||
|
||||
let start = Instant::now();
|
||||
let panic_dest = dest.clone();
|
||||
let send = match dest {
|
||||
| Destination::Federation(server) =>
|
||||
self.send_events_dest_federation(server, events).boxed(),
|
||||
@@ -780,24 +805,42 @@ impl Service {
|
||||
self.send_events_dest_push(user_id, pushkey, events).boxed(),
|
||||
};
|
||||
|
||||
send.inspect(move |result| {
|
||||
let instruments = &self.server.metrics.instruments;
|
||||
let outcome =
|
||||
KeyValue::new("outcome", if result.is_ok() { "success" } else { "failure" });
|
||||
instruments
|
||||
.sender_transaction_duration
|
||||
.record(start.elapsed().as_secs_f64(), &[destination.clone(), outcome.clone()]);
|
||||
for (kind, count) in [("pdu", pdus), ("edu", edus)] {
|
||||
if count > 0 {
|
||||
instruments.sender_events.add(count, &[
|
||||
AssertUnwindSafe(send)
|
||||
.catch_unwind()
|
||||
.map(move |result| {
|
||||
result.unwrap_or_else(|panic| {
|
||||
let error = Error::from_panic(panic);
|
||||
error!(dest = ?panic_dest, ?error, "Panicked while sending transaction, dropping it");
|
||||
self.server
|
||||
.metrics
|
||||
.instruments
|
||||
.sender_panics
|
||||
.add(1, &[KeyValue::new("scope", "transaction")]);
|
||||
|
||||
Err((panic_dest, Box::new(error)))
|
||||
})
|
||||
})
|
||||
.inspect(move |result| {
|
||||
let instruments = &self.server.metrics.instruments;
|
||||
let outcome =
|
||||
KeyValue::new("outcome", if result.is_ok() { "success" } else { "failure" });
|
||||
instruments
|
||||
.sender_transaction_duration
|
||||
.record(start.elapsed().as_secs_f64(), &[
|
||||
destination.clone(),
|
||||
KeyValue::new("kind", kind),
|
||||
outcome.clone(),
|
||||
]);
|
||||
for (kind, count) in [("pdu", pdus), ("edu", edus)] {
|
||||
if count > 0 {
|
||||
instruments.sender_events.add(count, &[
|
||||
destination.clone(),
|
||||
KeyValue::new("kind", kind),
|
||||
outcome.clone(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.boxed()
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
|
||||
Reference in new issue
Block a user