| 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>Designing Your Application for Recovery</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="Getting Started with Berkeley DB Transaction Processing" /> |
| 10 |
<link rel="up" href="filemanagement.html" title="Chapter 5. Managing DB Files" /> |
| 11 |
<link rel="prev" href="recovery.html" title="Recovery Procedures" /> |
| 12 |
<link rel="next" href="hotfailover.html" title="Using Hot Failovers" /> |
| 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">Designing Your Application for Recovery</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="recovery.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center">Chapter 5. Managing DB Files</th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="hotfailover.html">Next</a></td> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="sect1" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title" style="clear: both"><a id="architectrecovery"></a>Designing Your Application for Recovery</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<dl> |
| 41 |
<dt> |
| 42 |
<span class="sect2"> |
| 43 |
<a href="architectrecovery.html#multithreadrecovery">Recovery for Multi-Threaded Applications</a> |
| 44 |
</span> |
| 45 |
</dt> |
| 46 |
<dt> |
| 47 |
<span class="sect2"> |
| 48 |
<a href="architectrecovery.html#multiprocessrecovery">Recovery in Multi-Process Applications</a> |
| 49 |
</span> |
| 50 |
</dt> |
| 51 |
</dl> |
| 52 |
</div> |
| 53 |
<p> |
| 54 |
When building your DB application, you should consider how you will run recovery. If you are building a |
| 55 |
single threaded, single process application, it is fairly simple to run recovery when your application first |
| 56 |
opens its environment. In this case, you need only decide if you want to run recovery every time you open |
| 57 |
your application (recommended) or only some of the time, presumably triggered by a start up option |
| 58 |
controlled by your application's user. |
| 59 |
</p> |
| 60 |
<p> |
| 61 |
However, for multi-threaded and multi-process applications, you need to carefully consider how you will |
| 62 |
design your application's startup code so as to run recovery only when it makes sense to do so. |
| 63 |
</p> |
| 64 |
<div class="sect2" lang="en" xml:lang="en"> |
| 65 |
<div class="titlepage"> |
| 66 |
<div> |
| 67 |
<div> |
| 68 |
<h3 class="title"><a id="multithreadrecovery"></a>Recovery for Multi-Threaded Applications</h3> |
| 69 |
</div> |
| 70 |
</div> |
| 71 |
</div> |
| 72 |
<p> |
| 73 |
If your application uses only one environment handle, then handling recovery for a multi-threaded |
| 74 |
application is no more difficult than for a single threaded application. You simply open the environment |
| 75 |
in the application's main thread, and then pass that handle to each of the threads that will be |
| 76 |
performing DB operations. We illustrate this with our final example in this book (see |
| 77 |
<a class="xref" href="txnexample_c.html" title="Transaction Example">Transaction Example</a> |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
for more information). |
| 82 |
</p> |
| 83 |
<p> |
| 84 |
Alternatively, you can have each worker thread open its own environment handle. However, in this case, |
| 85 |
designing for recovery is a bit more complicated. |
| 86 |
</p> |
| 87 |
<p> |
| 88 |
Generally, when a thread performing database operations fails |
| 89 |
or hangs, it is frequently best to simply |
| 90 |
restart the application and run recovery upon application |
| 91 |
startup as normal. However, not all applications can afford |
| 92 |
to restart because a single thread has misbehaved. |
| 93 |
</p> |
| 94 |
<p> |
| 95 |
If you are attempting to continue operations in the face of a misbehaving thread, |
| 96 |
then at a minimum recovery must be run if a thread performing database operations fails or hangs. |
| 97 |
</p> |
| 98 |
<p> |
| 99 |
Remember that recovery clears the environment of all |
| 100 |
outstanding locks, including any that might be outstanding |
| 101 |
from an aborted thread. If these locks are not cleared, |
| 102 |
other threads performing database operations can back up |
| 103 |
behind the locks obtained but never cleared by the failed |
| 104 |
thread. The result will be an application that hangs |
| 105 |
indefinitely. |
| 106 |
</p> |
| 107 |
<p> |
| 108 |
To run recovery under these circumstances: |
| 109 |
</p> |
| 110 |
<div class="orderedlist"> |
| 111 |
<ol type="1"> |
| 112 |
<li> |
| 113 |
<p> |
| 114 |
Suspend or shutdown all other threads performing |
| 115 |
database operations. |
| 116 |
</p> |
| 117 |
</li> |
| 118 |
<li> |
| 119 |
<p> |
| 120 |
Discarding any open environment handles. Note that |
| 121 |
attempting to gracefully close these handles may be |
| 122 |
asking for trouble; the close can fail if the |
| 123 |
environment is already in need of recovery. For |
| 124 |
this reason, it is best and easiest to simply discard the handle. |
| 125 |
</p> |
| 126 |
</li> |
| 127 |
<li> |
| 128 |
<p> |
| 129 |
Open new handles, running recovery as you open |
| 130 |
them. |
| 131 |
See <a class="xref" href="recovery.html#normalrecovery" title="Normal Recovery">Normal Recovery</a> for more information. |
| 132 |
</p> |
| 133 |
</li> |
| 134 |
<li> |
| 135 |
<p> |
| 136 |
Restart all your database threads. |
| 137 |
</p> |
| 138 |
</li> |
| 139 |
</ol> |
| 140 |
</div> |
| 141 |
<p> |
| 142 |
A traditional way to handle this activity is to spawn a watcher thread that is responsible for making |
| 143 |
sure all is well with your threads, and performing the above actions if not. |
| 144 |
</p> |
| 145 |
<p> |
| 146 |
However, in the case where each worker thread opens and maintains its own environment handle, recovery |
| 147 |
is complicated for two reasons: |
| 148 |
</p> |
| 149 |
<div class="orderedlist"> |
| 150 |
<ol type="1"> |
| 151 |
<li> |
| 152 |
<p> |
| 153 |
For some applications and workloads, it might be |
| 154 |
worthwhile to give your database threads the |
| 155 |
ability to gracefully finalize any on-going |
| 156 |
transactions. If this is the case, your |
| 157 |
code must be capable of signaling each thread |
| 158 |
to halt DB activities and close its |
| 159 |
environment. If you simply run recovery against the |
| 160 |
environment, your database threads will |
| 161 |
detect this and fail in the midst of performing their |
| 162 |
database operations. |
| 163 |
</p> |
| 164 |
</li> |
| 165 |
<li> |
| 166 |
<p> |
| 167 |
Your code must be capable of ensuring only one |
| 168 |
thread runs recovery before allowing all other |
| 169 |
threads to open their respective environment |
| 170 |
handles. Recovery should be single threaded because when |
| 171 |
recovery is run against an environment, it is |
| 172 |
deleted and then recreated. This will cause all |
| 173 |
other processes and threads to "fail" when they |
| 174 |
attempt operations against the newly recovered |
| 175 |
environment. If all threads run recovery |
| 176 |
when they start up, then it is likely that some |
| 177 |
threads will fail because the environment that they |
| 178 |
are using has been recovered. This will cause the thread to have to re-execute its own recovery |
| 179 |
path. At best, this is inefficient and at worst it could cause your application to fall into an |
| 180 |
endless recovery pattern. |
| 181 |
</p> |
| 182 |
</li> |
| 183 |
</ol> |
| 184 |
</div> |
| 185 |
</div> |
| 186 |
<div class="sect2" lang="en" xml:lang="en"> |
| 187 |
<div class="titlepage"> |
| 188 |
<div> |
| 189 |
<div> |
| 190 |
<h3 class="title"><a id="multiprocessrecovery"></a>Recovery in Multi-Process Applications</h3> |
| 191 |
</div> |
| 192 |
</div> |
| 193 |
</div> |
| 194 |
<p> |
| 195 |
Frequently, DB applications use multiple processes to interact with the databases. For example, you may |
| 196 |
have a long-running process, such as some kind of server, and then a series of administrative tools that |
| 197 |
you use to inspect and administer the underlying databases. Or, in some web-based architectures, different |
| 198 |
services are run as independent processes that are managed by the server. |
| 199 |
</p> |
| 200 |
<p> |
| 201 |
In any case, recovery for a multi-process environment is complicated for two reasons: |
| 202 |
</p> |
| 203 |
<div class="orderedlist"> |
| 204 |
<ol type="1"> |
| 205 |
<li> |
| 206 |
<p> |
| 207 |
In the event that recovery must be run, you might |
| 208 |
want to notify processes interacting with the environment |
| 209 |
that recovery is about to occur and give them a |
| 210 |
chance to gracefully terminate. Whether it is |
| 211 |
worthwhile for you to do this is entirely dependent |
| 212 |
upon the nature of your application. Some |
| 213 |
long-running applications with multiple processes |
| 214 |
performing meaningful work might want to do this. |
| 215 |
Other applications with processes performing database |
| 216 |
operations that are likely to be harmed by error conditions in other |
| 217 |
processes will likely find it to be not worth the |
| 218 |
effort. For this latter group, the chances of |
| 219 |
performing a graceful shutdown may be low anyway. |
| 220 |
</p> |
| 221 |
</li> |
| 222 |
<li> |
| 223 |
<p> |
| 224 |
Unlike single process scenarios, it can quickly become wasteful for every process interacting |
| 225 |
with the databases to run recovery when it starts up. This is partly because recovery |
| 226 |
<span class="emphasis"><em>does</em></span> take some amount of time to run, but mostly you want to |
| 227 |
avoid a situation where your server must |
| 228 |
reopen all its environment handles just because you fire up a command line database |
| 229 |
administrative utility that always runs recovery. |
| 230 |
</p> |
| 231 |
</li> |
| 232 |
</ol> |
| 233 |
</div> |
| 234 |
<p> |
| 235 |
DB offers you two methods by which you can manage recovery for multi-process DB applications. |
| 236 |
Each has different strengths and weaknesses, and they are described in the next sections. |
| 237 |
</p> |
| 238 |
<div class="sect3" lang="en" xml:lang="en"> |
| 239 |
<div class="titlepage"> |
| 240 |
<div> |
| 241 |
<div> |
| 242 |
<h4 class="title"><a id="mp_recover_effects"></a>Effects of Multi-Process Recovery</h4> |
| 243 |
</div> |
| 244 |
</div> |
| 245 |
</div> |
| 246 |
<p> |
| 247 |
Before continuing, it is worth noting that the following sections describe recovery processes than |
| 248 |
can result in one process running recovery while other processes are currently actively performing |
| 249 |
database operations. |
| 250 |
</p> |
| 251 |
<p> |
| 252 |
When this happens, the current database operation will |
| 253 |
abnormally fail, indicating a DB_RUNRECOVERY condition. |
| 254 |
This means that your application should immediately abandon any database operations that it may have |
| 255 |
on-going, discard any environment handles it has opened, and obtain and open new handles. |
| 256 |
</p> |
| 257 |
<p> |
| 258 |
The net effect of this is that any writes performed by unresolved transactions will be lost. |
| 259 |
For persistent applications (servers, for example), the services it provides will also be |
| 260 |
unavailable for the amount of time that it takes to complete a recovery and for all participating |
| 261 |
processes to reopen their environment handles. |
| 262 |
</p> |
| 263 |
</div> |
| 264 |
<div class="sect3" lang="en" xml:lang="en"> |
| 265 |
<div class="titlepage"> |
| 266 |
<div> |
| 267 |
<div> |
| 268 |
<h4 class="title"><a id="db_register"></a>Process Registration</h4> |
| 269 |
</div> |
| 270 |
</div> |
| 271 |
</div> |
| 272 |
<p> |
| 273 |
One way to handle multi-process recovery is for every process to "register" its environment. In |
| 274 |
doing so, the process gains the ability to see if any other applications are using the |
| 275 |
environment and, if so, whether they have suffered an abnormal termination. If an abnormal |
| 276 |
termination is detected, the process runs recovery; otherwise, it does not. |
| 277 |
</p> |
| 278 |
<p> |
| 279 |
Note that using process registration also ensures that |
| 280 |
recovery is serialized across applications. That is, |
| 281 |
only one process at a time has a chance to run |
| 282 |
recovery. Generally this means that the first process |
| 283 |
to start up will run recovery, and all other processes |
| 284 |
will silently not run recovery because it is not |
| 285 |
needed. |
| 286 |
</p> |
| 287 |
<p> |
| 288 |
To cause your application to register its environment, you specify |
| 289 |
<span> |
| 290 |
the <code class="literal">DB_REGISTER</code> flag when you open your environment. |
| 291 |
You may also specify <code class="literal">DB_RECOVER</code>. However, it is an error to specify |
| 292 |
<code class="literal">DB_RECOVER_FATAL</code> when using |
| 293 |
the <code class="literal">DB_REGISTER</code> flag. |
| 294 |
</span> |
| 295 |
|
| 296 |
|
| 297 |
If during the open, DB determines that recovery must be run, it will automatically run the correct |
| 298 |
type of recovery for you, so long as you specify normal recovery |
| 299 |
on your environment open. If you do not specify normal recovery, and you register your environment, |
| 300 |
then no recovery is run if the registration process identifies a need for it. In this case, |
| 301 |
the environment open simply fails by |
| 302 |
<span>returning <code class="literal">DB_RUNRECOVERY</code>.</span> |
| 303 |
|
| 304 |
</p> |
| 305 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 306 |
<h3 class="title">Note</h3> |
| 307 |
<p> |
| 308 |
If you do not specify normal recovery when you open your first registered environment |
| 309 |
in the application, then that application will fail the environment open by |
| 310 |
<span>returning <code class="literal">DB_RUNRECOVERY</code>.</span> |
| 311 |
|
| 312 |
This is because the first process to register must create an internal |
| 313 |
registration file, and recovery is forced when that file is created. To |
| 314 |
avoid an abnormal termination of the environment open, specify recovery on |
| 315 |
the environment open for at least the first process starting in your |
| 316 |
application. |
| 317 |
</p> |
| 318 |
</div> |
| 319 |
<p> |
| 320 |
In addition, if you specify <code class="literal">DB_ENV_FAILCHK</code> |
| 321 |
when you register your environment, then a fail check is performed on |
| 322 |
environment open (fail checks are described in the next section). If, during the |
| 323 |
fail check process, an abnormal termination is detected for any of the processes |
| 324 |
involved in the application, DB releases any read locks held by the dead |
| 325 |
process and performs transaction aborts as necessary. This is done in an attempt |
| 326 |
to clean up the environment. |
| 327 |
</p> |
| 328 |
<p> |
| 329 |
In this situation, if a general cleanup of the |
| 330 |
environment is not possible and normal recovery is not specified on environment |
| 331 |
open, then the open will abort, |
| 332 |
<span>returning <code class="literal">DB_RUNRECOVERY</code>.</span> |
| 333 |
|
| 334 |
However, if this situation occurs and recovery was specified, then the appropriate type of recovery |
| 335 |
(normal or fatal) is run so as to bring the environment back to a healthy state. |
| 336 |
</p> |
| 337 |
<p> |
| 338 |
Be aware that there are some limitations/requirements if you want your various processes to |
| 339 |
coordinate recovery using registration: |
| 340 |
</p> |
| 341 |
<div class="orderedlist"> |
| 342 |
<ol type="1"> |
| 343 |
<li> |
| 344 |
<p> |
| 345 |
There can be only one environment handle per |
| 346 |
environment per process. In the case of multi-threaded |
| 347 |
processes, the environment handle must be shared across threads. |
| 348 |
</p> |
| 349 |
</li> |
| 350 |
<li> |
| 351 |
<p> |
| 352 |
All processes sharing the environment must use registration. If registration is |
| 353 |
not uniformly used across all participating processes, then you can see inconsistent results |
| 354 |
in terms of your application's ability to recognize that recovery must be run. |
| 355 |
</p> |
| 356 |
</li> |
| 357 |
</ol> |
| 358 |
</div> |
| 359 |
</div> |
| 360 |
<div class="sect3" lang="en" xml:lang="en"> |
| 361 |
<div class="titlepage"> |
| 362 |
<div> |
| 363 |
<div> |
| 364 |
<h4 class="title"><a id="failchk"></a>Failure Checking</h4> |
| 365 |
</div> |
| 366 |
</div> |
| 367 |
</div> |
| 368 |
<p> |
| 369 |
For very large and robust multi-process applications, the most common way to ensure all the |
| 370 |
processes are working as intended is to make use of a watchdog process. To assist a watchdog |
| 371 |
process, DB offers a failure checking mechanism. |
| 372 |
</p> |
| 373 |
<p> |
| 374 |
When a thread of control fails with open environment handles, the result is that there may be |
| 375 |
resources left locked or corrupted. Other threads of control may encountered these unavailable resources |
| 376 |
quickly or not at all, depending on data access patterns. |
| 377 |
</p> |
| 378 |
<p> |
| 379 |
In any case, the DB failure checking mechanism allows a watchdog to detect if an environment is |
| 380 |
unusable as a result of a thread of control failure. It should be called periodically |
| 381 |
(for example, once a minute) from the watchdog process. If the environment is deemed unusable, then |
| 382 |
the watchdog process is notified that recovery should be run. It is then up to the watchdog to |
| 383 |
actually run recovery. It is also the watchdog's responsibility to decide what to do about currently |
| 384 |
running processes before running recovery. The watchdog could, for example, attempt to |
| 385 |
gracefully shutdown or kill all relevant processes before running recovery. |
| 386 |
</p> |
| 387 |
<p> |
| 388 |
Note that failure checking need not be run from a separate process, although conceptually that is |
| 389 |
how the mechanism is meant to be used. This same mechanism could be used in a multi-threaded |
| 390 |
application that wants to have a watchdog thread. |
| 391 |
</p> |
| 392 |
<p> |
| 393 |
To use failure checking you must: |
| 394 |
</p> |
| 395 |
<div class="orderedlist"> |
| 396 |
<ol type="1"> |
| 397 |
<li> |
| 398 |
<p> |
| 399 |
<span> |
| 400 |
Provide an <code class="function">is_alive()</code> call back using the |
| 401 |
<code class="methodname">DB_ENV->set_isalive()</code> |
| 402 |
|
| 403 |
method. |
| 404 |
</span> |
| 405 |
|
| 406 |
|
| 407 |
DB uses this method to determine whether a specified process and thread |
| 408 |
is alive when the failure checking is performed. |
| 409 |
</p> |
| 410 |
</li> |
| 411 |
<li> |
| 412 |
<p> |
| 413 |
Possibly provide a |
| 414 |
|
| 415 |
<span> |
| 416 |
<code class="literal">thread_id</code> callback |
| 417 |
</span> |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
that uniquely identifies a process |
| 423 |
and thread of control. This |
| 424 |
<span>callback</span> |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
is only necessary if the standard process and thread |
| 429 |
identification functions for your platform are not sufficient to for use by failure |
| 430 |
checking. This is rarely necessary and is usually because the thread and/or process ids |
| 431 |
used by your system cannot fit into an unsigned integer. |
| 432 |
</p> |
| 433 |
<p> |
| 434 |
You provide this callback using the |
| 435 |
<code class="methodname">DB_ENV->set_thread_id()</code> |
| 436 |
|
| 437 |
method. See the API reference for this method for more information on when setting a thread |
| 438 |
id callback might be necessary. |
| 439 |
</p> |
| 440 |
</li> |
| 441 |
<li> |
| 442 |
<p> |
| 443 |
Call the |
| 444 |
<code class="methodname">DB_ENV->failchk()</code> |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
method periodically. You can do this either periodically (once per minute, for example), or |
| 450 |
whenever a thread of control exits for your application. |
| 451 |
</p> |
| 452 |
<p> |
| 453 |
If this method determines that a thread of control exited holding read locks, those locks |
| 454 |
are automatically released. If the thread of control exited with an unresolved transaction, |
| 455 |
that transaction is aborted. If any other problems exist beyond these such that the |
| 456 |
environment must be recovered, the method will |
| 457 |
<span>return <code class="literal">DB_RUNRECOVERY</code>.</span> |
| 458 |
|
| 459 |
|
| 460 |
</p> |
| 461 |
</li> |
| 462 |
</ol> |
| 463 |
</div> |
| 464 |
</div> |
| 465 |
</div> |
| 466 |
</div> |
| 467 |
<div class="navfooter"> |
| 468 |
<hr /> |
| 469 |
<table width="100%" summary="Navigation footer"> |
| 470 |
<tr> |
| 471 |
<td width="40%" align="left"><a accesskey="p" href="recovery.html">Prev</a> </td> |
| 472 |
<td width="20%" align="center"> |
| 473 |
<a accesskey="u" href="filemanagement.html">Up</a> |
| 474 |
</td> |
| 475 |
<td width="40%" align="right"> <a accesskey="n" href="hotfailover.html">Next</a></td> |
| 476 |
</tr> |
| 477 |
<tr> |
| 478 |
<td width="40%" align="left" valign="top">Recovery Procedures </td> |
| 479 |
<td width="20%" align="center"> |
| 480 |
<a accesskey="h" href="index.html">Home</a> |
| 481 |
</td> |
| 482 |
<td width="40%" align="right" valign="top"> Using Hot Failovers</td> |
| 483 |
</tr> |
| 484 |
</table> |
| 485 |
</div> |
| 486 |
</body> |
| 487 |
</html> |