mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Fixed two peers sending the same attachment at once, Noted the stream limits, Removed the PureBasic-era leftovers
- An attachment is received from one peer at a time: the others are refused NotReady and ask again at their next sync; one cut short lets the next one take it over - A 'replicating' row with all its bytes but never checked is asked again from part 0, and a peer asking from the wrong part resets it - Noted the stream limit (1,048,575 topic/node pairs, 2^43-1 message ids each) in the example config and known-issues.md - Removed NoDefaultTopic (code 6, retired), Writer::vacuum, the v8 detection and the old ini key tests - Cleared the PureBasic-era notes from the docs, revamp.md keeping only the old open items still unfixed; noted the GUI explorer (egui) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,46 @@ use bakashrot_proto::packets::{
|
||||
use bakashrot_proto::{ErrorCode, Packet};
|
||||
use bakashrot_storage::AttachmentState;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, PoisonError};
|
||||
use tracing::warn;
|
||||
|
||||
/// A replicated attachment being received over one connection: another
|
||||
/// asking for it is refused until this is dropped (finished, refused,
|
||||
/// the connection gone).
|
||||
struct Receiving<'a> {
|
||||
shared: &'a Shared,
|
||||
key: (String, String, u64),
|
||||
}
|
||||
|
||||
impl<'a> Receiving<'a> {
|
||||
/// None when it's being received already. Names without case, as
|
||||
/// everywhere ("Logs" is "logs").
|
||||
fn claim(shared: &'a Shared, topic: &str, node: &str, message_id: u64) -> Option<Self> {
|
||||
let key = (
|
||||
topic.to_ascii_lowercase(),
|
||||
node.to_ascii_lowercase(),
|
||||
message_id,
|
||||
);
|
||||
let claimed = shared
|
||||
.receiving
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.insert(key.clone());
|
||||
// Made only when claimed, the lock let go: dropping one releases it.
|
||||
claimed.then(|| Receiving { shared, key })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Receiving<'_> {
|
||||
fn drop(&mut self) {
|
||||
let mut receiving = self
|
||||
.shared
|
||||
.receiving
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
receiving.remove(&self.key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `address` is in `accept_from`: `*`, an address as written, or
|
||||
/// a hostname resolving to it (looked up now).
|
||||
@@ -152,6 +191,16 @@ pub(crate) async fn replicate_attachment(
|
||||
return Ok(refusal);
|
||||
}
|
||||
let message = format!("{}:{}#{}", request.topic, request.node, request.message_id);
|
||||
// One peer at a time per attachment, until this returns (or the
|
||||
// connection goes): two at once would both be refused a part.
|
||||
let Some(_receiving) =
|
||||
Receiving::claim(shared, &request.topic, &request.node, request.message_id)
|
||||
else {
|
||||
return Ok(refused(
|
||||
ErrorCode::NotReady,
|
||||
format!("{message}: being received from another server"),
|
||||
));
|
||||
};
|
||||
let ReplicateAttachment {
|
||||
topic,
|
||||
node,
|
||||
@@ -175,8 +224,22 @@ pub(crate) async fn replicate_attachment(
|
||||
size: stored.size,
|
||||
sha256: stored.sha256,
|
||||
};
|
||||
let next = stored.received / ATTACHMENT_PART_SIZE;
|
||||
if from_part != next || stored.received == stored.size {
|
||||
// Every part came but nobody is checking them (finishing it failed):
|
||||
// started over, it's never taken unchecked.
|
||||
let mut received = stored.received;
|
||||
if received == stored.size {
|
||||
warn!("{message}: its attachment came whole but wasn't checked, asked for again");
|
||||
let restart = Write::FinishReplicated {
|
||||
attachment: stored.id as u64,
|
||||
matches: false,
|
||||
};
|
||||
if let Err(refusal) = shared.pushes.send(restart).await {
|
||||
return Ok(refusal.into());
|
||||
}
|
||||
received = 0;
|
||||
}
|
||||
let next = received / ATTACHMENT_PART_SIZE;
|
||||
if from_part != next {
|
||||
return Ok(refused(
|
||||
ErrorCode::InvalidRequest,
|
||||
format!("{message}: part {next} is the next one here, not {from_part}"),
|
||||
|
||||
@@ -28,6 +28,7 @@ use bakashrot_pki::{Revocations, SharedRevocations, summarize, tls};
|
||||
use bakashrot_proto::ErrorCode;
|
||||
use bakashrot_proto::packets::RequestRefused;
|
||||
use bakashrot_storage::{Database, QueryRunner, Reader};
|
||||
use std::collections::HashSet;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -66,6 +67,9 @@ struct Shared {
|
||||
readers: Mutex<Vec<Reader>>,
|
||||
/// Query connections not in use.
|
||||
queries: Mutex<Vec<QueryRunner>>,
|
||||
/// The replicated attachments being received (topic, node, message
|
||||
/// id): one peer at a time each (`accept_from.rs`).
|
||||
receiving: Mutex<HashSet<(String, String, u64)>>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
@@ -108,6 +112,7 @@ impl Server {
|
||||
pushes,
|
||||
readers: Mutex::new(Vec::new()),
|
||||
queries: Mutex::new(Vec::new()),
|
||||
receiving: Mutex::new(HashSet::new()),
|
||||
config,
|
||||
}),
|
||||
})
|
||||
@@ -253,7 +258,7 @@ impl Shared {
|
||||
|
||||
/// Checkpoints the WAL every second, on a thread and a connection of its
|
||||
/// own, so the writer doesn't do it in the middle of a commit (2.5 times
|
||||
/// the pushes: `docs/storage.md`, "Against the old storage").
|
||||
/// the pushes: `docs/storage.md`, "The benchmark").
|
||||
fn start_checkpoints(database: &Database) -> Result<(), String> {
|
||||
let checkpointer = database.checkpointer().map_err(|error| error.to_string())?;
|
||||
std::thread::Builder::new()
|
||||
|
||||
Reference in New Issue
Block a user