Commit f73b5f87 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

#4869 increase code coverage of some logging code refs[t:4869]

git-svn-id: file:///svn/toku/tokudb@43404 c7de825b-a66e-492c-adef-691d508d4ae1
parent 924a51b8
...@@ -24,8 +24,7 @@ struct toku_logcursor { ...@@ -24,8 +24,7 @@ struct toku_logcursor {
#define LC_LSN_ERROR (DB_RUNRECOVERY) #define LC_LSN_ERROR (DB_RUNRECOVERY)
static void lc_print_logcursor (TOKULOGCURSOR lc) __attribute__((unused)); void toku_logcursor_print(TOKULOGCURSOR lc) {
static void lc_print_logcursor (TOKULOGCURSOR lc) {
printf("lc = %p\n", lc); printf("lc = %p\n", lc);
printf(" logdir = %s\n", lc->logdir); printf(" logdir = %s\n", lc->logdir);
printf(" logfiles = %p\n", lc->logfiles); printf(" logfiles = %p\n", lc->logfiles);
...@@ -51,7 +50,8 @@ static int lc_close_cur_logfile(TOKULOGCURSOR lc) { ...@@ -51,7 +50,8 @@ static int lc_close_cur_logfile(TOKULOGCURSOR lc) {
static toku_off_t lc_file_len(const char *name) { static toku_off_t lc_file_len(const char *name) {
toku_struct_stat buf; toku_struct_stat buf;
int r = toku_stat(name, &buf); assert(r == 0); int r = toku_stat(name, &buf);
assert(r == 0);
return buf.st_size; return buf.st_size;
} }
...@@ -117,15 +117,9 @@ static int lc_check_lsn(TOKULOGCURSOR lc, int dir) { ...@@ -117,15 +117,9 @@ static int lc_check_lsn(TOKULOGCURSOR lc, int dir) {
// - returns a pointer to a logcursor // - returns a pointer to a logcursor
static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) { static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) {
int failresult=0;
int r=0;
// malloc a cursor // malloc a cursor
TOKULOGCURSOR cursor = (TOKULOGCURSOR) toku_malloc(sizeof(struct toku_logcursor)); TOKULOGCURSOR cursor = (TOKULOGCURSOR) toku_xmalloc(sizeof(struct toku_logcursor));
if ( cursor == NULL ) {
failresult = ENOMEM;
goto lc_create_fail;
}
// find logfiles in logdir // find logfiles in logdir
cursor->is_open = FALSE; cursor->is_open = FALSE;
cursor->cur_logfiles_index = 0; cursor->cur_logfiles_index = 0;
...@@ -134,24 +128,12 @@ static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) { ...@@ -134,24 +128,12 @@ static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) {
cursor->buffer = toku_malloc(cursor->buffer_size); // it does not matter if it failes cursor->buffer = toku_malloc(cursor->buffer_size); // it does not matter if it failes
// cursor->logdir must be an absolute path // cursor->logdir must be an absolute path
if (toku_os_is_absolute_name(log_dir)) { if (toku_os_is_absolute_name(log_dir)) {
cursor->logdir = (char *) toku_malloc(strlen(log_dir)+1); cursor->logdir = (char *) toku_xmalloc(strlen(log_dir)+1);
if ( cursor->logdir == NULL ) {
failresult = ENOMEM;
goto lc_create_fail;
}
sprintf(cursor->logdir, "%s", log_dir); sprintf(cursor->logdir, "%s", log_dir);
} else { } else {
char *cwd = getcwd(NULL, 0); char *cwd = getcwd(NULL, 0);
if ( cwd == NULL ) { assert(cwd);
failresult = -1; cursor->logdir = (char *) toku_xmalloc(strlen(cwd)+strlen(log_dir)+2);
goto lc_create_fail;
}
cursor->logdir = (char *) toku_malloc(strlen(cwd)+strlen(log_dir)+2);
if ( cursor->logdir == NULL ) {
toku_free(cwd);
failresult = ENOMEM;
goto lc_create_fail;
}
sprintf(cursor->logdir, "%s/%s", cwd, log_dir); sprintf(cursor->logdir, "%s/%s", cwd, log_dir);
toku_free(cwd); toku_free(cwd);
} }
...@@ -161,12 +143,7 @@ static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) { ...@@ -161,12 +143,7 @@ static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) {
cursor->last_direction=LC_FIRST; cursor->last_direction=LC_FIRST;
*lc = cursor; *lc = cursor;
return r; return 0;
lc_create_fail:
toku_logcursor_destroy(&cursor);
*lc = NULL;
return failresult;
} }
static int lc_fix_bad_logfile(TOKULOGCURSOR lc); static int lc_fix_bad_logfile(TOKULOGCURSOR lc);
...@@ -194,29 +171,16 @@ int toku_logcursor_create_for_file(TOKULOGCURSOR *lc, const char *log_dir, const ...@@ -194,29 +171,16 @@ int toku_logcursor_create_for_file(TOKULOGCURSOR *lc, const char *log_dir, const
TOKULOGCURSOR cursor = *lc; TOKULOGCURSOR cursor = *lc;
int fullnamelen = strlen(cursor->logdir) + strlen(log_file) + 3; int fullnamelen = strlen(cursor->logdir) + strlen(log_file) + 3;
char *log_file_fullname = toku_malloc(fullnamelen); char *log_file_fullname = toku_xmalloc(fullnamelen);
if ( log_file_fullname == NULL ) {
failresult = ENOMEM;
goto fail;
}
sprintf(log_file_fullname, "%s/%s", cursor->logdir, log_file); sprintf(log_file_fullname, "%s/%s", cursor->logdir, log_file);
cursor->n_logfiles=1; cursor->n_logfiles=1;
char **logfiles = toku_malloc(sizeof(char**)); char **logfiles = toku_xmalloc(sizeof(char**));
if ( logfiles == NULL ) {
failresult = ENOMEM;
goto fail;
}
cursor->logfiles = logfiles; cursor->logfiles = logfiles;
cursor->logfiles[0] = log_file_fullname; cursor->logfiles[0] = log_file_fullname;
*lc = cursor; *lc = cursor;
return r; return 0;
fail:
toku_free(log_file_fullname);
toku_logcursor_destroy(&cursor);
*lc = NULL;
return failresult;
} }
int toku_logcursor_destroy(TOKULOGCURSOR *lc) { int toku_logcursor_destroy(TOKULOGCURSOR *lc) {
...@@ -461,9 +425,14 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) { ...@@ -461,9 +425,14 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
unsigned int version=0; unsigned int version=0;
int r = 0; int r = 0;
r = fseek(lc->cur_fp, 0, SEEK_SET); if ( r!=0 ) return r; r = fseek(lc->cur_fp, 0, SEEK_SET);
r = toku_read_logmagic(lc->cur_fp, &version); if ( r!=0 ) return r; if ( r!=0 )
if (version != TOKU_LOG_VERSION) return -1; return r;
r = toku_read_logmagic(lc->cur_fp, &version);
if ( r!=0 )
return r;
if (version != TOKU_LOG_VERSION)
return -1;
toku_off_t last_good_pos; toku_off_t last_good_pos;
last_good_pos = ftello(lc->cur_fp); last_good_pos = ftello(lc->cur_fp);
...@@ -473,7 +442,8 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) { ...@@ -473,7 +442,8 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
memset(&le, 0, sizeof(le)); memset(&le, 0, sizeof(le));
r = toku_log_fread(lc->cur_fp, &le); r = toku_log_fread(lc->cur_fp, &le);
toku_log_free_log_entry_resources(&le); toku_log_free_log_entry_resources(&le);
if ( r!=0 ) break; if ( r!=0 )
break;
last_good_pos = ftello(lc->cur_fp); last_good_pos = ftello(lc->cur_fp);
} }
// now have position of last good entry // now have position of last good entry
...@@ -481,9 +451,17 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) { ...@@ -481,9 +451,17 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
// 2) truncate the file to remove the error // 2) truncate the file to remove the error
// 3) reopen the file // 3) reopen the file
// 4) set the pos to last // 4) set the pos to last
r = lc_close_cur_logfile(lc); if ( r!=0 ) return r; r = lc_close_cur_logfile(lc);
r = truncate(lc->logfiles[lc->n_logfiles - 1], last_good_pos); if ( r!=0 ) return r; if ( r!=0 )
r = lc_open_logfile(lc, lc->n_logfiles-1); if ( r!=0 ) return r; return r;
r = fseek(lc->cur_fp, 0, SEEK_END); 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;
return 0; return 0;
} }
...@@ -42,6 +42,8 @@ int toku_logcursor_last(const TOKULOGCURSOR lc, struct log_entry **le); ...@@ -42,6 +42,8 @@ int toku_logcursor_last(const TOKULOGCURSOR lc, struct log_entry **le);
// return 0 if log exists, ENOENT if no log // return 0 if log exists, ENOENT if no log
int toku_logcursor_log_exists(const TOKULOGCURSOR lc); int toku_logcursor_log_exists(const TOKULOGCURSOR lc);
void toku_logcursor_print(TOKULOGCURSOR lc);
#if defined(__cplusplus) || defined(__cilkplusplus) #if defined(__cplusplus) || defined(__cilkplusplus)
}; };
#endif #endif
......
...@@ -21,32 +21,19 @@ struct toku_logfilemgr { ...@@ -21,32 +21,19 @@ struct toku_logfilemgr {
}; };
int toku_logfilemgr_create(TOKULOGFILEMGR *lfm) { int toku_logfilemgr_create(TOKULOGFILEMGR *lfm) {
int failresult=0;
// malloc a logfilemgr // malloc a logfilemgr
TOKULOGFILEMGR mgr = toku_malloc(sizeof(struct toku_logfilemgr)); TOKULOGFILEMGR mgr = toku_xmalloc(sizeof(struct toku_logfilemgr));
// printf("toku_logfilemgr_create [%p]\n", mgr);
if ( mgr == NULL ) {
failresult = ENOMEM;
goto createfail;
}
mgr->first = NULL; mgr->first = NULL;
mgr->last = NULL; mgr->last = NULL;
mgr->n_entries = 0; mgr->n_entries = 0;
*lfm = mgr; *lfm = mgr;
return 0; return 0;
createfail:
toku_logfilemgr_destroy(&mgr);
*lfm = NULL;
return failresult;
} }
int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) { int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) {
int r=0; int r=0;
if ( *lfm != NULL ) { // be tolerant of being passed a NULL if ( *lfm != NULL ) { // be tolerant of being passed a NULL
TOKULOGFILEMGR mgr = *lfm; TOKULOGFILEMGR mgr = *lfm;
// printf("toku_logfilemgr_destroy [%p], %d entries\n", mgr, mgr->n_entries);
while ( mgr->n_entries > 0 ) { while ( mgr->n_entries > 0 ) {
toku_logfilemgr_delete_oldest_logfile_info(mgr); toku_logfilemgr_delete_oldest_logfile_info(mgr);
} }
...@@ -57,7 +44,6 @@ int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) { ...@@ -57,7 +44,6 @@ int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) {
} }
int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) { int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
// printf("toku_logfilemgr_init [%p]\n", lfm);
assert(lfm!=0); assert(lfm!=0);
int r; int r;
...@@ -74,10 +60,7 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) { ...@@ -74,10 +60,7 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
char *basename; char *basename;
LSN tmp_lsn = {0}; LSN tmp_lsn = {0};
for(int i=0;i<n_logfiles;i++){ for(int i=0;i<n_logfiles;i++){
lf_info = toku_malloc(sizeof(struct toku_logfile_info)); lf_info = toku_xmalloc(sizeof(struct toku_logfile_info));
if ( lf_info == NULL ) {
return ENOMEM;
}
// find the index // find the index
// basename is the filename of the i-th logfile // basename is the filename of the i-th logfile
basename = strrchr(logfiles[i], '/') + 1; basename = strrchr(logfiles[i], '/') + 1;
...@@ -113,17 +96,14 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) { ...@@ -113,17 +96,14 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
} }
int toku_logfilemgr_num_logfiles(TOKULOGFILEMGR lfm) { int toku_logfilemgr_num_logfiles(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL); assert(lfm);
return lfm->n_entries; return lfm->n_entries;
} }
int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info) { int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info) {
assert(lfm!=NULL); assert(lfm);
struct lfm_entry *entry = toku_malloc(sizeof(struct lfm_entry)); struct lfm_entry *entry = toku_xmalloc(sizeof(struct lfm_entry));
// printf("toku_logfilemgr_add_logfile_info [%p] : entry [%p]\n", lfm, entry); entry->lf_info = lf_info;
int r=0;
if ( entry != NULL ) {
entry->lf_info=lf_info;
entry->next = NULL; entry->next = NULL;
if ( lfm->n_entries != 0 ) if ( lfm->n_entries != 0 )
lfm->last->next = entry; lfm->last->next = entry;
...@@ -132,22 +112,18 @@ int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info ...@@ -132,22 +112,18 @@ int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info
if (lfm->n_entries == 1 ) { if (lfm->n_entries == 1 ) {
lfm->first = lfm->last; lfm->first = lfm->last;
} }
} return 0;
else
r = ENOMEM;
return r;
} }
TOKULOGFILEINFO toku_logfilemgr_get_oldest_logfile_info(TOKULOGFILEMGR lfm) { TOKULOGFILEINFO toku_logfilemgr_get_oldest_logfile_info(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL); assert(lfm);
return lfm->first->lf_info; return lfm->first->lf_info;
} }
void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) { void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL); assert(lfm);
if ( lfm->n_entries > 0 ) { if ( lfm->n_entries > 0 ) {
struct lfm_entry *entry = lfm->first; struct lfm_entry *entry = lfm->first;
// printf("toku_logfilemgr_delete_oldest_logfile_info [%p] : entry [%p]\n", lfm, entry);
toku_free(entry->lf_info); toku_free(entry->lf_info);
lfm->first = entry->next; lfm->first = entry->next;
toku_free(entry); toku_free(entry);
...@@ -159,7 +135,7 @@ void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) { ...@@ -159,7 +135,7 @@ void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) {
} }
LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) { LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL); assert(lfm);
if ( lfm->n_entries == 0 ) { if ( lfm->n_entries == 0 ) {
LSN lsn; LSN lsn;
lsn.lsn = 0; lsn.lsn = 0;
...@@ -169,17 +145,16 @@ LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) { ...@@ -169,17 +145,16 @@ LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) {
} }
void toku_logfilemgr_update_last_lsn(TOKULOGFILEMGR lfm, LSN lsn) { void toku_logfilemgr_update_last_lsn(TOKULOGFILEMGR lfm, LSN lsn) {
assert(lfm!=NULL); assert(lfm);
assert(lfm->last!=NULL); assert(lfm->last!=NULL);
lfm->last->lf_info->maxlsn = lsn; lfm->last->lf_info->maxlsn = lsn;
} }
void toku_logfilemgr_print(TOKULOGFILEMGR lfm) { void toku_logfilemgr_print(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL); assert(lfm);
printf("toku_logfilemgr_print [%p] : %d entries \n", lfm, lfm->n_entries); printf("toku_logfilemgr_print [%p] : %d entries \n", lfm, lfm->n_entries);
int i;
struct lfm_entry *entry = lfm->first; struct lfm_entry *entry = lfm->first;
for (i=0;i<lfm->n_entries;i++) { for (int i=0;i<lfm->n_entries;i++) {
printf(" entry %d : index = %"PRId64", maxlsn = %"PRIu64"\n", i, entry->lf_info->index, entry->lf_info->maxlsn.lsn); printf(" entry %d : index = %"PRId64", maxlsn = %"PRIu64"\n", i, entry->lf_info->index, entry->lf_info->maxlsn.lsn);
entry = entry->next; entry = entry->next;
} }
......
#include "test.h"
#include "logcursor.h"
#define LOGDIR __FILE__ ".dir"
int test_main(int argc, const char *argv[]) {
int r;
default_parse_args(argc, argv);
r = system("rm -rf " LOGDIR);
assert(r == 0);
r = toku_os_mkdir(LOGDIR, S_IRWXU);
assert(r == 0);
TOKULOGCURSOR lc;
r = toku_logcursor_create(&lc, LOGDIR);
assert(r == 0);
toku_logcursor_print(lc);
r = toku_logcursor_destroy(&lc);
assert(r == 0);
return 0;
}
#include "test.h"
#include "logfilemgr.h"
int test_main(int argc, const char *argv[]) {
int r;
TOKULOGFILEMGR lfm = NULL;
r = toku_logfilemgr_create(&lfm);
assert(r == 0);
r = toku_logfilemgr_destroy(&lfm);
assert(r == 0);
return 0;
}
#include "test.h"
#include "logfilemgr.h"
int test_main(int argc, const char *argv[]) {
int r;
TOKULOGFILEMGR lfm = NULL;
r = toku_logfilemgr_create(&lfm);
assert(r == 0);
toku_logfilemgr_print(lfm);
r = toku_logfilemgr_destroy(&lfm);
assert(r == 0);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment