Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / docs / gsg_db_rep / CXX / processingloop.html
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>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" />
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">Processing Loop</th>
22         </tr>
23         <tr>
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>
27         </tr>
28       </table>
29       <hr />
30     </div>
31     <div class="sect1" lang="en" xml:lang="en">
32       <div class="titlepage">
33         <div>
34           <div>
35             <h2 class="title" style="clear: both"><a id="processingloop"></a>Processing Loop</h2>
36           </div>
37         </div>
38       </div>
39       <p>
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.
49                   </p>
50       <p>
51                           The flow of activities through the loop will
52                           generally be as follows:
53                   </p>
54       <div class="orderedlist">
55         <ol type="1">
56           <li>
57             <p>
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
71                                           could check for a
72                                           <span>
73                                             <code class="literal">DB_REP_HANDLE_DEAD</code>
74                                             return code
75                                           </span>
76                                           
77
78                                           when you use your
79                                           database handle(s). If you see
80                                           this, then you need to reopen
81                                           your database handle(s). 
82                                   </p>
83           </li>
84           <li>
85             <p>
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
100                                           read-write. You
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.
107                                   </p>
108             <p>
109                                           Also, note that if the local
110                                           environment 
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
117                                           (described below).
118                                   </p>
119           </li>
120           <li>
121             <p>
122                                         Once the databases are opened,
123                                         check to see if the local
124                                         environment is a
125                                         master. If it is, do whatever it is
126                                         a master should do for your
127                                         application.
128                                   </p>
129             <p>
130                                           Remember that the code for your
131                                           master should include some way
132                                           for you to tell the master 
133                                           to exit gracefully.
134                                   </p>
135           </li>
136           <li>
137             <p>
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.
145                                   </p>
146           </li>
147         </ol>
148       </div>
149       <p>
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):
154                   </p>
155       <pre class="programlisting">/* loop to manage replication activities */
156
157 Db *dbp;
158 int ret;
159 APP_DATA *app_data;
160 u_int32_t flags;
161
162 dbp = NULL;
163 ret = 0;
164
165 /* 
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
169  * the code.)
170  */
171 app_data = dbenv-&gt;get_app_private();
172
173
174 /* 
175  * Infinite loop. We exit depending on how the master and replica code
176  * is written.
177  */
178 for (;;) {
179     /* If dbp is not opened, we need to open it. */
180     if (dbp == 0) {
181         /* 
182          * Create the handle and then configure it. Before you open
183          * it, you have to decide what open flags to use:
184          */
185          dbp = new Db(&amp;dbenv, 0);
186
187         /*
188          * Now you can open your database handle.
189          *
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 
194          * open() call.
195          */
196          try {
197             dbp-&gt;open(NULL, DATABASE, NULL, DB_BTREE,
198                     app_data-&gt;is_master ? DB_CREATE | DB_AUTO_COMMIT :
199                     DB_AUTO_COMMIT, 0);
200          } catch(DbException dbe) {
201             if (dbe.get_errno() == ENOENT) {
202                 cout &lt;&lt; "No stock db available yet - retrying." &lt;&lt; endl;
203                 try {
204                     dbp-&gt;close(0);
205                 } catch (DbException dbe2) {
206                     cout &lt;&lt; "Unexpected error closing after failed" &lt;&lt;
207                             " open, message: " &lt;&lt; dbe2.what() &lt;&lt; endl;
208                     dbp = NULL;
209                     goto err;
210                  }
211                  dbp = NULL;
212                  sleep(SLEEPTIME);
213                  continue;
214             } else {
215                 dbenv.err(ret, "DB-&gt;open");
216                 throw dbe;
217             }
218          }
219     }
220
221     /*
222      * Now that the databases have been opened, continue with general
223      * processing, depending on whether we are a master or a replica.
224      */
225      if (app_data-&gt;is_master) {
226         /* 
227          * Do master stuff here. Don't forget to include a way to
228          * gracefully exit the loop. */
229          */
230      } else {
231         /* 
232          * Do replica stuff here. As is the case with the master
233          * code, be sure to include a way to gracefully exit the
234          * loop. 
235          */
236      }
237 } </pre>
238     </div>
239     <div class="navfooter">
240       <hr />
241       <table width="100%" summary="Navigation footer">
242         <tr>
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>
246           </td>
247           <td width="40%" align="right"> <a accesskey="n" href="exampledoloop.html">Next</a></td>
248         </tr>
249         <tr>
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>
253           </td>
254           <td width="40%" align="right" valign="top"> Example Processing Loop</td>
255         </tr>
256       </table>
257     </div>
258   </body>
259 </html>