2007-11-29 14:18:54 +00:00
|
|
|
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
|
|
|
|
|
2008-02-08 03:17:38 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "toku_assert.h"
|
2008-02-14 19:23:25 +00:00
|
|
|
#include "list.h"
|
2008-02-08 03:17:38 +00:00
|
|
|
#include "yerror.h"
|
2007-08-10 21:15:17 +00:00
|
|
|
#include <stdio.h>
|
2008-03-12 17:55:11 +00:00
|
|
|
#include <pthread.h>
|
2007-09-28 17:11:22 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2008-03-12 17:55:11 +00:00
|
|
|
// Locking for the logger
|
|
|
|
// For most purposes we use the big ydb lock.
|
|
|
|
// To log: grab the buf lock
|
|
|
|
// If the buf would overflow, then grab the file lock, swap file&buf, release buf lock, write the file, write the entry, release the file lock
|
|
|
|
// else append to buf & release lock
|
|
|
|
|
2007-10-19 17:05:10 +00:00
|
|
|
#define LOGGER_BUF_SIZE (1<<24)
|
2008-03-12 17:55:11 +00:00
|
|
|
|
|
|
|
struct mylock {
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
int is_locked;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int ml_init(struct mylock *l) {
|
|
|
|
l->is_locked=0;
|
|
|
|
return pthread_mutex_init(&l->lock, 0);
|
|
|
|
}
|
|
|
|
static inline int ml_lock(struct mylock *l) {
|
|
|
|
int r = pthread_mutex_lock(&l->lock);
|
|
|
|
assert(l->is_locked==0);
|
|
|
|
l->is_locked=1;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
static inline int ml_unlock(struct mylock *l) {
|
|
|
|
assert(l->is_locked==1);
|
|
|
|
l->is_locked=0;
|
|
|
|
return pthread_mutex_unlock(&l->lock);
|
|
|
|
}
|
|
|
|
static inline int ml_destroy(struct mylock *l) {
|
|
|
|
assert(l->is_locked==0);
|
|
|
|
return pthread_mutex_destroy(&l->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-10 19:24:45 +00:00
|
|
|
struct tokulogger {
|
2008-03-12 17:55:11 +00:00
|
|
|
enum typ_tag tag; // must be first
|
|
|
|
struct mylock input_lock, output_lock; // acquired in that order
|
2008-01-08 22:18:42 +00:00
|
|
|
int is_open;
|
|
|
|
int is_panicked;
|
|
|
|
int panic_errno;
|
2007-08-10 19:24:45 +00:00
|
|
|
char *directory;
|
2007-08-10 21:39:55 +00:00
|
|
|
int fd;
|
2008-01-11 22:24:43 +00:00
|
|
|
CACHETABLE ct;
|
2008-03-08 13:02:32 +00:00
|
|
|
int lg_max; // The size of the single file in the log. Default is 100MB in TokuDB
|
2008-03-12 17:55:11 +00:00
|
|
|
|
|
|
|
// To access these, you must have the input lock
|
|
|
|
struct logbytes *head,*tail;
|
|
|
|
LSN lsn; // the next available lsn
|
|
|
|
struct list live_txns; // just a linked list. Should be a hashtable.
|
|
|
|
int n_in_buf;
|
|
|
|
|
|
|
|
// To access these, you must have the output lock
|
|
|
|
LSN written_lsn; // the last lsn written
|
|
|
|
LSN fsynced_lsn; // What is the LSN of the highest fsynced log entry
|
|
|
|
long long next_log_file_number;
|
|
|
|
char buf[LOGGER_BUF_SIZE]; // used to marshall logbytes so we can use only a single write
|
|
|
|
int n_in_file;
|
|
|
|
|
2007-08-10 19:24:45 +00:00
|
|
|
};
|
2007-08-10 21:15:17 +00:00
|
|
|
|
2007-11-29 18:14:40 +00:00
|
|
|
int toku_logger_find_next_unused_log_file(const char *directory, long long *result);
|
|
|
|
int toku_logger_find_logfiles (const char *directory, int *n_resultsp, char ***resultp);
|
2007-09-28 17:11:22 +00:00
|
|
|
|
2007-11-21 13:07:49 +00:00
|
|
|
enum lt_command {
|
2007-11-14 17:58:38 +00:00
|
|
|
LT_COMMIT = 'C',
|
|
|
|
LT_DELETE = 'D',
|
2007-11-19 23:47:44 +00:00
|
|
|
LT_FCREATE = 'F',
|
2007-11-21 13:07:49 +00:00
|
|
|
LT_FHEADER = 'H',
|
2007-11-14 17:58:38 +00:00
|
|
|
LT_INSERT_WITH_NO_OVERWRITE = 'I',
|
2007-11-21 19:06:32 +00:00
|
|
|
LT_NEWBRTNODE = 'N',
|
2007-11-20 21:20:05 +00:00
|
|
|
LT_FOPEN = 'O',
|
2007-11-14 17:58:38 +00:00
|
|
|
LT_CHECKPOINT = 'P',
|
2007-11-18 12:48:36 +00:00
|
|
|
LT_BLOCK_RENAME = 'R',
|
|
|
|
LT_UNLINK = 'U'
|
2007-11-14 17:58:38 +00:00
|
|
|
};
|
2007-09-28 17:11:22 +00:00
|
|
|
|
|
|
|
struct tokutxn {
|
2008-02-14 19:23:25 +00:00
|
|
|
enum typ_tag tag;
|
2007-09-28 17:11:22 +00:00
|
|
|
u_int64_t txnid64;
|
|
|
|
TOKULOGGER logger;
|
2007-10-19 17:05:10 +00:00
|
|
|
TOKUTXN parent;
|
2007-11-24 03:50:28 +00:00
|
|
|
LSN last_lsn; /* Everytime anything is logged, update the LSN. (We need to atomically record the LSN along with writing into the log.) */
|
2008-02-26 15:51:15 +00:00
|
|
|
struct roll_entry *oldest_logentry,*newest_logentry; /* Only logentries with rollbacks are here. There is a list going from newest to oldest. */
|
2008-02-14 19:23:25 +00:00
|
|
|
struct list live_txns_link;
|
2007-09-28 17:11:22 +00:00
|
|
|
};
|
2007-11-22 07:13:08 +00:00
|
|
|
|
2008-03-12 17:55:11 +00:00
|
|
|
int toku_logger_finish (TOKULOGGER logger, struct logbytes *logbytes, struct wbuf *wbuf, int do_fsync);
|
2007-11-22 18:45:22 +00:00
|
|
|
|
2007-11-23 17:16:26 +00:00
|
|
|
static inline int toku_logsizeof_u_int8_t (u_int32_t v __attribute__((__unused__))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-22 18:45:22 +00:00
|
|
|
static inline int toku_logsizeof_u_int32_t (u_int32_t v __attribute__((__unused__))) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int toku_logsizeof_FILENUM (FILENUM v __attribute__((__unused__))) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int toku_logsizeof_DISKOFF (DISKOFF v __attribute__((__unused__))) {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int toku_logsizeof_TXNID (TXNID txnid __attribute__((__unused__))) {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int toku_logsizeof_BYTESTRING (BYTESTRING bs) {
|
2007-11-22 20:30:38 +00:00
|
|
|
return 4+bs.len;
|
2007-11-22 18:45:22 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 21:11:21 +00:00
|
|
|
static inline int toku_logsizeof_LOGGEDBRTHEADER (LOGGEDBRTHEADER bs) {
|
|
|
|
assert(bs.n_named_roots=0);
|
|
|
|
return 4+4+4+8+8+4+8;
|
|
|
|
}
|
2007-12-04 10:02:59 +00:00
|
|
|
|
|
|
|
static inline int toku_logsizeof_INTPAIRARRAY (INTPAIRARRAY pa) {
|
|
|
|
return 4+(4+4)*pa.size;
|
|
|
|
}
|
2008-02-14 19:23:25 +00:00
|
|
|
|
2008-03-14 19:14:31 +00:00
|
|
|
static inline char *fixup_fname(BYTESTRING *f) {
|
|
|
|
assert(f->len>0);
|
|
|
|
char *fname = toku_malloc(f->len+1);
|
|
|
|
memcpy(fname, f->data, f->len);
|
|
|
|
fname[f->len]=0;
|
|
|
|
return fname;
|
|
|
|
}
|