| 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>Ex_rep_base: a TCP/IP based communication infrastructure</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="rep.html" title="Chapter 12. Berkeley DB Replication" /> |
| 11 | <link rel="prev" href="rep_ex.html" title="Ex_rep: a replication example" /> |
| 12 | <link rel="next" href="rep_ex_rq.html" title="Ex_rep_base: putting it all together" /> |
| 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">Ex_rep_base: a TCP/IP based communication infrastructure</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_ex.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 12. |
| 26 | Berkeley DB Replication |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="rep_ex_rq.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="rep_ex_comm"></a>Ex_rep_base: a TCP/IP based communication infrastructure</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p> |
| 42 | Base API applications must implement a |
| 43 | communication infrastructure. The communication infrastructure |
| 44 | consists of three parts: a way to map environment IDs to particular |
| 45 | sites, the functions to send and receive messages, and the application |
| 46 | architecture that supports the particular communication infrastructure |
| 47 | used (for example, individual threads per communicating site, a shared |
| 48 | message handler for all sites, a hybrid solution). The communication |
| 49 | infrastructure for ex_rep_base is implemented in the file |
| 50 | <code class="filename">ex_rep/base/rep_net.c</code>, and each part of that infrastructure |
| 51 | is described as follows.</p> |
| 52 | <p>Ex_rep_base maintains a table of environment ID to TCP/IP port |
| 53 | mappings. A pointer to this table is stored in a structure pointed to |
| 54 | by the app_private field of the <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> object so it can be |
| 55 | accessed by any function that has the database environment handle. |
| 56 | The table is represented by a machtab_t structure which contains a |
| 57 | reference to a linked list of member_t's, both of which are defined in |
| 58 | <code class="filename">ex_rep/base/rep_net.c</code>. Each member_t contains the host and |
| 59 | port identification, the environment ID, and a file descriptor.</p> |
| 60 | <p>This design is particular to this application and communication |
| 61 | infrastructure, but provides an indication of the sort of functionality |
| 62 | that is needed to maintain the application-specific state for a |
| 63 | TCP/IP-based infrastructure. The goal of the table and its interfaces |
| 64 | is threefold: First, it must guarantee that given an environment ID, |
| 65 | the send function can send a message to the appropriate place. Second, |
| 66 | when given the special environment ID <a href="../api_reference/C/reptransport.html#transport_DB_EID_BROADCAST" class="olink">DB_EID_BROADCAST</a>, the send |
| 67 | function can send messages to all the machines in the group. Third, |
| 68 | upon receipt of an incoming message, the receive function can correctly |
| 69 | identify the sender and pass the appropriate environment ID to the |
| 70 | <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method.</p> |
| 71 | <p>Mapping a particular environment ID to a specific port is accomplished |
| 72 | by looping through the linked list until the desired environment ID is |
| 73 | found. Broadcast communication is implemented by looping through the |
| 74 | linked list and sending to each member found. Since each port |
| 75 | communicates with only a single other environment, receipt of a message |
| 76 | on a particular port precisely identifies the sender.</p> |
| 77 | <p>This is implemented in the quote_send, quote_send_broadcast and |
| 78 | quote_send_one functions, which can be found in |
| 79 | <code class="filename">ex_rep/base/rep_net.c</code>.</p> |
| 80 | <p>The example provided is merely one way to satisfy these requirements, |
| 81 | and there are alternative implementations as well. For instance, |
| 82 | instead of associating separate socket connections with each remote |
| 83 | environment, an application might instead label each message with a |
| 84 | sender identifier; instead of looping through a table and sending a |
| 85 | copy of a message to each member of the replication group, the |
| 86 | application could send a single message using a broadcast protocol.</p> |
| 87 | <p>The quote_send function is passed as the callback to |
| 88 | <a href="../api_reference/C/reptransport.html" class="olink">DB_ENV->rep_set_transport()</a>; Berkeley DB automatically sends messages as needed |
| 89 | for replication. The receive function is a mirror to the quote_send_one |
| 90 | function. It is not a callback function (the application is responsible |
| 91 | for collecting messages and calling <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> on them as is |
| 92 | convenient). In the sample application, all messages transmitted are |
| 93 | Berkeley DB messages that get handled by <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a>, however, this |
| 94 | is not always going to be the case. The application may want to pass |
| 95 | its own messages across the same channels, distinguish between its own |
| 96 | messages and those of Berkeley DB, and then pass only the Berkeley DB ones to |
| 97 | <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a>.</p> |
| 98 | <p>The final component of the communication infrastructure is the process |
| 99 | model used to communicate with all the sites in the replication group. |
| 100 | Each site creates a thread of control that listens on its designated |
| 101 | socket (as specified by the <span class="bold"><strong>-l</strong></span> command line argument) and |
| 102 | then creates a new channel for each site that contacts it. In addition, |
| 103 | each site explicitly connects to the sites specified in the |
| 104 | <span class="bold"><strong>-r</strong></span> and <span class="bold"><strong>-R</strong></span> |
| 105 | command line arguments. This is a fairly standard TCP/IP |
| 106 | process architecture and is implemented by the connect_thread, |
| 107 | connect_all and connect_site functions |
| 108 | in <code class="filename">ex_rep/base/rep_msg.c</code> and supporting functions |
| 109 | in <code class="filename">ex_rep/base/rep_net.c</code>.</p> |
| 110 | </div> |
| 111 | <div class="navfooter"> |
| 112 | <hr /> |
| 113 | <table width="100%" summary="Navigation footer"> |
| 114 | <tr> |
| 115 | <td width="40%" align="left"><a accesskey="p" href="rep_ex.html">Prev</a> </td> |
| 116 | <td width="20%" align="center"> |
| 117 | <a accesskey="u" href="rep.html">Up</a> |
| 118 | </td> |
| 119 | <td width="40%" align="right"> <a accesskey="n" href="rep_ex_rq.html">Next</a></td> |
| 120 | </tr> |
| 121 | <tr> |
| 122 | <td width="40%" align="left" valign="top">Ex_rep: a replication example </td> |
| 123 | <td width="20%" align="center"> |
| 124 | <a accesskey="h" href="index.html">Home</a> |
| 125 | </td> |
| 126 | <td width="40%" align="right" valign="top"> Ex_rep_base: putting it all together</td> |
| 127 | </tr> |
| 128 | </table> |
| 129 | </div> |
| 130 | </body> |
| 131 | </html> |