2013-04-16 23:57:55 -04:00
/* -*- mode: C; c-basic-offset: 4 -*- */
2013-04-16 23:59:03 -04:00
# ident "$Id$"
2013-04-16 23:57:55 -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."
# include "includes.h"
2013-04-16 23:57:58 -04:00
enum lc_direction { LC_FORWARD , LC_BACKWARD , LC_FIRST , LC_LAST } ;
2013-04-16 23:57:55 -04:00
struct toku_logcursor {
2013-04-16 23:57:58 -04:00
char * logdir ; // absolute directory name
2013-04-16 23:57:55 -04:00
char * * logfiles ;
int n_logfiles ;
int cur_logfiles_index ;
FILE * cur_fp ;
2013-04-16 23:58:56 -04:00
size_t buffer_size ;
void * buffer ;
2013-04-16 23:57:56 -04:00
BOOL is_open ;
struct log_entry entry ;
BOOL entry_valid ;
2013-04-16 23:57:58 -04:00
LSN cur_lsn ;
enum lc_direction last_direction ;
2013-04-16 23:57:55 -04:00
} ;
2013-04-16 23:58:00 -04:00
# define LC_LSN_ERROR (DB_RUNRECOVERY)
2013-04-16 23:57:58 -04:00
static void lc_print_logcursor ( TOKULOGCURSOR lc ) __attribute__ ( ( unused ) ) ;
static void lc_print_logcursor ( TOKULOGCURSOR lc ) {
printf ( " lc = %p \n " , lc ) ;
printf ( " logdir = %s \n " , lc - > logdir ) ;
printf ( " logfiles = %p \n " , lc - > logfiles ) ;
for ( int lf = 0 ; lf < lc - > n_logfiles ; lf + + ) {
printf ( " logfile[%d] = %p (%s) \n " , lf , lc - > logfiles [ lf ] , lc - > logfiles [ lf ] ) ;
}
printf ( " n_logfiles = %d \n " , lc - > n_logfiles ) ;
printf ( " cur_logfiles_index = %d \n " , lc - > cur_logfiles_index ) ;
printf ( " cur_fp = %p \n " , lc - > cur_fp ) ;
2013-04-16 23:57:58 -04:00
printf ( " cur_lsn = % " PRIu64 " \n " , lc - > cur_lsn . lsn ) ;
2013-04-16 23:58:00 -04:00
printf ( " last_direction = %d \n " , ( int ) lc - > last_direction ) ;
2013-04-16 23:57:58 -04:00
}
2013-04-16 23:57:55 -04:00
static int lc_close_cur_logfile ( TOKULOGCURSOR lc ) {
int r = 0 ;
if ( lc - > is_open ) {
r = fclose ( lc - > cur_fp ) ;
assert ( 0 = = r ) ;
2013-04-16 23:57:56 -04:00
lc - > is_open = FALSE ;
2013-04-16 23:57:55 -04:00
}
return 0 ;
}
2013-04-16 23:59:00 -04:00
2013-04-16 23:59:01 -04:00
static toku_off_t lc_file_len ( const char * name ) {
2013-04-16 23:59:00 -04:00
toku_struct_stat buf ;
int r = toku_stat ( name , & buf ) ; assert ( r = = 0 ) ;
return buf . st_size ;
}
2013-04-16 23:59:02 -04:00
// Cat the file and throw away the contents. This brings the file into the file system cache
// and makes subsequent accesses to it fast. The intention is to speed up backward scans of the
// file.
static void lc_catfile ( const char * fname , void * buffer , size_t buffer_size ) {
2013-04-16 23:59:01 -04:00
int fd = open ( fname , O_RDONLY ) ;
if ( fd > = 0 ) {
while ( 1 ) {
ssize_t r = read ( fd , buffer , buffer_size ) ;
if ( ( int ) r < = 0 )
break ;
}
2013-04-16 23:59:02 -04:00
close ( fd ) ;
2013-04-16 23:59:01 -04:00
}
}
2013-04-16 23:57:55 -04:00
static int lc_open_logfile ( TOKULOGCURSOR lc , int index ) {
int r = 0 ;
2013-04-16 23:57:56 -04:00
assert ( ! lc - > is_open ) ;
2013-04-16 23:57:58 -04:00
if ( index = = - 1 | | index > = lc - > n_logfiles ) return DB_NOTFOUND ;
2013-04-16 23:59:02 -04:00
lc_catfile ( lc - > logfiles [ index ] , lc - > buffer , lc - > buffer_size ) ;
2013-04-16 23:58:54 -04:00
lc - > cur_fp = fopen ( lc - > logfiles [ index ] , " rb " ) ;
2013-04-16 23:57:55 -04:00
if ( lc - > cur_fp = = NULL )
return DB_NOTFOUND ;
2013-04-16 23:58:56 -04:00
// debug printf("%s:%d %s %p %u\n", __FUNCTION__, __LINE__, lc->logfiles[index], lc->buffer, (unsigned) lc->buffer_size);
2013-04-16 23:58:58 -04:00
# if !TOKU_WINDOWS //Windows reads logs fastest if we use default settings (not use setvbuf to change buffering)
2013-04-16 23:58:56 -04:00
r = setvbuf ( lc - > cur_fp , lc - > buffer , _IOFBF , lc - > buffer_size ) ;
assert ( r = = 0 ) ;
2013-04-16 23:58:58 -04:00
# endif
2013-04-16 23:59:00 -04:00
// position fp past header, ignore 0 length file (t:2384)
2013-04-16 23:57:55 -04:00
unsigned int version = 0 ;
2013-04-16 23:59:00 -04:00
if ( lc_file_len ( lc - > logfiles [ index ] ) > = 12 ) {
r = toku_read_logmagic ( lc - > cur_fp , & version ) ;
if ( r ! = 0 )
return DB_BADFORMAT ;
if ( version ! = TOKU_LOG_VERSION )
return DB_BADFORMAT ;
}
2013-04-16 23:57:55 -04:00
// mark as open
2013-04-16 23:57:56 -04:00
lc - > is_open = TRUE ;
2013-04-16 23:57:55 -04:00
return r ;
}
2013-04-16 23:57:58 -04:00
static int lc_check_lsn ( TOKULOGCURSOR lc , int dir ) {
int r = 0 ;
LSN lsn = toku_log_entry_get_lsn ( & ( lc - > entry ) ) ;
if ( ( ( dir = = LC_FORWARD ) & & ( lsn . lsn ! = lc - > cur_lsn . lsn + 1 ) ) | |
( ( dir = = LC_BACKWARD ) & & ( lsn . lsn ! = lc - > cur_lsn . lsn - 1 ) ) ) {
2013-04-16 23:58:02 -04:00
// int index = lc->cur_logfiles_index;
// fprintf(stderr, "Bad LSN: %d %s direction = %d, lsn.lsn = %"PRIu64", cur_lsn.lsn=%"PRIu64"\n",
// index, lc->logfiles[index], dir, lsn.lsn, lc->cur_lsn.lsn);
2013-04-16 23:58:53 -04:00
if ( tokudb_recovery_trace )
2013-04-16 23:58:04 -04:00
printf ( " DB_RUNRECOVERY: %s:%d r=%d \n " , __FUNCTION__ , __LINE__ , 0 ) ;
2013-04-16 23:57:58 -04:00
return LC_LSN_ERROR ;
}
lc - > cur_lsn . lsn = lsn . lsn ;
return r ;
}
2013-04-16 23:57:55 -04:00
// toku_logcursor_create()
// - returns a pointer to a logcursor
2013-04-16 23:58:01 -04:00
static int lc_create ( TOKULOGCURSOR * lc , const char * log_dir ) {
2013-04-16 23:57:55 -04:00
int failresult = 0 ;
int r = 0 ;
// malloc a cursor
TOKULOGCURSOR cursor = ( TOKULOGCURSOR ) toku_malloc ( sizeof ( struct toku_logcursor ) ) ;
2013-04-16 23:57:58 -04:00
if ( cursor = = NULL ) {
failresult = ENOMEM ;
2013-04-16 23:58:01 -04:00
goto lc_create_fail ;
2013-04-16 23:57:58 -04:00
}
2013-04-16 23:57:55 -04:00
// find logfiles in logdir
2013-04-16 23:57:56 -04:00
cursor - > is_open = FALSE ;
2013-04-16 23:57:55 -04:00
cursor - > cur_logfiles_index = 0 ;
2013-04-16 23:57:56 -04:00
cursor - > entry_valid = FALSE ;
2013-04-16 23:58:56 -04:00
cursor - > buffer_size = 1 < < 20 ; // use a 1MB stream buffer (setvbuf)
cursor - > buffer = toku_malloc ( cursor - > buffer_size ) ; // it does not matter if it failes
2013-04-16 23:57:58 -04:00
// cursor->logdir must be an absolute path
2013-04-16 23:57:58 -04:00
if ( toku_os_is_absolute_name ( log_dir ) ) {
2013-04-16 23:57:58 -04:00
cursor - > logdir = ( char * ) toku_malloc ( strlen ( log_dir ) + 1 ) ;
if ( cursor - > logdir = = NULL ) {
failresult = ENOMEM ;
2013-04-16 23:58:01 -04:00
goto lc_create_fail ;
2013-04-16 23:57:58 -04:00
}
sprintf ( cursor - > logdir , " %s " , log_dir ) ;
2013-04-16 23:57:58 -04:00
} else {
2013-04-16 23:57:58 -04:00
char * cwd = getcwd ( NULL , 0 ) ;
if ( cwd = = NULL ) {
failresult = - 1 ;
2013-04-16 23:58:01 -04:00
goto lc_create_fail ;
2013-04-16 23:57:58 -04:00
}
cursor - > logdir = ( char * ) toku_malloc ( strlen ( cwd ) + strlen ( log_dir ) + 2 ) ;
if ( cursor - > logdir = = NULL ) {
toku_free ( cwd ) ;
failresult = ENOMEM ;
2013-04-16 23:58:01 -04:00
goto lc_create_fail ;
2013-04-16 23:57:58 -04:00
}
sprintf ( cursor - > logdir , " %s/%s " , cwd , log_dir ) ;
toku_free ( cwd ) ;
}
2013-04-16 23:57:58 -04:00
cursor - > logfiles = NULL ;
cursor - > n_logfiles = 0 ;
2013-04-16 23:57:58 -04:00
cursor - > cur_lsn . lsn = 0 ;
cursor - > last_direction = LC_FIRST ;
2013-04-16 23:58:56 -04:00
2013-04-16 23:57:55 -04:00
* lc = cursor ;
return r ;
2013-04-16 23:58:01 -04:00
lc_create_fail :
2013-04-16 23:57:55 -04:00
toku_logcursor_destroy ( & cursor ) ;
* lc = NULL ;
return failresult ;
}
2013-04-16 23:59:02 -04:00
static int lc_fix_bad_logfile ( TOKULOGCURSOR lc ) ;
2013-04-16 23:59:02 -04:00
2013-04-16 23:58:01 -04:00
int toku_logcursor_create ( TOKULOGCURSOR * lc , const char * log_dir ) {
2013-04-16 23:58:59 -04:00
TOKULOGCURSOR cursor ;
int r = lc_create ( & cursor , log_dir ) ;
2013-04-16 23:58:01 -04:00
if ( r ! = 0 )
return r ;
r = toku_logger_find_logfiles ( cursor - > logdir , & ( cursor - > logfiles ) , & ( cursor - > n_logfiles ) ) ;
if ( r ! = 0 ) {
toku_logcursor_destroy ( & cursor ) ;
2013-04-16 23:58:59 -04:00
} else {
* lc = cursor ;
2013-04-16 23:58:01 -04:00
}
return r ;
}
2013-04-16 23:57:58 -04:00
2013-04-16 23:58:01 -04:00
int toku_logcursor_create_for_file ( TOKULOGCURSOR * lc , const char * log_dir , const char * log_file ) {
int failresult = 0 ;
int r = lc_create ( lc , log_dir ) ;
if ( r ! = 0 )
2013-04-16 23:57:58 -04:00
return r ;
2013-04-16 23:58:01 -04:00
TOKULOGCURSOR cursor = * lc ;
2013-04-16 23:57:58 -04:00
int fullnamelen = strlen ( cursor - > logdir ) + strlen ( log_file ) + 3 ;
char * log_file_fullname = toku_malloc ( fullnamelen ) ;
if ( log_file_fullname = = NULL ) {
failresult = ENOMEM ;
goto fail ;
}
sprintf ( log_file_fullname , " %s/%s " , cursor - > logdir , log_file ) ;
2013-04-16 23:58:01 -04:00
cursor - > n_logfiles = 1 ;
char * * logfiles = toku_malloc ( sizeof ( char * * ) ) ;
if ( logfiles = = NULL ) {
failresult = ENOMEM ;
2013-04-16 23:57:58 -04:00
goto fail ;
}
2013-04-16 23:58:01 -04:00
cursor - > logfiles = logfiles ;
2013-04-16 23:57:58 -04:00
cursor - > logfiles [ 0 ] = log_file_fullname ;
* lc = cursor ;
return r ;
fail :
toku_free ( log_file_fullname ) ;
toku_logcursor_destroy ( & cursor ) ;
* lc = NULL ;
return failresult ;
}
2013-04-16 23:57:55 -04:00
int toku_logcursor_destroy ( TOKULOGCURSOR * lc ) {
int r = 0 ;
2013-04-16 23:59:04 -04:00
if ( * lc ) {
if ( ( * lc ) - > entry_valid ) {
toku_log_free_log_entry_resources ( & ( ( * lc ) - > entry ) ) ;
( * lc ) - > entry_valid = FALSE ;
}
r = lc_close_cur_logfile ( * lc ) ;
int lf ;
for ( lf = 0 ; lf < ( * lc ) - > n_logfiles ; lf + + ) {
if ( ( * lc ) - > logfiles [ lf ] ) toku_free ( ( * lc ) - > logfiles [ lf ] ) ;
}
if ( ( * lc ) - > logfiles ) toku_free ( ( * lc ) - > logfiles ) ;
if ( ( * lc ) - > logdir ) toku_free ( ( * lc ) - > logdir ) ;
if ( ( * lc ) - > buffer ) toku_free ( ( * lc ) - > buffer ) ;
toku_free ( * lc ) ;
* lc = NULL ;
2013-04-16 23:57:58 -04:00
}
2013-04-16 23:59:04 -04:00
return r ;
}
static int lc_log_read ( TOKULOGCURSOR lc )
{
int r = toku_log_fread ( lc - > cur_fp , & ( lc - > entry ) ) ;
while ( r = = EOF ) {
// move to next file
r = lc_close_cur_logfile ( lc ) ;
if ( r ! = 0 ) return r ;
if ( lc - > cur_logfiles_index = = lc - > n_logfiles - 1 ) return DB_NOTFOUND ;
lc - > cur_logfiles_index + + ;
r = lc_open_logfile ( lc , lc - > cur_logfiles_index ) ;
if ( r ! = 0 ) return r ;
r = toku_log_fread ( lc - > cur_fp , & ( lc - > entry ) ) ;
}
if ( r ! = 0 ) {
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
time_t tnow = time ( NULL ) ;
if ( r = = DB_BADFORMAT ) {
fprintf ( stderr , " %.24s Tokudb bad log format in %s \n " , ctime ( & tnow ) , lc - > logfiles [ lc - > cur_logfiles_index ] ) ;
}
else {
fprintf ( stderr , " %.24s Tokudb unexpected log format error '%s' in %s \n " , ctime ( & tnow ) , strerror ( r ) , lc - > logfiles [ lc - > cur_logfiles_index ] ) ;
}
}
return r ;
}
static int lc_log_read_backward ( TOKULOGCURSOR lc )
{
int r = toku_log_fread_backward ( lc - > cur_fp , & ( lc - > entry ) ) ;
while ( - 1 = = r ) { // if within header length of top of file
// move to previous file
r = lc_close_cur_logfile ( lc ) ;
if ( r ! = 0 )
return r ;
if ( lc - > cur_logfiles_index = = 0 )
return DB_NOTFOUND ;
lc - > cur_logfiles_index - - ;
r = lc_open_logfile ( lc , lc - > cur_logfiles_index ) ;
if ( r ! = 0 )
return r ;
// seek to end
r = fseek ( lc - > cur_fp , 0 , SEEK_END ) ;
assert ( 0 = = r ) ;
r = toku_log_fread_backward ( lc - > cur_fp , & ( lc - > entry ) ) ;
}
if ( r ! = 0 ) {
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
time_t tnow = time ( NULL ) ;
if ( r = = DB_BADFORMAT ) {
fprintf ( stderr , " %.24s Tokudb bad log format in %s \n " , ctime ( & tnow ) , lc - > logfiles [ lc - > cur_logfiles_index ] ) ;
}
else {
fprintf ( stderr , " %.24s Tokudb uUnexpected log format error '%s' in %s \n " , ctime ( & tnow ) , strerror ( r ) , lc - > logfiles [ lc - > cur_logfiles_index ] ) ;
}
2013-04-16 23:57:56 -04:00
}
2013-04-16 23:57:55 -04:00
return r ;
}
2013-04-16 23:57:56 -04:00
int toku_logcursor_next ( TOKULOGCURSOR lc , struct log_entry * * le ) {
2013-04-16 23:57:55 -04:00
int r = 0 ;
2013-04-16 23:57:58 -04:00
if ( lc - > entry_valid ) {
2013-04-16 23:57:56 -04:00
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = FALSE ;
2013-04-16 23:57:58 -04:00
if ( lc - > last_direction = = LC_BACKWARD ) {
struct log_entry junk ;
r = toku_log_fread ( lc - > cur_fp , & junk ) ;
assert ( r = = 0 ) ;
toku_log_free_log_entry_resources ( & junk ) ;
}
} else {
2013-04-16 23:57:55 -04:00
r = toku_logcursor_first ( lc , le ) ;
return r ;
}
2013-04-16 23:59:04 -04:00
// read the entry
r = lc_log_read ( lc ) ;
if ( r ! = 0 ) return r ;
r = lc_check_lsn ( lc , LC_FORWARD ) ;
if ( r ! = 0 ) return r ;
2013-04-16 23:57:58 -04:00
lc - > last_direction = LC_FORWARD ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = TRUE ;
2013-04-16 23:57:56 -04:00
* le = & ( lc - > entry ) ;
2013-04-16 23:57:55 -04:00
return r ;
}
2013-04-16 23:57:56 -04:00
int toku_logcursor_prev ( TOKULOGCURSOR lc , struct log_entry * * le ) {
2013-04-16 23:57:55 -04:00
int r = 0 ;
2013-04-16 23:57:58 -04:00
if ( lc - > entry_valid ) {
2013-04-16 23:57:56 -04:00
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = FALSE ;
2013-04-16 23:57:58 -04:00
if ( lc - > last_direction = = LC_FORWARD ) {
struct log_entry junk ;
r = toku_log_fread_backward ( lc - > cur_fp , & junk ) ;
assert ( r = = 0 ) ;
toku_log_free_log_entry_resources ( & junk ) ;
}
} else {
2013-04-16 23:57:55 -04:00
r = toku_logcursor_last ( lc , le ) ;
return r ;
}
2013-04-16 23:59:04 -04:00
// read the entry
r = lc_log_read_backward ( lc ) ;
if ( r ! = 0 ) return r ;
2013-04-16 23:57:58 -04:00
r = lc_check_lsn ( lc , LC_BACKWARD ) ;
2013-04-16 23:59:04 -04:00
if ( r ! = 0 ) return r ;
2013-04-16 23:57:58 -04:00
lc - > last_direction = LC_BACKWARD ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = TRUE ;
2013-04-16 23:57:56 -04:00
* le = & ( lc - > entry ) ;
return r ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:57:56 -04:00
int toku_logcursor_first ( TOKULOGCURSOR lc , struct log_entry * * le ) {
2013-04-16 23:57:55 -04:00
int r = 0 ;
2013-04-16 23:57:58 -04:00
if ( lc - > entry_valid ) {
2013-04-16 23:57:56 -04:00
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = FALSE ;
}
2013-04-16 23:57:55 -04:00
// close any but the first log file
if ( lc - > cur_logfiles_index ! = 0 ) {
lc_close_cur_logfile ( lc ) ;
}
// open first log file if needed
if ( ! lc - > is_open ) {
r = lc_open_logfile ( lc , 0 ) ;
if ( r ! = 0 )
return r ;
lc - > cur_logfiles_index = 0 ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:59:04 -04:00
// read the entry
r = lc_log_read ( lc ) ;
if ( r ! = 0 ) return r ;
2013-04-16 23:57:58 -04:00
r = lc_check_lsn ( lc , LC_FIRST ) ;
2013-04-16 23:59:04 -04:00
if ( r ! = 0 ) return r ;
2013-04-16 23:57:58 -04:00
lc - > last_direction = LC_FIRST ;
2013-04-16 23:57:56 -04:00
lc - > entry_valid = TRUE ;
* le = & ( lc - > entry ) ;
2013-04-16 23:57:55 -04:00
return r ;
}
2013-04-16 23:57:56 -04:00
int toku_logcursor_last ( TOKULOGCURSOR lc , struct log_entry * * le ) {
2013-04-16 23:57:55 -04:00
int r = 0 ;
2013-04-16 23:57:58 -04:00
if ( lc - > entry_valid ) {
2013-04-16 23:57:56 -04:00
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
2013-04-16 23:57:58 -04:00
lc - > entry_valid = FALSE ;
}
2013-04-16 23:57:55 -04:00
// close any but last log file
if ( lc - > cur_logfiles_index ! = lc - > n_logfiles - 1 ) {
lc_close_cur_logfile ( lc ) ;
}
// open last log file if needed
if ( ! lc - > is_open ) {
r = lc_open_logfile ( lc , lc - > n_logfiles - 1 ) ;
if ( r ! = 0 )
return r ;
lc - > cur_logfiles_index = lc - > n_logfiles - 1 ;
}
2013-04-16 23:57:59 -04:00
while ( 1 ) {
// seek to end
2013-04-16 23:59:02 -04:00
r = fseek ( lc - > cur_fp , 0 , SEEK_END ) ; assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
// read backward
r = toku_log_fread_backward ( lc - > cur_fp , & ( lc - > entry ) ) ;
2013-04-16 23:59:02 -04:00
if ( r = = 0 ) // got a good entry
2013-04-16 23:57:59 -04:00
break ;
2013-04-16 23:59:02 -04:00
if ( r > 0 ) {
2013-04-16 23:59:04 -04:00
toku_log_free_log_entry_resources ( & ( lc - > entry ) ) ;
2013-04-16 23:59:02 -04:00
// got an error,
// probably a corrupted last log entry due to a crash
// try scanning forward from the beginning to find the last good entry
2013-04-16 23:59:02 -04:00
time_t tnow = time ( NULL ) ;
fprintf ( stderr , " %.24s Tokudb recovery repairing log \n " , ctime ( & tnow ) ) ;
r = lc_fix_bad_logfile ( lc ) ;
if ( r ! = 0 ) {
fprintf ( stderr , " %.24s Tokudb recovery repair unsuccessful \n " , ctime ( & tnow ) ) ;
return DB_BADFORMAT ;
}
2013-04-16 23:59:02 -04:00
// try reading again
r = toku_log_fread_backward ( lc - > cur_fp , & ( lc - > entry ) ) ;
if ( r = = 0 ) // got a good entry
break ;
}
2013-04-16 23:57:59 -04:00
// move to previous file
r = lc_close_cur_logfile ( lc ) ;
if ( r ! = 0 )
return r ;
if ( lc - > cur_logfiles_index = = 0 )
return DB_NOTFOUND ;
lc - > cur_logfiles_index - - ;
r = lc_open_logfile ( lc , lc - > cur_logfiles_index ) ;
if ( r ! = 0 )
return r ;
}
2013-04-16 23:57:58 -04:00
r = lc_check_lsn ( lc , LC_LAST ) ;
if ( r ! = 0 )
return r ;
lc - > last_direction = LC_LAST ;
2013-04-16 23:57:56 -04:00
lc - > entry_valid = TRUE ;
* le = & ( lc - > entry ) ;
2013-04-16 23:57:55 -04:00
return r ;
}
2013-04-16 23:58:52 -04:00
// return 0 if log exists, ENOENT if no log
int
toku_logcursor_log_exists ( const TOKULOGCURSOR lc ) {
int r ;
if ( lc - > n_logfiles )
r = 0 ;
else
r = ENOENT ;
return r ;
}
2013-04-16 23:59:02 -04:00
2013-04-16 23:59:02 -04:00
// fix a logfile with a bad last entry
// - return with fp pointing to end-of-file so that toku_logcursor_last can be retried
static int lc_fix_bad_logfile ( TOKULOGCURSOR lc ) {
2013-04-16 23:59:02 -04:00
struct log_entry le ;
unsigned int version = 0 ;
int r = 0 ;
2013-04-16 23:59:02 -04:00
r = fseek ( lc - > cur_fp , 0 , SEEK_SET ) ; if ( r ! = 0 ) return r ;
r = toku_read_logmagic ( lc - > cur_fp , & version ) ; if ( r ! = 0 ) return r ;
2013-04-16 23:59:02 -04:00
2013-04-16 23:59:02 -04:00
toku_off_t last_good_pos ;
2013-04-16 23:59:02 -04:00
last_good_pos = ftello ( lc - > cur_fp ) ;
2013-04-16 23:59:02 -04:00
while ( 1 ) {
2013-04-16 23:59:02 -04:00
// initialize le
// - reading incomplete entries can result in fields that cannot be freed
memset ( & le , 0 , sizeof ( le ) ) ;
2013-04-16 23:59:02 -04:00
r = toku_log_fread ( lc - > cur_fp , & le ) ;
2013-04-16 23:59:02 -04:00
toku_log_free_log_entry_resources ( & le ) ;
2013-04-16 23:59:02 -04:00
if ( r ! = 0 ) break ;
2013-04-16 23:59:02 -04:00
last_good_pos = ftello ( lc - > cur_fp ) ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:59:02 -04:00
// now have position of last good entry
// 1) close the file
// 2) truncate the file to remove the error
// 3) reopen the file
// 4) set the pos to last
r = lc_close_cur_logfile ( lc ) ; if ( r ! = 0 ) return r ;
r = truncate ( lc - > logfiles [ lc - > n_logfiles - 1 ] , last_good_pos ) ; if ( r ! = 0 ) return r ;
r = lc_open_logfile ( lc , lc - > n_logfiles - 1 ) ; if ( r ! = 0 ) return r ;
r = fseek ( lc - > cur_fp , 0 , SEEK_END ) ; if ( r ! = 0 ) return r ;
2013-04-16 23:59:02 -04:00
return 0 ;
}