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 {
#define LC_LSN_ERROR (DB_RUNRECOVERY)
static void lc_print_logcursor (TOKULOGCURSOR lc) __attribute__((unused));
static void lc_print_logcursor (TOKULOGCURSOR lc) {
void toku_logcursor_print(TOKULOGCURSOR lc) {
printf("lc = %p\n", lc);
printf(" logdir = %s\n", lc->logdir);
printf(" logfiles = %p\n", lc->logfiles);
......@@ -51,7 +50,8 @@ static int lc_close_cur_logfile(TOKULOGCURSOR lc) {
static toku_off_t lc_file_len(const char *name) {
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;
}
......@@ -117,15 +117,9 @@ static int lc_check_lsn(TOKULOGCURSOR lc, int dir) {
// - returns a pointer to a logcursor
static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) {
int failresult=0;
int r=0;
// malloc a cursor
TOKULOGCURSOR cursor = (TOKULOGCURSOR) toku_malloc(sizeof(struct toku_logcursor));
if ( cursor == NULL ) {
failresult = ENOMEM;
goto lc_create_fail;
}
TOKULOGCURSOR cursor = (TOKULOGCURSOR) toku_xmalloc(sizeof(struct toku_logcursor));
// find logfiles in logdir
cursor->is_open = FALSE;
cursor->cur_logfiles_index = 0;
......@@ -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->logdir must be an absolute path
if (toku_os_is_absolute_name(log_dir)) {
cursor->logdir = (char *) toku_malloc(strlen(log_dir)+1);
if ( cursor->logdir == NULL ) {
failresult = ENOMEM;
goto lc_create_fail;
}
cursor->logdir = (char *) toku_xmalloc(strlen(log_dir)+1);
sprintf(cursor->logdir, "%s", log_dir);
} else {
char *cwd = getcwd(NULL, 0);
if ( cwd == NULL ) {
failresult = -1;
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;
}
assert(cwd);
cursor->logdir = (char *) toku_xmalloc(strlen(cwd)+strlen(log_dir)+2);
sprintf(cursor->logdir, "%s/%s", cwd, log_dir);
toku_free(cwd);
}
......@@ -161,12 +143,7 @@ static int lc_create(TOKULOGCURSOR *lc, const char *log_dir) {
cursor->last_direction=LC_FIRST;
*lc = cursor;
return r;
lc_create_fail:
toku_logcursor_destroy(&cursor);
*lc = NULL;
return failresult;
return 0;
}
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
TOKULOGCURSOR cursor = *lc;
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;
}
char *log_file_fullname = toku_xmalloc(fullnamelen);
sprintf(log_file_fullname, "%s/%s", cursor->logdir, log_file);
cursor->n_logfiles=1;
char **logfiles = toku_malloc(sizeof(char**));
if ( logfiles == NULL ) {
failresult = ENOMEM;
goto fail;
}
char **logfiles = toku_xmalloc(sizeof(char**));
cursor->logfiles = logfiles;
cursor->logfiles[0] = log_file_fullname;
*lc = cursor;
return r;
fail:
toku_free(log_file_fullname);
toku_logcursor_destroy(&cursor);
*lc = NULL;
return failresult;
return 0;
}
int toku_logcursor_destroy(TOKULOGCURSOR *lc) {
......@@ -461,9 +425,14 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
unsigned int version=0;
int r = 0;
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;
if (version != TOKU_LOG_VERSION) return -1;
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;
if (version != TOKU_LOG_VERSION)
return -1;
toku_off_t last_good_pos;
last_good_pos = ftello(lc->cur_fp);
......@@ -473,7 +442,8 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
memset(&le, 0, sizeof(le));
r = toku_log_fread(lc->cur_fp, &le);
toku_log_free_log_entry_resources(&le);
if ( r!=0 ) break;
if ( r!=0 )
break;
last_good_pos = ftello(lc->cur_fp);
}
// now have position of last good entry
......@@ -481,9 +451,17 @@ static int lc_fix_bad_logfile(TOKULOGCURSOR lc) {
// 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;
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;
return 0;
}
......@@ -42,6 +42,8 @@ int toku_logcursor_last(const TOKULOGCURSOR lc, struct log_entry **le);
// return 0 if log exists, ENOENT if no log
int toku_logcursor_log_exists(const TOKULOGCURSOR lc);
void toku_logcursor_print(TOKULOGCURSOR lc);
#if defined(__cplusplus) || defined(__cilkplusplus)
};
#endif
......
......@@ -21,32 +21,19 @@ struct toku_logfilemgr {
};
int toku_logfilemgr_create(TOKULOGFILEMGR *lfm) {
int failresult=0;
// malloc a logfilemgr
TOKULOGFILEMGR mgr = toku_malloc(sizeof(struct toku_logfilemgr));
// printf("toku_logfilemgr_create [%p]\n", mgr);
if ( mgr == NULL ) {
failresult = ENOMEM;
goto createfail;
}
TOKULOGFILEMGR mgr = toku_xmalloc(sizeof(struct toku_logfilemgr));
mgr->first = NULL;
mgr->last = NULL;
mgr->n_entries = 0;
*lfm = mgr;
return 0;
createfail:
toku_logfilemgr_destroy(&mgr);
*lfm = NULL;
return failresult;
}
int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) {
int r=0;
if ( *lfm != NULL ) { // be tolerant of being passed a NULL
TOKULOGFILEMGR mgr = *lfm;
// printf("toku_logfilemgr_destroy [%p], %d entries\n", mgr, mgr->n_entries);
while ( mgr->n_entries > 0 ) {
toku_logfilemgr_delete_oldest_logfile_info(mgr);
}
......@@ -57,7 +44,6 @@ int toku_logfilemgr_destroy(TOKULOGFILEMGR *lfm) {
}
int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
// printf("toku_logfilemgr_init [%p]\n", lfm);
assert(lfm!=0);
int r;
......@@ -74,10 +60,7 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
char *basename;
LSN tmp_lsn = {0};
for(int i=0;i<n_logfiles;i++){
lf_info = toku_malloc(sizeof(struct toku_logfile_info));
if ( lf_info == NULL ) {
return ENOMEM;
}
lf_info = toku_xmalloc(sizeof(struct toku_logfile_info));
// find the index
// basename is the filename of the i-th logfile
basename = strrchr(logfiles[i], '/') + 1;
......@@ -113,17 +96,14 @@ int toku_logfilemgr_init(TOKULOGFILEMGR lfm, const char *log_dir) {
}
int toku_logfilemgr_num_logfiles(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL);
assert(lfm);
return lfm->n_entries;
}
int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info) {
assert(lfm!=NULL);
struct lfm_entry *entry = toku_malloc(sizeof(struct lfm_entry));
// printf("toku_logfilemgr_add_logfile_info [%p] : entry [%p]\n", lfm, entry);
int r=0;
if ( entry != NULL ) {
entry->lf_info=lf_info;
assert(lfm);
struct lfm_entry *entry = toku_xmalloc(sizeof(struct lfm_entry));
entry->lf_info = lf_info;
entry->next = NULL;
if ( lfm->n_entries != 0 )
lfm->last->next = entry;
......@@ -132,22 +112,18 @@ int toku_logfilemgr_add_logfile_info(TOKULOGFILEMGR lfm, TOKULOGFILEINFO lf_info
if (lfm->n_entries == 1 ) {
lfm->first = lfm->last;
}
}
else
r = ENOMEM;
return r;
return 0;
}
TOKULOGFILEINFO toku_logfilemgr_get_oldest_logfile_info(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL);
assert(lfm);
return lfm->first->lf_info;
}
void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL);
assert(lfm);
if ( lfm->n_entries > 0 ) {
struct lfm_entry *entry = lfm->first;
// printf("toku_logfilemgr_delete_oldest_logfile_info [%p] : entry [%p]\n", lfm, entry);
toku_free(entry->lf_info);
lfm->first = entry->next;
toku_free(entry);
......@@ -159,7 +135,7 @@ void toku_logfilemgr_delete_oldest_logfile_info(TOKULOGFILEMGR lfm) {
}
LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL);
assert(lfm);
if ( lfm->n_entries == 0 ) {
LSN lsn;
lsn.lsn = 0;
......@@ -169,17 +145,16 @@ LSN toku_logfilemgr_get_last_lsn(TOKULOGFILEMGR lfm) {
}
void toku_logfilemgr_update_last_lsn(TOKULOGFILEMGR lfm, LSN lsn) {
assert(lfm!=NULL);
assert(lfm);
assert(lfm->last!=NULL);
lfm->last->lf_info->maxlsn = lsn;
}
void toku_logfilemgr_print(TOKULOGFILEMGR lfm) {
assert(lfm!=NULL);
assert(lfm);
printf("toku_logfilemgr_print [%p] : %d entries \n", lfm, lfm->n_entries);
int i;
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);
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