2004-07-06 01:22:18 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include <lladd/common.h>
|
2004-07-20 03:40:57 +00:00
|
|
|
#include "latches.h"
|
2004-06-24 21:10:31 +00:00
|
|
|
#include <lladd/transactional.h>
|
2004-07-06 01:22:18 +00:00
|
|
|
|
2004-06-28 22:48:02 +00:00
|
|
|
#include <lladd/recovery.h>
|
2004-07-14 21:25:59 +00:00
|
|
|
#include "logger/logWriter.h"
|
2004-07-06 01:22:18 +00:00
|
|
|
#include <lladd/bufferManager.h>
|
|
|
|
#include <lladd/logger/logger2.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
TransactionLog XactionTable[MAX_TRANSACTIONS];
|
|
|
|
int numActiveXactions = 0;
|
|
|
|
int xidCount = 0;
|
2004-07-20 03:40:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Locking for transactional2.c works as follows:
|
|
|
|
|
|
|
|
numActiveXactions, xidCount are protected, XactionTable is not.
|
|
|
|
This implies that we do not support multi-threaded transactions,
|
|
|
|
at least for now.
|
|
|
|
*/
|
|
|
|
pthread_mutex_t transactional_2_mutex;
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
#define INVALID_XTABLE_XID -1
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
/** Needed for debugging -- sometimes we don't want to run all of Tinit() */
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
void setupOperationsTable() {
|
2004-06-24 21:10:31 +00:00
|
|
|
memset(XactionTable, INVALID_XTABLE_XID, sizeof(TransactionLog)*MAX_TRANSACTIONS);
|
|
|
|
operationsTable[OPERATION_SET] = getSet();
|
|
|
|
operationsTable[OPERATION_INCREMENT] = getIncrement();
|
|
|
|
operationsTable[OPERATION_DECREMENT] = getDecrement();
|
|
|
|
operationsTable[OPERATION_PREPARE] = getPrepare();
|
2004-07-06 01:22:18 +00:00
|
|
|
/* operationsTable[OPERATION_LHINSERT] = getLHInsert();
|
|
|
|
operationsTable[OPERATION_LHREMOVE] = getLHRemove(); */
|
2004-06-24 21:10:31 +00:00
|
|
|
operationsTable[OPERATION_ALLOC] = getAlloc();
|
|
|
|
operationsTable[OPERATION_DEALLOC] = getDealloc();
|
2004-06-30 01:09:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tinit() {
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_init(&transactional_2_mutex, NULL);
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
setupOperationsTable();
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
/* pageInit(); */
|
2004-06-24 21:10:31 +00:00
|
|
|
bufInit();
|
|
|
|
|
|
|
|
openLogWriter();
|
|
|
|
|
|
|
|
InitiateRecovery();
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Tbegin() {
|
|
|
|
|
|
|
|
int i, index = 0;
|
2004-07-20 03:40:57 +00:00
|
|
|
int xidCount_tmp;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
if( numActiveXactions == MAX_TRANSACTIONS )
|
|
|
|
return EXCEED_MAX_TRANSACTIONS;
|
|
|
|
else
|
|
|
|
numActiveXactions++;
|
|
|
|
|
|
|
|
for( i = 0; i < MAX_TRANSACTIONS; i++ ) {
|
|
|
|
xidCount++;
|
|
|
|
if( XactionTable[xidCount%MAX_TRANSACTIONS].xid == INVALID_XTABLE_XID ) {
|
|
|
|
index = xidCount%MAX_TRANSACTIONS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
xidCount_tmp = xidCount;
|
|
|
|
/* Don't want to block while we're logging... */
|
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
assert( i < MAX_TRANSACTIONS );
|
|
|
|
|
|
|
|
XactionTable[index] = LogTransBegin(xidCount_tmp);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
return XactionTable[index].xid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tupdate(int xid, recordid rid, const void *dat, int op) {
|
|
|
|
LogEntry * e;
|
2004-07-20 03:40:57 +00:00
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
assert(numActiveXactions <= MAX_TRANSACTIONS);
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
|
|
|
#endif
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
e = LogUpdate(&XactionTable[xid % MAX_TRANSACTIONS], rid, op, dat);
|
|
|
|
|
|
|
|
assert(XactionTable[xid % MAX_TRANSACTIONS].prevLSN == e->LSN);
|
|
|
|
|
2004-06-28 22:48:02 +00:00
|
|
|
DEBUG("Tupdate() e->LSN: %ld\n", e->LSN);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
doUpdate(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @todo what about locking? */
|
|
|
|
void Tread(int xid, recordid rid, void * dat) {
|
|
|
|
readRecord(xid, rid, dat);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tcommit(int xid) {
|
2004-06-28 21:10:10 +00:00
|
|
|
lsn_t lsn;
|
2004-07-20 03:40:57 +00:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
assert(numActiveXactions <= MAX_TRANSACTIONS);
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
|
|
|
#endif
|
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
lsn = LogTransCommit(&XactionTable[xid % MAX_TRANSACTIONS]);
|
|
|
|
bufTransCommit(xid, lsn); /* unlocks pages */
|
2004-06-24 21:10:31 +00:00
|
|
|
XactionTable[xid%MAX_TRANSACTIONS].xid = INVALID_XTABLE_XID;
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
numActiveXactions--;
|
|
|
|
assert( numActiveXactions >= 0 );
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tabort(int xid) {
|
2004-06-28 21:10:10 +00:00
|
|
|
lsn_t lsn;
|
|
|
|
lsn = LogTransAbort(&XactionTable[xid%MAX_TRANSACTIONS]);
|
|
|
|
|
|
|
|
/* @todo is the order of the next two calls important? */
|
2004-06-24 21:10:31 +00:00
|
|
|
undoTrans(XactionTable[xid%MAX_TRANSACTIONS]);
|
2004-06-28 21:10:10 +00:00
|
|
|
bufTransAbort(xid, lsn);
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
XactionTable[xid%MAX_TRANSACTIONS].xid = INVALID_XTABLE_XID;
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
|
|
|
numActiveXactions--;
|
2004-06-24 21:10:31 +00:00
|
|
|
assert( numActiveXactions >= 0 );
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Tdeinit() {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < MAX_TRANSACTIONS; i++ ) {
|
|
|
|
if( XactionTable[i].xid != INVALID_XTABLE_XID ) {
|
|
|
|
Tabort(XactionTable[i].xid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert( numActiveXactions == 0 );
|
|
|
|
|
|
|
|
bufDeinit();
|
|
|
|
closeLogWriter();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trevive(int xid, long lsn) {
|
|
|
|
int index = xid % MAX_TRANSACTIONS;
|
|
|
|
if(XactionTable[index].xid != INVALID_XTABLE_XID) {
|
|
|
|
if(xid != XactionTable[index].xid) {
|
|
|
|
printf("Clashing Tprepare()'ed XID's encountered on recovery!!\n");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
assert(XactionTable[index].xid == xid);
|
|
|
|
assert(XactionTable[index].prevLSN == lsn);
|
|
|
|
} else {
|
|
|
|
XactionTable[index].xid = xid;
|
|
|
|
XactionTable[index].prevLSN = lsn;
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
numActiveXactions++;
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TsetXIDCount(int xid) {
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_lock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
xidCount = xid;
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&transactional_2_mutex);
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|