| 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>Architecting Transactional Data Store applications</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="transapp.html" title="Chapter 11. Berkeley DB Transactional Data Store Applications" /> |
| 11 | <link rel="prev" href="transapp_fail.html" title="Handling failure in Transactional Data Store applications" /> |
| 12 | <link rel="next" href="transapp_env_open.html" title="Opening the environment" /> |
| 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">Architecting Transactional Data Store applications</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_fail.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 11. |
| 26 | Berkeley DB Transactional Data Store Applications |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="transapp_env_open.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="transapp_app"></a>Architecting Transactional Data Store applications</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p> |
| 42 | When building Transactional Data Store applications, the architecture |
| 43 | decisions involve application startup (running recovery) and handling |
| 44 | system or application failure. For details on performing recovery, see |
| 45 | the <a class="xref" href="transapp_recovery.html" title="Recovery procedures">Recovery procedures</a>. |
| 46 | </p> |
| 47 | <p> |
| 48 | Recovery in a database environment is a single-threaded procedure, that |
| 49 | is, one thread of control or process must complete database environment |
| 50 | recovery before any other thread of control or process operates in the |
| 51 | Berkeley DB environment. |
| 52 | </p> |
| 53 | <p> |
| 54 | Performing recovery first marks any existing database environment as |
| 55 | "failed" and then removes it, causing threads of control running in the |
| 56 | database environment to fail and return to the application. This |
| 57 | feature allows applications to recover environments without concern for |
| 58 | threads of control that might still be running in the removed |
| 59 | environment. The subsequent re-creation of the database environment is |
| 60 | serialized, so multiple threads of control attempting to create a |
| 61 | database environment will serialize behind a single creating thread. |
| 62 | </p> |
| 63 | <p> |
| 64 | One consideration in removing (as part of recovering) a database |
| 65 | environment which may be in use by another thread, is the type of mutex |
| 66 | being used by the Berkeley DB library. In the case of database |
| 67 | environment failure when using test-and-set mutexes, threads of control |
| 68 | waiting on a mutex when the environment is marked "failed" will quickly |
| 69 | notice the failure and will return an error from the Berkeley DB API. |
| 70 | In the case of environment failure when using blocking mutexes, where |
| 71 | the underlying system mutex implementation does not unblock mutex |
| 72 | waiters after the thread of control holding the mutex dies, threads |
| 73 | waiting on a mutex when an environment is recovered might hang forever. |
| 74 | Applications blocked on events (for example, an application blocked on |
| 75 | a network socket, or a GUI event) may also fail to notice environment |
| 76 | recovery within a reasonable amount of time. Systems with such mutex |
| 77 | implementations are rare, but do exist; applications on such systems |
| 78 | should use an application architecture where the thread recovering the |
| 79 | database environment can explicitly terminate any process using the |
| 80 | failed environment, or configure Berkeley DB for test-and-set mutexes, |
| 81 | or incorporate some form of long-running timer or watchdog process to |
| 82 | wake or kill blocked processes should they block for too long. |
| 83 | </p> |
| 84 | <p> |
| 85 | Regardless, it makes little sense for multiple threads of control to |
| 86 | simultaneously attempt recovery of a database environment, since the |
| 87 | last one to run will remove all database environments created by the |
| 88 | threads of control that ran before it. However, for some applications, |
| 89 | it may make sense for applications to have a single thread of control |
| 90 | that performs recovery and then removes the database environment, after |
| 91 | which the application launches a number of processes, any of which will |
| 92 | create the database environment and continue forward. |
| 93 | </p> |
| 94 | <p> |
| 95 | There are three common ways to architect Berkeley DB Transactional Data |
| 96 | Store applications. The one chosen is usually based on whether or not |
| 97 | the application is comprised of a single process or group of processes |
| 98 | descended from a single process (for example, a server started when the |
| 99 | system first boots), or if the application is comprised of unrelated |
| 100 | processes (for example, processes started by web connections or users |
| 101 | logged into the system). |
| 102 | </p> |
| 103 | <div class="orderedlist"> |
| 104 | <ol type="1"> |
| 105 | <li> |
| 106 | <p> |
| 107 | The first way to architect Transactional Data Store |
| 108 | applications is as a single process (the process may or may not |
| 109 | be multithreaded.) |
| 110 | </p> |
| 111 | <p> |
| 112 | When this process starts, it runs recovery on the database |
| 113 | environment and then opens its databases. The application can |
| 114 | subsequently create new threads as it chooses. Those threads |
| 115 | can either share already open Berkeley DB <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> and <a href="../api_reference/C/db.html" class="olink">DB</a> |
| 116 | handles, or create their own. In this architecture, databases |
| 117 | are rarely opened or closed when more than a single thread of |
| 118 | control is running; that is, they are opened when only a single |
| 119 | thread is running, and closed after all threads but one have |
| 120 | exited. The last thread of control to exit closes the |
| 121 | databases and the database environment. |
| 122 | </p> |
| 123 | <p> |
| 124 | This architecture is simplest to implement because thread |
| 125 | serialization is easy and failure detection does not require |
| 126 | monitoring multiple processes. |
| 127 | </p> |
| 128 | <p> |
| 129 | If the application's thread model allows processes to continue |
| 130 | after thread failure, the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> method can be used to |
| 131 | determine if the database environment is usable after thread |
| 132 | failure. If the application does not call <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a>, or |
| 133 | <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> returns |
| 134 | <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>, |
| 135 | the application must |
| 136 | behave as if there has been a system failure, performing |
| 137 | recovery and re-creating the database environment. Once these |
| 138 | actions have been taken, other threads of control can continue |
| 139 | (as long as all existing Berkeley DB handles are first |
| 140 | discarded). |
| 141 | </p> |
| 142 | </li> |
| 143 | <li> |
| 144 | <p> |
| 145 | The second way to architect Transactional Data Store |
| 146 | applications is as a group of related processes (the processes |
| 147 | may or may not be multithreaded). |
| 148 | </p> |
| 149 | <p> |
| 150 | This architecture requires the order in which threads of control are |
| 151 | created be controlled to serialize database environment recovery. |
| 152 | </p> |
| 153 | <p> |
| 154 | In addition, this architecture requires that threads of control |
| 155 | be monitored. If any thread of control exits with open |
| 156 | Berkeley DB handles, the application may call the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> |
| 157 | method to detect lost mutexes and locks and determine if the |
| 158 | application can continue. If the application does not call |
| 159 | <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a>, or <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> returns that the database |
| 160 | environment can no longer be used, the application must behave |
| 161 | as if there has been a system failure, performing recovery and |
| 162 | creating a new database environment. Once these actions have |
| 163 | been taken, other threads of control can be continued (as long |
| 164 | as all existing Berkeley DB handles are first discarded), or |
| 165 | |
| 166 | </p> |
| 167 | <p> |
| 168 | The easiest way to structure groups of related processes is to |
| 169 | first create a single "watcher" process (often a script) that |
| 170 | starts when the system first boots, runs recovery on the |
| 171 | database environment and then creates the processes or threads |
| 172 | that will actually perform work. The initial thread has no |
| 173 | further responsibilities other than to wait on the threads of |
| 174 | control it has started, to ensure none of them unexpectedly |
| 175 | exit. If a thread of control exits, the watcher process |
| 176 | optionally calls the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> method. If the application |
| 177 | does not call <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> or if <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> returns that the |
| 178 | environment can no longer be used, the watcher kills all of the |
| 179 | threads of control using the failed environment, runs recovery, |
| 180 | and starts new threads of control to perform work. |
| 181 | </p> |
| 182 | </li> |
| 183 | <li> |
| 184 | <p> |
| 185 | The third way to architect Transactional Data Store |
| 186 | applications is as a group of unrelated processes (the |
| 187 | processes may or may not be multithreaded). This is the most |
| 188 | difficult architecture to implement because of the level of |
| 189 | difficulty in some systems of finding and monitoring unrelated |
| 190 | processes. There are several possible techniques to implement |
| 191 | this architecture. |
| 192 | </p> |
| 193 | <p> |
| 194 | One solution is to log a thread of control ID when a new |
| 195 | Berkeley DB handle is opened. For example, an initial |
| 196 | "watcher" process could run recovery on the database |
| 197 | environment and then create a sentinel file. Any "worker" |
| 198 | process wanting to use the environment would check for the |
| 199 | sentinel file. If the sentinel file does not exist, the worker |
| 200 | would fail or wait for the sentinel file to be created. Once |
| 201 | the sentinel file exists, the worker would register its process |
| 202 | ID with the watcher (via shared memory, IPC or some other |
| 203 | registry mechanism), and then the worker would open its |
| 204 | <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handles and proceed. When the worker finishes |
| 205 | using the environment, it would unregister its process ID with |
| 206 | the watcher. The watcher periodically checks to ensure that no |
| 207 | worker has failed while using the environment. If a worker |
| 208 | fails while using the environment, the watcher removes the |
| 209 | sentinel file, kills all of the workers currently using the |
| 210 | environment, runs recovery on the environment, and finally |
| 211 | creates a new sentinel file. |
| 212 | </p> |
| 213 | <p> |
| 214 | The weakness of this approach is that, on some systems, it is |
| 215 | difficult to determine if an unrelated process is still |
| 216 | running. For example, POSIX systems generally disallow sending |
| 217 | signals to unrelated processes. The trick to monitoring |
| 218 | unrelated processes is to find a system resource held by the |
| 219 | process that will be modified if the process dies. On POSIX |
| 220 | systems, flock- or fcntl-style locking will work, as will |
| 221 | LockFile on Windows systems. Other systems may have to use |
| 222 | other process-related information such as file reference counts |
| 223 | or modification times. In the worst case, threads of control |
| 224 | can be required to periodically re-register with the watcher |
| 225 | process: if the watcher has not heard from a thread of control |
| 226 | in a specified period of time, the watcher will take action, |
| 227 | recovering the environment. |
| 228 | </p> |
| 229 | <p> |
| 230 | The Berkeley DB library includes one built-in implementation of this approach, |
| 231 | the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> method's <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag: |
| 232 | </p> |
| 233 | <p> |
| 234 | If the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag is set, each process opening the |
| 235 | database environment first checks to see if recovery needs to |
| 236 | be performed. If recovery needs to be performed for any reason |
| 237 | (including the initial creation of the database environment), |
| 238 | and <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> is also specified, recovery will be performed |
| 239 | and then the open will proceed normally. If recovery needs to |
| 240 | be performed and <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> is not specified, |
| 241 | <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a> |
| 242 | will be returned. If recovery does not need to be performed, |
| 243 | <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> will be ignored. |
| 244 | </p> |
| 245 | <p> |
| 246 | Prior to the actual recovery beginning, the <a href="../api_reference/C/envevent_notify.html#event_notify_DB_EVENT_REG_PANIC" class="olink">DB_EVENT_REG_PANIC</a> |
| 247 | event is set for the environment. Processes in the application using |
| 248 | the <a href="../api_reference/C/envevent_notify.html" class="olink">DB_ENV->set_event_notify()</a> method will be notified when they do their next |
| 249 | operations in the environment. Processes receiving this event should |
| 250 | exit the environment. Also, the <a href="../api_reference/C/envevent_notify.html#event_notify_DB_EVENT_REG_ALIVE" class="olink">DB_EVENT_REG_ALIVE</a> event will be |
| 251 | triggered if there are other processes currently attached to the |
| 252 | environment. Only the process doing the recovery will receive this |
| 253 | event notification. It will receive this notification once for each |
| 254 | process still attached to the environment. The parameter of the |
| 255 | <a href="../api_reference/C/envevent_notify.html" class="olink">DB_ENV->set_event_notify()</a> callback will contain the process identifier of the |
| 256 | process still attached. The process doing the recovery can then |
| 257 | signal the attached process or perform some other operation prior to |
| 258 | recovery (i.e. kill the attached process). |
| 259 | </p> |
| 260 | <p> |
| 261 | The <a href="../api_reference/C/envset_timeout.html" class="olink">DB_ENV->set_timeout()</a> method's <a href="../api_reference/C/envset_timeout.html#set_timeout_DB_SET_REG_TIMEOUT" class="olink">DB_SET_REG_TIMEOUT</a> flag can be set to |
| 262 | establish a wait period before starting recovery. This creates a |
| 263 | window of time for other processes to receive the DB_EVENT_REG_PANIC |
| 264 | event and exit the environment. |
| 265 | </p> |
| 266 | <p> |
| 267 | There are three additional requirements for the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> |
| 268 | architecture to work: |
| 269 | </p> |
| 270 | <div class="itemizedlist"> |
| 271 | <ul type="disc"> |
| 272 | <li> |
| 273 | <p> |
| 274 | First, all applications using the database environment must |
| 275 | specify the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag when opening the environment. |
| 276 | However, there is no additional requirement if the application |
| 277 | chooses a single process to recover the environment, as the |
| 278 | first process to open the database environment will know to |
| 279 | perform recovery. |
| 280 | </p> |
| 281 | </li> |
| 282 | <li> |
| 283 | <p> |
| 284 | Second, there can only be a single <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handle per database |
| 285 | environment in each process. As the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> locking is |
| 286 | per-process, not per-thread, multiple <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handles in a single |
| 287 | environment could race with each other, potentially causing |
| 288 | data corruption. |
| 289 | </p> |
| 290 | </li> |
| 291 | <li> |
| 292 | <p> |
| 293 | Third, the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> implementation does not explicitly |
| 294 | terminate processes using the database environment which is |
| 295 | being recovered. Instead, it relies on the processes |
| 296 | themselves noticing the database environment has been discarded |
| 297 | from underneath them. For this reason, the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag |
| 298 | should be used with a mutex implementation that does not block |
| 299 | in the operating system, as that risks a thread of control |
| 300 | blocking forever on a mutex which will never be granted. Using |
| 301 | any test-and-set mutex implementation ensures this cannot |
| 302 | happen, and for that reason the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag is generally |
| 303 | used with a test-and-set mutex implementation. |
| 304 | </p> |
| 305 | </li> |
| 306 | </ul> |
| 307 | </div> |
| 308 | <p> |
| 309 | A second solution for groups of unrelated processes is also |
| 310 | based on a "watcher process". This solution is intended for |
| 311 | systems where it is not practical to monitor the processes |
| 312 | sharing a database environment, but it is possible to monitor |
| 313 | the environment to detect if a thread of control has failed |
| 314 | holding open Berkeley DB handles. This would be done by having |
| 315 | a "watcher" process periodically call the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> method. |
| 316 | If <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> returns that the environment can no longer be |
| 317 | used, the watcher would then take action, recovering the |
| 318 | environment. |
| 319 | </p> |
| 320 | <p> |
| 321 | The weakness of this approach is that all threads of control |
| 322 | using the environment must specify an "ID" function and an |
| 323 | "is-alive" function using the <a href="../api_reference/C/envset_thread_id.html" class="olink">DB_ENV->set_thread_id()</a> method. (In |
| 324 | other words, the Berkeley DB library must be able to assign a |
| 325 | unique ID to each thread of control, and additionally determine |
| 326 | if the thread of control is still running. It can be difficult |
| 327 | to portably provide that information in applications using a |
| 328 | variety of different programming languages and running on a |
| 329 | variety of different platforms.) |
| 330 | </p> |
| 331 | <p> |
| 332 | A third solution for groups of unrelated processes is a hybrid of the two |
| 333 | above. Along with implementing the built-in sentinel approach with the |
| 334 | the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> methods <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> flag, the <a href="../api_reference/C/envopen.html#envopen_DB_FAILCHK" class="olink">DB_FAILCHK</a> flag can be specified. |
| 335 | When using both flags, each process opening the database environment first |
| 336 | checks to see if recocvery needs to be performed. If recovery needs to be |
| 337 | performed for any reason, it will first determine if a thread of control |
| 338 | exited while holding database read locks, and release those. Then it will |
| 339 | abort any unresolved transactions. If these steps are successful, the process |
| 340 | opening the environment will continue without the need for any |
| 341 | additional recocvery. If these steps are unsuccessful, then additional |
| 342 | recovery will be performed if <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> is specified and if <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> is not |
| 343 | specified, <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>will be returned. |
| 344 | </p> |
| 345 | <p> |
| 346 | Since this solution is hybrid of the first two, all of the requirements of both |
| 347 | of them must be implemented (will need "ID" function, "is-alive" function, |
| 348 | single <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handle per database, etc.) |
| 349 | </p> |
| 350 | <p> |
| 351 | The described approaches are different, and should not be |
| 352 | combined. Applications might use either the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> |
| 353 | approach, the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> or the hybrid approach, but not together in |
| 354 | the same application. For example, a POSIX application written |
| 355 | as a library underneath a wide variety of interfaces and |
| 356 | differing APIs might choose the <a href="../api_reference/C/envopen.html#envopen_DB_REGISTER" class="olink">DB_REGISTER</a> approach for a |
| 357 | few reasons: first, it does not require making periodic calls |
| 358 | to the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> method; second, when implementing in a |
| 359 | variety of languages, is may be more difficult to specify |
| 360 | unique IDs for each thread of control; third, it may be more |
| 361 | difficult determine if a thread of control is still running, as |
| 362 | any particular thread of control is likely to lack sufficient |
| 363 | permissions to signal other processes. Alternatively, an |
| 364 | application with a dedicated watcher process, running with |
| 365 | appropriate permissions, might choose the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> approach |
| 366 | as supporting higher overall throughput and reliability, as |
| 367 | that approach allows the application to abort unresolved |
| 368 | transactions and continue forward without having to recover the |
| 369 | database environment. The hybrid approach is useful in situations |
| 370 | where running a dedicated watcher process is not practical but getting the |
| 371 | equivalent of <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV->failchk()</a> on the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> is important. |
| 372 | </p> |
| 373 | </li> |
| 374 | </ol> |
| 375 | </div> |
| 376 | <p> |
| 377 | Obviously, when implementing a process to monitor other threads of |
| 378 | control, it is important the watcher process' code be as simple and |
| 379 | well-tested as possible, because the application may hang if it fails. |
| 380 | </p> |
| 381 | </div> |
| 382 | <div class="navfooter"> |
| 383 | <hr /> |
| 384 | <table width="100%" summary="Navigation footer"> |
| 385 | <tr> |
| 386 | <td width="40%" align="left"><a accesskey="p" href="transapp_fail.html">Prev</a> </td> |
| 387 | <td width="20%" align="center"> |
| 388 | <a accesskey="u" href="transapp.html">Up</a> |
| 389 | </td> |
| 390 | <td width="40%" align="right"> <a accesskey="n" href="transapp_env_open.html">Next</a></td> |
| 391 | </tr> |
| 392 | <tr> |
| 393 | <td width="40%" align="left" valign="top">Handling failure in Transactional Data Store applications </td> |
| 394 | <td width="20%" align="center"> |
| 395 | <a accesskey="h" href="index.html">Home</a> |
| 396 | </td> |
| 397 | <td width="40%" align="right" valign="top"> Opening the environment</td> |
| 398 | </tr> |
| 399 | </table> |
| 400 | </div> |
| 401 | </body> |
| 402 | </html> |