config.html   [plain text]


<!--$Id: config.so,v 10.5 2007/12/13 03:05:54 margo Exp $-->
<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Application configuration</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<table width="100%"><tr valign=top>
<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Application Specific Logging and Recovery</dl></b></td>
<td align=right><a href="../apprec/auto.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../program/appsignals.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p align=center><b>Application configuration</b></p>
<p>The application should include a dispatch function that dispatches to
appropriate printing and/or recovery functions based on the log record
type and the operation code.  The dispatch function should take the same
arguments as the recovery function, and should call the appropriate
recovery and/or printing functions based on the log record type and the
operation code.  For example, the ex_apprec dispatch function is as
follows:</p>
<blockquote><pre>int
apprec_dispatch(dbenv, dbt, lsn, op)
	DB_ENV *dbenv;
	DBT *dbt;
	DB_LSN *lsn;
	db_recops op;
{
	u_int32_t rectype;
	/* Pull the record type out of the log record. */
	memcpy(&rectype, dbt-&gt;data, sizeof(rectype));
	switch (rectype) {
	case DB_ex_apprec_mkdir:
		return (ex_apprec_mkdir_recover(dbenv, dbt, lsn, op));
	default:
		/*
		 * We've hit an unexpected, allegedly user-defined record
		 * type.
		 */
		dbenv-&gt;errx(dbenv, "Unexpected log record type encountered");
		return (EINVAL);
	}
}
</pre></blockquote>
<p>Applications use this dispatch function and the automatically generated
functions as follows:</p>
<ol>
<p><li>When the application starts, call the <a href="../../api_c/env_set_app_dispatch.html">DB_ENV-&gt;set_app_dispatch</a>
with your dispatch function.
<p><li>Issue a <a href="../../api_c/txn_begin.html">DB_ENV-&gt;txn_begin</a> call before any operations you want to be
transaction-protected.
<p><li>Before accessing any data, issue the appropriate lock call to lock the
data (either for reading or writing).
<p><li>Before modifying any data that is transaction-protected, issue a call
to the appropriate log function.
<p><li>Call <a href="../../api_c/txn_commit.html">DB_TXN-&gt;commit</a> to save all the changes, or call <a href="../../api_c/txn_abort.html">DB_TXN-&gt;abort</a>
to cancel all of the modifications.
</ol>
<p>The recovery functions are called in the three following cases:</p>
<ol>
<p><li>During recovery after application or system failure, with op set to
<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_FORWARD_ROLL">DB_TXN_FORWARD_ROLL</a> or <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_BACKWARD_ROLL">DB_TXN_BACKWARD_ROLL</a>.
<p><li>During transaction abort, with op set to <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_ABORT">DB_TXN_ABORT</a>.
<p><li>On a replicated client to apply updates from the master, with op set to
<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_APPLY">DB_TXN_APPLY</a>.
</ol>
<p>For each log record type you declare, you must write the appropriate
function to undo and redo the modifications.  The shell of these
functions will be generated for you automatically, but you must fill in
the details.</p>
<p>Your code must be able to detect whether the described modifications
have been applied to the data.  The function will be called with the
"op" parameter set to <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_ABORT">DB_TXN_ABORT</a> when a transaction that wrote
the log record aborts, with <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_FORWARD_ROLL">DB_TXN_FORWARD_ROLL</a> and
<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_BACKWARD_ROLL">DB_TXN_BACKWARD_ROLL</a> during recovery, and with <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_APPLY">DB_TXN_APPLY</a>
on a replicated client.</p>
<p>The actions for <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_ABORT">DB_TXN_ABORT</a> and <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_BACKWARD_ROLL">DB_TXN_BACKWARD_ROLL</a>
should generally be the same, and the actions for
<a href="../../api_c/env_set_app_dispatch.html#DB_TXN_FORWARD_ROLL">DB_TXN_FORWARD_ROLL</a> and <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_APPLY">DB_TXN_APPLY</a> should generally
be the same.  However, if the application is using Berkeley DB replication
and another thread of control may be performing read operations while
log records are applied on a replication client, the recovery function
should perform appropriate locking during <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_APPLY">DB_TXN_APPLY</a>
operations.  In this case, the recovery function may encounter deadlocks
when issuing locking calls.  The application should run with the
deadlock detector, and the recovery function should simply return
<a href="../../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> if a deadlock is detected and a locking
operation fails with that error.</p>
<p>The <a href="../../api_c/env_set_app_dispatch.html#DB_TXN_PRINT">DB_TXN_PRINT</a> operation should print the log record,
typically using the auto-generated print function; it is not used in
the Berkeley DB library, but may be useful for debugging, as in the
<a href="../../utility/db_printlog.html">db_printlog</a> utility.  Applications may safely ignore this
operation code, they may handle printing from the recovery function, or
they may dispatch directly to the auto-generated print function.</p>
<p>One common way to determine whether operations need to be undone or
redone is the use of log sequence numbers (LSNs).  For example, each
access method database page contains the LSN of the most recent log
record that describes a modification to the page.  When the access
method changes a page, it writes a log record describing the change and
including the LSN that was on the page before the change.  This LSN is
referred to as the previous LSN.  The recovery functions read the page
described by a log record, and compare the LSN on the page to the LSN
they were passed.</p>
<p>If the page LSN is less than the passed LSN and the operation is an
undo, no action is necessary (because the modifications have not been
written to the page).  If the page LSN is the same as the previous LSN
and the operation is a redo, the actions described are reapplied to the
page.  If the page LSN is equal to the passed LSN and the operation is
an undo, the actions are removed from the page; if the page LSN is
greater than the passed LSN and the operation is a redo, no further
action is necessary.  If the action is a redo and the LSN on the page
is less than the previous LSN in the log record, it is an error because
it could happen only if some previous log record was not processed.</p>
<p>Examples of other recovery functions can be found in the Berkeley DB library
recovery functions (found in files named XXX_rec.c) and in the
application-specific recovery example (specifically, ex_apprec_rec.c).</p>
<p>Finally, applications need to ensure that any data modifications they
have made, that were part of a committed transaction, must be written
to stable storage before calling the <a href="../../api_c/txn_checkpoint.html">DB_ENV-&gt;txn_checkpoint</a> method.  This is
to allow the periodic removal of database environment log files.</p>
<table width="100%"><tr><td><br></td><td align=right><a href="../apprec/auto.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../program/appsignals.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
</body>
</html>