1
0
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:
2026-11-16 17:23:33 +01:00
parent 764bc3652a
commit a5a2f65442
38 changed files with 436 additions and 907 deletions
+1 -3
View File
@@ -29,9 +29,7 @@ is a CRL file from the CA, pushed to the servers.
only digits), 64 roles at most. Roles mean nothing to the certificates:
the server's config says what each may do.
- **Its OID is `1.3.6.1.4.1.67681.1.1`** (`IDENTITY_OID`), under
NibblePoker's own PEN (67681), since 0.3.0. Certificates made before
(a placeholder OID) aren't read: issue them again. The branches:
`oid.md`.
NibblePoker's own PEN (67681). The branches: `oid.md`.
- **The CN is the name too**, for people (`openssl x509 -text`, logs). Only
the extension is trusted.
- **Clients' certificates** are for `clientAuth`. **Servers'** are for
+13 -35
View File
@@ -5,9 +5,9 @@ What apps connect with, in layers:
| Layer | Where | For |
|---|---|---|
| `bakashrot-client` | `crates/bakashrot-client` | Rust: blocking, one connection per `Client` |
| The DLL | `crates/bakashrot-capi`: `bakashrot.dll`, `libbakashrot.so` | C and anything that loads a DLL: the old `BakaShrot_*` exports, one connection per process |
| The DLL | `crates/bakashrot-capi`: `bakashrot.dll`, `libbakashrot.so` | C and anything that loads a DLL: the `BakaShrot_*` exports, one connection per process |
| C header | `bindings/c/bakashrot.h` | generated from the DLL (cbindgen) |
| PureBasic | `bindings/purebasic/` | the prototypes and the error codes, as the old DLL's |
| PureBasic | `bindings/purebasic/` | the prototypes and the error codes |
| Python | `bindings/python/bakashrot.py` | `Client` over the DLL (ctypes), standard library only |
| The tools | `crates/bakashrot-tools`: `bakashrot` | the command line, over `bakashrot-client` |
@@ -40,7 +40,7 @@ crl = "ca.crl" # optional; a file not there yet: none
## The DLL
The old DLL's conventions, kept:
Its conventions:
- **One connection per process.** Calls from several threads wait for each
other.
@@ -82,25 +82,9 @@ The old DLL's conventions, kept:
The header has each one's comment. Every name starts with `BakaShrot_`.
### Changed from the PureBasic DLL
- **Connecting:** certificates instead of keys and a node:
`SetCaCertificate`, `SetCertificate`, `SetCrl` are new, `SetPrivateKey`
takes a PEM key; `SetPublicKey`, `SetServerPublicKey`, `SetNode`,
`SetNodeId` are gone. `GetNode` is new (the node comes from the
certificate).
- **No default topic:** `PushMessage(Data)` is gone, `PushMessageTo` names
its topic.
- **Attachments:** the same six, by names.
- **No ids:** `GetLatestMessage(TopicId, NodeId)`, `GetMessageTopicId`,
`GetMessageNodeId`, `ResolveTopic`, `ResolveNode`, `GetResolvedName` are
gone; `GetNamesList` lists names.
- **The file:** `bakashrot.dll` (`libbakashrot.so`), no libsodium next to
it.
## Python
`bindings/python/bakashrot.py`, the old module adapted: `Client(server, ca,
`bindings/python/bakashrot.py`: `Client(server, ca,
certificate, key, crl=None)` (PEM text or paths) or
`Client.from_file("client.toml")` (Python 3.11+, `tomllib`; the rest runs
on 3.8), `client.node` once connected, `push(data, topic,
@@ -118,15 +102,15 @@ wheel with the library in it (`pip install bakashrot-*.whl`, nothing else:
`bindings/purebasic/BakaShrot_Client_Prototypes.pbi` (with
`BakaShrot_ErrorCodes.pbi`): `BakaShrot_LoadClient("bakashrot.dll")`, then
the `W` exports under the old names, `BakaShrot_*String()` helpers for the
the `W` exports without their suffix, `BakaShrot_*String()` helpers for the
getters. Run directly, it's an example: `ClientExample <DllPath> <CaFile>
<CertificateFile> <KeyFile> <Host> <Port>`, pushing a message and one
with an attachment, reading both back (checked by hand with 5.73 against
a server; not in CI, which has no PureBasic).
with an attachment, reading both back (run by hand against a server;
not in CI, which has no PureBasic).
## The tools
`bakashrot`, the old `BakaShrot*` tools in one program (`bakashrot-cert`,
`bakashrot`, one program to push, read, query and list (`bakashrot-cert`,
for the CA, is apart: `certificates.md`). Each command connects with
`-c <file>` (`client.toml` by default), does one thing and exits: 0, 1 on a
failure (why on stderr: `Refused by the server (NotAllowed): ...`,
@@ -158,14 +142,10 @@ bakashrot list nodes --json {"kind":"node","names":["kitchen"
- **Messages and SQL:** inline, `@file` or `-` (a UTF-8 BOM dropped).
PowerShell before 7.3 drops the double quotes inside arguments: use a
file there.
- **Changed from the old tools:** one program; `-c` instead of the last
argument, `client.toml` instead of `client.ini`; `push` names its topic;
names only (no ids in the output, `ResolveIds` gone); errors on stderr;
`GetAttachment` is the `attachment` command.
- **Speed** (release, a local server, 2026-11-14): a command takes about
11 ms, connecting included (the old tools 8 ms); `messages --limit 0`
11 ms, connecting included; `messages --limit 0`
writes 200,000 messages to a file in 0.26 s (Python's `iter_messages()`:
1.5 s). Against the old ones: `server.md`, "Measured".
1.5 s). More: `server.md`, "Measured".
## Tests
@@ -176,12 +156,10 @@ bakashrot list nodes --json {"kind":"node","names":["kitchen"
unaligned), selecting messages, attachments (from a file and memory,
saved and loaded, the buffer, refused ones), every refusal and the
client's own errors, closing, another identity.
- `python.rs`: runs `bindings/python/check_bakashrot.py` (the old one,
ported: 52 checks) on the DLL; skipped, saying so, without Python 3.11+.
- `python.rs`: runs `bindings/python/check_bakashrot.py` on the DLL; skipped, saying so, without Python 3.11+.
- `header.rs`: `bindings/c/bakashrot.h` matches the exports (rewritten
when it doesn't: check it in).
`crates/bakashrot-tools/tests/bakashrot.rs` runs the tool against a server:
the old GetMessages suite's checks (`--follow` too), the Names,
BigMessages and Attachments suites' tool checks, queries, lists,
`client.toml`.
messages (`--follow` too), pushes and refusals, big messages,
attachments, queries, lists, `client.toml`.
+3 -19
View File
@@ -15,7 +15,7 @@ Decided in `../revamp.md`, "Decided".
| `[replication]` | the servers it sends to and takes from, retry and sync pace, batch size |
| `[roles.<name>]` | what certificates with that role may do |
Unknown keys are refused, so a typo or a key of the old ini is caught.
Unknown keys are refused, so a typo is caught.
## Who's who: certificates, then roles
@@ -100,27 +100,11 @@ file's order, else `"*"`'s line wherever it is; without any, it's kept
forever. Ages count from when a message was first pushed, on whichever
server; each server has its own.
## What changed from the old ini
| Old (`OldSource/Server/Config/schema.ini`) | Now |
|---|---|
| `PrivateKey`, `[BakaShrot] Node` | `ca`, `certificate`, `key`: the node is the certificate's name |
| `[BakaShrot.Client.*]`: keys, `Node`, `Allow*` | `[roles.*]`, the node and roles from the certificate |
| `[BakaShrot.Peer.*]`: `Host`, `Port`, `Node`, `PublicKey`, topic and node lists | `[replication] push_to` and `accept_from` (addresses), the lists in the peer's roles |
| `ReplicatingTopics` | each peer role's `send_topics` / `accept_topics` |
| topic and node ids, pins, ranges, `[BakaShrot.Nodes]` | names only: ids are the database's |
| `DefaultTopic` | dropped: a push always names its topic |
| `Host` + `Port` | `listen` |
| seconds | durations (`"10s"`) |
| `NetThreadPoolSize` | gone (tokio) |
| `UploadTimeout` | gone: attachments are streamed in their push (protocol 7), nothing waits |
| `FULL` / `NORMAL` | `"full"` / `"normal"`; `off` still refused |
## Tests
`crates/bakashrot-config/tests/config.rs`: the example, the defaults,
every setting, retention's order, permissions (several roles, none known,
replication per role), the topic lists that are gone, paths from the config's
folder, and the refusals, each with its line: TOML errors, unknown keys
(the old ini's too), every value that's wrong, names twice (without case),
folder, and the refusals, each with its line: TOML errors, unknown keys,
every value that's wrong, names twice (without case),
unquoted dotted names.
+14 -21
View File
@@ -1,8 +1,7 @@
# Known issues and ideas
What's still true of the PureBasic version's notes, for this one. That
version was removed on 2026-11-15; it's in git history up to commit
`68966a8` (`OldSource/Notes.md`, "Open items"; `OldSource/Design/`).
What's known to be wrong or missing, what's left as it is and why, and
ideas for later.
## Threat model
@@ -36,19 +35,27 @@ network**, not exposed to the internet. So:
- **The attachments a server lacks:** it reports the 256 oldest at a
time (`SyncState`). With several peers, ones none of them has can hold
back newer ones.
- **Peers are trusted with what they replicate** (old item 12): a server
- **Peers are trusted with what they replicate:** a server
in `accept_from` can send any message its roles accept, for any node.
Messages carry no proof of who first wrote them. Fix: sign each message
with its node's key when first stored, check it on replication (every
server then needs every node's public key).
- **Private keys sit in PEM files**, readable by whoever can read them: on
trusted machines, file permissions are enough.
- **One connection per process in the DLL** (old item 10, `revamp.md`,
- **One connection per process in the DLL** (`revamp.md`,
"Open questions"): a handle-based API for several. Polling rather than
pushed subscriptions; nothing reconnects by itself, on purpose.
- **At least once:** a push whose answer was lost (the server stopped
between committing and answering) is stored twice if retried. A client
that minds reads the latest message before retrying.
- **1,048,575 topic and node pairs per database** (2^20 - 1,
`MAX_STREAMS`, `bakashrot-storage/src/lib.rs`), for all topics
together, and 2^43 - 1 message ids per pair (`MAX_MESSAGE_ID`): the
20 and 43 bits of a message's packed key (`storage.md`, "Keys"). Past
the first, a new pair is refused from anyone, pushed or replicated;
pairs aren't freed when their messages are deleted. Enough for now
(the owner, 2026-11-16). Raising it means another key layout, so a
new schema version.
## Found by reading the code, not reproduced
@@ -73,20 +80,7 @@ Reproduce one before fixing it, and move it up (or drop it) once known.
the example config says so by `max_attachment_size` (1 GiB by
default). Fix if needed: a wait scaled to the parts already received,
or the hash's state kept with the attachment.
- **Two peers sending the same attachment at once.** Both can be
answered `Continue` for the same `from_part` (the check and the first
part written aren't one step). Their parts then interleave in the
writer's queue: each part is stored once, from whichever came first,
and each sender has one refused (`InvalidRequest`). With few parts (4
or fewer in flight, `PARTS_IN_FLIGHT`), all of them can be stored
while both senders were refused: neither sends `FinishReplicated`,
and the attachment stays `replicating` with `received = size`, asked
for and refused at every check-in until a restart (`clean_up` starts
it over). Needs two parts at least and a narrow timing: servers that
take from two others having the same attachment. Fix: finish it
whenever the last part is stored, or one receiver at a time per
attachment.
- **`SyncState` in one packet** (old item 9). Every head the peer may
- **`SyncState` in one packet.** Every head the peer may
send goes in one frame of 16 MiB at most: with very many topic and
node pairs (hundreds of thousands) it doesn't fit, the packet can't
be sent and replication stops. Fix: heads in several packets, or only
@@ -144,8 +138,7 @@ Reproduce one before fixing it, and move it up (or drop it) once known.
## Ideas for later
From the old benchmarks and suggestions (`OldSource/Design/Suggestions.md`),
worth it only when a use calls for them:
Worth it only when a use calls for them:
- **Several pushes in flight per connection.** One request at a time
(`protocol.md`, "Rules") caps a node at one push per round trip:
+1 -2
View File
@@ -6,8 +6,7 @@ machine, 2026-11-15, Windows): the outputs shown are what came back.
Get a release (CI's artifacts, or `release.ps1` / `python release.py`):
the programs, `bakashrot.dll` / `libbakashrot.so`, the wheel, the
example configs. Certificates made before 0.3.0 had a placeholder
identity OID (`oid.md`) and aren't read any more: issue them again.
example configs.
| Machine | What runs there | Files it gets |
|---|---|---|
+1 -13
View File
@@ -1,7 +1,6 @@
# The identity extension's OID
`IDENTITY_OID = 1.3.6.1.4.1.67681.1.1` (`crates/bakashrot-pki/src/lib.rs`),
since 0.3.0 (2026-11-16).
`IDENTITY_OID = 1.3.6.1.4.1.67681.1.1` (`crates/bakashrot-pki/src/lib.rs`).
## The branches
@@ -29,17 +28,6 @@ Within a deployment, only the CA's certificates are read, so another
extension using the same OID somewhere else can't confuse a server. The OID
mostly needs to be stable, and properly ours.
## Before 0.3.0: a placeholder
Up to 0.2.0 the OID was `1.3.6.1.4.1.32473.1.1`, under PEN 32473, "Example
Enterprise Number for Documentation Use" (RFC 5612), and `bakashrot-cert`
warned whenever it made a certificate. **Those certificates aren't read
any more** ("no BakaShrot identity"): make the CA's again or keep it (a
CA has no identity extension), issue every server's and node's again.
`OldSource/`'s UUID arc (`2.25.2320...0981`) couldn't be used: `rcgen`
writes OID parts of 64 bits at most, a UUID is 128.
## Changing the registration
The registry lists the organization, the contact's name and the contact's
+4 -36
View File
@@ -1,10 +1,7 @@
# Protocol 7
What goes over a connection between a client and a server, or two servers.
The code is `crates/bakashrot-proto`; this describes it. Protocol 6, the
PureBasic one, is `OldSource/Design/Packets.md`: what the requests do is
mostly the same, the transport, the encoding and the packets themselves
were simplified (see the end).
The code is `crates/bakashrot-proto`; this describes it.
**Status (2026-11-13):** packets, frames, names and error codes are written
and tested (`bakashrot-proto`), so is the TLS setup (`bakashrot-pki`,
@@ -123,6 +120,7 @@ An attachment C: GetAttachment(from_part) S: Attachment, then Attachme
Replicating P: SyncStateRequest S: SyncState
P: ReplicateMessages x the batches S: MessagePushed each / RequestRefused
P: ReplicateAttachment x missing S: Continue / RequestRefused
(NotReady: another peer sending it)
P: AttachmentPart x from next_part S: MessagePushed / RequestRefused
```
@@ -141,8 +139,6 @@ Replicating P: SyncStateRequest S: SyncState
## Rules
The same as protocol 6:
- **One request at a time:** a client sends one, waits for its answer (the
parts of an attachment being part of its request or answer).
- **Refuse or abort:** valid but not allowed or not found (permissions,
@@ -152,33 +148,5 @@ The same as protocol 6:
to answers.
- **The identity is the connection's:** requests never say who sends them.
A push picks its topic, never its node.
- **Error codes** (`error`): 1-99 refusals, 100+ the client's own; the
numbers of protocol 6, never renumbered.
## What changed from protocol 6
- **TLS** instead of the sealed Hello, the signed key exchange and the own
cipher: the handshake packets are gone, `Welcome` is new, the identity
comes from the certificate.
- **The version is checked once**, sent by both ends before anything else:
packets lost their magic and version.
- **Names only:** no ids on the wire, no refs. `ResolveName` /
`NameResolved` are gone, answers don't echo ids, `NamesList` is names,
`SyncState` gives its heads by name.
- **No default topic:** a push always names its topic, `Welcome` doesn't
give one (`NoDefaultTopic`, 6, is never sent).
- **Fewer packets, 26 → 18:** `GetMessages` with `after: nil` replaces
`GetLatestMessage`, `Messages` replaces `LatestMessage`; `SyncState`
lists the missing attachments (no `MissingAttachmentsRequest` /
`MissingAttachments`); answers don't echo what was asked.
- **Attachments are streamed in their request:** no upload ids, no uploads
waiting for their push (and no `UploadTimeout`), no request or answer per
part (`UploadStart`, `UploadStarted`, `UploadChunk`, `ChunkStored`,
`AttachmentChunk`, `ReplicateAttachmentChunk` are gone). An upload cut
short starts over (none resumed in protocol 6 either); reads and
replication go on `from_part`.
- **Encoding:** derived with `serde`, `[type, [fields...]]`, booleans and
nil where they belong (were 0/1 and empty values). Nothing may follow a
packet (was ignored).
- **No 4 KB limit before authentication:** TLS has authenticated both ends
before any frame.
- **Error codes** (`error`): 1-99 refusals, 100+ the client's own, never
renumbered.
+54 -57
View File
@@ -26,10 +26,9 @@ Decided 2026-11-14 (`../revamp.md`, "Operations"):
answered is stored, the ones after are refused (`ServerError`, "the
server is stopping"), then it exits with 0. Nothing else to wait for:
a client whose push wasn't answered pushes it again (at least once).
- **No console commands** (the old `quit`, `restart`, `vacuum`, `names`,
`help`): it needs no console, a service manager runs it as is. `names`
is `bakashrot list`, `restart` a stop and a start. Maybe an admin
channel later.
- **No console commands:** it needs no console, a service manager runs
it as is. Listing names is `bakashrot list`, restarting a stop and a
start. Maybe an admin channel later.
- **Logs go to the console** (stdout, `tracing`), without colours when
it isn't a terminal: redirect them, or let systemd or Docker keep
them. `BAKASHROT_LOG` sets the level: `error`, `warn`, `info` (the
@@ -128,7 +127,7 @@ otherwise:
part it needs. One it says doesn't match its SHA-256 is read back
here: damaged (a disk, a bug), this server's copy is dropped and asked
for again from its own peers (`replicating`, not served meanwhile),
instead of sent again at every check-in (the old open item 3).
instead of sent again at every check-in.
**Receiving** (`accept_from.rs`): replication requests are answered only
if the connection comes from `accept_from` (a hostname looked up at its
@@ -142,8 +141,15 @@ needs, the parts it had read back for the SHA-256; matching, it's ready,
otherwise started over (`DataMismatch`). A stop between its last part
and the check: started over at the next start.
**One server per node** stays the config's rule, as in the old version
(`known-issues.md`): a node pushing to two servers that replicate makes
- **One peer at a time per attachment** (2026-11-16): another asking for
the same one meanwhile is refused (`NotReady`) and offers it again at
its next check-in. Two at once could both be refused a part while
every part got stored, the attachment left whole and never checked.
- **Never taken unchecked:** one whole but not checked (finishing it
failed) is listed in `SyncState` from its first part and started over
when a peer asks for it, warned in the log.
**One server per node** stays the config's rule (`known-issues.md`): a node pushing to two servers that replicate makes
the same ids with different messages, undetected.
## A connection
@@ -256,8 +262,8 @@ each, 200-byte JSON, 20,000 pushes per client.
a connection's revocation check stopped reading its certificate at
every request (its serial is read once, at connect).
- One client waits for each answer: a round trip and a commit per push.
Through Python and the DLL, about 10,700 pushes/s (the old module did
about 9,000), and 140,000 messages/s read.
Through Python and the DLL, about 10,700 pushes/s, and 140,000
messages/s read.
- With `full`, every group commit waits for the disk's flush (about 1 ms
here): 10 clients make groups of about 10. More clients, bigger groups.
- **Attachments** (release, the tool, `normal`): 200 MB up in 1.3 s and
@@ -267,52 +273,47 @@ each, 200-byte JSON, 20,000 pushes per client.
against 39,800-40,400), `full` the same (the disk's), attachments and
tool commands the same. The programs and the DLL 25-31% smaller.
### Against the old server (2026-11-14)
### Through the DLL, Python and the tools (2026-11-14)
The same work on the same machine, release builds, one server on
localhost: the old one (`OldSource/Build/Bench/BenchServer.exe`, its DLL,
`bakashrot.py` and tools) and this one. Pushes and reads through each one's
Python module and DLL (10 clients: 10 processes, the same node), 200-byte
Release builds, one server on localhost. Pushes and reads through
Python and the DLL (10 clients: 10 processes, the same node), 200-byte
JSON; the rest through the tools.
| | Old, `full` | New, `full` | Old, `normal` | New, `normal` |
|---|---|---|---|---|
| One tool command, connecting included | 8 ms | 12 ms | 8 ms | 11 ms |
| Pushes, 1 client | 1,945/s | 1,954/s | 9,386/s | 11,600-13,100/s |
| Pushes, 10 clients | 9,006/s | 10,072/s | 25,419/s | 56,200-64,300/s |
| Reads, Python | 76,466/s | 130,861/s | 77,526/s | 146,368/s |
| The tool writing 120,000 messages to a file | | | 45.5 s | 0.14 s |
| A 200 MB attachment up, the tool | 3.49 s | 1.15 s | 2.84 s | 1.15 s |
| The same down | 1.57 s | 0.59 s | 1.62 s | 0.67 s |
| | `full` | `normal` |
|---|---|---|
| One tool command, connecting included | 12 ms | 11 ms |
| Pushes, 1 client | 1,954/s | 11,600-13,100/s |
| Pushes, 10 clients | 10,072/s | 56,200-64,300/s |
| Reads, Python | 130,861/s | 146,368/s |
| The tool writing 120,000 messages to a file | | 0.14 s |
| A 200 MB attachment up, the tool | 1.15 s | 1.15 s |
| The same down | 0.59 s | 0.67 s |
- **`full`, one client:** both wait for the disk's flush at every push.
- **`full`, one client:** it waits for the disk's flush at every push.
- **A command:** the TLS handshake's P-384 signatures and checks, both
ends, cost about 7 ms against the old handshake's X25519; aws-lc's CPU
Jitter Entropy seeding cost 25 ms more per process before it was turned
off (`certificates.md`, "TLS").
- **The old tool's read** printed a line at a time to the console API.
ends, cost about 7 ms; aws-lc's CPU Jitter Entropy seeding cost 25 ms
more per process before it was turned off (`certificates.md`, "TLS").
### Replication, against the old server (2026-11-14)
### Replication (2026-11-14)
`replication_rate` (`tests/end_to_end.rs`, ignored by default) against
the old `Tests/Bench/Bench-Replication.ps1 -Parts Ramp -Ramp 10x0`: 4
`replication_rate` (`tests/end_to_end.rs`, ignored by default): 4
servers in a Y (a hub sending to 3 leaves, each sending to the hub), 10
nodes pushing to each as fast as they're answered for 30 s, 200-byte
JSON, the same machine. The old one's pushers are its PureBasic bench
client, the new one's `bakashrot-client` threads in the test's process.
JSON, the same machine, the pushers `bakashrot-client` threads in the
test's process.
| | Old, `full` | New, `full` | Old, `normal` | New, `normal` |
|---|---|---|---|---|
| Pushes, all 4 servers | 16,638/s | 16,000-16,300/s | 33,895/s | 35,300-37,500/s |
| Slowest push | 134 ms | 50-57 ms | 854 ms | 420-510 ms |
| Behind when the pushes stop (most of a server) | 9.0 s of pushes | 0 | 11.4 s | 0.3-0.9 s |
| Until every server has everything | 8.8 s | 0.3 s | 14.8 s | 5.3-5.6 s |
| | `full` | `normal` |
|---|---|---|
| Pushes, all 4 servers | 16,000-16,300/s | 35,300-37,500/s |
| Slowest push | 50-57 ms | 420-510 ms |
| Behind when the pushes stop (most of a server) | 0 | 0.3-0.9 s |
| Until every server has everything | 0.3 s | 5.3-5.6 s |
- **Keeping up:** a link checks in again at once after sending; before
it waited `sync_interval` after every check-in, as the old one did, and
it waited `sync_interval` after every check-in, and
pushes went faster (19,300/s `full`, 40,200/s `normal`) while the
leaves fell 7-9 s behind and took 10-20 s to catch up.
- **`batch_size` 1000** (was 100, the old `PeerBatchSize`): `normal` went
- **`batch_size` 1000** (was 100): `normal` went
from 27,500-31,500/s to 35,300-37,500/s, and from 2-2.5 s to under 1 s
behind. Batches stay about 1 MiB at most.
- **The last few seconds** with `normal` are a link's `sync_interval`:
@@ -324,25 +325,21 @@ client, the new one's `bakashrot-client` threads in the test's process.
each:
- pushes and reads back (ids, data, times, another connection);
- runs of messages (`OldSource/Tests/GetMessages-Test.ps1`, the server's
part): from the start, after an id, with a limit, `more`, the last ones,
- runs of messages: from the start, after an id, with a limit, `more`, the last ones,
nothing yet, unknown topics and nodes, names without case, data that
isn't one line of JSON;
- big messages (`BigMessages-Test.ps1`, one server's part): five of 600 KB
and one of the largest size, one per answer, read back whole; one byte
- big messages: five of 600 KB and one of the largest size, one per answer, read back whole; one byte
over refused; a query result over 16 MB refused;
- names: topics by role, nodes;
- attachments (`Attachments-Test.ps1`, one server's part): 100 KB, 5 MB +
123 bytes, exactly 2 MB, 1 byte, 1 MB, read back and saved; the
- attachments: 100 KB, 5 MB + 123 bytes, exactly 2 MB, 1 byte, 1 MB, read back and saved; the
messages saying theirs; none, or no such message, refused leaving no
file; another reader, one not allowed; over the server's limit, 0 for
none, empty; the parts stored;
- attachment edges (`AttachmentEdges-Test.ps1`'s, through the client and
a raw connection): a source changed between its SHA-256 and its
- attachment edges (through the client and a raw connection): a source changed between its SHA-256 and its
sending, one failing half-way (the connection closed, the upload
dropped), a part of the wrong size, a wrong SHA-256, reading from a
part on, something else than a part (closed, dropped);
- queries (`BigMessages-Test.ps1`'s): not allowed, results, a non-JSON row
- queries: not allowed, results, a non-JSON row
refused with the hint, errors, writes and two statements refused;
- refusals keeping the connection (not allowed, unknown topic, a bad name
the client catches);
@@ -355,12 +352,9 @@ each:
- stopping cleanly: eight nodes pushing flat out, the server stopped
under them: every push answered is there after a restart, the ones
after refused as `ServerError`;
- retention (`Retention-Test.ps1`'s local part): a topic's messages and
their attachment deleted past its age, another topic kept forever, ids
- retention: a topic's messages and their attachment deleted past its age, another topic kept forever, ids
going on, a younger message kept through a pass;
- replication (`Names-Test.ps1`'s replication, `Restart-Test.ps1`,
`AttachmentEdges-Test.ps1`'s corruption): three servers of one CA, a
pushing to b and c. b gets every message by name with its id, time
- replication: three servers of one CA, a pushing to b and c. b gets every message by name with its id, time
and data, and the attachment; c, whose `accept_from` hasn't a, gets
nothing; b's own pushes don't go to a. b stopped, a pushes meanwhile
and its copy of an attachment gets damaged; b started again on its
@@ -372,8 +366,11 @@ each:
first part going on from the second (the first read back for the
SHA-256), one not matching refused and asked for from the start, a
certificate whose roles take nothing refused;
- two peers, one attachment (two raw connections): the second refused
while the first sends it (names in any case), the first finishing it;
one gone partway, the other going on from there; one whole but never
checked asked for from its first part and started over;
- `replication_rate`, ignored by default: the benchmark above (`SYNC`,
`NODES`, `SECONDS`, `BATCH`).
The old tests' tool parts (`--follow`, the output) are the tools' tests
(`client.md`).
The tool's own (`--follow`, the output): `client.md`.
+40 -75
View File
@@ -5,9 +5,6 @@ The server's database: one SQLite file. The code is
`crates/bakashrot-storage/src/schema_v9.sql`, made in one go. Decided in
`../revamp.md`, "Decided" (2026-11-13).
**Not compatible with v8:** a database of the old server isn't opened
(refused at start, saying so); its data isn't carried over.
## In short
- **By name outside, packed integers inside.** The rest of the code (the
@@ -17,7 +14,7 @@ The server's database: one SQLite file. The code is
and unpacks keys; the `messages` view shows plain columns.
- **One B-tree per kind of data:** messages, attachment parts. A push
writes its message's row and nothing else (no index): about 2.3 times
the pushes of v8's layout, 30% less disk (see "Measured").
the pushes of a layout with an index, 30% less disk (see "Measured").
- **One writer, many readers** (WAL). Writes go in batches, one
transaction each: a group commit.
@@ -65,8 +62,8 @@ attachment_parts.key = attachment_id << 20 | part
columns**: computed when read, never stored (a test checks the schema
unpacks keys as the code does).
Why not v8's three columns with an index? A second B-tree to write at every
push, and reads going through the index then to the row. Measured below.
Why no index? A second B-tree to write at every push, and reads going
through the index then to the row. Measured below.
## Writing
@@ -85,7 +82,6 @@ together, one disk flush for all.
| `damaged(id)` | a ready attachment found not matching its SHA-256: its parts dropped, asked for again (`replicating`) |
| `delete_before(topic, cutoff, limit)` | retention |
| `purge_attachments(max_parts)` | deleted messages' attachments |
| `Writer::vacuum()` (outside a batch) | the file shrunk to what it holds: the old server's `vacuum` command |
| `Checkpointer::checkpoint()` (its own connection) | the WAL copied into the database, every second, off the writer's way |
**SHA-256s are the caller's to check:** the server hashes the parts as they
@@ -108,8 +104,7 @@ read while the writer writes. Answers are the protocol's own types:
| `attachment(topic, node, id)`, `read_part(id, part)` | an attachment, its parts | the message's key; a part's key |
A topic and node with no messages read as none, unless the node has none
in any topic: `Error::UnknownName`, as the old server refused unknown
nodes. A topic is never unknown: it's allowed or not, by the roles.
in any topic: `Error::UnknownName`. A topic is never unknown: it's allowed or not, by the roles.
## Attachments
@@ -148,7 +143,7 @@ connection's next queries (found 2026-11-15). The result is
`{"columns":[...],"rows":[[...]]}`, every value a string or null (blobs in
hex), 10,000 rows and 16 MB at most (`TooBig`). A failing row fails it all,
with SQLite's message: never a partial result. "malformed JSON" (a JSON
function on a row that isn't) gets the old server's hint: add
function on a row that isn't) gets a hint: add
`is_json = 1` to the WHERE.
The `messages` view, by name:
@@ -156,7 +151,7 @@ The `messages` view, by name:
| Column | |
|---|---|
| `topic`, `node` | names |
| `message_id`, `message_data`, `is_json`, `pushed_at`, `pushed_utc` | as in v8 |
| `message_id`, `message_data`, `is_json`, `pushed_at`, `pushed_utc` | the message's |
| `attachment_size`, `attachment_state` | NULL without one |
```sql
@@ -170,7 +165,7 @@ SELECT message_data ->> '$.temp' FROM messages WHERE topic = 'sensors' AND is_js
whole: a millisecond or two for 10,000 messages per stream, growing with
them (see "Measured"). If that gets slow, a time index is one migration
away ("For later").
- `browse` / `browse_messages`: v8's, for database tools (set `browse`'s
- `browse` / `browse_messages`: for database tools (set `browse`'s
one row, open the view); no dropdowns any more.
## The file
@@ -181,7 +176,7 @@ SELECT message_data ->> '$.temp' FROM messages WHERE topic = 'sensors' AND is_js
- **Opening** (`Database::open`): an empty file is made a v9 database; a
v9 one is opened; an older one is migrated, a version at a time, each
step one transaction that also sets `schema_version` (none yet); a newer
one, the old server's, or another program's is refused, saying which.
one or another program's is refused, saying which.
- Every connection: WAL, `synchronous` from the config (`full`: every
commit flushed; `normal`: about once a second), a 5 s busy timeout. No
foreign keys: one writer keeps the tables consistent, `CHECK`s keep the
@@ -195,75 +190,58 @@ Python's `sqlite3` (SQLite 3.50), 5,000,000 small JSON messages pushed to
500 streams (100 topics x 5 nodes) at random, 100 per transaction, WAL,
`synchronous = NORMAL`, 16 MB page cache:
| | v9 (this) | v9 + a time index | the first draft (v8's layout) |
|---|---|---|---|
| Pushes/s | **23k** | 9-10k | 9k |
| File | **330 MB** | 457 MB | 484 MB |
| Latest 100 of a stream | 0.05 ms | 0.05 ms | 0.19 ms |
| 1000 after an id | 0.53 ms | 0.53 ms | 3.04 ms |
| Retention, 10% of a topic | 5.5 ms | 6.4 ms | 241 ms |
| Query: a node's last hour | 1.65 ms | 0.90 ms | 3.29 ms |
| Query: a topic's last hour | 8.4 ms | 4.7 ms | 16.0 ms |
| | v9 (this) | v9 + a time index |
|---|---|---|
| Pushes/s | **23k** | 9-10k |
| File | **330 MB** | 457 MB |
| Latest 100 of a stream | 0.05 ms | 0.05 ms |
| 1000 after an id | 0.53 ms | 0.53 ms |
| Retention, 10% of a topic | 5.5 ms | 6.4 ms |
| Query: a node's last hour | 1.65 ms | 0.90 ms |
| Query: a topic's last hour | 8.4 ms | 4.7 ms |
- A push dirties its stream's page: with pushes spread over many streams,
each costs about a page written to the WAL. Any index per stream (v8's,
a time index) doubles that, and a bigger cache doesn't help (it's writes,
each costs about a page written to the WAL. Any index per stream (a
time index) doubles that, and a bigger cache doesn't help (it's writes,
not reads).
- Bigger batches over fewer streams share pages: the server's group commit
helps most when many pushes come at once.
### Against the old storage (2026-11-13)
### The benchmark (2026-11-13)
The same workload on both, each storage layer alone (no network): pushes
over 500 streams picked at random, 100 per transaction, 200-byte JSON;
2000 latest-message reads; 500 reads of 1000 messages after a stream's
middle id; retention of 10% of one topic. The new side is
`compare_with_old` (`tests/storage.rs`, ignored by default:
`storage_rate` (`tests/storage.rs`, ignored by default:
`PUSHES=1000000 SYNC=normal cargo test --release -p bakashrot-storage --
--ignored --nocapture`, or `bench.ps1 -Only storage`). The old side
calls `OldSource`'s writer (`BakaShrot_Writer_WriteJobs()`, its group
commit, PureBasic 5.73, SQLite
3.54.1 from `sqlite3.dll`), from a program built outside `OldSource`.
Same machine, NVMe, one run each.
--ignored --nocapture`, or `bench.ps1 -Only storage`): the storage layer
alone (no network), pushes over 500 streams picked at random, 100 per
transaction, 200-byte JSON; 2000 latest-message reads; 500 reads of 1000
messages after a stream's middle id; retention of 10% of one topic. NVMe,
one run each.
| | Old, 1M | New, 1M | Old, 1M `full` | New, 1M `full` | Old, 5M | New, 5M |
|---|---|---|---|---|---|---|
| Pushes/s, first | **25,874** | 13,803 | **19,976** | 13,205 | **19,558** | 12,184 |
| Pushes/s, step 5's writer | 25,874 | **37,332** | 19,976 | **29,952** | 19,558 | **29,363** |
| File | 276 MB | 249 MB | 276 MB | 249 MB | 1,380 MB | 1,239 MB |
| Latest message | 0.013 ms | 0.017 ms | 0.013 ms | 0.017 ms | 0.014 ms | 0.017 ms |
| 1000 after an id | 4.26 ms | **0.43 ms** | 4.28 ms | **0.43 ms** | 4.32 ms | **0.45 ms** |
| Retention, 10% of a topic | 20 ms | **7 ms** | 23 ms | **9 ms** | 98 ms | **8 ms** |
| | 1M | 1M `full` | 5M |
|---|---|---|---|
| Pushes/s | 37,332 | 29,952 | 29,363 |
| File | 249 MB | 249 MB | 1,239 MB |
| Latest message | 0.017 ms | 0.017 ms | 0.017 ms |
| 1000 after an id | 0.43 ms | 0.43 ms | 0.45 ms |
| Retention, 10% of a topic | 7 ms | 9 ms | 8 ms |
`normal` unless said. At first, the old writer pushed faster, from code the
new one didn't have, not from the schema; step 5 brought two of them over
(the second row, about 1.5 times the old):
- **checkpoints off the push path:** `Database::checkpointer()`, every
second on its own connection (the server's thread), the writer only
checkpointing itself past 10,000 pages (`WRITER_CHECKPOINT_PAGES`,
SQLite's default is 1,000): 13.8k to 34.7k pushes/s;
- **no savepoint around a push** (a failed one leaves at most its empty
stream): 37.3k;
- **next ids in memory**, left out: an indexed read of the stream's row per
push, not where the time goes.
The new layout reads ranges 10 times faster, deletes faster (and keeps
doing so as the database grows), loses less push speed from 1M to 5M (-12%
against -24%), and makes a file 10% smaller with 200-byte messages (30%
with 45-byte ones: its saving is the indexes, a fixed cost per message).
`normal` unless said. Most of the push speed is the writer's: checkpoints
off the push path (`Database::checkpointer()`, every second on its own
connection, the writer only checkpointing itself past 10,000 pages,
`WRITER_CHECKPOINT_PAGES`), and no savepoint around a push (a failed one
leaves at most its empty stream).
## Tests
`crates/bakashrot-storage/tests/storage.rs`, on database files in
`target/`: opening (made, reopened, an empty file), refused databases
(another program's, v8, a newer version, no version), pushes and reads
(another program's, a newer version, no version), pushes and reads
(ids per stream, names without case, after/latest/limit/more, the 1 MiB
cut), uploads (parts in order and of their size, incomplete refused, a
failed operation not spoiling its batch, cancelled and left-over ones
deleted), replication (own ids, kept ones, attachments to ask for, too big,
started over, resumed), retention (limits, id order, heads kept, ids going
on, attachments purged in batches), vacuum (the file shrinks, ids go on),
on, attachments purged in batches),
the limits (no stream, no id left, the highest key), queries (results, every write refused, failing rows, too
big), the view's plans, and readers with two writers at once. In
`src/lib.rs`: the keys, the schema unpacking them the same, the states.
@@ -277,16 +255,3 @@ big), the view's plans, and readers with two writers at once. In
SQLite's interrupt.
- **Attachments as files** next to the database instead of in it, if their
size calls for it.
## What changed from v8
| v8 | v9 |
|---|---|
| `data(topic_id, node_id, message_id, ...)` + 3 indexes | `data(key, pushed_at, message_data)`, no index |
| `topics`, `nodes` with ids and origins | names in `streams` |
| `node_heads` | `streams.last_id` |
| `attachments` + `blobs` (two sets of states), `blob_chunks` | `attachments` (five states), `attachment_parts` |
| uploads waiting for their message, `UploadTimeout` | uploads inside their push |
| `is_json` stored | computed when read |
| no version in the file | `metadata` with `schema_version`; `application_id` |
| foreign keys declared, not checked | none, `CHECK`s |