Debian Codegraph @

docs/programmer_reference/cam_app.html

raw symbols SCIP
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>Architecting Data Store and Concurrent Data Store 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="cam.html" title="Chapter 10.  Berkeley DB Concurrent Data Store Applications" />
11 <link rel="prev" href="cam_fail.html" title="Handling failure in Data Store and Concurrent Data Store applications" />
12 <link rel="next" href="transapp.html" title="Chapter 11.  Berkeley DB Transactional Data Store Applications" />
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">Architecting Data Store and Concurrent Data Store applications</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="cam_fail.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 10. 
26 Berkeley DB Concurrent Data Store Applications
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transapp.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="cam_app"></a>Architecting Data Store and Concurrent Data Store applications</h2>
38 </div>
39 </div>
40 </div>
41 <p>When building Data Store and Concurrent Data Store applications, the
42 architecture decisions involve application startup (cleaning up any
43 existing databases, the removal of any existing database environment
44 and creation of a new environment), and handling system or application
45 failure. "Cleaning up" databases involves removal and re-creation
46 of the database, restoration from an archival copy and/or verification
47 and optional salvage, as described in <a class="xref" href="cam_fail.html" title="Handling failure in Data Store and Concurrent Data Store applications">Handling failure in Data Store and Concurrent Data Store applications</a>.</p>
48 <p>Data Store or Concurrent Data Store applications without database
49 environments are single process, by definition. These applications
50 should start up, re-create, restore, or verify and optionally salvage
51 their databases and run until eventual exit or application or system
52 failure. After system or application failure, that process can simply
53 repeat this procedure. This document will not discuss the case of these
54 applications further.</p>
55 <p>Otherwise, the first question of Data Store and Concurrent Data Store
56 architecture is the cleaning up existing databases and the removal of
57 existing database environments, and the subsequent creation of a new
58 environment. For obvious reasons, the application must serialize the
59 re-creation, restoration, or verification and optional salvage of its
60 databases. Further, environment removal and creation must be
61 single-threaded, that is, one thread of control (where a thread of
62 control is either a true thread or a process) must remove and re-create
63 the environment before any other thread of control can use the new
64 environment. It may simplify matters that Berkeley DB serializes creation of
65 the environment, so multiple threads of control attempting to create a
66 environment will serialize behind a single creating thread.</p>
67 <p>Removing a database environment will first mark the environment as
68 "failed", causing any threads of control still running in the
69 environment to fail and return to the application. This feature allows
70 applications to remove environments without concern for threads of
71 control that might still be running in the removed environment.</p>
72 <p>One consideration in removing a database environment which may be in use
73 by another thread, is the type of mutex being used by the Berkeley DB library.
74 In the case of database environment failure when using test-and-set
75 mutexes, threads of control waiting on a mutex when the environment is
76 marked "failed" will quickly notice the failure and will return an error
77 from the Berkeley DB API. In the case of environment failure when using
78 blocking mutexes, where the underlying system mutex implementation does
79 not unblock mutex waiters after the thread of control holding the mutex
80 dies, threads waiting on a mutex when an environment is recovered might
81 hang forever. Applications blocked on events (for example, an
82 application blocked on a network socket or a GUI event) may also fail
83 to notice environment recovery within a reasonable amount of time.
84 Systems with such mutex implementations are rare, but do exist;
85 applications on such systems should use an application architecture
86 where the thread recovering the database environment can explicitly
87 terminate any process using the failed environment, or configure Berkeley DB
88 for test-and-set mutexes, or incorporate some form of long-running timer
89 or watchdog process to wake or kill blocked processes should they block
90 for too long.</p>
91 <p>Regardless, it makes little sense for multiple threads of control to
92 simultaneously attempt to remove and re-create a environment, since the
93 last one to run will remove all environments created by the threads of
94 control that ran before it. However, for some few applications, it may
95 make sense for applications to have a single thread of control that
96 checks the existing databases and removes the environment, after which
97 the application launches a number of processes, any of which are able
98 to create the environment.</p>
99 <p>With respect to cleaning up existing databases, the database environment
100 must be removed before the databases are cleaned up. Removing the
101 environment causes any Berkeley DB library calls made by threads of control
102 running in the failed environment to return failure to the application.
103 Removing the database environment first ensures the threads of control
104 in the old environment do not race with the threads of control cleaning
105 up the databases, possibly overwriting them after the cleanup has
106 finished. Where the application architecture and system permit, many
107 applications kill all threads of control running in the failed database
108 environment before removing the failed database environment, on general
109 principles as well as to minimize overall system resource usage. It
110 does not matter if the new environment is created before or after the
111 databases are cleaned up.</p>
112 <p>After having dealt with database and database environment recovery after
113 failure, the next issue to manage is application failure. As described
114 in <a class="xref" href="cam_fail.html" title="Handling failure in Data Store and Concurrent Data Store applications">Handling failure in Data Store and Concurrent Data Store applications</a>, when a thread of control in a Data Store or
115 Concurrent Data Store application fails, it may exit holding data
116 structure mutexes or logical database locks. These mutexes and locks
117 must be released to avoid the remaining threads of control hanging
118 behind the failed thread of control's mutexes or locks.</p>
119 <p>There are three common ways to architect Berkeley DB Data Store and Concurrent
120 Data Store applications. The one chosen is usually based on whether or
121 not the application is comprised of a single process or group of
122 processes descended from a single process (for example, a server started
123 when the system first boots), or if the application is comprised of
124 unrelated processes (for example, processes started by web connections
125 or users logging into the system).</p>
126 <div class="orderedlist">
127 <ol type="1">
128 <li>The first way to architect Data Store and Concurrent Data Store
129 applications is as a single process (the process may or may not be
130 multithreaded.)
131 <p>When this process starts, it removes any existing database environment
132 and creates a new environment. It then cleans up the databases and
133 opens those databases in the environment. The application can
134 subsequently create new threads of control as it chooses. Those threads
135 of control can either share already open Berkeley DB <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> and
136 <a href="../api_reference/C/db.html" class="olink">DB</a> handles, or create their own. In this architecture,
137 databases are rarely opened or closed when more than a single thread of
138 control is running; that is, they are opened when only a single thread
139 is running, and closed after all threads but one have exited. The last
140 thread of control to exit closes the databases and the database
141 environment.</p><p>This architecture is simplest to implement because thread serialization
142 is easy and failure detection does not require monitoring multiple
143 processes.</p><p>If the application's thread model allows the process to continue after
144 thread failure, the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> method can be used to determine if
145 the database environment is usable after the failure. If the
146 application does not call <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a>, or
147 <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> returns <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>, the application
148 must behave as if there has been a system failure, removing the
149 environment and creating a new environment, and cleaning up any
150 databases it wants to continue to use. Once these actions have been
151 taken, other threads of control can continue (as long as all existing
152 Berkeley DB handles are first discarded), or restarted.</p></li>
153 <li>The second way to architect Data Store and Concurrent Data Store
154 applications is as a group of related processes (the processes may or
155 may not be multithreaded).
156 <p>This architecture requires the order in which threads of control are
157 created be controlled to serialize database environment removal and
158 creation, and database cleanup.</p><p>In addition, this architecture requires that threads of control be
159 monitored. If any thread of control exits with open Berkeley DB handles, the
160 application may call the <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> method to determine if the
161 database environment is usable after the exit. If the application does
162 not call <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a>, or <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> returns
163 <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>, the application must behave as if there has been
164 a system failure, removing the environment and creating a new
165 environment, and cleaning up any databases it wants to continue to use.
166 Once these actions have been taken, other threads of control can
167 continue (as long as all existing Berkeley DB handles are first discarded),
168 or restarted.</p><p>The easiest way to structure groups of related processes is to first
169 create a single "watcher" process (often a script) that starts when the
170 system first boots, removes and creates the database environment, cleans
171 up the databases and then creates the processes or threads that will
172 actually perform work. The initial thread has no further
173 responsibilities other than to wait on the threads of control it has
174 started, to ensure none of them unexpectedly exit. If a thread of
175 control exits, the watcher process optionally calls the
176 <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> method. If the application does not call
177 <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a>, or if <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> returns
178 <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>, the environment can no longer be used, the
179 watcher kills all of the threads of control using the failed
180 environment, cleans up, and starts new threads of control to perform
181 work.</p></li>
182 <li>The third way to architect Data Store and Concurrent Data Store
183 applications is as a group of unrelated processes (the processes may or
184 may not be multithreaded). This is the most difficult architecture to
185 implement because of the level of difficulty in some systems of finding
186 and monitoring unrelated processes.
187 <p>One solution is to log a thread of control ID when a new Berkeley DB handle
188 is opened. For example, an initial "watcher" process could open/create
189 the database environment, clean up the databases and then create a
190 sentinel file. Any "worker" process wanting to use the environment
191 would check for the sentinel file. If the sentinel file does not exist,
192 the worker would fail or wait for the sentinel file to be created. Once
193 the sentinel file exists, the worker would register its process ID with
194 the watcher (via shared memory, IPC or some other registry mechanism),
195 and then the worker would open its <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handles and proceed.
196 When the worker finishes using the environment, it would unregister its
197 process ID with the watcher. The watcher periodically checks to ensure
198 that no worker has failed while using the environment. If a worker
199 fails while using the environment, the watcher removes the sentinel
200 file, kills all of the workers currently using the environment, cleans
201 up the environment and databases, and finally creates a new sentinel
202 file.</p><p>The weakness of this approach is that, on some systems, it is difficult
203 to determine if an unrelated process is still running. For example,
204 POSIX systems generally disallow sending signals to unrelated processes.
205 The trick to monitoring unrelated processes is to find a system resource
206 held by the process that will be modified if the process dies. On POSIX
207 systems, flock- or fcntl-style locking will work, as will LockFile on
208 Windows systems. Other systems may have to use other process-related
209 information such as file reference counts or modification times. In the
210 worst case, threads of control can be required to periodically
211 re-register with the watcher process: if the watcher has not heard from
212 a thread of control in a specified period of time, the watcher will take
213 action, cleaning up the environment.</p><p>If it is not practical to monitor the processes sharing a database
214 environment, it may be possible to monitor the environment to detect if
215 a thread of control has failed holding open Berkeley DB handles. This would
216 be done by having a "watcher" process periodically call the
217 <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> method. If <a href="../api_reference/C/envfailchk.html" class="olink">DB_ENV-&gt;failchk()</a> returns
218 <a class="link" href="program_errorret.html#program_errorret.DB_RUNRECOVERY">DB_RUNRECOVERY</a>, the watcher would then take action, cleaning up
219 the environment.</p><p>The weakness of this approach is that all threads of control using the
220 environment must specify an "ID" function and an "is-alive" function
221 using the <a href="../api_reference/C/envset_thread_id.html" class="olink">DB_ENV-&gt;set_thread_id()</a> method. (In other words, the Berkeley DB
222 library must be able to assign a unique ID to each thread of control,
223 and additionally determine if the thread of control is still running.
224 It can be difficult to portably provide that information in applications
225 using a variety of different programming languages and running on a
226 variety of different platforms.)</p></li>
227 </ol>
228 </div>
229 <p>Obviously, when implementing a process to monitor other threads of
230 control, it is important the watcher process' code be as simple and
231 well-tested as possible, because the application may hang if it fails.</p>
232 </div>
233 <div class="navfooter">
234 <hr />
235 <table width="100%" summary="Navigation footer">
236 <tr>
237 <td width="40%" align="left"><a accesskey="p" href="cam_fail.html">Prev</a> </td>
238 <td width="20%" align="center">
239 <a accesskey="u" href="cam.html">Up</a>
240 </td>
241 <td width="40%" align="right"> <a accesskey="n" href="transapp.html">Next</a></td>
242 </tr>
243 <tr>
244 <td width="40%" align="left" valign="top">Handling failure in Data Store and Concurrent Data Store applications </td>
245 <td width="20%" align="center">
246 <a accesskey="h" href="index.html">Home</a>
247 </td>
248 <td width="40%" align="right" valign="top"> Chapter 11. 
249 Berkeley DB Transactional Data Store Applications
250 </td>
251 </tr>
252 </table>
253 </div>
254 </body>
255 </html>