Debian Codegraph @

docs/programmer_reference/xa_build.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Building a Global Transaction Manager</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="xa.html" title="Chapter 13.  Distributed Transactions" />
11 <link rel="prev" href="ch13s02.html" title="Berkeley DB XA Implementation" />
12 <link rel="next" href="xa_xa_intro.html" title="XA Introduction" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Building a Global Transaction Manager</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="ch13s02.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 13. 
26 Distributed Transactions
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="xa_xa_intro.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="xa_build"></a>Building a Global Transaction Manager</h2>
38 </div>
39 </div>
40 </div>
41 <div class="toc">
42 <dl>
43 <dt>
44 <span class="sect2">
45 <a href="xa_build.html#idp52778488">Communicating with multiple Berkeley DB environments</a>
46 </span>
47 </dt>
48 <dt>
49 <span class="sect2">
50 <a href="xa_build.html#idp52779432">Recovering from GTM failure</a>
51 </span>
52 </dt>
53 <dt>
54 <span class="sect2">
55 <a href="xa_build.html#idp52703176">Managing the Global Transaction ID (GID) name space</a>
56 </span>
57 </dt>
58 <dt>
59 <span class="sect2">
60 <a href="xa_build.html#idp52758336">Maintaining state for each distributed transaction.</a>
61 </span>
62 </dt>
63 <dt>
64 <span class="sect2">
65 <a href="xa_build.html#idp52777008">Recovering from the failure of a single environment</a>
66 </span>
67 </dt>
68 <dt>
69 <span class="sect2">
70 <a href="xa_build.html#idp52779896">Recovering from GTM failure</a>
71 </span>
72 </dt>
73 </dl>
74 </div>
75 <p>Managing distributed transactions and using the two-phase commit
76 protocol of Berkeley DB from an application requires the application to provide
77 the functionality of a global transaction manager (GTM). The GTM is
78 responsible for the following:</p>
79 <div class="itemizedlist">
80 <ul type="disc">
81 <li>Communicating with the multiple environments (potentially on separate
82 systems).</li>
83 <li>Managing the global transaction ID name space.</li>
84 <li>Maintaining state information about each distributed transaction.</li>
85 <li>Recovering from failures of individual environments.</li>
86 <li>Recovering the global transaction state after failure of the global
87 transaction manager.</li>
88 </ul>
89 </div>
90 <div class="sect2" lang="en" xml:lang="en">
91 <div class="titlepage">
92 <div>
93 <div>
94 <h3 class="title"><a id="idp52778488"></a>Communicating with multiple Berkeley DB environments</h3>
95 </div>
96 </div>
97 </div>
98 <p>Two-phase commit is required if a transaction spans operations
99 in multiple Berkeley DB environments or across operations in Berkeley DB
100 and some other database systems. If the environments
101 reside on the same machine, the application can communicate with each
102 environment through its own address space with no additional complexity.
103 If the environments reside on separate machines, the application
104
105 may use its own messaging capability, translating messages on the remote
106 machine into calls into the Berkeley DB library (including the recovery
107 calls). </p>
108 </div>
109 <div class="sect2" lang="en" xml:lang="en">
110 <div class="titlepage">
111 <div>
112 <div>
113 <h3 class="title"><a id="idp52779432"></a>Recovering from GTM failure</h3>
114 </div>
115 </div>
116 </div>
117 <p>If the GTM fails, it must first recover its local state. Assuming the
118 GTM uses Berkeley DB tables to maintain state, it should run the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility (or the DB_RECOVER option to <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a>) upon startup. Once the GTM is back up and running, it needs to review all its outstanding global transactions, that is, all transactions that are recorded but not yet completed.</p>
119 <p>
120 The global transactions that have not yet reached the prepare phase should be aborted. For each global transaction that has not yet prepared, the GTM should send a message to each participant telling it to abort its transaction.</p>
121 <p>Next the GTM should review its log to identify all participating environments that have transactions in the preparing, aborting, or committing states. For each such participant, the GTM should issue a <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> call. Upon receiving responses from each participant, the GTM must decide the fate of each transaction and issue appropriate calls. The correct behavior is defined as follows:</p>
122 <div class="variablelist">
123 <dl>
124 <dt>
125 <span class="term">preparing</span>
126 </dt>
127 <dd>if all participating environments return the transaction in the list of prepared but not yet completed transactions, then the GTM should commit the transaction. If any participating environment fails to return the transaction in this list, then the GTM must issue an abort to all environments participating in that global transaction.</dd>
128 <dt>
129 <span class="term">committing</span>
130 </dt>
131 <dd>the GTM should send a commit to any environment that returned this
132 transaction in its list of prepared but not yet completed
133 transactions.</dd>
134 <dt>
135 <span class="term">aborting</span>
136 </dt>
137 <dd>the GTM should send an abort to any environment that returned this
138 transaction in its list of prepared but not yet completed
139 transactions.</dd>
140 </dl>
141 </div>
142 </div>
143 <div class="sect2" lang="en" xml:lang="en">
144 <div class="titlepage">
145 <div>
146 <div>
147 <h3 class="title"><a id="idp52703176"></a>Managing the Global Transaction ID (GID) name space</h3>
148 </div>
149 </div>
150 </div>
151 <p>A global transaction is a transaction that spans multiple environments.
152 Each global transaction must have a unique transaction ID. This unique
153 ID is the global transaction ID (GID). In Berkeley DB, global transaction IDs
154 must be represented by the character array, <code class="literal">DB_GID_SIZE</code> (currently 128 bytes).
155 It is the responsibility of the
156 global transaction manager to assign GIDs, guarantee their uniqueness,
157 and manage the mapping of local transactions to GID. That is, for each
158 GID, the GTM should know which local transaction managers participated.
159 The Berkeley DB logging system or a Berkeley DB table could be used to record this
160 information.</p>
161 </div>
162 <div class="sect2" lang="en" xml:lang="en">
163 <div class="titlepage">
164 <div>
165 <div>
166 <h3 class="title"><a id="idp52758336"></a>Maintaining state for each distributed transaction.</h3>
167 </div>
168 </div>
169 </div>
170 <p>In addition to knowing which local environments participate in each
171 global transaction, the GTM must also know the state of each active
172 global transaction. As soon as a transaction becomes distributed (that
173 is, a second environment participates), the GTM must record the
174 existence of the global transaction and all participants (whether this
175 must reside on stable storage or not depends on the exact configuration
176 of the system). As new environments participate, the GTM must keep this
177 information up to date.</p>
178 <p>When the GTM is ready to begin commit processing, it should issue
179 <a href="../api_reference/C/txnprepare.html" class="olink">DB_TXN-&gt;prepare()</a> calls to each participating environment, indicating
180 the GID of the global transaction. Once all the participants have
181 successfully prepared, then the GTM must record that the global
182 transaction will be committed. This record should go to stable
183 storage. Once written to stable storage, the GTM can send
184 <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a> requests to each participating environment. Once
185 all environments have successfully completed the commit, the GTM can
186 either record the successful commit or can somehow "forget" the global
187 transaction.</p>
188 <p>
189 If an application uses nested transactions (that is, the
190 parent parameter is non-NULL in a call to <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV-&gt;txn_begin()</a>)
191 then, only the parent transaction should call <a href="../api_reference/C/txnprepare.html" class="olink">DB_TXN-&gt;prepare()</a>, not any of the child transactions.
192 </p>
193 <p>Should any participant fail to prepare, then the GTM must abort the
194 global transaction. The fact that the transaction is going to be
195 aborted should be written to stable storage. Once written, the GTM can
196 then issue <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN-&gt;abort()</a> requests to each environment. When all
197 aborts have returned successfully, the GTM can either record the
198 successful abort or "forget" the global transaction.</p>
199 <p>In summary, for each transaction, the GTM must maintain the following:</p>
200 <div class="itemizedlist">
201 <ul type="disc">
202 <li>A list of participating environments</li>
203 <li>The current state of each transaction (pre-prepare, preparing,
204 committing, aborting, done)</li>
205 </ul>
206 </div>
207 </div>
208 <div class="sect2" lang="en" xml:lang="en">
209 <div class="titlepage">
210 <div>
211 <div>
212 <h3 class="title"><a id="idp52777008"></a>Recovering from the failure of a single environment</h3>
213 </div>
214 </div>
215 </div>
216 <p>If a single environment fails, there is no need to bring down or recover other environments (the only exception to this is if all environments are managed in the same application address space and there is a risk that the failure of the environment corrupted other environments). Instead, once the failing environment comes back up, it should be recovered (that is, conventional recovery, via the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility or by specifying the <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> flag to <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> should be run). If the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility is used, then the -e option must be specified. In this case, the application will almost certainly want to specify environmental parameters via a <a class="xref" href="env_db_config.html" title="DB_CONFIG configuration file">DB_CONFIG configuration file</a> in the environment's home directory, so that the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility can create an appropriately configured environment.
217 If the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility is not used, then the GTM should call <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> specifying the <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> flag. It should then call <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a>, which will return an array of DB_TXN handles for the set of prepared, but not yet completed transactions. For each transaction, the GTM should combine this knowledge with its transaction state table and call either <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a> or <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN-&gt;abort()</a>. After that process is complete, the environment is ready to participate in new transactions.
218 </p>
219 <p>If the GTM is running in a system with multiple GTMs, it is possible that some of the transactions returned via <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> do not belong to the current environment. The GTM should detect this and call <a href="../api_reference/C/txndiscard.html" class="olink">DB_TXN-&gt;discard()</a> on each such transaction handle. Furthermore, it is important to note the environment does not retain information about which GTM has issued <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> operations. Therefore, each GTM should issue all its <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> calls before another GTM issues its calls. If the calls are interleaved, each GTM may not get a complete and consistent set of transactions. The simplest way to enforce this is for each GTM to make sure it can receive all its outstanding transactions in a single <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> call. The maximum number of possible outstanding transactions is roughly the maximum number of active transactions in the environment (whose value can be obtained using the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility). To simplify this procedure, the caller should allocate an array large enough to hold the list of transactions (for example, allocate an array able to hold three times the maximum number of transactions). If that is not possible, callers should check that the array was not completely filled in when <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> returns. If the array was completely filled in, each transaction should be explicitly discarded, and the call repeated with a larger array.</p>
220 <p>The newly recovered environment will forbid any new transactions from being started until the prepared but not yet completed transactions have been resolved. In the multiple GTM case, this means that all GTMs must recover before any GTM can begin issuing new transactions.</p>
221 <p>
222 The GTM must determine how long it needs to retain global transaction commit and abort records. If the participating environments are following a DB_TXN_SYNC policy, that is, they are forcing commit and abort records to disk before replying to the GTM, then once the GTM has heard from all participants, it need not retain its persistent log records. However, if participating environments are running at weaker durability levels, such as <a href="../api_reference/C/envset_flags.html#set_flags_DB_TXN_WRITE_NOSYNC" class="olink">DB_TXN_WRITE_NOSYNC</a> or <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a>, then the GTM must retain all commit and abort records until all participants have completed a checkpoint following the completion of a transaction.
223 </p>
224 </div>
225 <div class="sect2" lang="en" xml:lang="en">
226 <div class="titlepage">
227 <div>
228 <div>
229 <h3 class="title"><a id="idp52779896"></a>Recovering from GTM failure</h3>
230 </div>
231 </div>
232 </div>
233 <p>If the GTM fails, it must first recover its local state. Assuming the
234 GTM uses Berkeley DB tables to maintain state, it should run the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility (or the DB_RECOVER option to <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a>) upon startup. Once the GTM is back up and running, it needs to review all its outstanding global transactions, that is, all transactions that are recorded but not yet completed.</p>
235 <p>
236 The global transactions that have not yet reached the prepare phase should be aborted. For each global transaction that has not yet prepared, the GTM should send a message to each participant telling it to abort its transaction.</p>
237 <p>Next the GTM should review its log to identify all participating environments that have transactions in the preparing, aborting, or committing states. For each such participant, the GTM should issue a <a href="../api_reference/C/txnrecover.html" class="olink">DB_ENV-&gt;txn_recover()</a> call. Upon receiving responses from each participant, the GTM must decide the fate of each transaction and issue appropriate calls. The correct behavior is defined as follows:</p>
238 <div class="variablelist">
239 <dl>
240 <dt>
241 <span class="term">preparing</span>
242 </dt>
243 <dd>if all participating environments return the transaction in the list of prepared but not yet completed transactions, then the GTM should commit the transaction. If any participating environment fails to return the transaction in this list, then the GTM must issue an abort to all environments participating in that global transaction.</dd>
244 <dt>
245 <span class="term">committing</span>
246 </dt>
247 <dd>the GTM should send a commit to any environment that returned this
248 transaction in its list of prepared but not yet completed
249 transactions.</dd>
250 <dt>
251 <span class="term">aborting</span>
252 </dt>
253 <dd>the GTM should send an abort to any environment that returned this
254 transaction in its list of prepared but not yet completed
255 transactions.</dd>
256 </dl>
257 </div>
258 </div>
259 </div>
260 <div class="navfooter">
261 <hr />
262 <table width="100%" summary="Navigation footer">
263 <tr>
264 <td width="40%" align="left"><a accesskey="p" href="ch13s02.html">Prev</a> </td>
265 <td width="20%" align="center">
266 <a accesskey="u" href="xa.html">Up</a>
267 </td>
268 <td width="40%" align="right"> <a accesskey="n" href="xa_xa_intro.html">Next</a></td>
269 </tr>
270 <tr>
271 <td width="40%" align="left" valign="top">Berkeley DB XA Implementation </td>
272 <td width="20%" align="center">
273 <a accesskey="h" href="index.html">Home</a>
274 </td>
275 <td width="40%" align="right" valign="top"> XA Introduction</td>
276 </tr>
277 </table>
278 </div>
279 </body>
280 </html>