Skip to content

Commit

Permalink
libsql-server: use LRU cache to store active namespaces
Browse files Browse the repository at this point in the history
This is a port of Marin's libsql/sqld#689

This PR replaces the namespace store dumb hashmap with an LRU cache to bound
the maximum number of namespaces loaded into memory while unbounding the number
of namespaces allocated on a sqld instance.  Additionally, this enables fully
concurrent access to namespaces by removing the global lock on the namespace
store.

Co-authored-by: Piotr Sarna <[email protected]>
  • Loading branch information
Marin Postma and psarna committed Dec 1, 2023
1 parent b3145d8 commit 5f0be83
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 95 deletions.
1 change: 1 addition & 0 deletions libsql-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
http-body = "0.4"
url = { version = "2.3", features = ["serde"] }
uuid = { version = "1.3", features = ["v4", "serde"] }
moka = { version = "0.12.1", features = ["future"] }

[dev-dependencies]
arbitrary = { version = "1.3.0", features = ["derive_arbitrary"] }
Expand Down
14 changes: 12 additions & 2 deletions libsql-server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub enum Error {
UrlParseError(#[from] url::ParseError),
#[error("Namespace store has shutdown")]
NamespaceStoreShutdown,
// This is for errors returned by moka
#[error(transparent)]
Ref(#[from] std::sync::Arc<Self>),
}

trait ResponseError: std::error::Error {
Expand All @@ -105,6 +108,12 @@ trait ResponseError: std::error::Error {
impl ResponseError for Error {}

impl IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
(&self).into_response()
}
}

impl IntoResponse for &Error {
fn into_response(self) -> axum::response::Response {
use Error::*;

Expand Down Expand Up @@ -150,6 +159,7 @@ impl IntoResponse for Error {
PrimaryStreamInterupted => self.format_err(StatusCode::INTERNAL_SERVER_ERROR),
UrlParseError(_) => self.format_err(StatusCode::BAD_REQUEST),
NamespaceStoreShutdown => self.format_err(StatusCode::SERVICE_UNAVAILABLE),
Ref(this) => this.as_ref().into_response(),
}
}
}
Expand Down Expand Up @@ -224,7 +234,7 @@ pub enum LoadDumpError {

impl ResponseError for LoadDumpError {}

impl IntoResponse for LoadDumpError {
impl IntoResponse for &LoadDumpError {
fn into_response(self) -> axum::response::Response {
use LoadDumpError::*;

Expand All @@ -244,7 +254,7 @@ impl IntoResponse for LoadDumpError {

impl ResponseError for ForkError {}

impl IntoResponse for ForkError {
impl IntoResponse for &ForkError {
fn into_response(self) -> axum::response::Response {
match self {
ForkError::Internal(_)
Expand Down
Loading

0 comments on commit 5f0be83

Please sign in to comment.