2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char sccsid
[] = "@(#)txn_rec.c 10.15 (Sleepycat) 1/3/99";
46 #ifndef NO_SYSTEM_INCLUDES
47 #include <sys/types.h>
58 #include "common_ext.h"
60 static int __txn_restore_txn
__P((DB_ENV
*, DB_LSN
*, __txn_xa_regop_args
*));
62 #define IS_XA_TXN(R) (R->xid.size != 0)
65 * PUBLIC: int __txn_regop_recover
66 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
68 * These records are only ever written for commits.
71 __txn_regop_recover(logp
, dbtp
, lsnp
, redo
, info
)
78 __txn_regop_args
*argp
;
82 (void)__txn_regop_print(logp
, dbtp
, lsnp
, redo
, info
);
85 COMPQUIET(logp
, NULL
);
87 if ((ret
= __txn_regop_read(dbtp
->data
, &argp
)) != 0)
90 if (argp
->opcode
!= TXN_COMMIT
)
93 if (__db_txnlist_find(info
, argp
->txnid
->txnid
) == DB_NOTFOUND
)
94 ret
= __db_txnlist_add(info
, argp
->txnid
->txnid
);
97 *lsnp
= argp
->prev_lsn
;
104 * PUBLIC: int __txn_xa_regop_recover
105 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
107 * These records are only ever written for prepares.
110 __txn_xa_regop_recover(logp
, dbtp
, lsnp
, redo
, info
)
117 __txn_xa_regop_args
*argp
;
121 (void)__txn_xa_regop_print(logp
, dbtp
, lsnp
, redo
, info
);
124 COMPQUIET(logp
, NULL
);
126 if ((ret
= __txn_xa_regop_read(dbtp
->data
, &argp
)) != 0)
129 if (argp
->opcode
!= TXN_PREPARE
)
133 * Whether we are in XA or not, we need to call
134 * __db_txnlist_find so that we update the maxid.
135 * If this is an XA transaction, then we treat
136 * prepares like commits so that we roll forward to
137 * a point where we can handle commit/abort calls
138 * from the TMS. If this isn't XA, then a prepare
139 * is treated like a No-op; we only care about the
142 ret
= __db_txnlist_find(info
, argp
->txnid
->txnid
);
143 if (IS_XA_TXN(argp
) && ret
== DB_NOTFOUND
) {
145 * This is an XA prepared, but not yet committed
146 * transaction. We need to add it to the
147 * transaction list, so that it gets rolled
148 * forward. We also have to add it to the region's
149 * internal state so it can be properly aborted
152 ret
= __db_txnlist_add(info
, argp
->txnid
->txnid
);
154 ret
= __txn_restore_txn(logp
->dbenv
,
160 *lsnp
= argp
->prev_lsn
;
167 * PUBLIC: int __txn_ckp_recover __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
170 __txn_ckp_recover(logp
, dbtp
, lsnp
, redo
, info
)
177 __txn_ckp_args
*argp
;
181 __txn_ckp_print(logp
, dbtp
, lsnp
, redo
, info
);
183 COMPQUIET(logp
, NULL
);
185 if ((ret
= __txn_ckp_read(dbtp
->data
, &argp
)) != 0)
189 * Check for 'restart' checkpoint record. This occurs when the
190 * checkpoint lsn is equal to the lsn of the checkpoint record
191 * and means that we could set the transaction ID back to 1, so
192 * that we don't exhaust the transaction ID name space.
194 if (argp
->ckp_lsn
.file
== lsnp
->file
&&
195 argp
->ckp_lsn
.offset
== lsnp
->offset
)
196 __db_txnlist_gen(info
, redo
? -1 : 1);
198 *lsnp
= argp
->last_ckp
;
204 * __txn_child_recover
205 * Recover a commit record for a child transaction.
207 * PUBLIC: int __txn_child_recover
208 * PUBLIC: __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
211 __txn_child_recover(logp
, dbtp
, lsnp
, redo
, info
)
218 __txn_child_args
*argp
;
222 (void)__txn_child_print(logp
, dbtp
, lsnp
, redo
, info
);
225 COMPQUIET(logp
, NULL
);
227 if ((ret
= __txn_child_read(dbtp
->data
, &argp
)) != 0)
231 * We count the child as committed only if its parent committed.
232 * So, if we are not yet in the transaction list, but our parent
233 * is, then we should go ahead and commit.
235 if (argp
->opcode
!= TXN_COMMIT
)
238 if (__db_txnlist_find(info
, argp
->parent
) == 0 &&
239 __db_txnlist_find(info
, argp
->txnid
->txnid
) == DB_NOTFOUND
)
240 ret
= __db_txnlist_add(info
, argp
->txnid
->txnid
);
243 *lsnp
= argp
->prev_lsn
;
250 * __txn_restore_txn --
251 * Using only during XA recovery. If we find any transactions that are
252 * prepared, but not yet committed, then we need to restore the transaction's
253 * state into the shared region, because the TM is going to issue a txn_abort
254 * or txn_commit and we need to respond correctly.
256 * lsnp is the LSN of the returned LSN
257 * argp is the perpare record (in an appropriate structure)
260 __txn_restore_txn(dbenv
, lsnp
, argp
)
263 __txn_xa_regop_args
*argp
;
269 if (argp
->xid
.size
== 0)
272 mgr
= dbenv
->tx_info
;
275 /* Allocate a new transaction detail structure. */
276 if ((ret
= __db_shalloc(mgr
->mem
, sizeof(TXN_DETAIL
), 0, &td
)) != 0)
279 /* Place transaction on active transaction list. */
280 SH_TAILQ_INSERT_HEAD(&mgr
->region
->active_txn
, td
, links
, __txn_detail
);
282 td
->txnid
= argp
->txnid
->txnid
;
283 td
->begin_lsn
= argp
->begin_lsn
;
284 td
->last_lsn
= *lsnp
;
287 td
->status
= TXN_PREPARED
;
288 td
->xa_status
= TXN_XA_PREPARED
;
289 memcpy(td
->xid
, argp
->xid
.data
, argp
->xid
.size
);
290 td
->bqual
= argp
->bqual
;
291 td
->gtrid
= argp
->gtrid
;
292 td
->format
= argp
->formatID
;
294 UNLOCK_TXNREGION(mgr
);