2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996-2009 Oracle. All rights reserved.
8 * The President and Fellows of Harvard University. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "db_config.h"
40 #include "dbinc/db_page.h"
41 #include "dbinc/lock.h"
42 #include "dbinc/txn.h"
43 #include "dbinc/db_am.h"
46 * PUBLIC: int __txn_regop_recover
47 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
49 * These records are only ever written for commits. Normally, we redo any
50 * committed transaction, however if we are doing recovery to a timestamp, then
51 * we may treat transactions that committed after the timestamp as aborted.
54 __txn_regop_recover(env, dbtp, lsnp, op, info)
61 __txn_regop_args *argp;
67 (void)__txn_regop_print(env, dbtp, lsnp, op, info);
70 if ((ret = __txn_regop_read(env, dbtp->data, &argp)) != 0)
75 * We are only ever called during FORWARD_ROLL or BACKWARD_ROLL.
76 * We check for the former explicitly and the last two clauses
77 * apply to the BACKWARD_ROLL case.
80 if (op == DB_TXN_FORWARD_ROLL) {
82 * If this was a 2-phase-commit transaction, then it
83 * might already have been removed from the list, and
84 * that's OK. Ignore the return code from remove.
86 if ((ret = __db_txnlist_remove(env,
87 info, argp->txnp->txnid)) != DB_NOTFOUND && ret != 0)
89 } else if ((env->dbenv->tx_timestamp != 0 &&
90 argp->timestamp > (int32_t)env->dbenv->tx_timestamp) ||
91 (!IS_ZERO_LSN(headp->trunc_lsn) &&
92 LOG_COMPARE(&headp->trunc_lsn, lsnp) < 0)) {
94 * We failed either the timestamp check or the trunc_lsn check,
95 * so we treat this as an abort even if it was a commit record.
97 if ((ret = __db_txnlist_update(env, info,
98 argp->txnp->txnid, TXN_ABORT, NULL, &status, 1)) != 0)
100 else if (status != TXN_IGNORE && status != TXN_OK)
103 /* This is a normal commit; mark it appropriately. */
104 if ((ret = __db_txnlist_update(env,
105 info, argp->txnp->txnid, argp->opcode, lsnp,
106 &status, 0)) == DB_NOTFOUND) {
107 if ((ret = __db_txnlist_add(env,
108 info, argp->txnp->txnid,
109 argp->opcode == TXN_ABORT ?
110 TXN_IGNORE : argp->opcode, lsnp)) != 0)
112 } else if (ret != 0 ||
113 (status != TXN_IGNORE && status != TXN_OK))
118 *lsnp = argp->prev_lsn;
122 "txnid %lx commit record found, already on commit list",
123 (u_long)argp->txnp->txnid);
126 __os_free(env, argp);
132 * PUBLIC: int __txn_prepare_recover
133 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
135 * These records are only ever written for prepares.
138 __txn_prepare_recover(env, dbtp, lsnp, op, info)
145 __txn_prepare_args *argp;
153 (void)__txn_prepare_print(env, dbtp, lsnp, op, info);
156 if ((ret = __txn_prepare_read(env, dbtp->data, &argp)) != 0)
159 if (argp->opcode != TXN_PREPARE && argp->opcode != TXN_ABORT) {
166 * The return value here is either a DB_NOTFOUND or it is
167 * the transaction status from the list. It is not a normal
168 * error return, so we must make sure that in each of the
169 * cases below, we overwrite the ret value so we return
172 ret = __db_txnlist_find(env, info, argp->txnp->txnid, &status);
175 * If we are rolling forward, then an aborted prepare
176 * indicates that this may be the last record we'll see for
177 * this transaction ID, so we should remove it from the list.
180 if (op == DB_TXN_FORWARD_ROLL) {
181 if ((ret = __db_txnlist_remove(env,
182 info, argp->txnp->txnid)) != 0)
184 } else if (op == DB_TXN_BACKWARD_ROLL && status == TXN_PREPARE) {
186 * On the backward pass, we have four possibilities:
187 * 1. The transaction is already committed, no-op.
188 * 2. The transaction is already aborted, no-op.
189 * 3. The prepare failed and was aborted, mark as abort.
190 * 4. The transaction is neither committed nor aborted.
191 * Treat this like a commit and roll forward so that
192 * the transaction can be resurrected in the region.
193 * We handle cases 3 and 4 here; cases 1 and 2
194 * are the final clause below.
196 if (argp->opcode == TXN_ABORT) {
197 if ((ret = __db_txnlist_update(env,
198 info, argp->txnp->txnid,
199 TXN_ABORT, NULL, &status, 0)) != 0 &&
200 status != TXN_PREPARE)
205 * This is prepared, but not yet committed transaction. We
206 * need to add it to the transaction list, so that it gets
207 * rolled forward. We also have to add it to the region's
208 * internal state so it can be properly aborted or committed
209 * after recovery (see txn_recover).
211 else if ((ret = __db_txnlist_remove(env,
212 info, argp->txnp->txnid)) != 0) {
213 txn_err: __db_errx(env,
214 "transaction not in list %lx",
215 (u_long)argp->txnp->txnid);
217 } else if (IS_ZERO_LSN(headp->trunc_lsn) ||
218 LOG_COMPARE(&headp->trunc_lsn, lsnp) >= 0) {
219 if ((ret = __db_txnlist_add(env,
220 info, argp->txnp->txnid, TXN_COMMIT, lsnp)) == 0) {
221 /* Re-acquire the locks for this transaction. */
222 lock_dbt = &argp->locks;
223 if (LOCKING_ON(env)) {
225 if ((ret = __lock_getlocker(lt,
226 argp->txnp->txnid, 1,
227 &argp->txnp->locker)) != 0)
229 if ((ret = __lock_get_list(env,
230 argp->txnp->locker, 0,
231 DB_LOCK_WRITE, lock_dbt)) != 0)
235 ret = __txn_restore_txn(env, lsnp, argp);
242 *lsnp = argp->prev_lsn;
244 err: __os_free(env, argp);
250 * PUBLIC: int __txn_ckp_recover
251 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
254 __txn_ckp_recover(env, dbtp, lsnp, op, info)
261 __txn_ckp_args *argp;
265 __txn_ckp_print(env, dbtp, lsnp, op, info);
267 if ((ret = __txn_ckp_read(env, dbtp->data, &argp)) != 0)
270 if (op == DB_TXN_BACKWARD_ROLL)
271 __db_txnlist_ckp(env, info, lsnp);
273 *lsnp = argp->last_ckp;
274 __os_free(env, argp);
279 * __txn_child_recover
280 * Recover a commit record for a child transaction.
282 * PUBLIC: int __txn_child_recover
283 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
286 __txn_child_recover(env, dbtp, lsnp, op, info)
293 __txn_child_args *argp;
294 u_int32_t c_stat, p_stat, tmpstat;
298 (void)__txn_child_print(env, dbtp, lsnp, op, info);
300 if ((ret = __txn_child_read(env, dbtp->data, &argp)) != 0)
304 * This is a record in a PARENT's log trail indicating that a
305 * child committed. If we are aborting, return the childs last
306 * record's LSN. If we are in recovery, then if the
307 * parent is committing, we set ourselves up to commit, else
310 if (op == DB_TXN_ABORT) {
312 ret = __db_txnlist_lsnadd(env, info, &argp->prev_lsn);
314 } else if (op == DB_TXN_BACKWARD_ROLL) {
315 /* Child might exist -- look for it. */
316 ret = __db_txnlist_find(env, info, argp->child, &c_stat);
318 __db_txnlist_find(env, info, argp->txnp->txnid, &p_stat);
319 if (ret != 0 && ret != DB_NOTFOUND)
321 if (t_ret != 0 && t_ret != DB_NOTFOUND) {
326 * If the parent is in state COMMIT or IGNORE, then we apply
327 * that to the child, else we need to abort the child.
330 if (ret == DB_NOTFOUND ||
331 c_stat == TXN_OK || c_stat == TXN_COMMIT) {
332 if (t_ret == DB_NOTFOUND ||
333 (p_stat != TXN_COMMIT && p_stat != TXN_IGNORE))
338 if (ret == DB_NOTFOUND)
339 ret = __db_txnlist_add(env,
340 info, argp->child, c_stat, NULL);
342 ret = __db_txnlist_update(env, info,
343 argp->child, c_stat, NULL, &tmpstat, 0);
344 } else if (c_stat == TXN_EXPECTED) {
346 * The open after this create succeeded. If the
347 * parent succeeded, we don't want to redo; if the
348 * parent aborted, we do want to undo.
358 ret = __db_txnlist_update(env,
359 info, argp->child, c_stat, NULL, &tmpstat, 0);
360 } else if (c_stat == TXN_UNEXPECTED) {
362 * The open after this create failed. If the parent
363 * is rolling forward, we need to roll forward. If
364 * the parent failed, then we do not want to abort
365 * (because the file may not be the one in which we
368 ret = __db_txnlist_update(env, info, argp->child,
369 p_stat == TXN_COMMIT ? TXN_COMMIT : TXN_IGNORE,
372 } else if (op == DB_TXN_OPENFILES) {
374 * If we have a partial subtransaction, then the whole
375 * transaction should be ignored.
377 if ((ret = __db_txnlist_find(env,
378 info, argp->child, &c_stat)) == DB_NOTFOUND)
379 ret = __db_txnlist_update(env, info,
380 argp->txnp->txnid, TXN_IGNORE,
382 } else if (DB_REDO(op)) {
385 __db_txnlist_remove(env, info, argp->child)) != 0)
387 "Transaction not in list %x", argp->child);
391 *lsnp = argp->prev_lsn;
393 out: __os_free(env, argp);
399 * __txn_restore_txn --
400 * Using only during XA recovery. If we find any transactions that are
401 * prepared, but not yet committed, then we need to restore the transaction's
402 * state into the shared region, because the TM is going to issue an abort
403 * or commit and we need to respond correctly.
405 * lsnp is the LSN of the returned LSN
406 * argp is the prepare record (in an appropriate structure)
408 * PUBLIC: int __txn_restore_txn __P((ENV *, DB_LSN *, __txn_prepare_args *));
411 __txn_restore_txn(env, lsnp, argp)
414 __txn_prepare_args *argp;
417 DB_TXNREGION *region;
421 if (argp->gid.size == 0)
424 mgr = env->tx_handle;
425 region = mgr->reginfo.primary;
426 TXN_SYSTEM_LOCK(env);
428 /* Allocate a new transaction detail structure. */
429 if ((ret = __env_alloc(&mgr->reginfo, sizeof(TXN_DETAIL), &td)) != 0) {
430 TXN_SYSTEM_UNLOCK(env);
434 /* Place transaction on active transaction list. */
435 SH_TAILQ_INSERT_HEAD(®ion->active_txn, td, links, __txn_detail);
437 td->txnid = argp->txnp->txnid;
438 __os_id(env->dbenv, &td->pid, &td->tid);
439 td->last_lsn = *lsnp;
440 td->begin_lsn = argp->begin_lsn;
441 td->parent = INVALID_ROFF;
442 td->name = INVALID_ROFF;
443 SH_TAILQ_INIT(&td->kids);
444 MAX_LSN(td->read_lsn);
445 MAX_LSN(td->visible_lsn);
447 td->mvcc_mtx = MUTEX_INVALID;
448 td->status = TXN_PREPARED;
449 td->flags = TXN_DTL_RESTORED;
450 memcpy(td->gid, argp->gid.data, argp->gid.size);
452 td->nlog_slots = TXN_NSLOTS;
453 td->log_dbs = R_OFFSET(&mgr->reginfo, td->slots);
455 region->stat.st_nrestores++;
456 #ifdef HAVE_STATISTICS
457 region->stat.st_nactive++;
458 if (region->stat.st_nactive > region->stat.st_maxnactive)
459 region->stat.st_maxnactive = region->stat.st_nactive;
461 TXN_SYSTEM_UNLOCK(env);
466 * __txn_recycle_recover --
467 * Recovery function for recycle.
469 * PUBLIC: int __txn_recycle_recover
470 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
473 __txn_recycle_recover(env, dbtp, lsnp, op, info)
480 __txn_recycle_args *argp;
484 (void)__txn_child_print(env, dbtp, lsnp, op, info);
486 if ((ret = __txn_recycle_read(env, dbtp->data, &argp)) != 0)
489 COMPQUIET(lsnp, NULL);
491 if ((ret = __db_txnlist_gen(env, info,
492 DB_UNDO(op) ? -1 : 1, argp->min, argp->max)) != 0)
495 __os_free(env, argp);
501 * PUBLIC: int __txn_regop_42_recover
502 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
504 * These records are only ever written for commits. Normally, we redo any
505 * committed transaction, however if we are doing recovery to a timestamp, then
506 * we may treat transactions that committed after the timestamp as aborted.
509 __txn_regop_42_recover(env, dbtp, lsnp, op, info)
516 __txn_regop_42_args *argp;
522 (void)__txn_regop_42_print(env, dbtp, lsnp, op, info);
525 if ((ret = __txn_regop_42_read(env, dbtp->data, &argp)) != 0)
530 * We are only ever called during FORWARD_ROLL or BACKWARD_ROLL.
531 * We check for the former explicitly and the last two clauses
532 * apply to the BACKWARD_ROLL case.
535 if (op == DB_TXN_FORWARD_ROLL) {
537 * If this was a 2-phase-commit transaction, then it
538 * might already have been removed from the list, and
539 * that's OK. Ignore the return code from remove.
541 if ((ret = __db_txnlist_remove(env,
542 info, argp->txnp->txnid)) != DB_NOTFOUND && ret != 0)
544 } else if ((env->dbenv->tx_timestamp != 0 &&
545 argp->timestamp > (int32_t)env->dbenv->tx_timestamp) ||
546 (!IS_ZERO_LSN(headp->trunc_lsn) &&
547 LOG_COMPARE(&headp->trunc_lsn, lsnp) < 0)) {
549 * We failed either the timestamp check or the trunc_lsn check,
550 * so we treat this as an abort even if it was a commit record.
552 if ((ret = __db_txnlist_update(env, info,
553 argp->txnp->txnid, TXN_ABORT, NULL, &status, 1)) != 0)
555 else if (status != TXN_IGNORE && status != TXN_OK)
558 /* This is a normal commit; mark it appropriately. */
559 if ((ret = __db_txnlist_update(env,
560 info, argp->txnp->txnid, argp->opcode, lsnp,
561 &status, 0)) == DB_NOTFOUND) {
562 if ((ret = __db_txnlist_add(env,
563 info, argp->txnp->txnid,
564 argp->opcode == TXN_ABORT ?
565 TXN_IGNORE : argp->opcode, lsnp)) != 0)
567 } else if (ret != 0 ||
568 (status != TXN_IGNORE && status != TXN_OK))
573 *lsnp = argp->prev_lsn;
577 "txnid %lx commit record found, already on commit list",
578 (u_long)argp->txnp->txnid);
581 __os_free(env, argp);
587 * PUBLIC: int __txn_ckp_42_recover
588 * PUBLIC: __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
591 __txn_ckp_42_recover(env, dbtp, lsnp, op, info)
598 __txn_ckp_42_args *argp;
602 __txn_ckp_42_print(env, dbtp, lsnp, op, info);
604 if ((ret = __txn_ckp_42_read(env, dbtp->data, &argp)) != 0)
607 if (op == DB_TXN_BACKWARD_ROLL)
608 __db_txnlist_ckp(env, info, lsnp);
610 *lsnp = argp->last_ckp;
611 __os_free(env, argp);