@@ -676,6 +676,12 @@ impl Replicator {
676
676
gzip_path. join ( "db.gz" )
677
677
}
678
678
679
+ fn restore_db_path ( & self ) -> PathBuf {
680
+ let mut gzip_path = PathBuf :: from ( & self . db_path ) ;
681
+ gzip_path. pop ( ) ;
682
+ gzip_path. join ( "data.tmp" )
683
+ }
684
+
679
685
// Replicates local WAL pages to S3, if local WAL is present.
680
686
// This function is called under the assumption that if local WAL
681
687
// file is present, it was already detected to be newer than its
@@ -976,35 +982,40 @@ impl Replicator {
976
982
977
983
// at this point we know, we should do a full restore
978
984
979
- let backup_path = format ! ( "{}.bottomless.backup" , self . db_path) ;
980
- tokio:: fs:: rename ( & self . db_path , & backup_path) . await . ok ( ) ; // Best effort
981
- match self . full_restore ( generation, timestamp, last_frame) . await {
985
+ let restore_path = self . restore_db_path ( ) ;
986
+ let _ = tokio:: fs:: remove_file ( & restore_path) . await ; // remove previous (failed) restoration
987
+ match self
988
+ . full_restore ( & restore_path, generation, timestamp, last_frame)
989
+ . await
990
+ {
982
991
Ok ( result) => {
983
992
let elapsed = Instant :: now ( ) - start_ts;
984
993
tracing:: info!( "Finished database restoration in {:?}" , elapsed) ;
985
- tokio:: fs:: remove_file ( backup_path) . await . ok ( ) ;
994
+ tokio:: fs:: rename ( & restore_path, & self . db_path ) . await ?;
995
+ let _ = self . remove_wal_files ( ) . await ; // best effort, WAL files may not exists
986
996
Ok ( result)
987
997
}
988
998
Err ( e) => {
989
999
tracing:: error!( "failed to restore the database: {}. Rollback" , e) ;
990
- tokio:: fs:: rename ( & backup_path , & self . db_path ) . await . ok ( ) ;
1000
+ let _ = tokio:: fs:: remove_file ( restore_path ) . await ;
991
1001
Err ( e)
992
1002
}
993
1003
}
994
1004
}
995
1005
996
1006
async fn full_restore (
997
1007
& mut self ,
1008
+ restore_path : & Path ,
998
1009
generation : Uuid ,
999
1010
timestamp : Option < NaiveDateTime > ,
1000
1011
last_frame : u32 ,
1001
1012
) -> Result < ( RestoreAction , bool ) > {
1002
- let _ = self . remove_wal_files ( ) . await ; // best effort, WAL files may not exists
1013
+ tracing :: debug! ( "Restoring database to `{}`" , restore_path . display ( ) ) ;
1003
1014
let mut db = OpenOptions :: new ( )
1004
1015
. create ( true )
1005
1016
. read ( true )
1006
1017
. write ( true )
1007
- . open ( & self . db_path )
1018
+ . open ( restore_path )
1008
1019
. await ?;
1009
1020
1010
1021
let mut restore_stack = Vec :: new ( ) ;
0 commit comments