Skip to content

Commit e5aa4a2

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
feat(git): move repo-name registry to Postgres + relax RWM chart gate (HA relay) (#1432)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent b32db5e commit e5aa4a2

17 files changed

Lines changed: 727 additions & 156 deletions

File tree

‎crates/buzz-db/src/git_repo.rs‎

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
//! Git repository name registry (NIP-34 kind:30617).
2+
//!
3+
//! The relay holds no persistent per-repo filesystem state: git reads and
4+
//! writes hydrate an ephemeral bare repo from object storage per request, and
5+
//! writer serialization is the object-store pointer CAS (see
6+
//! `docs/git-on-object-storage.md`, `Inv_NoFork`). Repo-*name* uniqueness is
7+
//! the one remaining shared-state need, and it lives here — in Postgres, not on
8+
//! local disk — so the relay is stateless and can run multiple replicas without
9+
//! a ReadWriteMany volume.
10+
//!
11+
//! Names are unique **within a community**: the primary key is
12+
//! `(community_id, repo_id)`, matching the multi-tenant invariant that every
13+
//! tenant-scoped key leads with `community_id`. The PK enforces uniqueness
14+
//! atomically via `INSERT … ON CONFLICT DO NOTHING`, which replaces the old
15+
//! filesystem `create_dir` race guard. `owner_pubkey` distinguishes an
16+
//! idempotent re-announce (same owner) from a collision (different owner), and
17+
//! backs the per-pubkey quota via `COUNT`.
18+
19+
use sqlx::{PgPool, Row as _};
20+
21+
use crate::error::Result;
22+
use crate::CommunityId;
23+
24+
/// Outcome of a name-reservation attempt.
25+
///
26+
/// The caller (kind:30617 handler) uses this to decide whether to seed the
27+
/// manifest pointer and, on seed failure, whether to release the reservation:
28+
/// only a `Reserved` (freshly inserted) row should be rolled back — an
29+
/// `AlreadyOwned` re-announce must leave the pre-existing reservation intact.
30+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31+
pub enum ReserveOutcome {
32+
/// The name was newly claimed by this owner (a fresh row was inserted).
33+
Reserved,
34+
/// The name was already reserved by this same owner — idempotent
35+
/// re-announce, a no-op update. No row was inserted; the quota was not
36+
/// re-checked (re-announcing an already-owned name never grows the count).
37+
AlreadyOwned,
38+
/// The name is held by a *different* owner — a collision. No row was
39+
/// inserted.
40+
TakenByOther,
41+
}
42+
43+
/// Return the current owner pubkey of `repo_id` in `community`, or `None` if
44+
/// the name is unreserved. Used to classify an announce (same-owner
45+
/// re-announce vs cross-owner collision) and to gate the quota check before a
46+
/// fresh claim.
47+
pub async fn repo_name_owner(
48+
pool: &PgPool,
49+
community: CommunityId,
50+
repo_id: &str,
51+
) -> Result<Option<String>> {
52+
let row = sqlx::query(
53+
"SELECT owner_pubkey FROM git_repo_names \
54+
WHERE community_id = $1 AND repo_id = $2",
55+
)
56+
.bind(community.as_uuid())
57+
.bind(repo_id)
58+
.fetch_optional(pool)
59+
.await?;
60+
row.map(|r| r.try_get("owner_pubkey"))
61+
.transpose()
62+
.map_err(crate::error::DbError::from)
63+
}
64+
65+
/// Reserve `repo_id` for `owner_pubkey` within `community`, enforcing a
66+
/// per-pubkey quota of `max_repos_per_pubkey`.
67+
///
68+
/// Semantics (mirrors the previous filesystem registry exactly):
69+
/// - already reserved by the same owner → [`ReserveOutcome::AlreadyOwned`]
70+
/// (idempotent, no quota check);
71+
/// - already reserved by another owner → [`ReserveOutcome::TakenByOther`];
72+
/// - otherwise, if the owner is under quota, atomically claim it →
73+
/// [`ReserveOutcome::Reserved`]; if a concurrent announce wins the insert
74+
/// race, the `ON CONFLICT` re-read resolves it to `AlreadyOwned` (same owner
75+
/// racing itself) or `TakenByOther`.
76+
///
77+
/// Returns `Err` only on backend/database failure — a full quota is *not* an
78+
/// error here; the caller enforces the limit against `Reserved` outcomes using
79+
/// [`count_repos_for_owner`]. (Kept as a separate call so the handler owns the
80+
/// error message and the ordering, matching the old code.)
81+
pub async fn reserve_repo_name(
82+
pool: &PgPool,
83+
community: CommunityId,
84+
repo_id: &str,
85+
owner_pubkey: &str,
86+
) -> Result<ReserveOutcome> {
87+
// Atomic claim: insert only if the (community, repo) is free. RETURNING is
88+
// non-empty exactly when *this* statement inserted the row, so it cleanly
89+
// distinguishes "I claimed it" from "someone already holds it" without a
90+
// separate read (TOCTOU-free, the same guarantee `create_dir` gave).
91+
let inserted = sqlx::query(
92+
"INSERT INTO git_repo_names (community_id, repo_id, owner_pubkey) \
93+
VALUES ($1, $2, $3) \
94+
ON CONFLICT (community_id, repo_id) DO NOTHING \
95+
RETURNING owner_pubkey",
96+
)
97+
.bind(community.as_uuid())
98+
.bind(repo_id)
99+
.bind(owner_pubkey)
100+
.fetch_optional(pool)
101+
.await?;
102+
103+
if inserted.is_some() {
104+
return Ok(ReserveOutcome::Reserved);
105+
}
106+
107+
// The row already existed — read the holder to classify same-owner
108+
// re-announce vs cross-owner collision.
109+
let existing = sqlx::query(
110+
"SELECT owner_pubkey FROM git_repo_names \
111+
WHERE community_id = $1 AND repo_id = $2",
112+
)
113+
.bind(community.as_uuid())
114+
.bind(repo_id)
115+
.fetch_optional(pool)
116+
.await?;
117+
118+
match existing {
119+
Some(row) => {
120+
let holder: String = row
121+
.try_get("owner_pubkey")
122+
.map_err(crate::error::DbError::from)?;
123+
if holder == owner_pubkey {
124+
Ok(ReserveOutcome::AlreadyOwned)
125+
} else {
126+
Ok(ReserveOutcome::TakenByOther)
127+
}
128+
}
129+
// Extremely narrow: the conflicting row was deleted between our INSERT
130+
// and this SELECT (e.g. a concurrent seed-failure rollback). Treat as
131+
// taken-by-other rather than silently granting — the announcer can
132+
// retry, and we never hand out a name we didn't atomically claim.
133+
None => Ok(ReserveOutcome::TakenByOther),
134+
}
135+
}
136+
137+
/// Count the repos currently reserved by `owner_pubkey` in `community`.
138+
///
139+
/// Backs the per-pubkey quota. Called *before* [`reserve_repo_name`] for a
140+
/// not-yet-owned name, so the handler can reject over-quota announces with its
141+
/// own error message.
142+
pub async fn count_repos_for_owner(
143+
pool: &PgPool,
144+
community: CommunityId,
145+
owner_pubkey: &str,
146+
) -> Result<i64> {
147+
let row = sqlx::query(
148+
"SELECT COUNT(*) AS n FROM git_repo_names \
149+
WHERE community_id = $1 AND owner_pubkey = $2",
150+
)
151+
.bind(community.as_uuid())
152+
.bind(owner_pubkey)
153+
.fetch_one(pool)
154+
.await?;
155+
row.try_get("n").map_err(crate::error::DbError::from)
156+
}
157+
158+
/// Release a reservation held by `owner_pubkey` (rollback path).
159+
///
160+
/// Used only when seeding the manifest pointer fails *after* a fresh
161+
/// [`ReserveOutcome::Reserved`], so the announce is all-or-nothing. Scoped to
162+
/// `owner_pubkey` so a rollback can never delete a name a *different* owner
163+
/// concurrently holds. Returns the number of rows removed (0 or 1).
164+
pub async fn release_repo_name(
165+
pool: &PgPool,
166+
community: CommunityId,
167+
repo_id: &str,
168+
owner_pubkey: &str,
169+
) -> Result<u64> {
170+
let result = sqlx::query(
171+
"DELETE FROM git_repo_names \
172+
WHERE community_id = $1 AND repo_id = $2 AND owner_pubkey = $3",
173+
)
174+
.bind(community.as_uuid())
175+
.bind(repo_id)
176+
.bind(owner_pubkey)
177+
.execute(pool)
178+
.await?;
179+
Ok(result.rows_affected())
180+
}
181+
182+
#[cfg(test)]
183+
mod tests {
184+
use super::*;
185+
use uuid::Uuid;
186+
187+
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz";
188+
189+
async fn setup_pool() -> PgPool {
190+
let database_url = std::env::var("BUZZ_TEST_DATABASE_URL")
191+
.or_else(|_| std::env::var("DATABASE_URL"))
192+
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
193+
PgPool::connect(&database_url)
194+
.await
195+
.expect("connect to test DB")
196+
}
197+
198+
async fn make_test_community(pool: &PgPool) -> CommunityId {
199+
let id = Uuid::new_v4();
200+
let host = format!("git-repo-test-{}.example", id.simple());
201+
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
202+
.bind(id)
203+
.bind(host)
204+
.execute(pool)
205+
.await
206+
.expect("insert test community");
207+
CommunityId::from_uuid(id)
208+
}
209+
210+
fn pk() -> String {
211+
format!("{:064x}", Uuid::new_v4().as_u128())
212+
}
213+
214+
/// A fresh name is `Reserved`; re-announcing it as the *same* owner is
215+
/// `AlreadyOwned` (idempotent) and never grows the owner's count; a
216+
/// *different* owner is `TakenByOther`.
217+
#[tokio::test]
218+
#[ignore = "requires Postgres"]
219+
async fn reserve_classifies_fresh_idempotent_and_collision() {
220+
let pool = setup_pool().await;
221+
let community = make_test_community(&pool).await;
222+
let owner = pk();
223+
let other = pk();
224+
let repo = format!("repo-{}", Uuid::new_v4().simple());
225+
226+
assert_eq!(
227+
reserve_repo_name(&pool, community, &repo, &owner)
228+
.await
229+
.expect("fresh reserve"),
230+
ReserveOutcome::Reserved,
231+
"first claim of a free name is Reserved"
232+
);
233+
assert_eq!(
234+
reserve_repo_name(&pool, community, &repo, &owner)
235+
.await
236+
.expect("re-reserve same owner"),
237+
ReserveOutcome::AlreadyOwned,
238+
"same-owner re-announce is idempotent AlreadyOwned"
239+
);
240+
assert_eq!(
241+
reserve_repo_name(&pool, community, &repo, &other)
242+
.await
243+
.expect("re-reserve other owner"),
244+
ReserveOutcome::TakenByOther,
245+
"a different owner claiming a held name is TakenByOther"
246+
);
247+
assert_eq!(
248+
count_repos_for_owner(&pool, community, &owner)
249+
.await
250+
.expect("count owner"),
251+
1,
252+
"re-announce must not double-count the owner's quota"
253+
);
254+
assert_eq!(
255+
count_repos_for_owner(&pool, community, &other)
256+
.await
257+
.expect("count other"),
258+
0,
259+
"a failed (TakenByOther) claim must not count toward the loser's quota"
260+
);
261+
}
262+
263+
/// `repo_name_owner` returns the holder for a reserved name and `None` for a
264+
/// free one, so the handler can classify before claiming.
265+
#[tokio::test]
266+
#[ignore = "requires Postgres"]
267+
async fn repo_name_owner_reflects_reservation() {
268+
let pool = setup_pool().await;
269+
let community = make_test_community(&pool).await;
270+
let owner = pk();
271+
let repo = format!("repo-{}", Uuid::new_v4().simple());
272+
273+
assert!(
274+
repo_name_owner(&pool, community, &repo)
275+
.await
276+
.expect("owner of free name")
277+
.is_none(),
278+
"an unreserved name has no owner"
279+
);
280+
reserve_repo_name(&pool, community, &repo, &owner)
281+
.await
282+
.expect("reserve");
283+
assert_eq!(
284+
repo_name_owner(&pool, community, &repo)
285+
.await
286+
.expect("owner of reserved name"),
287+
Some(owner),
288+
"a reserved name resolves to its owner"
289+
);
290+
}
291+
292+
/// Release is owner-scoped: it removes the reservation only for the holder,
293+
/// freeing the name for a subsequent claim; a release by a *non*-holder is a
294+
/// no-op that leaves the reservation intact.
295+
#[tokio::test]
296+
#[ignore = "requires Postgres"]
297+
async fn release_is_owner_scoped_and_frees_the_name() {
298+
let pool = setup_pool().await;
299+
let community = make_test_community(&pool).await;
300+
let owner = pk();
301+
let stranger = pk();
302+
let repo = format!("repo-{}", Uuid::new_v4().simple());
303+
304+
reserve_repo_name(&pool, community, &repo, &owner)
305+
.await
306+
.expect("reserve");
307+
308+
// A non-holder cannot release the name.
309+
assert_eq!(
310+
release_repo_name(&pool, community, &repo, &stranger)
311+
.await
312+
.expect("stranger release"),
313+
0,
314+
"release by a non-holder removes nothing"
315+
);
316+
assert_eq!(
317+
repo_name_owner(&pool, community, &repo)
318+
.await
319+
.expect("still owned"),
320+
Some(owner.clone()),
321+
"the reservation survives a stranger's release attempt"
322+
);
323+
324+
// The holder releases it, freeing the name.
325+
assert_eq!(
326+
release_repo_name(&pool, community, &repo, &owner)
327+
.await
328+
.expect("owner release"),
329+
1,
330+
"the holder's release removes exactly the one row"
331+
);
332+
assert_eq!(
333+
reserve_repo_name(&pool, community, &repo, &stranger)
334+
.await
335+
.expect("reclaim after release"),
336+
ReserveOutcome::Reserved,
337+
"once released, the name is free for a new owner"
338+
);
339+
}
340+
341+
/// Names are unique *within* a community, not globally: the same repo name
342+
/// may be independently reserved by different owners in different
343+
/// communities without collision.
344+
#[tokio::test]
345+
#[ignore = "requires Postgres"]
346+
async fn names_are_scoped_per_community() {
347+
let pool = setup_pool().await;
348+
let community_a = make_test_community(&pool).await;
349+
let community_b = make_test_community(&pool).await;
350+
let owner_a = pk();
351+
let owner_b = pk();
352+
let repo = format!("repo-{}", Uuid::new_v4().simple());
353+
354+
assert_eq!(
355+
reserve_repo_name(&pool, community_a, &repo, &owner_a)
356+
.await
357+
.expect("reserve in A"),
358+
ReserveOutcome::Reserved
359+
);
360+
assert_eq!(
361+
reserve_repo_name(&pool, community_b, &repo, &owner_b)
362+
.await
363+
.expect("reserve same name in B"),
364+
ReserveOutcome::Reserved,
365+
"the same name in a different community is a fresh, independent claim"
366+
);
367+
assert_eq!(
368+
repo_name_owner(&pool, community_a, &repo)
369+
.await
370+
.expect("owner in A"),
371+
Some(owner_a)
372+
);
373+
assert_eq!(
374+
repo_name_owner(&pool, community_b, &repo)
375+
.await
376+
.expect("owner in B"),
377+
Some(owner_b)
378+
);
379+
}
380+
}

0 commit comments

Comments
 (0)