| 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>Chapter 5. Managing DB Files</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="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> |
| 11 |
<link rel="prev" href="reversesplit.html" title="Reverse BTree Splits" /> |
| 12 |
<link rel="next" href="backuprestore.html" title="Backup Procedures" /> |
| 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">Chapter 5. Managing DB Files</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="reversesplit.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center"> </th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="backuprestore.html">Next</a></td> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="chapter" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title"><a id="filemanagement"></a>Chapter 5. Managing DB Files</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<p> |
| 41 |
<b>Table of Contents</b> |
| 42 |
</p> |
| 43 |
<dl> |
| 44 |
<dt> |
| 45 |
<span class="sect1"> |
| 46 |
<a href="filemanagement.html#checkpoints">Checkpoints</a> |
| 47 |
</span> |
| 48 |
</dt> |
| 49 |
<dt> |
| 50 |
<span class="sect1"> |
| 51 |
<a href="backuprestore.html">Backup Procedures</a> |
| 52 |
</span> |
| 53 |
</dt> |
| 54 |
<dd> |
| 55 |
<dl> |
| 56 |
<dt> |
| 57 |
<span class="sect2"> |
| 58 |
<a href="backuprestore.html#copyutilities">About Unix Copy Utilities</a> |
| 59 |
</span> |
| 60 |
</dt> |
| 61 |
<dt> |
| 62 |
<span class="sect2"> |
| 63 |
<a href="backuprestore.html#standardbackup">Offline Backups</a> |
| 64 |
</span> |
| 65 |
</dt> |
| 66 |
<dt> |
| 67 |
<span class="sect2"> |
| 68 |
<a href="backuprestore.html#hotbackup">Hot Backup</a> |
| 69 |
</span> |
| 70 |
</dt> |
| 71 |
<dt> |
| 72 |
<span class="sect2"> |
| 73 |
<a href="backuprestore.html#incrementalbackups">Incremental Backups</a> |
| 74 |
</span> |
| 75 |
</dt> |
| 76 |
</dl> |
| 77 |
</dd> |
| 78 |
<dt> |
| 79 |
<span class="sect1"> |
| 80 |
<a href="recovery.html">Recovery Procedures</a> |
| 81 |
</span> |
| 82 |
</dt> |
| 83 |
<dd> |
| 84 |
<dl> |
| 85 |
<dt> |
| 86 |
<span class="sect2"> |
| 87 |
<a href="recovery.html#normalrecovery">Normal Recovery</a> |
| 88 |
</span> |
| 89 |
</dt> |
| 90 |
<dt> |
| 91 |
<span class="sect2"> |
| 92 |
<a href="recovery.html#catastrophicrecovery">Catastrophic Recovery</a> |
| 93 |
</span> |
| 94 |
</dt> |
| 95 |
</dl> |
| 96 |
</dd> |
| 97 |
<dt> |
| 98 |
<span class="sect1"> |
| 99 |
<a href="architectrecovery.html">Designing Your Application for Recovery</a> |
| 100 |
</span> |
| 101 |
</dt> |
| 102 |
<dd> |
| 103 |
<dl> |
| 104 |
<dt> |
| 105 |
<span class="sect2"> |
| 106 |
<a href="architectrecovery.html#multithreadrecovery">Recovery for Multi-Threaded Applications</a> |
| 107 |
</span> |
| 108 |
</dt> |
| 109 |
<dt> |
| 110 |
<span class="sect2"> |
| 111 |
<a href="architectrecovery.html#multiprocessrecovery">Recovery in Multi-Process Applications</a> |
| 112 |
</span> |
| 113 |
</dt> |
| 114 |
</dl> |
| 115 |
</dd> |
| 116 |
<dt> |
| 117 |
<span class="sect1"> |
| 118 |
<a href="hotfailover.html">Using Hot Failovers</a> |
| 119 |
</span> |
| 120 |
</dt> |
| 121 |
<dt> |
| 122 |
<span class="sect1"> |
| 123 |
<a href="logfileremoval.html">Removing Log Files</a> |
| 124 |
</span> |
| 125 |
</dt> |
| 126 |
<dt> |
| 127 |
<span class="sect1"> |
| 128 |
<a href="logconfig.html">Configuring the Logging Subsystem</a> |
| 129 |
</span> |
| 130 |
</dt> |
| 131 |
<dd> |
| 132 |
<dl> |
| 133 |
<dt> |
| 134 |
<span class="sect2"> |
| 135 |
<a href="logconfig.html#logfilesize">Setting the Log File Size</a> |
| 136 |
</span> |
| 137 |
</dt> |
| 138 |
<dt> |
| 139 |
<span class="sect2"> |
| 140 |
<a href="logconfig.html#logregionsize">Configuring the Logging Region Size</a> |
| 141 |
</span> |
| 142 |
</dt> |
| 143 |
<dt> |
| 144 |
<span class="sect2"> |
| 145 |
<a href="logconfig.html#inmemorylogging">Configuring In-Memory Logging</a> |
| 146 |
</span> |
| 147 |
</dt> |
| 148 |
<dt> |
| 149 |
<span class="sect2"> |
| 150 |
<a href="logconfig.html#logbuffer">Setting the In-Memory Log Buffer Size</a> |
| 151 |
</span> |
| 152 |
</dt> |
| 153 |
</dl> |
| 154 |
</dd> |
| 155 |
</dl> |
| 156 |
</div> |
| 157 |
<p> |
| 158 |
DB is capable of storing several types of files on disk: |
| 159 |
</p> |
| 160 |
<div class="itemizedlist"> |
| 161 |
<ul type="disc"> |
| 162 |
<li> |
| 163 |
<p> |
| 164 |
Data files, which contain the actual data in your database. |
| 165 |
</p> |
| 166 |
</li> |
| 167 |
<li> |
| 168 |
<p> |
| 169 |
Log files, which contain information required to recover your |
| 170 |
database in the event of a system or application failure. |
| 171 |
</p> |
| 172 |
</li> |
| 173 |
<li> |
| 174 |
<p> |
| 175 |
Region files, which contain information necessary for the |
| 176 |
overall operation of your application. |
| 177 |
</p> |
| 178 |
</li> |
| 179 |
<li> |
| 180 |
<p> |
| 181 |
Temporary files, which are created only under certain special circumstances. These files never need to |
| 182 |
be backed up or otherwise managed and so they are not a consideration for the topics described in this |
| 183 |
chapter. See <a class="xref" href="enabletxn.html#security" title="Security Considerations">Security Considerations</a> |
| 184 |
for more information on temporary files. |
| 185 |
</p> |
| 186 |
</li> |
| 187 |
</ul> |
| 188 |
</div> |
| 189 |
<p> |
| 190 |
Of these, you must manage your data and log files by ensuring that they |
| 191 |
are backed up. You should also pay attention to the amount of disk space |
| 192 |
your log files are consuming, and periodically remove any unneeded |
| 193 |
files. Finally, you can optionally tune your logging subsystem to best |
| 194 |
suit your application's needs and requirements. |
| 195 |
These topics are discussed in this chapter. |
| 196 |
</p> |
| 197 |
<div class="sect1" lang="en" xml:lang="en"> |
| 198 |
<div class="titlepage"> |
| 199 |
<div> |
| 200 |
<div> |
| 201 |
<h2 class="title" style="clear: both"><a id="checkpoints"></a>Checkpoints</h2> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
</div> |
| 205 |
<p> |
| 206 |
Before we can discuss DB file management, we need to |
| 207 |
describe checkpoints. When databases are modified (that is, a |
| 208 |
transaction is committed), the modifications are recorded in |
| 209 |
DB's logs, but they are <span class="emphasis"><em>not</em></span> |
| 210 |
necessarily reflected in the actual database files on disk. |
| 211 |
</p> |
| 212 |
<p> |
| 213 |
This means that as time goes on, increasingly |
| 214 |
more data is contained in your log files that is not |
| 215 |
contained in your data files. As a result, you must keep more |
| 216 |
log files around than you might actually need. Also, any |
| 217 |
recovery run from your log files will take increasingly longer |
| 218 |
amounts of time, because there is more data in the log files |
| 219 |
that must be reflected back into the data files during the |
| 220 |
recovery process. |
| 221 |
</p> |
| 222 |
<p> |
| 223 |
You can reduce these problems by periodically |
| 224 |
running a checkpoint against your environment. The checkpoint: |
| 225 |
</p> |
| 226 |
<div class="itemizedlist"> |
| 227 |
<ul type="disc"> |
| 228 |
<li> |
| 229 |
<p> |
| 230 |
Flushes dirty pages from the in-memory cache. This means that data modifications found in your |
| 231 |
in-memory cache are written to the database files on disk. Note that a checkpoint also causes data |
| 232 |
dirtied by an uncommitted transaction to also be written to your database files on disk. In this latter |
| 233 |
case, DB's normal recovery is used to remove any such modifications that were subsequently |
| 234 |
abandoned by your application using a transaction abort. |
| 235 |
</p> |
| 236 |
<p> |
| 237 |
Normal recovery is describe in <a class="xref" href="recovery.html" title="Recovery Procedures">Recovery Procedures</a>. |
| 238 |
</p> |
| 239 |
</li> |
| 240 |
<li> |
| 241 |
<p> |
| 242 |
Writes a checkpoint record. |
| 243 |
</p> |
| 244 |
</li> |
| 245 |
<li> |
| 246 |
<p> |
| 247 |
Flushes the log. This causes all log data that has not yet been written to disk to be written. |
| 248 |
</p> |
| 249 |
</li> |
| 250 |
<li> |
| 251 |
<p> |
| 252 |
Writes a list of open databases. |
| 253 |
</p> |
| 254 |
</li> |
| 255 |
</ul> |
| 256 |
</div> |
| 257 |
<p> |
| 258 |
There are several ways to run a checkpoint. One way is to use |
| 259 |
the <span class="command"><strong>db_checkpoint</strong></span> command line utility. (Note, however, that this command line utility |
| 260 |
cannot be used if your environment was opened using |
| 261 |
|
| 262 |
<span> |
| 263 |
<code class="methodname">EnvironmentConfig.setPrivate()</code>.) |
| 264 |
</span> |
| 265 |
</p> |
| 266 |
<p> |
| 267 |
You can also run a thread that periodically checkpoints your |
| 268 |
environment for you by calling the |
| 269 |
|
| 270 |
|
| 271 |
<code class="methodname">Environment.checkpoint()</code> |
| 272 |
method. |
| 273 |
</p> |
| 274 |
<p> |
| 275 |
Note that you can prevent a checkpoint from occurring unless more |
| 276 |
than a specified amount of log data has been written since the |
| 277 |
last checkpoint. You can also prevent the checkpoint from |
| 278 |
running unless more than a specified amount of time has |
| 279 |
occurred since the last checkpoint. These conditions are |
| 280 |
particularly interesting if you have multiple threads |
| 281 |
<span>or processes</span> |
| 282 |
running checkpoints. |
| 283 |
</p> |
| 284 |
<p> |
| 285 |
For configuration information, see the |
| 286 |
|
| 287 |
|
| 288 |
<a class="ulink" href="http://download.oracle.com/docs/cd/E17076_02/html/java/com/sleepycat/db/CheckpointConfig.html" target="_top"> |
| 289 |
CheckpointConfig Javadoc page. |
| 290 |
</a> |
| 291 |
</p> |
| 292 |
<p> |
| 293 |
Note that running checkpoints can be quite expensive. DB must |
| 294 |
flush every dirty page to the backing database files. On the |
| 295 |
other hand, if you do not run checkpoints often enough, your |
| 296 |
recovery time can be unnecessarily long and you may be using more |
| 297 |
disk space than you really need. Also, you cannot remove log files |
| 298 |
until a checkpoint is run. Therefore, deciding how frequently |
| 299 |
to run a checkpoint is one of the most |
| 300 |
common tuning activity for DB applications. |
| 301 |
</p> |
| 302 |
<p> |
| 303 |
For example, the following class performs a checkpoint every 60 seconds, so long as 500 kb of logging data has been |
| 304 |
written since the last checkpoint: |
| 305 |
</p> |
| 306 |
<pre class="programlisting"><span>package db.txn;</span> |
| 307 |
|
| 308 |
import com.sleepycat.db.CheckpointConfig; |
| 309 |
import com.sleepycat.db.DatabaseException; |
| 310 |
import com.sleepycat.db.Environment; |
| 311 |
|
| 312 |
public class CheckPointer extends Thread |
| 313 |
{ |
| 314 |
private CheckpointConfig cpc = new CheckpointConfig(); |
| 315 |
private Environment myEnv = null; |
| 316 |
private static boolean canRun = true; |
| 317 |
|
| 318 |
|
| 319 |
// Constructor. |
| 320 |
CheckPointer(Environment env) { |
| 321 |
myEnv = env; |
| 322 |
// Run a checkpoint only if 500 kbytes of log data has been |
| 323 |
// written. |
| 324 |
cpc.setKBytes(500); |
| 325 |
} |
| 326 |
|
| 327 |
// Thread method that performs a checkpoint every |
| 328 |
// 60 seconds |
| 329 |
public void run () { |
| 330 |
while (canRun) { |
| 331 |
try { |
| 332 |
myEnv.checkpoint(cpc); |
| 333 |
sleep(60000); |
| 334 |
} catch (DatabaseException de) { |
| 335 |
System.err.println("Checkpoint error: " + |
| 336 |
de.toString()); |
| 337 |
} catch (InterruptedException e) { |
| 338 |
// Should never get here |
| 339 |
System.err.println("got interrupted exception"); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
public static void stopRunning() { |
| 345 |
canRun = false; |
| 346 |
} |
| 347 |
} </pre> |
| 348 |
<p> |
| 349 |
And you use this class as follows. Note that we add the call to shutdown the checkpoint thread in our application's |
| 350 |
shutdown code: |
| 351 |
</p> |
| 352 |
<pre class="programlisting">package db.txn; |
| 353 |
|
| 354 |
import com.sleepycat.db.DatabaseException; |
| 355 |
import com.sleepycat.db.Environment; |
| 356 |
import com.sleepycat.db.EnvironmentConfig; |
| 357 |
|
| 358 |
import java.io.File; |
| 359 |
import java.io.FileNotFoundException; |
| 360 |
|
| 361 |
|
| 362 |
public class TryCheckPoint { |
| 363 |
|
| 364 |
private static String myEnvPath = "./"; |
| 365 |
|
| 366 |
private static Environment myEnv = null; |
| 367 |
|
| 368 |
|
| 369 |
private static void usage() { |
| 370 |
System.out.println("TxnGuide [-h <env directory>]"); |
| 371 |
System.exit(-1); |
| 372 |
} |
| 373 |
|
| 374 |
public static void main(String args[]) { |
| 375 |
try { |
| 376 |
// Parse the arguments list |
| 377 |
parseArgs(args); |
| 378 |
// Open the environment and databases |
| 379 |
openEnv(); |
| 380 |
|
| 381 |
// Start the checkpoint thread |
| 382 |
CheckPointer cp = new CheckPointer(myEnv); |
| 383 |
cp.start(); |
| 384 |
|
| 385 |
////////////////////////////////// |
| 386 |
// Do database work here as normal |
| 387 |
////////////////////////////////// |
| 388 |
|
| 389 |
// Once all database work is completed, stop the checkpoint |
| 390 |
// thread. |
| 391 |
CheckPointer.stopRunning(); |
| 392 |
|
| 393 |
// Join the checkpoint thread in case it needs some time to |
| 394 |
// cleanly shutdown. |
| 395 |
cp.join(); |
| 396 |
|
| 397 |
} catch (Exception e) { |
| 398 |
System.err.println("TryCheckPoint: " + e.toString()); |
| 399 |
e.printStackTrace(); |
| 400 |
} finally { |
| 401 |
closeEnv(); |
| 402 |
} |
| 403 |
System.out.println("All done."); |
| 404 |
} |
| 405 |
|
| 406 |
// Open an environment and databases |
| 407 |
private static void openEnv() throws DatabaseException { |
| 408 |
System.out.println("opening env"); |
| 409 |
|
| 410 |
// Set up the environment. |
| 411 |
EnvironmentConfig myEnvConfig = new EnvironmentConfig(); |
| 412 |
myEnvConfig.setAllowCreate(true); |
| 413 |
myEnvConfig.setInitializeCache(true); |
| 414 |
myEnvConfig.setInitializeLocking(true); |
| 415 |
myEnvConfig.setInitializeLogging(true); |
| 416 |
myEnvConfig.setTransactional(true); |
| 417 |
// EnvironmentConfig.setThreaded(true) is the default behavior |
| 418 |
// in Java, so we do not have to do anything to cause the |
| 419 |
// environment handle to be free-threaded. |
| 420 |
|
| 421 |
try { |
| 422 |
// Open the environment |
| 423 |
myEnv = new Environment(new File(myEnvPath), // Env home |
| 424 |
myEnvConfig); |
| 425 |
|
| 426 |
// Skipping the database opens and closes for brevity |
| 427 |
|
| 428 |
} catch (FileNotFoundException fnfe) { |
| 429 |
System.err.println("openEnv: " + fnfe.toString()); |
| 430 |
System.exit(-1); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
// Close the environment and databases |
| 435 |
private static void closeEnv() { |
| 436 |
System.out.println("Closing env"); |
| 437 |
if (myEnv != null ) { |
| 438 |
try { |
| 439 |
myEnv.close(); |
| 440 |
} catch (DatabaseException e) { |
| 441 |
System.err.println("closeEnv: " + e.toString()); |
| 442 |
e.printStackTrace(); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
private TryCheckPoint() {} |
| 448 |
|
| 449 |
private static void parseArgs(String args[]) { |
| 450 |
for(int i = 0; i < args.length; ++i) { |
| 451 |
if (args[i].startsWith("-")) { |
| 452 |
switch(args[i].charAt(1)) { |
| 453 |
case 'h': |
| 454 |
myEnvPath = new String(args[++i]); |
| 455 |
break; |
| 456 |
default: |
| 457 |
usage(); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} </pre> |
| 463 |
</div> |
| 464 |
</div> |
| 465 |
<div class="navfooter"> |
| 466 |
<hr /> |
| 467 |
<table width="100%" summary="Navigation footer"> |
| 468 |
<tr> |
| 469 |
<td width="40%" align="left"><a accesskey="p" href="reversesplit.html">Prev</a> </td> |
| 470 |
<td width="20%" align="center"> </td> |
| 471 |
<td width="40%" align="right"> <a accesskey="n" href="backuprestore.html">Next</a></td> |
| 472 |
</tr> |
| 473 |
<tr> |
| 474 |
<td width="40%" align="left" valign="top">Reverse BTree Splits </td> |
| 475 |
<td width="20%" align="center"> |
| 476 |
<a accesskey="h" href="index.html">Home</a> |
| 477 |
</td> |
| 478 |
<td width="40%" align="right" valign="top"> Backup Procedures</td> |
| 479 |
</tr> |
| 480 |
</table> |
| 481 |
</div> |
| 482 |
</body> |
| 483 |
</html> |