Skip to content

Commit

Permalink
namespace: make namespace cache configurable
Browse files Browse the repository at this point in the history
... 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.
  • Loading branch information
psarna committed Dec 1, 2023
1 parent 5f0be83 commit ed03946
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions libsql-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct Server<C = HttpConnector, A = AddrIncoming, D = HttpsConnector<HttpCo
pub heartbeat_config: Option<HeartbeatConfig>,
pub disable_namespaces: bool,
pub shutdown: Arc<Notify>,
pub max_active_namespaces: usize,
}

impl<C, A, D> Default for Server<C, A, D> {
Expand All @@ -117,6 +118,7 @@ impl<C, A, D> Default for Server<C, A, D> {
heartbeat_config: Default::default(),
disable_namespaces: true,
shutdown: Default::default(),
max_active_namespaces: 100,
}
}
}
Expand Down Expand Up @@ -384,6 +386,7 @@ where
db_config: self.db_config.clone(),
base_path: self.path.clone(),
auth: auth.clone(),
max_active_namespaces: self.max_active_namespaces,
};
let (namespaces, proxy_service, replication_service) = replica.configure().await?;
self.rpc_client_config = None;
Expand Down Expand Up @@ -422,6 +425,7 @@ where
extensions,
base_path: self.path.clone(),
disable_namespaces: self.disable_namespaces,
max_active_namespaces: self.max_active_namespaces,
join_set: &mut join_set,
auth: auth.clone(),
};
Expand Down Expand Up @@ -487,6 +491,7 @@ struct Primary<'a, A> {
extensions: Arc<[PathBuf]>,
base_path: Arc<Path>,
disable_namespaces: bool,
max_active_namespaces: usize,
auth: Arc<Auth>,
join_set: &'a mut JoinSet<anyhow::Result<()>>,
}
Expand Down Expand Up @@ -517,7 +522,12 @@ where
disable_namespace: self.disable_namespaces,
};
let factory = PrimaryNamespaceMaker::new(conf);
let namespaces = NamespaceStore::new(factory, false, self.db_config.snapshot_at_shutdown);
let namespaces = NamespaceStore::new(
factory,
false,
self.db_config.snapshot_at_shutdown,
self.max_active_namespaces,
);

// eagerly load the default namespace when namespaces are disabled
if self.disable_namespaces {
Expand Down Expand Up @@ -583,6 +593,7 @@ struct Replica<C> {
db_config: DbConfig,
base_path: Arc<Path>,
auth: Arc<Auth>,
max_active_namespaces: usize,
}

impl<C: Connector> Replica<C> {
Expand All @@ -605,7 +616,7 @@ impl<C: Connector> Replica<C> {
max_total_response_size: self.db_config.max_total_response_size,
};
let factory = ReplicaNamespaceMaker::new(conf);
let namespaces = NamespaceStore::new(factory, true, false);
let namespaces = NamespaceStore::new(factory, true, false, self.max_active_namespaces);
let replication_service = ReplicationLogProxyService::new(channel.clone(), uri.clone());
let proxy_service = ReplicaProxyService::new(channel, uri, self.auth.clone());

Expand Down
5 changes: 5 additions & 0 deletions libsql-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ struct Cli {
/// Enable snapshot at shutdown
#[clap(long)]
snapshot_at_shutdown: bool,

/// Max active namespaces kept in-memory
#[clap(long, env = "SQLD_MAX_ACTIVE_NAMESPACES", default_value = "100")]
max_active_namespaces: usize,
}

#[derive(clap::Subcommand, Debug)]
Expand Down Expand Up @@ -506,6 +510,7 @@ async fn build_server(config: &Cli) -> anyhow::Result<Server> {
disable_default_namespace: config.disable_default_namespace,
disable_namespaces: !config.enable_namespaces,
shutdown,
max_active_namespaces: config.max_active_namespaces,
})
}

Expand Down
10 changes: 8 additions & 2 deletions libsql-server/src/namespace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,12 @@ struct NamespaceStoreInner<M: MakeNamespace> {
}

impl<M: MakeNamespace> NamespaceStore<M> {
pub fn new(make_namespace: M, allow_lazy_creation: bool, snapshot_at_shutdown: bool) -> Self {
pub fn new(
make_namespace: M,
allow_lazy_creation: bool,
snapshot_at_shutdown: bool,
max_active_namespaces: usize,
) -> Self {
let store = Cache::<NamespaceName, NamespaceEntry<M::Database>>::builder()
.async_eviction_listener(|name, ns, _| {
Box::pin(async move {
Expand All @@ -374,7 +379,8 @@ impl<M: MakeNamespace> NamespaceStore<M> {
})
})
// TODO(marin): configurable capacity
.max_capacity(25)
.max_capacity(max_active_namespaces as u64)
.time_to_idle(Duration::from_secs(300))
.build();
Self {
inner: Arc::new(NamespaceStoreInner {
Expand Down

0 comments on commit ed03946

Please sign in to comment.