• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/db-4.8.30/docs/gsg_db_rep/JAVA/
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��3.��The DB Replication Manager</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 Replicated Berkeley DB Applications" />
10    <link rel="up" href="index.html" title="Getting Started with Replicated Berkeley DB Applications" />
11    <link rel="prev" href="simpleprogramlisting.html" title="Program Listing" />
12    <link rel="next" href="repmgr_init_example_c.html" title="Adding the Replication Manager to SimpleTxn" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��3.��The DB Replication Manager</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="simpleprogramlisting.html">Prev</a>��</td>
22          <th width="60%" align="center">��</th>
23          <td width="20%" align="right">��<a accesskey="n" href="repmgr_init_example_c.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="repapp"></a>Chapter��3.��The DB Replication Manager</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="repapp.html#rep_init_code">
44                Starting and Stopping Replication
45            </a>
46            </span>
47          </dt>
48          <dd>
49            <dl>
50              <dt>
51                <span class="sect2">
52                  <a href="repapp.html#election_flags">Managing Election Policies</a>
53                </span>
54              </dt>
55              <dt>
56                <span class="sect2">
57                  <a href="repapp.html#thread_count">Selecting the Number of Threads</a>
58                </span>
59              </dt>
60            </dl>
61          </dd>
62          <dt>
63            <span class="sect1">
64              <a href="repmgr_init_example_c.html">Adding the Replication Manager to
65                    
66                    
67                    <span>SimpleTxn</span>
68            </a>
69            </span>
70          </dt>
71          <dt>
72            <span class="sect1">
73              <a href="fwrkpermmessage.html">Permanent Message Handling</a>
74            </span>
75          </dt>
76          <dd>
77            <dl>
78              <dt>
79                <span class="sect2">
80                  <a href="fwrkpermmessage.html#fmwrkpermpolicy">Identifying Permanent Message Policies</a>
81                </span>
82              </dt>
83              <dt>
84                <span class="sect2">
85                  <a href="fwrkpermmessage.html#fmwrkpermtimeout">Setting the Permanent Message Timeout</a>
86                </span>
87              </dt>
88              <dt>
89                <span class="sect2">
90                  <a href="fwrkpermmessage.html#perm2fmwrkexample">Adding a Permanent Message Policy to 
91                            
92                            
93                            <span>RepQuoteExampleGSG</span>
94                    </a>
95                </span>
96              </dt>
97            </dl>
98          </dd>
99          <dt>
100            <span class="sect1">
101              <a href="electiontimes.html">Managing Election Times</a>
102            </span>
103          </dt>
104          <dd>
105            <dl>
106              <dt>
107                <span class="sect2">
108                  <a href="electiontimes.html#electiontimeout">Managing Election Timeouts</a>
109                </span>
110              </dt>
111              <dt>
112                <span class="sect2">
113                  <a href="electiontimes.html#electretrytime">Managing Election Retry Times</a>
114                </span>
115              </dt>
116            </dl>
117          </dd>
118          <dt>
119            <span class="sect1">
120              <a href="fmwrkconnectretry.html">Managing Connection Retries</a>
121            </span>
122          </dt>
123          <dt>
124            <span class="sect1">
125              <a href="heartbeats.html">Managing Heartbeats</a>
126            </span>
127          </dt>
128        </dl>
129      </div>
130      <p>
131        The easiest way to add replication to your transactional
132        application is to use the Replication Manager. The Replication Manager provides a comprehensive 
133        communications layer that enables replication. For a brief listing
134        of the Replication Manager's feature set, see 
135        <a class="xref" href="apioverview.html#repframeworkoverview" title="Replication Manager Overview">Replication Manager Overview</a>.
136    </p>
137      <p>
138            To use the Replication Manager, you make use of special methods off the
139            <code class="classname">Environment</code> and
140            <code class="methodname">EnvironmentConfig</code>
141            classes. You also use a series of related classes to perform
142            your implementation. For example, in order to detect whether
143            your code is running as a master or a replica, you must
144            implement <code class="classname">com.sleepycat.db.EventHandler</code>.
145            (see <a class="xref" href="fwrkmasterreplica.html#determinestate" title="Determining State">Determining State</a>).
146            That is:
147    </p>
148      <div class="orderedlist">
149        <ol type="1">
150          <li>
151            <p>
152                                    Create an environment handle as normal.
153                            </p>
154          </li>
155          <li>
156            <p>
157                                    Configure your environment handle as
158                                    needed (e.g. set the error file and
159                                    error prefix values, if desired).
160                            </p>
161          </li>
162          <li>
163            <p>
164                                    Use the Replication Manager replication methods to
165                                    configure the Replication Manager. Using these
166                                    methods causes DB to know that you
167                                    are using the Replication Manager.
168                            </p>
169            <p>
170                                    Configuring the Replication Manager entails setting the replication
171                                    environment's priority, setting the TCP/IP address
172                                    that this replication environment will use for
173                                    incoming replication messages, identifying
174                                    TCP/IP addresses of other replication
175                                    environments, setting the number of
176                                    replication environments in the
177                                    replication group, and so forth. These actions are
178                                    discussed throughout the remainder of
179                                    this chapter.
180                            </p>
181          </li>
182          <li>
183            <p>
184                                    Open your environment handle. When you
185                                    do this, be sure to specify 
186
187                                    
188                                    
189                                    <span>
190                                    <code class="methodname">EnvironmentConfig.setInitializeReplication()</code>
191                                    when you configure your environment
192                                    handle. This is in addition to the
193                                    configuration that you would normally
194                                    use for a transactional application.
195                                    This causes replication to be
196                                    initialized for the application.
197                                    </span>
198                            </p>
199          </li>
200          <li>
201            <p>
202                                    Start replication by calling
203                                    
204                                    
205                                    <span><code class="methodname">Environment.replicationManagerStart()</code>.</span>
206                            </p>
207          </li>
208          <li>
209            <p>
210                                    Open your databases as needed. Masters
211                                    must open their databases for read
212                                    and write activity. Replicas can open
213                                    their databases for read-only activity, but
214                                    doing so means they must re-open the
215                                    databases if the replica ever becomes a
216                                    master.  Either way, replicas should never attempt to
217                                    write to the database(s) directly.
218                            </p>
219          </li>
220        </ol>
221      </div>
222      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
223        <h3 class="title">Note</h3>
224        <p>
225                            The Replication Manager allows you to only use one
226                            environment handle per process.
227                    </p>
228      </div>
229      <p>
230                    When you are ready to shut down your application: 
231            </p>
232      <div class="orderedlist">
233        <ol type="1">
234          <li>
235            <p>
236                                    Close your databases
237                            </p>
238          </li>
239          <li>
240            <p>
241                                    Close your environment. This causes
242                                    replication to stop as well.
243                            </p>
244          </li>
245        </ol>
246      </div>
247      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
248        <h3 class="title">Note</h3>
249        <p>
250                            Before you can use the Replication Manager, you may have to
251                            enable it in your DB library. This is
252                            <span class="emphasis"><em>not</em></span> a requirement for
253                            Microsoft Windows systems, or Unix systems that
254                            use pthread mutexes by default. Other systems,
255                            notably BSD and BSD-derived systems (such as
256                            Mac OS X), must enable the Replication Manager when you
257                            configure the DB build.
258                    </p>
259        <p>
260                            You do this by <span class="emphasis"><em>not</em></span>
261                            disabling replication and by configuring the
262                            library with POSIX threads support. In other
263                            words, replication must be turned on in the
264                            build (it is by default), and POSIX thread
265                            support must be enabled if it is not already by
266                            default.  To do this, use the
267                            <code class="literal">--enable-pthread_api</code> switch
268                            on the configure script.
269                    </p>
270        <p>
271                        For example:
272                    </p>
273        <pre class="programlisting">../dist/configure --enable-pthread-api</pre>
274      </div>
275      <div class="sect1" lang="en" xml:lang="en">
276        <div class="titlepage">
277          <div>
278            <div>
279              <h2 class="title" style="clear: both"><a id="rep_init_code"></a>
280                Starting and Stopping Replication
281            </h2>
282            </div>
283          </div>
284        </div>
285        <div class="toc">
286          <dl>
287            <dt>
288              <span class="sect2">
289                <a href="repapp.html#election_flags">Managing Election Policies</a>
290              </span>
291            </dt>
292            <dt>
293              <span class="sect2">
294                <a href="repapp.html#thread_count">Selecting the Number of Threads</a>
295              </span>
296            </dt>
297          </dl>
298        </div>
299        <p>
300                    As described above, you introduce replication to an
301                    application by starting with a transactional
302                    application, performing some basic replication
303                    configuration, and then starting replication using 
304                    
305                    
306                    <span><code class="methodname">Environment.replicationManagerStart()</code>.</span>
307            </p>
308        <p>
309                    You stop replication by closing your environment
310                    cleanly in the same way you would for any DB application.
311            </p>
312        <p>
313                For example, the following code fragment initializes, then
314                stops and starts replication. Note that other replication
315                activities are omitted for brevity.
316            </p>
317        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
318          <h3 class="title">Note</h3>
319          <p>
320            Note that the following code fragment would be part of a larger
321            class that must implement
322            <code class="classname">com.sleepycat.db.EventHandler</code>. This
323            class is used to track state changes between master and
324            replica. We put off that implementation for the moment, but the
325            point remains that the following code fragment would be
326            contained in a method or two that you would include in your 
327            <code class="classname">com.sleepycat.db.EventHandler</code> 
328            implementation. 
329        </p>
330        </div>
331        <pre class="programlisting">
332import com.sleepycat.db.Environment;
333import com.sleepycat.db.EnvironmentConfig;
334import com.sleepycat.db.ReplicationHostAddress;
335import com.sleepycat.db.ReplicationManagerStartPolicy;
336
337...
338    String progname = "example_replication";
339    String envHome = "TESTDIR";
340    int cachesize = 10 * 1024 * 1024;
341
342    Environment dbenv;
343    ReplicationHostAddress thisHost;
344    String listenHost = "mymachine.sleepycat.com";
345    int listenPort = 8080;
346
347    ReplicationHostAddress otherReplica;
348    String otherHost = "anothermachine.sleepycat.com";
349    int otherPort = 8081;
350
351
352try {
353    // Configure the environment handle
354    EnvironmentConfig envConfig = new EnvironmentConfig();
355    envConfig.setErrorStream(System.err);
356    envConfig.setErrorPrefix(progname);
357    envConfig.setCacheSize(cachesize);
358    envConfig.setTxnNoSync(true);
359
360    // Identify the local replication site. This is the local hostname 
361    // and  port that this replication participant will use to receive
362    // incoming replication messages. Note that this can be
363    // performed only once for the application. It is required.
364    thisHost = new ReplicationHostAddress(listenHost, listenPort);
365    envConfig.setReplicationManagerLocalSite(thisHost);
366
367    // Set this application's priority. This is used for elections.
368    // 
369    // Set this number to a positive integer, or 0 if you do not want
370    // this site to be able to become a master.
371    envConfig.setReplicationPriority(100);
372    // Add a site to the list of replication environments known to 
373    // this application.
374    otherReplica = new ReplicationHostAddress(otherHost, otherPort);
375    envConfig.replicationManagerAddRemoteSite(otherReplica);
376
377    // Identify the number of sites in the replication group. This is
378    // necessary so that elections and permanent message
379    // handling can be performed correctly.
380    envConfig.setReplicationNumSites(2);
381
382    // Configure the environment's subsystems. Note that we initialize
383    // replication. This is required.
384    envConfig.setAllowCreate(true);
385    envConfig.setRunRecovery(true);
386    envConfig.setThreaded(true);
387    envConfig.setInitializeReplication(true);
388    envConfig.setInitializeLocking(true);
389    envConfig.setInitializeLogging(true);
390    envConfig.setInitializeCache(true);
391    envConfig.setTransactional(true);
392
393    // Missing from this is where we set the event handle and the
394    // acknowledgement policy. We discuss these things later in this 
395    // book. 
396
397    // Open our environment handle.
398    try {
399        dbenv = new Environment(envHome, envConfig);
400    } catch(FileNotFoundException e) {
401        System.err.println("FileNotFound exception: " + e.toString());
402        System.err.println(
403            "Ensure that the environment directory is pre-created.");
404    }
405
406    // Start the replication manager such that it has three threads.
407    dbenv.replicationManagerStart(3,
408        ReplicationManagerStartPolicy.REP_ELECTION);
409
410    //////////////////////////////////////////////////
411    // All other application code goes here, including
412    // database opens.
413    //////////////////////////////////////////////////
414
415} catch (DatabaseException dbe) {
416    // Error handling goes here
417}
418
419// Close out your application here.
420try {
421    // Make sure all your databases are closed.
422
423    // Closing your environment stops replication.
424   dbenv.close();
425} catch (DatabaseException dbe) {
426    // Error handling here.
427}
428
429// All done.  </pre>
430        <div class="sect2" lang="en" xml:lang="en">
431          <div class="titlepage">
432            <div>
433              <div>
434                <h3 class="title"><a id="election_flags"></a>Managing Election Policies</h3>
435              </div>
436            </div>
437          </div>
438          <p>
439                Before continuing, it is worth taking a look at the
440
441                
442
443                <span>
444                        startup election options that you can set for
445                        replication. You set these using the
446                        <code class="classname">ReplicationManagerStartPolicy</code> 
447                        class that you pass to the
448                        <code class="methodname">Environment.replicationManagerStart()</code>
449                        method.
450                        
451                </span>
452            </p>
453          <p>
454                    In the previous example, we specified 
455                        
456                        <code class="literal">ReplicationManagerStartPolicy.REP_ELECTION</code>
457                    when we started replication. This causes the
458                    application to try to find a master upon startup. If it
459                    cannot, it calls for an election. In the event an
460                    election is held, the environment receiving the most number of
461                    votes will become the master.
462            </p>
463          <p>
464                    There's some important points to make here:
465            </p>
466          <div class="itemizedlist">
467            <ul type="disc">
468              <li>
469                <p>
470                                    This 
471                                     
472                                    <span>option</span> 
473                                    only requires that other
474                                    environments in the replication group
475                                    participate in the vote. There is no
476                                    requirement that
477                                    <span class="emphasis"><em>all</em></span> such
478                                    environments participate. In other
479                                    words, if an environment
480                                    starts up, it can call for an
481                                    election, and select a master, even 
482                                    if all other environment have not yet
483                                    joined the  replication group.
484                            </p>
485              </li>
486              <li>
487                <p>
488                                    It only requires a simple majority of
489                                    participating environments to elect a master. The number of
490                                    environments used to calculate the simple
491                                    majority is based on the value set for
492
493                                    
494                                    
495                                    <span><code class="methodname">EnvironmentConfig.setReplicationNumSites()</code>.</span>
496                                    
497                                    
498                                    This is always true of elections held using the Replication Manager.
499                            </p>
500              </li>
501              <li>
502                <p>
503                                    As always, the environment participating in the election with the most
504                                    up-to-date log files is selected as
505                                    master. If an environment with more recent log files
506                                    has not yet joined the replication
507                                    group, it may not become the master.
508                            </p>
509              </li>
510            </ul>
511          </div>
512          <p>
513                    Any one of these points may be enough to cause a
514                    less-than-optimum environment to be selected as master.
515                    Therefore, to give you a better degree of control over
516                    which environment becomes a master at application startup,
517                    the Replication Manager offers the following start-up
518                                     
519                                    <span>options:</span> 
520            </p>
521          <div class="informaltable">
522            <table border="1" width="80%">
523              <colgroup>
524                <col />
525                <col />
526              </colgroup>
527              <thead>
528                <tr>
529                  <th>Option</th>
530                  <th>Description</th>
531                </tr>
532              </thead>
533              <tbody>
534                <tr>
535                  <td>
536                    <code class="literal">ReplicationManagerStartPolicy.REP_MASTER</code>
537                  </td>
538                  <td>
539                   <p>
540                   The application starts up and declares the environment to be a master
541                   without calling for an election. It is an error for more
542                   than one environment to start up using this flag, or for
543                   an environment
544                   to use this flag when a master already exists. 
545                   </p>
546                   <p>
547                           Note that no replication group should
548                           <span class="emphasis"><em>ever</em></span> operate with more than
549                           one master.
550                   </p>
551                   <p>
552                           In the event that a environment attempts to become a
553                           master when a master already exists, the
554                           replication code will resolve the problem by
555                           holding an election. Note, however, that there
556                           is always a possibility of data loss in the face
557                           of duplicate masters, because once a master is
558                           selected, the environment that loses the election will
559                           have to roll back any transactions committed
560                           until it is in sync with the "real" master.
561                   </p>
562
563                  </td>
564                </tr>
565                <tr>
566                  <td>
567                    <code class="literal">ReplicationManagerStartPolicy.REP_CLIENT</code>
568                  </td>
569                  <td>
570                          <p>
571                                  The application starts up and declares
572                                  the environment to be a replica without calling for
573                                  an election. Note that the environment
574                                  can still become a master if a subsequent
575                                  application starts up, calls for an
576                                  election, and this environment is elected
577                                  master.
578                          </p>
579                  </td>
580                </tr>
581                <tr>
582                  <td>
583                    <code class="literal">ReplicationManagerStartPolicy.REP_ELECTION</code>
584                  </td>
585                  <td>
586                          <p>
587                          As described above, the application starts up,
588                          looks for a master, and if one is not found calls
589                          for an election.
590                          </p>
591                  </td>
592                </tr>
593              </tbody>
594            </table>
595          </div>
596        </div>
597        <div class="sect2" lang="en" xml:lang="en">
598          <div class="titlepage">
599            <div>
600              <div>
601                <h3 class="title"><a id="thread_count"></a>Selecting the Number of Threads</h3>
602              </div>
603            </div>
604          </div>
605          <p>
606                    Under the hood, the Replication Manager is threaded and you can
607                    control the number of threads used to process messages received from
608                    other replicas. The threads that the Replication Manager uses are:
609            </p>
610          <div class="itemizedlist">
611            <ul type="disc">
612              <li>
613                <p>
614                                    Incoming message thread. This thread
615                                    receives messages from the site's
616                                    socket and passes those messages to
617                                    message processing threads (see below)
618                                    for handling.
619                            </p>
620              </li>
621              <li>
622                <p>
623                                    Outgoing message thread. Outgoing
624                                    messages are sent from whatever thread
625                                    performed a write to the database(s).
626                                    That is, the thread that called, for
627                                    example, 
628                                    
629                                    
630                                    <code class="methodname">Database.put()</code>
631                                    is the thread that writes replication messages
632                                    about that fact to the socket. 
633                            </p>
634                <p>
635                                    Note that if this write activity would
636                                    cause the thread to be blocked due to
637                                    some condition on the socket, the Replication Manager
638                                    will hand the outgoing message to the
639                                    incoming message thread, and it will
640                                    then write the message to the socket.
641                                    This prevents your database write
642                                    threads from blocking due to abnormal
643                                    network I/O conditions.
644                            </p>
645              </li>
646              <li>
647                <p>
648                                    Message processing threads are
649                                    responsible for parsing and then
650                                    responding to incoming replication
651                                    messages. Typically, a response will
652                                    include write activity to your
653                                    database(s), so these threads can be
654                                    busy performing disk I/O.
655                            </p>
656              </li>
657            </ul>
658          </div>
659          <p>
660                    Of these threads, the only ones that you have any
661                    configuration control over are the message processing
662                    threads. In this case, you can determine how many
663                    of these threads you want to run.
664            </p>
665          <p>
666                    It is always a bit of an art to decide on a thread count,
667                    but the short answer is you probably do not need more
668                    than three threads here, and it is likely that one will
669                    suffice. That said, the best thing to do is set your
670                    thread count to a fairly low number and then increase
671                    it if it appears that your application will benefit
672                    from the additional threads.
673            </p>
674        </div>
675      </div>
676    </div>
677    <div class="navfooter">
678      <hr />
679      <table width="100%" summary="Navigation footer">
680        <tr>
681          <td width="40%" align="left"><a accesskey="p" href="simpleprogramlisting.html">Prev</a>��</td>
682          <td width="20%" align="center">��</td>
683          <td width="40%" align="right">��<a accesskey="n" href="repmgr_init_example_c.html">Next</a></td>
684        </tr>
685        <tr>
686          <td width="40%" align="left" valign="top">Program Listing��</td>
687          <td width="20%" align="center">
688            <a accesskey="h" href="index.html">Home</a>
689          </td>
690          <td width="40%" align="right" valign="top">��Adding the Replication Manager to
691                    
692                    
693                    <span>SimpleTxn</span>
694            </td>
695        </tr>
696      </table>
697    </div>
698  </body>
699</html>
700