| 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>Application configuration</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="apprec.html" title="Chapter 14. Application Specific Logging and Recovery" /> |
| 11 | <link rel="prev" href="apprec_auto.html" title="Automatically generated functions" /> |
| 12 | <link rel="next" href="program.html" title="Chapter 15. Programmer Notes" /> |
| 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">Application configuration</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="apprec_auto.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 14. |
| 26 | Application Specific Logging and Recovery |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="program.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="apprec_config"></a>Application configuration</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The application should include a dispatch function that dispatches to |
| 42 | appropriate printing and/or recovery functions based on the log record |
| 43 | type and the operation code. The dispatch function should take the same |
| 44 | arguments as the recovery function, and should call the appropriate |
| 45 | recovery and/or printing functions based on the log record type and the |
| 46 | operation code. For example, the ex_apprec dispatch function is as |
| 47 | follows:</p> |
| 48 | <pre class="programlisting">int |
| 49 | apprec_dispatch(dbenv, dbt, lsn, op) |
| 50 | DB_ENV *dbenv; |
| 51 | DBT *dbt; |
| 52 | DB_LSN *lsn; |
| 53 | db_recops op; |
| 54 | { |
| 55 | u_int32_t rectype; |
| 56 | /* Pull the record type out of the log record. */ |
| 57 | memcpy(&rectype, dbt->data, sizeof(rectype)); |
| 58 | switch (rectype) { |
| 59 | case DB_ex_apprec_mkdir: |
| 60 | return (ex_apprec_mkdir_recover(dbenv, dbt, lsn, op)); |
| 61 | default: |
| 62 | /* |
| 63 | * We've hit an unexpected, allegedly user-defined record |
| 64 | * type. |
| 65 | */ |
| 66 | dbenv->errx(dbenv, "Unexpected log record type encountered"); |
| 67 | return (EINVAL); |
| 68 | } |
| 69 | }</pre> |
| 70 | <p>Applications use this dispatch function and the automatically generated |
| 71 | functions as follows:</p> |
| 72 | <div class="orderedlist"> |
| 73 | <ol type="1"> |
| 74 | <li>When the application starts, call the <a href="../api_reference/C/envset_app_dispatch.html" class="olink">DB_ENV->set_app_dispatch()</a> |
| 75 | with your dispatch function.</li> |
| 76 | <li>Issue a <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV->txn_begin()</a> call before any operations you want to be |
| 77 | transaction-protected.</li> |
| 78 | <li>Before accessing any data, issue the appropriate lock call to lock the |
| 79 | data (either for reading or writing).</li> |
| 80 | <li>Before modifying any data that is transaction-protected, issue a call |
| 81 | to the appropriate log function.</li> |
| 82 | <li>Call <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a> |
| 83 | to cancel all of the modifications.</li> |
| 84 | </ol> |
| 85 | </div> |
| 86 | <p>The recovery functions are called in the three following cases:</p> |
| 87 | <div class="orderedlist"> |
| 88 | <ol type="1"> |
| 89 | <li>During recovery after application or system failure, with op set to |
| 90 | <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_FORWARD_ROLL" class="olink">DB_TXN_FORWARD_ROLL</a> or <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_BACKWARD_ROLL" class="olink">DB_TXN_BACKWARD_ROLL</a>.</li> |
| 91 | <li>During transaction abort, with op set to <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_ABORT" class="olink">DB_TXN_ABORT</a>.</li> |
| 92 | <li>On a replicated client to apply updates from the master, with op set to |
| 93 | <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_APPLY" class="olink">DB_TXN_APPLY</a>.</li> |
| 94 | </ol> |
| 95 | </div> |
| 96 | <p>For each log record type you declare, you must write the appropriate |
| 97 | function to undo and redo the modifications. The shell of these |
| 98 | functions will be generated for you automatically, but you must fill in |
| 99 | the details.</p> |
| 100 | <p>Your code must be able to detect whether the described modifications |
| 101 | have been applied to the data. The function will be called with the |
| 102 | "op" parameter set to <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_ABORT" class="olink">DB_TXN_ABORT</a> when a transaction that wrote |
| 103 | the log record aborts, with <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_FORWARD_ROLL" class="olink">DB_TXN_FORWARD_ROLL</a> and |
| 104 | <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_BACKWARD_ROLL" class="olink">DB_TXN_BACKWARD_ROLL</a> during recovery, and with <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_APPLY" class="olink">DB_TXN_APPLY</a> |
| 105 | on a replicated client.</p> |
| 106 | <p>The actions for <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_ABORT" class="olink">DB_TXN_ABORT</a> and <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_BACKWARD_ROLL" class="olink">DB_TXN_BACKWARD_ROLL</a> |
| 107 | should generally be the same, and the actions for |
| 108 | <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_FORWARD_ROLL" class="olink">DB_TXN_FORWARD_ROLL</a> and <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_APPLY" class="olink">DB_TXN_APPLY</a> should generally |
| 109 | be the same. However, if the application is using Berkeley DB replication |
| 110 | and another thread of control may be performing read operations while |
| 111 | log records are applied on a replication client, the recovery function |
| 112 | should perform appropriate locking during <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_APPLY" class="olink">DB_TXN_APPLY</a> |
| 113 | operations. In this case, the recovery function may encounter deadlocks |
| 114 | when issuing locking calls. The application should run with the |
| 115 | deadlock detector, and the recovery function should simply return |
| 116 | <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> if a deadlock is detected and a locking |
| 117 | operation fails with that error.</p> |
| 118 | <p>The <a href="../api_reference/C/envset_app_dispatch.html#set_app_dispatch_DB_TXN_PRINT" class="olink">DB_TXN_PRINT</a> operation should print the log record, |
| 119 | typically using the auto-generated print function; it is not used in |
| 120 | the Berkeley DB library, but may be useful for debugging, as in the |
| 121 | <a href="../api_reference/C/db_printlog.html" class="olink">db_printlog</a> utility. Applications may safely ignore this |
| 122 | operation code, they may handle printing from the recovery function, or |
| 123 | they may dispatch directly to the auto-generated print function.</p> |
| 124 | <p>One common way to determine whether operations need to be undone or |
| 125 | redone is the use of log sequence numbers (LSNs). For example, each |
| 126 | access method database page contains the LSN of the most recent log |
| 127 | record that describes a modification to the page. When the access |
| 128 | method changes a page, it writes a log record describing the change and |
| 129 | including the LSN that was on the page before the change. This LSN is |
| 130 | referred to as the previous LSN. The recovery functions read the page |
| 131 | described by a log record, and compare the LSN on the page to the LSN |
| 132 | they were passed.</p> |
| 133 | <p>If the page LSN is less than the passed LSN and the operation is an |
| 134 | undo, no action is necessary (because the modifications have not been |
| 135 | written to the page). If the page LSN is the same as the previous LSN |
| 136 | and the operation is a redo, the actions described are reapplied to the |
| 137 | page. If the page LSN is equal to the passed LSN and the operation is |
| 138 | an undo, the actions are removed from the page; if the page LSN is |
| 139 | greater than the passed LSN and the operation is a redo, no further |
| 140 | action is necessary. If the action is a redo and the LSN on the page |
| 141 | is less than the previous LSN in the log record, it is an error because |
| 142 | it could happen only if some previous log record was not processed.</p> |
| 143 | <p>Examples of other recovery functions can be found in the Berkeley DB library |
| 144 | recovery functions (found in files named XXX_rec.c) and in the |
| 145 | application-specific recovery example (specifically, ex_apprec_rec.c).</p> |
| 146 | <p>Finally, applications need to ensure that any data modifications they |
| 147 | have made, that were part of a committed transaction, must be written |
| 148 | to stable storage before calling the <a href="../api_reference/C/txncheckpoint.html" class="olink">DB_ENV->txn_checkpoint()</a> method. This is |
| 149 | to allow the periodic removal of database environment log files.</p> |
| 150 | </div> |
| 151 | <div class="navfooter"> |
| 152 | <hr /> |
| 153 | <table width="100%" summary="Navigation footer"> |
| 154 | <tr> |
| 155 | <td width="40%" align="left"><a accesskey="p" href="apprec_auto.html">Prev</a> </td> |
| 156 | <td width="20%" align="center"> |
| 157 | <a accesskey="u" href="apprec.html">Up</a> |
| 158 | </td> |
| 159 | <td width="40%" align="right"> <a accesskey="n" href="program.html">Next</a></td> |
| 160 | </tr> |
| 161 | <tr> |
| 162 | <td width="40%" align="left" valign="top">Automatically generated functions </td> |
| 163 | <td width="20%" align="center"> |
| 164 | <a accesskey="h" href="index.html">Home</a> |
| 165 | </td> |
| 166 | <td width="40%" align="right" valign="top"> Chapter 15. |
| 167 | Programmer Notes |
| 168 | </td> |
| 169 | </tr> |
| 170 | </table> |
| 171 | </div> |
| 172 | </body> |
| 173 | </html> |