Skip to content

Commit 8b17eb1

Browse files
Aresoncoder 0codex
authored andcommitted
fix(db): audit partition catalog before creation
Audit partition coverage and trigger parity before optional DDL, publish cached diagnostics and metrics, and keep the canary read-only when creation is disabled. Co-authored-by: coder 0 <d97ebdbb198c7237c94f84ea8bb8a73583ea067407eebd0062abbb3962527fb1@buzz.block.builderlab.xyz> Signed-off-by: coder 0 <d97ebdbb198c7237c94f84ea8bb8a73583ea067407eebd0062abbb3962527fb1@buzz.block.builderlab.xyz> Co-authored-by: Codex <[email protected]> Ai-assisted: true Signed-off-by: Ian Oberst <[email protected]> Signed-off-by: Ian Oberst <[email protected]>
1 parent b37e477 commit 8b17eb1

11 files changed

Lines changed: 4008 additions & 149 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ REDIS_URL=redis://localhost:6379
3737
# Max connections in each of the relay's Postgres pools — writer and, when
3838
# READ_DATABASE_URL is set, reader (default 50).
3939
# BUZZ_DB_POOL_SIZE=50
40+
# Read-only partition catalog audit interval, clamped to 60s..24h (default 900s).
41+
# BUZZ_PARTITION_AUDIT_INTERVAL_SECS=900
42+
# Emergency kill switch for monthly partition CREATE; audits remain enabled.
43+
# BUZZ_PARTITION_MANAGER_CREATE_ENABLED=true
4044

4145
# Writer-session Postgres timeouts for buzz-db-backed pools and the relay audit
4246
# pool, all in milliseconds; 0 disables. The separately deployed push gateway

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/buzz-admin/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ buzz-media = { workspace = true }
2424
nostr = { workspace = true }
2525
tokio = { workspace = true }
2626
serde_json = { workspace = true }
27+
serde = { workspace = true }
2728
anyhow = { workspace = true }
2829
hex = { workspace = true }
2930
deadpool-redis = { workspace = true }

‎crates/buzz-admin/src/main.rs‎

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ use std::time::Instant;
3131
use anyhow::Result;
3232
use buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST;
3333
use buzz_core::tenant::{relay_url_authority, TenantContext};
34-
use buzz_db::{Db, DbConfig};
34+
use buzz_db::{partition::PartitionAuditReport, Db, DbConfig};
3535
use buzz_media::{BucketSnapshot, MediaConfig, MediaStorage, S3AddressingStyle, SweepError};
3636
use buzz_pubsub::{EventTopic, PubSubManager};
3737
use clap::{Parser, Subcommand};
3838
use nostr::{EventBuilder, Keys, Kind, Tag};
39+
use serde::Serialize;
40+
use sqlx::Row;
3941
use tracing::warn;
4042

4143
#[derive(Parser)]
@@ -89,6 +91,12 @@ enum Command {
8991
#[arg(long, default_value_t = 10_000_000)]
9092
max_objects: u64,
9193
},
94+
/// Run the partition catalog audit using a read-only database session.
95+
PartitionAudit {
96+
/// Future months to include in the coverage check.
97+
#[arg(long, default_value_t = 3)]
98+
months_ahead: u32,
99+
},
92100
/// Inspect deployment-wide Buzz product feedback.
93101
ProductFeedback {
94102
#[command(subcommand)]
@@ -166,6 +174,7 @@ async fn run(cli: Cli) -> Result<i32> {
166174
Ok(0)
167175
}
168176
Command::StorageSnapshot { max_objects } => cmd_storage_snapshot(max_objects).await,
177+
Command::PartitionAudit { months_ahead } => cmd_partition_audit(months_ahead).await,
169178
Command::AddMember { pubkey, role } => cmd_add_member(pubkey, role).await,
170179
Command::RemoveMember { pubkey, role } => cmd_remove_member(pubkey, role).await,
171180
Command::ListMembers => cmd_list_members().await,
@@ -314,6 +323,94 @@ fn storage_config_from_env() -> Result<MediaConfig> {
314323
})
315324
}
316325

