2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
9
* The President and Fellows of Harvard University. All rights reserved.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of the University nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#include "db_config.h"
39
static const char revid[] = "$Id$";
42
#ifndef NO_SYSTEM_INCLUDES
43
#include <sys/types.h>
49
#include "dbinc/db_page.h"
50
#include "dbinc/txn.h"
51
#include "dbinc/db_am.h"
52
#include "dbinc/db_dispatch.h"
54
#define IS_XA_TXN(R) (R->xid.size != 0)
57
* PUBLIC: int __txn_regop_recover
58
* PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
60
* These records are only ever written for commits. Normally, we redo any
61
* committed transaction, however if we are doing recovery to a timestamp, then
62
* we may treat transactions that commited after the timestamp as aborted.
65
__txn_regop_recover(dbenv, dbtp, lsnp, op, info)
73
__txn_regop_args *argp;
77
(void)__txn_regop_print(dbenv, dbtp, lsnp, op, info);
80
if ((ret = __txn_regop_read(dbenv, dbtp->data, &argp)) != 0)
85
* We are only ever called during FORWARD_ROLL or BACKWARD_ROLL.
86
* We check for the former explicitly and the last two clauses
87
* apply to the BACKWARD_ROLL case.
90
if (op == DB_TXN_FORWARD_ROLL)
92
* If this was a 2-phase-commit transaction, then it
93
* might already have been removed from the list, and
94
* that's OK. Ignore the return code from remove.
96
(void)__db_txnlist_remove(dbenv, info, argp->txnid->txnid);
97
else if ((dbenv->tx_timestamp != 0 &&
98
argp->timestamp > (int32_t)dbenv->tx_timestamp) ||
99
(!IS_ZERO_LSN(headp->trunc_lsn) &&
100
log_compare(&headp->trunc_lsn, lsnp) < 0)) {
102
* We failed either the timestamp check or the trunc_lsn check,
103
* so we treat this as an abort even if it was a commit record.
105
ret = __db_txnlist_update(dbenv,
106
info, argp->txnid->txnid, TXN_ABORT, NULL);
108
if (ret == TXN_NOTFOUND)
109
ret = __db_txnlist_add(dbenv,
110
info, argp->txnid->txnid, TXN_IGNORE, NULL);
111
else if (ret != TXN_OK)
113
/* else ret = 0; Not necessary because TXN_OK == 0 */
115
/* This is a normal commit; mark it appropriately. */
116
ret = __db_txnlist_update(dbenv,
117
info, argp->txnid->txnid, argp->opcode, lsnp);
119
if (ret == TXN_NOTFOUND)
120
ret = __db_txnlist_add(dbenv,
121
info, argp->txnid->txnid,
122
argp->opcode == TXN_ABORT ?
123
TXN_IGNORE : argp->opcode, lsnp);
124
else if (ret != TXN_OK)
126
/* else ret = 0; Not necessary because TXN_OK == 0 */
130
*lsnp = argp->prev_lsn;
134
"txnid %lx commit record found, already on commit list",
138
__os_free(dbenv, argp);
144
* PUBLIC: int __txn_xa_regop_recover
145
* PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
147
* These records are only ever written for prepares.
150
__txn_xa_regop_recover(dbenv, dbtp, lsnp, op, info)
157
__txn_xa_regop_args *argp;
161
(void)__txn_xa_regop_print(dbenv, dbtp, lsnp, op, info);
164
if ((ret = __txn_xa_regop_read(dbenv, dbtp->data, &argp)) != 0)
167
if (argp->opcode != TXN_PREPARE) {
172
ret = __db_txnlist_find(dbenv, info, argp->txnid->txnid);
175
* If we are rolling forward, then an aborted prepare
176
* indicates that this may the last record we'll see for
177
* this transaction ID, so we should remove it from the
181
if (op == DB_TXN_FORWARD_ROLL) {
182
if ((ret = __db_txnlist_remove(dbenv,
183
info, argp->txnid->txnid)) != TXN_OK)
185
} else if (op == DB_TXN_BACKWARD_ROLL && ret == TXN_PREPARE) {
187
* On the backward pass, we have three possibilities:
188
* 1. The transaction is already committed, no-op.
189
* 2. The transaction is already aborted, no-op.
190
* 3. 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 case 3 here; cases 1 and 2 are the final clause
195
* This is prepared, but not yet committed transaction. We
196
* need to add it to the transaction list, so that it gets
197
* rolled forward. We also have to add it to the region's
198
* internal state so it can be properly aborted or committed
199
* after recovery (see txn_recover).
201
if ((ret = __db_txnlist_remove(dbenv,
202
info, argp->txnid->txnid)) != TXN_OK) {
203
txn_err: __db_err(dbenv,
204
"Transaction not in list %x", argp->txnid->txnid);
206
} else if ((ret = __db_txnlist_add(dbenv,
207
info, argp->txnid->txnid, TXN_COMMIT, lsnp)) == 0)
208
ret = __txn_restore_txn(dbenv, lsnp, argp);
213
*lsnp = argp->prev_lsn;
215
err: __os_free(dbenv, argp);
221
* PUBLIC: int __txn_ckp_recover
222
* PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
225
__txn_ckp_recover(dbenv, dbtp, lsnp, op, info)
232
__txn_ckp_args *argp;
236
__txn_ckp_print(dbenv, dbtp, lsnp, op, info);
238
COMPQUIET(dbenv, NULL);
240
if ((ret = __txn_ckp_read(dbenv, dbtp->data, &argp)) != 0)
243
if (op == DB_TXN_BACKWARD_ROLL)
244
__db_txnlist_ckp(dbenv, info, lsnp);
246
*lsnp = argp->last_ckp;
247
__os_free(dbenv, argp);
252
* __txn_child_recover
253
* Recover a commit record for a child transaction.
255
* PUBLIC: int __txn_child_recover
256
* PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
259
__txn_child_recover(dbenv, dbtp, lsnp, op, info)
266
__txn_child_args *argp;
267
int c_stat, p_stat, ret;
270
(void)__txn_child_print(dbenv, dbtp, lsnp, op, info);
272
if ((ret = __txn_child_read(dbenv, dbtp->data, &argp)) != 0)
276
* This is a record in a PARENT's log trail indicating that a
277
* child commited. If we are aborting, we need to update the
278
* parent's LSN array. If we are in recovery, then if the
279
* parent is commiting, we set ourselves up to commit, else
282
if (op == DB_TXN_ABORT) {
283
/* Note that __db_txnlist_lsnadd rewrites its LSN
284
* parameter, so you cannot reuse the argp->c_lsn field.
286
ret = __db_txnlist_lsnadd(dbenv,
287
info, &argp->c_lsn, TXNLIST_NEW);
288
} else if (op == DB_TXN_BACKWARD_ROLL) {
289
/* Child might exist -- look for it. */
290
c_stat = __db_txnlist_find(dbenv, info, argp->child);
291
p_stat = __db_txnlist_find(dbenv, info, argp->txnid->txnid);
293
if (c_stat == TXN_EXPECTED) {
295
* The open after this create succeeded. If the
296
* parent succeeded, we don't want to redo; if the
297
* parent aborted, we do want to undo.
299
ret = __db_txnlist_update(dbenv,
301
p_stat == TXN_COMMIT ? TXN_IGNORE : TXN_ABORT,
305
} else if (c_stat == TXN_UNEXPECTED) {
307
* The open after this create failed. If the parent
308
* is rolling forward, we need to roll forward. If
309
* the parent failed, then we do not want to abort
310
* (because the file may not be the one in which we
313
ret = __db_txnlist_update(dbenv, info, argp->child,
314
p_stat == TXN_COMMIT ? TXN_COMMIT : TXN_IGNORE,
318
} else if (c_stat != TXN_IGNORE) {
319
ret = __db_txnlist_add(dbenv, info, argp->child,
320
p_stat == TXN_COMMIT ? TXN_COMMIT : TXN_ABORT,
326
__db_txnlist_remove(dbenv, info, argp->child)) != TXN_OK) {
328
"Transaction not in list %x", argp->txnid->txnid);
334
*lsnp = argp->prev_lsn;
336
__os_free(dbenv, argp);
342
* __txn_restore_txn --
343
* Using only during XA recovery. If we find any transactions that are
344
* prepared, but not yet committed, then we need to restore the transaction's
345
* state into the shared region, because the TM is going to issue an abort
346
* or commit and we need to respond correctly.
348
* lsnp is the LSN of the returned LSN
349
* argp is the perpare record (in an appropriate structure)
351
* PUBLIC: int __txn_restore_txn __P((DB_ENV *,
352
* PUBLIC: DB_LSN *, __txn_xa_regop_args *));
355
__txn_restore_txn(dbenv, lsnp, argp)
358
__txn_xa_regop_args *argp;
362
DB_TXNREGION *region;
365
if (argp->xid.size == 0)
368
mgr = dbenv->tx_handle;
369
region = mgr->reginfo.primary;
370
R_LOCK(dbenv, &mgr->reginfo);
372
/* Allocate a new transaction detail structure. */
374
__db_shalloc(mgr->reginfo.addr, sizeof(TXN_DETAIL), 0, &td)) != 0) {
375
R_UNLOCK(dbenv, &mgr->reginfo);
379
/* Place transaction on active transaction list. */
380
SH_TAILQ_INSERT_HEAD(®ion->active_txn, td, links, __txn_detail);
382
td->txnid = argp->txnid->txnid;
383
td->begin_lsn = argp->begin_lsn;
384
td->last_lsn = *lsnp;
386
td->status = TXN_PREPARED;
387
td->xa_status = TXN_XA_PREPARED;
388
memcpy(td->xid, argp->xid.data, argp->xid.size);
389
td->bqual = argp->bqual;
390
td->gtrid = argp->gtrid;
391
td->format = argp->formatID;
393
F_SET(td, TXN_RESTORED);
395
region->stat.st_nrestores++;
396
region->stat.st_nactive++;
397
if (region->stat.st_nactive > region->stat.st_maxnactive)
398
region->stat.st_maxnactive = region->stat.st_nactive;
399
R_UNLOCK(dbenv, &mgr->reginfo);
404
* __txn_recycle_recover --
405
* Recovery function for recycle.
407
* PUBLIC: int __txn_recycle_recover
408
* PUBLIC: __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
411
__txn_recycle_recover(dbenv, dbtp, lsnp, op, info)
418
__txn_recycle_args *argp;
422
(void)__txn_child_print(dbenv, dbtp, lsnp, op, info);
424
if ((ret = __txn_recycle_read(dbenv, dbtp->data, &argp)) != 0)
427
COMPQUIET(lsnp, NULL);
429
if ((ret = __db_txnlist_gen(dbenv, info,
430
DB_UNDO(op) ? -1 : 1, argp->min, argp->max)) != 0)
433
__os_free(dbenv, argp);