| 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>Checkpoints</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_deadlock.html" title="Deadlock detection" /> |
| 12 | <link rel="next" href="transapp_archival.html" title="Database and log file archival" /> |
| 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">Checkpoints</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_deadlock.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_archival.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_checkpoint"></a>Checkpoints</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The second component of the infrastructure is performing checkpoints of |
| 42 | the log files. Performing checkpoints is necessary for two reasons.</p> |
| 43 | <p>First, you may be able to remove Berkeley DB log files from your database |
| 44 | environment after a checkpoint. Change records are written into the log |
| 45 | files when databases are modified, but the actual changes to the |
| 46 | database are not necessarily written to disk. When a checkpoint is |
| 47 | performed, changes to the database are written into the backing database |
| 48 | file. Once the database pages are written, log files can be archived |
| 49 | and removed from the database environment because they will never be |
| 50 | needed for anything other than catastrophic failure. (Log files which |
| 51 | are involved in active transactions may not be removed, and there must |
| 52 | always be at least one log file in the database environment.)</p> |
| 53 | <p>The second reason to perform checkpoints is because checkpoint frequency |
| 54 | is inversely proportional to the amount of time it takes to run database |
| 55 | recovery after a system or application failure. This is because |
| 56 | recovery after failure has to redo or undo changes only since the last |
| 57 | checkpoint, as changes before the checkpoint have all been flushed to |
| 58 | the databases.</p> |
| 59 | <p> |
| 60 | Berkeley DB provides the <a href="../api_reference/C/db_checkpoint.html" class="olink">db_checkpoint</a> utility, which can be used to perform |
| 61 | checkpoints. Alternatively, applications can write their own |
| 62 | checkpoint thread using the underlying <a href="../api_reference/C/txncheckpoint.html" class="olink">DB_ENV->txn_checkpoint()</a> function. The |
| 63 | following code fragment checkpoints the database environment every 60 |
| 64 | seconds: |
| 65 | </p> |
| 66 | <pre class="programlisting">int |
| 67 | main(int argc, char *argv) |
| 68 | { |
| 69 | extern int optind; |
| 70 | DB *db_cats, *db_color, *db_fruit; |
| 71 | DB_ENV *dbenv; |
| 72 | pthread_t ptid; |
| 73 | int ch; |
| 74 | |
| 75 | while ((ch = getopt(argc, argv, "")) != EOF) |
| 76 | switch (ch) { |
| 77 | case '?': |
| 78 | default: |
| 79 | usage(); |
| 80 | } |
| 81 | argc -= optind; |
| 82 | argv += optind; |
| 83 | |
| 84 | env_dir_create(); |
| 85 | env_open(&dbenv); |
| 86 | |
| 87 | <span class="bold"><strong> /* Start a checkpoint thread. */ |
| 88 | if ((errno = pthread_create( |
| 89 | &ptid, NULL, checkpoint_thread, (void *)dbenv)) != 0) { |
| 90 | fprintf(stderr, |
| 91 | "txnapp: failed spawning checkpoint thread: %s\n", |
| 92 | strerror(errno)); |
| 93 | exit (1); |
| 94 | }</strong></span> |
| 95 | |
| 96 | /* Open database: Key is fruit class; Data is specific type. */ |
| 97 | db_open(dbenv, &db_fruit, "fruit", 0); |
| 98 | |
| 99 | /* Open database: Key is a color; Data is an integer. */ |
| 100 | db_open(dbenv, &db_color, "color", 0); |
| 101 | |
| 102 | /* |
| 103 | * Open database: |
| 104 | * Key is a name; Data is: company name, cat breeds. |
| 105 | */ |
| 106 | db_open(dbenv, &db_cats, "cats", 1); |
| 107 | |
| 108 | add_fruit(dbenv, db_fruit, "apple", "yellow delicious"); |
| 109 | |
| 110 | add_color(dbenv, db_color, "blue", 0); |
| 111 | add_color(dbenv, db_color, "blue", 3); |
| 112 | |
| 113 | add_cat(dbenv, db_cats, |
| 114 | "Amy Adams", |
| 115 | "Oracle", |
| 116 | "abyssinian", |
| 117 | "bengal", |
| 118 | "chartreaux", |
| 119 | NULL); |
| 120 | |
| 121 | return (0); |
| 122 | } |
| 123 | |
| 124 | <span class="bold"><strong>void * |
| 125 | checkpoint_thread(void *arg) |
| 126 | { |
| 127 | DB_ENV *dbenv; |
| 128 | int ret; |
| 129 | |
| 130 | dbenv = arg; |
| 131 | dbenv->errx(dbenv, "Checkpoint thread: %lu", (u_long)pthread_self()); |
| 132 | |
| 133 | /* Checkpoint once a minute. */ |
| 134 | for (;; sleep(60)) |
| 135 | if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0)) != 0) { |
| 136 | dbenv->err(dbenv, ret, "checkpoint thread"); |
| 137 | exit (1); |
| 138 | } |
| 139 | |
| 140 | /* NOTREACHED */ |
| 141 | }</strong></span></pre> |
| 142 | <p>Because checkpoints can be quite expensive, choosing how often to |
| 143 | perform a checkpoint is a common tuning parameter for Berkeley DB |
| 144 | applications.</p> |
| 145 | </div> |
| 146 | <div class="navfooter"> |
| 147 | <hr /> |
| 148 | <table width="100%" summary="Navigation footer"> |
| 149 | <tr> |
| 150 | <td width="40%" align="left"><a accesskey="p" href="transapp_deadlock.html">Prev</a> </td> |
| 151 | <td width="20%" align="center"> |
| 152 | <a accesskey="u" href="transapp.html">Up</a> |
| 153 | </td> |
| 154 | <td width="40%" align="right"> <a accesskey="n" href="transapp_archival.html">Next</a></td> |
| 155 | </tr> |
| 156 | <tr> |
| 157 | <td width="40%" align="left" valign="top">Deadlock detection </td> |
| 158 | <td width="20%" align="center"> |
| 159 | <a accesskey="h" href="index.html">Home</a> |
| 160 | </td> |
| 161 | <td width="40%" align="right" valign="top"> Database and log file archival</td> |
| 162 | </tr> |
| 163 | </table> |
| 164 | </div> |
| 165 | </body> |
| 166 | </html> |