~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

A group's commits are made under a lock of its own

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/group-write-locks
crates/group-host/src/lib.rs+2−2
@@ -81,8 +81,8 @@ pub struct Host {
pub identity: identity::Resolver,
/// Each commit's revision, later than the last.
pub clock: atproto::tid::TidClock,
/// Held while a commit is made, so each builds on the one before.
writing: std::sync::Mutex<()>,
/// Held while a commit is made, one lock for each group's commits.
writing: repo::Writing,
/// Told of each new event, which wakes the firehose's streams.
pub events: tokio::sync::broadcast::Sender<()>,
/// When the relays were last told of news.
crates/group-host/src/repo.rs+31−1
@@ -93,6 +93,36 @@ pub struct Asked<'a> {
pub copied: bool,
}
/// How many locks the groups' commits are spread over.
const WRITING_LOCKS: usize = 64;
/// The locks commits are made under, a group's always the same one. A
/// group's commits, to its public repo and its repos in spaces, are made
/// one at a time, so each builds on the one before, and what a write asks
/// of the group as it is (its roles) holds until it is made; other
/// groups' are made beside them. The groups share a fixed number of locks,
/// rather than one each, so the locks never grow: two groups on one only
/// wait for each other.
pub(crate) struct Writing([std::sync::Mutex<()>; WRITING_LOCKS]);
impl Default for Writing {
fn default() -> Writing {
Writing(std::array::from_fn(|_| Default::default()))
}
}
impl Writing {
/// Held while a commit of `did`'s is made: its DID, never its handle,
/// so that one group is always the one lock.
pub(crate) fn of(&self, did: &str) -> std::sync::MutexGuard<'_, ()> {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
did.hash(&mut hasher);
let lock = &self.0[hasher.finish() as usize % WRITING_LOCKS];
lock.lock().expect("not poisoned")
}
}
fn invalid_swap(message: impl Into<String>) -> XrpcError {
XrpcError::new(StatusCode::BAD_REQUEST, "InvalidSwap", message)
}
@@ -305,7 +335,6 @@ impl Host {
writes: &[Write],
asked: &Asked,
) -> Result<Option<(Committed, Vec<Write>)>, XrpcError> {
let _one_at_a_time = self.writing.lock().expect("not poisoned");
let account = self.store.account(did).map_err(XrpcError::internal)?;
let account = account.ok_or_else(|| {
XrpcError::new(
@@ -314,6 +343,7 @@ impl Host {
format!("no repo {did} here"),
)
})?;
let _one_at_a_time = self.writing.of(&account.did);
if !asked.off {
self.active(&account.did)?;
}
crates/group-host/src/space.rs+1−1
@@ -85,7 +85,6 @@ impl Host {
writes: &[Write],
asked: &Asked,
) -> Result<Option<(Commit, Vec<Write>)>, XrpcError> {
let _one_at_a_time = self.writing.lock().expect("not poisoned");
let account = self.store.account(did).map_err(XrpcError::internal)?;
let account = account.ok_or_else(|| {
XrpcError::new(
@@ -94,6 +93,7 @@ impl Host {
format!("no repo {did} here"),
)
})?;
let _one_at_a_time = self.writing.of(&account.did);
if !asked.off {
self.active(&account.did)?;
}
crates/group-host/tests/repos.rs+26−0
@@ -271,6 +271,32 @@ async fn writes_records_as_signed_commits() {
);
}
#[tokio::test]
async fn makes_a_groups_writes_one_at_a_time_and_others_beside_them() {
let h = start(&[], false).await;
let riders = h.host.create_account(RIDERS).await.unwrap();
let walkers = h.host.create_account("walkers.groups.test").await.unwrap();
// Each group written from many threads at once, riders by its DID and
// by its handle alike: every write is made, none refused for a head
// another moved under it.
let names = [riders.as_str(), RIDERS, walkers.as_str()];
std::thread::scope(|s| {
for (n, name) in names.iter().enumerate() {
for t in 0..8 {
let host = &h.host;
s.spawn(move || {
for i in 0..5 {
let rkey = format!("n{n}t{t}i{i}");
host.write(name, &[create(&rkey, "Ride safe")]).unwrap();
}
});
}
}
});
assert_eq!(h.host.store.paths(&riders).unwrap().len(), 80);
assert_eq!(h.host.store.paths(&walkers).unwrap().len(), 40);
}
#[tokio::test]
async fn refuses_writes_it_cannot_make() {
let h = start(&[], false).await;
docs/design/signing-in.md+5−3
@@ -250,9 +250,11 @@ and then what the records' rules ask ([membership](membership.md)); in
someone's inbox,
`invite` ([membership](membership.md)); in a space for chosen people, to
be one of them and hold `repo:*`; and otherwise what the space's access
record grants. Both are asked under the lock the commit is made under,
so no change of roles slips in between, but for an invite, whose checks
are made just before its write. An upload is
record grants. Both are asked under the group's write lock, which its
commits to its public repo and its repos in spaces are all made under,
one at a time, so no change of roles slips in between, but for an
invite, whose checks are made just before its write. Other groups'
commits are made beside them. An upload is
asked the same way: the scope must take the file's type, and the roles
must still let the member write something as the group.