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>Configuring the Logging Subsystem</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 Berkeley DB Transaction Processing" />
10    <link rel="up" href="filemanagement.html" title="Chapter 5. Managing DB Files" />
11    <link rel="prev" href="logfileremoval.html" title="Removing Log Files" />
12    <link rel="next" href="wrapup.html" title="Chapter 6. Summary and Examples" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Configuring the Logging Subsystem</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="logfileremoval.html">Prev</a> </td>
22          <th width="60%" align="center">Chapter 5. Managing DB Files</th>
23          <td width="20%" align="right"> <a accesskey="n" href="wrapup.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="logconfig"></a>Configuring the Logging Subsystem</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <dl>
38          <dt>
39            <span class="sect2">
40              <a href="logconfig.html#logfilesize">Setting the Log File Size</a>
41            </span>
42          </dt>
43          <dt>
44            <span class="sect2">
45              <a href="logconfig.html#logregionsize">Configuring the Logging Region Size</a>
46            </span>
47          </dt>
48          <dt>
49            <span class="sect2">
50              <a href="logconfig.html#inmemorylogging">Configuring In-Memory Logging</a>
51            </span>
52          </dt>
53          <dt>
54            <span class="sect2">
55              <a href="logconfig.html#logbuffer">Setting the In-Memory Log Buffer Size</a>
56            </span>
57          </dt>
58        </dl>
59      </div>
60      <p>
61            You can configure the following aspects of the logging
62            subsystem:
63        </p>
64      <div class="itemizedlist">
65        <ul type="disc">
66          <li>
67            <p>
68                    Size of the log files.
69                </p>
70          </li>
71          <li>
72            <p>
73                    Size of the logging subsystem's region.
74                    See <a class="xref" href="logconfig.html#logregionsize" title="Configuring the Logging Region Size">Configuring the Logging Region Size</a>.
75                </p>
76          </li>
77          <li>
78            <p>
79                    Maintain logs entirely in-memory. 
80                    See <a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>
81                    for more information.
82                </p>
83          </li>
84          <li>
85            <p>
86                    Size of the log buffer in memory.
87                    See <a class="xref" href="logconfig.html#logbuffer" title="Setting the In-Memory Log Buffer Size">Setting the In-Memory Log Buffer Size</a>.
88                </p>
89          </li>
90          <li>
91            <p>
92                    On-disk location of your log files. See 
93                    <a class="xref" href="enabletxn.html#splittingdata" title="Identifying Specific File Locations">Identifying Specific File Locations</a>.
94                </p>
95          </li>
96        </ul>
97      </div>
98      <div class="sect2" lang="en" xml:lang="en">
99        <div class="titlepage">
100          <div>
101            <div>
102              <h3 class="title"><a id="logfilesize"></a>Setting the Log File Size</h3>
103            </div>
104          </div>
105        </div>
106        <p>
107                    Whenever a pre-defined amount of data is written to a
108                    log file (10 MB by default), DB stops using the
109                    current log file and starts writing to a new file.
110                    You can change the maximum amount of data contained in each
111                    log file by using the 
112                        <span>
113                            
114                            <code class="methodname">DbEnv::set_lg_max()</code>
115                            method.
116                        </span>
117                        
118                        Note that this method can be used at any time
119                        during an application's lifetime.
120                </p>
121        <p>
122                    Setting the log file size to something larger than its
123                    default value is largely a matter of
124                    convenience and a reflection of the application's
125                    preference in backup media and frequency. However, if
126                    you set the log file size too low relative to your
127                    application's traffic patterns, you can cause
128                    yourself trouble.
129                </p>
130        <p>
131                    From a performance perspective, setting the log file
132                    size to a low value can cause your active transactions to
133                    pause their writing activities more frequently than would
134                    occur with larger log file sizes. Whenever a
135                    transaction completes the log buffer is flushed to
136                    disk. Normally other transactions can continue to
137                    write to the log buffer while this flush is in
138                    progress. However, when one log file is being closed
139                    and another created, all transactions must cease
140                    writing to the log buffer until the switch over is
141                    completed. 
142                </p>
143        <p>
144                    Beyond performance concerns, using smaller log files
145                    can cause you to use more physical files on disk.
146                    As a result, your application could run out
147                    of log sequence numbers, depending on how busy your
148                    application is.
149                </p>
150        <p>
151                    Every log file is identified with a 10 digit number.
152                    Moreover, the maximum number of log files that your application
153                    is allowed to create in its lifetime is 2,000,000,000.
154                </p>
155        <p>
156                    For example, if your application performs 6,000 transactions per
157                    second for 24 hours a day, and you are logging 500 bytes of
158                    data per transaction into 10 MB log files, then you
159                    will run out of log files in around 221 years:
160                </p>
161        <pre class="programlisting">     (10 * 2^20 * 2000000000) / (6000 * 500 * 365 * 60 *60 * 24) = 221 </pre>
162        <p>
163                    However, if you were writing 2000 bytes of data per
164                    transaction, and using 1 MB log files, then the same
165                    formula shows you running out of log files in 5 years time.
166                </p>
167        <p>
168                    All of these time frames are quite long, to be sure,
169                    but if you do run out of log files after, say, 5 years
170                    of continuous operations, then you must reset your log
171                    sequence numbers. To do so:
172                </p>
173        <div class="orderedlist">
174          <ol type="1">
175            <li>
176              <p>
177                            Backup your databases as if to prepare for
178                            catastrophic failure. See 
179                            <a class="xref" href="backuprestore.html" title="Backup Procedures">Backup Procedures</a>
180                            for more information.
181                        </p>
182            </li>
183            <li>
184              <p>
185                            Reset the log file's sequence number using the
186                            <span class="command"><strong>db_load</strong></span> utility's
187                            <code class="literal">-r</code> option.
188                        </p>
189            </li>
190            <li>
191              <p>
192                            Remove all of the log files from your
193                            environment. Note that this is the only
194                            situation in which all of the log files are
195                            removed from an environment; in all other
196                            cases, at least a single log file is retained.
197                        </p>
198            </li>
199            <li>
200              <p>
201                            Restart your application.
202                        </p>
203            </li>
204          </ol>
205        </div>
206      </div>
207      <div class="sect2" lang="en" xml:lang="en">
208        <div class="titlepage">
209          <div>
210            <div>
211              <h3 class="title"><a id="logregionsize"></a>Configuring the Logging Region Size</h3>
212            </div>
213          </div>
214        </div>
215        <p>
216                The logging subsystem's default region size is 60 KB. The
217                logging region is used to store filenames, and so you may
218                need to increase its size if a large number of files (that
219                is, if you have a very large number of databases) will
220                be opened and registered with DB's log manager.
221            </p>
222        <p>
223                You can set the size of your logging region by using the 
224                <span>
225                    
226                    <code class="methodname">DbEnv::set_lg_region()</code>
227                </span>
228                
229                method. Note that this method can only be called before the
230                first environment handle for your application is opened.
231            </p>
232      </div>
233      <div class="sect2" lang="en" xml:lang="en">
234        <div class="titlepage">
235          <div>
236            <div>
237              <h3 class="title"><a id="inmemorylogging"></a>Configuring In-Memory Logging</h3>
238            </div>
239          </div>
240        </div>
241        <p>
242                It is possible to configure your logging subsystem such
243                that logs are maintained entirely in memory. When
244                you do this, you give up your transactional durability
245                guarantee. Without log files, you have no way to run
246                recovery so any system or software failures that you might
247                experience can corrupt your databases.
248            </p>
249        <p>
250                However, by giving up your durability guarantees, you can
251                greatly improve your application's throughput by avoiding
252                the disk I/O necessary to write logging information to
253                disk. In this case, you still retain your transactional
254                atomicity, consistency, and isolation guarantees.
255            </p>
256        <p>
257                To configure your logging subsystem to maintain your logs
258                entirely in-memory:
259            </p>
260        <div class="itemizedlist">
261          <ul type="disc">
262            <li>
263              <p>
264                        Make sure your log buffer is capable of holding all 
265                        log information that can accumulate during the longest
266                        running transaction. See <a class="xref" href="logconfig.html#logbuffer" title="Setting the In-Memory Log Buffer Size">Setting the In-Memory Log Buffer Size</a> for details.
267                    </p>
268            </li>
269            <li>
270              <p>
271                        Do not run normal recovery when you open your environment.  In this configuration, there are no
272                        log files available against which you can run recovery. As a result, if you specify recovery
273                        when you open your environment, it is ignored.
274                    </p>
275            </li>
276            <li>
277              <p>
278                        Specify
279                            <span>
280                                <code class="literal">DB_LOG_IN_MEMORY</code> to the
281                                
282                                <code class="methodname">DbEnv::log_set_config()</code>
283                            </span>
284                            
285                            method. Note that you must specify this before
286                            your application opens its first environment
287                            handle.
288                    </p>
289            </li>
290          </ul>
291        </div>
292        <p>
293                For example:
294            </p>
295        <pre class="programlisting">#include "db_cxx.h"
296
297...
298                                                                                                                                  
299int main(void)
300{
301    // Set the normal flags for a transactional subsystem. Note that
302    // we DO NOT specify DB_RECOVER.
303    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
304                                           // exist, create it.
305                          DB_INIT_LOCK  |  // Initialize locking
306                          DB_INIT_LOG   |  // Initialize logging
307                          DB_INIT_MPOOL |  // Initialize the cache
308                          DB_THREAD     |  // Free-thread the env handle
309                          DB_INIT_TXN;     // Initialize transactions
310
311    std::string envHome("/export1/testEnv");
312    DbEnv myEnv(0);
313
314    try {
315
316        // Indicate that logging is to be performed only in memory. 
317        // Doing this means that we give up our transactional durability
318        // guarantee.
319        myEnv.log_set_config(DB_LOG_IN_MEMORY, 1);
320
321        // Configure the size of our log memory buffer. This must be
322        // large enough to hold all the logging information likely
323        // to be created for our longest running transaction. The
324        // default size for the logging buffer is 1 MB when logging
325        // is performed in-memory. For this example, we arbitrarily
326        // set the logging buffer to 5 MB.
327        myEnv.set_lg_bsize(5 * 1024 * 1024);
328
329        // Open the environment as normal.
330        myEnv.open(envHome.c_str(), env_flags, 0);
331
332    } catch(DbException &amp;e) {
333        std::cerr &lt;&lt; "Error opening database and environment: "
334                  &lt;&lt; file_name &lt;&lt; ", "
335                  &lt;&lt; envHome &lt;&lt; std::endl;
336        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
337    }
338
339    // From here, you open databases, create transactions and 
340    // perform database operations exactly as you would if you 
341    // were logging to disk. This part is omitted for brevity.  </pre>
342      </div>
343      <div class="sect2" lang="en" xml:lang="en">
344        <div class="titlepage">
345          <div>
346            <div>
347              <h3 class="title"><a id="logbuffer"></a>Setting the In-Memory Log Buffer Size</h3>
348            </div>
349          </div>
350        </div>
351        <p>
352                When your application is configured for on-disk logging
353                (the default behavior for transactional applications), log
354                information is stored in-memory until the storage space
355                fills up, or a transaction commit forces the log
356                information to be flushed to disk. 
357            </p>
358        <p>
359                It is possible to increase the amount of memory available
360                to your file log buffer. Doing so improves throughput for
361                long-running transactions, or for transactions that produce
362                a large amount of data.
363            </p>
364        <p>
365                When you have your logging subsystem configured to maintain
366                your log entirely in memory (see
367                <a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>), it is very important
368                to configure your log buffer size because the log buffer
369                must be capable of holding all log information that can
370                accumulate during the longest running transaction.
371                You must make sure that the in-memory log buffer size is
372                large enough that no transaction will ever span the entire
373                buffer. You must also avoid a state where the in-memory
374                buffer is full and no space can be freed because a
375                transaction that started the first log "file" is still
376                active.
377            </p>
378        <p>
379                When your logging subsystem is configured for on-disk
380                logging, the default log buffer space is 32 KB. When
381                in-memory logging is configured, the default log buffer
382                space is 1 MB.
383            </p>
384        <p>
385                You can increase your log buffer space using the 
386                <span>
387                    
388                    <code class="methodname">DbEnv::set_lg_bsize()</code>
389                </span>
390                
391                method. Note that this method can only be called before the
392                first environment handle for your application is opened.
393            </p>
394      </div>
395    </div>
396    <div class="navfooter">
397      <hr />
398      <table width="100%" summary="Navigation footer">
399        <tr>
400          <td width="40%" align="left"><a accesskey="p" href="logfileremoval.html">Prev</a> </td>
401          <td width="20%" align="center">
402            <a accesskey="u" href="filemanagement.html">Up</a>
403          </td>
404          <td width="40%" align="right"> <a accesskey="n" href="wrapup.html">Next</a></td>
405        </tr>
406        <tr>
407          <td width="40%" align="left" valign="top">Removing Log Files </td>
408          <td width="20%" align="center">
409            <a accesskey="h" href="index.html">Home</a>
410          </td>
411          <td width="40%" align="right" valign="top"> Chapter 6. Summary and Examples</td>
412        </tr>
413      </table>
414    </div>
415  </body>
416</html>
417