Skip to content

Commit 2e86fc2

Browse files
committed
namespace: make namespace cache configurable
... specifically, half-configurable, since we could also configure the time-to-idle period. On the other hand, that's another burden for the user to decide, and 5 minutes sound generous enough.
1 parent 5f0be83 commit 2e86fc2

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

libsql-server/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct Server<C = HttpConnector, A = AddrIncoming, D = HttpsConnector<HttpCo
100100
pub heartbeat_config: Option<HeartbeatConfig>,
101101
pub disable_namespaces: bool,
102102
pub shutdown: Arc<Notify>,
103+
pub max_active_namespaces: usize,
103104
}
104105

105106
impl<C, A, D> Default for Server<C, A, D> {
@@ -117,6 +118,7 @@ impl<C, A, D> Default for Server<C, A, D> {
117118
heartbeat_config: Default::default(),
118119
disable_namespaces: true,
119120
shutdown: Default::default(),
121+
max_active_namespaces: 100,
120122
}
121123
}
122124
}
@@ -384,6 +386,7 @@ where
384386
db_config: self.db_config.clone(),
385387
base_path: self.path.clone(),
386388
auth: auth.clone(),
389+
max_active_namespaces: self.max_active_namespaces,
387390
};
388391
let (namespaces, proxy_service, replication_service) = replica.configure().await?;
389392
self.rpc_client_config = None;
@@ -422,6 +425,7 @@ where
422425
extensions,
423426
base_path: self.path.clone(),
424427
disable_namespaces: self.disable_namespaces,
428+
max_active_namespaces: self.max_active_namespaces,
425429
join_set: &mut join_set,
426430
auth: auth.clone(),
427431
};
@@ -487,6 +491,7 @@ struct Primary<'a, A> {
487491
extensions: Arc<[PathBuf]>,
488492
base_path: Arc<Path>,
489493
disable_namespaces: bool,
494+
max_active_namespaces: usize,
490495
auth: Arc<Auth>,
491496
join_set: &'a mut JoinSet<anyhow::Result<()>>,
492497
}
@@ -517,7 +522,12 @@ where
517522
disable_namespace: self.disable_namespaces,
518523
};
519524
let factory = PrimaryNamespaceMaker::new(conf);
520-
let namespaces = NamespaceStore::new(factory, false, self.db_config.snapshot_at_shutdown);
525+
let namespaces = NamespaceStore::new(
526+
factory,
527+
false,
528+
self.db_config.snapshot_at_shutdown,
529+
self.max_active_namespaces,
530+
);
521531

522532
// eagerly load the default namespace when namespaces are disabled
523533
if self.disable_namespaces {
@@ -583,6 +593,7 @@ struct Replica<C> {
583593
db_config: DbConfig,
584594
base_path: Arc<Path>,
585595
auth: Arc<Auth>,
596+
max_active_namespaces: usize,
586597
}
587598

588599
impl<C: Connector> Replica<C> {
@@ -605,7 +616,7 @@ impl<C: Connector> Replica<C> {
605616
max_total_response_size: self.db_config.max_total_response_size,
606617
};
607618
let factory = ReplicaNamespaceMaker::new(conf);
608-
let namespaces = NamespaceStore::new(factory, true, false);
619+
let namespaces = NamespaceStore::new(factory, true, false, self.max_active_namespaces);
609620
let replication_service = ReplicationLogProxyService::new(channel.clone(), uri.clone());
610621
let proxy_service = ReplicaProxyService::new(channel, uri, self.auth.clone());
611622

libsql-server/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ struct Cli {
195195
/// Enable snapshot at shutdown
196196
#[clap(long)]
197197
snapshot_at_shutdown: bool,
198+
199+
/// Max active namespaces kept in-memory
200+
#[clap(long, env = "SQLD_MAX_ACTIVE_NAMESPACES", default_value = "100")]
201+
max_active_namespaces: usize,
198202
}
199203

200204
#[derive(clap::Subcommand, Debug)]
@@ -506,6 +510,7 @@ async fn build_server(config: &Cli) -> anyhow::Result<Server> {
506510
disable_default_namespace: config.disable_default_namespace,
507511
disable_namespaces: !config.enable_namespaces,
508512
shutdown,
513+
max_active_namespaces: config.max_active_namespaces,
509514
})
510515
}
511516

libsql-server/src/namespace/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,12 @@ struct NamespaceStoreInner<M: MakeNamespace> {
360360
}
361361

362362
impl<M: MakeNamespace> NamespaceStore<M> {
363-
pub fn new(make_namespace: M, allow_lazy_creation: bool, snapshot_at_shutdown: bool) -> Self {
363+
pub fn new(
364+
make_namespace: M,
365+
allow_lazy_creation: bool,
366+
snapshot_at_shutdown: bool,
367+
max_active_namespaces: usize,
368+
) -> Self {
364369
let store = Cache::<NamespaceName, NamespaceEntry<M::Database>>::builder()
365370
.async_eviction_listener(|name, ns, _| {
366371
Box::pin(async move {
@@ -374,7 +379,8 @@ impl<M: MakeNamespace> NamespaceStore<M> {
374379
})
375380
})
376381
// TODO(marin): configurable capacity
377-
.max_capacity(25)
382+
.max_capacity(max_active_namespaces as u64)
383+
.time_to_idle(Duration::from_secs(300))
378384
.build();
379385
Self {
380386
inner: Arc::new(NamespaceStoreInner {

libsql-server/src/test/bottomless.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async fn configure_server(
6464
},
6565
path: path.into().into(),
6666
disable_default_namespace: false,
67+
max_active_namespaces: 100,
6768
heartbeat_config: None,
6869
idle_shutdown_timeout: None,
6970
initial_idle_shutdown_timeout: None,

0 commit comments

Comments
 (0)