2004-06-24 21:10:31 +00:00
|
|
|
/*---
|
|
|
|
This software is copyrighted by the Regents of the University of
|
|
|
|
California, and other parties. The following terms apply to all files
|
|
|
|
associated with the software unless explicitly disclaimed in
|
|
|
|
individual files.
|
|
|
|
|
|
|
|
The authors hereby grant permission to use, copy, modify, distribute,
|
|
|
|
and license this software and its documentation for any purpose,
|
|
|
|
provided that existing copyright notices are retained in all copies
|
|
|
|
and that this notice is included verbatim in any distributions. No
|
|
|
|
written agreement, license, or royalty fee is required for any of the
|
|
|
|
authorized uses. Modifications to this software may be copyrighted by
|
|
|
|
their authors and need not follow the licensing terms described here,
|
|
|
|
provided that the new terms are clearly indicated on the first page of
|
|
|
|
each file where they apply.
|
|
|
|
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
|
|
|
|
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
|
|
|
|
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
|
|
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
|
|
NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND
|
|
|
|
THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
|
|
|
|
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
|
|
|
|
GOVERNMENT USE: If you are acquiring this software on behalf of the
|
|
|
|
U.S. government, the Government shall have only "Restricted Rights" in
|
|
|
|
the software and related documentation as defined in the Federal
|
|
|
|
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are
|
|
|
|
acquiring the software on behalf of the Department of Defense, the
|
|
|
|
software shall be classified as "Commercial Computer Software" and the
|
|
|
|
Government shall have only "Restricted Rights" as defined in Clause
|
|
|
|
252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
|
|
|
|
authors grant the U.S. Government and others acting in its behalf
|
|
|
|
permission to use and distribute the software in accordance with the
|
|
|
|
terms specified in this license.
|
|
|
|
---*/
|
|
|
|
/*******************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* implementation of the page buffer
|
|
|
|
* *************************************************/
|
2004-07-06 01:22:18 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <lladd/common.h>
|
2004-07-20 00:15:17 +00:00
|
|
|
#include <latches.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <lladd/bufferManager.h>
|
2004-06-25 18:56:43 +00:00
|
|
|
#include "blobManager.h"
|
2004-06-30 01:09:57 +00:00
|
|
|
#include <lladd/pageCache.h>
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-14 21:25:59 +00:00
|
|
|
#include "page.h"
|
2004-07-15 00:42:36 +00:00
|
|
|
#include "pageFile.h"
|
2004-07-14 20:49:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Invariant: This lock should be held while updating lastFreepage, or
|
|
|
|
while performing any operation that may decrease the amount of
|
|
|
|
freespace in the page that lastFreepage refers to.
|
|
|
|
|
|
|
|
Since pageCompact and pageDeRalloc may only increase this value,
|
|
|
|
they do not need to hold this lock. Since bufferManager is the
|
|
|
|
only place where pageRalloc is called, pageRalloc does not obtain
|
|
|
|
this lock.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static pthread_mutex_t lastFreepage_mutex;
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_t add_pending_mutex;
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
static unsigned int lastFreepage = 0;
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
/**
|
|
|
|
* @param pageid ID of the page you want to load
|
|
|
|
* @return fully formed Page type
|
|
|
|
* @return page with -1 ID if page not found
|
|
|
|
*/
|
2004-07-20 00:15:17 +00:00
|
|
|
Page * loadPage(int pageid);
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_cond_t addPendingOK;
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
int bufInit() {
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
/* stable = NULL; */
|
2004-07-14 20:49:18 +00:00
|
|
|
pageInit();
|
2004-06-30 01:09:57 +00:00
|
|
|
openPageFile();
|
|
|
|
pageCacheInit();
|
2004-06-25 18:56:43 +00:00
|
|
|
openBlobStore();
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
lastFreepage = 0;
|
|
|
|
pthread_mutex_init(&lastFreepage_mutex , NULL);
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_cond_init(&addPendingOK, NULL);
|
|
|
|
pthread_mutex_init(&add_pending_mutex, NULL);
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
void bufDeinit() {
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
closeBlobStore();
|
|
|
|
pageCacheDeinit();
|
|
|
|
closePageFile();
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
return;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-06-30 01:09:57 +00:00
|
|
|
/**
|
|
|
|
Just close file descriptors, don't do any other clean up. (For
|
|
|
|
testing.)
|
2004-06-28 22:48:02 +00:00
|
|
|
*/
|
2004-06-30 01:09:57 +00:00
|
|
|
void simulateBufferManagerCrash() {
|
|
|
|
closeBlobStore();
|
|
|
|
closePageFile();
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
/* ** No file I/O below this line. ** */
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
Page * loadPage (int pageid) {
|
2004-07-20 00:15:17 +00:00
|
|
|
Page * p = loadPagePtr(pageid);
|
|
|
|
assert (p->id == pageid);
|
|
|
|
return p;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Page * lastRallocPage = 0;
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
/** @todo ralloc ignores it's xid parameter; change the interface? */
|
2004-07-06 01:22:18 +00:00
|
|
|
recordid ralloc(int xid, /*lsn_t lsn,*/ long size) {
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
recordid ret;
|
2004-07-14 20:49:18 +00:00
|
|
|
Page * p;
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
/* DEBUG("Rallocing record of size %ld\n", (long int)size); */
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-06-30 01:09:57 +00:00
|
|
|
assert(size < BLOB_THRESHOLD_SIZE || size == BLOB_SLOT);
|
2004-06-28 21:10:10 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
pthread_mutex_lock(&lastFreepage_mutex);
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
while(freespace(p = loadPage(lastFreepage)) < size ) { unlock(p->loadlatch); lastFreepage++; }
|
2004-07-09 22:05:33 +00:00
|
|
|
|
|
|
|
ret = pageRalloc(p, size);
|
2004-06-28 22:48:02 +00:00
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
unlock(p->loadlatch);
|
2004-07-14 20:49:18 +00:00
|
|
|
pthread_mutex_unlock(&lastFreepage_mutex);
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
/* DEBUG("alloced rid = {%d, %d, %ld}\n", ret.page, ret.slot, ret.size); */
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
return ret;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
void slotRalloc(int pageid, lsn_t lsn, recordid rid) {
|
|
|
|
Page * loadedPage = loadPage(rid.page);
|
|
|
|
pageSlotRalloc(loadedPage, lsn, rid);
|
2004-07-20 00:15:17 +00:00
|
|
|
unlock(loadedPage->loadlatch);
|
2004-07-14 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
long readLSN(int pageid) {
|
2004-07-20 00:15:17 +00:00
|
|
|
Page *p;
|
|
|
|
lsn_t lsn = pageReadLSN(p = loadPage(pageid));
|
|
|
|
unlock(p->loadlatch);
|
|
|
|
return lsn;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
void writeRecord(int xid, lsn_t lsn, recordid rid, const void *dat) {
|
|
|
|
|
2004-07-09 22:05:33 +00:00
|
|
|
Page *p;
|
|
|
|
|
|
|
|
if(rid.size > BLOB_THRESHOLD_SIZE) {
|
2004-07-20 00:15:17 +00:00
|
|
|
/* DEBUG("Writing blob.\n"); */
|
2004-07-09 22:05:33 +00:00
|
|
|
writeBlob(xid, lsn, rid, dat);
|
|
|
|
|
|
|
|
} else {
|
2004-07-20 00:15:17 +00:00
|
|
|
/* DEBUG("Writing record.\n"); */
|
|
|
|
p = loadPage(rid.page); /* loadPagePtr(rid.page); */
|
2004-07-09 22:05:33 +00:00
|
|
|
assert( (p->id == rid.page) && (p->memAddr != NULL) );
|
|
|
|
/** @todo This assert should be here, but the tests are broken, so it causes bogus failures. */
|
|
|
|
/*assert(pageReadLSN(*p) <= lsn);*/
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
pageWriteRecord(xid, p, rid, lsn, dat);
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
unlock(p->loadlatch);
|
|
|
|
|
2004-07-09 22:05:33 +00:00
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
2004-07-09 22:05:33 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
void readRecord(int xid, recordid rid, void *buf) {
|
2004-06-28 21:10:10 +00:00
|
|
|
if(rid.size > BLOB_THRESHOLD_SIZE) {
|
2004-07-20 00:15:17 +00:00
|
|
|
/* DEBUG("Reading blob. xid = %d rid = { %d %d %ld } buf = %x\n",
|
|
|
|
xid, rid.page, rid.slot, rid.size, (unsigned int)buf); */
|
2004-06-28 21:10:10 +00:00
|
|
|
readBlob(xid, rid, buf);
|
|
|
|
} else {
|
2004-07-20 00:15:17 +00:00
|
|
|
Page * p = loadPage(rid.page);
|
|
|
|
assert(rid.page == p->id);
|
|
|
|
/* DEBUG("Reading record xid = %d rid = { %d %d %ld } buf = %x\n",
|
|
|
|
xid, rid.page, rid.slot, rid.size, (unsigned int)buf); */
|
|
|
|
pageReadRecord(xid, p, rid, buf);
|
|
|
|
assert(rid.page == p->id);
|
|
|
|
unlock(p->loadlatch);
|
2004-06-28 21:10:10 +00:00
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
int bufTransCommit(int xid, lsn_t lsn) {
|
|
|
|
|
|
|
|
commitBlobs(xid);
|
|
|
|
pageCommit(xid);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
return 0;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
int bufTransAbort(int xid, lsn_t lsn) {
|
2004-06-30 01:09:57 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
abortBlobs(xid); /* abortBlobs doesn't write any log entries, so it doesn't need the lsn. */
|
|
|
|
pageAbort(xid);
|
2004-06-24 21:10:31 +00:00
|
|
|
|
2004-06-28 21:10:10 +00:00
|
|
|
return 0;
|
2004-06-24 21:10:31 +00:00
|
|
|
}
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
void setSlotType(int pageid, int slot, int type) {
|
|
|
|
Page * p = loadPage(pageid);
|
|
|
|
pageSetSlotType(p, slot, type);
|
2004-07-20 00:15:17 +00:00
|
|
|
unlock(p->loadlatch);
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Inform bufferManager that a new event (such as an update) will be
|
|
|
|
performed on page pageid. This function may not be called on a
|
|
|
|
page after finalize() has been called on that page, and each call
|
|
|
|
to this function must be followed by a corresponding call to
|
|
|
|
removePendingEvent.
|
|
|
|
|
|
|
|
This function is called by the logger when CLR or UPDATE records
|
|
|
|
are written.
|
|
|
|
|
|
|
|
@see finalize, removePendingEvent
|
|
|
|
|
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
void addPendingEvent(int pageid){
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
Page * p;
|
|
|
|
|
|
|
|
p = loadPage(pageid);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&add_pending_mutex);
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
while(p->waiting) {
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&add_pending_mutex);
|
|
|
|
|
|
|
|
unlock(p->loadlatch);
|
|
|
|
DEBUG("B");
|
|
|
|
pthread_mutex_lock(&add_pending_mutex);
|
|
|
|
pthread_cond_wait(&addPendingOK, &add_pending_mutex);
|
|
|
|
pthread_mutex_unlock(&add_pending_mutex);
|
|
|
|
|
|
|
|
p = loadPage(pageid);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&add_pending_mutex);
|
|
|
|
|
|
|
|
}
|
2004-07-14 20:49:18 +00:00
|
|
|
|
|
|
|
p->pending++;
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&add_pending_mutex);
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
unlock(p->loadlatch);
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
/**
|
|
|
|
|
|
|
|
Because updates to a page might not happen in order, we need to
|
|
|
|
make sure that we've applied all updates to a page that we've heard
|
|
|
|
about before we flush that page to disk.
|
|
|
|
|
|
|
|
This method informs bufferManager that an update has been applied.
|
|
|
|
It is called by operations.c every time doUpdate, redoUpdate, or
|
|
|
|
undoUpdate is called.
|
|
|
|
|
|
|
|
@todo as implemented, loadPage() ... doOperation is not atomic!
|
|
|
|
|
|
|
|
*/
|
2004-07-14 20:49:18 +00:00
|
|
|
void removePendingEvent(int pageid) {
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
Page * p;
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
p = loadPage(pageid);
|
2004-07-14 20:49:18 +00:00
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_lock(&(add_pending_mutex));
|
2004-07-14 20:49:18 +00:00
|
|
|
p->pending--;
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
assert(p->id == pageid);
|
2004-07-14 20:49:18 +00:00
|
|
|
assert(p->pending >= 0);
|
|
|
|
|
|
|
|
if(p->waiting && !p->pending) {
|
|
|
|
assert(p->waiting == 1);
|
|
|
|
pthread_cond_signal(&(p->noMorePending));
|
|
|
|
}
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
pthread_mutex_unlock(&(add_pending_mutex));
|
2004-07-20 00:15:17 +00:00
|
|
|
|
|
|
|
unlock(p->loadlatch);
|
2004-07-20 03:40:57 +00:00
|
|
|
|
|
|
|
|
2004-07-14 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|