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
2007-11-23 18:27:50 +00:00
/* Recover an env. The logs are in argv[1]. The new database is created in the cwd. */
// Test:
// cd ../src/tests/tmpdir
// ../../../newbrt/recover ../dir.test_log2.c.tdb
2013-04-16 23:57:20 -04:00
# include "includes.h"
2007-11-23 20:36:03 +00: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
static DB * const null_db = 0 ;
2013-04-16 23:57:44 -04:00
static TOKULOGGER const null_tokulogger = 0 ;
2008-03-14 19:14:31 +00:00
// These data structures really should be part of a recovery data structure. Recovery could be multithreaded (on different environments...) But this is OK since recovery can only happen in one
static CACHETABLE ct ;
static struct cf_pair {
FILENUM filenum ;
CACHEFILE cf ;
BRT brt ; // set to zero on an fopen, but filled in when an fheader is seen.
} * cf_pairs ;
2013-04-16 23:57:20 -04:00
static int n_cf_pairs = 0 , max_cf_pairs = 0 ;
2008-03-14 19:14:31 +00:00
int toku_recover_init ( void ) {
int r = toku_create_cachetable ( & ct , 1 < < 25 , ( LSN ) { 0 } , 0 ) ;
return r ;
}
void toku_recover_cleanup ( void ) {
int i ;
for ( i = 0 ; i < n_cf_pairs ; i + + ) {
if ( cf_pairs [ i ] . brt ) {
2013-04-16 23:57:38 -04:00
int r = toku_close_brt ( cf_pairs [ i ] . brt , 0 , 0 ) ;
2008-03-14 19:14:31 +00:00
//r = toku_cachefile_close(&cf_pairs[i].cf);
assert ( r = = 0 ) ;
}
}
toku_free ( cf_pairs ) ;
{
int r = toku_cachetable_close ( & ct ) ;
assert ( r = = 0 ) ;
}
}
2013-04-16 23:57:53 -04:00
enum backward_state { BS_INIT , BS_SAW_CKPT_END , BS_SAW_CKPT } ;
struct backward_scan_state {
enum backward_state bs ;
LSN checkpoint_lsn ;
int n_live_txns ;
LSN min_live_txn ;
} ;
2013-04-16 23:57:54 -04:00
static struct backward_scan_state initial_bss = { BS_INIT , { 0 } , 0 , { 0 } } ;
2013-04-16 23:57:53 -04:00
2013-04-16 23:57:20 -04:00
static void
toku_recover_commit ( LSN UU ( lsn ) , TXNID UU ( txnid ) ) {
2008-03-14 19:14:31 +00:00
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_commit ( struct logtype_commit * UU ( l ) , struct backward_scan_state * UU ( bs ) ) {
return 0 ;
}
2013-04-16 23:57:20 -04:00
static void
toku_recover_xabort ( LSN UU ( lsn ) , TXNID UU ( txnid ) ) {
2008-04-21 04:22:45 +00:00
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_xabort ( struct logtype_xabort * UU ( l ) , struct backward_scan_state * UU ( bs ) ) {
return 0 ;
}
2008-03-14 19:14:31 +00:00
2013-04-16 23:57:20 -04:00
static void
create_dir_from_file ( const char * fname ) {
2008-04-18 14:45:04 +00:00
int i ;
char * tmp = toku_strdup ( fname ) ;
char ch ;
for ( 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 ) ) ;
}
assert ( r = = 0 | | ( errno = = EEXIST ) ) ;
umask ( oldu ) ;
tmp [ i ] = ch ;
}
}
}
toku_free ( tmp ) ;
}
2013-04-16 23:57:20 -04:00
static int
toku_recover_note_cachefile ( FILENUM fnum , CACHEFILE cf , BRT brt ) {
2008-03-14 19:14:31 +00:00
if ( max_cf_pairs = = 0 ) {
n_cf_pairs = 1 ;
max_cf_pairs = 2 ;
MALLOC_N ( max_cf_pairs , cf_pairs ) ;
if ( cf_pairs = = 0 ) return errno ;
} else {
if ( n_cf_pairs > = max_cf_pairs ) {
max_cf_pairs * = 2 ;
cf_pairs = toku_realloc ( cf_pairs , max_cf_pairs * sizeof ( * cf_pairs ) ) ;
}
n_cf_pairs + + ;
}
cf_pairs [ n_cf_pairs - 1 ] . filenum = fnum ;
cf_pairs [ n_cf_pairs - 1 ] . cf = cf ;
cf_pairs [ n_cf_pairs - 1 ] . brt = brt ;
return 0 ;
}
2013-04-16 23:57:53 -04:00
static int find_cachefile ( FILENUM fnum , struct cf_pair * * cf_pair ) {
int i ;
for ( i = 0 ; i < n_cf_pairs ; i + + ) {
if ( fnum . fileid = = cf_pairs [ i ] . filenum . fileid ) {
* cf_pair = cf_pairs + i ;
return 0 ;
}
}
return 1 ;
}
2013-04-16 23:57:44 -04:00
static void
2013-04-16 23:57:53 -04:00
internal_toku_recover_fopen_or_fcreate ( int flags , int mode , char * fixedfname , FILENUM filenum )
// Open the file if it is not already open. If it is already open, then do nothing.
{
{
struct cf_pair * pair = NULL ;
int r = find_cachefile ( filenum , & pair ) ;
if ( 0 = = r ) {
toku_free ( fixedfname ) ;
return ;
}
}
2013-04-16 23:57:44 -04:00
CACHEFILE cf ;
int fd = open ( fixedfname , O_RDWR | O_BINARY | flags , mode ) ;
2013-04-16 23:57:53 -04:00
if ( fd < 0 ) {
char org_wd [ 1000 ] ;
char * wd = getcwd ( org_wd , sizeof ( org_wd ) ) ;
fprintf ( stderr , " %s:%d Could not open file %s, cwd=%s, errno=%d (%s) \n " ,
__FILE__ , __LINE__ , fixedfname , wd , errno , strerror ( errno ) ) ;
}
2013-04-16 23:57:44 -04:00
assert ( fd > = 0 ) ;
BRT brt = 0 ;
int r = toku_brt_create ( & brt ) ;
assert ( r = = 0 ) ;
brt - > fname = fixedfname ;
brt - > h = 0 ;
brt - > compare_fun = toku_default_compare_fun ; // we'll need to set these to the right comparison function, or do without them.
brt - > dup_compare = toku_default_compare_fun ;
brt - > db = 0 ;
r = toku_cachetable_openfd ( & cf , ct , fd , fixedfname ) ;
assert ( r = = 0 ) ;
brt - > cf = cf ;
r = toku_read_brt_header_and_store_in_cachefile ( brt - > cf , & brt - > h ) ;
2013-04-16 23:57:49 -04:00
if ( r = = TOKUDB_DICTIONARY_NO_HEADER ) {
2013-04-16 23:57:47 -04:00
r = toku_brt_alloc_init_header ( brt ) ;
2013-04-16 23:57:44 -04:00
}
toku_recover_note_cachefile ( filenum , cf , brt ) ;
}
static void
toku_recover_fopen ( LSN UU ( lsn ) , TXNID UU ( txnid ) , BYTESTRING fname , FILENUM filenum ) {
char * fixedfname = fixup_fname ( & fname ) ;
toku_free_BYTESTRING ( fname ) ;
internal_toku_recover_fopen_or_fcreate ( 0 , 0 , fixedfname , filenum ) ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_fopen ( struct logtype_fopen * l , struct backward_scan_state * UU ( bs ) ) {
toku_free_BYTESTRING ( l - > fname ) ;
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.
static void
toku_recover_fcreate ( LSN UU ( lsn ) , TXNID UU ( txnid ) , FILENUM filenum , BYTESTRING fname , u_int32_t mode ) {
char * fixedfname = fixup_fname ( & fname ) ;
toku_free_BYTESTRING ( fname ) ;
create_dir_from_file ( fixedfname ) ;
internal_toku_recover_fopen_or_fcreate ( O_CREAT | O_TRUNC , mode , fixedfname , filenum ) ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_fcreate ( struct logtype_fcreate * l , struct backward_scan_state * UU ( bs ) ) {
toku_free_BYTESTRING ( l - > fname ) ;
return 0 ;
}
2013-04-16 23:57:20 -04:00
static void
toku_recover_enqrootentry ( LSN lsn __attribute__ ( ( __unused__ ) ) , FILENUM filenum , TXNID xid , u_int32_t typ , BYTESTRING key , BYTESTRING val ) {
2008-04-09 02:45:27 +00:00
struct cf_pair * pair = NULL ;
int r = find_cachefile ( filenum , & pair ) ;
2013-04-16 23:57:53 -04:00
if ( r ! = 0 ) {
// if we didn't find a cachefile, then we don't have to do anything.
return ;
}
2013-04-16 23:57:44 -04:00
struct brt_cmd cmd ;
DBT keydbt , valdbt ;
2013-04-16 23:57:44 -04:00
cmd . type = ( enum brt_cmd_type ) typ ;
2013-04-16 23:57:44 -04:00
cmd . xid = xid ;
cmd . u . id . key = toku_fill_dbt ( & keydbt , key . data , key . len ) ;
cmd . u . id . val = toku_fill_dbt ( & valdbt , val . data , val . len ) ;
r = toku_brt_root_put_cmd ( pair - > brt , & cmd , null_tokulogger ) ;
2008-04-09 02:45:27 +00:00
assert ( r = = 0 ) ;
toku_free ( key . data ) ;
toku_free ( val . data ) ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_enqrootentry ( struct logtype_enqrootentry * l , struct backward_scan_state * UU ( bs ) ) {
toku_free_BYTESTRING ( l - > key ) ;
toku_free_BYTESTRING ( l - > data ) ;
return 0 ;
}
2013-04-16 23:57:20 -04:00
static void
2013-04-16 23:57:55 -04:00
toku_recover_fclose ( LSN UU ( lsn ) , BYTESTRING UU ( fname ) , FILENUM filenum ) {
2008-04-17 03:11:55 +00:00
struct cf_pair * pair = NULL ;
int r = find_cachefile ( filenum , & pair ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:38 -04:00
r = toku_close_brt ( pair - > brt , 0 , 0 ) ;
2008-04-17 03:11:55 +00:00
assert ( r = = 0 ) ;
pair - > brt = 0 ;
toku_free_BYTESTRING ( fname ) ;
}
2013-04-16 23:57:55 -04:00
static int toku_recover_backward_fclose ( struct logtype_fclose * l , struct backward_scan_state * UU ( bs ) ) {
2013-04-16 23:57:53 -04:00
toku_free_BYTESTRING ( l - > fname ) ;
return 0 ;
}
2013-04-16 23:57:47 -04:00
static int toku_recover_begin_checkpoint ( LSN UU ( lsn ) ) {
return 0 ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_begin_checkpoint ( struct logtype_begin_checkpoint * l , struct backward_scan_state * bs ) {
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 ) ;
return 1 ;
}
else return 0 ;
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:57:47 -04:00
static int toku_recover_end_checkpoint ( LSN UU ( lsn ) , TXNID UU ( txnid ) ) {
return 0 ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_end_checkpoint ( struct logtype_end_checkpoint * l , struct backward_scan_state * bs ) {
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:57:53 -04:00
// going backward we open it, so going forward we don't have to do anything
static int toku_recover_fassociate ( LSN UU ( lsn ) , FILENUM UU ( filenum ) , BYTESTRING UU ( fname ) ) {
2013-04-16 23:57:53 -04:00
return 0 ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_fassociate ( struct logtype_fassociate * l , struct backward_scan_state * UU ( bs ) ) {
char * fixedfname = fixup_fname ( & l - > fname ) ;
toku_free_BYTESTRING ( l - > fname ) ;
internal_toku_recover_fopen_or_fcreate ( 0 , 0 , fixedfname , l - > filenum ) ;
2013-04-16 23:57:47 -04:00
return 0 ;
}
2013-04-16 23:57:55 -04:00
static int toku_recover_xstillopen ( LSN UU ( lsn ) , TXNID UU ( txnid ) , TXNID UU ( parent ) ) {
2008-03-21 19:40:32 +00:00
return 0 ;
}
2008-03-14 19:14:31 +00:00
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_xstillopen ( struct logtype_xstillopen * l , struct backward_scan_state * bs ) {
switch ( bs - > bs ) {
case BS_INIT :
return 0 ; // ignore live txns from incomplete checkpoint
case BS_SAW_CKPT_END :
if ( bs - > n_live_txns = = 0 ) {
bs - > min_live_txn = l - > lsn ;
} else {
if ( bs - > min_live_txn . lsn > l - > txnid ) bs - > min_live_txn = l - > lsn ;
}
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 ( ) ;
}
2008-03-21 21:02:30 +00:00
static int toku_recover_xbegin ( LSN UU ( lsn ) , TXNID UU ( parent ) ) {
return 0 ;
}
2013-04-16 23:57:53 -04:00
static int toku_recover_backward_xbegin ( struct logtype_xbegin * l , struct backward_scan_state * bs ) {
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
if ( bs - > min_live_txn . lsn > = l - > lsn . lsn ) {
fprintf ( stderr , " Turning around at xbegin % " PRIu64 " \n " , l - > lsn . lsn ) ;
return 1 ;
} else {
fprintf ( stderr , " scanning back at xbegin % " PRIu64 " (looking for % " PRIu64 " \n " , l - > lsn . lsn , bs - > min_live_txn . lsn ) ;
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: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 ) ;
if ( r = = 0 ) {
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 ;
}
2008-03-12 19:40:38 +00:00
int tokudb_recover ( const char * data_dir , const char * log_dir ) {
2013-04-16 23:57:39 -04:00
int failresult = 0 ;
2007-11-23 18:27:50 +00:00
int r ;
2007-12-26 16:52:55 +00:00
int entrycount = 0 ;
2007-11-23 18:27:50 +00:00
char * * logfiles ;
2008-03-12 19:40:38 +00:00
int lockfd ;
{
2013-04-16 23:57:27 -04:00
const char fname [ ] = " /__recoverylock_dont_delete_me " ;
2008-03-12 19:40:38 +00:00
int namelen = strlen ( data_dir ) ;
2013-04-16 23:57:27 -04:00
char lockfname [ namelen + sizeof ( fname ) ] ;
2008-03-12 19:40:38 +00:00
2013-04-16 23:57:42 -04:00
int l = snprintf ( lockfname , sizeof ( lockfname ) , " %s%s " , data_dir , fname ) ;
assert ( l + 1 = = ( signed ) ( sizeof ( lockfname ) ) ) ;
2013-04-16 23:57:28 -04:00
lockfd = toku_os_lock_file ( lockfname ) ;
2008-03-12 19:40:38 +00:00
if ( lockfd < 0 ) {
printf ( " Couldn't run recovery because some other process holds the recovery lock %s \n " , lockfname ) ;
2008-03-14 11:02:28 +00:00
return errno ;
2008-03-12 19:40:38 +00:00
}
}
2013-04-16 23:57:39 -04:00
r = toku_delete_rolltmp_files ( log_dir ) ;
if ( r ! = 0 ) { failresult = r ; goto fail ; }
2013-04-16 23:57:53 -04:00
int n_logfiles ;
r = toku_logger_find_logfiles ( log_dir , & logfiles , & n_logfiles ) ;
2013-04-16 23:57:39 -04:00
if ( r ! = 0 ) { failresult = r ; goto fail ; }
2007-11-23 18:27:50 +00:00
int i ;
2008-01-11 03:09:14 +00:00
toku_recover_init ( ) ;
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);
}
char data_wd [ 1000 ] ;
{
r = chdir ( data_dir ) ; assert ( r = = 0 ) ;
char * wd = getcwd ( data_wd , sizeof ( data_wd ) ) ;
assert ( wd ! = 0 ) ;
//printf("%s:%d data_wd=\"%s\"\n", __FILE__, __LINE__, data_wd);
}
2013-04-16 23:57:53 -04:00
2013-04-16 23:57:55 -04:00
LSN lastlsn = ZERO_LSN ;
2013-04-16 23:57:53 -04:00
FILE * f = NULL ;
for ( i = 0 ; i < n_logfiles ; i + + ) {
if ( f ) fclose ( f ) ;
r = chdir ( org_wd ) ;
assert ( r = = 0 ) ;
char * logfile = logfiles [ n_logfiles - i - 1 ] ;
f = fopen ( logfile , " r " ) ;
assert ( f ) ;
2013-04-16 23:57:55 -04:00
printf ( " Opened %s \n " , logfiles [ n_logfiles - i - 1 ] ) ;
2013-04-16 23:57:53 -04:00
r = fseek ( f , 0 , SEEK_END ) ; assert ( r = = 0 ) ;
struct log_entry le ;
struct backward_scan_state bs = initial_bss ;
2013-04-16 23:57:53 -04:00
r = chdir ( data_wd ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:53 -04:00
while ( 1 ) {
r = toku_log_fread_backward ( f , & le ) ;
if ( r = = - 1 ) break ; // Ran out of file
2013-04-16 23:57:55 -04:00
LSN thislsn = toku_log_entry_get_lsn ( & le ) ;
if ( lastlsn . lsn ! = 0 ) {
if ( thislsn . lsn ! = lastlsn . lsn - 1 )
2013-04-16 23:57:55 -04:00
printf ( " bw lastlsn=% " PRIu64 " lsn=% " PRIu64 " \n " , lastlsn . lsn , thislsn . lsn ) ;
2013-04-16 23:57:55 -04:00
//assert(thislsn.lsn == lastlsn.lsn - 1);
}
lastlsn = thislsn ;
2013-04-16 23:57:53 -04:00
logtype_dispatch_assign ( & le , toku_recover_backward_ , r , & bs ) ;
if ( r ! = 0 ) goto go_forward ;
}
}
i - - ;
// We got to the end of the last log
if ( f ) { r = fclose ( f ) ; assert ( r = = 0 ) ; }
// Now we go forward from this point
while ( n_logfiles - i - 1 < n_logfiles ) {
2007-12-22 20:56:20 +00:00
//fprintf(stderr, "Opening %s\n", logfiles[i]);
2013-04-16 23:57:53 -04:00
int j = n_logfiles - i - 1 ;
2008-03-12 19:40:38 +00:00
r = chdir ( org_wd ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:53 -04:00
f = fopen ( logfiles [ j ] , " r " ) ;
assert ( f ) ;
2007-11-23 18:27:50 +00:00
struct log_entry le ;
u_int32_t version ;
2013-04-16 23:57:53 -04:00
//printf("Reading file %d: %s\n", j, logfiles[j]);
2007-11-28 19:09:24 +00:00
r = toku_read_and_print_logmagic ( f , & version ) ;
2007-11-23 18:27:50 +00:00
assert ( r = = 0 & & version = = 0 ) ;
2013-04-16 23:57:53 -04:00
go_forward : // we have an open file, so go forward.
//printf("Going forward\n");
2008-03-12 19:40:38 +00:00
r = chdir ( data_wd ) ;
assert ( r = = 0 ) ;
2007-11-29 18:14:40 +00:00
while ( ( r = toku_log_fread ( f , & le ) ) = = 0 ) {
2013-04-16 23:57:53 -04:00
//printf("doing %c\n", le.cmd);
2013-04-16 23:57:55 -04:00
LSN thislsn = toku_log_entry_get_lsn ( & le ) ;
if ( lastlsn . lsn ! = thislsn . lsn ) {
2013-04-16 23:57:55 -04:00
printf ( " fw expectlsn=% " PRIu64 " lsn=% " PRIu64 " \n " , lastlsn . lsn , thislsn . lsn ) ;
2013-04-16 23:57:55 -04:00
}
// assert(lastlsn.lsn == thislsn.lsn);
lastlsn . lsn + = 1 ;
2008-02-08 22:16:02 +00:00
logtype_dispatch_args ( & le , toku_recover_ ) ;
2007-12-29 19:27:01 +00:00
entrycount + + ;
2007-11-23 18:27:50 +00:00
}
if ( r ! = EOF ) {
if ( r = = DB_BADFORMAT ) {
2008-01-25 21:50:07 +00:00
fprintf ( stderr , " Bad log format at record %d \n " , entrycount ) ;
2008-03-14 11:02:28 +00:00
return r ;
2007-11-23 18:27:50 +00:00
} else {
fprintf ( stderr , " Huh? %s \n " , strerror ( r ) ) ;
2008-03-14 11:02:28 +00:00
return r ;
2007-11-23 18:27:50 +00:00
}
}
fclose ( f ) ;
2013-04-16 23:57:53 -04:00
i - - ;
2007-11-23 18:27:50 +00:00
}
2008-01-11 03:09:14 +00:00
toku_recover_cleanup ( ) ;
2008-03-21 19:40:32 +00:00
for ( i = 0 ; logfiles [ i ] ; i + + ) {
2007-11-23 18:27:50 +00:00
toku_free ( logfiles [ i ] ) ;
}
toku_free ( logfiles ) ;
2008-03-12 19:40:38 +00:00
2013-04-16 23:57:28 -04:00
r = toku_os_unlock_file ( lockfd ) ;
2008-03-14 11:02:28 +00:00
if ( r ! = 0 ) return errno ;
r = chdir ( org_wd ) ;
if ( r ! = 0 ) return errno ;
2008-03-12 19:40:38 +00:00
//printf("%s:%d recovery successful! ls -l says\n", __FILE__, __LINE__);
//system("ls -l");
2007-11-23 18:27:50 +00:00
return 0 ;
2013-04-16 23:57:39 -04:00
fail :
toku_os_unlock_file ( lockfd ) ;
chdir ( org_wd ) ;
return failresult ;
2007-11-23 18:27:50 +00:00
}