diff --git a/lladd/logger/logger2.h b/lladd/logger/logger2.h index 51894c7..ba197e7 100644 --- a/lladd/logger/logger2.h +++ b/lladd/logger/logger2.h @@ -83,6 +83,18 @@ typedef struct { LogHandle lh; } TransactionLog; +#define LOG_TO_FILE 0 +#define LOG_TO_MEMORY 1 + +int LogInit(int logType); + +int LogDeinit(); + +void LogForce(lsn_t lsn); + +lsn_t LogTruncationPoint(); + +LogEntry * LogReadLSN(lsn_t lsn); /** Inform the logging layer that a new transaction has begun. @@ -118,7 +130,8 @@ LogEntry * LogUpdate(TransactionLog * l, Page * p, recordid rid, int operation, (Needed so that the lsn slot of the page in question can be updated.) */ -lsn_t LogCLR (LogEntry * undone); +//lsn_t LogCLR (LogEntry * undone); +lsn_t LogCLR(int xid, lsn_t LSN, recordid rid, lsn_t prevLSN); /** Write a end transaction record @see XEND diff --git a/src/lladd/logger/logHandle.c b/src/lladd/logger/logHandle.c index 3a3b244..1176188 100644 --- a/src/lladd/logger/logHandle.c +++ b/src/lladd/logger/logHandle.c @@ -41,7 +41,6 @@ terms specified in this license. ---*/ #include "logHandle.h" -#include "logWriter.h" #include #include @@ -56,7 +55,7 @@ static void set_offsets(LogHandle * h, LogEntry * e, lsn_t lastRead); LogHandle getLogHandle() { - lsn_t lsn = firstLogEntry(); + lsn_t lsn = LogTruncationPoint(); return getGuardedHandle(lsn, NULL, NULL); } @@ -75,7 +74,7 @@ LogHandle getGuardedHandle(lsn_t lsn, guard_fcn_t * guard, void * guard_state) { } LogEntry * nextInLog(LogHandle * h) { - LogEntry * ret = readLSNEntry(h->next_offset); + LogEntry * ret = LogReadLSN(h->next_offset); if(ret != NULL) { set_offsets(h, ret, h->next_offset); } @@ -95,7 +94,7 @@ LogEntry * previousInTransaction(LogHandle * h) { LogEntry * ret = NULL; if(h->prev_offset > 0) { /* printf("A"); fflush(NULL); */ - ret = readLSNEntry(h->prev_offset); + ret = LogReadLSN(h->prev_offset); set_offsets(h, ret, h->prev_offset); /*printf("B"); fflush(NULL); */ diff --git a/src/lladd/logger/logHandle.h b/src/lladd/logger/logHandle.h index 9525f1e..7e69449 100644 --- a/src/lladd/logger/logHandle.h +++ b/src/lladd/logger/logHandle.h @@ -41,8 +41,6 @@ terms specified in this license. ---*/ #include -/*#include "logEntry.h" - #include "logWriter.h" */ #ifndef __LOGHANDLE_H #define __LOGHANDLE_H diff --git a/src/lladd/logger/logWriter.h b/src/lladd/logger/logWriter.h index 5a719f1..7887778 100644 --- a/src/lladd/logger/logWriter.h +++ b/src/lladd/logger/logWriter.h @@ -70,7 +70,6 @@ terms specified in this license. #ifndef __LOGWRITER_H__ #define __LOGWRITER_H__ -/*#include "logEntry.h"*/ #include #include diff --git a/src/lladd/logger/logger2.c b/src/lladd/logger/logger2.c index f725753..3f6cf49 100644 --- a/src/lladd/logger/logger2.c +++ b/src/lladd/logger/logger2.c @@ -49,6 +49,58 @@ terms specified in this license. /*#include */ #include #include + +static int loggerType = -1; + +static void genericLogWrite(LogEntry * e) { + assert(loggerType != -1); // Otherwise, we haven't been initialized. + if(loggerType == LOG_TO_FILE) { + writeLogEntry(e); + } +} + +int LogInit(int logType) { + if(LOG_TO_FILE == logType) { + openLogWriter(); + } else { + return -1; + } + loggerType = logType; + return 0; +} + +int LogDeinit() { + assert(loggerType != -1); + if(LOG_TO_FILE == loggerType) { + closeLogWriter(); + } + return 0; +} + +void LogForce(lsn_t lsn) { + assert(loggerType != -1); + if(LOG_TO_FILE == loggerType) { + if(flushedLSN() < lsn) { + syncLog(); + } + } +} + +lsn_t LogTruncationPoint() { + assert(loggerType != -1); + if(LOG_TO_FILE == loggerType) { + return firstLogEntry(); + } + abort(); +} +LogEntry * LogReadLSN(lsn_t lsn) { + assert(loggerType != -1); + if(LOG_TO_FILE == loggerType) { + return readLSNEntry(lsn); + } + abort(); +} + TransactionLog LogTransBegin(int xid) { TransactionLog tl; tl.xid = xid; @@ -62,7 +114,8 @@ static lsn_t LogTransCommon(TransactionLog * l, int type) { LogEntry * e = allocCommonLogEntry(l->prevLSN, l->xid, type); lsn_t ret; - writeLogEntry(e); + genericLogWrite(e); + l->prevLSN = e->LSN; DEBUG("Log Common %d, LSN: %ld type: %ld (prevLSN %ld)\n", e->xid, (long int)e->LSN, (long int)e->type, (long int)e->prevLSN); @@ -76,7 +129,10 @@ static lsn_t LogTransCommon(TransactionLog * l, int type) { } extern int numActiveXactions; -lsn_t LogTransCommit(TransactionLog * l) { +/** + @todo This belongs in logWriter.c and needs a new name. +*/ +static lsn_t LogTransBundledCommit(TransactionLog * l) { static pthread_mutex_t check_commit = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t tooFewXacts = PTHREAD_COND_INITIALIZER; static int pendingCommits = 0; @@ -130,6 +186,14 @@ lsn_t LogTransCommit(TransactionLog * l) { return ret; } +lsn_t LogTransCommit(TransactionLog * l) { + assert(loggerType != -1); + if(LOG_TO_FILE == loggerType) { + return LogTransBundledCommit(l); + } + abort(); +} + lsn_t LogTransAbort(TransactionLog * l) { return LogTransCommon(l, XABORT); } @@ -165,7 +229,8 @@ LogEntry * LogUpdate(TransactionLog * l, Page * p, recordid rid, int operation, e = allocUpdateLogEntry(l->prevLSN, l->xid, operation, rid, args, argSize, preImage); - writeLogEntry(e); + // writeLogEntry(e); + genericLogWrite(e); DEBUG("Log Common %d, LSN: %ld type: %ld (prevLSN %ld) (argSize %ld)\n", e->xid, (long int)e->LSN, (long int)e->type, (long int)e->prevLSN, (long int) argSize); @@ -177,13 +242,13 @@ LogEntry * LogUpdate(TransactionLog * l, Page * p, recordid rid, int operation, return e; } -lsn_t LogCLR(LogEntry * undone) { +lsn_t LogCLR(int xid, lsn_t LSN, recordid rid, lsn_t prevLSN) { lsn_t ret; - LogEntry * e = allocCLRLogEntry(-1, undone->xid, undone->LSN, undone->contents.update.rid, undone->prevLSN); - writeLogEntry(e); + LogEntry * e = allocCLRLogEntry(-1, xid, LSN, rid, prevLSN); + genericLogWrite(e); - DEBUG("Log CLR %d, LSN: %ld type: %ld (undoing: %ld, next to undo: %ld)\n", e->xid, - (long int)e->LSN, (long int)e->type, (long int)undone->LSN, (long int)undone->prevLSN); + DEBUG("Log CLR %d, LSN: %ld (undoing: %ld, next to undo: %ld)\n", xid, + e->LSN, LSN, prevLSN); ret = e->LSN; free(e); diff --git a/src/lladd/operations.c b/src/lladd/operations.c index b2d9246..50a9692 100644 --- a/src/lladd/operations.c +++ b/src/lladd/operations.c @@ -41,7 +41,7 @@ terms specified in this license. ---*/ #include -#include "logger/logWriter.h" +#include #include #include #include @@ -85,7 +85,7 @@ void redoUpdate(const LogEntry * e) { releasePage(p); } else if(e->type == CLRLOG) { - LogEntry * f = readLSNEntry(e->contents.clr.thisUpdateLSN); + LogEntry * f = LogReadLSN(e->contents.clr.thisUpdateLSN); recordid rid = f->contents.update.rid; Page * p = NULL; @@ -99,7 +99,7 @@ void redoUpdate(const LogEntry * e) { /* See if the page contains the result of the undo that this CLR is supposed to perform. If it doesn't, then undo the original operation. */ - /* if(f->LSN > pageReadLSN(e->contents.update.rid.page)) { */ + if(isNullRid || f->LSN > pageReadLSN(p)) { DEBUG("OPERATION Undoing for clr, %ld {%d %d %ld}\n", f->LSN, rid.page, rid.slot, rid.size); diff --git a/src/lladd/operations/nestedTopActions.c b/src/lladd/operations/nestedTopActions.c index a0c329a..5d2fb5c 100644 --- a/src/lladd/operations/nestedTopActions.c +++ b/src/lladd/operations/nestedTopActions.c @@ -48,7 +48,6 @@ terms specified in this license. #include #include #include -#include "../logger/logWriter.h" #include #include #include @@ -114,19 +113,16 @@ lsn_t TendNestedTopAction(int xid, void * handle) { recordid undoneRID = NULLRID; // Not correct, but this field is unused anyway. ;) // Write a CLR. - LogEntry * e = allocCLRLogEntry(-1, xid, undoneLSN, undoneRID, *prevLSN); - writeLogEntry(e); + lsn_t clrLSN = LogCLR(xid, undoneLSN, undoneRID, *prevLSN); // Ensure that the next action in this transaction points to the CLR. - XactionTable[xid % MAX_TRANSACTIONS].prevLSN = e->LSN; + XactionTable[xid % MAX_TRANSACTIONS].prevLSN = clrLSN; DEBUG("NestedTopAction CLR %d, LSN: %ld type: %ld (undoing: %ld, next to undo: %ld)\n", e->xid, - (long int)e->LSN, (long int)e->type, (long int)undone->LSN, (long int)undone->prevLSN); + clrLSN, undoneLSN, *prevLSN); - lsn_t ret = e->LSN; - free(e); free(prevLSN); pthread_mutex_unlock(&transactional_2_mutex); - return ret; + return clrLSN; } diff --git a/src/lladd/operations/prepare.c b/src/lladd/operations/prepare.c index bab67d5..0b132ef 100644 --- a/src/lladd/operations/prepare.c +++ b/src/lladd/operations/prepare.c @@ -47,13 +47,13 @@ terms specified in this license. **********************************************/ #include -#include "../logger/logWriter.h" +#include #include #include recordid prepare_bogus_rec = { 0, 0, 0}; static int operate(int xid, Page * p, lsn_t lsn, recordid rid, const void *dat) { - syncLog(); + LogForce(lsn); return 0; } diff --git a/src/lladd/pageFile.c b/src/lladd/pageFile.c index 8f263d7..b16eded 100644 --- a/src/lladd/pageFile.c +++ b/src/lladd/pageFile.c @@ -8,7 +8,7 @@ #include "pageFile.h" #include -#include "logger/logWriter.h" +#include #include #include @@ -85,10 +85,8 @@ void pageWrite(Page * ret) { /* assert(ret->pending == 0); */ - if(flushedLSN() < pageReadLSN(ret)) { - DEBUG("pageWrite is calling syncLog()!\n"); - syncLog(); - } + // If necessary, force the log to disk so that ret's LSN will be stable. + LogForce(pageReadLSN(ret)); pthread_mutex_lock(&stable_mutex); diff --git a/src/lladd/recovery2.c b/src/lladd/recovery2.c index 2ddbf0b..4bc03c6 100644 --- a/src/lladd/recovery2.c +++ b/src/lladd/recovery2.c @@ -15,7 +15,6 @@ #include #include "linkedlist.h" #include "logger/logHandle.h" -#include "logger/logWriter.h" #include #include @@ -230,7 +229,7 @@ static void Undo(int recovery) { /* Need to log a clr here. */ - clr_lsn = LogCLR(e); + clr_lsn = LogCLR(e->xid, e->LSN, e->contents.update.rid, e->prevLSN); /* Undo update is a no-op if the page does not reflect this update, but it will write the new clr_lsn if necessary. */ @@ -242,7 +241,7 @@ static void Undo(int recovery) { } else { // The log entry is not associated with a particular page. // (Therefore, it must be an idempotent logical log entry.) - clr_lsn = LogCLR(e); + clr_lsn = LogCLR(e->xid, e->LSN, e->contents.update.rid, e->prevLSN); undoUpdate(e, NULL, clr_lsn); } break; diff --git a/src/lladd/transactional2.c b/src/lladd/transactional2.c index 5d01f2c..0e874db 100644 --- a/src/lladd/transactional2.c +++ b/src/lladd/transactional2.c @@ -4,7 +4,6 @@ #include #include -#include "logger/logWriter.h" #include #include #include @@ -100,7 +99,7 @@ int Tinit() { bufInit(); - openLogWriter(); + LogInit(LOG_TO_FILE); try_ret(compensation_error()) { pageOperationsInit(); @@ -354,7 +353,7 @@ int Tdeinit() { assert( numActiveXactions == 0 ); ThashDeinit(); bufDeinit(); - closeLogWriter(); + LogDeinit(); return 0; }