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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Processing Loop</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 Replicated Berkeley DB Applications" />
10 <link rel="up" href="fwrkmasterreplica.html" title="Chapter 4. Replica versus Master Processes" />
11 <link rel="prev" href="fwrkmasterreplica.html" title="Chapter 4. Replica versus Master Processes" />
12 <link rel="next" href="exampledoloop.html" title="Example Processing Loop" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Processing Loop</th>
24 <td width="20%" align="left"><a accesskey="p" href="fwrkmasterreplica.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Replica versus Master Processes</th>
26 <td width="20%" align="right"> <a accesskey="n" href="exampledoloop.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="processingloop"></a>Processing Loop</h2>
40 Typically the central part of any replication application
41 is some sort of a continuous loop that constantly
42 checks the state of the environment (whether it is a
43 replica or a master), opens and/or closes the
44 databases as is necessary, and performs other useful
45 work. A loop such as this one must of necessity
46 take special care to know whether it is operating
47 on a master or a replica environment because all of its
48 activities are dependent upon that state.
51 The flow of activities through the loop will
52 generally be as follows:
54 <div class="orderedlist">
58 Check whether the environment has
59 changed state. If it has, you
60 might want to reopen your
61 database handles, especially if
62 you opened your replica's
63 database handles as read-only.
64 In this case, you might need to
65 reopen them as read-write.
66 However, if you always open your
67 database handles as read-write,
68 then it is not automatically necessary to
69 reopen the databases due to a
70 state change. Instead, you
73 <code class="literal">DB_REP_HANDLE_DEAD</code>
79 database handle(s). If you see
80 this, then you need to reopen
81 your database handle(s).
86 If the databases are closed,
87 create new database handles,
88 configure the handle as is
89 appropriate, and then open the
90 databases. Note that handle
91 configuration will be different,
92 depending on whether the handle
93 is opened as a replica or a
94 master. At a minimum, the master
95 should be opened with database
96 creation privileges, whereas the
97 replica does not need to be. You
98 must also open the master such
99 that its databases are
101 <span class="emphasis"><em>can</em></span> open
102 replicas with read-only
103 databases, so long as you are
104 prepared to close and then reopen
105 the handle in the event the
106 client becomes a master.
109 Also, note that if the local
111 is a replica, then it is possible
112 that databases do not currently
113 exist. In this case, the database
114 open attempts will fail. Your
115 code will have to take this
116 corner case into account
122 Once the databases are opened,
123 check to see if the local
125 master. If it is, do whatever it is
126 a master should do for your
130 Remember that the code for your
131 master should include some way
132 for you to tell the master
138 If the local environment is not a
139 master, then do whatever it is
140 your replica environments should do.
141 Again, like the code for your
142 master environments, you should provide
143 a way for your replicas to exit
144 the processing loop gracefully.
150 The following code fragment illustrates
151 these points (note that we fill out this
152 fragment with a working example
153 next in this chapter):
155 <pre class="programlisting">/* loop to manage replication activities */
166 * Remember that for this to work, an APP_DATA struct would have first
167 * had to been set to the environment handle's app_private data
168 * member. (dbenv is presumably declared and opened in another part of
171 app_data = dbenv->get_app_private();
175 * Infinite loop. We exit depending on how the master and replica code
179 /* If dbp is not opened, we need to open it. */
182 * Create the handle and then configure it. Before you open
183 * it, you have to decide what open flags to use:
185 dbp = new Db(&dbenv, 0);
188 * Now you can open your database handle.
190 * One thing to watch out for is a case where the databases
191 * you are trying to open do not yet exist. This can happen
192 * for replicas where the databases are being opened
193 * read-only. If this happens, ENOENT is returned by the
197 dbp->open(NULL, DATABASE, NULL, DB_BTREE,
198 app_data->is_master ? DB_CREATE | DB_AUTO_COMMIT :
200 } catch(DbException dbe) {
201 if (dbe.get_errno() == ENOENT) {
202 cout << "No stock db available yet - retrying." << endl;
205 } catch (DbException dbe2) {
206 cout << "Unexpected error closing after failed" <<
207 " open, message: " << dbe2.what() << endl;
215 dbenv.err(ret, "DB->open");
222 * Now that the databases have been opened, continue with general
223 * processing, depending on whether we are a master or a replica.
225 if (app_data->is_master) {
227 * Do master stuff here. Don't forget to include a way to
228 * gracefully exit the loop. */
232 * Do replica stuff here. As is the case with the master
233 * code, be sure to include a way to gracefully exit the
239 <div class="navfooter">
241 <table width="100%" summary="Navigation footer">
243 <td width="40%" align="left"><a accesskey="p" href="fwrkmasterreplica.html">Prev</a> </td>
244 <td width="20%" align="center">
245 <a accesskey="u" href="fwrkmasterreplica.html">Up</a>
247 <td width="40%" align="right"> <a accesskey="n" href="exampledoloop.html">Next</a></td>
250 <td width="40%" align="left" valign="top">Chapter 4. Replica versus Master Processes </td>
251 <td width="20%" align="center">
252 <a accesskey="h" href="index.html">Home</a>
254 <td width="40%" align="right" valign="top"> Example Processing Loop</td>