Skip to content

Commit 045bef9

Browse files
committed
replace parking_lot with tokio::sync::Mutex in some places
1 parent 4fa2ce8 commit 045bef9

5 files changed

Lines changed: 37 additions & 30 deletions

File tree

libsql-server/src/connection/program.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,15 @@ pub fn check_program_auth(
363363
}
364364
StmtKind::Attach(ref ns) => {
365365
ctx.auth.has_right(ns, Permission::AttachRead)?;
366-
if !ctx.meta_store.handle(ns.clone()).get().allow_attach {
367-
return Err(Error::NotAuthorized(format!(
368-
"Namespace `{ns}` doesn't allow attach"
369-
)));
370-
}
366+
return tokio::runtime::Handle::current().block_on(async {
367+
if !ctx.meta_store.handle(ns.clone()).await.get().allow_attach {
368+
return Err(Error::NotAuthorized(format!(
369+
"Namespace `{ns}` doesn't allow attach"
370+
)));
371+
} else {
372+
Ok(())
373+
}
374+
});
371375
}
372376
StmtKind::Detach => (),
373377
}

libsql-server/src/http/admin/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ async fn handle_create_namespace<C: Connector>(
331331
));
332332
}
333333
// TODO: move this check into meta store
334-
if !app_state.namespaces.exists(&ns) {
334+
if !app_state.namespaces.exists(&ns).await {
335335
return Err(Error::NamespaceDoesntExist(ns.to_string()));
336336
}
337337

libsql-server/src/namespace/meta_store.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ struct MetaStoreInner {
7070
// TODO(lucio): Use a concurrent hashmap so we don't block connection creation
7171
// when we are updating the config. The config si already synced via the watch
7272
// channel.
73-
configs: Mutex<HashMap<NamespaceName, Sender<InnerConfig>>>,
74-
conn: Mutex<MetaStoreConnection>,
73+
configs: tokio::sync::Mutex<HashMap<NamespaceName, Sender<InnerConfig>>>,
74+
conn: tokio::sync::Mutex<MetaStoreConnection>,
7575
wal_manager: MetaStoreWalManager,
7676
}
7777

@@ -313,7 +313,7 @@ fn process(msg: ChangeMsg, inner: Arc<MetaStoreInner>) {
313313
} else {
314314
Ok(())
315315
};
316-
let mut configs = inner.configs.lock();
316+
let mut configs = inner.configs.blocking_lock();
317317
if let Some(config_watch) = configs.get_mut(&namespace) {
318318
let new_version = config_watch.borrow().version.wrapping_add(1);
319319

@@ -330,7 +330,7 @@ fn process(msg: ChangeMsg, inner: Arc<MetaStoreInner>) {
330330
let _ = ret_chan.send(ret);
331331
} else {
332332
let ret = if flush {
333-
let mut configs = inner.configs.lock();
333+
let mut configs = inner.configs.blocking_lock();
334334
if let Some(config_watch) = configs.get_mut(&namespace) {
335335
let config = config_watch.subscribe().borrow().clone();
336336
try_process(&inner, &namespace, &config.config)
@@ -351,7 +351,7 @@ fn try_process(
351351
) -> Result<()> {
352352
let config_encoded = metadata::DatabaseConfig::from(&*config).encode_to_vec();
353353

354-
let mut conn = inner.conn.lock();
354+
let mut conn = inner.conn.blocking_lock();
355355
if let Some(schema) = config.shared_schema_name.as_ref() {
356356
let tx = conn.transaction()?;
357357
if let Some(ref schema) = config.shared_schema_name {
@@ -470,11 +470,11 @@ impl MetaStore {
470470
Ok(Self { changes_tx, inner })
471471
}
472472

473-
pub fn handle(&self, namespace: NamespaceName) -> MetaStoreHandle {
473+
pub async fn handle(&self, namespace: NamespaceName) -> MetaStoreHandle {
474474
tracing::debug!("getting meta store handle");
475475
let change_tx = self.changes_tx.clone();
476476

477-
let mut configs = self.inner.configs.lock();
477+
let mut configs = self.inner.configs.lock().await;
478478
let sender = configs.entry(namespace.clone()).or_insert_with(|| {
479479
// TODO(lucio): if no entry exists we need to ensure we send the update to
480480
// the bg channel.
@@ -495,11 +495,11 @@ impl MetaStore {
495495
pub fn remove(&self, namespace: NamespaceName) -> Result<Option<Arc<DatabaseConfig>>> {
496496
tracing::debug!("removing namespace `{}` from meta store", namespace);
497497

498-
let mut configs = self.inner.configs.lock();
498+
let mut configs = self.inner.configs.blocking_lock();
499499
let r = if let Some(sender) = configs.get(&namespace) {
500500
tracing::debug!("removed namespace `{}` from meta store", namespace);
501501
let config = sender.borrow().clone();
502-
let mut conn = self.inner.conn.lock();
502+
let mut conn = self.inner.conn.blocking_lock();
503503
let tx = conn.transaction()?;
504504
if config.config.is_shared_schema {
505505
if crate::schema::db::schema_has_linked_dbs(&tx, &namespace)? {
@@ -535,8 +535,8 @@ impl MetaStore {
535535
// TODO: we need to either make sure that the metastore is restored
536536
// before we start accepting connections or we need to contact bottomless
537537
// here to check if a namespace exists. Preferably the former.
538-
pub fn exists(&self, namespace: &NamespaceName) -> bool {
539-
self.inner.configs.lock().contains_key(namespace)
538+
pub async fn exists(&self, namespace: &NamespaceName) -> bool {
539+
self.inner.configs.lock().await.contains_key(namespace)
540540
}
541541

542542
pub(crate) async fn shutdown(&self) -> crate::Result<()> {
@@ -559,7 +559,7 @@ impl MetaStore {
559559
) -> crate::Result<MigrationSummary> {
560560
let inner = self.inner.clone();
561561
let summary = tokio::task::spawn_blocking(move || {
562-
let mut conn = inner.conn.lock();
562+
let mut conn = inner.conn.blocking_lock();
563563
crate::schema::get_migrations_summary(&mut conn, schema)
564564
})
565565
.await
@@ -574,7 +574,7 @@ impl MetaStore {
574574
) -> crate::Result<Option<MigrationDetails>> {
575575
let inner = self.inner.clone();
576576
let details = tokio::task::spawn_blocking(move || {
577-
let mut conn = inner.conn.lock();
577+
let mut conn = inner.conn.blocking_lock();
578578
crate::schema::get_migration_details(&mut conn, schema, job_id)
579579
})
580580
.await

libsql-server/src/namespace/store.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ impl NamespaceStore {
9999
})
100100
}
101101

102-
pub fn exists(&self, namespace: &NamespaceName) -> bool {
103-
self.inner.metadata.exists(namespace)
102+
pub async fn exists(&self, namespace: &NamespaceName) -> bool {
103+
self.inner.metadata.exists(namespace).await
104104
}
105105

106106
pub async fn destroy(&self, namespace: NamespaceName, prune_all: bool) -> crate::Result<()> {
@@ -173,7 +173,7 @@ impl NamespaceStore {
173173
ns.destroy().await?;
174174
}
175175

176-
let db_config = self.inner.metadata.handle(namespace.clone());
176+
let db_config = self.inner.metadata.handle(namespace.clone()).await;
177177
// destroy on-disk database
178178
self.cleanup(
179179
&namespace,
@@ -226,7 +226,7 @@ impl NamespaceStore {
226226
}
227227

228228
// check that the source namespace exists
229-
if !self.inner.metadata.exists(&from) {
229+
if !self.inner.metadata.exists(&from).await {
230230
return Err(crate::error::Error::NamespaceDoesntExist(from.to_string()));
231231
}
232232

@@ -241,11 +241,11 @@ impl NamespaceStore {
241241
}
242242

243243
// FIXME: we could potentially delete the namespace while trying to fork it
244-
if !self.inner.metadata.exists(&from) {
244+
if !self.inner.metadata.exists(&from).await {
245245
return Err(crate::Error::NamespaceDoesntExist(from.to_string()));
246246
}
247247

248-
let from_config = self.inner.metadata.handle(from.clone());
248+
let from_config = self.inner.metadata.handle(from.clone()).await;
249249
let from_entry = self
250250
.load_namespace(&from, from_config.clone(), RestoreOption::Latest)
251251
.await?;
@@ -280,7 +280,7 @@ impl NamespaceStore {
280280
should_delete: true,
281281
};
282282

283-
let handle = self.inner.metadata.handle(to.clone());
283+
let handle = self.inner.metadata.handle(to.clone()).await;
284284
handle
285285
.store_and_maybe_flush(Some(to_config.into()), false)
286286
.await?;
@@ -328,7 +328,7 @@ impl NamespaceStore {
328328
Fun: FnOnce(&Namespace) -> R,
329329
{
330330
if namespace != NamespaceName::default()
331-
&& !self.inner.metadata.exists(&namespace)
331+
&& !self.inner.metadata.exists(&namespace).await
332332
&& !self.inner.allow_lazy_creation
333333
{
334334
return Err(Error::NamespaceDoesntExist(namespace.to_string()));
@@ -346,7 +346,7 @@ impl NamespaceStore {
346346
}
347347
};
348348

349-
let handle = self.inner.metadata.handle(namespace.to_owned());
349+
let handle = self.inner.metadata.handle(namespace.to_owned()).await;
350350
f(self
351351
.load_namespace(&namespace, handle, RestoreOption::Latest)
352352
.await?)
@@ -440,12 +440,12 @@ impl NamespaceStore {
440440
// FIXME: move the default namespace check out of this function.
441441
if self.inner.allow_lazy_creation || namespace == NamespaceName::default() {
442442
tracing::trace!("auto-creating the namespace");
443-
} else if self.inner.metadata.exists(&namespace) {
443+
} else if self.inner.metadata.exists(&namespace).await {
444444
return Err(Error::NamespaceAlreadyExist(namespace.to_string()));
445445
}
446446

447447
let db_config = Arc::new(db_config);
448-
let handle = self.inner.metadata.handle(namespace.clone());
448+
let handle = self.inner.metadata.handle(namespace.clone()).await;
449449
tracing::debug!("storing db config");
450450
handle.store(db_config).await?;
451451
tracing::debug!("completed storing db config, loading namespace");

libsql-server/src/schema/db.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ mod test {
482482
async fn register_schema(meta_store: &MetaStore, schema: &'static str) {
483483
meta_store
484484
.handle(schema.into())
485+
.await
485486
.store(DatabaseConfig {
486487
is_shared_schema: true,
487488
..Default::default()
@@ -497,6 +498,7 @@ mod test {
497498
) -> crate::Result<()> {
498499
meta_store
499500
.handle(name.into())
501+
.await
500502
.store(DatabaseConfig {
501503
shared_schema_name: Some(schema.into()),
502504
..Default::default()
@@ -561,6 +563,7 @@ mod test {
561563
// necessary checks beforehand, and return a nice error message.
562564
assert!(meta_store
563565
.handle("ns1".into())
566+
.await
564567
.store(DatabaseConfig {
565568
shared_schema_name: Some("schema1".into()),
566569
..Default::default()

0 commit comments

Comments
 (0)