1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: txn.h,v 12.20 2008/01/08 20:58:18 bostic Exp $
7 */
8
9#ifndef	_DB_TXN_H_
10#define	_DB_TXN_H_
11
12#include "dbinc/xa.h"
13
14#if defined(__cplusplus)
15extern "C" {
16#endif
17
18/* Operation parameters to the delayed commit processing code. */
19typedef enum {
20	TXN_CLOSE,		/* Close a DB handle whose close had failed. */
21	TXN_REMOVE,		/* Remove a file. */
22	TXN_TRADE,		/* Trade lockers. */
23	TXN_TRADED		/* Already traded; downgrade lock. */
24} TXN_EVENT_T;
25
26struct __db_txnregion;	typedef struct __db_txnregion DB_TXNREGION;
27struct __txn_logrec;	typedef struct __txn_logrec DB_TXNLOGREC;
28
29/*
30 * !!!
31 * TXN_MINIMUM = (DB_LOCK_MAXID + 1) but this makes compilers complain.
32 */
33#define	TXN_MINIMUM	0x80000000
34#define	TXN_MAXIMUM	0xffffffff	/* Maximum number of txn ids. */
35#define	TXN_INVALID	0		/* Invalid transaction ID. */
36
37#define	DEF_MAX_TXNS	100		/* Default max transactions. */
38#define	TXN_NSLOTS	4		/* Initial slots to hold DB refs */
39
40/*
41 * Internal data maintained in shared memory for each transaction.
42 */
43typedef struct __txn_detail {
44	u_int32_t txnid;		/* current transaction id
45					   used to link free list also */
46	pid_t pid;			/* Process owning txn */
47	db_threadid_t tid;		/* Thread owning txn */
48
49	DB_LSN	last_lsn;		/* Last LSN written for this txn. */
50	DB_LSN	begin_lsn;		/* LSN of begin record. */
51	roff_t	parent;			/* Offset of transaction's parent. */
52	roff_t	name;			/* Offset of txn name. */
53
54	u_int32_t	nlog_dbs;	/* Number of databases used. */
55	u_int32_t	nlog_slots;	/* Number of allocated slots. */
56	roff_t		log_dbs;	/* Databases used. */
57
58	DB_LSN	read_lsn;		/* Read LSN for MVCC. */
59	DB_LSN	visible_lsn;		/* LSN at which this transaction's
60					   changes are visible. */
61	db_mutex_t	mvcc_mtx;	/* Version mutex. */
62	u_int32_t	mvcc_ref;	/* Number of buffers created by this
63					   transaction still in cache.  */
64
65	SH_TAILQ_HEAD(__tdkids)	kids;	/* Linked list of child txn detail. */
66	SH_TAILQ_ENTRY		klinks;
67
68	/* TXN_{ABORTED, COMMITTED PREPARED, RUNNING} */
69	u_int32_t status;		/* status of the transaction */
70
71#define	TXN_DTL_COLLECTED	0x1	/* collected during txn_recover */
72#define	TXN_DTL_RESTORED	0x2	/* prepared txn restored */
73#define	TXN_DTL_INMEMORY	0x4	/* uses in memory logs */
74	u_int32_t flags;
75
76	/* TXN_XA_{ABORTED, DEADLOCKED, ENDED, PREPARED, STARTED, SUSPENDED} */
77	SH_TAILQ_ENTRY	links;		/* active/free/snapshot list */
78
79	u_int32_t xa_status;		/* XA status */
80
81	/*
82	 * XID (xid_t) structure: because these fields are logged, the
83	 * sizes have to be explicit.
84	 */
85	u_int8_t xid[XIDDATASIZE];	/* XA global transaction id */
86	u_int32_t bqual;		/* bqual_length from XID */
87	u_int32_t gtrid;		/* gtrid_length from XID */
88	int32_t format;			/* XA format */
89	roff_t slots[TXN_NSLOTS];	/* Initial DB slot allocation. */
90} TXN_DETAIL;
91
92/*
93 * DB_TXNMGR --
94 *	The transaction manager encapsulates the transaction system.
95 */
96struct __db_txnmgr {
97	/*
98	 * These fields need to be protected for multi-threaded support.
99	 *
100	 * Lock list of active transactions (including the content of each
101	 * TXN_DETAIL structure on the list).
102	 */
103	db_mutex_t mutex;
104					/* List of active transactions. */
105	TAILQ_HEAD(_chain, __db_txn)	txn_chain;
106
107	u_int32_t n_discards;		/* Number of txns discarded. */
108
109	/* These fields are never updated after creation, so not protected. */
110	ENV	*env;			/* Environment. */
111	REGINFO	 reginfo;		/* Region information. */
112};
113
114/* Macros to lock/unlock the transaction region as a whole. */
115#define	TXN_SYSTEM_LOCK(env)						\
116	MUTEX_LOCK(env, ((DB_TXNREGION *)				\
117	    (env)->tx_handle->reginfo.primary)->mtx_region)
118#define	TXN_SYSTEM_UNLOCK(env)						\
119	MUTEX_UNLOCK(env, ((DB_TXNREGION *)				\
120	    (env)->tx_handle->reginfo.primary)->mtx_region)
121
122/*
123 * DB_TXNREGION --
124 *	The primary transaction data structure in the shared memory region.
125 */
126struct __db_txnregion {
127	db_mutex_t	mtx_region;	/* Region mutex. */
128
129	u_int32_t	maxtxns;	/* maximum number of active TXNs */
130	u_int32_t	last_txnid;	/* last transaction id given out */
131	u_int32_t	cur_maxid;	/* current max unused id. */
132
133	db_mutex_t	mtx_ckp;	/* Single thread checkpoints. */
134	DB_LSN		last_ckp;	/* lsn of the last checkpoint */
135	time_t		time_ckp;	/* time of last checkpoint */
136
137	DB_TXN_STAT	stat;		/* Statistics for txns. */
138
139#define	TXN_IN_RECOVERY	 0x01		/* environment is being recovered */
140	u_int32_t	flags;
141					/* active TXN list */
142	SH_TAILQ_HEAD(__active) active_txn;
143	SH_TAILQ_HEAD(__mvcc) mvcc_txn;
144};
145
146/*
147 * DB_TXNLOGREC --
148 *	An in-memory, linked-list copy of a log record.
149 */
150struct __txn_logrec {
151	STAILQ_ENTRY(__txn_logrec) links;/* Linked list. */
152
153	u_int8_t data[1];		/* Log record. */
154};
155
156/*
157 * Log record types.  Note that these are *not* alphabetical.  This is
158 * intentional so that we don't change the meaning of values between
159 * software upgrades.
160 *
161 * EXPECTED, UNEXPECTED, IGNORE, and OK are used in the txnlist functions.
162 * Here is an explanation of how the statuses are used.
163 *
164 * TXN_OK
165 *	BEGIN records for transactions found on the txnlist during
166 *	OPENFILES (BEGIN records are those with a prev_lsn of 0,0)
167 *
168 * TXN_COMMIT
169 *	Transaction committed and should be rolled forward.
170 *
171 * TXN_ABORT
172 *	This transaction's changes must be undone.  Either there was
173 *	never a prepare or commit record for this transaction OR there
174 *	was a commit, but we are recovering to a timestamp or particular
175 *	LSN and that point is before this transaction's commit.
176 *
177 * TXN_PREPARE
178 *	Prepare record, but no commit record is in the log.
179 *
180 * TXN_IGNORE
181 *	Generic meaning is that this transaction should not be
182 *	processed during later recovery passes.  We use it in a
183 *	number of different manners:
184 *
185 *	1. We never saw its BEGIN record.  Therefore, the logs have
186 *	   been reclaimed and we *know* that this transaction doesn't
187 *	   need to be aborted, because in order for it to be
188 *	   reclaimed, there must have been a subsequent checkpoint
189 *	   (and any dirty pages for this transaction made it to
190 *	   disk).
191 *
192 *	2. This is a child transaction that created a database.
193 *	   For some reason, we don't want to recreate that database
194 *	   (i.e., it already exists or some other database created
195 *	   after it exists).
196 *
197 *	3. During recovery open of subdatabases, if the master check fails,
198 *	   we use a TXN_IGNORE on the create of the subdb in the nested
199 *	   transaction.
200 *
201 *	4. During a remove, the file with the name being removed isn't
202 *	   the file for which we are recovering a remove.
203 *
204 * TXN_EXPECTED
205 *	After a successful open during recovery, we update the
206 *	transaction's status to TXN_EXPECTED.  The open was done
207 *	in the parent, but in the open log record, we record the
208 *	child transaction's ID if we also did a create.  When there
209 *	is a valid ID in that field, we use it and mark the child's
210 *	status as TXN_EXPECTED (indicating that we don't need to redo
211 *	a create for this file).
212 *
213 *	When recovering a remove, if we don't find or can't open
214 *	the file, the child (which does the remove) gets marked
215 *	EXPECTED (indicating that we don't need to redo the remove).
216 *
217 * TXN_UNEXPECTED
218 *	During recovery, we attempted an open that should have succeeded
219 *	and we got ENOENT, so like with the EXPECTED case, we indicate
220 *	in the child that we got the UNEXPECTED return so that we do redo
221 *	the creating/deleting operation.
222 *
223 */
224#define	TXN_OK		0
225#define	TXN_COMMIT	1
226#define	TXN_PREPARE	2
227#define	TXN_ABORT	3
228#define	TXN_IGNORE	4
229#define	TXN_EXPECTED	5
230#define	TXN_UNEXPECTED	6
231
232#if defined(__cplusplus)
233}
234#endif
235
236#include "dbinc_auto/txn_auto.h"
237#include "dbinc_auto/txn_ext.h"
238#include "dbinc_auto/xa_ext.h"
239#endif /* !_DB_TXN_H_ */
240