@@ -9,6 +9,8 @@ use bytes::Bytes;
99use enclose:: enclose;
1010use futures:: Stream ;
1111use libsql_sys:: EncryptionConfig ;
12+ use libsql_wal:: io:: StdIO ;
13+ use libsql_wal:: registry:: WalRegistry ;
1214use tokio:: io:: AsyncBufReadExt as _;
1315use tokio:: sync:: watch;
1416use tokio:: task:: JoinSet ;
@@ -29,7 +31,7 @@ use crate::namespace::{
2931} ;
3032use crate :: replication:: { FrameNo , ReplicationLogger } ;
3133use 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
3436use 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