2013-04-17 00:00:59 -04:00
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
2013-04-16 23:59:03 -04:00
# ident "$Id$"
2013-04-17 00:00:59 -04:00
# ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved."
2013-04-16 23:57:55 -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."
2013-04-17 00:01:15 -04:00
# include <memory.h>
# include <ctype.h>
# include "ft.h"
# include "log-internal.h"
2013-04-17 00:00:37 -04:00
# include "txn_manager.h"
2013-04-17 00:01:13 -04:00
# include "rollback_log_node_cache.h"
2013-04-16 23:57:55 -04:00
2013-04-16 23:57:58 -04:00
static const int log_format_version = TOKU_LOG_VERSION ;
2013-04-16 23:57:55 -04:00
static int open_logfile ( TOKULOGGER logger ) ;
2013-04-17 00:01:08 -04:00
static void logger_write_buffer ( TOKULOGGER logger , LSN * fsynced_lsn ) ;
2013-04-17 00:01:15 -04:00
static void delete_logfile ( TOKULOGGER logger , long long index , uint32_t version ) ;
2013-04-16 23:59:02 -04:00
static void grab_output ( TOKULOGGER logger , LSN * fsynced_lsn ) ;
static void release_output ( TOKULOGGER logger , LSN fsynced_lsn ) ;
2013-04-16 23:58:03 -04:00
2013-04-17 00:01:01 -04:00
static void toku_print_bytes ( FILE * outf , uint32_t len , char * data ) {
2013-04-17 00:00:17 -04:00
fprintf ( outf , " \" " ) ;
2013-04-17 00:01:01 -04:00
uint32_t i ;
2013-04-17 00:00:17 -04:00
for ( i = 0 ; i < len ; i + + ) {
2013-04-17 00:00:43 -04:00
switch ( data [ i ] ) {
case ' " ' : fprintf ( outf , " \\ \" " ) ; break ;
case ' \\ ' : fprintf ( outf , " \\ \\ " ) ; break ;
case ' \n ' : fprintf ( outf , " \\ n " ) ; break ;
default :
if ( isprint ( data [ i ] ) ) fprintf ( outf , " %c " , data [ i ] ) ;
else fprintf ( outf , " \\ %03o " , ( unsigned char ) ( data [ i ] ) ) ;
}
2013-04-17 00:00:17 -04:00
}
fprintf ( outf , " \" " ) ;
}
2013-04-17 00:01:01 -04:00
static bool is_a_logfile_any_version ( const char * name , uint64_t * number_result , uint32_t * version_of_log ) {
bool rval = true ;
2013-04-16 23:59:17 -04:00
uint64_t result ;
int n ;
int r ;
uint32_t version ;
2013-04-17 00:00:58 -04:00
r = sscanf ( name , " log% " SCNu64 " .tokulog% " SCNu32 " %n " , & result , & version , & n ) ;
2013-04-16 23:59:17 -04:00
if ( r ! = 2 | | name [ n ] ! = ' \0 ' | | version < = TOKU_LOG_VERSION_1 ) {
//Version 1 does NOT append 'version' to end of '.tokulog'
version = TOKU_LOG_VERSION_1 ;
2013-04-17 00:00:58 -04:00
r = sscanf ( name , " log% " SCNu64 " .tokulog%n " , & result , & n ) ;
2013-04-16 23:59:17 -04:00
if ( r ! = 1 | | name [ n ] ! = ' \0 ' ) {
2013-04-17 00:01:01 -04:00
rval = false ;
2013-04-16 23:59:17 -04:00
}
}
if ( rval ) {
* number_result = result ;
* version_of_log = version ;
}
return rval ;
}
2013-04-16 23:59:08 -04:00
// added for #2424, improved for #2521
2013-04-17 00:01:01 -04:00
static bool is_a_logfile ( const char * name , long long * number_result ) {
bool rval ;
2013-04-16 23:59:17 -04:00
uint64_t result ;
uint32_t version ;
rval = is_a_logfile_any_version ( name , & result , & version ) ;
if ( rval & & version ! = TOKU_LOG_VERSION )
2013-04-17 00:01:01 -04:00
rval = false ;
2013-04-16 23:59:17 -04:00
if ( rval )
* number_result = result ;
return rval ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:59:08 -04:00
2013-04-17 00:01:08 -04:00
// TODO: can't fail
2013-04-16 23:57:55 -04:00
int toku_logger_create ( TOKULOGGER * resultp ) {
2013-04-17 00:01:07 -04:00
TOKULOGGER CALLOC ( result ) ;
2013-04-17 00:00:58 -04:00
if ( result = = 0 ) return get_error_errno ( ) ;
2013-04-17 00:01:01 -04:00
result - > is_open = false ;
result - > write_log_files = true ;
result - > trim_log_files = true ;
2013-04-16 23:57:55 -04:00
result - > directory = 0 ;
2013-04-16 23:58:55 -04:00
result - > remove_finalize_callback = NULL ;
2013-04-16 23:58:04 -04:00
// fd is uninitialized on purpose
// ct is uninitialized on purpose
result - > lg_max = 100 < < 20 ; // 100MB default
// lsn is uninitialized
2013-04-17 00:00:58 -04:00
result - > inbuf = ( struct logbuf ) { 0 , LOGGER_MIN_BUF_SIZE , ( char * ) toku_xmalloc ( LOGGER_MIN_BUF_SIZE ) , ZERO_LSN } ;
result - > outbuf = ( struct logbuf ) { 0 , LOGGER_MIN_BUF_SIZE , ( char * ) toku_xmalloc ( LOGGER_MIN_BUF_SIZE ) , ZERO_LSN } ;
2013-04-16 23:58:04 -04:00
// written_lsn is uninitialized
// fsynced_lsn is uninitialized
2013-04-16 23:59:05 -04:00
result - > last_completed_checkpoint_lsn = ZERO_LSN ;
2013-04-16 23:58:04 -04:00
// next_log_file_number is uninitialized
// n_in_file is uninitialized
2013-04-17 00:00:35 -04:00
result - > write_block_size = FT_DEFAULT_NODE_SIZE ; // default logging size is the same as the default brt block size
2013-04-16 23:58:01 -04:00
toku_logfilemgr_create ( & result - > logfilemgr ) ;
2013-04-16 23:57:55 -04:00
* resultp = result ;
2013-04-17 00:00:31 -04:00
ml_init ( & result - > input_lock ) ;
toku_mutex_init ( & result - > output_condition_lock , NULL ) ;
toku_cond_init ( & result - > output_condition , NULL ) ;
2013-04-16 23:59:03 -04:00
result - > input_lock_ctr = 0 ;
result - > output_condition_lock_ctr = 0 ;
result - > swap_ctr = 0 ;
2013-04-16 23:59:05 -04:00
result - > rollback_cachefile = NULL ;
2013-04-17 00:01:01 -04:00
result - > output_is_available = true ;
2013-04-17 00:00:37 -04:00
toku_txn_manager_init ( & result - > txn_manager ) ;
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-17 00:01:08 -04:00
static void fsync_logdir ( TOKULOGGER logger ) {
toku_fsync_dirfd_without_accounting ( logger - > dir ) ;
2013-04-16 23:59:02 -04:00
}
static int open_logdir ( TOKULOGGER logger , const char * directory ) {
if ( toku_os_is_absolute_name ( directory ) ) {
logger - > directory = toku_strdup ( directory ) ;
} else {
char * cwd = getcwd ( NULL , 0 ) ;
if ( cwd = = NULL )
return - 1 ;
2013-04-17 00:00:58 -04:00
char * MALLOC_N ( strlen ( cwd ) + strlen ( directory ) + 2 , new_log_dir ) ;
2013-04-16 23:59:02 -04:00
if ( new_log_dir = = NULL ) {
toku_free ( cwd ) ;
2013-04-16 23:59:02 -04:00
return - 2 ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:59:02 -04:00
sprintf ( new_log_dir , " %s/%s " , cwd , directory ) ;
2013-04-16 23:59:02 -04:00
toku_free ( cwd ) ;
2013-04-16 23:59:02 -04:00
logger - > directory = new_log_dir ;
}
2013-04-17 00:00:58 -04:00
if ( logger - > directory = = 0 ) return get_error_errno ( ) ;
2013-04-16 23:59:02 -04:00
logger - > dir = opendir ( logger - > directory ) ;
if ( logger - > dir = = NULL ) return - 1 ;
return 0 ;
}
static int close_logdir ( TOKULOGGER logger ) {
return closedir ( logger - > dir ) ;
}
2013-04-17 00:00:57 -04:00
int
toku_logger_open_with_last_xid ( const char * directory , TOKULOGGER logger , TXNID last_xid ) {
2013-04-16 23:57:55 -04:00
if ( logger - > is_open ) return EINVAL ;
2013-04-16 23:57:58 -04:00
2013-04-16 23:57:55 -04:00
int r ;
2013-04-17 00:00:57 -04:00
TXNID last_xid_if_clean_shutdown ;
r = toku_logfilemgr_init ( logger - > logfilemgr , directory , & last_xid_if_clean_shutdown ) ;
if ( r ! = 0 )
2013-04-16 23:58:01 -04:00
return r ;
logger - > lsn = toku_logfilemgr_get_last_lsn ( logger - > logfilemgr ) ;
2013-04-16 23:57:58 -04:00
logger - > written_lsn = logger - > lsn ;
logger - > fsynced_lsn = logger - > lsn ;
2013-04-16 23:58:04 -04:00
logger - > inbuf . max_lsn_in_buf = logger - > lsn ;
logger - > outbuf . max_lsn_in_buf = logger - > lsn ;
2013-04-16 23:57:58 -04:00
2013-04-16 23:59:02 -04:00
// open directory, save pointer for fsyncing t:2445
r = open_logdir ( logger , directory ) ;
if ( r ! = 0 ) return r ;
2013-04-16 23:57:55 -04:00
long long nexti ;
2013-04-16 23:59:02 -04:00
r = toku_logger_find_next_unused_log_file ( logger - > directory , & nexti ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
2013-04-16 23:59:02 -04:00
2013-04-16 23:57:55 -04:00
logger - > next_log_file_number = nexti ;
open_logfile ( logger ) ;
2013-04-17 00:00:57 -04:00
if ( last_xid = = TXNID_NONE ) {
last_xid = last_xid_if_clean_shutdown ;
}
toku_txn_manager_set_last_xid_from_logger ( logger - > txn_manager , last_xid ) ;
2013-04-16 23:57:55 -04:00
2013-04-17 00:01:01 -04:00
logger - > is_open = true ;
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-17 00:00:57 -04:00
int toku_logger_open ( const char * directory , TOKULOGGER logger ) {
return toku_logger_open_with_last_xid ( directory , logger , TXNID_NONE ) ;
}
2013-04-17 00:00:14 -04:00
bool toku_logger_rollback_is_open ( TOKULOGGER logger ) {
return logger - > rollback_cachefile ! = NULL ;
}
2013-04-17 00:01:13 -04:00
# define MAX_CACHED_ROLLBACK_NODES 4096
void
toku_logger_initialize_rollback_cache ( TOKULOGGER logger , FT ft ) {
toku_free_unused_blocknums ( ft - > blocktable , ft - > h - > root_blocknum ) ;
logger - > rollback_cache . init ( MAX_CACHED_ROLLBACK_NODES ) ;
}
2013-04-16 23:59:05 -04:00
int
2013-04-17 00:01:01 -04:00
toku_logger_open_rollback ( TOKULOGGER logger , CACHETABLE cachetable , bool create ) {
2013-04-16 23:59:05 -04:00
assert ( logger - > is_open ) ;
assert ( ! logger - > rollback_cachefile ) ;
2013-04-17 00:01:13 -04:00
2013-04-17 00:00:35 -04:00
FT_HANDLE t = NULL ; // Note, there is no DB associated with this BRT.
2013-04-17 00:01:08 -04:00
toku_ft_handle_create ( & t ) ;
int r = toku_ft_handle_open ( t , ROLLBACK_CACHEFILE_NAME , create , create , cachetable , NULL_TXN ) ;
2013-04-16 23:59:44 -04:00
assert_zero ( r ) ;
2013-04-17 00:00:36 -04:00
logger - > rollback_cachefile = t - > ft - > cf ;
2013-04-17 00:01:13 -04:00
toku_logger_initialize_rollback_cache ( logger , t - > ft ) ;
2013-04-16 23:59:05 -04:00
//Verify it is empty
//Must have no data blocks (rollback logs or otherwise).
2013-04-17 00:01:13 -04:00
toku_block_verify_no_data_blocks_except_root ( t - > ft - > blocktable , t - > ft - > h - > root_blocknum ) ;
2013-04-17 00:01:01 -04:00
bool is_empty ;
2013-04-17 00:00:35 -04:00
is_empty = toku_ft_is_empty_fast ( t ) ;
2013-04-16 23:59:22 -04:00
assert ( is_empty ) ;
2013-04-16 23:59:05 -04:00
return r ;
}
// Requires: Rollback cachefile can only be closed immediately after a checkpoint,
// so it will always be clean (!h->dirty) when about to be closed.
// Rollback log can only be closed when there are no open transactions,
// so it will always be empty (no data blocks) when about to be closed.
2013-04-17 00:01:09 -04:00
void toku_logger_close_rollback ( TOKULOGGER logger ) {
2013-04-16 23:59:05 -04:00
CACHEFILE cf = logger - > rollback_cachefile ; // stored in logger at rollback cachefile open
2013-04-17 00:01:08 -04:00
if ( cf ) {
2013-04-17 00:00:35 -04:00
FT_HANDLE ft_to_close ;
2013-04-16 23:59:05 -04:00
{ //Find "brt"
2013-04-17 00:01:13 -04:00
logger - > rollback_cache . destroy ( ) ;
2013-04-17 00:00:59 -04:00
FT CAST_FROM_VOIDP ( ft , toku_cachefile_get_userdata ( cf ) ) ;
2013-04-16 23:59:05 -04:00
//Verify it is safe to close it.
2013-04-17 00:01:08 -04:00
assert ( ! ft - > h - > dirty ) ; //Must not be dirty.
2013-04-17 00:01:13 -04:00
toku_free_unused_blocknums ( ft - > blocktable , ft - > h - > root_blocknum ) ;
2013-04-17 00:01:08 -04:00
//Must have no data blocks (rollback logs or otherwise).
2013-04-17 00:01:13 -04:00
toku_block_verify_no_data_blocks_except_root ( ft - > blocktable , ft - > h - > root_blocknum ) ;
2013-04-17 00:00:37 -04:00
assert ( ! ft - > h - > dirty ) ;
2013-04-17 00:00:43 -04:00
ft_to_close = toku_ft_get_only_existing_ft_handle ( ft ) ;
2013-04-17 00:00:28 -04:00
{
2013-04-17 00:01:01 -04:00
bool is_empty ;
2013-04-17 00:00:35 -04:00
is_empty = toku_ft_is_empty_fast ( ft_to_close ) ;
2013-04-17 00:00:28 -04:00
assert ( is_empty ) ;
}
2013-04-17 00:00:37 -04:00
assert ( ! ft - > h - > dirty ) ; // it should not have been dirtied by the toku_ft_is_empty test.
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:00:51 -04:00
toku_ft_handle_close ( ft_to_close ) ;
2013-04-16 23:59:05 -04:00
//Set as dealt with already.
logger - > rollback_cachefile = NULL ;
}
}
2013-04-16 23:57:55 -04:00
// No locks held on entry
// No locks held on exit.
2013-04-16 23:59:02 -04:00
// No locks are needed, since you cannot legally close the log concurrently with doing anything else.
2013-04-17 00:01:08 -04:00
// TODO: can't fail
2013-04-16 23:57:55 -04:00
int toku_logger_close ( TOKULOGGER * loggerp ) {
2013-04-17 00:01:08 -04:00
int r ;
2013-04-16 23:57:55 -04:00
TOKULOGGER logger = * loggerp ;
2013-04-17 00:01:08 -04:00
if ( ! logger - > is_open ) {
goto is_closed ;
}
2013-04-16 23:59:02 -04:00
ml_lock ( & logger - > input_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > input_lock_ctr + + ;
2013-04-16 23:59:02 -04:00
LSN fsynced_lsn ;
grab_output ( logger , & fsynced_lsn ) ;
2013-04-17 00:01:08 -04:00
logger_write_buffer ( logger , & fsynced_lsn ) ;
2013-04-16 23:57:55 -04:00
if ( logger - > fd ! = - 1 ) {
2013-04-16 23:59:02 -04:00
if ( logger - > write_log_files ) {
2013-04-17 00:01:08 -04:00
toku_file_fsync_without_accounting ( logger - > fd ) ;
2013-04-16 23:59:02 -04:00
}
2013-04-17 00:01:08 -04:00
r = close ( logger - > fd ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:08 -04:00
r = close_logdir ( logger ) ;
assert ( r = = 0 ) ;
2013-04-16 23:57:55 -04:00
logger - > fd = - 1 ;
2013-04-16 23:59:02 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-16 23:57:55 -04:00
2013-04-17 00:01:08 -04:00
is_closed :
2013-04-16 23:58:04 -04:00
toku_free ( logger - > inbuf . buf ) ;
toku_free ( logger - > outbuf . buf ) ;
2013-04-16 23:59:02 -04:00
// before destroying locks they must be left in the unlocked state.
2013-04-17 00:00:31 -04:00
ml_destroy ( & logger - > input_lock ) ;
toku_mutex_destroy ( & logger - > output_condition_lock ) ;
toku_cond_destroy ( & logger - > output_condition ) ;
2013-04-17 00:00:37 -04:00
toku_txn_manager_destroy ( logger - > txn_manager ) ;
2013-04-16 23:57:55 -04:00
if ( logger - > directory ) toku_free ( logger - > directory ) ;
2013-04-16 23:58:01 -04:00
toku_logfilemgr_destroy ( & logger - > logfilemgr ) ;
2013-04-16 23:57:55 -04:00
toku_free ( logger ) ;
* loggerp = 0 ;
2013-04-17 00:01:08 -04:00
return 0 ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:08 -04:00
void toku_logger_shutdown ( TOKULOGGER logger ) {
2013-04-16 23:57:59 -04:00
if ( logger - > is_open ) {
2013-04-17 00:00:49 -04:00
TXN_MANAGER mgr = logger - > txn_manager ;
if ( toku_txn_manager_num_live_txns ( mgr ) = = 0 ) {
TXNID last_xid = toku_txn_manager_get_last_xid ( mgr ) ;
2013-04-17 00:01:08 -04:00
toku_log_shutdown ( logger , NULL , true , 0 , last_xid ) ;
2013-04-16 23:58:53 -04:00
}
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:57:55 -04:00
2013-04-16 23:59:02 -04:00
static int close_and_open_logfile ( TOKULOGGER logger , LSN * fsynced_lsn )
// Effect: close the current file, and open the next one.
// Entry: This thread has permission to modify the output.
// Exit: This thread has permission to modify the output.
{
2013-04-16 23:58:04 -04:00
int r ;
if ( logger - > write_log_files ) {
2013-04-17 00:01:08 -04:00
toku_file_fsync_without_accounting ( logger - > fd ) ;
2013-04-17 00:00:43 -04:00
* fsynced_lsn = logger - > written_lsn ;
2013-04-16 23:58:58 -04:00
toku_logfilemgr_update_last_lsn ( logger - > logfilemgr , logger - > written_lsn ) ; // fixes t:2294
2013-04-16 23:58:04 -04:00
}
2013-04-17 00:00:58 -04:00
r = close ( logger - > fd ) ; if ( r ! = 0 ) return get_error_errno ( ) ;
2013-04-16 23:58:04 -04:00
return open_logfile ( logger ) ;
}
static int
2013-04-16 23:58:04 -04:00
max_int ( int a , int b )
2013-04-16 23:58:04 -04:00
{
if ( a > b ) return a ;
return b ;
}
2013-04-16 23:59:02 -04:00
// ***********************************************************
// output mutex/condition manipulation routines
// ***********************************************************
static void
wait_till_output_available ( TOKULOGGER logger )
// Effect: Wait until output becomes available.
// Implementation hint: Use a pthread_cond_wait.
// Entry: Holds the output_condition_lock (but not the inlock)
// Exit: Holds the output_condition_lock and logger->output_is_available
//
{
while ( ! logger - > output_is_available ) {
2013-04-17 00:00:31 -04:00
toku_cond_wait ( & logger - > output_condition , & logger - > output_condition_lock ) ;
2013-04-16 23:59:02 -04:00
}
}
static void
grab_output ( TOKULOGGER logger , LSN * fsynced_lsn )
// Effect: Wait until output becomes available and get permission to modify output.
// Entry: Holds no lock (including not holding the input lock, since we never hold both at once).
// Exit: Hold permission to modify output (but none of the locks).
{
2013-04-17 00:00:31 -04:00
toku_mutex_lock ( & logger - > output_condition_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > output_condition_lock_ctr + + ;
2013-04-16 23:59:02 -04:00
wait_till_output_available ( logger ) ;
2013-04-17 00:01:01 -04:00
logger - > output_is_available = false ;
2013-04-16 23:59:02 -04:00
if ( fsynced_lsn ) {
2013-04-17 00:00:43 -04:00
* fsynced_lsn = logger - > fsynced_lsn ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:59:03 -04:00
logger - > output_condition_lock_ctr + + ;
2013-04-17 00:00:31 -04:00
toku_mutex_unlock ( & logger - > output_condition_lock ) ;
2013-04-16 23:59:02 -04:00
}
2013-04-17 00:01:01 -04:00
static bool
2013-04-16 23:59:02 -04:00
wait_till_output_already_written_or_output_buffer_available ( TOKULOGGER logger , LSN lsn , LSN * fsynced_lsn )
// Effect: Wait until either the output is available or the lsn has been written.
// Return true iff the lsn has been written.
// If returning true, then on exit we don't hold output permission.
// If returning false, then on exit we do hold output permission.
// Entry: Hold no locks.
// Exit: Hold the output permission if returns false.
{
2013-04-17 00:01:01 -04:00
bool result ;
2013-04-17 00:00:31 -04:00
toku_mutex_lock ( & logger - > output_condition_lock ) ;
logger - > output_condition_lock_ctr + + ;
2013-04-16 23:59:02 -04:00
while ( 1 ) {
2013-04-17 00:00:43 -04:00
if ( logger - > fsynced_lsn . lsn > = lsn . lsn ) { // we can look at the fsynced lsn since we have the lock.
2013-04-17 00:01:01 -04:00
result = true ;
2013-04-17 00:00:43 -04:00
break ;
}
if ( logger - > output_is_available ) {
2013-04-17 00:01:01 -04:00
logger - > output_is_available = false ;
result = false ;
2013-04-17 00:00:43 -04:00
break ;
}
// otherwise wait for a good time to look again.
toku_cond_wait ( & logger - > output_condition , & logger - > output_condition_lock ) ;
2013-04-16 23:59:02 -04:00
}
* fsynced_lsn = logger - > fsynced_lsn ;
2013-04-17 00:00:31 -04:00
logger - > output_condition_lock_ctr + + ;
toku_mutex_unlock ( & logger - > output_condition_lock ) ;
2013-04-16 23:59:02 -04:00
return result ;
}
static void
release_output ( TOKULOGGER logger , LSN fsynced_lsn )
// Effect: Release output permission.
// Entry: Holds output permissions, but no locks.
// Exit: Holds neither locks nor output permission.
{
2013-04-17 00:00:31 -04:00
toku_mutex_lock ( & logger - > output_condition_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > output_condition_lock_ctr + + ;
2013-04-17 00:01:01 -04:00
logger - > output_is_available = true ;
2013-04-16 23:59:02 -04:00
if ( logger - > fsynced_lsn . lsn < fsynced_lsn . lsn ) {
2013-04-17 00:00:43 -04:00
logger - > fsynced_lsn = fsynced_lsn ;
2013-04-16 23:59:02 -04:00
}
2013-04-17 00:00:31 -04:00
toku_cond_broadcast ( & logger - > output_condition ) ;
2013-04-16 23:59:03 -04:00
logger - > output_condition_lock_ctr + + ;
2013-04-17 00:00:31 -04:00
toku_mutex_unlock ( & logger - > output_condition_lock ) ;
2013-04-16 23:59:02 -04:00
}
static void
swap_inbuf_outbuf ( TOKULOGGER logger )
// Effect: Swap the inbuf and outbuf
// Entry and exit: Hold the input lock and permission to modify output.
{
struct logbuf tmp = logger - > inbuf ;
logger - > inbuf = logger - > outbuf ;
logger - > outbuf = tmp ;
assert ( logger - > inbuf . n_in_buf = = 0 ) ;
2013-04-16 23:59:03 -04:00
logger - > swap_ctr + + ;
2013-04-16 23:59:02 -04:00
}
static void
write_outbuf_to_logfile ( TOKULOGGER logger , LSN * fsynced_lsn )
// Effect: Write the contents of outbuf to logfile. Don't necessarily fsync (but it might, in which case fynced_lsn is updated).
// If the logfile gets too big, open the next one (that's the case where an fsync might happen).
// Entry and exit: Holds permission to modify output (and doesn't let it go, so it's ok to also hold the inlock).
{
if ( logger - > outbuf . n_in_buf > 0 ) {
2013-04-17 00:00:43 -04:00
toku_os_full_write ( logger - > fd , logger - > outbuf . buf , logger - > outbuf . n_in_buf ) ;
assert ( logger - > outbuf . max_lsn_in_buf . lsn > logger - > written_lsn . lsn ) ; // since there is something in the buffer, its LSN must be bigger than what's previously written.
logger - > written_lsn = logger - > outbuf . max_lsn_in_buf ;
logger - > n_in_file + = logger - > outbuf . n_in_buf ;
logger - > outbuf . n_in_buf = 0 ;
2013-04-16 23:59:02 -04:00
}
// If the file got too big, then open a new file.
if ( logger - > n_in_file > logger - > lg_max ) {
2013-04-17 00:00:43 -04:00
int r = close_and_open_logfile ( logger , fsynced_lsn ) ;
assert_zero ( r ) ;
2013-04-16 23:59:02 -04:00
}
}
2013-04-17 00:01:08 -04:00
void
2013-04-16 23:58:04 -04:00
toku_logger_make_space_in_inbuf ( TOKULOGGER logger , int n_bytes_needed )
// Entry: Holds the inlock
// Exit: Holds the inlock
// Effect: Upon exit, the inlock is held and there are at least n_bytes_needed in the buffer.
2013-04-16 23:59:02 -04:00
// May release the inlock (and then reacquire it), so this is not atomic.
// May obtain the output lock and output permission (but if it does so, it will have released the inlock, since we don't hold both locks at once).
// (But may hold output permission and inlock at the same time.)
// Implementation hint: Makes space in the inbuf, possibly by writing the inbuf to disk or increasing the size of the inbuf. There might not be an fsync.
2013-04-16 23:58:04 -04:00
// Arguments: logger: the logger (side effects)
// n_bytes_needed: how many bytes to make space for.
{
2013-04-17 00:01:08 -04:00
if ( logger - > inbuf . n_in_buf + n_bytes_needed < = LOGGER_MIN_BUF_SIZE ) {
return ;
}
2013-04-16 23:59:03 -04:00
logger - > input_lock_ctr + + ;
2013-04-17 00:00:31 -04:00
ml_unlock ( & logger - > input_lock ) ;
2013-04-16 23:59:02 -04:00
LSN fsynced_lsn ;
grab_output ( logger , & fsynced_lsn ) ;
2013-04-17 00:00:31 -04:00
ml_lock ( & logger - > input_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > input_lock_ctr + + ;
2013-04-16 23:59:02 -04:00
// Some other thread may have written the log out while we didn't have the lock. If we have space now, then be happy.
if ( logger - > inbuf . n_in_buf + n_bytes_needed < = LOGGER_MIN_BUF_SIZE ) {
2013-04-17 00:00:43 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-17 00:01:08 -04:00
return ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:58:04 -04:00
if ( logger - > inbuf . n_in_buf > 0 ) {
2013-04-17 00:00:43 -04:00
// There isn't enough space, and there is something in the buffer, so write the inbuf.
swap_inbuf_outbuf ( logger ) ;
2013-04-16 23:59:02 -04:00
2013-04-17 00:00:43 -04:00
// Don't release the inlock in this case, because we don't want to get starved.
write_outbuf_to_logfile ( logger , & fsynced_lsn ) ;
2013-04-16 23:58:04 -04:00
}
2013-04-16 23:59:02 -04:00
// the inbuf is empty. Make it big enough (just in case it is somehow smaller than a single log entry).
2013-04-16 23:58:04 -04:00
if ( n_bytes_needed > logger - > inbuf . buf_size ) {
2013-04-17 00:00:43 -04:00
assert ( n_bytes_needed < ( 1 < < 30 ) ) ; // it seems unlikely to work if a logentry gets that big.
int new_size = max_int ( logger - > inbuf . buf_size * 2 , n_bytes_needed ) ; // make it at least twice as big, and big enough for n_bytes
assert ( new_size < ( 1 < < 30 ) ) ;
XREALLOC_N ( new_size , logger - > inbuf . buf ) ;
logger - > inbuf . buf_size = new_size ;
2013-04-16 23:58:04 -04:00
}
2013-04-16 23:59:02 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:09 -04:00
void toku_logger_fsync ( TOKULOGGER logger )
2013-04-16 23:59:02 -04:00
// Effect: This is the exported fsync used by ydb.c for env_log_flush. Group commit doesn't have to work.
2013-04-16 23:57:55 -04:00
// Entry: Holds no locks
// Exit: Holds no locks
2013-04-16 23:59:02 -04:00
// Implementation note: Acquire the output condition lock, then the output permission, then release the output condition lock, then get the input lock.
// Then release everything.
{
2013-04-17 00:00:31 -04:00
ml_lock ( & logger - > input_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > input_lock_ctr + + ;
2013-04-17 00:01:08 -04:00
toku_logger_maybe_fsync ( logger , logger - > inbuf . max_lsn_in_buf , true ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:09 -04:00
void toku_logger_fsync_if_lsn_not_fsynced ( TOKULOGGER logger , LSN lsn ) {
2013-04-17 00:01:08 -04:00
if ( logger - > write_log_files ) {
2013-04-17 00:00:31 -04:00
ml_lock ( & logger - > input_lock ) ;
2013-04-16 23:59:18 -04:00
logger - > input_lock_ctr + + ;
2013-04-17 00:01:08 -04:00
toku_logger_maybe_fsync ( logger , lsn , true ) ;
2013-04-16 23:59:06 -04:00
}
}
2013-04-16 23:57:55 -04:00
int toku_logger_is_open ( TOKULOGGER logger ) {
if ( logger = = 0 ) return 0 ;
return logger - > is_open ;
}
2013-04-16 23:57:59 -04:00
void toku_logger_set_cachetable ( TOKULOGGER logger , CACHETABLE ct ) {
logger - > ct = ct ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_logger_set_lg_max ( TOKULOGGER logger , uint32_t lg_max ) {
2013-04-16 23:57:55 -04:00
if ( logger = = 0 ) return EINVAL ; // no logger
if ( logger - > is_open ) return EINVAL ;
if ( lg_max > ( 1 < < 30 ) ) return EINVAL ; // too big
logger - > lg_max = lg_max ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logger_get_lg_max ( TOKULOGGER logger , uint32_t * lg_maxp ) {
2013-04-16 23:57:55 -04:00
if ( logger = = 0 ) return EINVAL ; // no logger
* lg_maxp = logger - > lg_max ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logger_set_lg_bsize ( TOKULOGGER logger , uint32_t bsize ) {
2013-04-16 23:57:55 -04:00
if ( logger = = 0 ) return EINVAL ; // no logger
if ( logger - > is_open ) return EINVAL ;
if ( bsize < = 0 | | bsize > ( 1 < < 30 ) ) return EINVAL ;
logger - > write_block_size = bsize ;
return 0 ;
}
2013-04-16 23:59:02 -04:00
int toku_logger_find_next_unused_log_file ( const char * directory , long long * result )
// This is called during logger initialalization, and no locks are required.
{
2013-04-16 23:57:55 -04:00
DIR * d = opendir ( directory ) ;
2013-04-16 23:58:04 -04:00
long long maxf = - 1 ; * result = maxf ;
2013-04-16 23:57:55 -04:00
struct dirent * de ;
2013-04-17 00:00:58 -04:00
if ( d = = 0 ) return get_error_errno ( ) ;
2013-04-16 23:57:55 -04:00
while ( ( de = readdir ( d ) ) ) {
2013-04-17 00:00:58 -04:00
if ( de = = 0 ) return get_error_errno ( ) ;
2013-04-16 23:59:08 -04:00
long long thisl ;
if ( is_a_logfile ( de - > d_name , & thisl ) ) {
if ( ( long long ) thisl > maxf ) maxf = thisl ;
2013-04-16 23:59:02 -04:00
}
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:58:04 -04:00
* result = maxf + 1 ;
2013-04-16 23:57:55 -04:00
int r = closedir ( d ) ;
return r ;
}
2013-04-16 23:59:25 -04:00
// TODO: Put this in portability layer when ready
// in: file pathname that may have a dirname prefix
// return: file leaf name
static char * fileleafname ( char * pathname ) {
const char delimiter = ' / ' ;
char * leafname = strrchr ( pathname , delimiter ) ;
if ( leafname )
2013-04-17 00:00:43 -04:00
leafname + + ;
2013-04-16 23:59:25 -04:00
else
2013-04-17 00:00:43 -04:00
leafname = pathname ;
2013-04-16 23:59:25 -04:00
return leafname ;
}
2013-04-16 23:57:55 -04:00
static int logfilenamecompare ( const void * ap , const void * bp ) {
char * a = * ( char * * ) ap ;
2013-04-16 23:59:25 -04:00
char * a_leafname = fileleafname ( a ) ;
2013-04-16 23:57:55 -04:00
char * b = * ( char * * ) bp ;
2013-04-16 23:59:25 -04:00
char * b_leafname = fileleafname ( b ) ;
int rval ;
2013-04-17 00:01:01 -04:00
bool valid ;
2013-04-16 23:59:25 -04:00
uint64_t num_a = 0 ; // placate compiler
uint64_t num_b = 0 ;
uint32_t ver_a = 0 ;
uint32_t ver_b = 0 ;
valid = is_a_logfile_any_version ( a_leafname , & num_a , & ver_a ) ;
invariant ( valid ) ;
valid = is_a_logfile_any_version ( b_leafname , & num_b , & ver_b ) ;
invariant ( valid ) ;
if ( ver_a < ver_b ) rval = - 1 ;
else if ( ver_a > ver_b ) rval = + 1 ;
else if ( num_a < num_b ) rval = - 1 ;
else if ( num_a > num_b ) rval = + 1 ;
else rval = 0 ;
return rval ;
2013-04-16 23:57:55 -04:00
}
// Return the log files in sorted order
// Return a null_terminated array of strings, and also return the number of strings in the array.
2013-04-16 23:59:02 -04:00
// Requires: Race conditions must be dealt with by caller. Either call during initialization or grab the output permission.
int toku_logger_find_logfiles ( const char * directory , char * * * resultp , int * n_logfiles )
{
2013-04-16 23:57:55 -04:00
int result_limit = 2 ;
int n_results = 0 ;
char * * MALLOC_N ( result_limit , result ) ;
2013-04-16 23:57:55 -04:00
assert ( result ! = NULL ) ;
2013-04-16 23:57:55 -04:00
struct dirent * de ;
DIR * d = opendir ( directory ) ;
if ( d = = 0 ) {
2013-04-17 00:00:58 -04:00
int er = get_error_errno ( ) ;
2013-04-16 23:57:55 -04:00
toku_free ( result ) ;
2013-04-17 00:00:58 -04:00
return er ;
2013-04-16 23:57:55 -04:00
}
int dirnamelen = strlen ( directory ) ;
while ( ( de = readdir ( d ) ) ) {
2013-04-17 00:00:43 -04:00
uint64_t thisl ;
2013-04-16 23:59:25 -04:00
uint32_t version_ignore ;
if ( ! ( is_a_logfile_any_version ( de - > d_name , & thisl , & version_ignore ) ) ) continue ; //#2424: Skip over files that don't match the exact logfile template
2013-04-17 00:00:43 -04:00
if ( n_results + 1 > = result_limit ) {
result_limit * = 2 ;
2013-04-17 00:00:58 -04:00
XREALLOC_N ( result_limit , result ) ;
2013-04-17 00:00:43 -04:00
}
int fnamelen = dirnamelen + strlen ( de - > d_name ) + 2 ; // One for the slash and one for the trailing NUL.
2013-04-17 00:00:58 -04:00
char * XMALLOC_N ( fnamelen , fname ) ;
2013-04-17 00:00:43 -04:00
snprintf ( fname , fnamelen , " %s/%s " , directory , de - > d_name ) ;
result [ n_results + + ] = fname ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:59:25 -04:00
// Return them in increasing order. Set width to allow for newer log file names ("xxx.tokulog13")
// which are one character longer than old log file names ("xxx.tokulog2"). The comparison function
// won't look beyond the terminating NUL, so an extra character in the comparison string doesn't matter.
// Allow room for terminating NUL after "xxx.tokulog13" even if result[0] is of form "xxx.tokulog2."
int width = sizeof ( result [ 0 ] + 2 ) ;
qsort ( result , n_results , width , logfilenamecompare ) ;
2013-04-16 23:57:55 -04:00
* resultp = result ;
* n_logfiles = n_results ;
result [ n_results ] = 0 ; // make a trailing null
return d ? closedir ( d ) : 0 ;
}
2013-04-16 23:59:02 -04:00
static int open_logfile ( TOKULOGGER logger )
// Entry and Exit: This thread has permission to modify the output.
{
2013-04-16 23:57:55 -04:00
int fnamelen = strlen ( logger - > directory ) + 50 ;
char fname [ fnamelen ] ;
2013-04-16 23:59:17 -04:00
snprintf ( fname , fnamelen , " %s/log%012lld.tokulog%d " , logger - > directory , logger - > next_log_file_number , TOKU_LOG_VERSION ) ;
2013-04-16 23:58:01 -04:00
long long index = logger - > next_log_file_number ;
2013-04-16 23:57:55 -04:00
if ( logger - > write_log_files ) {
2013-04-16 23:57:59 -04:00
logger - > fd = open ( fname , O_CREAT + O_WRONLY + O_TRUNC + O_EXCL + O_BINARY , S_IRWXU ) ;
2013-04-17 00:01:08 -04:00
if ( logger - > fd = = - 1 ) {
return get_error_errno ( ) ;
}
fsync_logdir ( logger ) ;
2013-04-16 23:57:59 -04:00
logger - > next_log_file_number + + ;
2013-04-16 23:57:55 -04:00
} else {
logger - > fd = open ( DEV_NULL_FILE , O_WRONLY + O_BINARY ) ;
2013-04-17 00:01:08 -04:00
if ( logger - > fd = = - 1 ) {
return get_error_errno ( ) ;
}
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:59:02 -04:00
toku_os_full_write ( logger - > fd , " tokulogg " , 8 ) ;
2013-04-16 23:57:55 -04:00
int version_l = toku_htonl ( log_format_version ) ; //version MUST be in network byte order regardless of disk order
2013-04-16 23:59:02 -04:00
toku_os_full_write ( logger - > fd , & version_l , 4 ) ;
2013-04-16 23:58:01 -04:00
if ( logger - > write_log_files ) {
2013-04-17 00:01:08 -04:00
TOKULOGFILEINFO XMALLOC ( lf_info ) ;
2013-04-16 23:58:01 -04:00
lf_info - > index = index ;
2013-04-16 23:58:58 -04:00
lf_info - > maxlsn = logger - > written_lsn ;
2013-04-16 23:59:25 -04:00
lf_info - > version = TOKU_LOG_VERSION ;
2013-04-16 23:58:01 -04:00
toku_logfilemgr_add_logfile_info ( logger - > logfilemgr , lf_info ) ;
}
2013-04-16 23:57:55 -04:00
logger - > fsynced_lsn = logger - > written_lsn ;
logger - > n_in_file = 12 ;
return 0 ;
}
2013-04-17 00:01:15 -04:00
static void delete_logfile ( TOKULOGGER logger , long long index , uint32_t version )
2013-04-16 23:59:02 -04:00
// Entry and Exit: This thread has permission to modify the output.
{
2013-04-16 23:58:01 -04:00
int fnamelen = strlen ( logger - > directory ) + 50 ;
char fname [ fnamelen ] ;
2013-04-16 23:59:25 -04:00
snprintf ( fname , fnamelen , " %s/log%012lld.tokulog%d " , logger - > directory , index , version ) ;
2013-04-16 23:58:01 -04:00
int r = remove ( fname ) ;
2013-04-17 00:01:15 -04:00
invariant_zero ( r ) ;
2013-04-16 23:58:01 -04:00
}
2013-04-17 00:01:15 -04:00
void toku_logger_maybe_trim_log ( TOKULOGGER logger , LSN trim_lsn )
2013-04-16 23:59:02 -04:00
// On entry and exit: No logger locks held.
// Acquires and releases output permission.
{
LSN fsynced_lsn ;
grab_output ( logger , & fsynced_lsn ) ;
2013-04-16 23:58:01 -04:00
TOKULOGFILEMGR lfm = logger - > logfilemgr ;
int n_logfiles = toku_logfilemgr_num_logfiles ( lfm ) ;
TOKULOGFILEINFO lf_info = NULL ;
2013-04-16 23:58:01 -04:00
2013-04-16 23:58:02 -04:00
if ( logger - > write_log_files & & logger - > trim_log_files ) {
2013-04-16 23:58:01 -04:00
while ( n_logfiles > 1 ) { // don't delete current logfile
2013-04-16 23:59:25 -04:00
uint32_t log_version ;
2013-04-16 23:58:01 -04:00
lf_info = toku_logfilemgr_get_oldest_logfile_info ( lfm ) ;
2013-04-16 23:59:25 -04:00
log_version = lf_info - > version ;
2013-04-16 23:59:37 -04:00
if ( lf_info - > maxlsn . lsn > = trim_lsn . lsn ) {
2013-04-16 23:58:01 -04:00
// file contains an open LSN, can't delete this or any newer log files
break ;
}
// need to save copy - toku_logfilemgr_delete_oldest_logfile_info free's the lf_info
long index = lf_info - > index ;
toku_logfilemgr_delete_oldest_logfile_info ( lfm ) ;
n_logfiles - - ;
2013-04-17 00:01:15 -04:00
delete_logfile ( logger , index , log_version ) ;
2013-04-16 23:58:01 -04:00
}
}
2013-04-16 23:59:02 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-16 23:58:01 -04:00
}
2013-04-17 00:01:01 -04:00
void toku_logger_write_log_files ( TOKULOGGER logger , bool write_log_files )
2013-04-17 00:00:14 -04:00
// Called only during initialization (or just after recovery), so no locks are needed.
2013-04-16 23:59:02 -04:00
{
2013-04-16 23:57:59 -04:00
logger - > write_log_files = write_log_files ;
}
2013-04-17 00:01:01 -04:00
void toku_logger_trim_log_files ( TOKULOGGER logger , bool trim_log_files )
2013-04-16 23:59:02 -04:00
// Called only during initialization, so no locks are needed.
{
2013-04-16 23:58:02 -04:00
logger - > trim_log_files = trim_log_files ;
}
2013-04-17 00:01:08 -04:00
void toku_logger_maybe_fsync ( TOKULOGGER logger , LSN lsn , int do_fsync )
2013-04-16 23:58:04 -04:00
// Effect: If fsync is nonzero, then make sure that the log is flushed and synced at least up to lsn.
2013-04-16 23:59:02 -04:00
// Entry: Holds input lock. The log entry has already been written to the input buffer.
2013-04-16 23:58:04 -04:00
// Exit: Holds no locks.
2013-04-16 23:59:02 -04:00
// The input lock may be released and then reacquired. Thus this function does not run atomically with respect to other threads.
2013-04-16 23:58:04 -04:00
{
2013-04-16 23:59:02 -04:00
if ( do_fsync ) {
2013-04-17 00:00:43 -04:00
// reacquire the locks (acquire output permission first)
logger - > input_lock_ctr + + ;
ml_unlock ( & logger - > input_lock ) ;
LSN fsynced_lsn ;
2013-04-17 00:01:01 -04:00
bool already_done = wait_till_output_already_written_or_output_buffer_available ( logger , lsn , & fsynced_lsn ) ;
2013-04-17 00:01:08 -04:00
if ( already_done ) {
return ;
}
2013-04-16 23:59:02 -04:00
2013-04-17 00:00:43 -04:00
// otherwise we now own the output permission, and our lsn isn't outputed.
2013-04-16 23:59:02 -04:00
2013-04-17 00:00:43 -04:00
ml_lock ( & logger - > input_lock ) ;
logger - > input_lock_ctr + + ;
2013-04-16 23:58:04 -04:00
2013-04-17 00:00:43 -04:00
swap_inbuf_outbuf ( logger ) ;
logger - > input_lock_ctr + + ;
ml_unlock ( & logger - > input_lock ) ; // release the input lock now, so other threads can fill the inbuf. (Thus enabling group commit.)
write_outbuf_to_logfile ( logger , & fsynced_lsn ) ;
if ( fsynced_lsn . lsn < lsn . lsn ) {
// it may have gotten fsynced by the write_outbuf_to_logfile.
2013-04-17 00:01:08 -04:00
toku_file_fsync_without_accounting ( logger - > fd ) ;
2013-04-17 00:00:43 -04:00
assert ( fsynced_lsn . lsn < = logger - > written_lsn . lsn ) ;
fsynced_lsn = logger - > written_lsn ;
}
// the last lsn is only accessed while holding output permission or else when the log file is old.
2013-04-17 00:01:08 -04:00
if ( logger - > write_log_files ) {
2013-04-17 00:00:43 -04:00
toku_logfilemgr_update_last_lsn ( logger - > logfilemgr , logger - > written_lsn ) ;
2013-04-17 00:01:08 -04:00
}
2013-04-17 00:00:43 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-16 23:59:02 -04:00
} else {
2013-04-17 00:00:43 -04:00
logger - > input_lock_ctr + + ;
ml_unlock ( & logger - > input_lock ) ;
2013-04-16 23:57:55 -04:00
}
}
2013-04-17 00:01:08 -04:00
static void
logger_write_buffer ( TOKULOGGER logger , LSN * fsynced_lsn )
2013-04-16 23:59:02 -04:00
// Entry: Holds the input lock and permission to modify output.
// Exit: Holds only the permission to modify output.
2013-04-16 23:58:04 -04:00
// Effect: Write the buffers to the output. If DO_FSYNC is true, then fsync.
2013-04-16 23:59:02 -04:00
// Note: Only called during single-threaded activity from toku_logger_restart, so locks aren't really needed.
2013-04-16 23:58:04 -04:00
{
2013-04-16 23:59:02 -04:00
swap_inbuf_outbuf ( logger ) ;
2013-04-17 00:00:31 -04:00
logger - > input_lock_ctr + + ;
ml_unlock ( & logger - > input_lock ) ;
2013-04-16 23:59:02 -04:00
write_outbuf_to_logfile ( logger , fsynced_lsn ) ;
if ( logger - > write_log_files ) {
2013-04-17 00:01:08 -04:00
toku_file_fsync_without_accounting ( logger - > fd ) ;
2013-04-17 00:00:43 -04:00
toku_logfilemgr_update_last_lsn ( logger - > logfilemgr , logger - > written_lsn ) ; // t:2294
2013-04-16 23:58:04 -04:00
}
}
2013-04-16 23:59:02 -04:00
int toku_logger_restart ( TOKULOGGER logger , LSN lastlsn )
// Entry and exit: Holds no locks (this is called only during single-threaded activity, such as initial start).
{
2013-04-16 23:57:59 -04:00
int r ;
// flush out the log buffer
2013-04-16 23:59:02 -04:00
LSN fsynced_lsn ;
grab_output ( logger , & fsynced_lsn ) ;
2013-04-17 00:00:31 -04:00
ml_lock ( & logger - > input_lock ) ;
2013-04-16 23:59:03 -04:00
logger - > input_lock_ctr + + ;
2013-04-17 00:01:08 -04:00
logger_write_buffer ( logger , & fsynced_lsn ) ;
2013-04-16 23:57:59 -04:00
// close the log file
2013-04-16 23:59:02 -04:00
if ( logger - > write_log_files ) { // fsyncs don't work to /dev/null
2013-04-17 00:01:08 -04:00
toku_file_fsync_without_accounting ( logger - > fd ) ;
2013-04-16 23:59:02 -04:00
}
r = close ( logger - > fd ) ; assert ( r = = 0 ) ;
2013-04-16 23:57:59 -04:00
logger - > fd = - 1 ;
// reset the LSN's to the lastlsn when the logger was opened
logger - > lsn = logger - > written_lsn = logger - > fsynced_lsn = lastlsn ;
2013-04-17 00:01:01 -04:00
logger - > write_log_files = true ;
logger - > trim_log_files = true ;
2013-04-16 23:57:59 -04:00
// open a new log file
2013-04-16 23:59:02 -04:00
r = open_logfile ( logger ) ;
release_output ( logger , fsynced_lsn ) ;
return r ;
2013-04-16 23:57:59 -04:00
}
2013-04-16 23:58:04 -04:00
// fname is the iname
2013-04-17 00:01:08 -04:00
void toku_logger_log_fcreate ( TOKUTXN txn , const char * fname , FILENUM filenum , uint32_t mode ,
uint32_t treeflags , uint32_t nodesize , uint32_t basementnodesize ,
enum toku_compression_method compression_method ) {
if ( txn ) {
BYTESTRING bs_fname = { . len = ( uint32_t ) strlen ( fname ) , . data = ( char * ) fname } ;
// fsync log on fcreate
toku_log_fcreate ( txn - > logger , ( LSN * ) 0 , 1 , txn , toku_txn_get_txnid ( txn ) , filenum ,
bs_fname , mode , treeflags , nodesize , basementnodesize , compression_method ) ;
}
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:58:04 -04:00
2013-04-17 00:00:37 -04:00
// We only do fdelete on open ft's, so we pass the filenum here
2013-04-17 00:01:08 -04:00
void toku_logger_log_fdelete ( TOKUTXN txn , FILENUM filenum ) {
if ( txn ) {
//No fsync.
toku_log_fdelete ( txn - > logger , ( LSN * ) 0 , 0 , txn , toku_txn_get_txnid ( txn ) , filenum ) ;
}
2013-04-16 23:58:04 -04:00
}
2013-04-16 23:57:55 -04:00
/* fopen isn't really an action. It's just for bookkeeping. We need to know the filename that goes with a filenum. */
2013-04-17 00:01:08 -04:00
void toku_logger_log_fopen ( TOKUTXN txn , const char * fname , FILENUM filenum , uint32_t treeflags ) {
if ( txn ) {
BYTESTRING bs ;
bs . len = strlen ( fname ) ;
bs . data = ( char * ) fname ;
toku_log_fopen ( txn - > logger , ( LSN * ) 0 , 0 , bs , filenum , treeflags ) ;
}
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:01 -04:00
static int toku_fread_uint8_t_nocrclen ( FILE * f , uint8_t * v ) {
2013-04-16 23:57:55 -04:00
int vi = fgetc ( f ) ;
if ( vi = = EOF ) return - 1 ;
2013-04-17 00:01:01 -04:00
uint8_t vc = ( uint8_t ) vi ;
2013-04-16 23:57:55 -04:00
* v = vc ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_fread_uint8_t ( FILE * f , uint8_t * v , struct x1764 * mm , uint32_t * len ) {
2013-04-16 23:57:55 -04:00
int vi = fgetc ( f ) ;
if ( vi = = EOF ) return - 1 ;
2013-04-17 00:01:01 -04:00
uint8_t vc = ( uint8_t ) vi ;
2013-04-16 23:57:55 -04:00
x1764_add ( mm , & vc , 1 ) ;
( * len ) + + ;
* v = vc ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_fread_uint32_t_nocrclen ( FILE * f , uint32_t * v ) {
uint32_t result ;
uint8_t * cp = ( uint8_t * ) & result ;
2013-04-16 23:57:55 -04:00
int r ;
2013-04-17 00:01:01 -04:00
r = toku_fread_uint8_t_nocrclen ( f , cp + 0 ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t_nocrclen ( f , cp + 1 ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t_nocrclen ( f , cp + 2 ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t_nocrclen ( f , cp + 3 ) ; if ( r ! = 0 ) return r ;
2013-04-16 23:57:55 -04:00
* v = toku_dtoh32 ( result ) ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_fread_uint32_t ( FILE * f , uint32_t * v , struct x1764 * checksum , uint32_t * len ) {
uint32_t result ;
uint8_t * cp = ( uint8_t * ) & result ;
2013-04-16 23:57:55 -04:00
int r ;
2013-04-17 00:01:01 -04:00
r = toku_fread_uint8_t ( f , cp + 0 , checksum , len ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t ( f , cp + 1 , checksum , len ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t ( f , cp + 2 , checksum , len ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint8_t ( f , cp + 3 , checksum , len ) ; if ( r ! = 0 ) return r ;
2013-04-16 23:57:55 -04:00
* v = toku_dtoh32 ( result ) ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_fread_uint64_t ( FILE * f , uint64_t * v , struct x1764 * checksum , uint32_t * len ) {
uint32_t v1 , v2 ;
2013-04-16 23:57:55 -04:00
int r ;
2013-04-17 00:01:01 -04:00
r = toku_fread_uint32_t ( f , & v1 , checksum , len ) ; if ( r ! = 0 ) return r ;
r = toku_fread_uint32_t ( f , & v2 , checksum , len ) ; if ( r ! = 0 ) return r ;
* v = ( ( ( uint64_t ) v1 ) < < 32 ) | ( ( uint64_t ) v2 ) ;
2013-04-16 23:57:55 -04:00
return 0 ;
}
2013-04-16 23:58:53 -04:00
2013-04-17 00:01:01 -04:00
int toku_fread_bool ( FILE * f , bool * v , struct x1764 * mm , uint32_t * len ) {
uint8_t iv ;
int r = toku_fread_uint8_t ( f , & iv , mm , len ) ;
2013-04-17 00:00:58 -04:00
if ( r = = 0 ) {
* v = ( iv ! = 0 ) ;
}
2013-04-16 23:59:40 -04:00
return r ;
}
2013-04-17 00:01:01 -04:00
int toku_fread_LSN ( FILE * f , LSN * lsn , struct x1764 * checksum , uint32_t * len ) {
return toku_fread_uint64_t ( f , & lsn - > lsn , checksum , len ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:58:53 -04:00
2013-04-17 00:01:01 -04:00
int toku_fread_BLOCKNUM ( FILE * f , BLOCKNUM * b , struct x1764 * checksum , uint32_t * len ) {
return toku_fread_uint64_t ( f , ( uint64_t * ) & b - > b , checksum , len ) ;
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_fread_FILENUM ( FILE * f , FILENUM * filenum , struct x1764 * checksum , uint32_t * len ) {
return toku_fread_uint32_t ( f , & filenum - > fileid , checksum , len ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:58:53 -04:00
2013-04-17 00:01:01 -04:00
int toku_fread_TXNID ( FILE * f , TXNID * txnid , struct x1764 * checksum , uint32_t * len ) {
return toku_fread_uint64_t ( f , txnid , checksum , len ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_fread_XIDP ( FILE * f , XIDP * xidp , struct x1764 * checksum , uint32_t * len ) {
2013-04-17 00:00:17 -04:00
// These reads are verbose because XA defined the fields as "long", but we use 4 bytes, 1 byte and 1 byte respectively.
2013-04-17 00:00:17 -04:00
TOKU_XA_XID * XMALLOC ( xid ) ;
2013-04-17 00:00:17 -04:00
{
2013-04-17 00:01:01 -04:00
uint32_t formatID ;
int r = toku_fread_uint32_t ( f , & formatID , checksum , len ) ;
2013-04-17 00:00:58 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:17 -04:00
xid - > formatID = formatID ;
}
{
2013-04-17 00:01:01 -04:00
uint8_t gtrid_length ;
int r = toku_fread_uint8_t ( f , & gtrid_length , checksum , len ) ;
2013-04-17 00:00:58 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:17 -04:00
xid - > gtrid_length = gtrid_length ;
}
{
2013-04-17 00:01:01 -04:00
uint8_t bqual_length ;
int r = toku_fread_uint8_t ( f , & bqual_length , checksum , len ) ;
2013-04-17 00:00:58 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:17 -04:00
xid - > bqual_length = bqual_length ;
}
for ( int i = 0 ; i < xid - > gtrid_length + xid - > bqual_length ; i + + ) {
2013-04-17 00:01:01 -04:00
uint8_t byte ;
int r = toku_fread_uint8_t ( f , & byte , checksum , len ) ;
2013-04-17 00:00:43 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:58 -04:00
xid - > data [ i ] = byte ;
2013-04-17 00:00:14 -04:00
}
2013-04-17 00:00:17 -04:00
* xidp = xid ;
2013-04-17 00:00:14 -04:00
return 0 ;
}
2013-04-16 23:57:55 -04:00
// fills in the bs with malloced data.
2013-04-17 00:01:01 -04:00
int toku_fread_BYTESTRING ( FILE * f , BYTESTRING * bs , struct x1764 * checksum , uint32_t * len ) {
int r = toku_fread_uint32_t ( f , ( uint32_t * ) & bs - > len , checksum , len ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:58 -04:00
XMALLOC_N ( bs - > len , bs - > data ) ;
2013-04-17 00:01:01 -04:00
uint32_t i ;
2013-04-16 23:57:55 -04:00
for ( i = 0 ; i < bs - > len ; i + + ) {
2013-04-17 00:01:01 -04:00
r = toku_fread_uint8_t ( f , ( uint8_t * ) & bs - > data [ i ] , checksum , len ) ;
2013-04-17 00:00:43 -04:00
if ( r ! = 0 ) {
toku_free ( bs - > data ) ;
bs - > data = 0 ;
return r ;
}
2013-04-16 23:57:55 -04:00
}
return 0 ;
}
2013-04-16 23:58:58 -04:00
// fills in the fs with malloced data.
2013-04-17 00:01:01 -04:00
int toku_fread_FILENUMS ( FILE * f , FILENUMS * fs , struct x1764 * checksum , uint32_t * len ) {
int r = toku_fread_uint32_t ( f , ( uint32_t * ) & fs - > num , checksum , len ) ;
2013-04-16 23:58:58 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:58 -04:00
XMALLOC_N ( fs - > num , fs - > filenums ) ;
2013-04-17 00:01:01 -04:00
uint32_t i ;
2013-04-16 23:58:58 -04:00
for ( i = 0 ; i < fs - > num ; i + + ) {
2013-04-17 00:00:43 -04:00
r = toku_fread_FILENUM ( f , & fs - > filenums [ i ] , checksum , len ) ;
if ( r ! = 0 ) {
toku_free ( fs - > filenums ) ;
fs - > filenums = 0 ;
return r ;
}
2013-04-16 23:58:58 -04:00
}
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logprint_LSN ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
2013-04-16 23:57:55 -04:00
LSN v ;
int r = toku_fread_LSN ( inf , & v , checksum , len ) ;
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s=% " PRIu64 , fieldname , v . lsn ) ;
return 0 ;
}
2013-04-16 23:58:53 -04:00
2013-04-17 00:01:01 -04:00
int toku_logprint_TXNID ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
2013-04-16 23:57:55 -04:00
TXNID v ;
int r = toku_fread_TXNID ( inf , & v , checksum , len ) ;
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s=% " PRIu64 , fieldname , v ) ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logprint_XIDP ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
2013-04-17 00:00:17 -04:00
XIDP vp ;
int r = toku_fread_XIDP ( inf , & vp , checksum , len ) ;
2013-04-17 00:00:14 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:00:17 -04:00
fprintf ( outf , " %s={formatID=0x%lx gtrid_length=%ld bqual_length=%ld data= " , fieldname , vp - > formatID , vp - > gtrid_length , vp - > bqual_length ) ;
toku_print_bytes ( outf , vp - > gtrid_length + vp - > bqual_length , vp - > data ) ;
fprintf ( outf , " } " ) ;
toku_free ( vp ) ;
2013-04-17 00:00:14 -04:00
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logprint_uint8_t ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format ) {
uint8_t v ;
int r = toku_fread_uint8_t ( inf , & v , checksum , len ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s=%d " , fieldname , v ) ;
if ( format ) fprintf ( outf , format , v ) ;
else if ( v = = ' \' ' ) fprintf ( outf , " (' \' ') " ) ;
else if ( isprint ( v ) ) fprintf ( outf , " ('%c') " , v ) ;
else { } /*nothing*/
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_logprint_uint32_t ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format ) {
uint32_t v ;
int r = toku_fread_uint32_t ( inf , & v , checksum , len ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s= " , fieldname ) ;
fprintf ( outf , format ? format : " %d " , v ) ;
return 0 ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:57:55 -04:00
2013-04-17 00:01:01 -04:00
int toku_logprint_uint64_t ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format ) {
uint64_t v ;
int r = toku_fread_uint64_t ( inf , & v , checksum , len ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s= " , fieldname ) ;
2013-04-17 00:00:58 -04:00
fprintf ( outf , format ? format : " % " PRId64 , v ) ;
2013-04-16 23:57:55 -04:00
return 0 ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_logprint_bool ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
bool v ;
int r = toku_fread_bool ( inf , & v , checksum , len ) ;
2013-04-16 23:59:40 -04:00
if ( r ! = 0 ) return r ;
2013-04-17 00:01:01 -04:00
fprintf ( outf , " %s=%s " , fieldname , v ? " true " : " false " ) ;
2013-04-16 23:59:40 -04:00
return 0 ;
}
2013-04-17 00:01:01 -04:00
void toku_print_BYTESTRING ( FILE * outf , uint32_t len , char * data ) {
2013-04-17 00:00:17 -04:00
fprintf ( outf , " {len=%u data= " , len ) ;
toku_print_bytes ( outf , len , data ) ;
fprintf ( outf , " } " ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_logprint_BYTESTRING ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
2013-04-16 23:57:55 -04:00
BYTESTRING bs ;
int r = toku_fread_BYTESTRING ( inf , & bs , checksum , len ) ;
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s= " , fieldname ) ;
toku_print_BYTESTRING ( outf , bs . len , bs . data ) ;
toku_free ( bs . data ) ;
return 0 ;
}
2013-04-16 23:58:58 -04:00
2013-04-17 00:01:01 -04:00
int toku_logprint_BLOCKNUM ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format ) {
return toku_logprint_uint64_t ( outf , inf , fieldname , checksum , len , format ) ;
2013-04-16 23:59:05 -04:00
}
2013-04-17 00:01:01 -04:00
int toku_logprint_FILENUM ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format ) {
return toku_logprint_uint32_t ( outf , inf , fieldname , checksum , len , format ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:58:58 -04:00
static void
2013-04-17 00:01:01 -04:00
toku_print_FILENUMS ( FILE * outf , uint32_t num , FILENUM * filenums ) {
2013-04-16 23:58:58 -04:00
fprintf ( outf , " {num=%u filenums= \" " , num ) ;
2013-04-17 00:01:01 -04:00
uint32_t i ;
2013-04-16 23:58:58 -04:00
for ( i = 0 ; i < num ; i + + ) {
if ( i > 0 )
fprintf ( outf , " , " ) ;
2013-04-17 00:00:58 -04:00
fprintf ( outf , " 0x% " PRIx32 , filenums [ i ] . fileid ) ;
2013-04-16 23:58:58 -04:00
}
fprintf ( outf , " \" } " ) ;
}
2013-04-17 00:01:01 -04:00
int toku_logprint_FILENUMS ( FILE * outf , FILE * inf , const char * fieldname , struct x1764 * checksum , uint32_t * len , const char * format __attribute__ ( ( __unused__ ) ) ) {
2013-04-16 23:58:58 -04:00
FILENUMS bs ;
int r = toku_fread_FILENUMS ( inf , & bs , checksum , len ) ;
if ( r ! = 0 ) return r ;
fprintf ( outf , " %s= " , fieldname ) ;
toku_print_FILENUMS ( outf , bs . num , bs . filenums ) ;
toku_free ( bs . filenums ) ;
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_read_and_print_logmagic ( FILE * f , uint32_t * versionp ) {
2013-04-16 23:57:55 -04:00
{
2013-04-17 00:00:43 -04:00
char magic [ 8 ] ;
int r = fread ( magic , 1 , 8 , f ) ;
if ( r ! = 8 ) {
return DB_BADFORMAT ;
}
if ( memcmp ( magic , " tokulogg " , 8 ) ! = 0 ) {
return DB_BADFORMAT ;
}
2013-04-16 23:57:55 -04:00
}
{
2013-04-17 00:00:43 -04:00
int version ;
int r = fread ( & version , 1 , 4 , f ) ;
if ( r ! = 4 ) {
return DB_BADFORMAT ;
}
printf ( " tokulog v.%u \n " , toku_ntohl ( version ) ) ;
2013-04-16 23:57:55 -04:00
//version MUST be in network order regardless of disk order
2013-04-17 00:00:43 -04:00
* versionp = toku_ntohl ( version ) ;
2013-04-16 23:57:55 -04:00
}
return 0 ;
}
2013-04-17 00:01:01 -04:00
int toku_read_logmagic ( FILE * f , uint32_t * versionp ) {
2013-04-16 23:57:55 -04:00
{
2013-04-17 00:00:43 -04:00
char magic [ 8 ] ;
int r = fread ( magic , 1 , 8 , f ) ;
if ( r ! = 8 ) {
return DB_BADFORMAT ;
}
if ( memcmp ( magic , " tokulogg " , 8 ) ! = 0 ) {
return DB_BADFORMAT ;
}
2013-04-16 23:57:55 -04:00
}
{
2013-04-17 00:00:43 -04:00
int version ;
int r = fread ( & version , 1 , 4 , f ) ;
if ( r ! = 4 ) {
return DB_BADFORMAT ;
}
* versionp = toku_ntohl ( version ) ;
2013-04-16 23:57:55 -04:00
}
return 0 ;
}
2013-04-16 23:57:55 -04:00
TXNID toku_txn_get_txnid ( TOKUTXN txn ) {
if ( txn = = 0 ) return 0 ;
else return txn - > txnid64 ;
}
LSN toku_logger_last_lsn ( TOKULOGGER logger ) {
2013-04-16 23:57:59 -04:00
return logger - > lsn ;
2013-04-16 23:57:55 -04:00
}
TOKULOGGER toku_txn_logger ( TOKUTXN txn ) {
return txn ? txn - > logger : 0 ;
}
2013-04-17 00:01:09 -04:00
void toku_txnid2txn ( TOKULOGGER logger , TXNID txnid , TOKUTXN * result ) {
2013-04-17 00:00:37 -04:00
toku_txn_manager_id2txn ( logger - > txn_manager , txnid , result ) ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:59:02 -04:00
// Find the earliest LSN in a log. No locks are needed.
2013-04-16 23:57:55 -04:00
static int peek_at_log ( TOKULOGGER logger , char * filename , LSN * first_lsn ) {
int fd = open ( filename , O_RDONLY + O_BINARY ) ;
if ( fd < 0 ) {
2013-04-17 00:00:58 -04:00
int er = get_error_errno ( ) ;
if ( logger - > write_log_files ) printf ( " couldn't open: %s \n " , strerror ( er ) ) ;
return er ;
2013-04-16 23:57:55 -04:00
}
enum { SKIP = 12 + 1 + 4 } ; // read the 12 byte header, the first cmd, and the first len
unsigned char header [ SKIP + 8 ] ;
int r = read ( fd , header , SKIP + 8 ) ;
if ( r ! = SKIP + 8 ) return 0 ; // cannot determine that it's archivable, so we'll assume no. If a later-log is archivable is then this one will be too.
2013-04-17 00:01:01 -04:00
uint64_t lsn ;
2013-04-16 23:57:55 -04:00
{
struct rbuf rb ;
rb . buf = header + SKIP ;
rb . size = 8 ;
rb . ndone = 0 ;
lsn = rbuf_ulonglong ( & rb ) ;
}
r = close ( fd ) ;
if ( r ! = 0 ) { return 0 ; }
first_lsn - > lsn = lsn ;
return 0 ;
}
// Return a malloc'd array of malloc'd strings which are the filenames that can be archived.
2013-04-16 23:59:02 -04:00
// Output permission are obtained briefly so we can get a list of the log files without conflicting.
2013-04-16 23:57:55 -04:00
int toku_logger_log_archive ( TOKULOGGER logger , char * * * logs_p , int flags ) {
if ( flags ! = 0 ) return EINVAL ; // don't know what to do.
int all_n_logs ;
int i ;
char * * all_logs ;
int n_logfiles ;
2013-04-16 23:59:02 -04:00
LSN fsynced_lsn ;
grab_output ( logger , & fsynced_lsn ) ;
2013-04-16 23:57:55 -04:00
int r = toku_logger_find_logfiles ( logger - > directory , & all_logs , & n_logfiles ) ;
2013-04-16 23:59:02 -04:00
release_output ( logger , fsynced_lsn ) ;
2013-04-16 23:57:55 -04:00
if ( r ! = 0 ) return r ;
for ( i = 0 ; all_logs [ i ] ; i + + ) ;
all_n_logs = i ;
// get them into increasing order
qsort ( all_logs , all_n_logs , sizeof ( all_logs [ 0 ] ) , logfilenamecompare ) ;
2013-04-16 23:59:05 -04:00
LSN save_lsn = logger - > last_completed_checkpoint_lsn ;
2013-04-16 23:57:55 -04:00
// Now starting at the last one, look for archivable ones.
// Count the total number of bytes, because we have to return a single big array. (That's the BDB interface. Bleah...)
2013-04-16 23:57:56 -04:00
LSN earliest_lsn_in_logfile = { ( unsigned long long ) ( - 1LL ) } ;
r = peek_at_log ( logger , all_logs [ all_n_logs - 1 ] , & earliest_lsn_in_logfile ) ; // try to find the lsn that's in the most recent log
2013-04-16 23:59:05 -04:00
if ( earliest_lsn_in_logfile . lsn < = save_lsn . lsn ) {
2013-04-17 00:00:43 -04:00
i = all_n_logs - 1 ;
2013-04-16 23:57:55 -04:00
} else {
2013-04-17 00:00:43 -04:00
for ( i = all_n_logs - 2 ; i > = 0 ; i - - ) { // start at all_n_logs-2 because we never archive the most recent log
r = peek_at_log ( logger , all_logs [ i ] , & earliest_lsn_in_logfile ) ;
if ( r ! = 0 ) continue ; // In case of error, just keep going
if ( earliest_lsn_in_logfile . lsn < = save_lsn . lsn ) {
break ;
}
}
2013-04-16 23:57:55 -04:00
}
// all log files up to, but but not including, i can be archived.
int n_to_archive = i ;
int count_bytes = 0 ;
for ( i = 0 ; i < n_to_archive ; i + + ) {
2013-04-17 00:00:43 -04:00
count_bytes + = 1 + strlen ( all_logs [ i ] ) ;
2013-04-16 23:57:55 -04:00
}
char * * result ;
if ( i = = 0 ) {
2013-04-17 00:00:43 -04:00
result = 0 ;
2013-04-16 23:57:55 -04:00
} else {
2013-04-17 00:00:59 -04:00
CAST_FROM_VOIDP ( result , toku_xmalloc ( ( 1 + n_to_archive ) * sizeof ( * result ) + count_bytes ) ) ;
2013-04-17 00:00:43 -04:00
char * base = ( char * ) ( result + 1 + n_to_archive ) ;
for ( i = 0 ; i < n_to_archive ; i + + ) {
int len = 1 + strlen ( all_logs [ i ] ) ;
result [ i ] = base ;
memcpy ( base , all_logs [ i ] , len ) ;
base + = len ;
}
result [ n_to_archive ] = 0 ;
2013-04-16 23:57:55 -04:00
}
for ( i = 0 ; all_logs [ i ] ; i + + ) {
2013-04-17 00:00:43 -04:00
toku_free ( all_logs [ i ] ) ;
2013-04-16 23:57:55 -04:00
}
toku_free ( all_logs ) ;
* logs_p = result ;
return 0 ;
}
TOKUTXN toku_logger_txn_parent ( TOKUTXN txn ) {
return txn - > parent ;
}
void toku_logger_note_checkpoint ( TOKULOGGER logger , LSN lsn ) {
2013-04-16 23:59:05 -04:00
logger - > last_completed_checkpoint_lsn = lsn ;
2013-04-16 23:57:55 -04:00
}
2013-04-16 23:57:56 -04:00
2013-04-17 00:00:08 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Engine status
//
// Status is intended for display to humans to help understand system behavior.
// It does not need to be perfectly thread-safe.
2013-04-16 23:59:03 -04:00
2013-04-17 00:00:08 -04:00
static LOGGER_STATUS_S logger_status ;
# define STATUS_INIT(k,t,l) { \
2013-04-17 00:00:43 -04:00
logger_status . status [ k ] . keyname = # k ; \
logger_status . status [ k ] . type = t ; \
logger_status . status [ k ] . legend = " logger: " l ; \
2013-04-16 23:59:15 -04:00
}
2013-04-17 00:00:08 -04:00
static void
status_init ( void ) {
// Note, this function initializes the keyname, type, and legend fields.
// Value fields are initialized to zero by compiler.
STATUS_INIT ( LOGGER_NEXT_LSN , UINT64 , " next LSN " ) ;
STATUS_INIT ( LOGGER_ILOCK_CTR , UINT64 , " ilock count " ) ;
STATUS_INIT ( LOGGER_OLOCK_CTR , UINT64 , " olock count " ) ;
STATUS_INIT ( LOGGER_SWAP_CTR , UINT64 , " swap count " ) ;
logger_status . initialized = true ;
}
# undef STATUS_INIT
# define STATUS_VALUE(x) logger_status.status[x].value.num
void
toku_logger_get_status ( TOKULOGGER logger , LOGGER_STATUS statp ) {
if ( ! logger_status . initialized )
2013-04-17 00:00:43 -04:00
status_init ( ) ;
2013-04-17 00:00:08 -04:00
if ( logger ) {
STATUS_VALUE ( LOGGER_NEXT_LSN ) = logger - > lsn . lsn ;
2013-04-17 00:00:43 -04:00
STATUS_VALUE ( LOGGER_ILOCK_CTR ) = logger - > input_lock_ctr ;
STATUS_VALUE ( LOGGER_OLOCK_CTR ) = logger - > output_condition_lock_ctr ;
STATUS_VALUE ( LOGGER_SWAP_CTR ) = logger - > swap_ctr ;
2013-04-16 23:59:15 -04:00
}
2013-04-17 00:00:08 -04:00
* statp = logger_status ;
2013-04-16 23:59:03 -04:00
}
2013-04-16 23:59:17 -04:00
2013-04-17 00:00:08 -04:00
//////////////////////////////////////////////////////////////////////////////////////////////////////
2013-04-16 23:59:33 -04:00
// Used for upgrade:
// if any valid log files exist in log_dir, then
2013-04-17 00:01:01 -04:00
// set *found_any_logs to true and set *version_found to version number of latest log
2013-04-16 23:59:17 -04:00
int
2013-04-17 00:01:01 -04:00
toku_get_version_of_logs_on_disk ( const char * log_dir , bool * found_any_logs , uint32_t * version_found ) {
bool found = false ;
2013-04-16 23:59:25 -04:00
uint32_t highest_version = 0 ;
2013-04-16 23:59:17 -04:00
int r = 0 ;
struct dirent * de ;
DIR * d = opendir ( log_dir ) ;
if ( d = = NULL ) {
2013-04-17 00:00:58 -04:00
r = get_error_errno ( ) ;
2013-04-16 23:59:17 -04:00
}
else {
2013-04-16 23:59:25 -04:00
// Examine every file in the directory and find highest version
2013-04-16 23:59:17 -04:00
while ( ( de = readdir ( d ) ) ) {
uint32_t this_log_version ;
uint64_t this_log_number ;
2013-04-17 00:01:01 -04:00
bool is_log = is_a_logfile_any_version ( de - > d_name , & this_log_number , & this_log_version ) ;
2013-04-16 23:59:17 -04:00
if ( is_log ) {
2013-04-16 23:59:33 -04:00
if ( ! found ) { // first log file found
2013-04-17 00:01:01 -04:00
found = true ;
2013-04-17 00:00:43 -04:00
highest_version = this_log_version ;
2013-04-16 23:59:25 -04:00
}
2013-04-17 00:00:43 -04:00
else
highest_version = highest_version > this_log_version ? highest_version : this_log_version ;
2013-04-16 23:59:17 -04:00
}
}
}
{
int r2 = closedir ( d ) ;
if ( r = = 0 ) r = r2 ;
}
if ( r = = 0 ) {
* found_any_logs = found ;
if ( found )
2013-04-16 23:59:25 -04:00
* version_found = highest_version ;
2013-04-16 23:59:17 -04:00
}
return r ;
}
2013-04-17 00:00:37 -04:00
TXN_MANAGER toku_logger_get_txn_manager ( TOKULOGGER logger ) {
return logger - > txn_manager ;
}
2013-04-17 00:00:08 -04:00
# undef STATUS_VALUE