| 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>Building replicated 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="rep.html" title="Chapter 12. Berkeley DB Replication" /> |
| 11 | <link rel="prev" href="rep_pri.html" title="Replication environment priorities" /> |
| 12 | <link rel="next" href="rep_mgr_meth.html" title="Replication Manager methods" /> |
| 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">Building replicated applications</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_pri.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 12. |
| 26 | Berkeley DB Replication |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="rep_mgr_meth.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="rep_app"></a>Building replicated applications</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The simplest way to build a replicated Berkeley DB application is to first |
| 42 | build (and debug!) the transactional version of the same application. |
| 43 | Then, add a thin replication layer: application initialization must be |
| 44 | changed and the application's communication infrastructure must be |
| 45 | added.</p> |
| 46 | <p>The application initialization changes are relatively simple. |
| 47 | Replication Manager provides a communication infrastructure, but |
| 48 | in order to use the replication Base APIs you must provide your own.</p> |
| 49 | <p>For implementation reasons, all replicated databases must reside in |
| 50 | the data directories set from <a href="../api_reference/C/envset_data_dir.html" class="olink">DB_ENV->set_data_dir()</a> (or in the |
| 51 | default environment home directory, if not using |
| 52 | <a href="../api_reference/C/envset_data_dir.html" class="olink">DB_ENV->set_data_dir()</a>), rather than in a subdirectory below the |
| 53 | specified directory. Care must be taken in applications using |
| 54 | relative pathnames and changing working directories after opening the |
| 55 | environment. In such applications the replication initialization code |
| 56 | may not be able to locate the databases, and applications that change |
| 57 | their working directories may need to use absolute pathnames.</p> |
| 58 | <p>During application initialization, the application performs |
| 59 | three additional tasks: first, it must specify the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_REP" class="olink">DB_INIT_REP</a> |
| 60 | flag when opening its database environment and additionally, a |
| 61 | Replication Manager application must also specify the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag; |
| 62 | second, it must provide Berkeley DB information about its communications |
| 63 | infrastructure; and third, it must start the Berkeley DB replication system. |
| 64 | Generally, a replicated application will do normal Berkeley DB recovery and |
| 65 | configuration, exactly like any other transactional application.</p> |
| 66 | <p> |
| 67 | Replication Manager applications configure the built-in communications |
| 68 | infrastructure by calling obtaining a <a href="../api_reference/C/db_site.html" class="olink">DB_SITE</a> handle, and then using |
| 69 | it to configure the local site. It can optionally obtain one or more |
| 70 | <a href="../api_reference/C/db_site.html" class="olink">DB_SITE</a> handles to configure remote sites. Once the environment has |
| 71 | been opened, the application starts the replication system by calling |
| 72 | the <a href="../api_reference/C/repmgrstart.html" class="olink">DB_ENV->repmgr_start()</a> method. |
| 73 | </p> |
| 74 | <p>A Base API application calls the |
| 75 | <a href="../api_reference/C/reptransport.html" class="olink">DB_ENV->rep_set_transport()</a> method to configure the entry point to its own |
| 76 | communications infrastructure, and then calls the |
| 77 | <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> method to join or create the replication group.</p> |
| 78 | <p>When starting the replication system, an application has two choices: |
| 79 | it may choose the group master site explicitly, or alternatively it |
| 80 | may configure all group members as clients and then call for an |
| 81 | election, letting the clients select the master from among |
| 82 | themselves. Either is correct, and the choice is entirely up to the |
| 83 | application.</p> |
| 84 | <p>Replication Manager applications make this choice simply by setting |
| 85 | the flags parameter to the <a href="../api_reference/C/repmgrstart.html" class="olink">DB_ENV->repmgr_start()</a> method.</p> |
| 86 | <p>For a Base API application, the result of |
| 87 | calling <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> is usually the discovery of a master, or the |
| 88 | declaration of the local environment as the master. If a master has |
| 89 | not been discovered after a reasonable amount of time, the application |
| 90 | should call <a href="../api_reference/C/repelect.html" class="olink">DB_ENV->rep_elect()</a> to call for an election.</p> |
| 91 | <p>Consider a Base API application with multiple processes or multiple |
| 92 | environment handles |
| 93 | that modify databases in the replicated environment. All modifications |
| 94 | must be done on the master environment. The first process to join or |
| 95 | create the master environment must call both the |
| 96 | <a href="../api_reference/C/reptransport.html" class="olink">DB_ENV->rep_set_transport()</a> and the <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> method. Subsequent |
| 97 | replication processes must at least call the <a href="../api_reference/C/reptransport.html" class="olink">DB_ENV->rep_set_transport()</a> method. |
| 98 | Those processes may call the <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a> method (as long as they use the |
| 99 | same master or client argument). If multiple processes are modifying |
| 100 | the master environment there must be a unified communication |
| 101 | infrastructure such that messages arriving at clients have a single |
| 102 | master ID. Additionally the application must be structured so that all |
| 103 | incoming messages are able to be processed by a single <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> |
| 104 | handle.</p> |
| 105 | <p>Note that not all processes running in replicated environments need to |
| 106 | call <a href="../api_reference/C/repmgrstart.html" class="olink">DB_ENV->repmgr_start()</a>, <a href="../api_reference/C/reptransport.html" class="olink">DB_ENV->rep_set_transport()</a> or <a href="../api_reference/C/repstart.html" class="olink">DB_ENV->rep_start()</a>. Read-only |
| 107 | processes running in a master environment do not need to be configured |
| 108 | for replication in any way. Processes running in a client environment |
| 109 | are read-only by definition, and so do not need to be configured for |
| 110 | replication either (although, in the case of clients that may become |
| 111 | masters, it is usually simplest to configure for replication on process |
| 112 | startup rather than trying to reconfigure when the client becomes a |
| 113 | master). Obviously, at least one thread of control on each client must |
| 114 | be configured for replication as messages must be passed between the |
| 115 | master and the client.</p> |
| 116 | <p>Any site in a replication group may have its own private |
| 117 | transactional databases in the environment as well. A site may |
| 118 | create a local database by specifying the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_TXN_NOT_DURABLE" class="olink">DB_TXN_NOT_DURABLE</a> |
| 119 | flag to the <a href="../api_reference/C/dbset_flags.html" class="olink">DB->set_flags()</a> method. The application |
| 120 | must never create a private database with the same name |
| 121 | as a database replicated across the entire environment |
| 122 | as data corruption can result.</p> |
| 123 | <p>For implementation reasons, Base API applications must process |
| 124 | all incoming replication messages |
| 125 | using the same <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handle. It is not required that |
| 126 | a single thread of control process all messages, only that all threads |
| 127 | of control processing messages use the same handle.</p> |
| 128 | <p>No additional calls are required to shut down a database environment |
| 129 | participating in a replication group. The application should shut down |
| 130 | the environment in the usual manner, by calling the <a href="../api_reference/C/envclose.html" class="olink">DB_ENV->close()</a> method. |
| 131 | For Replication Manager applications, this also terminates all network |
| 132 | connections and background processing threads.</p> |
| 133 | </div> |
| 134 | <div class="navfooter"> |
| 135 | <hr /> |
| 136 | <table width="100%" summary="Navigation footer"> |
| 137 | <tr> |
| 138 | <td width="40%" align="left"><a accesskey="p" href="rep_pri.html">Prev</a> </td> |
| 139 | <td width="20%" align="center"> |
| 140 | <a accesskey="u" href="rep.html">Up</a> |
| 141 | </td> |
| 142 | <td width="40%" align="right"> <a accesskey="n" href="rep_mgr_meth.html">Next</a></td> |
| 143 | </tr> |
| 144 | <tr> |
| 145 | <td width="40%" align="left" valign="top">Replication environment priorities </td> |
| 146 | <td width="20%" align="center"> |
| 147 | <a accesskey="h" href="index.html">Home</a> |
| 148 | </td> |
| 149 | <td width="40%" align="right" valign="top"> Replication Manager methods</td> |
| 150 | </tr> |
| 151 | </table> |
| 152 | </div> |
| 153 | </body> |
| 154 | </html> |