2004-06-24 21:10:31 +00:00
|
|
|
/**
|
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
@file Implements three phase recovery
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-07-06 01:22:18 +00:00
|
|
|
#include <config.h>
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/common.h>
|
2007-04-03 09:18:45 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2004-07-06 01:22:18 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
#include <pbl/pbl.h>
|
2007-04-03 09:18:45 +00:00
|
|
|
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/recovery.h>
|
|
|
|
#include <stasis/bufferManager.h>
|
|
|
|
#include <stasis/lockManager.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
/** @todo Add better log iterator guard support and remove this include.*/
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/operations/prepare.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
#include "logger/logHandle.h"
|
|
|
|
/** @todo Get rid of linkedlist.[ch] */
|
|
|
|
#include "linkedlist.h"
|
|
|
|
#include "page.h" // Needed for pageReadLSN.
|
2004-07-06 01:22:18 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
static pblHashTable_t * transactionLSN;
|
2006-10-28 03:31:27 +00:00
|
|
|
static LinkedList * rollbackLSNs = NULL;
|
2006-08-08 01:41:45 +00:00
|
|
|
/** @todo There is no real reason to have this mutex (which prevents
|
|
|
|
concurrent aborts, except that we need to protect rollbackLSNs's
|
|
|
|
from concurrent modifications. */
|
|
|
|
static pthread_mutex_t rollback_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
/**
|
|
|
|
Determines which transactions committed, and which need to be redone.
|
|
|
|
|
|
|
|
In the original version, this function also:
|
|
|
|
- Determined the point in the log at which to start the Redo pass.
|
|
|
|
- Calculated a list of all dirty pages.
|
|
|
|
|
|
|
|
It no longer does either of these things:
|
|
|
|
- A checkpointing algorithm could figure out where the redo pass
|
|
|
|
should begin. (It would then truncate the log at that point.) This
|
|
|
|
function could be called before analysis if efficiency is a concern.
|
|
|
|
- We were using the list of dirty pages as an optimization to prevent
|
|
|
|
the pages from being read later during recovery. Since this function
|
|
|
|
no longer reads the pages in, there's no longer any reason to build
|
|
|
|
the list of dirty pages.
|
|
|
|
*/
|
|
|
|
static void Analysis () {
|
|
|
|
|
2006-04-11 02:20:21 +00:00
|
|
|
const LogEntry * e;
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
LogHandle lh = getLogHandle();
|
|
|
|
|
|
|
|
/** After recovery, we need to know what the highest XID in the
|
|
|
|
log was so that we don't accidentally reuse XID's. This keeps
|
|
|
|
track of that value. */
|
|
|
|
int highestXid = 0;
|
|
|
|
|
|
|
|
/** @todo loadCheckPoint() - Jump forward in the log to the last
|
2007-04-03 09:18:45 +00:00
|
|
|
checkpoint. (getLogHandle should do this automatically,
|
2004-06-24 21:10:31 +00:00
|
|
|
since the log will be truncated on checkpoint anyway.) */
|
|
|
|
|
|
|
|
while((e = nextInLog(&lh))) {
|
|
|
|
|
|
|
|
lsn_t * xactLSN = (lsn_t*)pblHtLookup(transactionLSN, &(e->xid), sizeof(int));
|
|
|
|
|
|
|
|
if(highestXid < e->xid) {
|
|
|
|
highestXid = e->xid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Track LSN's in two data structures:
|
|
|
|
- map: xid -> max LSN
|
|
|
|
- sorted list of maxLSN's
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(xactLSN == NULL) {
|
|
|
|
xactLSN = malloc(sizeof(lsn_t));
|
|
|
|
pblHtInsert(transactionLSN, &(e->xid), sizeof(int), xactLSN);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* We've seen this xact before, and must have put a value in
|
|
|
|
rollbackLSNs for it. That value is now stale, so remove
|
|
|
|
it. */
|
|
|
|
|
|
|
|
DEBUG("Removing %ld\n", *xactLSN);
|
|
|
|
removeVal(&rollbackLSNs, *xactLSN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, rollbackLSNs certainly does not contain an LSN for this
|
|
|
|
transaction, and *xactLSN points to a value in the hash, so
|
|
|
|
writing to it updates the hash. This doesn't update the
|
|
|
|
rollbackLSN data structure, so it doesn't hurt to update this
|
|
|
|
value for all log entries. */
|
|
|
|
|
|
|
|
*xactLSN = e->LSN;
|
|
|
|
|
|
|
|
switch(e->type) {
|
|
|
|
case XCOMMIT:
|
|
|
|
/* We've removed this XACT's last LSN from the list of LSN's to
|
|
|
|
be rolled back, so we're done. */
|
|
|
|
break;
|
|
|
|
case XEND:
|
|
|
|
/*
|
|
|
|
XEND means this transaction reached stable storage.
|
|
|
|
Therefore, we can skip redoing any of its operations. (The
|
|
|
|
timestamps on each page guarantee that the redo phase will
|
|
|
|
not overwrite this transaction's work with stale data.)
|
2006-09-27 20:28:44 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
The redo phase checks for a transaction's presence in
|
2004-06-28 22:48:02 +00:00
|
|
|
transactionLSN before redoing its actions. Therefore, if we
|
|
|
|
remove this transaction from the hash, it will not be redone.
|
2004-06-24 21:10:31 +00:00
|
|
|
*/
|
|
|
|
pblHtRemove(transactionLSN, &(e->xid), sizeof(int));
|
|
|
|
break;
|
|
|
|
case UPDATELOG:
|
|
|
|
case CLRLOG:
|
|
|
|
/*
|
|
|
|
If the last record we see for a transaction is an update or clr,
|
|
|
|
then the transaction must not have committed, so it must need
|
|
|
|
to be rolled back.
|
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
Add it to the list
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
DEBUG("Adding %ld\n", e->LSN);
|
|
|
|
|
|
|
|
addSortedVal(&rollbackLSNs, e->LSN);
|
|
|
|
break;
|
|
|
|
case XABORT:
|
2007-04-03 09:18:45 +00:00
|
|
|
// If the last record we see for a transaction is an abort, then
|
|
|
|
// the transaction didn't commit, and must be rolled back.
|
2007-03-30 09:16:21 +00:00
|
|
|
DEBUG("Adding %ld\n", e->LSN);
|
|
|
|
addSortedVal(&rollbackLSNs, e->LSN);
|
2006-10-04 04:41:19 +00:00
|
|
|
break;
|
|
|
|
case INTERNALLOG:
|
2007-04-03 09:18:45 +00:00
|
|
|
// Created by the logger, just ignore it
|
2007-03-30 09:16:21 +00:00
|
|
|
// Make sure the log entry doesn't interfere with real xacts.
|
|
|
|
assert(e->xid == INVALID_XID);
|
2006-10-04 04:41:19 +00:00
|
|
|
break;
|
2004-06-24 21:10:31 +00:00
|
|
|
default:
|
2006-10-04 04:41:19 +00:00
|
|
|
abort();
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2006-04-11 02:20:21 +00:00
|
|
|
FreeLogEntry(e);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
TsetXIDCount(highestXid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Redo() {
|
|
|
|
LogHandle lh = getLogHandle();
|
2006-04-11 02:20:21 +00:00
|
|
|
const LogEntry * e;
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
while((e = nextInLog(&lh))) {
|
2007-04-03 09:18:45 +00:00
|
|
|
// Is this log entry part of a transaction that needs to be redone?
|
2004-06-24 21:10:31 +00:00
|
|
|
if(pblHtLookup(transactionLSN, &(e->xid), sizeof(int)) != NULL) {
|
2007-03-30 09:16:21 +00:00
|
|
|
// Check to see if this entry's action needs to be redone
|
|
|
|
switch(e->type) {
|
|
|
|
case UPDATELOG:
|
|
|
|
case CLRLOG:
|
|
|
|
{
|
|
|
|
// redoUpdate checks the page that contains e->rid, so we
|
|
|
|
// don't need to check to see if the page is newer than this
|
|
|
|
// log entry.
|
|
|
|
redoUpdate(e);
|
|
|
|
FreeLogEntry(e);
|
|
|
|
} break;
|
|
|
|
case DEFERLOG:
|
|
|
|
{
|
2007-04-03 09:18:45 +00:00
|
|
|
// XXX deferred_push(e);
|
2007-03-30 09:16:21 +00:00
|
|
|
} break;
|
|
|
|
case XCOMMIT:
|
|
|
|
{
|
|
|
|
if(globalLockManager.commit)
|
|
|
|
globalLockManager.commit(e->xid);
|
|
|
|
FreeLogEntry(e);
|
|
|
|
} break;
|
|
|
|
case XABORT:
|
|
|
|
{
|
|
|
|
// wait until undo is complete before informing the lock manager
|
|
|
|
FreeLogEntry(e);
|
|
|
|
} break;
|
|
|
|
case INTERNALLOG:
|
|
|
|
{
|
|
|
|
FreeLogEntry(e);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-03 09:18:45 +00:00
|
|
|
/**
|
|
|
|
@todo Guards shouldn't be hardcoded in Undo()
|
2007-03-30 09:16:21 +00:00
|
|
|
*/
|
2004-06-24 21:10:31 +00:00
|
|
|
static void Undo(int recovery) {
|
|
|
|
LogHandle lh;
|
|
|
|
void * prepare_guard_state;
|
|
|
|
|
|
|
|
while(rollbackLSNs != NULL) {
|
2006-04-11 02:20:21 +00:00
|
|
|
const LogEntry * e;
|
2004-06-24 21:10:31 +00:00
|
|
|
lsn_t rollback = popMaxVal(&rollbackLSNs);
|
|
|
|
|
2004-10-02 07:29:34 +00:00
|
|
|
prepare_guard_state = getPrepareGuardState();
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
DEBUG("Undoing LSN %ld\n", (long int)rollback);
|
2007-04-03 09:18:45 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
if(recovery) {
|
|
|
|
lh = getGuardedHandle(rollback, &prepareGuard, prepare_guard_state);
|
|
|
|
} else {
|
|
|
|
lh = getLSNHandle(rollback);
|
|
|
|
}
|
2004-07-04 00:46:49 +00:00
|
|
|
|
2005-02-10 21:56:32 +00:00
|
|
|
int thisXid = -1;
|
2004-06-24 21:10:31 +00:00
|
|
|
while((e = previousInTransaction(&lh))) {
|
2005-02-10 21:56:32 +00:00
|
|
|
thisXid = e->xid;
|
2004-06-24 21:10:31 +00:00
|
|
|
lsn_t this_lsn, clr_lsn;
|
|
|
|
switch(e->type) {
|
|
|
|
case UPDATELOG:
|
2004-07-23 20:21:44 +00:00
|
|
|
{
|
2007-04-03 09:18:45 +00:00
|
|
|
// If the rid is valid, load the page for undoUpdate.
|
|
|
|
// undoUpdate checks the LSN before applying physical undos
|
|
|
|
|
|
|
|
Page * p = NULL;
|
|
|
|
if(e->update.rid.size != -1) {
|
|
|
|
p = loadPage(thisXid, e->update.rid.page);
|
2005-01-14 10:08:10 +00:00
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
// If this fails, something is wrong with redo or normal operation.
|
2007-10-02 00:18:33 +00:00
|
|
|
this_lsn = stasis_page_lsn_read(p);
|
2005-01-14 10:08:10 +00:00
|
|
|
assert(e->LSN <= this_lsn);
|
2006-09-27 20:28:44 +00:00
|
|
|
|
2007-04-03 09:18:45 +00:00
|
|
|
} else {
|
2005-01-14 10:08:10 +00:00
|
|
|
// The log entry is not associated with a particular page.
|
|
|
|
// (Therefore, it must be an idempotent logical log entry.)
|
|
|
|
}
|
2007-04-03 09:18:45 +00:00
|
|
|
|
|
|
|
clr_lsn = LogCLR(e);
|
|
|
|
undoUpdate(e, p, clr_lsn);
|
|
|
|
|
|
|
|
if(p) {
|
|
|
|
releasePage(p);
|
|
|
|
}
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-03-30 09:16:21 +00:00
|
|
|
case DEFERLOG:
|
|
|
|
// The transaction is aborting, so it never committed. Therefore
|
|
|
|
// actions deferred to commit have never been applied; ignore this
|
|
|
|
// log entry.
|
2007-04-03 09:18:45 +00:00
|
|
|
break;
|
|
|
|
case CLRLOG:
|
|
|
|
// Don't undo CLRs; they were undone during Redo
|
|
|
|
break;
|
2004-06-28 21:10:10 +00:00
|
|
|
case XABORT:
|
2007-04-03 09:18:45 +00:00
|
|
|
// Since XABORT is a no-op, we can silentlt ignore it. XABORT
|
|
|
|
// records may be passed in by undoTrans.
|
|
|
|
break;
|
|
|
|
case XCOMMIT:
|
|
|
|
// Should never abort a transaction that contains a commit record
|
|
|
|
abort();
|
2004-06-24 21:10:31 +00:00
|
|
|
default:
|
2007-03-30 09:20:03 +00:00
|
|
|
printf
|
2007-03-30 09:16:21 +00:00
|
|
|
("Unknown log type to undo (TYPE=%d,XID= %d,LSN=%lld), skipping...\n",
|
|
|
|
e->type, e->xid, e->LSN);
|
2007-04-03 09:18:45 +00:00
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2006-04-11 02:20:21 +00:00
|
|
|
FreeLogEntry(e);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2005-02-10 21:56:32 +00:00
|
|
|
int transactionWasPrepared = prepareAction(prepare_guard_state);
|
2004-10-02 07:29:34 +00:00
|
|
|
free(prepare_guard_state);
|
2005-02-10 21:56:32 +00:00
|
|
|
if(!transactionWasPrepared && globalLockManager.abort) {
|
|
|
|
globalLockManager.abort(thisXid);
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitiateRecovery() {
|
|
|
|
|
|
|
|
transactionLSN = pblHtCreate();
|
|
|
|
DEBUG("Analysis started\n");
|
|
|
|
Analysis();
|
|
|
|
DEBUG("Redo started\n");
|
|
|
|
Redo();
|
|
|
|
DEBUG("Undo started\n");
|
2008-02-29 18:03:32 +00:00
|
|
|
TallocPostInit();
|
2004-06-24 21:10:31 +00:00
|
|
|
Undo(1);
|
|
|
|
DEBUG("Recovery complete.\n");
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
pblHtDelete(transactionLSN);
|
|
|
|
|
2006-10-28 03:31:27 +00:00
|
|
|
destroyList(&rollbackLSNs);
|
|
|
|
assert(rollbackLSNs==0);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void undoTrans(TransactionLog transaction) {
|
2006-08-08 01:41:45 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&rollback_mutex);
|
|
|
|
assert(!rollbackLSNs);
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
if(transaction.prevLSN > 0) {
|
2004-06-28 22:48:02 +00:00
|
|
|
DEBUG("scheduling lsn %ld for undo.\n", transaction.prevLSN);
|
2004-06-24 21:10:31 +00:00
|
|
|
addSortedVal(&rollbackLSNs, transaction.prevLSN);
|
|
|
|
} else {
|
2004-06-28 22:48:02 +00:00
|
|
|
/* Nothing to undo. (Happens for read-only xacts.) */
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Undo(0);
|
2006-08-08 01:41:45 +00:00
|
|
|
if(rollbackLSNs) {
|
2006-10-28 03:31:27 +00:00
|
|
|
destroyList(&rollbackLSNs);
|
2006-08-08 01:41:45 +00:00
|
|
|
}
|
2006-10-28 03:31:27 +00:00
|
|
|
assert(rollbackLSNs == 0);
|
2006-08-08 01:41:45 +00:00
|
|
|
pthread_mutex_unlock(&rollback_mutex);
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|