Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ac2ad43

Browse files
committed
namespaced usage limits
1 parent b09d9ee commit ac2ad43

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

sqld/src/connection/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{fs, io};
77
use crate::error::Error;
88
use crate::Result;
99

10+
#[derive(Debug)]
1011
pub struct DatabaseConfigStore {
1112
config_path: PathBuf,
1213
tmp_config_path: PathBuf,

sqld/src/http/admin/mod.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,28 @@ use tokio_util::io::ReaderStream;
1111
use url::Url;
1212
use uuid::Uuid;
1313

14-
use crate::connection::config::{DatabaseConfig, DatabaseConfigStore};
14+
use crate::connection::config::DatabaseConfig;
1515
use crate::error::LoadDumpError;
1616
use crate::namespace::{DumpStream, MakeNamespace, NamespaceStore, RestoreOption};
1717

1818
pub mod stats;
1919

2020
struct AppState<M: MakeNamespace> {
21-
db_config_store: Arc<DatabaseConfigStore>,
2221
namespaces: NamespaceStore<M>,
2322
}
2423

25-
pub async fn run<M, A>(
26-
acceptor: A,
27-
db_config_store: Arc<DatabaseConfigStore>,
28-
namespaces: NamespaceStore<M>,
29-
) -> anyhow::Result<()>
24+
pub async fn run<M, A>(acceptor: A, namespaces: NamespaceStore<M>) -> anyhow::Result<()>
3025
where
3126
A: crate::net::Accept,
3227
M: MakeNamespace,
3328
{
3429
use axum::routing::{get, post};
3530
let router = axum::Router::new()
3631
.route("/", get(handle_get_index))
37-
.route("/v1/config", get(handle_get_config))
38-
.route("/v1/block", post(handle_post_block))
32+
.route(
33+
"/v1/namespaces/:namespace/config",
34+
get(handle_get_config).post(handle_post_config),
35+
)
3936
.route(
4037
"/v1/namespaces/:namespace/fork/:to",
4138
post(handle_fork_namespace),
@@ -50,10 +47,7 @@ where
5047
)
5148
.route("/v1/namespaces/:namespace", delete(handle_delete_namespace))
5249
.route("/v1/namespaces/:namespace/stats", get(stats::handle_stats))
53-
.with_state(Arc::new(AppState {
54-
db_config_store,
55-
namespaces,
56-
}));
50+
.with_state(Arc::new(AppState { namespaces }));
5751

5852
hyper::server::Server::builder(acceptor)
5953
.serve(router.into_make_service())
@@ -68,8 +62,10 @@ async fn handle_get_index() -> &'static str {
6862

6963
async fn handle_get_config<M: MakeNamespace>(
7064
State(app_state): State<Arc<AppState<M>>>,
71-
) -> Json<Arc<DatabaseConfig>> {
72-
Json(app_state.db_config_store.get())
65+
Path(namespace): Path<String>,
66+
) -> crate::Result<Json<Arc<DatabaseConfig>>> {
67+
let store = app_state.namespaces.config_store(namespace.into()).await?;
68+
Ok(Json(store.get()))
7369
}
7470

7571
#[derive(Debug, Deserialize)]
@@ -85,22 +81,20 @@ struct CreateNamespaceReq {
8581
dump_url: Option<Url>,
8682
}
8783

88-
async fn handle_post_block<M: MakeNamespace>(
84+
async fn handle_post_config<M: MakeNamespace>(
8985
State(app_state): State<Arc<AppState<M>>>,
86+
Path(namespace): Path<String>,
9087
Json(req): Json<BlockReq>,
91-
) -> (axum::http::StatusCode, &'static str) {
92-
let mut config = (*app_state.db_config_store.get()).clone();
88+
) -> crate::Result<()> {
89+
let store = app_state.namespaces.config_store(namespace.into()).await?;
90+
let mut config = (*store.get()).clone();
9391
config.block_reads = req.block_reads;
9492
config.block_writes = req.block_writes;
9593
config.block_reason = req.block_reason;
9694

97-
match app_state.db_config_store.store(config) {
98-
Ok(()) => (axum::http::StatusCode::OK, "OK"),
99-
Err(err) => {
100-
tracing::warn!("Could not store database config: {err}");
101-
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Failed")
102-
}
103-
}
95+
store.store(config)?;
96+
97+
Ok(())
10498
}
10599

106100
async fn handle_create_namespace<M: MakeNamespace>(

sqld/src/lib.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use url::Url;
3232
use utils::services::idle_shutdown::IdleShutdownKicker;
3333

3434
use crate::auth::Auth;
35-
use crate::connection::config::DatabaseConfigStore;
3635
use crate::connection::{Connection, MakeConnection};
3736
use crate::error::Error;
3837
use crate::migration::maybe_migrate;
@@ -91,7 +90,6 @@ pub struct Server<C = HttpConnector, A = AddrIncoming> {
9190
struct Services<M: MakeNamespace, A, P, S> {
9291
namespaces: NamespaceStore<M>,
9392
idle_shutdown_kicker: Option<IdleShutdownKicker>,
94-
db_config_store: Arc<DatabaseConfigStore>,
9593
proxy_service: P,
9694
replication_service: S,
9795
user_api_config: UserApiConfig<A>,
@@ -130,11 +128,7 @@ where
130128
user_http.configure(join_set);
131129

132130
if let Some(AdminApiConfig { acceptor }) = self.admin_api_config {
133-
join_set.spawn(http::admin::run(
134-
acceptor,
135-
self.db_config_store,
136-
self.namespaces,
137-
));
131+
join_set.spawn(http::admin::run(acceptor, self.namespaces));
138132
}
139133
}
140134
}
@@ -316,9 +310,6 @@ where
316310
let db_is_dirty = init_sentinel_file(&self.path)?;
317311
let idle_shutdown_kicker = self.setup_shutdown();
318312

319-
let db_config_store = Arc::new(
320-
DatabaseConfigStore::load(&self.path).context("Could not load database config")?,
321-
);
322313
let snapshot_callback = self.make_snapshot_callback();
323314
let auth = self.user_api_config.get_auth()?.into();
324315
let extensions = self.db_config.validate_extensions()?;
@@ -328,7 +319,6 @@ where
328319
let replica = Replica {
329320
rpc_config,
330321
stats_sender,
331-
db_config_store: db_config_store.clone(),
332322
extensions,
333323
db_config: self.db_config.clone(),
334324
base_path: self.path.clone(),
@@ -337,7 +327,6 @@ where
337327
let services = Services {
338328
namespaces,
339329
idle_shutdown_kicker,
340-
db_config_store,
341330
proxy_service,
342331
replication_service,
343332
user_api_config: self.user_api_config,
@@ -357,7 +346,6 @@ where
357346
db_config: self.db_config.clone(),
358347
idle_shutdown_kicker: idle_shutdown_kicker.clone(),
359348
stats_sender,
360-
db_config_store: db_config_store.clone(),
361349
db_is_dirty,
362350
snapshot_callback,
363351
extensions,
@@ -371,7 +359,6 @@ where
371359
let services = Services {
372360
namespaces,
373361
idle_shutdown_kicker,
374-
db_config_store,
375362
proxy_service,
376363
replication_service,
377364
user_api_config: self.user_api_config,
@@ -415,7 +402,6 @@ struct Primary<'a, A> {
415402
db_config: DbConfig,
416403
idle_shutdown_kicker: Option<IdleShutdownKicker>,
417404
stats_sender: StatsSender,
418-
db_config_store: Arc<DatabaseConfigStore>,
419405
db_is_dirty: bool,
420406
snapshot_callback: NamespacedSnapshotCallback,
421407
extensions: Arc<[PathBuf]>,
@@ -445,7 +431,6 @@ where
445431
bottomless_replication: self.db_config.bottomless_replication,
446432
extensions: self.extensions,
447433
stats_sender: self.stats_sender.clone(),
448-
config_store: self.db_config_store,
449434
max_response_size: self.db_config.max_response_size,
450435
max_total_response_size: self.db_config.max_total_response_size,
451436
checkpoint_interval: self.db_config.checkpoint_interval,
@@ -491,7 +476,6 @@ where
491476
struct Replica<C> {
492477
rpc_config: RpcClientConfig<C>,
493478
stats_sender: StatsSender,
494-
db_config_store: Arc<DatabaseConfigStore>,
495479
extensions: Arc<[PathBuf]>,
496480
db_config: DbConfig,
497481
base_path: Arc<Path>,
@@ -512,7 +496,6 @@ impl<C: Connector> Replica<C> {
512496
uri: uri.clone(),
513497
extensions: self.extensions.clone(),
514498
stats_sender: self.stats_sender.clone(),
515-
config_store: self.db_config_store.clone(),
516499
base_path: self.base_path,
517500
max_response_size: self.db_config.max_response_size,
518501
max_total_response_size: self.db_config.max_total_response_size,

sqld/src/namespace/mod.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,16 @@ impl<M: MakeNamespace> NamespaceStore<M> {
403403
Ok(())
404404
}
405405

406-
pub async fn stats(&self, namespace: Bytes) -> crate::Result<Arc<Stats>> {
406+
pub(crate) async fn stats(&self, namespace: Bytes) -> crate::Result<Arc<Stats>> {
407407
self.with(namespace, |ns| ns.stats.clone()).await
408408
}
409+
410+
pub(crate) async fn config_store(
411+
&self,
412+
namespace: Bytes,
413+
) -> crate::Result<Arc<DatabaseConfigStore>> {
414+
self.with(namespace, |ns| ns.db_config_store.clone()).await
415+
}
409416
}
410417

411418
/// A namspace isolates the resources pertaining to a database of type T
@@ -415,6 +422,7 @@ pub struct Namespace<T: Database> {
415422
/// The set of tasks associated with this namespace
416423
tasks: JoinSet<anyhow::Result<()>>,
417424
stats: Arc<Stats>,
425+
db_config_store: Arc<DatabaseConfigStore>,
418426
}
419427

420428
impl<T: Database> Namespace<T> {
@@ -438,8 +446,6 @@ pub struct ReplicaNamespaceConfig {
438446
pub extensions: Arc<[PathBuf]>,
439447
/// Stats monitor
440448
pub stats_sender: StatsSender,
441-
/// Reference to the config store
442-
pub config_store: Arc<DatabaseConfigStore>,
443449
}
444450

445451
impl Namespace<ReplicaDatabase> {
@@ -459,6 +465,10 @@ impl Namespace<ReplicaDatabase> {
459465
));
460466
}
461467

468+
let db_config_store = Arc::new(
469+
DatabaseConfigStore::load(&db_path).context("Could not load database config")?,
470+
);
471+
462472
let mut join_set = JoinSet::new();
463473
let replicator = Replicator::new(
464474
db_path.clone(),
@@ -489,7 +499,7 @@ impl Namespace<ReplicaDatabase> {
489499
config.channel.clone(),
490500
config.uri.clone(),
491501
stats.clone(),
492-
config.config_store.clone(),
502+
db_config_store.clone(),
493503
applied_frame_no_receiver,
494504
config.max_response_size,
495505
config.max_total_response_size,
@@ -507,6 +517,7 @@ impl Namespace<ReplicaDatabase> {
507517
connection_maker: Arc::new(connection_maker),
508518
},
509519
stats,
520+
db_config_store,
510521
})
511522
}
512523
}
@@ -520,7 +531,6 @@ pub struct PrimaryNamespaceConfig {
520531
pub bottomless_replication: Option<bottomless::replicator::Options>,
521532
pub extensions: Arc<[PathBuf]>,
522533
pub stats_sender: StatsSender,
523-
pub config_store: Arc<DatabaseConfigStore>,
524534
pub max_response_size: u64,
525535
pub max_total_response_size: u64,
526536
pub checkpoint_interval: Option<Duration>,
@@ -625,12 +635,16 @@ impl Namespace<PrimaryDatabase> {
625635
)
626636
.await?;
627637

638+
let db_config_store = Arc::new(
639+
DatabaseConfigStore::load(&db_path).context("Could not load database config")?,
640+
);
641+
628642
let connection_maker: Arc<_> = LibSqlDbFactory::new(
629643
db_path.clone(),
630644
&REPLICATION_METHODS,
631645
ctx_builder.clone(),
632646
stats.clone(),
633-
config.config_store.clone(),
647+
db_config_store.clone(),
634648
config.extensions.clone(),
635649
config.max_response_size,
636650
config.max_total_response_size,
@@ -673,6 +687,7 @@ impl Namespace<PrimaryDatabase> {
673687
connection_maker,
674688
},
675689
stats,
690+
db_config_store,
676691
})
677692
}
678693
}

0 commit comments

Comments
 (0)