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

Commit 05c51f8

Browse files
committed
namespaced usage limits
1 parent eac5751 commit 05c51f8

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
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: 0 additions & 14 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>,
@@ -132,7 +130,6 @@ where
132130
if let Some(AdminApiConfig { acceptor }) = self.admin_api_config {
133131
join_set.spawn(http::admin::run(
134132
acceptor,
135-
self.db_config_store,
136133
self.namespaces,
137134
));
138135
}
@@ -316,9 +313,6 @@ where
316313
let db_is_dirty = init_sentinel_file(&self.path)?;
317314
let idle_shutdown_kicker = self.setup_shutdown();
318315

319-
let db_config_store = Arc::new(
320-
DatabaseConfigStore::load(&self.path).context("Could not load database config")?,
321-
);
322316
let snapshot_callback = self.make_snapshot_callback();
323317
let auth = self.user_api_config.get_auth()?.into();
324318
let extensions = self.db_config.validate_extensions()?;
@@ -328,7 +322,6 @@ where
328322
let replica = Replica {
329323
rpc_config,
330324
stats_sender,
331-
db_config_store: db_config_store.clone(),
332325
extensions,
333326
db_config: self.db_config.clone(),
334327
base_path: self.path.clone(),
@@ -337,7 +330,6 @@ where
337330
let services = Services {
338331
namespaces,
339332
idle_shutdown_kicker,
340-
db_config_store,
341333
proxy_service,
342334
replication_service,
343335
user_api_config: self.user_api_config,
@@ -357,7 +349,6 @@ where
357349
db_config: self.db_config.clone(),
358350
idle_shutdown_kicker: idle_shutdown_kicker.clone(),
359351
stats_sender,
360-
db_config_store: db_config_store.clone(),
361352
db_is_dirty,
362353
snapshot_callback,
363354
extensions,
@@ -371,7 +362,6 @@ where
371362
let services = Services {
372363
namespaces,
373364
idle_shutdown_kicker,
374-
db_config_store,
375365
proxy_service,
376366
replication_service,
377367
user_api_config: self.user_api_config,
@@ -415,7 +405,6 @@ struct Primary<'a, A> {
415405
db_config: DbConfig,
416406
idle_shutdown_kicker: Option<IdleShutdownKicker>,
417407
stats_sender: StatsSender,
418-
db_config_store: Arc<DatabaseConfigStore>,
419408
db_is_dirty: bool,
420409
snapshot_callback: NamespacedSnapshotCallback,
421410
extensions: Arc<[PathBuf]>,
@@ -445,7 +434,6 @@ where
445434
bottomless_replication: self.db_config.bottomless_replication,
446435
extensions: self.extensions,
447436
stats_sender: self.stats_sender.clone(),
448-
config_store: self.db_config_store,
449437
max_response_size: self.db_config.max_response_size,
450438
max_total_response_size: self.db_config.max_total_response_size,
451439
checkpoint_interval: self.db_config.checkpoint_interval,
@@ -491,7 +479,6 @@ where
491479
struct Replica<C> {
492480
rpc_config: RpcClientConfig<C>,
493481
stats_sender: StatsSender,
494-
db_config_store: Arc<DatabaseConfigStore>,
495482
extensions: Arc<[PathBuf]>,
496483
db_config: DbConfig,
497484
base_path: Arc<Path>,
@@ -512,7 +499,6 @@ impl<C: Connector> Replica<C> {
512499
uri: uri.clone(),
513500
extensions: self.extensions.clone(),
514501
stats_sender: self.stats_sender.clone(),
515-
config_store: self.db_config_store.clone(),
516502
base_path: self.base_path,
517503
max_response_size: self.db_config.max_response_size,
518504
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
@@ -402,9 +402,16 @@ impl<M: MakeNamespace> NamespaceStore<M> {
402402
Ok(())
403403
}
404404

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

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

419427
impl<T: Database> Namespace<T> {
@@ -437,8 +445,6 @@ pub struct ReplicaNamespaceConfig {
437445
pub extensions: Arc<[PathBuf]>,
438446
/// Stats monitor
439447
pub stats_sender: StatsSender,
440-
/// Reference to the config store
441-
pub config_store: Arc<DatabaseConfigStore>,
442448
}
443449

444450
impl Namespace<ReplicaDatabase> {
@@ -458,6 +464,10 @@ impl Namespace<ReplicaDatabase> {
458464
));
459465
}
460466

467+
let db_config_store = Arc::new(
468+
DatabaseConfigStore::load(&db_path).context("Could not load database config")?,
469+
);
470+
461471
let mut join_set = JoinSet::new();
462472
let replicator = Replicator::new(
463473
db_path.clone(),
@@ -487,7 +497,7 @@ impl Namespace<ReplicaDatabase> {
487497
config.channel.clone(),
488498
config.uri.clone(),
489499
stats.clone(),
490-
config.config_store.clone(),
500+
db_config_store.clone(),
491501
applied_frame_no_receiver,
492502
config.max_response_size,
493503
config.max_total_response_size,
@@ -505,6 +515,7 @@ impl Namespace<ReplicaDatabase> {
505515
connection_maker: Arc::new(connection_maker),
506516
},
507517
stats,
518+
db_config_store,
508519
})
509520
}
510521
}
@@ -518,7 +529,6 @@ pub struct PrimaryNamespaceConfig {
518529
pub bottomless_replication: Option<bottomless::replicator::Options>,
519530
pub extensions: Arc<[PathBuf]>,
520531
pub stats_sender: StatsSender,
521-
pub config_store: Arc<DatabaseConfigStore>,
522532
pub max_response_size: u64,
523533
pub max_total_response_size: u64,
524534
pub checkpoint_interval: Option<Duration>,
@@ -622,12 +632,16 @@ impl Namespace<PrimaryDatabase> {
622632
)
623633
.await?;
624634

635+
let db_config_store = Arc::new(
636+
DatabaseConfigStore::load(&db_path).context("Could not load database config")?,
637+
);
638+
625639
let connection_maker: Arc<_> = LibSqlDbFactory::new(
626640
db_path.clone(),
627641
&REPLICATION_METHODS,
628642
ctx_builder.clone(),
629643
stats.clone(),
630-
config.config_store.clone(),
644+
db_config_store.clone(),
631645
config.extensions.clone(),
632646
config.max_response_size,
633647
config.max_total_response_size,
@@ -670,6 +684,7 @@ impl Namespace<PrimaryDatabase> {
670684
connection_maker,
671685
},
672686
stats,
687+
db_config_store,
673688
})
674689
}
675690
}

0 commit comments

Comments
 (0)