Skip to content

Commit 0eeaf96

Browse files
committed
add cleanup_libsql helper function
1 parent ab0d9ab commit 0eeaf96

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

  • libsql-server/src/namespace/configurator

libsql-server/src/namespace/configurator/helpers.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use bytes::Bytes;
99
use enclose::enclose;
1010
use futures::Stream;
1111
use libsql_sys::EncryptionConfig;
12+
use libsql_wal::io::StdIO;
13+
use libsql_wal::registry::WalRegistry;
1214
use tokio::io::AsyncBufReadExt as _;
1315
use tokio::sync::watch;
1416
use tokio::task::JoinSet;
@@ -29,7 +31,7 @@ use crate::namespace::{
2931
};
3032
use crate::replication::{FrameNo, ReplicationLogger};
3133
use crate::stats::Stats;
32-
use crate::{StatsSender, BLOCKING_RT, DB_CREATE_TIMEOUT, DEFAULT_AUTO_CHECKPOINT};
34+
use crate::{SqldStorage, StatsSender, BLOCKING_RT, DB_CREATE_TIMEOUT, DEFAULT_AUTO_CHECKPOINT};
3335

3436
use super::{BaseNamespaceConfig, PrimaryConfig};
3537

@@ -464,3 +466,53 @@ pub(super) async fn cleanup_primary(
464466

465467
Ok(())
466468
}
469+
470+
pub async fn cleanup_libsql(
471+
namespace: &NamespaceName,
472+
registry: &WalRegistry<StdIO, SqldStorage>,
473+
base_path: &Path,
474+
) -> crate::Result<()> {
475+
let namespace = namespace.clone().into();
476+
if let Some(shared) = registry.tombstone(&namespace).await {
477+
// shutdown the registry, don't seal the current segment so that it's not
478+
tokio::task::spawn_blocking({
479+
let shared = shared.clone();
480+
move || shared.shutdown()
481+
})
482+
.await
483+
.unwrap()?;
484+
}
485+
486+
let ns_db_path = base_path.join("dbs").join(namespace.as_str());
487+
if ns_db_path.try_exists()? {
488+
tracing::debug!("removing database directory: {}", ns_db_path.display());
489+
let _ = tokio::fs::remove_dir_all(ns_db_path).await;
490+
}
491+
492+
let ns_wals_path = base_path.join("wals").join(namespace.as_str());
493+
if ns_wals_path.try_exists()? {
494+
tracing::debug!("removing database directory: {}", ns_wals_path.display());
495+
if let Err(e) = tokio::fs::remove_dir_all(ns_wals_path).await {
496+
// what can go wrong?:
497+
match e.kind() {
498+
// alright, there's nothing to delete anyway
499+
std::io::ErrorKind::NotFound => (),
500+
_ => {
501+
// something unexpected happened, this namespaces is in a bad state.
502+
// The entry will not be removed from the registry to prevent another
503+
// namespace with the same name to be reuse the same wal files. a
504+
// manual intervention is necessary
505+
// FIXME: on namespace creation, we could ensure that this directory is
506+
// clean.
507+
tracing::error!("error deleting `{namespace}` wal directory, manual intervention may be necessary: {e}");
508+
return Err(e.into());
509+
}
510+
}
511+
}
512+
}
513+
514+
// when all is cleaned, leave place for next one
515+
registry.remove(&namespace).await;
516+
517+
Ok(())
518+
}

0 commit comments

Comments
 (0)