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