| 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 |
|
| 78 |
<a class="xref" href="txnexample_java.html" title="Base API Transaction Example">Base API Transaction Example</a> |
| 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 |
The following sections describe a mechanism that you can use to determine if and when you should run |
| 236 |
recovery in a multi-process application. |
| 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 |
|
| 290 |
|
| 291 |
<span> |
| 292 |
<code class="literal">true</code> to the <code class="methodname">EnvironmentConfig.setRegister()</code> |
| 293 |
method when you open your environment. You may also specify |
| 294 |
<code class="literal">true</code> to <code class="methodname">EnvironmentConfig.setRunRecovery()</code>. |
| 295 |
However, it is an error to specify <code class="literal">true</code> to |
| 296 |
<code class="methodname">EnvironmentConfig.setRunFatalRecovery()</code> when |
| 297 |
you are also registering your environment with the <code class="methodname">setRegister()</code> |
| 298 |
method. |
| 299 |
</span> |
| 300 |
If during the open, DB determines that recovery must be run, it will automatically run the correct |
| 301 |
type of recovery for you, so long as you specify normal recovery |
| 302 |
on your environment open. If you do not specify normal recovery, and you register your environment, |
| 303 |
then no recovery is run if the registration process identifies a need for it. In this case, |
| 304 |
the environment open simply fails by |
| 305 |
|
| 306 |
<span>throwing <code class="classname">RunRecoveryException</code>.</span> |
| 307 |
</p> |
| 308 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 309 |
<h3 class="title">Note</h3> |
| 310 |
<p> |
| 311 |
If you do not specify normal recovery when you open your first registered environment |
| 312 |
in the application, then that application will fail the environment open by |
| 313 |
|
| 314 |
<span>throwing <code class="classname">RunRecoveryException</code>.</span> |
| 315 |
This is because the first process to register must create an internal |
| 316 |
registration file, and recovery is forced when that file is created. To |
| 317 |
avoid an abnormal termination of the environment open, specify recovery on |
| 318 |
the environment open for at least the first process starting in your |
| 319 |
application. |
| 320 |
</p> |
| 321 |
</div> |
| 322 |
<p> |
| 323 |
Be aware that there are some limitations/requirements if you want your various processes to |
| 324 |
coordinate recovery using registration: |
| 325 |
</p> |
| 326 |
<div class="orderedlist"> |
| 327 |
<ol type="1"> |
| 328 |
<li> |
| 329 |
<p> |
| 330 |
There can be only one environment handle per |
| 331 |
environment per process. In the case of multi-threaded |
| 332 |
processes, the environment handle must be shared across threads. |
| 333 |
</p> |
| 334 |
</li> |
| 335 |
<li> |
| 336 |
<p> |
| 337 |
All processes sharing the environment must use registration. If registration is |
| 338 |
not uniformly used across all participating processes, then you can see inconsistent results |
| 339 |
in terms of your application's ability to recognize that recovery must be run. |
| 340 |
</p> |
| 341 |
</li> |
| 342 |
</ol> |
| 343 |
</div> |
| 344 |
</div> |
| 345 |
</div> |
| 346 |
</div> |
| 347 |
<div class="navfooter"> |
| 348 |
<hr /> |
| 349 |
<table width="100%" summary="Navigation footer"> |
| 350 |
<tr> |
| 351 |
<td width="40%" align="left"><a accesskey="p" href="recovery.html">Prev</a> </td> |
| 352 |
<td width="20%" align="center"> |
| 353 |
<a accesskey="u" href="filemanagement.html">Up</a> |
| 354 |
</td> |
| 355 |
<td width="40%" align="right"> <a accesskey="n" href="hotfailover.html">Next</a></td> |
| 356 |
</tr> |
| 357 |
<tr> |
| 358 |
<td width="40%" align="left" valign="top">Recovery Procedures </td> |
| 359 |
<td width="20%" align="center"> |
| 360 |
<a accesskey="h" href="index.html">Home</a> |
| 361 |
</td> |
| 362 |
<td width="40%" align="right" valign="top"> Using Hot Failovers</td> |
| 363 |
</tr> |
| 364 |
</table> |
| 365 |
</div> |
| 366 |
</body> |
| 367 |
</html> |