mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added replication, Added clean stops, logs and releases (0.2.0)
Step 11: the servers replicate by name (push_to and accept_from), their messages and attachments, damaged copies asked for again. Step 12: the server stops cleanly on SIGTERM/SIGINT (Ctrl+C, Ctrl+Break and the console closing on Windows), committing what was queued; logs go to the console, BAKASHROT_LOG sets the level. release.py builds a release per platform (the programs, the DLL, a Python wheel with the library, THIRD-PARTY.txt) and smoke-tests it; CI runs it on v* tags. Built for size (opt-level "s"): 25-31% smaller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,14 +39,17 @@ logs = "30d"
|
||||
"sensors.*" = "12h"
|
||||
"*" = "forever"
|
||||
|
||||
# Who this server replicates with, by address. A link carries messages
|
||||
# one way: the server that dials sends, the one dialed takes them.
|
||||
[replication]
|
||||
peers = [] # dialed: ["10.0.0.2:7000", "server-2.lan:7000"]
|
||||
push_to = [] # dialed and sent to: ["10.0.0.2:7000", "server-2.lan:7000"]
|
||||
accept_from = [] # taken from: ["10.0.0.2", "server-2.lan"], or ["*"]
|
||||
# The only topics replicated at all, whatever the roles say: a filter for
|
||||
# edge nodes. Without it, no filter; [] replicates nothing.
|
||||
# replicated_topics = ["logs", "alerts.*"]
|
||||
retry_delay = "10s"
|
||||
sync_interval = "5s"
|
||||
batch_size = 100 # messages per packet, 1-10000
|
||||
batch_size = 1000 # messages per packet, 1-10000
|
||||
|
||||
# What certificates with a role may do. A certificate gets everything its
|
||||
# roles allow; one without any role listed here is refused. Every key is
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
//!
|
||||
//! - [`Server`]: where it listens, its files, its topics, its database.
|
||||
//! - [`Retention`]: how long each topic's messages are kept.
|
||||
//! - [`Replication`]: the peers it dials, what it replicates, how often.
|
||||
//! - [`Replication`]: the servers it sends to and takes from, what it
|
||||
//! replicates, how often.
|
||||
//! - [`Role`]: what certificates with a role may do; [`Permissions`]: what
|
||||
//! a certificate's roles allow together.
|
||||
//!
|
||||
@@ -200,20 +201,26 @@ impl Retention {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Replication {
|
||||
/// The peers it dials (`host:port`); peers dialing in come by
|
||||
/// themselves. What's replicated with each is its roles'.
|
||||
/// The servers it dials and sends to (`host:port`). What's sent to
|
||||
/// each is its roles' `send_*`.
|
||||
#[serde(deserialize_with = "values::peers")]
|
||||
pub peers: Vec<String>,
|
||||
pub push_to: Vec<String>,
|
||||
/// The servers it takes replicated messages from: IP addresses or
|
||||
/// hostnames, `*` for all; none by default. What's taken is their
|
||||
/// roles' `accept_*`.
|
||||
#[serde(deserialize_with = "values::hosts")]
|
||||
pub accept_from: Vec<String>,
|
||||
/// The only topics replicated at all, whatever the roles say (an edge
|
||||
/// node's filter); none: no filter.
|
||||
pub replicated_topics: Option<NameList>,
|
||||
/// How long to wait before dialing a peer again; 10s by default.
|
||||
#[serde(deserialize_with = "values::duration")]
|
||||
pub retry_delay: Duration,
|
||||
/// How often a linked peer is sent what it misses; 5s by default.
|
||||
/// How long a link waits, once nothing was left to send, before it
|
||||
/// looks again; 5s by default.
|
||||
#[serde(deserialize_with = "values::duration")]
|
||||
pub sync_interval: Duration,
|
||||
/// The most messages sent to a peer per packet; 100 by default.
|
||||
/// The most messages sent to a peer per packet; 1000 by default.
|
||||
#[serde(deserialize_with = "values::batch_size")]
|
||||
pub batch_size: u32,
|
||||
}
|
||||
@@ -221,11 +228,12 @@ pub struct Replication {
|
||||
impl Default for Replication {
|
||||
fn default() -> Self {
|
||||
Replication {
|
||||
peers: Vec::new(),
|
||||
push_to: Vec::new(),
|
||||
accept_from: Vec::new(),
|
||||
replicated_topics: None,
|
||||
retry_delay: Duration::from_secs(10),
|
||||
sync_interval: Duration::from_secs(5),
|
||||
batch_size: 100,
|
||||
batch_size: 1000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,30 @@ impl Permissions {
|
||||
.any(|role| role.accept_topics.matches(topic) && role.accept_nodes.matches(node))
|
||||
}
|
||||
|
||||
/// The topics and the nodes taken from it, as `SyncState` lists them:
|
||||
/// its roles' `accept_*` (of those that accept topics), none twice.
|
||||
/// What's taken is still [`Permissions::accepts`]'.
|
||||
pub fn accepted(&self) -> (Vec<String>, Vec<String>) {
|
||||
let (mut topics, mut nodes) = (Vec::<String>::new(), Vec::<String>::new());
|
||||
for role in self
|
||||
.roles
|
||||
.iter()
|
||||
.filter(|role| !role.accept_topics.is_empty())
|
||||
{
|
||||
for (list, entries) in [
|
||||
(&mut topics, role.accept_topics.entries()),
|
||||
(&mut nodes, role.accept_nodes.entries()),
|
||||
] {
|
||||
for entry in entries {
|
||||
if !list.iter().any(|known| names_equal(known, entry)) {
|
||||
list.push(entry.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(topics, nodes)
|
||||
}
|
||||
|
||||
/// Whether it replicates at all: a peer.
|
||||
pub fn replicates(&self) -> bool {
|
||||
self.roles
|
||||
|
||||
@@ -91,16 +91,19 @@ pub(crate) fn is_peer_address(text: &str) -> bool {
|
||||
let port = port.parse::<u16>().is_ok_and(|port| port != 0);
|
||||
let host = match host.strip_prefix('[').and_then(|h| h.strip_suffix(']')) {
|
||||
Some(v6) => v6.parse::<Ipv6Addr>().is_ok(),
|
||||
None => {
|
||||
!host.is_empty()
|
||||
&& host
|
||||
.bytes()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == b'.' || c == b'-')
|
||||
}
|
||||
None => is_hostname(host),
|
||||
};
|
||||
port && host
|
||||
}
|
||||
|
||||
/// A hostname or an IPv4 address: letters, digits, dots and dashes.
|
||||
fn is_hostname(host: &str) -> bool {
|
||||
!host.is_empty()
|
||||
&& host
|
||||
.bytes()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == b'.' || c == b'-')
|
||||
}
|
||||
|
||||
// As serde reads them (`deserialize_with`): a value that's wrong fails
|
||||
// there, and toml says on which line.
|
||||
|
||||
@@ -149,6 +152,27 @@ pub(crate) fn peers<'de, D: Deserializer<'de>>(reader: D) -> Result<Vec<String>,
|
||||
Ok(peers)
|
||||
}
|
||||
|
||||
/// Hosts without a port: IP addresses or hostnames, or `*` for all; none
|
||||
/// twice.
|
||||
pub(crate) fn hosts<'de, D: Deserializer<'de>>(reader: D) -> Result<Vec<String>, D::Error> {
|
||||
let hosts = Vec::<String>::deserialize(reader)?;
|
||||
for (at, host) in hosts.iter().enumerate() {
|
||||
if host != "*" && !is_hostname(host) && host.parse::<Ipv6Addr>().is_err() {
|
||||
return Err(D::Error::custom(format!(
|
||||
"'{host}' isn't an IP address or a hostname (without a port), like \
|
||||
\"10.0.0.2\" or \"server-2.lan\", or \"*\" for all"
|
||||
)));
|
||||
}
|
||||
if hosts[..at]
|
||||
.iter()
|
||||
.any(|before| before.eq_ignore_ascii_case(host))
|
||||
{
|
||||
return Err(D::Error::custom(format!("'{host}' is given twice")));
|
||||
}
|
||||
}
|
||||
Ok(hosts)
|
||||
}
|
||||
|
||||
/// A batch size, 1 to `MAX_MESSAGES_PER_PACKET`.
|
||||
pub(crate) fn batch_size<'de, D: Deserializer<'de>>(reader: D) -> Result<u32, D::Error> {
|
||||
let size = u32::deserialize(reader)?;
|
||||
|
||||
@@ -42,7 +42,8 @@ fn the_example() {
|
||||
assert!(!server.has_topic("sensors"));
|
||||
assert_eq!(server.synchronous, Synchronous::Full);
|
||||
assert_eq!(server.max_attachment_size, 1 << 30);
|
||||
assert_eq!(config.replication.peers, Vec::<String>::new());
|
||||
assert_eq!(config.replication.push_to, Vec::<String>::new());
|
||||
assert_eq!(config.replication.accept_from, Vec::<String>::new());
|
||||
assert_eq!(config.retention.age("logs"), days(30));
|
||||
assert_eq!(
|
||||
config.retention.age("sensors.kitchen"),
|
||||
@@ -65,7 +66,7 @@ fn defaults() {
|
||||
assert_eq!(config.retention.age("logs"), None);
|
||||
assert_eq!(config.replication.retry_delay, Duration::from_secs(10));
|
||||
assert_eq!(config.replication.sync_interval, Duration::from_secs(5));
|
||||
assert_eq!(config.replication.batch_size, 100);
|
||||
assert_eq!(config.replication.batch_size, 1000);
|
||||
assert_eq!(config.replication.replicated_topics, None);
|
||||
assert!(config.roles.is_empty());
|
||||
assert!(config.permissions(&["anything"]).is_none());
|
||||
@@ -81,7 +82,8 @@ max_attachment_size = 0
|
||||
retention_interval = "1h30m"
|
||||
|
||||
[replication]
|
||||
peers = ["10.0.0.2:7000", "server-3.lan:7001", "[fd00::4]:7000"]
|
||||
push_to = ["10.0.0.2:7000", "server-3.lan:7001", "[fd00::4]:7000"]
|
||||
accept_from = ["10.0.0.2", "server-3.lan", "fd00::4"]
|
||||
retry_delay = "1m"
|
||||
sync_interval = "2s"
|
||||
batch_size = 10000
|
||||
@@ -93,7 +95,8 @@ replicated_topics = ["logs"]
|
||||
assert_eq!(config.server.synchronous, Synchronous::Normal);
|
||||
assert_eq!(config.server.max_attachment_size, 0);
|
||||
assert_eq!(config.server.retention_interval, Duration::from_secs(5400));
|
||||
assert_eq!(config.replication.peers.len(), 3);
|
||||
assert_eq!(config.replication.push_to.len(), 3);
|
||||
assert_eq!(config.replication.accept_from[2], "fd00::4");
|
||||
assert_eq!(config.replication.retry_delay, Duration::from_secs(60));
|
||||
assert_eq!(config.replication.batch_size, 10_000);
|
||||
}
|
||||
@@ -181,6 +184,10 @@ accept_nodes = ["garage"]
|
||||
// A role without the topics replicates nothing, whatever its nodes.
|
||||
let only_peer = config.permissions(&["peer"]).unwrap();
|
||||
assert!(!only_peer.accepts("logs", "kitchen-1"));
|
||||
// What SyncState lists: the accepting roles' lists only.
|
||||
let strings = |list: &[&str]| list.iter().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||
assert_eq!(peer.accepted(), (strings(&["*"]), strings(&["garage"])));
|
||||
assert_eq!(only_peer.accepted(), (vec![], vec![]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -324,13 +331,25 @@ fn refusals() {
|
||||
),
|
||||
// [replication]
|
||||
(
|
||||
&with("[replication]\npeers = [\"10.0.0.2\"]\n"),
|
||||
&with("[replication]\npush_to = [\"10.0.0.2\"]\n"),
|
||||
"isn't a host and a port",
|
||||
),
|
||||
(
|
||||
&with("[replication]\npeers = [\"a:1\", \"a:1\"]\n"),
|
||||
&with("[replication]\npush_to = [\"a:1\", \"a:1\"]\n"),
|
||||
"'a:1' is given twice",
|
||||
),
|
||||
(
|
||||
&with("[replication]\naccept_from = [\"10.0.0.2:7000\"]\n"),
|
||||
"without a port",
|
||||
),
|
||||
(
|
||||
&with("[replication]\naccept_from = [\"a.lan\", \"A.lan\"]\n"),
|
||||
"'A.lan' is given twice",
|
||||
),
|
||||
(
|
||||
&with("[replication]\npeers = []\n"),
|
||||
"unknown field `peers`",
|
||||
),
|
||||
(
|
||||
&with("[replication]\nbatch_size = 0\n"),
|
||||
"batch size must be 1 to 10000",
|
||||
|
||||
Reference in New Issue
Block a user