From ec6ae091617bdfdca9e65e8d3e65b950d234f676 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 4 Mar 2001 19:42:05 -0500 Subject: Import changeset --- bdb/txn/txn.c | 869 +++++++++++++++++++++++++++++++++++++++++++++++++ bdb/txn/txn.src | 114 +++++++ bdb/txn/txn_auto.c | 893 +++++++++++++++++++++++++++++++++++++++++++++++++++ bdb/txn/txn_rec.c | 339 +++++++++++++++++++ bdb/txn/txn_region.c | 393 +++++++++++++++++++++++ 5 files changed, 2608 insertions(+) create mode 100644 bdb/txn/txn.c create mode 100644 bdb/txn/txn.src create mode 100644 bdb/txn/txn_auto.c create mode 100644 bdb/txn/txn_rec.c create mode 100644 bdb/txn/txn_region.c (limited to 'bdb/txn') diff --git a/bdb/txn/txn.c b/bdb/txn/txn.c new file mode 100644 index 00000000000..0f6d894c19b --- /dev/null +++ b/bdb/txn/txn.c @@ -0,0 +1,869 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1996, 1997, 1998, 1999, 2000 + * Sleepycat Software. All rights reserved. + */ +/* + * Copyright (c) 1995, 1996 + * The President and Fellows of Harvard University. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Margo Seltzer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "db_config.h" + +#ifndef lint +static const char revid[] = "$Id: txn.c,v 11.61 2001/01/10 18:18:52 bostic Exp $"; +#endif /* not lint */ + +#ifndef NO_SYSTEM_INCLUDES +#include + +#if TIME_WITH_SYS_TIME +#include +#include +#else +#if HAVE_SYS_TIME_H +#include +#else +#include +#endif +#endif + +#include +#endif + +#ifdef HAVE_RPC +#include "db_server.h" +#endif + +#include "db_int.h" +#include "db_shash.h" +#include "txn.h" +#include "lock.h" +#include "log.h" +#include "db_dispatch.h" +#include "db_page.h" +#include "db_ext.h" + +#ifdef HAVE_RPC +#include "gen_client_ext.h" +#include "rpc_client_ext.h" +#endif + +static int __txn_begin __P((DB_TXN *)); +static int __txn_isvalid __P((const DB_TXN *, TXN_DETAIL **, u_int32_t)); +static int __txn_undo __P((DB_TXN *)); + +/* + * txn_begin -- + * This is a wrapper to the actual begin process. Normal txn_begin() + * allocates a DB_TXN structure for the caller, while txn_xa_begin() does + * not. Other than that, both call into the common __txn_begin code(). + * + * Internally, we use TXN_DETAIL structures, but the DB_TXN structure + * provides access to the transaction ID and the offset in the transaction + * region of the TXN_DETAIL structure. + */ +int +txn_begin(dbenv, parent, txnpp, flags) + DB_ENV *dbenv; + DB_TXN *parent, **txnpp; + u_int32_t flags; +{ + DB_TXN *txn; + int ret; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_begin(dbenv, parent, txnpp, flags)); +#endif + + PANIC_CHECK(dbenv); + ENV_REQUIRES_CONFIG(dbenv, dbenv->tx_handle, DB_INIT_TXN); + + if ((ret = __db_fchk(dbenv, + "txn_begin", flags, + DB_TXN_NOWAIT | DB_TXN_NOSYNC | DB_TXN_SYNC)) != 0) + return (ret); + if ((ret = __db_fcchk(dbenv, + "txn_begin", flags, DB_TXN_NOSYNC, DB_TXN_SYNC)) != 0) + return (ret); + + if ((ret = __os_calloc(dbenv, 1, sizeof(DB_TXN), &txn)) != 0) + return (ret); + + txn->mgrp = dbenv->tx_handle; + txn->parent = parent; + TAILQ_INIT(&txn->kids); + txn->flags = TXN_MALLOC; + if (LF_ISSET(DB_TXN_NOSYNC)) + F_SET(txn, TXN_NOSYNC); + if (LF_ISSET(DB_TXN_SYNC)) + F_SET(txn, TXN_SYNC); + if (LF_ISSET(DB_TXN_NOWAIT)) + F_SET(txn, TXN_NOWAIT); + + if ((ret = __txn_begin(txn)) != 0) { + __os_free(txn, sizeof(DB_TXN)); + txn = NULL; + } + + if (txn != NULL && parent != NULL) + TAILQ_INSERT_HEAD(&parent->kids, txn, klinks); + + *txnpp = txn; + return (ret); +} + +/* + * __txn_xa_begin -- + * XA version of txn_begin. + * + * PUBLIC: int __txn_xa_begin __P((DB_ENV *, DB_TXN *)); + */ +int +__txn_xa_begin(dbenv, txn) + DB_ENV *dbenv; + DB_TXN *txn; +{ + PANIC_CHECK(dbenv); + + memset(txn, 0, sizeof(DB_TXN)); + + txn->mgrp = dbenv->tx_handle; + + return (__txn_begin(txn)); +} + +/* + * __txn_begin -- + * Normal DB version of txn_begin. + */ +static int +__txn_begin(txn) + DB_TXN *txn; +{ + DB_ENV *dbenv; + DB_LSN begin_lsn; + DB_TXNMGR *mgr; + DB_TXNREGION *region; + TXN_DETAIL *td; + size_t off; + u_int32_t id; + int ret; + + mgr = txn->mgrp; + dbenv = mgr->dbenv; + region = mgr->reginfo.primary; + + /* + * We do not have to write begin records (and if we do not, then we + * need never write records for read-only transactions). However, + * we do need to find the current LSN so that we can store it in the + * transaction structure, so we can know where to take checkpoints. + */ + if (LOGGING_ON(dbenv) && + (ret = log_put(dbenv, &begin_lsn, NULL, DB_CURLSN)) != 0) + goto err2; + + R_LOCK(dbenv, &mgr->reginfo); + + /* Make sure that last_txnid is not going to wrap around. */ + if (region->last_txnid == TXN_INVALID) { + __db_err(dbenv, +"txn_begin: transaction ID wrapped. Exit the database environment\nand restart the application as if application failure had occurred"); + ret = EINVAL; + goto err1; + } + + /* Allocate a new transaction detail structure. */ + if ((ret = + __db_shalloc(mgr->reginfo.addr, sizeof(TXN_DETAIL), 0, &td)) != 0) { + __db_err(dbenv, + "Unable to allocate memory for transaction detail"); + goto err1; + } + + /* Place transaction on active transaction list. */ + SH_TAILQ_INSERT_HEAD(®ion->active_txn, td, links, __txn_detail); + + id = ++region->last_txnid; + ++region->nbegins; + if (++region->nactive > region->maxnactive) + region->maxnactive = region->nactive; + + td->txnid = id; + td->begin_lsn = begin_lsn; + ZERO_LSN(td->last_lsn); + td->status = TXN_RUNNING; + if (txn->parent != NULL) + td->parent = txn->parent->off; + else + td->parent = INVALID_ROFF; + + off = R_OFFSET(&mgr->reginfo, td); + R_UNLOCK(dbenv, &mgr->reginfo); + + ZERO_LSN(txn->last_lsn); + txn->txnid = id; + txn->off = off; + + /* + * If this is a transaction family, we must link the child to the + * maximal grandparent in the lock table for deadlock detection. + */ + if (txn->parent != NULL && LOCKING_ON(dbenv)) + if ((ret = __lock_addfamilylocker(dbenv, + txn->parent->txnid, txn->txnid)) != 0) + goto err2; + + if (F_ISSET(txn, TXN_MALLOC)) { + MUTEX_THREAD_LOCK(dbenv, mgr->mutexp); + TAILQ_INSERT_TAIL(&mgr->txn_chain, txn, links); + MUTEX_THREAD_UNLOCK(dbenv, mgr->mutexp); + } + + return (0); + +err1: R_UNLOCK(dbenv, &mgr->reginfo); + +err2: return (ret); +} + +/* + * txn_commit -- + * Commit a transaction. + */ +int +txn_commit(txnp, flags) + DB_TXN *txnp; + u_int32_t flags; +{ + DB_ENV *dbenv; + DB_TXN *kid; + int is_commit, ret, t_ret; + + dbenv = txnp->mgrp->dbenv; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_commit(txnp, flags)); +#endif + + PANIC_CHECK(dbenv); + + if ((ret = __txn_isvalid(txnp, NULL, TXN_COMMITTED)) != 0) + return (ret); + + /* + * We clear flags that are incorrect, ignoring any flag errors, and + * default to synchronous operations. By definition, transaction + * handles are dead when we return, and this error should never + * happen, but we don't want to fail in the field 'cause the app is + * specifying the wrong flag for some reason. + */ + if (__db_fchk(dbenv, + "txn_commit", flags, DB_TXN_NOSYNC | DB_TXN_SYNC) != 0) + flags = DB_TXN_SYNC; + if (__db_fcchk(dbenv, + "txn_commit", flags, DB_TXN_NOSYNC, DB_TXN_SYNC) != 0) + flags = DB_TXN_SYNC; + if (LF_ISSET(DB_TXN_NOSYNC)) { + F_CLR(txnp, TXN_SYNC); + F_SET(txnp, TXN_NOSYNC); + } + if (LF_ISSET(DB_TXN_SYNC)) { + F_CLR(txnp, TXN_NOSYNC); + F_SET(txnp, TXN_SYNC); + } + + /* + * Commit any unresolved children. If there's an error, abort any + * unresolved children and the parent. + */ + while ((kid = TAILQ_FIRST(&txnp->kids)) != NULL) + if ((ret = txn_commit(kid, flags)) != 0) { + while ((kid = TAILQ_FIRST(&txnp->kids)) != NULL) + (void)txn_abort(kid); + (void)txn_abort(txnp); + goto err; + } + + /* + * If there are any log records, write a log record and sync the log, + * else do no log writes. If the commit is for a child transaction, + * we do not need to commit the child synchronously since it may still + * abort (if its parent aborts), and otherwise its parent or ultimate + * ancestor will write synchronously. + * + * I'd rather return a logging error than a flag-wrong error, so if + * the log routines fail, set "ret" without regard to previous value. + */ + if (LOGGING_ON(dbenv) && !IS_ZERO_LSN(txnp->last_lsn)) { + if (txnp->parent == NULL) { + if ((t_ret = __txn_regop_log(dbenv, + txnp, &txnp->last_lsn, + (F_ISSET(dbenv, DB_ENV_TXN_NOSYNC) && + !F_ISSET(txnp, TXN_SYNC)) || + F_ISSET(txnp, TXN_NOSYNC) ? 0 : DB_FLUSH, + TXN_COMMIT, (int32_t)time(NULL))) != 0) { + ret = t_ret; + goto err; + } + } else { + /* Log the commit in the parent! */ + if ((t_ret = __txn_child_log(dbenv, + txnp->parent, &txnp->parent->last_lsn, + 0, txnp->txnid, &txnp->last_lsn)) != 0) { + ret = t_ret; + goto err; + } + + F_SET(txnp->parent, TXN_CHILDCOMMIT); + } + } + + is_commit = 1; + if (0) { +err: is_commit = 0; + } + if ((t_ret = __txn_end(txnp, is_commit)) != 0 && ret == 0) + ret = t_ret; + return (ret); +} + +/* + * txn_abort -- + * Abort a transaction. + */ +int +txn_abort(txnp) + DB_TXN *txnp; +{ + DB_ENV *dbenv; + DB_TXN *kid; + int ret, t_ret; + + dbenv = txnp->mgrp->dbenv; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_abort(txnp)); +#endif + + PANIC_CHECK(dbenv); + + if ((ret = __txn_isvalid(txnp, NULL, TXN_ABORTED)) != 0) + return (ret); + + /* Abort any unresolved children. */ + while ((kid = TAILQ_FIRST(&txnp->kids)) != NULL) + if ((t_ret = txn_abort(kid)) != 0 && ret == 0) + ret = t_ret; + + if ((t_ret = __txn_undo(txnp)) != 0 && ret == 0) + ret = t_ret; + + if ((t_ret = __txn_end(txnp, 0)) != 0 && ret == 0) + ret = t_ret; + return (ret); +} + +/* + * txn_prepare -- + * Flush the log so a future commit is guaranteed to succeed. + */ +int +txn_prepare(txnp) + DB_TXN *txnp; +{ + DBT xid; + DB_ENV *dbenv; + DB_TXN *kid; + TXN_DETAIL *td; + int ret; + + dbenv = txnp->mgrp->dbenv; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_prepare(txnp)); +#endif + + PANIC_CHECK(dbenv); + + if ((ret = __txn_isvalid(txnp, &td, TXN_PREPARED)) != 0) + return (ret); + + /* Prepare any unresolved children. */ + while ((kid = TAILQ_FIRST(&txnp->kids)) != NULL) + if ((ret = txn_prepare(kid)) != 0) + return (ret); + + /* + * We indicate that a transaction is an XA transaction by putting + * a valid size in the xid.size fiels. XA requires that the transaction + * be either ENDED or SUSPENDED when prepare is called, so we know + * that if the xa_status isn't in one of those states, but we are + * calling prepare that we are not an XA transaction. + */ + + if (LOGGING_ON(dbenv)) { + memset(&xid, 0, sizeof(xid)); + xid.data = td->xid; + xid.size = td->xa_status != TXN_XA_ENDED && + td->xa_status != TXN_XA_SUSPENDED ? 0 : sizeof(td->xid); + if ((ret = __txn_xa_regop_log(dbenv, txnp, &txnp->last_lsn, + (F_ISSET(dbenv, DB_ENV_TXN_NOSYNC) && + !F_ISSET(txnp, TXN_SYNC)) || + F_ISSET(txnp, TXN_NOSYNC) ? 0 : DB_FLUSH, TXN_PREPARE, + &xid, td->format, td->gtrid, td->bqual, + &td->begin_lsn)) != 0) { + __db_err(dbenv, "txn_prepare: log_write failed %s", + db_strerror(ret)); + return (ret); + } + if (txnp->parent != NULL) + F_SET(txnp->parent, TXN_CHILDCOMMIT); + } + + MUTEX_THREAD_LOCK(dbenv, txnp->mgrp->mutexp); + td->status = TXN_PREPARED; + MUTEX_THREAD_UNLOCK(dbenv, txnp->mgrp->mutexp); + return (0); +} + +/* + * txn_id -- + * Return the transaction ID. + */ +u_int32_t +txn_id(txnp) + DB_TXN *txnp; +{ + return (txnp->txnid); +} + +/* + * __txn_isvalid -- + * Return 0 if the txnp is reasonable, otherwise panic. + */ +static int +__txn_isvalid(txnp, tdp, op) + const DB_TXN *txnp; + TXN_DETAIL **tdp; + u_int32_t op; +{ + DB_TXNMGR *mgrp; + TXN_DETAIL *tp; + + mgrp = txnp->mgrp; + + /* Check for live cursors. */ + if (txnp->cursors != 0) { + __db_err(mgrp->dbenv, "transaction has active cursors"); + goto err; + } + + /* Check transaction's status. */ + tp = (TXN_DETAIL *)R_ADDR(&mgrp->reginfo, txnp->off); + if (tdp != NULL) + *tdp = tp; + + switch (tp->status) { + case TXN_ABORTED: + case TXN_COMMITTED: + default: + __db_err(mgrp->dbenv, "transaction already %s", + tp->status == TXN_COMMITTED ? "committed" : "aborted"); + goto err; + case TXN_PREPARED: + if (op == TXN_PREPARED) { + __db_err(mgrp->dbenv, "transaction already prepared"); + goto err; + } + case TXN_RUNNING: + break; + } + + return (0); + +err: /* + * If there's a serious problem with the transaction, panic. TXN + * handles are dead by definition when we return, and if you use + * a cursor you forgot to close, we have no idea what will happen. + */ + return (__db_panic(mgrp->dbenv, EINVAL)); +} + +/* + * __txn_end -- + * Internal transaction end routine. + * + * PUBLIC: int __txn_end __P((DB_TXN *, int)); + */ +int +__txn_end(txnp, is_commit) + DB_TXN *txnp; + int is_commit; +{ + DB_ENV *dbenv; + DB_LOCKREQ request; + DB_TXNMGR *mgr; + DB_TXNREGION *region; + TXN_DETAIL *tp; + int ret; + + mgr = txnp->mgrp; + dbenv = mgr->dbenv; + region = mgr->reginfo.primary; + + /* Release the locks. */ + request.op = txnp->parent == NULL || + is_commit == 0 ? DB_LOCK_PUT_ALL : DB_LOCK_INHERIT; + + if (LOCKING_ON(dbenv)) { + ret = lock_vec(dbenv, txnp->txnid, 0, &request, 1, NULL); + if (ret != 0 && (ret != DB_LOCK_DEADLOCK || is_commit)) { + __db_err(dbenv, "%s: release locks failed %s", + is_commit ? "txn_commit" : "txn_abort", + db_strerror(ret)); + __db_panic(dbenv, ret); + } + } + + /* End the transaction. */ + R_LOCK(dbenv, &mgr->reginfo); + + tp = (TXN_DETAIL *)R_ADDR(&mgr->reginfo, txnp->off); + SH_TAILQ_REMOVE(®ion->active_txn, tp, links, __txn_detail); + __db_shalloc_free(mgr->reginfo.addr, tp); + + if (is_commit) + region->ncommits++; + else + region->naborts++; + --region->nactive; + + R_UNLOCK(dbenv, &mgr->reginfo); + + /* + * The transaction cannot get more locks, remove its locker info. + */ + if (LOCKING_ON(dbenv)) + __lock_freefamilylocker(dbenv->lk_handle, txnp->txnid); + if (txnp->parent != NULL) + TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks); + + /* Free the space. */ + if (F_ISSET(txnp, TXN_MALLOC)) { + MUTEX_THREAD_LOCK(dbenv, mgr->mutexp); + TAILQ_REMOVE(&mgr->txn_chain, txnp, links); + MUTEX_THREAD_UNLOCK(dbenv, mgr->mutexp); + + __os_free(txnp, sizeof(*txnp)); + } + + return (0); +} + +/* + * __txn_undo -- + * Undo the transaction with id txnid. Returns 0 on success and + * errno on failure. + */ +static int +__txn_undo(txnp) + DB_TXN *txnp; +{ + DBT rdbt; + DB_ENV *dbenv; + DB_LSN key_lsn; + DB_TXNMGR *mgr; + void *txnlist; + int ret, threaded; + + mgr = txnp->mgrp; + dbenv = mgr->dbenv; + txnlist = NULL; + + if (!LOGGING_ON(dbenv)) + return (0); + + /* + * This is the simplest way to code this, but if the mallocs during + * recovery turn out to be a performance issue, we can do the + * allocation here and use DB_DBT_USERMEM. + */ + memset(&rdbt, 0, sizeof(rdbt)); + threaded = F_ISSET(dbenv, DB_ENV_THREAD) ? 1 : 0; + if (threaded) + F_SET(&rdbt, DB_DBT_MALLOC); + + key_lsn = txnp->last_lsn; + + /* Allocate a transaction list for children or aborted page creates. */ + if ((ret = __db_txnlist_init(dbenv, &txnlist)) != 0) + return (ret); + + if (F_ISSET(txnp, TXN_CHILDCOMMIT) && + (ret = __db_txnlist_lsninit(dbenv, + txnlist, &txnp->last_lsn)) != 0) + return (ret); + + for (ret = 0; ret == 0 && !IS_ZERO_LSN(key_lsn);) { + /* + * The dispatch routine returns the lsn of the record + * before the current one in the key_lsn argument. + */ + if ((ret = log_get(dbenv, &key_lsn, &rdbt, DB_SET)) == 0) { + ret = __db_dispatch(dbenv, + &rdbt, &key_lsn, DB_TXN_ABORT, txnlist); + if (threaded && rdbt.data != NULL) { + __os_free(rdbt.data, rdbt.size); + rdbt.data = NULL; + } + if (F_ISSET(txnp, TXN_CHILDCOMMIT)) + (void)__db_txnlist_lsnadd(dbenv, + txnlist, &key_lsn, 0); + } + if (ret != 0) { + __db_err(txnp->mgrp->dbenv, + "txn_abort: Log undo failed for LSN: %lu %lu: %s", + (u_long)key_lsn.file, (u_long)key_lsn.offset, + db_strerror(ret)); + if (txnlist != NULL) + __db_txnlist_end(dbenv, txnlist); + return (ret); + } + } + + if (txnlist != NULL) { + __db_do_the_limbo(dbenv, txnlist); + __db_txnlist_end(dbenv, txnlist); + } + + return (ret); +} + +/* + * Transaction checkpoint. + * If either kbytes or minutes is non-zero, then we only take the checkpoint + * more than "minutes" minutes have passed since the last checkpoint or if + * more than "kbytes" of log data have been written since the last checkpoint. + * When taking a checkpoint, find the oldest active transaction and figure out + * its first LSN. This is the lowest LSN we can checkpoint, since any record + * written after since that point may be involved in a transaction and may + * therefore need to be undone in the case of an abort. + */ +int +txn_checkpoint(dbenv, kbytes, minutes, flags) + DB_ENV *dbenv; + u_int32_t kbytes, minutes, flags; +{ + DB_LOG *dblp; + DB_LSN ckp_lsn, sync_lsn, last_ckp; + DB_TXNMGR *mgr; + DB_TXNREGION *region; + LOG *lp; + TXN_DETAIL *txnp; + time_t last_ckp_time, now; + u_int32_t bytes, mbytes; + int interval, ret; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_checkpoint(dbenv, kbytes, minutes)); +#endif + PANIC_CHECK(dbenv); + ENV_REQUIRES_CONFIG(dbenv, dbenv->tx_handle, DB_INIT_TXN); + + mgr = dbenv->tx_handle; + region = mgr->reginfo.primary; + dblp = dbenv->lg_handle; + lp = dblp->reginfo.primary; + + /* + * Check if we need to checkpoint. + */ + ZERO_LSN(ckp_lsn); + + if (LF_ISSET(DB_FORCE)) + goto do_ckp; + + R_LOCK(dbenv, &dblp->reginfo); + mbytes = lp->stat.st_wc_mbytes; + /* + * We add the current buffer offset so as to count bytes that + * have not yet been written, but are sitting in the log buffer. + */ + bytes = lp->stat.st_wc_bytes + lp->b_off; + ckp_lsn = lp->lsn; + R_UNLOCK(dbenv, &dblp->reginfo); + + /* Don't checkpoint a quiescent database. */ + if (bytes == 0 && mbytes == 0) + return (0); + + if (kbytes != 0 && mbytes * 1024 + bytes / 1024 >= (u_int32_t)kbytes) + goto do_ckp; + + if (minutes != 0) { + (void)time(&now); + + R_LOCK(dbenv, &mgr->reginfo); + last_ckp_time = region->time_ckp; + R_UNLOCK(dbenv, &mgr->reginfo); + + if (now - last_ckp_time >= (time_t)(minutes * 60)) + goto do_ckp; + } + + /* + * If we checked time and data and didn't go to checkpoint, + * we're done. + */ + if (minutes != 0 || kbytes != 0) + return (0); + +do_ckp: + if (IS_ZERO_LSN(ckp_lsn)) { + R_LOCK(dbenv, &dblp->reginfo); + ckp_lsn = lp->lsn; + R_UNLOCK(dbenv, &dblp->reginfo); + } + + /* + * We have to find an LSN such that all transactions begun + * before that LSN are complete. + */ + R_LOCK(dbenv, &mgr->reginfo); + + if (IS_ZERO_LSN(region->pending_ckp)) { + for (txnp = + SH_TAILQ_FIRST(®ion->active_txn, __txn_detail); + txnp != NULL; + txnp = SH_TAILQ_NEXT(txnp, links, __txn_detail)) { + + /* + * Look through the active transactions for the + * lowest begin lsn. + */ + if (!IS_ZERO_LSN(txnp->begin_lsn) && + log_compare(&txnp->begin_lsn, &ckp_lsn) < 0) + ckp_lsn = txnp->begin_lsn; + } + region->pending_ckp = ckp_lsn; + } else + ckp_lsn = region->pending_ckp; + + R_UNLOCK(dbenv, &mgr->reginfo); + + /* + * Try three times to sync the mpool buffers up to the specified LSN, + * sleeping 1, 2 and 4 seconds between attempts. + */ + if (MPOOL_ON(dbenv)) + for (interval = 1;;) { + /* + * memp_sync may change the lsn you pass it, so don't + * pass it the actual ckp_lsn, pass it a local instead. + */ + sync_lsn = ckp_lsn; + if ((ret = memp_sync(dbenv, &sync_lsn)) == 0) + break; + + /* + * ret == DB_INCOMPLETE means there are still buffers + * to flush, the checkpoint is not complete. + */ + if (ret == DB_INCOMPLETE) { + if (interval > 4) + return (ret); + + (void)__os_sleep(dbenv, interval, 0); + interval *= 2; + } else { + __db_err(dbenv, + "txn_checkpoint: failure in memp_sync %s", + db_strerror(ret)); + return (ret); + } + } + + if (LOGGING_ON(dbenv)) { + R_LOCK(dbenv, &mgr->reginfo); + last_ckp = region->last_ckp; + ZERO_LSN(region->pending_ckp); + R_UNLOCK(dbenv, &mgr->reginfo); + + if ((ret = __txn_ckp_log(dbenv, + NULL, &ckp_lsn, DB_CHECKPOINT, &ckp_lsn, + &last_ckp, (int32_t)time(NULL))) != 0) { + __db_err(dbenv, + "txn_checkpoint: log failed at LSN [%ld %ld] %s", + (long)ckp_lsn.file, (long)ckp_lsn.offset, + db_strerror(ret)); + return (ret); + } + + R_LOCK(dbenv, &mgr->reginfo); + region->last_ckp = ckp_lsn; + (void)time(®ion->time_ckp); + R_UNLOCK(dbenv, &mgr->reginfo); + } + return (0); +} + +/* + * __txn_activekids -- + * Return if this transaction has any active children. + * + * PUBLIC: int __txn_activekids __P((DB_ENV *, u_int32_t, DB_TXN *)); + */ +int +__txn_activekids(dbenv, rectype, txnp) + DB_ENV *dbenv; + u_int32_t rectype; + DB_TXN *txnp; +{ + /* + * On a child commit, we know that there are children (i.e., the + * commiting child at the least. In that case, skip this check. + */ + if (rectype == DB_txn_child) + return (0); + + if (TAILQ_FIRST(&txnp->kids) != NULL) { + __db_err(dbenv, "Child transaction is active"); + return (EPERM); + } + return (0); +} diff --git a/bdb/txn/txn.src b/bdb/txn/txn.src new file mode 100644 index 00000000000..b1e131c2bd7 --- /dev/null +++ b/bdb/txn/txn.src @@ -0,0 +1,114 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1996, 1997, 1998, 1999, 2000 + * Sleepycat Software. All rights reserved. + * + * $Id: txn.src,v 11.10 2001/01/02 00:58:33 margo Exp $ + */ + +PREFIX txn + +INCLUDE #include "db_config.h" +INCLUDE +INCLUDE #ifndef NO_SYSTEM_INCLUDES +INCLUDE #include +INCLUDE +INCLUDE #include +INCLUDE #include +INCLUDE #include +INCLUDE #endif +INCLUDE +INCLUDE #include "db_int.h" +INCLUDE #include "db_page.h" +INCLUDE #include "db_dispatch.h" +INCLUDE #include "db_am.h" +INCLUDE #include "txn.h" +INCLUDE + +/* + * DEPRECATED in 3.1 to add timestamps. + */ +DEPRECATED old_regop 6 +ARG opcode u_int32_t lu +END + +/* + * This is the standard log operation for commit. + * Note that we are using an int32_t for the timestamp. This means that + * in 2039 we will need to deprecate this log record and create one that + * either changes the Epoch or has a 64-bit offset. + */ +BEGIN regop 10 +ARG opcode u_int32_t lu +ARG timestamp int32_t ld +END + +DEPRECATED old_ckp 7 +POINTER ckp_lsn DB_LSN * lu +POINTER last_ckp DB_LSN * lu +END + +/* + * This is the checkpoint record. It contains the lsn that the checkpoint + * guarantees and a pointer to the last checkpoint so we can walk backwards + * by checkpoint. + * + * ckp_lsn: + * The lsn in the log of the most recent point at which all begun + * transactions have been aborted. This is the point for which + * the checkpoint is relevant. + * last_ckp: + * The previous checkpoint. + * timestamp: + * See comment in commit about timestamps. + */ +BEGIN ckp 11 +POINTER ckp_lsn DB_LSN * lu +POINTER last_ckp DB_LSN * lu +ARG timestamp int32_t ld +END + +/* + * This is the standard log operation for prepare (since right now + * we only use prepare in an XA environment). + */ +DEPRECATED xa_regop_old 8 +ARG opcode u_int32_t lu +DBT xid DBT s +ARG formatID int32_t ld +ARG gtrid u_int32_t u +ARG bqual u_int32_t u +END + +/* + * This is the standard log operation for prepare (since right now + * we only use prepare in an XA environment). + */ +BEGIN xa_regop 13 +ARG opcode u_int32_t lu +DBT xid DBT s +ARG formatID int32_t ld +ARG gtrid u_int32_t u +ARG bqual u_int32_t u +POINTER begin_lsn DB_LSN * lu +END + +/* + * This is the log operation for a child commit. + */ +DEPRECATED child_old 9 +ARG opcode u_int32_t lu +ARG parent u_int32_t lx +END + +/* + * This is the (new) log operation for a child commit. It is + * logged as a record in the PARENT. The child field contains + * the transaction ID of the child committing and the c_lsn is + * the last LSN of the child's log trail. + */ +BEGIN child 12 +ARG child u_int32_t lx +POINTER c_lsn DB_LSN * lu +END diff --git a/bdb/txn/txn_auto.c b/bdb/txn/txn_auto.c new file mode 100644 index 00000000000..cbfa536733e --- /dev/null +++ b/bdb/txn/txn_auto.c @@ -0,0 +1,893 @@ +/* Do not edit: automatically built by gen_rec.awk. */ +#include "db_config.h" + +#ifndef NO_SYSTEM_INCLUDES +#include + +#include +#include +#include +#endif + +#include "db_int.h" +#include "db_page.h" +#include "db_dispatch.h" +#include "db_am.h" +#include "txn.h" + +int +__txn_old_regop_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_old_regop_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_old_regop_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_old_regop: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\topcode: %lu\n", (u_long)argp->opcode); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_old_regop_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_old_regop_args **argpp; +{ + __txn_old_regop_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_old_regop_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->opcode, bp, sizeof(argp->opcode)); + bp += sizeof(argp->opcode); + *argpp = argp; + return (0); +} + +int +__txn_regop_log(dbenv, txnid, ret_lsnp, flags, + opcode, timestamp) + DB_ENV *dbenv; + DB_TXN *txnid; + DB_LSN *ret_lsnp; + u_int32_t flags; + u_int32_t opcode; + int32_t timestamp; +{ + DBT logrec; + DB_LSN *lsnp, null_lsn; + u_int32_t rectype, txn_num; + int ret; + u_int8_t *bp; + + rectype = DB_txn_regop; + if (txnid != NULL && + TAILQ_FIRST(&txnid->kids) != NULL && + (ret = __txn_activekids(dbenv, rectype, txnid)) != 0) + return (ret); + txn_num = txnid == NULL ? 0 : txnid->txnid; + if (txnid == NULL) { + ZERO_LSN(null_lsn); + lsnp = &null_lsn; + } else + lsnp = &txnid->last_lsn; + logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN) + + sizeof(opcode) + + sizeof(timestamp); + if ((ret = __os_malloc(dbenv, logrec.size, NULL, &logrec.data)) != 0) + return (ret); + + bp = logrec.data; + memcpy(bp, &rectype, sizeof(rectype)); + bp += sizeof(rectype); + memcpy(bp, &txn_num, sizeof(txn_num)); + bp += sizeof(txn_num); + memcpy(bp, lsnp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(bp, &opcode, sizeof(opcode)); + bp += sizeof(opcode); + memcpy(bp, ×tamp, sizeof(timestamp)); + bp += sizeof(timestamp); + DB_ASSERT((u_int32_t)(bp - (u_int8_t *)logrec.data) == logrec.size); + ret = log_put(dbenv, ret_lsnp, (DBT *)&logrec, flags); + if (txnid != NULL) + txnid->last_lsn = *ret_lsnp; + __os_free(logrec.data, logrec.size); + return (ret); +} + +int +__txn_regop_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_regop_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_regop_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_regop: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\topcode: %lu\n", (u_long)argp->opcode); + printf("\ttimestamp: %ld\n", (long)argp->timestamp); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_regop_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_regop_args **argpp; +{ + __txn_regop_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_regop_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->opcode, bp, sizeof(argp->opcode)); + bp += sizeof(argp->opcode); + memcpy(&argp->timestamp, bp, sizeof(argp->timestamp)); + bp += sizeof(argp->timestamp); + *argpp = argp; + return (0); +} + +int +__txn_old_ckp_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_old_ckp_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_old_ckp_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_old_ckp: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\tckp_lsn: [%lu][%lu]\n", + (u_long)argp->ckp_lsn.file, (u_long)argp->ckp_lsn.offset); + printf("\tlast_ckp: [%lu][%lu]\n", + (u_long)argp->last_ckp.file, (u_long)argp->last_ckp.offset); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_old_ckp_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_old_ckp_args **argpp; +{ + __txn_old_ckp_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_old_ckp_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->ckp_lsn, bp, sizeof(argp->ckp_lsn)); + bp += sizeof(argp->ckp_lsn); + memcpy(&argp->last_ckp, bp, sizeof(argp->last_ckp)); + bp += sizeof(argp->last_ckp); + *argpp = argp; + return (0); +} + +int +__txn_ckp_log(dbenv, txnid, ret_lsnp, flags, + ckp_lsn, last_ckp, timestamp) + DB_ENV *dbenv; + DB_TXN *txnid; + DB_LSN *ret_lsnp; + u_int32_t flags; + DB_LSN * ckp_lsn; + DB_LSN * last_ckp; + int32_t timestamp; +{ + DBT logrec; + DB_LSN *lsnp, null_lsn; + u_int32_t rectype, txn_num; + int ret; + u_int8_t *bp; + + rectype = DB_txn_ckp; + if (txnid != NULL && + TAILQ_FIRST(&txnid->kids) != NULL && + (ret = __txn_activekids(dbenv, rectype, txnid)) != 0) + return (ret); + txn_num = txnid == NULL ? 0 : txnid->txnid; + if (txnid == NULL) { + ZERO_LSN(null_lsn); + lsnp = &null_lsn; + } else + lsnp = &txnid->last_lsn; + logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN) + + sizeof(*ckp_lsn) + + sizeof(*last_ckp) + + sizeof(timestamp); + if ((ret = __os_malloc(dbenv, logrec.size, NULL, &logrec.data)) != 0) + return (ret); + + bp = logrec.data; + memcpy(bp, &rectype, sizeof(rectype)); + bp += sizeof(rectype); + memcpy(bp, &txn_num, sizeof(txn_num)); + bp += sizeof(txn_num); + memcpy(bp, lsnp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + if (ckp_lsn != NULL) + memcpy(bp, ckp_lsn, sizeof(*ckp_lsn)); + else + memset(bp, 0, sizeof(*ckp_lsn)); + bp += sizeof(*ckp_lsn); + if (last_ckp != NULL) + memcpy(bp, last_ckp, sizeof(*last_ckp)); + else + memset(bp, 0, sizeof(*last_ckp)); + bp += sizeof(*last_ckp); + memcpy(bp, ×tamp, sizeof(timestamp)); + bp += sizeof(timestamp); + DB_ASSERT((u_int32_t)(bp - (u_int8_t *)logrec.data) == logrec.size); + ret = log_put(dbenv, ret_lsnp, (DBT *)&logrec, flags); + if (txnid != NULL) + txnid->last_lsn = *ret_lsnp; + __os_free(logrec.data, logrec.size); + return (ret); +} + +int +__txn_ckp_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_ckp_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_ckp_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_ckp: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\tckp_lsn: [%lu][%lu]\n", + (u_long)argp->ckp_lsn.file, (u_long)argp->ckp_lsn.offset); + printf("\tlast_ckp: [%lu][%lu]\n", + (u_long)argp->last_ckp.file, (u_long)argp->last_ckp.offset); + printf("\ttimestamp: %ld\n", (long)argp->timestamp); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_ckp_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_ckp_args **argpp; +{ + __txn_ckp_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_ckp_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->ckp_lsn, bp, sizeof(argp->ckp_lsn)); + bp += sizeof(argp->ckp_lsn); + memcpy(&argp->last_ckp, bp, sizeof(argp->last_ckp)); + bp += sizeof(argp->last_ckp); + memcpy(&argp->timestamp, bp, sizeof(argp->timestamp)); + bp += sizeof(argp->timestamp); + *argpp = argp; + return (0); +} + +int +__txn_xa_regop_old_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_xa_regop_old_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_xa_regop_old_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_xa_regop_old: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\topcode: %lu\n", (u_long)argp->opcode); + printf("\txid: "); + for (i = 0; i < argp->xid.size; i++) { + ch = ((u_int8_t *)argp->xid.data)[i]; + if (isprint(ch) || ch == 0xa) + putchar(ch); + else + printf("%#x ", ch); + } + printf("\n"); + printf("\tformatID: %ld\n", (long)argp->formatID); + printf("\tgtrid: %u\n", argp->gtrid); + printf("\tbqual: %u\n", argp->bqual); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_xa_regop_old_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_xa_regop_old_args **argpp; +{ + __txn_xa_regop_old_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_xa_regop_old_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->opcode, bp, sizeof(argp->opcode)); + bp += sizeof(argp->opcode); + memset(&argp->xid, 0, sizeof(argp->xid)); + memcpy(&argp->xid.size, bp, sizeof(u_int32_t)); + bp += sizeof(u_int32_t); + argp->xid.data = bp; + bp += argp->xid.size; + memcpy(&argp->formatID, bp, sizeof(argp->formatID)); + bp += sizeof(argp->formatID); + memcpy(&argp->gtrid, bp, sizeof(argp->gtrid)); + bp += sizeof(argp->gtrid); + memcpy(&argp->bqual, bp, sizeof(argp->bqual)); + bp += sizeof(argp->bqual); + *argpp = argp; + return (0); +} + +int +__txn_xa_regop_log(dbenv, txnid, ret_lsnp, flags, + opcode, xid, formatID, gtrid, bqual, begin_lsn) + DB_ENV *dbenv; + DB_TXN *txnid; + DB_LSN *ret_lsnp; + u_int32_t flags; + u_int32_t opcode; + const DBT *xid; + int32_t formatID; + u_int32_t gtrid; + u_int32_t bqual; + DB_LSN * begin_lsn; +{ + DBT logrec; + DB_LSN *lsnp, null_lsn; + u_int32_t zero; + u_int32_t rectype, txn_num; + int ret; + u_int8_t *bp; + + rectype = DB_txn_xa_regop; + if (txnid != NULL && + TAILQ_FIRST(&txnid->kids) != NULL && + (ret = __txn_activekids(dbenv, rectype, txnid)) != 0) + return (ret); + txn_num = txnid == NULL ? 0 : txnid->txnid; + if (txnid == NULL) { + ZERO_LSN(null_lsn); + lsnp = &null_lsn; + } else + lsnp = &txnid->last_lsn; + logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN) + + sizeof(opcode) + + sizeof(u_int32_t) + (xid == NULL ? 0 : xid->size) + + sizeof(formatID) + + sizeof(gtrid) + + sizeof(bqual) + + sizeof(*begin_lsn); + if ((ret = __os_malloc(dbenv, logrec.size, NULL, &logrec.data)) != 0) + return (ret); + + bp = logrec.data; + memcpy(bp, &rectype, sizeof(rectype)); + bp += sizeof(rectype); + memcpy(bp, &txn_num, sizeof(txn_num)); + bp += sizeof(txn_num); + memcpy(bp, lsnp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(bp, &opcode, sizeof(opcode)); + bp += sizeof(opcode); + if (xid == NULL) { + zero = 0; + memcpy(bp, &zero, sizeof(u_int32_t)); + bp += sizeof(u_int32_t); + } else { + memcpy(bp, &xid->size, sizeof(xid->size)); + bp += sizeof(xid->size); + memcpy(bp, xid->data, xid->size); + bp += xid->size; + } + memcpy(bp, &formatID, sizeof(formatID)); + bp += sizeof(formatID); + memcpy(bp, >rid, sizeof(gtrid)); + bp += sizeof(gtrid); + memcpy(bp, &bqual, sizeof(bqual)); + bp += sizeof(bqual); + if (begin_lsn != NULL) + memcpy(bp, begin_lsn, sizeof(*begin_lsn)); + else + memset(bp, 0, sizeof(*begin_lsn)); + bp += sizeof(*begin_lsn); + DB_ASSERT((u_int32_t)(bp - (u_int8_t *)logrec.data) == logrec.size); + ret = log_put(dbenv, ret_lsnp, (DBT *)&logrec, flags); + if (txnid != NULL) + txnid->last_lsn = *ret_lsnp; + __os_free(logrec.data, logrec.size); + return (ret); +} + +int +__txn_xa_regop_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_xa_regop_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_xa_regop_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_xa_regop: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\topcode: %lu\n", (u_long)argp->opcode); + printf("\txid: "); + for (i = 0; i < argp->xid.size; i++) { + ch = ((u_int8_t *)argp->xid.data)[i]; + if (isprint(ch) || ch == 0xa) + putchar(ch); + else + printf("%#x ", ch); + } + printf("\n"); + printf("\tformatID: %ld\n", (long)argp->formatID); + printf("\tgtrid: %u\n", argp->gtrid); + printf("\tbqual: %u\n", argp->bqual); + printf("\tbegin_lsn: [%lu][%lu]\n", + (u_long)argp->begin_lsn.file, (u_long)argp->begin_lsn.offset); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_xa_regop_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_xa_regop_args **argpp; +{ + __txn_xa_regop_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_xa_regop_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->opcode, bp, sizeof(argp->opcode)); + bp += sizeof(argp->opcode); + memset(&argp->xid, 0, sizeof(argp->xid)); + memcpy(&argp->xid.size, bp, sizeof(u_int32_t)); + bp += sizeof(u_int32_t); + argp->xid.data = bp; + bp += argp->xid.size; + memcpy(&argp->formatID, bp, sizeof(argp->formatID)); + bp += sizeof(argp->formatID); + memcpy(&argp->gtrid, bp, sizeof(argp->gtrid)); + bp += sizeof(argp->gtrid); + memcpy(&argp->bqual, bp, sizeof(argp->bqual)); + bp += sizeof(argp->bqual); + memcpy(&argp->begin_lsn, bp, sizeof(argp->begin_lsn)); + bp += sizeof(argp->begin_lsn); + *argpp = argp; + return (0); +} + +int +__txn_child_old_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_child_old_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_child_old_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_child_old: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\topcode: %lu\n", (u_long)argp->opcode); + printf("\tparent: 0x%lx\n", (u_long)argp->parent); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_child_old_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_child_old_args **argpp; +{ + __txn_child_old_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_child_old_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->opcode, bp, sizeof(argp->opcode)); + bp += sizeof(argp->opcode); + memcpy(&argp->parent, bp, sizeof(argp->parent)); + bp += sizeof(argp->parent); + *argpp = argp; + return (0); +} + +int +__txn_child_log(dbenv, txnid, ret_lsnp, flags, + child, c_lsn) + DB_ENV *dbenv; + DB_TXN *txnid; + DB_LSN *ret_lsnp; + u_int32_t flags; + u_int32_t child; + DB_LSN * c_lsn; +{ + DBT logrec; + DB_LSN *lsnp, null_lsn; + u_int32_t rectype, txn_num; + int ret; + u_int8_t *bp; + + rectype = DB_txn_child; + if (txnid != NULL && + TAILQ_FIRST(&txnid->kids) != NULL && + (ret = __txn_activekids(dbenv, rectype, txnid)) != 0) + return (ret); + txn_num = txnid == NULL ? 0 : txnid->txnid; + if (txnid == NULL) { + ZERO_LSN(null_lsn); + lsnp = &null_lsn; + } else + lsnp = &txnid->last_lsn; + logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN) + + sizeof(child) + + sizeof(*c_lsn); + if ((ret = __os_malloc(dbenv, logrec.size, NULL, &logrec.data)) != 0) + return (ret); + + bp = logrec.data; + memcpy(bp, &rectype, sizeof(rectype)); + bp += sizeof(rectype); + memcpy(bp, &txn_num, sizeof(txn_num)); + bp += sizeof(txn_num); + memcpy(bp, lsnp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(bp, &child, sizeof(child)); + bp += sizeof(child); + if (c_lsn != NULL) + memcpy(bp, c_lsn, sizeof(*c_lsn)); + else + memset(bp, 0, sizeof(*c_lsn)); + bp += sizeof(*c_lsn); + DB_ASSERT((u_int32_t)(bp - (u_int8_t *)logrec.data) == logrec.size); + ret = log_put(dbenv, ret_lsnp, (DBT *)&logrec, flags); + if (txnid != NULL) + txnid->last_lsn = *ret_lsnp; + __os_free(logrec.data, logrec.size); + return (ret); +} + +int +__txn_child_print(dbenv, dbtp, lsnp, notused2, notused3) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops notused2; + void *notused3; +{ + __txn_child_args *argp; + u_int32_t i; + u_int ch; + int ret; + + i = 0; + ch = 0; + notused2 = DB_TXN_ABORT; + notused3 = NULL; + + if ((ret = __txn_child_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + printf("[%lu][%lu]txn_child: rec: %lu txnid %lx prevlsn [%lu][%lu]\n", + (u_long)lsnp->file, + (u_long)lsnp->offset, + (u_long)argp->type, + (u_long)argp->txnid->txnid, + (u_long)argp->prev_lsn.file, + (u_long)argp->prev_lsn.offset); + printf("\tchild: 0x%lx\n", (u_long)argp->child); + printf("\tc_lsn: [%lu][%lu]\n", + (u_long)argp->c_lsn.file, (u_long)argp->c_lsn.offset); + printf("\n"); + __os_free(argp, 0); + return (0); +} + +int +__txn_child_read(dbenv, recbuf, argpp) + DB_ENV *dbenv; + void *recbuf; + __txn_child_args **argpp; +{ + __txn_child_args *argp; + u_int8_t *bp; + int ret; + + ret = __os_malloc(dbenv, sizeof(__txn_child_args) + + sizeof(DB_TXN), NULL, &argp); + if (ret != 0) + return (ret); + argp->txnid = (DB_TXN *)&argp[1]; + bp = recbuf; + memcpy(&argp->type, bp, sizeof(argp->type)); + bp += sizeof(argp->type); + memcpy(&argp->txnid->txnid, bp, sizeof(argp->txnid->txnid)); + bp += sizeof(argp->txnid->txnid); + memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN)); + bp += sizeof(DB_LSN); + memcpy(&argp->child, bp, sizeof(argp->child)); + bp += sizeof(argp->child); + memcpy(&argp->c_lsn, bp, sizeof(argp->c_lsn)); + bp += sizeof(argp->c_lsn); + *argpp = argp; + return (0); +} + +int +__txn_init_print(dbenv) + DB_ENV *dbenv; +{ + int ret; + + if ((ret = __db_add_recovery(dbenv, + __txn_old_regop_print, DB_txn_old_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_regop_print, DB_txn_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_old_ckp_print, DB_txn_old_ckp)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_ckp_print, DB_txn_ckp)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_xa_regop_old_print, DB_txn_xa_regop_old)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_xa_regop_print, DB_txn_xa_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_child_old_print, DB_txn_child_old)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_child_print, DB_txn_child)) != 0) + return (ret); + return (0); +} + +int +__txn_init_recover(dbenv) + DB_ENV *dbenv; +{ + int ret; + + if ((ret = __db_add_recovery(dbenv, + __deprecated_recover, DB_txn_old_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_regop_recover, DB_txn_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __deprecated_recover, DB_txn_old_ckp)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_ckp_recover, DB_txn_ckp)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __deprecated_recover, DB_txn_xa_regop_old)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_xa_regop_recover, DB_txn_xa_regop)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __deprecated_recover, DB_txn_child_old)) != 0) + return (ret); + if ((ret = __db_add_recovery(dbenv, + __txn_child_recover, DB_txn_child)) != 0) + return (ret); + return (0); +} + diff --git a/bdb/txn/txn_rec.c b/bdb/txn/txn_rec.c new file mode 100644 index 00000000000..bed20d98e1e --- /dev/null +++ b/bdb/txn/txn_rec.c @@ -0,0 +1,339 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1996, 1997, 1998, 1999, 2000 + * Sleepycat Software. All rights reserved. + */ +/* + * Copyright (c) 1996 + * The President and Fellows of Harvard University. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "db_config.h" + +#ifndef lint +static const char revid[] = "$Id: txn_rec.c,v 11.15 2001/01/11 18:19:55 bostic Exp $"; +#endif /* not lint */ + +#ifndef NO_SYSTEM_INCLUDES +#include + +#endif + +#include "db_int.h" +#include "db_page.h" +#include "txn.h" +#include "db_am.h" +#include "db_dispatch.h" +#include "log.h" +#include "common_ext.h" + +static int __txn_restore_txn __P((DB_ENV *, DB_LSN *, __txn_xa_regop_args *)); + +#define IS_XA_TXN(R) (R->xid.size != 0) + +/* + * PUBLIC: int __txn_regop_recover + * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *)); + * + * These records are only ever written for commits. Normally, we redo any + * committed transaction, however if we are doing recovery to a timestamp, then + * we may treat transactions that commited after the timestamp as aborted. + */ +int +__txn_regop_recover(dbenv, dbtp, lsnp, op, info) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops op; + void *info; +{ + __txn_regop_args *argp; + int ret; + +#ifdef DEBUG_RECOVER + (void)__txn_regop_print(dbenv, dbtp, lsnp, op, info); +#endif + + if ((ret = __txn_regop_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + + if (argp->opcode != TXN_COMMIT) { + ret = EINVAL; + goto err; + } + + if (op == DB_TXN_FORWARD_ROLL) + ret = __db_txnlist_remove(info, argp->txnid->txnid); + else if (dbenv->tx_timestamp == 0 || + argp->timestamp <= (int32_t)dbenv->tx_timestamp) + /* + * We know this is the backward roll case because we + * are never called during ABORT or OPENFILES. + */ + ret = __db_txnlist_add(dbenv, info, argp->txnid->txnid, 0); + else + /* + * This is commit record, but we failed the timestamp check + * so we should treat it as an abort and add it to the list + * as an aborted record. + */ + ret = __db_txnlist_add(dbenv, info, argp->txnid->txnid, 1); + + if (ret == 0) + *lsnp = argp->prev_lsn; +err: __os_free(argp, 0); + + return (ret); +} + +/* + * PUBLIC: int __txn_xa_regop_recover + * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *)); + * + * These records are only ever written for prepares. + */ +int +__txn_xa_regop_recover(dbenv, dbtp, lsnp, op, info) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops op; + void *info; +{ + __txn_xa_regop_args *argp; + int ret; + +#ifdef DEBUG_RECOVER + (void)__txn_xa_regop_print(dbenv, dbtp, lsnp, op, info); +#endif + + if ((ret = __txn_xa_regop_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + + if (argp->opcode != TXN_PREPARE) { + ret = EINVAL; + goto err; + } + + ret = __db_txnlist_find(info, argp->txnid->txnid); + + /* + * If we are rolling forward, then an aborted prepare + * indicates that this is the last record we'll see for + * this transaction ID and we should remove it from the + * list. + */ + + if (op == DB_TXN_FORWARD_ROLL && ret == 1) + ret = __db_txnlist_remove(info, argp->txnid->txnid); + else if (op == DB_TXN_BACKWARD_ROLL && ret != 0) { + /* + * On the backward pass, we have three possibilities: + * 1. The transaction is already committed, no-op. + * 2. The transaction is not committed and we are XA, treat + * like commited and roll forward so that can be committed + * or aborted late. + * 3. The transaction is not committed and we are not XA + * mark the transaction as aborted. + * + * Cases 2 and 3 are handled here. + */ + + /* + * Should never have seen this transaction unless it was + * commited. + */ + DB_ASSERT(ret == DB_NOTFOUND); + + if (IS_XA_TXN(argp)) { + /* + * This is an XA prepared, but not yet committed + * transaction. We need to add it to the + * transaction list, so that it gets rolled + * forward. We also have to add it to the region's + * internal state so it can be properly aborted + * or recovered. + */ + if ((ret = __db_txnlist_add(dbenv, + info, argp->txnid->txnid, 0)) == 0) + ret = __txn_restore_txn(dbenv, lsnp, argp); + } else + ret = __db_txnlist_add(dbenv, + info, argp->txnid->txnid, 1); + } else + ret = 0; + + if (ret == 0) + *lsnp = argp->prev_lsn; + +err: __os_free(argp, 0); + + return (ret); +} + +/* + * PUBLIC: int __txn_ckp_recover + * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *)); + */ +int +__txn_ckp_recover(dbenv, dbtp, lsnp, op, info) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops op; + void *info; +{ + __txn_ckp_args *argp; + int ret; + +#ifdef DEBUG_RECOVER + __txn_ckp_print(dbenv, dbtp, lsnp, op, info); +#endif + COMPQUIET(dbenv, NULL); + + if ((ret = __txn_ckp_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + + /* + * Check for 'restart' checkpoint record. This occurs when the + * checkpoint lsn is equal to the lsn of the checkpoint record + * and means that we could set the transaction ID back to 1, so + * that we don't exhaust the transaction ID name space. + */ + if (argp->ckp_lsn.file == lsnp->file && + argp->ckp_lsn.offset == lsnp->offset) + __db_txnlist_gen(info, DB_REDO(op) ? -1 : 1); + + *lsnp = argp->last_ckp; + __os_free(argp, 0); + return (DB_TXN_CKP); +} + +/* + * __txn_child_recover + * Recover a commit record for a child transaction. + * + * PUBLIC: int __txn_child_recover + * PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *)); + */ +int +__txn_child_recover(dbenv, dbtp, lsnp, op, info) + DB_ENV *dbenv; + DBT *dbtp; + DB_LSN *lsnp; + db_recops op; + void *info; +{ + __txn_child_args *argp; + int ret; + +#ifdef DEBUG_RECOVER + (void)__txn_child_print(dbenv, dbtp, lsnp, op, info); +#endif + if ((ret = __txn_child_read(dbenv, dbtp->data, &argp)) != 0) + return (ret); + + /* + * This is a record in a PARENT's log trail indicating that a + * child commited. If we are aborting, we need to update the + * parent's LSN array. If we are in recovery, then if the + * parent is commiting, we set ourselves up to commit, else + * we do nothing. + */ + if (op == DB_TXN_ABORT) { + /* Note that __db_txnlist_lsnadd rewrites its LSN + * parameter, so you cannot reuse the argp->c_lsn field. + */ + ret = __db_txnlist_lsnadd(dbenv, + info, &argp->c_lsn, TXNLIST_NEW); + } else if (op == DB_TXN_BACKWARD_ROLL) { + if (__db_txnlist_find(info, argp->txnid->txnid) == 0) + ret = __db_txnlist_add(dbenv, info, argp->child, 0); + else + ret = __db_txnlist_add(dbenv, info, argp->child, 1); + } else + ret = __db_txnlist_remove(info, argp->child); + + if (ret == 0) + *lsnp = argp->prev_lsn; + + __os_free(argp, 0); + + return (ret); +} + +/* + * __txn_restore_txn -- + * Using only during XA recovery. If we find any transactions that are + * prepared, but not yet committed, then we need to restore the transaction's + * state into the shared region, because the TM is going to issue a txn_abort + * or txn_commit and we need to respond correctly. + * + * lsnp is the LSN of the returned LSN + * argp is the perpare record (in an appropriate structure) + */ +static int +__txn_restore_txn(dbenv, lsnp, argp) + DB_ENV *dbenv; + DB_LSN *lsnp; + __txn_xa_regop_args *argp; +{ + DB_TXNMGR *mgr; + TXN_DETAIL *td; + DB_TXNREGION *region; + int ret; + + if (argp->xid.size == 0) + return (0); + + mgr = dbenv->tx_handle; + region = mgr->reginfo.primary; + R_LOCK(dbenv, &mgr->reginfo); + + /* Allocate a new transaction detail structure. */ + if ((ret = + __db_shalloc(mgr->reginfo.addr, sizeof(TXN_DETAIL), 0, &td)) != 0) + return (ret); + + /* Place transaction on active transaction list. */ + SH_TAILQ_INSERT_HEAD(®ion->active_txn, td, links, __txn_detail); + + td->txnid = argp->txnid->txnid; + td->begin_lsn = argp->begin_lsn; + td->last_lsn = *lsnp; + td->parent = 0; + td->status = TXN_PREPARED; + td->xa_status = TXN_XA_PREPARED; + memcpy(td->xid, argp->xid.data, argp->xid.size); + td->bqual = argp->bqual; + td->gtrid = argp->gtrid; + td->format = argp->formatID; + + R_UNLOCK(dbenv, &mgr->reginfo); + return (0); +} diff --git a/bdb/txn/txn_region.c b/bdb/txn/txn_region.c new file mode 100644 index 00000000000..77ce3d08f89 --- /dev/null +++ b/bdb/txn/txn_region.c @@ -0,0 +1,393 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1996, 1997, 1998, 1999, 2000 + * Sleepycat Software. All rights reserved. + */ + +#include "db_config.h" + +#ifndef lint +static const char revid[] = "$Id: txn_region.c,v 11.36 2001/01/11 18:19:55 bostic Exp $"; +#endif /* not lint */ + +#ifndef NO_SYSTEM_INCLUDES +#include + +#if TIME_WITH_SYS_TIME +#include +#include +#else +#if HAVE_SYS_TIME_H +#include +#else +#include +#endif +#endif + +#include +#endif + +#ifdef HAVE_RPC +#include "db_server.h" +#endif + +#include "db_int.h" +#include "db_page.h" +#include "log.h" /* for __log_lastckp */ +#include "txn.h" +#include "db_am.h" + +#ifdef HAVE_RPC +#include "gen_client_ext.h" +#include "rpc_client_ext.h" +#endif + +static int __txn_init __P((DB_ENV *, DB_TXNMGR *)); +static int __txn_set_tx_max __P((DB_ENV *, u_int32_t)); +static int __txn_set_tx_recover __P((DB_ENV *, + int (*)(DB_ENV *, DBT *, DB_LSN *, db_recops))); +static int __txn_set_tx_timestamp __P((DB_ENV *, time_t *)); + +/* + * __txn_dbenv_create -- + * Transaction specific initialization of the DB_ENV structure. + * + * PUBLIC: void __txn_dbenv_create __P((DB_ENV *)); + */ +void +__txn_dbenv_create(dbenv) + DB_ENV *dbenv; +{ + dbenv->tx_max = DEF_MAX_TXNS; + + dbenv->set_tx_max = __txn_set_tx_max; + dbenv->set_tx_recover = __txn_set_tx_recover; + dbenv->set_tx_timestamp = __txn_set_tx_timestamp; + +#ifdef HAVE_RPC + /* + * If we have a client, overwrite what we just setup to point to + * client functions. + */ + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) { + dbenv->set_tx_max = __dbcl_set_tx_max; + dbenv->set_tx_recover = __dbcl_set_tx_recover; + dbenv->set_tx_timestamp = __dbcl_set_tx_timestamp; + } +#endif +} + +/* + * __txn_set_tx_max -- + * Set the size of the transaction table. + */ +static int +__txn_set_tx_max(dbenv, tx_max) + DB_ENV *dbenv; + u_int32_t tx_max; +{ + ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_tx_max"); + + dbenv->tx_max = tx_max; + return (0); +} + +/* + * __txn_set_tx_recover -- + * Set the transaction abort recover function. + */ +static int +__txn_set_tx_recover(dbenv, tx_recover) + DB_ENV *dbenv; + int (*tx_recover) __P((DB_ENV *, DBT *, DB_LSN *, db_recops)); +{ + dbenv->tx_recover = tx_recover; + return (0); +} + +/* + * __txn_set_tx_timestamp -- + * Set the transaction recovery timestamp. + */ +static int +__txn_set_tx_timestamp(dbenv, timestamp) + DB_ENV *dbenv; + time_t *timestamp; +{ + ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_tx_timestamp"); + + dbenv->tx_timestamp = *timestamp; + return (0); +} + +/* + * __txn_open -- + * Open a transaction region. + * + * PUBLIC: int __txn_open __P((DB_ENV *)); + */ +int +__txn_open(dbenv) + DB_ENV *dbenv; +{ + DB_TXNMGR *tmgrp; + int ret; + + /* Create/initialize the transaction manager structure. */ + if ((ret = __os_calloc(dbenv, 1, sizeof(DB_TXNMGR), &tmgrp)) != 0) + return (ret); + TAILQ_INIT(&tmgrp->txn_chain); + tmgrp->dbenv = dbenv; + + /* Join/create the txn region. */ + tmgrp->reginfo.type = REGION_TYPE_TXN; + tmgrp->reginfo.id = INVALID_REGION_ID; + tmgrp->reginfo.mode = dbenv->db_mode; + tmgrp->reginfo.flags = REGION_JOIN_OK; + if (F_ISSET(dbenv, DB_ENV_CREATE)) + F_SET(&tmgrp->reginfo, REGION_CREATE_OK); + if ((ret = __db_r_attach(dbenv, + &tmgrp->reginfo, TXN_REGION_SIZE(dbenv->tx_max))) != 0) + goto err; + + /* If we created the region, initialize it. */ + if (F_ISSET(&tmgrp->reginfo, REGION_CREATE)) + if ((ret = __txn_init(dbenv, tmgrp)) != 0) + goto err; + + /* Set the local addresses. */ + tmgrp->reginfo.primary = + R_ADDR(&tmgrp->reginfo, tmgrp->reginfo.rp->primary); + + /* Acquire a mutex to protect the active TXN list. */ + if (F_ISSET(dbenv, DB_ENV_THREAD)) { + if ((ret = __db_mutex_alloc( + dbenv, &tmgrp->reginfo, &tmgrp->mutexp)) != 0) + goto err; + if ((ret = __db_mutex_init( + dbenv, tmgrp->mutexp, 0, MUTEX_THREAD)) != 0) + goto err; + } + + R_UNLOCK(dbenv, &tmgrp->reginfo); + + dbenv->tx_handle = tmgrp; + return (0); + +err: if (tmgrp->reginfo.addr != NULL) { + if (F_ISSET(&tmgrp->reginfo, REGION_CREATE)) + ret = __db_panic(dbenv, ret); + R_UNLOCK(dbenv, &tmgrp->reginfo); + + (void)__db_r_detach(dbenv, &tmgrp->reginfo, 0); + } + if (tmgrp->mutexp != NULL) + __db_mutex_free(dbenv, &tmgrp->reginfo, tmgrp->mutexp); + __os_free(tmgrp, sizeof(*tmgrp)); + return (ret); +} + +/* + * __txn_init -- + * Initialize a transaction region in shared memory. + */ +static int +__txn_init(dbenv, tmgrp) + DB_ENV *dbenv; + DB_TXNMGR *tmgrp; +{ + DB_LSN last_ckp; + DB_TXNREGION *region; + int ret; + + ZERO_LSN(last_ckp); + /* + * If possible, fetch the last checkpoint LSN from the log system + * so that the backwards chain of checkpoints is unbroken when + * the environment is removed and recreated. [#2865] + */ + if (LOGGING_ON(dbenv) && (ret = __log_lastckp(dbenv, &last_ckp)) != 0) + return (ret); + + if ((ret = __db_shalloc(tmgrp->reginfo.addr, + sizeof(DB_TXNREGION), 0, &tmgrp->reginfo.primary)) != 0) { + __db_err(dbenv, + "Unable to allocate memory for the transaction region"); + return (ret); + } + tmgrp->reginfo.rp->primary = + R_OFFSET(&tmgrp->reginfo, tmgrp->reginfo.primary); + region = tmgrp->reginfo.primary; + memset(region, 0, sizeof(*region)); + + region->maxtxns = dbenv->tx_max; + region->last_txnid = TXN_MINIMUM; + ZERO_LSN(region->pending_ckp); + region->last_ckp = last_ckp; + region->time_ckp = time(NULL); + + /* + * XXX + * If we ever do more types of locking and logging, this changes. + */ + region->logtype = 0; + region->locktype = 0; + region->naborts = 0; + region->ncommits = 0; + region->nbegins = 0; + region->nactive = 0; + region->maxnactive = 0; + + SH_TAILQ_INIT(®ion->active_txn); + + return (0); +} + +/* + * __txn_close -- + * Close a transaction region. + * + * PUBLIC: int __txn_close __P((DB_ENV *)); + */ +int +__txn_close(dbenv) + DB_ENV *dbenv; +{ + DB_TXN *txnp; + DB_TXNMGR *tmgrp; + u_int32_t txnid; + int ret, t_ret; + + ret = 0; + tmgrp = dbenv->tx_handle; + + /* + * This function can only be called once per process (i.e., not + * once per thread), so no synchronization is required. + * + * The caller is doing something wrong if close is called with + * active transactions. Try and abort any active transactions, + * but it's quite likely the aborts will fail because recovery + * won't find open files. If we can't abort any transaction, + * panic, we have to run recovery to get back to a known state. + */ + if (TAILQ_FIRST(&tmgrp->txn_chain) != NULL) { + __db_err(dbenv, + "Error: closing the transaction region with active transactions\n"); + ret = EINVAL; + while ((txnp = TAILQ_FIRST(&tmgrp->txn_chain)) != NULL) { + txnid = txnp->txnid; + if ((t_ret = txn_abort(txnp)) != 0) { + __db_err(dbenv, + "Unable to abort transaction 0x%x: %s\n", + txnid, db_strerror(t_ret)); + ret = __db_panic(dbenv, t_ret); + } + } + } + + /* Flush the log. */ + if (LOGGING_ON(dbenv) && + (t_ret = log_flush(dbenv, NULL)) != 0 && ret == 0) + ret = t_ret; + + /* Discard the per-thread lock. */ + if (tmgrp->mutexp != NULL) + __db_mutex_free(dbenv, &tmgrp->reginfo, tmgrp->mutexp); + + /* Detach from the region. */ + if ((t_ret = __db_r_detach(dbenv, &tmgrp->reginfo, 0)) != 0 && ret == 0) + ret = t_ret; + + __os_free(tmgrp, sizeof(*tmgrp)); + + dbenv->tx_handle = NULL; + return (ret); +} + +int +txn_stat(dbenv, statp, db_malloc) + DB_ENV *dbenv; + DB_TXN_STAT **statp; + void *(*db_malloc) __P((size_t)); +{ + DB_TXNMGR *mgr; + DB_TXNREGION *region; + DB_TXN_STAT *stats; + TXN_DETAIL *txnp; + size_t nbytes; + u_int32_t nactive, ndx; + int ret, slop; + +#ifdef HAVE_RPC + if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) + return (__dbcl_txn_stat(dbenv, statp, db_malloc)); +#endif + + PANIC_CHECK(dbenv); + ENV_REQUIRES_CONFIG(dbenv, dbenv->tx_handle, DB_INIT_TXN); + + *statp = NULL; + + slop = 200; + mgr = dbenv->tx_handle; + region = mgr->reginfo.primary; + +retry: R_LOCK(dbenv, &mgr->reginfo); + nactive = region->nactive; + R_UNLOCK(dbenv, &mgr->reginfo); + + /* + * Allocate extra active structures to handle any transactions that + * are created while we have the region unlocked. + */ + nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * (nactive + slop); + if ((ret = __os_malloc(dbenv, nbytes, db_malloc, &stats)) != 0) + return (ret); + + R_LOCK(dbenv, &mgr->reginfo); + stats->st_last_txnid = region->last_txnid; + stats->st_last_ckp = region->last_ckp; + stats->st_maxtxns = region->maxtxns; + stats->st_naborts = region->naborts; + stats->st_nbegins = region->nbegins; + stats->st_ncommits = region->ncommits; + stats->st_pending_ckp = region->pending_ckp; + stats->st_time_ckp = region->time_ckp; + stats->st_nactive = region->nactive; + if (stats->st_nactive > nactive + 200) { + R_UNLOCK(dbenv, &mgr->reginfo); + slop *= 2; + goto retry; + } + stats->st_maxnactive = region->maxnactive; + stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1]; + + ndx = 0; + for (txnp = SH_TAILQ_FIRST(®ion->active_txn, __txn_detail); + txnp != NULL; + txnp = SH_TAILQ_NEXT(txnp, links, __txn_detail)) { + stats->st_txnarray[ndx].txnid = txnp->txnid; + if (txnp->parent == INVALID_ROFF) + stats->st_txnarray[ndx].parentid = TXN_INVALID_ID; + else + stats->st_txnarray[ndx].parentid = + ((TXN_DETAIL *)R_ADDR(&mgr->reginfo, + txnp->parent))->txnid; + stats->st_txnarray[ndx].lsn = txnp->begin_lsn; + ndx++; + + if (ndx >= stats->st_nactive) + break; + } + + stats->st_region_wait = mgr->reginfo.rp->mutex.mutex_set_wait; + stats->st_region_nowait = mgr->reginfo.rp->mutex.mutex_set_nowait; + stats->st_regsize = mgr->reginfo.rp->size; + + R_UNLOCK(dbenv, &mgr->reginfo); + + *statp = stats; + return (0); +} -- cgit v1.2.1