1<!--$Id: ex_rq.so,v 1.7 2006/08/24 18:01:51 bostic Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB Reference Guide: Ex_rep_base: putting it all together</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Replication</dl></b></td>
13<td align=right><a href="/rep/ex_comm.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/xa/intro.html"><img src="/images/next.gif" alt="Next"></a>
14</td></tr></table>
15<p align=center><b>Ex_rep_base: putting it all together</b></p>
16<p>Beyond simply initializing a replicated environment, an application
17that uses the Base replication API must set up its communication
18infrastructure, and then make sure that incoming messages are received
19and processed.</p>
20<p>To initialize replication, ex_rep_base creates a Berkeley DB environment and
21calls <a href="/api_c/rep_transport.html">DB_ENV-&gt;rep_set_transport</a> to establish a send function.  (See
22the main function in <b>ex_rep/base/rep_base.c</b>, including its
23calls to the create_env and env_init functions in
24<b>ex_rep/common/rep_common.c</b>.)</p>
25<p>ex_rep_base opens a listening socket for incoming connections and opens
26an outgoing connection to every machine that it knows about (that is,
27all the sites listed in the <b>-o</b> command line argument).
28Applications can structure the details of this in different ways, but
29ex_rep_base creates a user-level thread to listen on its socket, plus
30a thread to loop and handle messages on each socket, in addition to the
31threads needed to manage the user interface, update the database on the
32master, and read from the database on the client (in other words, in
33addition to the normal functionality of any database application).</p>
34<p>Once the initial threads have all been started and the communications
35infrastructure is initialized, the application signals that it is ready
36for replication and joins a replication group by calling
37<a href="/api_c/rep_start.html">DB_ENV-&gt;rep_start</a>.  (Again, see the main function in
38<b>ex_rep/base/rep_base.c</b>.)</p>
39<p>Note the use of the optional second argument to <a href="/api_c/rep_start.html">DB_ENV-&gt;rep_start</a> in
40the client initialization code.  The argument "local" is a piece of
41data, opaque to Berkeley DB, that will be broadcast to each member of a
42replication group; it allows new clients to join a replication group,
43without knowing the location of all its members;  the new client will
44be contacted by the members it does not know about, who will receive
45the new client's contact information that was specified in "myaddr."
46See <a href="/ref/rep/newsite.html">Connecting to a new site</a> for more
47information.</p>
48<p>The final piece of a replicated application is the code that loops,
49receives, and processes messages from a given remote environment.
50ex_rep_base runs one of these loops in a parallel thread for each
51socket connection (see the hm_loop function in
52<b>ex_rep/base/rep_msg.c</b>).  Other applications may want to queue
53messages somehow and process them asynchronously, or select() on a
54number of sockets and either look up the correct environment ID for each
55or encapsulate the ID in the communications protocol.</p>
56<table width="100%"><tr><td><br></td><td align=right><a href="/rep/ex_comm.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/xa/intro.html"><img src="/images/next.gif" alt="Next"></a>
57</td></tr></table>
58<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
59</body>
60</html>
61