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:59:09 -04:00
# ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
2013-04-16 23:57:48 -04:00
# 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:59:17 -04:00
static const char recovery_lock_file [ ] = " /__tokudb_recoverylock_dont_delete_me " ;
2013-04-16 23:58:53 -04:00
int tokudb_recovery_trace = 0 ; // turn on recovery tracing, default off.
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
2013-04-16 23:59:17 -04:00
# define VERIFY_COUNTS(n) toku_verify_or_set_counts(n, FALSE)
2008-03-14 19:14:31 +00:00
# else
# define VERIFY_COUNTS(n) ((void)0)
# endif
2013-04-16 23:58:54 -04:00
// time in seconds between recovery progress reports
2013-04-16 23:58:53 -04:00
# define TOKUDB_RECOVERY_PROGRESS_TIME 15
2013-04-16 23:58:52 -04:00
struct scan_state {
2013-04-16 23:58:56 -04:00
enum {
BACKWARD_NEWER_CHECKPOINT_END = 1 ,
BACKWARD_BETWEEN_CHECKPOINT_BEGIN_END ,
FORWARD_BETWEEN_CHECKPOINT_BEGIN_END ,
FORWARD_NEWER_CHECKPOINT_END ,
} ss ;
2013-04-16 23:59:05 -04:00
LSN checkpoint_begin_lsn ;
LSN checkpoint_end_lsn ;
uint64_t checkpoint_end_timestamp ;
uint64_t checkpoint_begin_timestamp ;
uint32_t checkpoint_num_fassociate ;
uint32_t checkpoint_num_xstillopen ;
2013-04-16 23:57:59 -04:00
} ;
2013-04-16 23:59:10 -04:00
static const char * scan_state_strings [ ] = {
" ? " , " bw_newer " , " bw_between " , " fw_between " , " fw_newer " ,
} ;
2013-04-16 23:58:52 -04:00
static void scan_state_init ( struct scan_state * ss ) {
2013-04-16 23:59:05 -04:00
ss - > ss = BACKWARD_NEWER_CHECKPOINT_END ;
ss - > checkpoint_begin_lsn = ZERO_LSN ;
ss - > checkpoint_end_lsn = ZERO_LSN ;
ss - > checkpoint_num_fassociate = 0 ;
ss - > checkpoint_num_xstillopen = 0 ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:58:56 -04:00
static const char * scan_state_string ( struct scan_state * ss ) {
assert ( BACKWARD_NEWER_CHECKPOINT_END < = ss - > ss & & ss - > ss < = FORWARD_NEWER_CHECKPOINT_END ) ;
return scan_state_strings [ ss - > ss ] ;
2013-04-16 23:58:53 -04:00
}
2013-04-16 23:58:02 -04:00
// File map tuple
struct file_map_tuple {
FILENUM filenum ;
BRT brt ;
2013-04-16 23:58:56 -04:00
char * iname ;
2013-04-16 23:58:02 -04:00
} ;
2013-04-16 23:58:56 -04:00
static void file_map_tuple_init ( struct file_map_tuple * tuple , FILENUM filenum , BRT brt , char * iname ) {
2013-04-16 23:58:02 -04:00
tuple - > filenum = filenum ;
tuple - > brt = brt ;
2013-04-16 23:58:56 -04:00
tuple - > iname = iname ;
2013-04-16 23:58:02 -04:00
}
static void file_map_tuple_destroy ( struct file_map_tuple * tuple ) {
2013-04-16 23:58:56 -04:00
if ( tuple - > iname ) {
toku_free ( tuple - > iname ) ;
tuple - > iname = NULL ;
2013-04-16 23:58:02 -04:00
}
}
2013-04-16 23:58:56 -04:00
// Map filenum to brt
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:53 -04:00
static uint32_t file_map_get_num_dictionaries ( struct file_map * fmap ) {
return toku_omt_size ( fmap - > filenums ) ;
}
2013-04-16 23:59:05 -04:00
static void file_map_close_dictionaries ( struct file_map * fmap , BOOL recovery_succeeded , TOKULOGGER logger ) {
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 ;
2013-04-16 23:59:46 -04:00
r = toku_omt_fetch ( fmap - > filenums , n - 1 , & v ) ;
2013-04-16 23:58:02 -04:00
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
2013-04-16 23:59:23 -04:00
r = toku_brt_set_panic ( tuple - > brt , DB_RUNRECOVERY , " recovery failed " ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:03 -04:00
}
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:59:05 -04:00
if ( logger - > rollback_cachefile ! = tuple - > brt - > cf ) {
//Rollback cachefile is closed manually at end of recovery, not here
r = toku_close_brt ( tuple - > brt , & error_string ) ;
if ( ! recovery_succeeded ) {
if ( tokudb_recovery_trace )
fprintf ( stderr , " %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:56 -04:00
static int file_map_insert ( struct file_map * fmap , FILENUM fnum , BRT brt , char * iname ) {
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = toku_malloc ( sizeof ( struct file_map_tuple ) ) ;
assert ( tuple ) ;
2013-04-16 23:58:56 -04:00
file_map_tuple_init ( tuple , fnum , brt , iname ) ;
2013-04-16 23:58:02 -04:00
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 ;
2013-04-16 23:59:46 -04:00
int r = toku_omt_find_zero ( fmap - > filenums , file_map_h , & fnum , & v , & idx ) ;
2013-04-16 23:58:02 -04:00
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 ) ;
}
}
2013-04-16 23:59:01 -04:00
// Look up file info: given FILENUM, return file_map_tuple (or DB_NOTFOUND)
2013-04-16 23:58:02 -04:00
static int file_map_find ( struct file_map * fmap , FILENUM fnum , struct file_map_tuple * * file_map_tuple ) {
OMTVALUE v ; u_int32_t idx ;
2013-04-16 23:59:46 -04:00
int r = toku_omt_find_zero ( fmap - > filenums , file_map_h , & fnum , & v , & idx ) ;
2013-04-16 23:58:02 -04:00
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:59:05 -04:00
else assert ( r = = DB_NOTFOUND ) ;
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 ;
2013-04-16 23:59:37 -04:00
brt_update_func update_function ;
2013-04-16 23:58:59 -04:00
generate_row_for_put_func generate_row_for_put ;
generate_row_for_del_func generate_row_for_del ;
2013-04-16 23:58:52 -04:00
struct scan_state ss ;
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:59:21 -04:00
static int recover_env_init ( RECOVER_ENV renv , brt_compare_func bt_compare ,
2013-04-16 23:59:37 -04:00
brt_update_func update_function ,
2013-04-16 23:58:59 -04:00
generate_row_for_put_func generate_row_for_put ,
generate_row_for_del_func generate_row_for_del ,
2013-04-16 23:58:58 -04:00
size_t cachetable_size ) {
2013-04-16 23:57:59 -04:00
int r ;
2013-04-16 23:58:53 -04:00
r = toku_create_cachetable ( & renv - > ct , cachetable_size ? cachetable_size : 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 ;
2013-04-16 23:59:37 -04:00
renv - > update_function = update_function ;
2013-04-16 23:58:59 -04:00
renv - > generate_row_for_put = generate_row_for_put ;
renv - > generate_row_for_del = generate_row_for_del ;
2013-04-16 23:58:01 -04:00
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:58:53 -04:00
if ( tokudb_recovery_trace )
fprintf ( stderr , " %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:59:05 -04:00
file_map_close_dictionaries ( & renv - > fmap , recovery_succeeded , renv - > logger ) ;
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:59:05 -04:00
r = toku_logger_close_rollback ( renv - > logger , ! recovery_succeeded ) ;
assert ( r = = 0 ) ;
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:59:44 -04:00
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
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
fprintf ( stderr , " %s:%d \n " , __FUNCTION__ , __LINE__ ) ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:53 -04:00
static const char * recover_state ( RECOVER_ENV renv ) {
2013-04-16 23:58:56 -04:00
return scan_state_string ( & renv - > ss ) ;
2013-04-16 23:58:53 -04:00
}
2013-04-16 23:59:04 -04:00
// function supplied to transaction commit and abort
// No yielding is necessary, but it must call the f function if provided.
2013-04-16 23:59:06 -04:00
static void recover_yield ( voidfp f , void * fpthunk , void * UU ( yieldthunk ) ) {
if ( f ) f ( fpthunk ) ;
2008-03-14 19:14:31 +00:00
}
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:59:44 -04:00
static int internal_recover_fopen_or_fcreate ( RECOVER_ENV renv , BOOL must_create , int mode , BYTESTRING * bs_iname , FILENUM filenum , u_int32_t treeflags ,
TOKUTXN txn , uint32_t nodesize , uint32_t basementnodesize , LSN max_acceptable_lsn ) {
2013-04-16 23:58:00 -04:00
int r ;
2013-04-16 23:58:56 -04:00
char * iname = fixup_fname ( bs_iname ) ;
2013-04-16 23:58:00 -04:00
2013-04-16 23:58:56 -04:00
BRT brt = NULL ;
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:59:23 -04:00
r = toku_brt_set_flags ( brt , treeflags ) ;
2013-04-16 23:59:34 -04:00
assert ( r = = 0 ) ;
if ( nodesize ! = 0 ) {
r = toku_brt_set_nodesize ( brt , nodesize ) ;
assert ( r = = 0 ) ;
}
2013-04-16 23:57:59 -04:00
2013-04-16 23:59:44 -04:00
if ( basementnodesize ! = 0 ) {
r = toku_brt_set_basementnodesize ( brt , basementnodesize ) ;
assert ( r = = 0 ) ;
}
2013-04-16 23:57:59 -04:00
// set the key compare functions
2013-04-16 23:59:23 -04:00
if ( ! ( treeflags & TOKU_DB_KEYCMP_BUILTIN ) & & renv - > bt_compare ) {
r = toku_brt_set_bt_compare ( brt , renv - > bt_compare ) ;
2013-04-16 23:59:34 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:23 -04:00
}
2013-04-16 23:58:01 -04:00
2013-04-16 23:59:37 -04:00
if ( renv - > update_function ) {
r = toku_brt_set_update ( brt , renv - > update_function ) ;
assert ( r = = 0 ) ;
}
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:59:35 -04:00
r = toku_brt_open_recovery ( brt , iname , must_create , must_create , renv - > ct , txn , fake_db , filenum , max_acceptable_lsn ) ;
2013-04-16 23:58:03 -04:00
if ( r ! = 0 ) {
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:56 -04:00
toku_free ( iname ) ;
2013-04-16 23:58:05 -04:00
toku_free ( fake_db ) ; //Free memory allocated for the fake db.
2013-04-16 23:59:34 -04:00
if ( r = = ENOENT ) //Not an error to simply be missing.
2013-04-16 23:58:06 -04:00
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:56 -04:00
file_map_insert ( & renv - > fmap , filenum , brt , iname ) ;
2013-04-16 23:58:03 -04:00
return 0 ;
2013-04-16 23:57:44 -04:00
}
2013-04-16 23:59:06 -04:00
static int toku_recover_local_txn_checkpoint ( struct logtype_local_txn_checkpoint * l , RECOVER_ENV UU ( renv ) ) {
int r ;
switch ( renv - > ss . ss ) {
case FORWARD_BETWEEN_CHECKPOINT_BEGIN_END :
case FORWARD_NEWER_CHECKPOINT_END : {
// assert that the transaction exists
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 & & txn ! = NULL ) ;
r = 0 ;
break ;
}
default :
assert ( 0 ) ;
return 0 ;
}
return r ;
}
static int toku_recover_backward_local_txn_checkpoint ( struct logtype_local_txn_checkpoint * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
2013-04-16 23:59:05 -04:00
static int toku_recover_begin_checkpoint ( struct logtype_begin_checkpoint * l , RECOVER_ENV renv ) {
int r ;
switch ( renv - > ss . ss ) {
case FORWARD_BETWEEN_CHECKPOINT_BEGIN_END :
assert ( l - > lsn . lsn = = renv - > ss . checkpoint_begin_lsn . lsn ) ;
r = 0 ;
break ;
case FORWARD_NEWER_CHECKPOINT_END :
assert ( l - > lsn . lsn > renv - > ss . checkpoint_end_lsn . lsn ) ;
r = 0 ; // ignore it (log only has a begin checkpoint)
break ;
default :
fprintf ( stderr , " Tokudb recovery %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) renv - > ss . ss ) ;
abort ( ) ;
break ;
}
return r ;
}
static int toku_recover_backward_begin_checkpoint ( struct logtype_begin_checkpoint * l , RECOVER_ENV renv ) {
int r ;
time_t tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery bw_begin_checkpoint at % " PRIu64 " timestamp % " PRIu64 " (%s) \n " , ctime ( & tnow ) , l - > lsn . lsn , l - > timestamp , recover_state ( renv ) ) ;
switch ( renv - > ss . ss ) {
case BACKWARD_NEWER_CHECKPOINT_END :
2013-04-16 23:59:35 -04:00
// incomplete checkpoint, nothing to do
2013-04-16 23:59:35 -04:00
r = 0 ;
2013-04-16 23:59:05 -04:00
break ;
case BACKWARD_BETWEEN_CHECKPOINT_BEGIN_END :
assert ( l - > lsn . lsn = = renv - > ss . checkpoint_begin_lsn . lsn ) ;
renv - > ss . ss = FORWARD_BETWEEN_CHECKPOINT_BEGIN_END ;
renv - > ss . checkpoint_begin_timestamp = l - > timestamp ;
renv - > goforward = TRUE ;
tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery turning around at begin checkpoint % " PRIu64 " time % " PRIu64 " \n " ,
ctime ( & tnow ) , l - > lsn . lsn ,
renv - > ss . checkpoint_end_timestamp - renv - > ss . checkpoint_begin_timestamp ) ;
r = 0 ;
break ;
default :
fprintf ( stderr , " Tokudb recovery %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) renv - > ss . ss ) ;
abort ( ) ;
break ;
}
return r ;
}
static int toku_recover_end_checkpoint ( struct logtype_end_checkpoint * l , RECOVER_ENV renv ) {
int r ;
switch ( renv - > ss . ss ) {
case FORWARD_BETWEEN_CHECKPOINT_BEGIN_END :
assert ( l - > xid = = renv - > ss . checkpoint_begin_lsn . lsn ) ;
assert ( l - > lsn . lsn = = renv - > ss . checkpoint_end_lsn . lsn ) ;
assert ( l - > num_fassociate_entries = = renv - > ss . checkpoint_num_fassociate ) ;
assert ( l - > num_xstillopen_entries = = renv - > ss . checkpoint_num_xstillopen ) ;
renv - > ss . ss = FORWARD_NEWER_CHECKPOINT_END ;
r = 0 ;
break ;
case FORWARD_NEWER_CHECKPOINT_END :
assert ( 0 ) ;
return 0 ;
default :
assert ( 0 ) ;
return 0 ;
}
return r ;
}
static int toku_recover_backward_end_checkpoint ( struct logtype_end_checkpoint * l , RECOVER_ENV renv ) {
time_t tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery bw_end_checkpoint at % " PRIu64 " timestamp % " PRIu64 " xid % " PRIu64 " (%s) \n " , ctime ( & tnow ) , l - > lsn . lsn , l - > timestamp , l - > xid , recover_state ( renv ) ) ;
switch ( renv - > ss . ss ) {
case BACKWARD_NEWER_CHECKPOINT_END :
renv - > ss . ss = BACKWARD_BETWEEN_CHECKPOINT_BEGIN_END ;
renv - > ss . checkpoint_begin_lsn . lsn = l - > xid ;
renv - > ss . checkpoint_end_lsn . lsn = l - > lsn . lsn ;
renv - > ss . checkpoint_end_timestamp = l - > timestamp ;
return 0 ;
case BACKWARD_BETWEEN_CHECKPOINT_BEGIN_END :
fprintf ( stderr , " Tokudb recovery %s:%d Should not see two end_checkpoint log entries without an intervening begin_checkpoint \n " , __FILE__ , __LINE__ ) ;
abort ( ) ;
default :
break ;
}
fprintf ( stderr , " Tokudb recovery %s: %d Unknown checkpoint state %d \n " , __FILE__ , __LINE__ , ( int ) renv - > ss . ss ) ;
abort ( ) ;
}
static int toku_recover_fassociate ( struct logtype_fassociate * l , RECOVER_ENV renv ) {
2013-04-16 23:58:55 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:59:05 -04:00
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
char * fname = fixup_fname ( & l - > iname ) ;
switch ( renv - > ss . ss ) {
case FORWARD_BETWEEN_CHECKPOINT_BEGIN_END :
renv - > ss . checkpoint_num_fassociate + + ;
assert ( r = = DB_NOTFOUND ) ; //Not open
2013-04-16 23:59:35 -04:00
// Open it if it exists.
// If rollback file, specify which checkpointed version of file we need (not just the latest)
// because we cannot use a rollback log that is later than the last complete checkpoint. See #3113.
2013-04-16 23:59:44 -04:00
{
BOOL rollback_file = ! strcmp ( fname , ROLLBACK_CACHEFILE_NAME ) ;
LSN max_acceptable_lsn = MAX_LSN ;
if ( rollback_file )
max_acceptable_lsn = renv - > ss . checkpoint_begin_lsn ;
r = internal_recover_fopen_or_fcreate ( renv , FALSE , 0 , & l - > iname , l - > filenum , l - > treeflags , NULL , 0 , 0 , max_acceptable_lsn ) ;
if ( r = = 0 & & rollback_file ) {
//Load rollback cachefile
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
assert ( r = = 0 ) ;
renv - > logger - > rollback_cachefile = tuple - > brt - > cf ;
}
}
2013-04-16 23:59:05 -04:00
break ;
case FORWARD_NEWER_CHECKPOINT_END :
if ( r = = 0 ) { //IF it is open
// assert that the filenum maps to the correct iname
assert ( strcmp ( fname , tuple - > iname ) = = 0 ) ;
}
r = 0 ;
break ;
default :
assert ( 0 ) ;
return 0 ;
}
toku_free ( fname ) ;
return r ;
}
2013-04-16 23:58:56 -04:00
2013-04-16 23:59:05 -04:00
static int toku_recover_backward_fassociate ( struct logtype_fassociate * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
2013-04-16 23:58:56 -04:00
2013-04-16 23:59:05 -04:00
static int
recover_transaction ( TOKUTXN * txnp , TXNID xid , TXNID parentxid , TOKULOGGER logger ) {
int r ;
// lookup the parent
TOKUTXN parent = NULL ;
if ( parentxid ! = TXNID_NONE ) {
r = toku_txnid2txn ( logger , parentxid , & parent ) ;
2013-04-16 23:58:55 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( parent ! = NULL ) ;
}
// create a transaction and bind it to the transaction id
TOKUTXN txn = NULL ;
{
//Verify it does not yet exist.
r = toku_txnid2txn ( logger , xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn = = NULL ) ;
}
2013-04-16 23:59:22 -04:00
r = toku_txn_begin_with_xid ( parent , & txn , logger , xid , TXN_SNAPSHOT_NONE ) ;
2013-04-16 23:59:05 -04:00
assert ( r = = 0 ) ;
if ( txnp ) * txnp = txn ;
return 0 ;
}
static int toku_recover_xstillopen ( struct logtype_xstillopen * l , RECOVER_ENV UU ( renv ) ) {
int r ;
switch ( renv - > ss . ss ) {
case FORWARD_BETWEEN_CHECKPOINT_BEGIN_END : {
renv - > ss . checkpoint_num_xstillopen + + ;
TOKUTXN txn = NULL ;
{ //Create the transaction.
r = recover_transaction ( & txn , l - > xid , l - > parentxid , renv - > logger ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
}
{ //Recover rest of transaction.
# define COPY_TO_INFO(field) .field = l->field
struct txninfo info = {
COPY_TO_INFO ( rollentry_raw_count ) ,
. num_brts = 0 , //Set afterwards
. open_brts = NULL , //Set afterwards
COPY_TO_INFO ( force_fsync_on_commit ) ,
COPY_TO_INFO ( num_rollback_nodes ) ,
COPY_TO_INFO ( num_rollentries ) ,
COPY_TO_INFO ( spilled_rollback_head ) ,
COPY_TO_INFO ( spilled_rollback_tail ) ,
COPY_TO_INFO ( current_rollback )
} ;
# undef COPY_TO_INFO
//Generate open_brts
BRT array [ l - > open_filenums . num ] ; //Allocate maximum possible requirement
info . open_brts = array ;
uint32_t i ;
for ( i = 0 ; i < l - > open_filenums . num ; i + + ) {
//open_filenums.filenums[]
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > open_filenums . filenums [ i ] , & tuple ) ;
if ( r = = 0 ) {
info . open_brts [ info . num_brts + + ] = tuple - > brt ;
}
else {
assert ( r = = DB_NOTFOUND ) ;
}
}
r = toku_txn_load_txninfo ( txn , & info ) ;
assert ( r = = 0 ) ;
}
break ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:59:05 -04:00
case FORWARD_NEWER_CHECKPOINT_END : {
// assert that the transaction exists
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 & & txn ! = NULL ) ;
r = 0 ;
break ;
}
default :
assert ( 0 ) ;
return 0 ;
}
return r ;
}
static int toku_recover_backward_xstillopen ( struct logtype_xstillopen * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_suppress_rollback ( struct logtype_suppress_rollback * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
struct file_map_tuple * tuple = NULL ;
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
if ( r = = 0 ) {
//File is open
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
struct brt_header * h = tuple - > brt - > h ;
2013-04-16 23:59:18 -04:00
toku_brt_header_suppress_rollbacks ( h , txn ) ;
2013-04-16 23:59:20 -04:00
r = toku_txn_note_brt ( txn , tuple - > brt ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
}
return 0 ;
}
static int toku_recover_backward_suppress_rollback ( struct logtype_suppress_rollback * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_xbegin ( struct logtype_xbegin * l , RECOVER_ENV renv ) {
int r ;
r = recover_transaction ( NULL , l - > lsn . lsn , l - > parentxid , renv - > logger ) ;
return r ;
}
static int toku_recover_backward_xbegin ( struct logtype_xbegin * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_xcommit ( struct logtype_xcommit * l , RECOVER_ENV renv ) {
int r ;
// find the transaction by transaction id
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
// commit the transaction
r = toku_txn_commit_with_lsn ( txn , TRUE , recover_yield , NULL , l - > lsn ,
2013-04-17 00:00:02 -04:00
NULL , NULL ,
false ) ;
2013-04-16 23:59:05 -04:00
assert ( r = = 0 ) ;
// close the transaction
toku_txn_close_txn ( txn ) ;
return 0 ;
}
static int toku_recover_backward_xcommit ( struct logtype_xcommit * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_xabort ( struct logtype_xabort * l , RECOVER_ENV renv ) {
int r ;
// find the transaction by transaction id
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
// abort the transaction
2013-04-17 00:00:02 -04:00
r = toku_txn_abort_with_lsn ( txn , recover_yield , NULL , l - > lsn , NULL , NULL , false ) ;
2013-04-16 23:59:05 -04:00
assert ( r = = 0 ) ;
// close the transaction
toku_txn_close_txn ( txn ) ;
return 0 ;
}
static int toku_recover_backward_xabort ( struct logtype_xabort * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:58:56 -04:00
// fcreate is like fopen except that the file must be created.
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:55 -04:00
int r ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:58:55 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:59:05 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:55 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:56 -04:00
// assert that filenum is closed
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:59:05 -04:00
assert ( r = = DB_NOTFOUND ) ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:59:05 -04:00
//unlink if it exists (recreate from scratch).
char * iname = fixup_fname ( & l - > iname ) ;
r = unlink ( iname ) ;
if ( r ! = 0 & & errno ! = ENOENT ) {
fprintf ( stderr , " Tokudb recovery %s:%d unlink %s %d \n " , __FUNCTION__ , __LINE__ , iname , errno ) ;
2013-04-16 23:58:56 -04:00
toku_free ( iname ) ;
2013-04-16 23:59:05 -04:00
return r ;
2013-04-16 23:58:52 -04:00
}
2013-04-16 23:59:05 -04:00
assert ( strcmp ( iname , ROLLBACK_CACHEFILE_NAME ) ) ; //Creation of rollback cachefile never gets logged.
toku_free ( iname ) ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:59:05 -04:00
BOOL must_create = TRUE ;
2013-04-16 23:59:44 -04:00
r = internal_recover_fopen_or_fcreate ( renv , must_create , l - > mode , & l - > iname , l - > filenum , l - > treeflags , txn , l - > nodesize , l - > basementnodesize , MAX_LSN ) ;
2013-04-16 23:58:56 -04:00
return r ;
2013-04-16 23:57:44 -04:00
}
2013-04-16 23:59:05 -04:00
static int toku_recover_backward_fcreate ( struct logtype_fcreate * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:57:56 -04:00
2013-04-16 23:59:34 -04:00
2013-04-16 23:58:56 -04:00
static int toku_recover_fopen ( struct logtype_fopen * l , RECOVER_ENV renv ) {
2013-04-16 23:59:05 -04:00
int r ;
// assert that filenum is closed
2013-04-16 23:58:56 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:59:05 -04:00
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
assert ( r = = DB_NOTFOUND ) ;
BOOL must_create = FALSE ;
TOKUTXN txn = NULL ;
char * fname = fixup_fname ( & l - > iname ) ;
if ( strcmp ( fname , ROLLBACK_CACHEFILE_NAME ) ) {
//Rollback cachefile can only be opened via fassociate.
2013-04-16 23:59:44 -04:00
r = internal_recover_fopen_or_fcreate ( renv , must_create , 0 , & l - > iname , l - > filenum , l - > treeflags , txn , 0 , 0 , MAX_LSN ) ;
2013-04-16 23:58:56 -04:00
}
2013-04-16 23:59:05 -04:00
toku_free ( fname ) ;
2013-04-16 23:58:56 -04:00
return r ;
}
2013-04-16 23:59:05 -04:00
static int toku_recover_backward_fopen ( struct logtype_fopen * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
2013-04-16 23:58:56 -04:00
return 0 ;
}
2013-04-16 23:59:37 -04:00
static int toku_recover_change_fdescriptor ( struct logtype_change_fdescriptor * l , RECOVER_ENV renv ) {
2013-04-16 23:59:17 -04:00
int r ;
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
if ( r = = 0 ) {
2013-04-16 23:59:37 -04:00
TOKUTXN txn = NULL ;
2013-04-16 23:59:17 -04:00
//Maybe do the descriptor (lsn filter)
2013-04-16 23:59:37 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
DBT old_descriptor , new_descriptor ;
toku_fill_dbt (
& old_descriptor ,
l - > old_descriptor . data ,
l - > old_descriptor . len
) ;
toku_fill_dbt (
& new_descriptor ,
l - > new_descriptor . data ,
l - > new_descriptor . len
) ;
2013-04-16 23:59:37 -04:00
r = toku_brt_change_descriptor (
2013-04-16 23:59:37 -04:00
tuple - > brt ,
& old_descriptor ,
& new_descriptor ,
FALSE ,
txn
) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:17 -04:00
}
return 0 ;
}
2013-04-16 23:59:37 -04:00
static int toku_recover_backward_change_fdescriptor ( struct logtype_change_fdescriptor * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:59:17 -04:00
return 0 ;
}
2013-04-16 23:59:01 -04:00
// if file referred to in l is open, close it
2013-04-16 23:58:56 -04:00
static int toku_recover_fclose ( struct logtype_fclose * l , RECOVER_ENV renv ) {
struct file_map_tuple * tuple = NULL ;
int r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:59:01 -04:00
if ( r = = 0 ) { // if file is open
2013-04-16 23:58:56 -04:00
char * iname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:59:01 -04:00
assert ( strcmp ( tuple - > iname , iname ) = = 0 ) ; // verify that file_map has same iname as log entry
2013-04-16 23:58:56 -04:00
DB * fake_db = tuple - > brt - > db ; //Need to free the fake db that was malloced
2013-04-16 23:59:05 -04:00
if ( strcmp ( iname , ROLLBACK_CACHEFILE_NAME ) ) {
//Rollback cachefile is closed manually at end of recovery, not here
r = toku_close_brt_lsn ( tuple - > brt , 0 , TRUE , l - > lsn ) ;
assert ( r = = 0 ) ;
}
2013-04-16 23:58:56 -04:00
toku_free ( fake_db ) ; //Must free the DB after the brt is closed
file_map_remove ( & renv - > fmap , l - > filenum ) ;
2013-04-16 23:59:05 -04:00
toku_free ( iname ) ;
2013-04-16 23:58:56 -04:00
}
return 0 ;
}
static int toku_recover_backward_fclose ( struct logtype_fclose * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
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 ;
2013-04-16 23:59:05 -04:00
int r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:06 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:58:06 -04:00
char * fixediname = fixup_fname ( & l - > iname ) ;
2013-04-16 23:59:05 -04:00
{ //Only if it exists
2013-04-16 23:58:06 -04:00
toku_struct_stat buf ;
r = toku_stat ( fixediname , & buf ) ;
2013-04-16 23:59:05 -04:00
if ( r = = 0 ) {
// txn exists and file exists, so create fdelete rollback entry
DBT iname_dbt ;
toku_fill_dbt ( & iname_dbt , fixediname , strlen ( fixediname ) + 1 ) ;
r = toku_brt_remove_on_commit ( txn , & iname_dbt ) ;
assert ( r = = 0 ) ;
}
else {
assert ( errno = = ENOENT ) ;
}
2013-04-16 23:58:06 -04:00
}
2013-04-16 23:58:56 -04:00
2013-04-16 23:58:06 -04:00
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:56 -04:00
static int toku_recover_enq_insert ( struct logtype_enq_insert * l , RECOVER_ENV renv ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:58:02 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:58:56 -04:00
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:59:05 -04:00
if ( r = = 0 ) {
//Maybe do the insertion if we found the cachefile.
DBT keydbt , valdbt ;
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 , FALSE , BRT_INSERT ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:01 -04:00
}
return 0 ;
}
2013-04-16 23:58:56 -04:00
static int toku_recover_backward_enq_insert ( struct logtype_enq_insert * 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:59:00 -04:00
static int toku_recover_enq_insert_no_overwrite ( struct logtype_enq_insert_no_overwrite * l , RECOVER_ENV renv ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:59:00 -04:00
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:59:05 -04:00
if ( r = = 0 ) {
//Maybe do the insertion if we found the cachefile.
DBT keydbt , valdbt ;
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 , FALSE , BRT_INSERT_NO_OVERWRITE ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:00 -04:00
}
return 0 ;
}
static int toku_recover_backward_enq_insert_no_overwrite ( struct logtype_enq_insert_no_overwrite * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
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:56 -04:00
int r ;
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 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:58:56 -04:00
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
2013-04-16 23:59:05 -04:00
if ( r = = 0 ) {
//Maybe do the deletion if we found the cachefile.
DBT keydbt ;
toku_fill_dbt ( & keydbt , l - > key . data , l - > key . len ) ;
r = toku_brt_maybe_delete ( tuple - > brt , & keydbt , txn , TRUE , l - > lsn , FALSE ) ;
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:59:05 -04:00
static int toku_recover_enq_insert_multiple ( struct logtype_enq_insert_multiple * l , RECOVER_ENV renv ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
DB * src_db = NULL ;
2013-04-16 23:59:33 -04:00
BOOL do_inserts = TRUE ;
2013-04-16 23:59:05 -04:00
{
2013-04-16 23:58:56 -04:00
struct file_map_tuple * tuple = NULL ;
2013-04-16 23:59:05 -04:00
r = file_map_find ( & renv - > fmap , l - > src_filenum , & tuple ) ;
if ( l - > src_filenum . fileid = = FILENUM_NONE . fileid )
assert ( r = = DB_NOTFOUND ) ;
else {
2013-04-16 23:59:33 -04:00
if ( r = = 0 )
src_db = tuple - > brt - > db ;
else
2013-04-16 23:59:33 -04:00
do_inserts = FALSE ; // src file was probably deleted, #3129
2013-04-16 23:58:56 -04:00
}
}
2013-04-16 23:59:33 -04:00
if ( do_inserts ) {
DBT src_key , src_val , dest_key , dest_val ;
toku_fill_dbt ( & src_key , l - > src_key . data , l - > src_key . len ) ;
toku_fill_dbt ( & src_val , l - > src_val . data , l - > src_val . len ) ;
toku_init_dbt_flags ( & dest_key , DB_DBT_REALLOC ) ;
toku_init_dbt_flags ( & dest_val , DB_DBT_REALLOC ) ;
for ( uint32_t file = 0 ; file < l - > dest_filenums . num ; file + + ) {
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > dest_filenums . filenums [ file ] , & tuple ) ;
if ( r = = 0 ) {
// We found the cachefile. (maybe) Do the insert.
DB * db = tuple - > brt - > db ;
r = renv - > generate_row_for_put ( db , src_db , & dest_key , & dest_val , & src_key , & src_val ) ;
assert ( r = = 0 ) ;
r = toku_brt_maybe_insert ( tuple - > brt , & dest_key , & dest_val , txn , TRUE , l - > lsn , FALSE , BRT_INSERT ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:33 -04:00
2013-04-16 23:59:33 -04:00
//flags==0 means generate_row_for_put callback changed it
//(and freed any memory necessary to do so) so that values are now stored
//in temporary memory that does not need to be freed. We need to continue
//using DB_DBT_REALLOC however.
if ( dest_key . flags = = 0 )
toku_init_dbt_flags ( & dest_key , DB_DBT_REALLOC ) ;
if ( dest_val . flags = = 0 )
toku_init_dbt_flags ( & dest_val , DB_DBT_REALLOC ) ;
}
2013-04-16 23:58:55 -04:00
}
2013-04-16 23:59:33 -04:00
if ( dest_key . data ) toku_free ( dest_key . data ) ; //TODO: #2321 May need windows hack
if ( dest_val . data ) toku_free ( dest_val . data ) ; //TODO: #2321 May need windows hack
2013-04-16 23:58:01 -04:00
}
2013-04-16 23:58:56 -04:00
return 0 ;
}
2013-04-16 23:59:05 -04:00
static int toku_recover_backward_enq_insert_multiple ( struct logtype_enq_insert_multiple * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:56 -04:00
// nothing
return 0 ;
}
2013-04-16 23:59:05 -04:00
static int toku_recover_enq_delete_multiple ( struct logtype_enq_delete_multiple * l , RECOVER_ENV renv ) {
2013-04-16 23:58:56 -04:00
int r ;
TOKUTXN txn = NULL ;
2013-04-16 23:59:05 -04:00
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
2013-04-16 23:58:56 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
DB * src_db = NULL ;
2013-04-16 23:59:33 -04:00
BOOL do_deletes = TRUE ;
2013-04-16 23:59:05 -04:00
{
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > src_filenum , & tuple ) ;
if ( l - > src_filenum . fileid = = FILENUM_NONE . fileid )
assert ( r = = DB_NOTFOUND ) ;
else {
2013-04-16 23:59:33 -04:00
if ( r = = 0 )
src_db = tuple - > brt - > db ;
else
2013-04-16 23:59:33 -04:00
do_deletes = FALSE ; // src file was probably deleted, #3129
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:58:56 -04:00
}
2013-04-16 23:59:33 -04:00
2013-04-16 23:59:33 -04:00
if ( do_deletes ) {
DBT src_key , src_val , dest_key ;
toku_fill_dbt ( & src_key , l - > src_key . data , l - > src_key . len ) ;
toku_fill_dbt ( & src_val , l - > src_val . data , l - > src_val . len ) ;
toku_init_dbt_flags ( & dest_key , DB_DBT_REALLOC ) ;
for ( uint32_t file = 0 ; file < l - > dest_filenums . num ; file + + ) {
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > dest_filenums . filenums [ file ] , & tuple ) ;
if ( r = = 0 ) {
// We found the cachefile. (maybe) Do the delete.
DB * db = tuple - > brt - > db ;
r = renv - > generate_row_for_del ( db , src_db , & dest_key , & src_key , & src_val ) ;
assert ( r = = 0 ) ;
r = toku_brt_maybe_delete ( tuple - > brt , & dest_key , txn , TRUE , l - > lsn , FALSE ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:33 -04:00
2013-04-16 23:59:33 -04:00
//flags==0 indicates the return values are stored in temporary memory that does
//not need to be freed. We need to continue using DB_DBT_REALLOC however.
if ( dest_key . flags = = 0 )
toku_init_dbt_flags ( & dest_key , DB_DBT_REALLOC ) ;
}
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:59:33 -04:00
if ( dest_key . flags & DB_DBT_REALLOC & & dest_key . data ) toku_free ( dest_key . data ) ; //TODO: #2321 May need windows hack
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:58:56 -04:00
return 0 ;
}
2013-04-16 23:59:05 -04:00
static int toku_recover_backward_enq_delete_multiple ( struct logtype_enq_delete_multiple * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:58:56 -04:00
// nothing
return 0 ;
}
2013-04-16 23:59:37 -04:00
static int toku_recover_enq_update ( struct logtype_enq_update * l , RECOVER_ENV renv ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
if ( r = = 0 ) {
// Maybe do the update if we found the cachefile.
DBT key , extra ;
toku_fill_dbt ( & key , l - > key . data , l - > key . len ) ;
toku_fill_dbt ( & extra , l - > extra . data , l - > extra . len ) ;
r = toku_brt_maybe_update ( tuple - > brt , & key , & extra , txn , TRUE , l - > lsn ,
FALSE ) ;
assert ( r = = 0 ) ;
}
return 0 ;
}
static int toku_recover_enq_updatebroadcast ( struct logtype_enq_updatebroadcast * l , RECOVER_ENV renv ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
struct file_map_tuple * tuple = NULL ;
r = file_map_find ( & renv - > fmap , l - > filenum , & tuple ) ;
if ( r = = 0 ) {
// Maybe do the update broadcast if we found the cachefile.
DBT extra ;
toku_fill_dbt ( & extra , l - > extra . data , l - > extra . len ) ;
r = toku_brt_maybe_update_broadcast ( tuple - > brt , & extra , txn , TRUE ,
l - > lsn , FALSE , l - > is_resetting_op ) ;
assert ( r = = 0 ) ;
}
return 0 ;
}
static int toku_recover_backward_enq_update ( struct logtype_enq_update * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_backward_enq_updatebroadcast ( struct logtype_enq_updatebroadcast * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
2013-04-16 23:58:53 -04:00
static int toku_recover_comment ( struct logtype_comment * 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:53 -04:00
static int toku_recover_backward_comment ( struct logtype_comment * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:57:59 -04:00
// nothing
2013-04-16 23:58:59 -04:00
return 0 ;
}
2013-04-16 23:59:17 -04:00
static int toku_recover_shutdown ( struct logtype_shutdown * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
static int toku_recover_backward_shutdown ( struct logtype_shutdown * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
2013-04-16 23:58:59 -04:00
static int toku_recover_load ( struct logtype_load * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
2013-04-16 23:59:03 -04:00
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
2013-04-16 23:59:05 -04:00
assert ( txn ! = NULL ) ;
2013-04-16 23:59:03 -04:00
char * old_iname = fixup_fname ( & l - > old_iname ) ;
char * new_iname = fixup_fname ( & l - > new_iname ) ;
2013-04-16 23:59:05 -04:00
r = toku_brt_load_recovery ( txn , old_iname , new_iname , 0 , 0 , ( LSN * ) NULL ) ;
2013-04-16 23:59:03 -04:00
assert ( r = = 0 ) ;
toku_free ( old_iname ) ;
toku_free ( new_iname ) ;
2013-04-16 23:58:59 -04:00
return 0 ;
}
static int toku_recover_backward_load ( struct logtype_load * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
2013-04-16 23:57:56 -04:00
return 0 ;
}
2013-04-16 23:59:29 -04:00
// #2954
static int toku_recover_hot_index ( struct logtype_hot_index * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
int r ;
TOKUTXN txn = NULL ;
r = toku_txnid2txn ( renv - > logger , l - > xid , & txn ) ;
assert ( r = = 0 ) ;
assert ( txn ! = NULL ) ;
// just make an entry in the rollback log
// - set do_log = 0 -> don't write to recovery log
r = toku_brt_hot_index_recovery ( txn , l - > hot_index_filenums , 0 , 0 , ( LSN * ) NULL ) ;
assert ( r = = 0 ) ;
return 0 ;
}
// #2954
static int toku_recover_backward_hot_index ( struct logtype_hot_index * UU ( l ) , RECOVER_ENV UU ( renv ) ) {
// nothing
return 0 ;
}
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:59:17 -04:00
if ( le - > cmd = = LT_shutdown | | le - > cmd = = LT_comment ) {
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 ;
}
2013-04-16 23:59:05 -04:00
if ( le - > u . begin_checkpoint . lsn . lsn ! = end_checkpoint . u . end_checkpoint . xid ) {
2013-04-16 23:57:59 -04:00
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:53 -04:00
static uint32_t recover_get_num_live_txns ( RECOVER_ENV renv ) {
return toku_omt_size ( renv - > logger - > live_txns ) ;
}
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 ;
2013-04-16 23:59:46 -04:00
r = toku_omt_fetch ( renv - > logger - > live_txns , n_live_txns - 1 , & v ) ;
2013-04-16 23:58:02 -04:00
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
2013-04-17 00:00:02 -04:00
r = toku_txn_abort_txn ( txn , recover_yield , NULL , NULL , NULL , false ) ;
2013-04-16 23:58:02 -04:00
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:53 -04:00
static void recover_trace_le ( const char * f , int l , int r , struct log_entry * le ) {
if ( le ) {
LSN thislsn = toku_log_entry_get_lsn ( le ) ;
fprintf ( stderr , " %s:%d r=%d cmd=%c lsn=% " PRIu64 " \n " , f , l , r , le - > cmd , thislsn . lsn ) ;
} else
fprintf ( stderr , " %s:%d r=%d cmd=? \n " , f , l , r ) ;
}
2013-04-16 23:58:56 -04:00
// For test purposes only.
static void ( * recover_callback_fx ) ( void * ) = NULL ;
static void * recover_callback_args = NULL ;
static void ( * recover_callback2_fx ) ( void * ) = NULL ;
static void * recover_callback2_args = NULL ;
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 ;
2013-04-16 23:58:53 -04:00
time_t tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery starting \n " , ctime ( & tnow ) ) ;
2013-04-16 23:58:52 -04:00
2008-03-12 19:40:38 +00:00
char org_wd [ 1000 ] ;
{
char * wd = getcwd ( org_wd , sizeof ( org_wd ) ) ;
assert ( wd ! = 0 ) ;
}
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:58:53 -04:00
LSN thislsn ;
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:53 -04:00
if ( tokudb_recovery_trace )
fprintf ( stderr , " RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:58:00 -04:00
rr = DB_RUNRECOVERY ; goto errorexit ;
}
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:53 -04:00
scan_state_init ( & renv - > ss ) ;
2013-04-16 23:58:53 -04:00
tnow = time ( NULL ) ;
2013-04-16 23:58:53 -04:00
time_t tlast = tnow ;
2013-04-16 23:58:53 -04:00
fprintf ( stderr , " %.24s Tokudb recovery scanning backward from % " PRIu64 " \n " , ctime ( & tnow ) , lastlsn . lsn ) ;
2013-04-16 23:58:53 -04:00
for ( unsigned i = 0 ; 1 ; i + + ) {
2013-04-16 23:58:56 -04:00
// get the previous log entry (first time gets the last one)
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:58:53 -04:00
if ( tokudb_recovery_trace )
recover_trace_le ( __FUNCTION__ , __LINE__ , r , le ) ;
2013-04-16 23:58:00 -04:00
if ( r ! = 0 ) {
2013-04-16 23:59:02 -04:00
if ( r = = DB_NOTFOUND )
break ;
rr = DB_RUNRECOVERY ;
goto errorexit ;
2013-04-16 23:58:00 -04:00
}
2013-04-16 23:58:56 -04:00
// trace progress
2013-04-16 23:58:53 -04:00
if ( ( i % 1000 ) = = 0 ) {
tnow = time ( NULL ) ;
if ( tnow - tlast > = TOKUDB_RECOVERY_PROGRESS_TIME ) {
thislsn = toku_log_entry_get_lsn ( le ) ;
2013-04-16 23:58:54 -04:00
fprintf ( stderr , " %.24s Tokudb recovery scanning backward from % " PRIu64 " at % " PRIu64 " (%s) \n " , ctime ( & tnow ) , lastlsn . lsn , thislsn . lsn , recover_state ( renv ) ) ;
2013-04-16 23:58:53 -04:00
tlast = tnow ;
}
}
2013-04-16 23:58:56 -04:00
// dispatch the log entry handler
2013-04-16 23:59:05 -04:00
assert ( renv - > ss . ss = = BACKWARD_BETWEEN_CHECKPOINT_BEGIN_END | |
2013-04-16 23:58:56 -04:00
renv - > ss . ss = = BACKWARD_NEWER_CHECKPOINT_END ) ;
2013-04-16 23:58:01 -04:00
logtype_dispatch_assign ( le , toku_recover_backward_ , r , renv ) ;
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
recover_trace_le ( __FUNCTION__ , __LINE__ , r , le ) ;
2013-04-16 23:57:59 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
fprintf ( stderr , " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:59:03 -04:00
rr = DB_RUNRECOVERY ;
goto errorexit ;
2013-04-16 23:58:03 -04:00
}
2013-04-16 23:58:56 -04:00
if ( renv - > goforward )
2013-04-16 23:57:58 -04:00
break ;
2013-04-16 23:57:53 -04:00
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:58:56 -04:00
// run first callback
if ( recover_callback_fx )
recover_callback_fx ( recover_callback_args ) ;
2013-04-16 23:57:59 -04:00
// scan forwards
2013-04-16 23:58:56 -04:00
assert ( le ) ;
thislsn = toku_log_entry_get_lsn ( le ) ;
2013-04-16 23:58:53 -04:00
tnow = time ( NULL ) ;
2013-04-16 23:58:56 -04:00
fprintf ( stderr , " %.24s Tokudb recovery starts scanning forward to % " PRIu64 " from % " PRIu64 " left % " PRIu64 " (%s) \n " , ctime ( & tnow ) , lastlsn . lsn , thislsn . lsn , lastlsn . lsn - thislsn . lsn , recover_state ( renv ) ) ;
2013-04-16 23:58:53 -04:00
for ( unsigned i = 0 ; 1 ; i + + ) {
2013-04-16 23:58:56 -04:00
// trace progress
2013-04-16 23:58:53 -04:00
if ( ( i % 1000 ) = = 0 ) {
tnow = time ( NULL ) ;
if ( tnow - tlast > = TOKUDB_RECOVERY_PROGRESS_TIME ) {
thislsn = toku_log_entry_get_lsn ( le ) ;
2013-04-16 23:58:54 -04:00
fprintf ( stderr , " %.24s Tokudb recovery scanning forward to % " PRIu64 " at % " PRIu64 " left % " PRIu64 " (%s) \n " , ctime ( & tnow ) , lastlsn . lsn , thislsn . lsn , lastlsn . lsn - thislsn . lsn , recover_state ( renv ) ) ;
2013-04-16 23:58:53 -04:00
tlast = tnow ;
}
}
2013-04-16 23:58:56 -04:00
// dispatch the log entry handler (first time calls the forward handler for the log entry at the turnaround
2013-04-16 23:59:05 -04:00
assert ( renv - > ss . ss = = FORWARD_BETWEEN_CHECKPOINT_BEGIN_END | |
2013-04-16 23:58:56 -04:00
renv - > ss . ss = = FORWARD_NEWER_CHECKPOINT_END ) ;
2013-04-16 23:58:03 -04:00
logtype_dispatch_assign ( le , toku_recover_ , r , renv ) ;
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
recover_trace_le ( __FUNCTION__ , __LINE__ , r , le ) ;
2013-04-16 23:58:03 -04:00
if ( r ! = 0 ) {
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
fprintf ( stderr , " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , r ) ;
2013-04-16 23:59:02 -04:00
rr = DB_RUNRECOVERY ;
goto errorexit ;
2013-04-16 23:58:03 -04:00
}
2013-04-16 23:58:56 -04:00
// get the next log entry
le = NULL ;
r = toku_logcursor_next ( logcursor , & le ) ;
if ( tokudb_recovery_trace )
recover_trace_le ( __FUNCTION__ , __LINE__ , r , le ) ;
if ( r ! = 0 ) {
2013-04-16 23:59:02 -04:00
if ( r = = DB_NOTFOUND )
break ;
rr = DB_RUNRECOVERY ;
goto errorexit ;
2013-04-16 23:58:56 -04:00
}
2007-11-23 18:27:50 +00:00
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:58:56 -04:00
// verify the final recovery state
assert ( renv - > ss . ss = = FORWARD_NEWER_CHECKPOINT_END ) ;
2013-04-16 23:57:59 -04:00
r = toku_logcursor_destroy ( & logcursor ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:58:56 -04:00
// run second callback
if ( recover_callback2_fx )
recover_callback2_fx ( recover_callback2_args ) ;
2013-04-16 23:57:59 -04:00
// 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:52 -04:00
// abort the live transactions
2013-04-16 23:58:53 -04:00
uint32_t n = recover_get_num_live_txns ( renv ) ;
if ( n > 0 ) {
tnow = time ( NULL ) ;
2013-04-16 23:58:53 -04:00
fprintf ( stderr , " %.24s Tokudb recovery aborting % " PRIu32 " live transaction%s \n " , ctime ( & tnow ) , n , n > 1 ? " s " : " " ) ;
2013-04-16 23:58:53 -04:00
}
2013-04-16 23:58:02 -04:00
recover_abort_live_txns ( renv ) ;
2013-04-16 23:57:59 -04:00
// close the open dictionaries
2013-04-16 23:58:53 -04:00
n = file_map_get_num_dictionaries ( & renv - > fmap ) ;
if ( n > 0 ) {
tnow = time ( NULL ) ;
2013-04-16 23:58:53 -04:00
fprintf ( stderr , " %.24s Tokudb recovery closing % " PRIu32 " dictionar%s \n " , ctime ( & tnow ) , n , n > 1 ? " ies " : " y " ) ;
2013-04-16 23:58:53 -04:00
}
2013-04-16 23:59:05 -04:00
file_map_close_dictionaries ( & renv - > fmap , TRUE , renv - > logger ) ;
2013-04-16 23:57:59 -04:00
// write a recovery log entry
BYTESTRING recover_comment = { strlen ( " recover " ) , " recover " } ;
2013-04-16 23:58:53 -04:00
r = toku_log_comment ( renv - > logger , NULL , TRUE , 0 , recover_comment ) ;
2013-04-16 23:57:59 -04:00
assert ( r = = 0 ) ;
// checkpoint
2013-04-16 23:58:53 -04:00
tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery making a checkpoint \n " , ctime ( & tnow ) ) ;
2013-04-17 00:00:02 -04:00
r = toku_checkpoint ( renv - > ct , renv - > logger , NULL , NULL , NULL , NULL , RECOVERY_CHECKPOINT ) ;
2013-04-16 23:58:01 -04:00
assert ( r = = 0 ) ;
2013-04-16 23:58:53 -04:00
tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery done \n " , ctime ( & tnow ) ) ;
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 :
2013-04-16 23:58:53 -04:00
tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery failed %d \n " , ctime ( & tnow ) , rr ) ;
2013-04-16 23:58:52 -04:00
2013-04-16 23:58:00 -04:00
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:59:17 -04:00
int
toku_recover_lock ( const char * lock_dir , int * lockfd ) {
2013-04-16 23:58:54 -04:00
if ( ! lock_dir )
return ENOENT ;
2013-04-16 23:58:00 -04:00
int namelen = strlen ( lock_dir ) ;
2013-04-16 23:59:17 -04:00
char lockfname [ namelen + sizeof ( recovery_lock_file ) ] ;
2013-04-16 23:58:00 -04:00
2013-04-16 23:59:17 -04:00
int l = snprintf ( lockfname , sizeof ( lockfname ) , " %s%s " , lock_dir , recovery_lock_file ) ;
2013-04-16 23:58:00 -04:00
assert ( l + 1 = = ( signed ) ( sizeof ( lockfname ) ) ) ;
* lockfd = toku_os_lock_file ( lockfname ) ;
if ( * lockfd < 0 ) {
int e = errno ;
2013-04-16 23:58:53 -04:00
fprintf ( stderr , " Couldn't run recovery because some other process holds the recovery lock %s \n " , lockfname ) ;
2013-04-16 23:58:00 -04:00
return e ;
}
return 0 ;
}
2013-04-16 23:59:17 -04:00
int
toku_recover_unlock ( int lockfd ) {
2013-04-16 23:58:00 -04:00
int r = toku_os_unlock_file ( lockfd ) ;
if ( r ! = 0 )
return errno ;
return 0 ;
}
2013-04-16 23:59:17 -04:00
2013-04-16 23:58:58 -04:00
int tokudb_recover ( const char * env_dir , const char * log_dir ,
brt_compare_func bt_compare ,
2013-04-16 23:59:37 -04:00
brt_update_func update_function ,
2013-04-16 23:58:59 -04:00
generate_row_for_put_func generate_row_for_put ,
generate_row_for_del_func generate_row_for_del ,
2013-04-16 23:58:58 -04:00
size_t cachetable_size ) {
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:59:17 -04:00
r = toku_recover_lock ( log_dir , & lockfd ) ;
2013-04-16 23:58:00 -04:00
if ( r ! = 0 )
return r ;
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 ;
2013-04-16 23:59:21 -04:00
r = recover_env_init ( & renv , bt_compare ,
2013-04-16 23:59:37 -04:00
update_function ,
2013-04-16 23:58:59 -04:00
generate_row_for_put ,
generate_row_for_del ,
2013-04-16 23:58:58 -04:00
cachetable_size ) ;
2013-04-16 23:57:59 -04:00
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:59:17 -04:00
r = toku_recover_unlock ( lockfd ) ;
2013-04-16 23:58:00 -04:00
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 ;
}
2013-04-16 23:58:56 -04:00
void toku_recover_set_callback ( void ( * callback_fx ) ( void * ) , void * callback_args ) {
recover_callback_fx = callback_fx ;
recover_callback_args = callback_args ;
}
void toku_recover_set_callback2 ( void ( * callback_fx ) ( void * ) , void * callback_args ) {
recover_callback2_fx = callback_fx ;
recover_callback2_args = callback_args ;
}