326+
#[derive(Serialize)]
327+
struct PartitionAuditIdentity {
328+
database: String,
329+
user: String,
330+
schema: String,
331+
default_transaction_read_only: bool,
332+
transaction_read_only: bool,
333+
}
334+
335+
#[derive(Serialize)]
336+
struct PartitionAuditOutput {
337+
schema_version: u32,
338+
mode: &'static str,
339+
source_sha: &'static str,
340+
build_id: &'static str,
341+
build_url: &'static str,
342+
outcome: &'static str,
343+
months_ahead: u32,
344+
identity: PartitionAuditIdentity,
345+
report: PartitionAuditReport,
346+
}
347+
348+
async fn cmd_partition_audit(months_ahead: u32) -> Result<i32> {
349+
let db_url = std::env::var("DATABASE_URL")
350+
.map_err(|_| anyhow::anyhow!("DATABASE_URL is required for partition-audit"))?;
351+
let config = DbConfig {
352+
database_url: db_url,
353+
max_connections: 1,
354+
min_connections: 1,
355+
statement_timeout_ms: 30_000,
356+
default_transaction_read_only: true,
357+
..DbConfig::default()
358+
};
359+
let pool = Db::connect_writer_pool(&config).await?;
360+
let row = sqlx::query(
361+
"SELECT current_database() AS database, current_user AS user, \
362+
current_schema() AS schema, \
363+
current_setting('default_transaction_read_only') = 'on' \
364+
AS default_transaction_read_only, \
365+
current_setting('transaction_read_only') = 'on' AS transaction_read_only",
366+
)
367+
.fetch_one(&pool)
368+
.await?;
369+
let identity = PartitionAuditIdentity {
370+
database: row.try_get("database")?,
371+
user: row.try_get("user")?,
372+
schema: row.try_get("schema")?,
373+
default_transaction_read_only: row.try_get("default_transaction_read_only")?,
374+
transaction_read_only: row.try_get("transaction_read_only")?,
375+
};
376+
if !identity.default_transaction_read_only || !identity.transaction_read_only {
377+
anyhow::bail!("partition-audit connection is not read-only");
378+
}
379+
380+
let report = Db::from_pool(pool)
381+
.audit_partitions_report(months_ahead)
382+
.await;
383+
let outcome = if !report.errors.is_empty() {
384+
"error"
385+
} else if !report.serving_safe() {
386+
"unsafe"
387+
} else if report.tables.iter().any(|table| table.degraded()) {
388+
"degraded"
389+
} else {
390+
"ok"
391+
};
392+
let code = match outcome {
393+
"error" => 5,
394+
"unsafe" => 2,
395+
_ => 0,
396+
};
397+
println!(
398+
"{}",
399+
serde_json::to_string_pretty(&PartitionAuditOutput {
400+
schema_version: 1,
401+
mode: "read_only",
402+
source_sha: option_env!("BUZZ_SOURCE_SHA").unwrap_or("unknown"),
403+
build_id: option_env!("BUZZ_BUILD_ID").unwrap_or("local"),
404+
build_url: option_env!("BUZZ_BUILD_URL").unwrap_or("unknown"),
405+
outcome,
406+
months_ahead,
407+
identity,
408+
report,
409+
})?
410+
);
411+
Ok(code)
412+
}
413+
317414
async fn cmd_add_member(pubkey_arg: String, role: String) -> Result<i32> {
318415
if let Err(msg) = validate_role(&role) {
319416
eprintln!("error: {msg}");
@@ -581,8 +678,7 @@ async fn connect_db() -> Result<Db> {
581678
}
582679

583680
fn db_config_from_env() -> DbConfig {
584-
let db_url = std::env::var("DATABASE_URL")
585-
.unwrap_or_else(|_| "postgres://buzz:buzz_dev@localhost:5432/buzz".to_string());
681+
let db_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| DbConfig::default().database_url);
586682
DbConfig {
587683
database_url: db_url,
588684
..DbConfig::default()
@@ -801,3 +897,18 @@ mod storage_snapshot_tests {
801897
assert_eq!(persist_calls.load(Ordering::SeqCst), 0);
802898
}
803899
}
900+
901+
#[cfg(test)]
902+
mod tests {
903+
use super::*;
904+
905+
#[test]
906+
fn parses_partition_audit_month_horizon() {
907+
let cli = Cli::try_parse_from(["buzz-admin", "partition-audit", "--months-ahead", "6"])
908+
.expect("parse partition-audit command");
909+
assert!(matches!(
910+
cli.command,
911+
Command::PartitionAudit { months_ahead: 6 }
912+
));
913+
}
914+
}

‎crates/buzz-db/src/runtime/mod.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ pub struct DbConfig {
539539
/// (env `BUZZ_DB_STATEMENT_TIMEOUT_MS`). `0` disables it and is the
540540
/// default because migrations and backfills may legitimately run long.
541541
pub statement_timeout_ms: u64,
542+
/// Whether every new writer-pool connection defaults all transactions to
543+
/// read-only. Intended for operator audit commands, never the relay pool.
544+
pub default_transaction_read_only: bool,
542545
}
543546

544547
impl Default for DbConfig {
@@ -559,6 +562,7 @@ impl Default for DbConfig {
559562
lock_timeout_ms: DEFAULT_LOCK_TIMEOUT_MS,
560563
idle_txn_timeout_ms: DEFAULT_IDLE_TXN_TIMEOUT_MS,
561564
statement_timeout_ms: 0,
565+
default_transaction_read_only: false,
562566
}
563567
}
564568
}
@@ -644,6 +648,7 @@ impl Db {
644648
let lock_timeout_ms = config.lock_timeout_ms;
645649
let idle_txn_timeout_ms = config.idle_txn_timeout_ms;
646650
let statement_timeout_ms = config.statement_timeout_ms;
651+
let default_transaction_read_only = config.default_transaction_read_only;
647652
let options = PgPoolOptions::new()
648653
.max_connections(config.max_connections)
649654
.min_connections(config.min_connections)
@@ -688,11 +693,17 @@ impl Db {
688693
if let Err(error) = sqlx::query(
689694
"SELECT set_config('lock_timeout', $1, false), \
690695
set_config('idle_in_transaction_session_timeout', $2, false), \
691-
set_config('statement_timeout', $3, false)",
696+
set_config('statement_timeout', $3, false), \
697+
set_config('default_transaction_read_only', $4, false)",
692698
)
693699
.bind(lock_timeout_ms.to_string())
694700
.bind(idle_txn_timeout_ms.to_string())
695701
.bind(statement_timeout_ms.to_string())
702+
.bind(if default_transaction_read_only {
703+
"on"
704+
} else {
705+
"off"
706+
})
696707
.execute(&mut *conn)
697708
.await
698709
{

0 commit comments

Comments
 (0)