2007-11-29 14:18:54 +00:00
/* -*- mode: C; c-basic-offset: 4 -*- */
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-16 23:57:48 -04:00
# ident "Copyright (c) 2007, 2008, 2009 Tokutek Inc. All rights reserved."
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
2007-11-29 14:18:54 +00:00
2013-04-16 23:57:20 -04:00
# include "includes.h"
2013-04-16 23:57:59 -04:00
# include "log_header.h"
2013-04-16 23:58:01 -04:00
# include "checkpoint.h"
2007-11-23 20:36:03 +00:00
2013-04-16 23:58:04 -04:00
int toku_recover_trace = 0 ;
2013-04-16 23:57:59 -04:00
2008-04-07 01:30:25 +00:00
//#define DO_VERIFY_COUNTS
2008-03-14 19:14:31 +00:00
# ifdef DO_VERIFY_COUNTS
# define VERIFY_COUNTS(n) toku_verify_counts(n)
# else
# define VERIFY_COUNTS(n) ((void)0)
# endif
2013-04-16 23:57:59 -04:00
struct backward_scan_state {
enum backward_state { BS_INIT , BS_SAW_CKPT_END , BS_SAW_CKPT } bs ;
LSN checkpoint_lsn ;
int n_live_txns ;
TXNID min_live_txn ;
} ;
static void backward_scan_state_init ( struct backward_scan_state * bs ) {
bs - > bs = BS_INIT ; bs - > checkpoint_lsn = ZERO_LSN ; bs - > n_live_txns = 0 ; bs - > min_live_txn = 0 ;
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:02 -04:00
// File map tuple
struct file_map_tuple {
FILENUM filenum ;
BRT brt ;
char * fname ;
} ;
static void file_map_tuple_init ( struct file_map_tuple * tuple , FILENUM filenum , BRT brt , char * fname ) {
tuple - > filenum = filenum ;
tuple - > brt = brt ;
tuple - > fname = fname ;
}
static void file_map_tuple_destroy ( struct file_map_tuple * tuple ) {
if ( tuple - > fname ) {
toku_free ( tuple - > fname ) ;
tuple - > fname = NULL ;
}
}
// Map filenum to brt, fname
2013-04-16 23:57:59 -04:00
// TODO why can't we use the cachetable to find by filenum?
2013-04-16 23:58:00 -04:00
struct file_map {
2013-04-16 23:58:02 -04:00
OMT filenums ;
2013-04-16 23:57:59 -04:00
} ;
2013-04-16 23:58:00 -04:00
static void file_map_init ( struct file_map * fmap ) {
2013-04-16 23:58:02 -04:00
int r = toku_omt_create ( & fmap - > filenums ) ;
assert ( r = = 0 ) ;
}
static void file_map_destroy ( struct file_map * fmap ) {
toku_omt_destroy ( & fmap - > filenums ) ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:03 -04:00
static void file_map_close_dictionaries ( struct file_map * fmap , BOOL recovery_succeeded ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:02 -04:00
while ( 1 ) {
u_int32_t n = toku_omt_size ( fmap - > filenums ) ;
if ( n = = 0 )
break ;
OMTVALUE v ;
r = toku_omt_fetch ( fmap - > filenums , n - 1 , & v , NULL ) ;
assert ( r = = 0 ) ;
r = toku_omt_delete_at ( fmap - > filenums , n - 1 ) ;
assert ( r = = 0 ) ;
struct file_map_tuple * tuple = v ;
assert ( tuple - > brt ) ;
2013-04-16 23:58:03 -04:00
if ( ! recovery_succeeded ) {
// don't update the brt on close
toku_brt_set_panic ( tuple - > brt , DB_RUNRECOVERY , " recovery failed " ) ;
}
2013-04-16 23:58:02 -04:00
//Logging is already back on. No need to pass LSN into close.
2013-04-16 23:58:03 -04:00
char * error_string = NULL ;
2013-04-16 23:58:05 -04:00
DB * fake_db = tuple - > brt - > db ; //Need to free the fake db that was malloced
2013-04-16 23:58:06 -04:00
r = toku_close_brt ( tuple - > brt , & error_string ) ;
2013-04-16 23:58:03 -04:00
if ( ! recovery_succeeded ) {
printf ( " %s:%d %d %s \n " , __FUNCTION__ , __LINE__ , r , error_string ) ;
assert ( r ! = 0 ) ;
} else
assert ( r = = 0 ) ;
if ( error_string )
toku_free ( error_string ) ;
2013-04-16 23:58:05 -04:00
toku_free ( fake_db ) ; //Must free the DB after the brt is closed
2013-04-16 23:58:03 -04:00
2013-04-16 23:58:02 -04:00
file_map_tuple_destroy ( tuple ) ;
toku_free ( tuple ) ;
2013-04-16 23:57:59 -04:00
}
}
2013-04-16 23:58:02 -04:00
static int file_map_h ( OMTVALUE omtv , void * v ) {
struct file_map_tuple * a = omtv ;
FILENUM * b = v ;
if ( a - > filenum . fileid < b - > fileid ) return - 1 ;
if ( a - > filenum . fileid > b - > fileid ) return + 1 ;
2013-04-16 23:57:59 -04:00
return 0 ;
}
2013-04-16 23:58:02 -04:00
static int file_map_insert ( struct file_map * fmap , FILENUM fnum , BRT brt , char * fname ) {
struct file_map_tuple * tuple = toku_malloc ( sizeof ( struct file_map_tuple ) ) ;
assert ( tuple ) ;
file_map_tuple_init ( tuple , fnum , brt , fname ) ;
int r = toku_omt_insert ( fmap - > filenums , tuple , file_map_h , & fnum , NULL ) ;
return r ;
}
static void file_map_remove ( struct file_map * fmap , FILENUM fnum ) {
OMTVALUE v ; u_int32_t idx ;
int r = toku_omt_find_zero ( fmap - > filenums , file_map_h , & fnum , & v , & idx , NULL ) ;
if ( r = = 0 ) {
struct file_map_tuple * tuple = v ;
r = toku_omt_delete_at ( fmap - > filenums , idx ) ;
file_map_tuple_destroy ( tuple ) ;
toku_free ( tuple ) ;
}
}
static int file_map_find ( struct file_map * fmap , FILENUM fnum , struct file_map_tuple * * file_map_tuple ) {
OMTVALUE v ; u_int32_t idx ;
int r = toku_omt_find_zero ( fmap - > filenums , file_map_h , & fnum , & v , & idx , NULL ) ;
if ( r = = 0 ) {
struct file_map_tuple * tuple = v ;
assert ( tuple - > filenum . fileid = = fnum . fileid ) ;
* file_map_tuple = tuple ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:02 -04:00
return r ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:02 -04:00
// The recovery environment
2013-04-16 23:57:59 -04:00
struct recover_env {
CACHETABLE ct ;
TOKULOGGER logger ;
brt_compare_func bt_compare ;
brt_compare_func dup_compare ;
struct backward_scan_state bs ;
2013-04-16 23:58:00 -04:00
struct file_map fmap ;
2013-04-16 23:58:03 -04:00
BOOL goforward ;
2013-04-16 23:57:59 -04:00
} ;
typedef struct recover_env * RECOVER_ENV ;
2008-03-14 19:14:31 +00:00
2013-04-16 23:58:01 -04:00
static int recover_env_init ( RECOVER_ENV renv , brt_compare_func bt_compare , brt_compare_func dup_compare ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:01 -04:00
r = toku_create_cachetable ( & renv - > ct , 1 < < 25 , ( LSN ) { 0 } , 0 ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:01 -04:00
r = toku_logger_create ( & renv - > logger ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:01 -04:00
toku_logger_write_log_files ( renv - > logger , FALSE ) ;
toku_logger_set_cachetable ( renv - > logger , renv - > ct ) ;
renv - > bt_compare = bt_compare ;
renv - > dup_compare = dup_compare ;
file_map_init ( & renv - > fmap ) ;
2013-04-16 23:58:03 -04:00
renv - > goforward = FALSE ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:57:59 -04:00
if ( toku_recover_trace )
printf ( " %s:%d \n " , __FUNCTION__ , __LINE__ ) ;
2008-03-14 19:14:31 +00:00
return r ;
}
2013-04-16 23:58:03 -04:00
static void recover_env_cleanup ( RECOVER_ENV renv , BOOL recovery_succeeded ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:03 -04:00
file_map_close_dictionaries ( & renv - > fmap , recovery_succeeded ) ;
2013-04-16 23:58:02 -04:00
file_map_destroy ( & renv - > fmap ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:01 -04:00
r = toku_logger_close ( & renv - > logger ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:01 -04:00
r = toku_cachetable_close ( & renv - > ct ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
if ( toku_recover_trace )
printf ( " %s:%d \n " , __FUNCTION__ , __LINE__ ) ;
2013-04-16 23:57:59 -04:00
}
// Null function supplied to transaction commit and abort
static void recover_yield ( voidfp UU ( f ) , void * UU ( extra ) ) {
// nothing
2008-03-14 19:14:31 +00:00
}
2013-04-16 23:58:03 -04:00
static int toku_recover_commit ( struct logtype_commit * l , RECOVER_ENV renv ) {
2013-04-16 23:57:59 -04:00
int r ;
// find the transaction by transaction id
2013-04-16 23:58:01 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > txnid , & txn ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( txn = = NULL )
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:59 -04:00
// commit the transaction
2013-04-16 23:58:03 -04:00
r = toku_txn_commit_with_lsn ( txn , TRUE , recover_yield , NULL , l - > lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:57:53 -04:00
2013-04-16 23:57:59 -04:00
// close the transaction
toku_txn_close_txn ( txn ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2008-03-14 19:14:31 +00:00
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_commit ( struct logtype_commit * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_xabort ( struct logtype_xabort * l , RECOVER_ENV renv ) {
2013-04-16 23:57:59 -04:00
int r ;
// find the transaction by transaction id
2013-04-16 23:58:01 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > txnid , & txn ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( txn = = NULL )
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:59 -04:00
// abort the transaction
2013-04-16 23:58:03 -04:00
r = toku_txn_abort_with_lsn ( txn , recover_yield , NULL , l - > lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
// close the transaction
toku_txn_close_txn ( txn ) ;
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_backward_xabort ( struct logtype_xabort * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:01 -04:00
// nothing
return 0 ;
}
2013-04-16 23:57:59 -04:00
static void create_dir_from_file ( const char * fname ) {
2008-04-18 14:45:04 +00:00
char * tmp = toku_strdup ( fname ) ;
char ch ;
2013-04-16 23:58:00 -04:00
for ( int i = 0 ; ( ch = fname [ i ] ) ; i + + ) {
2013-04-16 23:57:29 -04:00
//
// TODO: this may fail in windows, double check the absolute path names
// and '/' as the directory delimiter or something
//
2008-04-18 14:45:04 +00:00
if ( ch = = ' / ' ) {
if ( i > 0 ) {
tmp [ i ] = 0 ;
mode_t oldu = umask ( 0 ) ;
2013-04-16 23:57:28 -04:00
int r = toku_os_mkdir ( tmp , S_IRWXU ) ;
2008-04-18 14:45:04 +00:00
if ( r ! = 0 & & errno ! = EEXIST ) {
printf ( " error: %s \n " , strerror ( errno ) ) ;
}
2013-04-16 23:57:59 -04:00
assert ( r = = 0 | | ( errno = = EEXIST ) ) ;
2008-04-18 14:45:04 +00:00
umask ( oldu ) ;
tmp [ i ] = ch ;
}
}
}
toku_free ( tmp ) ;
}
2013-04-16 23:58:05 -04:00
static int
abort_on_upgrade ( DB * UU ( pdb ) ,
u_int32_t UU ( old_version ) , const DBT * UU ( old_descriptor ) , const DBT * UU ( old_key ) , const DBT * UU ( old_val ) ,
u_int32_t UU ( new_version ) , const DBT * UU ( new_descriptor ) , const DBT * UU ( new_key ) , const DBT * UU ( new_val ) ) {
assert ( FALSE ) ; //Must not upgrade.
return ENOSYS ;
}
2013-04-16 23:57:53 -04:00
// Open the file if it is not already open. If it is already open, then do nothing.
2013-04-16 23:58:05 -04:00
static int internal_toku_recover_fopen_or_fcreate ( RECOVER_ENV renv , int flags , int mode , char * fixedfname , FILENUM filenum , u_int32_t treeflags , u_int32_t descriptor_version , BYTESTRING * descriptor ) {
2013-04-16 23:58:00 -04:00
int r ;
// already open
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , filenum , & tuple ) ;
2013-04-16 23:58:00 -04:00
if ( r = = 0 ) {
2013-04-16 23:58:01 -04:00
assert ( strcmp ( tuple - > fname , fixedfname ) = = 0 ) ;
2013-04-16 23:58:00 -04:00
toku_free ( fixedfname ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:53 -04:00
}
2013-04-16 23:58:00 -04:00
if ( flags & O_TRUNC ) {
// maybe unlink
r = unlink ( fixedfname ) ;
if ( r ! = 0 )
printf ( " %s:%d unlink %d \n " , __FUNCTION__ , __LINE__ , errno ) ;
2013-04-16 23:57:53 -04:00
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:57:44 -04:00
BRT brt = 0 ;
2013-04-16 23:58:00 -04:00
r = toku_brt_create ( & brt ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:04 -04:00
toku_brt_set_flags ( brt , treeflags ) ;
2013-04-16 23:57:59 -04:00
// set the key compare functions
2013-04-16 23:58:04 -04:00
if ( ! ( treeflags & TOKU_DB_KEYCMP_BUILTIN ) & & renv - > bt_compare )
2013-04-16 23:58:01 -04:00
toku_brt_set_bt_compare ( brt , renv - > bt_compare ) ;
2013-04-16 23:58:04 -04:00
if ( ! ( treeflags & TOKU_DB_VALCMP_BUILTIN ) & & renv - > dup_compare )
2013-04-16 23:58:01 -04:00
toku_brt_set_dup_compare ( brt , renv - > dup_compare ) ;
// bind to filenum when opened
toku_brt_set_filenum ( brt , filenum ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:06 -04:00
// TODO mode (FUTURE FEATURE)
2013-04-16 23:58:00 -04:00
mode = mode ;
2013-04-16 23:58:05 -04:00
//Create fake DB for comparison functions.
DB * XCALLOC ( fake_db ) ;
2013-04-16 23:58:05 -04:00
if ( flags & O_CREAT & & descriptor_version > 0 ) {
DBT descriptor_dbt ;
toku_fill_dbt ( & descriptor_dbt , descriptor - > data , descriptor - > len ) ;
r = toku_brt_set_descriptor ( brt , descriptor_version , & descriptor_dbt , abort_on_upgrade ) ;
if ( r ! = 0 ) goto close_brt ;
}
2013-04-16 23:58:52 -04:00
r = toku_brt_open_recovery ( brt , fixedfname , fixedfname , ( flags & O_CREAT ) ! = 0 , FALSE , renv - > ct , NULL , fake_db , ( flags & O_CREAT ) ) ;
2013-04-16 23:58:03 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:05 -04:00
close_brt : ;
2013-04-16 23:58:03 -04:00
//Note: If brt_open fails, then close_brt will NOT write a header to disk.
//No need to provide lsn
2013-04-16 23:58:06 -04:00
int rr = toku_close_brt ( brt , NULL ) ; assert ( rr = = 0 ) ;
2013-04-16 23:58:03 -04:00
toku_free ( fixedfname ) ;
2013-04-16 23:58:05 -04:00
toku_free ( fake_db ) ; //Free memory allocated for the fake db.
2013-04-16 23:58:06 -04:00
if ( r = = ENOENT ) //Not an error to simply be missing.
r = 0 ;
2013-04-16 23:58:03 -04:00
return r ;
}
2013-04-16 23:58:00 -04:00
2013-04-16 23:58:02 -04:00
file_map_insert ( & renv - > fmap , filenum , brt , fixedfname ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:44 -04:00
}
2013-04-16 23:58:03 -04:00
static int toku_recover_fopen ( struct logtype_fopen * l , RECOVER_ENV renv ) {
2013-04-16 23:58:04 -04:00
char * fixedfname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:58:05 -04:00
return internal_toku_recover_fopen_or_fcreate ( renv , 0 , 0 , fixedfname , l - > filenum , l - > treeflags , 0 , NULL ) ;
2013-04-16 23:57:44 -04:00
}
2013-04-16 23:58:06 -04:00
static int
maybe_do_fclose_during_recover_backward ( RECOVER_ENV renv , FILENUM filenum ) {
2013-04-16 23:58:01 -04:00
if ( renv - > bs . bs = = BS_SAW_CKPT_END ) {
2013-04-16 23:58:00 -04:00
// close the tree
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:06 -04:00
int r = file_map_find ( & renv - > fmap , filenum , & tuple ) ;
2013-04-16 23:58:00 -04:00
if ( r = = 0 ) {
2013-04-16 23:58:03 -04:00
//Must keep existing lsn.
//The only way this should be dirty, is if its doing a file-format upgrade.
//If not dirty, header will not be written.
2013-04-16 23:58:05 -04:00
DB * fake_db = tuple - > brt - > db ; //Need to free the fake db that was malloced
2013-04-16 23:58:06 -04:00
r = toku_close_brt_lsn ( tuple - > brt , 0 , TRUE , tuple - > brt - > h - > checkpoint_lsn ) ;
2013-04-16 23:58:00 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:05 -04:00
toku_free ( fake_db ) ; //Must free the DB after the brt is closed
2013-04-16 23:58:06 -04:00
file_map_remove ( & renv - > fmap , filenum ) ;
2013-04-16 23:58:00 -04:00
}
}
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:06 -04:00
static int toku_recover_backward_fopen ( struct logtype_fopen * l , RECOVER_ENV renv ) {
int r = maybe_do_fclose_during_recover_backward ( renv , l - > filenum ) ;
assert ( r = = 0 ) ;
return 0 ;
}
2013-04-16 23:57:44 -04:00
// fcreate is like fopen except that the file must be created. Also creates the dir if needed.
2013-04-16 23:58:03 -04:00
static int toku_recover_fcreate ( struct logtype_fcreate * l , RECOVER_ENV renv ) {
2013-04-16 23:58:04 -04:00
char * fixedfname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:57:44 -04:00
create_dir_from_file ( fixedfname ) ;
2013-04-16 23:58:05 -04:00
return internal_toku_recover_fopen_or_fcreate ( renv , O_CREAT | O_TRUNC , l - > mode , fixedfname , l - > filenum , l - > treeflags , l - > descriptor_version , & l - > descriptor ) ;
2013-04-16 23:57:44 -04:00
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_fcreate ( struct logtype_fcreate * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:06 -04:00
int r = maybe_do_fclose_during_recover_backward ( renv , l - > filenum ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:57:56 -04:00
2013-04-16 23:58:04 -04:00
// fdelete is a transactional file delete.
2013-04-16 23:58:06 -04:00
static int toku_recover_fdelete ( struct logtype_fdelete * l , RECOVER_ENV renv ) {
TOKUTXN txn = NULL ;
int r = toku_txnid2txn ( renv - > logger , l - > txnid , & txn ) ;
assert ( r = = 0 ) ;
//TODO: #??? (insert ticket number) bug that could leave orphaned files here.
if ( txn = = NULL )
return 0 ;
char * fixediname = fixup_fname ( & l - > iname ) ;
{ //Skip if does not exist.
toku_struct_stat buf ;
r = toku_stat ( fixediname , & buf ) ;
if ( r = = - 1 & & errno = = ENOENT )
goto cleanup ;
}
DBT iname_dbt ;
toku_fill_dbt ( & iname_dbt , fixediname , strlen ( fixediname ) + 1 ) ;
r = toku_brt_remove_on_commit ( txn , & iname_dbt , & iname_dbt ) ;
assert ( r = = 0 ) ;
cleanup :
toku_free ( fixediname ) ;
return 0 ;
2013-04-16 23:58:04 -04:00
}
static int toku_recover_backward_fdelete ( struct logtype_fdelete * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:06 -04:00
// nothing
return 0 ;
2013-04-16 23:58:04 -04:00
}
2013-04-16 23:58:03 -04:00
static int toku_recover_enq_insert ( struct logtype_enq_insert * l , RECOVER_ENV renv ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:03 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:57:56 -04:00
if ( r ! = 0 ) {
// if we didn't find a cachefile, then we don't have to do anything.
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:58:01 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( txn = = NULL )
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
DBT keydbt , valdbt ;
2013-04-16 23:58:03 -04:00
toku_fill_dbt ( & keydbt , l - > key . data , l - > key . len ) ;
toku_fill_dbt ( & valdbt , l - > value . data , l - > value . len ) ;
r = toku_brt_maybe_insert ( tuple - > brt , & keydbt , & valdbt , txn , TRUE , l - > lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
}
static int toku_recover_backward_enq_insert ( struct logtype_enq_insert * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:57:53 -04:00
2013-04-16 23:58:03 -04:00
static int toku_recover_tablelock_on_empty_table ( struct logtype_tablelock_on_empty_table * l , RECOVER_ENV renv ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:03 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:58:01 -04:00
if ( r ! = 0 ) {
// if we didn't find a cachefile, then we don't have to do anything.
return 0 ;
}
2013-04-16 23:58:02 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:01 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:02 -04:00
if ( txn ! = NULL ) {
r = toku_brt_note_table_lock ( tuple - > brt , txn ) ;
assert ( r = = 0 ) ;
}
2013-04-16 23:58:01 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_backward_tablelock_on_empty_table ( struct logtype_tablelock_on_empty_table * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_enq_delete_both ( struct logtype_enq_delete_both * l , RECOVER_ENV renv ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:03 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:57:56 -04:00
if ( r ! = 0 ) {
// if we didn't find a cachefile, then we don't have to do anything.
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:58:01 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( txn = = NULL )
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
DBT keydbt , valdbt ;
2013-04-16 23:58:03 -04:00
toku_fill_dbt ( & keydbt , l - > key . data , l - > key . len ) ;
toku_fill_dbt ( & valdbt , l - > value . data , l - > value . len ) ;
r = toku_brt_maybe_delete_both ( tuple - > brt , & keydbt , & valdbt , txn , TRUE , l - > lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_enq_delete_both ( struct logtype_enq_delete_both * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_enq_delete_any ( struct logtype_enq_delete_any * l , RECOVER_ENV renv ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:03 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:57:56 -04:00
if ( r ! = 0 ) {
// if we didn't find a cachefile, then we don't have to do anything.
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:58:02 -04:00
}
2013-04-16 23:58:01 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( txn = = NULL )
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:59 -04:00
DBT keydbt ;
2013-04-16 23:58:03 -04:00
toku_fill_dbt ( & keydbt , l - > key . data , l - > key . len ) ;
r = toku_brt_maybe_delete ( tuple - > brt , & keydbt , txn , TRUE , l - > lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_enq_delete_any ( struct logtype_enq_delete_any * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:57:53 -04:00
2013-04-16 23:58:03 -04:00
static int toku_recover_fclose ( struct logtype_fclose * l , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:03 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:58:00 -04:00
if ( r = = 0 ) {
2013-04-16 23:58:04 -04:00
char * fixedfname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:58:01 -04:00
assert ( strcmp ( tuple - > fname , fixedfname ) = = 0 ) ;
toku_free ( fixedfname ) ;
2013-04-16 23:58:05 -04:00
DB * fake_db = tuple - > brt - > db ; //Need to free the fake db that was malloced
2013-04-16 23:58:06 -04:00
r = toku_close_brt_lsn ( tuple - > brt , 0 , TRUE , l - > lsn ) ;
2013-04-16 23:58:00 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:05 -04:00
toku_free ( fake_db ) ; //Must free the DB after the brt is closed
2013-04-16 23:58:03 -04:00
file_map_remove ( & renv - > fmap , l - > filenum ) ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:58:03 -04:00
return 0 ;
2008-04-17 03:11:55 +00:00
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_fclose ( struct logtype_fclose * l , RECOVER_ENV renv ) {
if ( renv - > bs . bs = = BS_SAW_CKPT ) {
2013-04-16 23:58:00 -04:00
// tree open
2013-04-16 23:58:04 -04:00
char * fixedfname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:58:05 -04:00
internal_toku_recover_fopen_or_fcreate ( renv , 0 , 0 , fixedfname , l - > filenum , l - > treeflags , 0 , NULL ) ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_begin_checkpoint ( struct logtype_begin_checkpoint * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:47 -04:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_begin_checkpoint ( struct logtype_begin_checkpoint * l , RECOVER_ENV renv ) {
struct backward_scan_state * bs = & renv - > bs ;
2013-04-16 23:57:53 -04:00
switch ( bs - > bs ) {
case BS_INIT :
return 0 ; // incomplete checkpoint
case BS_SAW_CKPT_END :
assert ( bs - > checkpoint_lsn . lsn = = l - > lsn . lsn ) ;
bs - > bs = BS_SAW_CKPT ;
if ( bs - > n_live_txns = = 0 ) {
fprintf ( stderr , " Turning around at begin_checkpoint % " PRIu64 " \n " , l - > lsn . lsn ) ;
2013-04-16 23:58:03 -04:00
renv - > goforward = TRUE ;
return 0 ;
2013-04-16 23:58:00 -04:00
} else
return 0 ;
2013-04-16 23:57:53 -04:00
case BS_SAW_CKPT :
return 0 ; // ignore it
}
2013-04-16 23:57:54 -04:00
fprintf ( stderr , " %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) bs - > bs ) ;
2013-04-16 23:57:53 -04:00
abort ( ) ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_end_checkpoint ( struct logtype_end_checkpoint * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:47 -04:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_end_checkpoint ( struct logtype_end_checkpoint * l , RECOVER_ENV renv ) {
struct backward_scan_state * bs = & renv - > bs ;
2013-04-16 23:57:53 -04:00
switch ( bs - > bs ) {
case BS_INIT :
bs - > bs = BS_SAW_CKPT_END ;
bs - > checkpoint_lsn . lsn = l - > txnid ;
return 0 ;
case BS_SAW_CKPT_END :
fprintf ( stderr , " %s:%d Should not see two end_checkpoint log entries without an intervening begin_checkpoint \n " , __FILE__ , __LINE__ ) ;
abort ( ) ;
case BS_SAW_CKPT :
return 0 ;
}
2013-04-16 23:57:54 -04:00
fprintf ( stderr , " %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) bs - > bs ) ;
2013-04-16 23:57:53 -04:00
abort ( ) ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_fassociate ( struct logtype_fassociate * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_fassociate ( struct logtype_fassociate * l , RECOVER_ENV renv ) {
2013-04-16 23:58:04 -04:00
char * fixedfname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:58:05 -04:00
return internal_toku_recover_fopen_or_fcreate ( renv , 0 , 0 , fixedfname , l - > filenum , l - > treeflags , 0 , NULL ) ;
2013-04-16 23:57:47 -04:00
}
2013-04-16 23:58:03 -04:00
static int toku_recover_xstillopen ( struct logtype_xstillopen * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2008-03-21 19:40:32 +00:00
return 0 ;
}
2008-03-14 19:14:31 +00:00
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_xstillopen ( struct logtype_xstillopen * l , RECOVER_ENV renv ) {
struct backward_scan_state * bs = & renv - > bs ;
2013-04-16 23:57:53 -04:00
switch ( bs - > bs ) {
case BS_INIT :
return 0 ; // ignore live txns from incomplete checkpoint
case BS_SAW_CKPT_END :
2013-04-16 23:58:00 -04:00
if ( bs - > n_live_txns = = 0 )
2013-04-16 23:57:59 -04:00
bs - > min_live_txn = l - > txnid ;
2013-04-16 23:58:00 -04:00
else if ( toku_txnid_older ( l - > txnid , bs - > min_live_txn ) )
2013-04-16 23:58:00 -04:00
bs - > min_live_txn = l - > txnid ;
2013-04-16 23:57:53 -04:00
bs - > n_live_txns + + ;
return 0 ;
case BS_SAW_CKPT :
return 0 ; // ignore live txns from older checkpoints
}
2013-04-16 23:57:54 -04:00
fprintf ( stderr , " %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) bs - > bs ) ;
2013-04-16 23:57:53 -04:00
abort ( ) ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_xbegin ( struct logtype_xbegin * l , RECOVER_ENV renv ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:57:59 -04:00
// lookup the parent
2013-04-16 23:57:59 -04:00
TOKUTXN parent = NULL ;
2013-04-16 23:58:03 -04:00
if ( l - > parenttxnid ! = 0 ) {
r = toku_txnid2txn ( renv - > logger , l - > parenttxnid , & parent ) ;
2013-04-16 23:58:02 -04:00
assert ( r = = 0 ) ;
if ( parent = = NULL )
return 0 ;
2013-04-16 23:58:01 -04:00
}
2013-04-16 23:57:53 -04:00
2013-04-16 23:57:59 -04:00
// create a transaction and bind it to the transaction id
2013-04-16 23:57:59 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:58:03 -04:00
r = toku_txn_begin_with_xid ( parent , & txn , renv - > logger , l - > lsn . lsn ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2008-03-21 21:02:30 +00:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_xbegin ( struct logtype_xbegin * l , RECOVER_ENV renv ) {
struct backward_scan_state * bs = & renv - > bs ;
2013-04-16 23:57:53 -04:00
switch ( bs - > bs ) {
case BS_INIT :
return 0 ; // ignore txns that began after checkpoint
case BS_SAW_CKPT_END :
return 0 ; // ignore txns that began during the checkpoint
case BS_SAW_CKPT :
assert ( bs - > n_live_txns > 0 ) ; // the only thing we are doing here is looking for a live txn, so there better be one
// If we got to the min, return nonzero
2013-04-16 23:57:59 -04:00
if ( bs - > min_live_txn > = l - > lsn . lsn ) {
2013-04-16 23:57:53 -04:00
fprintf ( stderr , " Turning around at xbegin % " PRIu64 " \n " , l - > lsn . lsn ) ;
2013-04-16 23:58:03 -04:00
renv - > goforward = TRUE ;
return 0 ;
2013-04-16 23:57:53 -04:00
} else {
2013-04-16 23:58:00 -04:00
fprintf ( stderr , " Scanning back at xbegin % " PRIu64 " (looking for % " PRIu64 " ) \n " , l - > lsn . lsn , bs - > min_live_txn ) ;
2013-04-16 23:57:53 -04:00
return 0 ;
}
}
2013-04-16 23:57:54 -04:00
fprintf ( stderr , " %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) bs - > bs ) ;
2013-04-16 23:57:53 -04:00
abort ( ) ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_timestamp ( struct logtype_timestamp * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_timestamp ( struct logtype_timestamp * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-16 23:58:03 -04:00
static int toku_recover_shutdown ( struct logtype_shutdown * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:58:01 -04:00
static int toku_recover_backward_shutdown ( struct logtype_shutdown * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:57:39 -04:00
static int toku_delete_rolltmp_files ( const char * log_dir ) {
struct dirent * de ;
DIR * d = opendir ( log_dir ) ;
if ( d = = 0 ) {
return errno ;
}
int result = 0 ;
while ( ( de = readdir ( d ) ) ) {
char rolltmp_prefix [ ] = " __rolltmp. " ;
int r = memcmp ( de - > d_name , rolltmp_prefix , sizeof ( rolltmp_prefix ) - 1 ) ;
2013-04-16 23:57:59 -04:00
if ( r = = 0 ) {
2013-04-16 23:57:39 -04:00
int fnamelen = strlen ( log_dir ) + strlen ( de - > d_name ) + 2 ; // One for the slash and one for the trailing NUL.
char fname [ fnamelen ] ;
2013-04-16 23:57:42 -04:00
int l = snprintf ( fname , fnamelen , " %s/%s " , log_dir , de - > d_name ) ;
assert ( l + 1 = = fnamelen ) ;
2013-04-16 23:57:39 -04:00
r = unlink ( fname ) ;
if ( r ! = 0 ) {
result = errno ;
perror ( " Trying to delete a rolltmp file " ) ;
}
}
}
{
int r = closedir ( d ) ;
if ( r = = - 1 ) return errno ;
}
return result ;
}
2013-04-16 23:57:59 -04:00
// Effects: If there are no log files, or if there is a "clean" checkpoint at the end of the log,
2013-04-16 23:57:59 -04:00
// then we don't need recovery to run. Skip the shutdown log entry if there is one.
// Returns: TRUE if we need recovery, otherwise FALSE.
2013-04-16 23:58:00 -04:00
int tokudb_needs_recovery ( const char * log_dir , BOOL ignore_log_empty ) {
2013-04-16 23:57:59 -04:00
int needs_recovery ;
2007-11-23 18:27:50 +00:00
int r ;
2013-04-16 23:57:59 -04:00
TOKULOGCURSOR logcursor = NULL ;
2008-03-12 19:40:38 +00:00
2013-04-16 23:57:59 -04:00
r = toku_logcursor_create ( & logcursor , log_dir ) ;
if ( r ! = 0 ) {
needs_recovery = TRUE ; goto exit ;
}
struct log_entry * le = NULL ;
r = toku_logcursor_last ( logcursor , & le ) ;
2013-04-16 23:58:00 -04:00
if ( r = = DB_NOTFOUND & & ignore_log_empty ) {
2013-04-16 23:57:59 -04:00
needs_recovery = FALSE ; goto exit ;
}
if ( r ! = 0 ) {
needs_recovery = TRUE ; goto exit ;
}
2013-04-16 23:57:59 -04:00
if ( le - > cmd = = LT_shutdown | | le - > cmd = = LT_timestamp ) {
2013-04-16 23:57:59 -04:00
r = toku_logcursor_prev ( logcursor , & le ) ;
if ( r ! = 0 ) {
needs_recovery = TRUE ; goto exit ;
}
2013-04-16 23:57:59 -04:00
}
if ( le - > cmd ! = LT_end_checkpoint ) {
needs_recovery = TRUE ; goto exit ;
}
struct log_entry end_checkpoint = * le ;
2008-03-12 19:40:38 +00:00
2013-04-16 23:57:59 -04:00
r = toku_logcursor_prev ( logcursor , & le ) ;
if ( r ! = 0 | | le - > cmd ! = LT_begin_checkpoint ) {
needs_recovery = TRUE ; goto exit ;
}
if ( le - > u . begin_checkpoint . lsn . lsn ! = end_checkpoint . u . end_checkpoint . txnid ) {
needs_recovery = TRUE ; goto exit ;
}
needs_recovery = FALSE ;
exit :
if ( logcursor ) {
r = toku_logcursor_destroy ( & logcursor ) ;
assert ( r = = 0 ) ;
2008-03-12 19:40:38 +00:00
}
2013-04-16 23:57:59 -04:00
return needs_recovery ;
}
2013-04-16 23:58:02 -04:00
// abort all of the remaining live transactions in descending transaction id order
static void recover_abort_live_txns ( RECOVER_ENV renv ) {
int r ;
while ( 1 ) {
u_int32_t n_live_txns = toku_omt_size ( renv - > logger - > live_txns ) ;
if ( n_live_txns = = 0 )
break ;
OMTVALUE v ;
r = toku_omt_fetch ( renv - > logger - > live_txns , n_live_txns - 1 , & v , NULL ) ;
if ( r ! = 0 )
break ;
TOKUTXN txn = ( TOKUTXN ) v ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:02 -04:00
// abort the transaction
r = toku_txn_abort_txn ( txn , recover_yield , NULL ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:02 -04:00
// close the transaction
toku_txn_close_txn ( txn ) ;
}
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:04 -04:00
static int do_recovery ( RECOVER_ENV renv , const char * env_dir , const char * log_dir ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:00 -04:00
int rr = 0 ;
TOKULOGCURSOR logcursor = NULL ;
2013-04-16 23:58:00 -04:00
struct log_entry * le = NULL ;
2008-03-12 19:40:38 +00:00
char org_wd [ 1000 ] ;
{
char * wd = getcwd ( org_wd , sizeof ( org_wd ) ) ;
assert ( wd ! = 0 ) ;
//printf("%s:%d org_wd=\"%s\"\n", __FILE__, __LINE__, org_wd);
}
2013-04-16 23:57:53 -04:00
2013-04-16 23:58:01 -04:00
r = toku_logger_open ( log_dir , renv - > logger ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:02 -04:00
// grab the last LSN so that it can be restored when the log is restarted
2013-04-16 23:58:01 -04:00
LSN lastlsn = toku_logger_last_lsn ( renv - > logger ) ;
2013-04-16 23:57:58 -04:00
2013-04-16 23:58:00 -04:00
// there must be at least one log entry
r = toku_logcursor_create ( & logcursor , log_dir ) ;
assert ( r = = 0 ) ;
r = toku_logcursor_last ( logcursor , & le ) ;
if ( r ! = 0 ) {
2013-04-16 23:58:04 -04:00
if ( toku_recover_trace )
printf ( " RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:58:00 -04:00
rr = DB_RUNRECOVERY ; goto errorexit ;
}
// TODO use logcursor->invalidate()
r = toku_logcursor_destroy ( & logcursor ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:58 -04:00
r = toku_logcursor_create ( & logcursor , log_dir ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:04 -04:00
r = chdir ( env_dir ) ;
2013-04-16 23:58:00 -04:00
if ( r ! = 0 ) {
// no data directory error
rr = errno ; goto errorexit ;
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:57:59 -04:00
// scan backwards
2013-04-16 23:58:01 -04:00
backward_scan_state_init ( & renv - > bs ) ;
2013-04-16 23:57:58 -04:00
while ( 1 ) {
2013-04-16 23:57:59 -04:00
le = NULL ;
2013-04-16 23:57:58 -04:00
r = toku_logcursor_prev ( logcursor , & le ) ;
2013-04-16 23:57:59 -04:00
if ( toku_recover_trace )
printf ( " %s:%d r=%d cmd=%c \n " , __FUNCTION__ , __LINE__ , r , le ? le - > cmd : ' ? ' ) ;
2013-04-16 23:58:00 -04:00
if ( r ! = 0 ) {
if ( r = = DB_RUNRECOVERY ) {
rr = DB_RUNRECOVERY ; goto errorexit ;
}
2013-04-16 23:57:58 -04:00
break ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:58:01 -04:00
logtype_dispatch_assign ( le , toku_recover_backward_ , r , renv ) ;
2013-04-16 23:58:03 -04:00
if ( toku_recover_trace )
printf ( " %s:%d r=%d cmd=%c \n " , __FUNCTION__ , __LINE__ , r , le ? le - > cmd : ' ? ' ) ;
2013-04-16 23:57:59 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:04 -04:00
if ( toku_recover_trace )
printf ( " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:58:03 -04:00
rr = DB_RUNRECOVERY ; goto errorexit ;
}
if ( renv - > goforward ) {
logtype_dispatch_assign ( le , toku_recover_ , r , renv ) ;
2013-04-16 23:57:59 -04:00
if ( toku_recover_trace )
printf ( " %s:%d r=%d cmd=%c \n " , __FUNCTION__ , __LINE__ , r , le ? le - > cmd : ' ? ' ) ;
2013-04-16 23:58:03 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:04 -04:00
if ( toku_recover_trace )
printf ( " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:58:03 -04:00
rr = DB_RUNRECOVERY ; goto errorexit ;
}
2013-04-16 23:57:58 -04:00
break ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:57:53 -04:00
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:57:59 -04:00
// scan forwards
2013-04-16 23:57:58 -04:00
while ( 1 ) {
2013-04-16 23:57:59 -04:00
le = NULL ;
2013-04-16 23:57:58 -04:00
r = toku_logcursor_next ( logcursor , & le ) ;
2013-04-16 23:57:59 -04:00
if ( toku_recover_trace )
printf ( " %s:%d r=%d cmd=%c \n " , __FUNCTION__ , __LINE__ , r , le ? le - > cmd : ' ? ' ) ;
2013-04-16 23:58:00 -04:00
if ( r ! = 0 ) {
if ( r = = DB_RUNRECOVERY ) {
rr = DB_RUNRECOVERY ; goto errorexit ;
}
2013-04-16 23:57:58 -04:00
break ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:58:03 -04:00
logtype_dispatch_assign ( le , toku_recover_ , r , renv ) ;
if ( toku_recover_trace )
printf ( " %s:%d r=%d cmd=%c \n " , __FUNCTION__ , __LINE__ , r , le ? le - > cmd : ' ? ' ) ;
if ( r ! = 0 ) {
2013-04-16 23:58:04 -04:00
if ( toku_recover_trace )
printf ( " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:58:03 -04:00
rr = DB_RUNRECOVERY ; goto errorexit ;
}
2007-11-23 18:27:50 +00:00
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:57:59 -04:00
r = toku_logcursor_destroy ( & logcursor ) ;
assert ( r = = 0 ) ;
// restart logging
2013-04-16 23:58:01 -04:00
toku_logger_restart ( renv - > logger , lastlsn ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:02 -04:00
// abort the live transactions
recover_abort_live_txns ( renv ) ;
2013-04-16 23:57:59 -04:00
// close the open dictionaries
2013-04-16 23:58:03 -04:00
file_map_close_dictionaries ( & renv - > fmap , TRUE ) ;
2013-04-16 23:57:59 -04:00
// write a recovery log entry
BYTESTRING recover_comment = { strlen ( " recover " ) , " recover " } ;
2013-04-16 23:58:01 -04:00
r = toku_log_timestamp ( renv - > logger , NULL , TRUE , 0 , recover_comment ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
// checkpoint
2013-04-16 23:58:06 -04:00
r = toku_checkpoint ( renv - > ct , renv - > logger , NULL , NULL , NULL , NULL ) ;
2013-04-16 23:58:01 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:57:58 -04:00
r = chdir ( org_wd ) ;
assert ( r = = 0 ) ;
2008-03-14 11:02:28 +00:00
2013-04-16 23:57:59 -04:00
return 0 ;
2013-04-16 23:58:00 -04:00
errorexit :
if ( logcursor ) {
r = toku_logcursor_destroy ( & logcursor ) ;
assert ( r = = 0 ) ;
}
r = chdir ( org_wd ) ;
assert ( r = = 0 ) ;
return rr ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:00 -04:00
static int recover_lock ( const char * lock_dir , int * lockfd ) {
2013-04-16 23:58:04 -04:00
const char fname [ ] = " /__tokudb_recoverylock_dont_delete_me " ;
2013-04-16 23:58:00 -04:00
int namelen = strlen ( lock_dir ) ;
char lockfname [ namelen + sizeof ( fname ) ] ;
int l = snprintf ( lockfname , sizeof ( lockfname ) , " %s%s " , lock_dir , fname ) ;
assert ( l + 1 = = ( signed ) ( sizeof ( lockfname ) ) ) ;
* lockfd = toku_os_lock_file ( lockfname ) ;
if ( * lockfd < 0 ) {
int e = errno ;
printf ( " Couldn't run recovery because some other process holds the recovery lock %s \n " , lockfname ) ;
return e ;
}
return 0 ;
}
static int recover_unlock ( int lockfd ) {
int r = toku_os_unlock_file ( lockfd ) ;
if ( r ! = 0 )
return errno ;
return 0 ;
}
int tokudb_recover_delete_rolltmp_files ( const char * UU ( data_dir ) , const char * log_dir ) {
int lockfd = - 1 ;
int r = recover_lock ( log_dir , & lockfd ) ;
if ( r ! = 0 )
return r ;
r = toku_delete_rolltmp_files ( log_dir ) ;
int rr = recover_unlock ( lockfd ) ;
if ( r = = 0 & & rr ! = 0 )
r = rr ;
return r ;
}
2013-04-16 23:58:04 -04:00
int tokudb_recover ( const char * env_dir , const char * log_dir , brt_compare_func bt_compare , brt_compare_func dup_compare ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:00 -04:00
int lockfd = - 1 ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:00 -04:00
r = recover_lock ( log_dir , & lockfd ) ;
if ( r ! = 0 )
return r ;
2013-04-16 23:57:59 -04:00
r = toku_delete_rolltmp_files ( log_dir ) ;
if ( r ! = 0 ) {
2013-04-16 23:58:00 -04:00
( void ) recover_unlock ( lockfd ) ;
2013-04-16 23:57:59 -04:00
return r ;
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:57:59 -04:00
int rr = 0 ;
2013-04-16 23:58:00 -04:00
if ( tokudb_needs_recovery ( log_dir , FALSE ) ) {
2013-04-16 23:57:59 -04:00
struct recover_env renv ;
r = recover_env_init ( & renv , bt_compare , dup_compare ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:04 -04:00
rr = do_recovery ( & renv , env_dir , log_dir ) ;
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:03 -04:00
recover_env_cleanup ( & renv , ( BOOL ) ( rr = = 0 ) ) ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:00 -04:00
r = recover_unlock ( lockfd ) ;
if ( r ! = 0 )
return r ;
2008-03-12 19:40:38 +00:00
2013-04-16 23:57:59 -04:00
return rr ;
2007-11-23 18:27:50 +00:00
}
2013-04-16 23:58:52 -04:00
// Return 0 if recovery log exists, ENOENT if log is missing
int
tokudb_recover_log_exists ( const char * log_dir ) {
int r ;
TOKULOGCURSOR logcursor ;
r = toku_logcursor_create ( & logcursor , log_dir ) ;
if ( r = = 0 ) {
int rclose ;
r = toku_logcursor_log_exists ( logcursor ) ; // return ENOENT if no log
rclose = toku_logcursor_destroy ( & logcursor ) ;
assert ( rclose = = 0 ) ;
}
else
r = ENOENT ;
return r ;
}