Chapter 3. The DB Replication Manager

Table of Contents

Starting and Stopping Replication
Managing Election Policies
Selecting the Number of Threads
Adding the Replication Manager to RepMgr
Permanent Message Handling
Identifying Permanent Message Policies
Setting the Permanent Message Timeout
Adding a Permanent Message Policy to RepMgrGSG
Managing Election Times
Managing Election Timeouts
Managing Election Retry Times
Managing Connection Retries
Managing Heartbeats

The easiest way to add replication to your transactional application is to use the Replication Manager. The Replication Manager provides a comprehensive communications layer that enables replication. For a brief listing of the Replication Manager's feature set, see Replication Manager Overview.

To use the Replication Manager, you make use of special methods off the DbEnv class. That is:

  1. Create an environment handle as normal.

  2. Configure your environment handle as needed (e.g. set the error file and error prefix values, if desired).

  3. Use the Replication Manager replication methods to configure the Replication Manager. Using these methods causes DB to know that you are using the Replication Manager.

    Configuring the Replication Manager entails setting the replication environment's priority, setting the TCP/IP address that this replication environment will use for incoming replication messages, identifying TCP/IP addresses of other replication environments, setting the number of replication environments in the replication group, and so forth. These actions are discussed throughout the remainder of this chapter.

  4. Open your environment handle. When you do this, be sure to specify DB_INIT_REP and DB_THREAD to your open flags. (This is in addition to the flags that you normally use for a single-threaded transactional application). The first of these causes replication to be initialized for the application. The second causes your environment handle to be free-threaded (thread safe). Both flags are required for Replication Manager usage.

  5. Start replication by calling DbEnv::repmgr_start().

  6. Open your databases as needed. Masters must open their databases for read and write activity. Replicas can open their databases for read-only activity, but doing so means they must re-open the databases if the replica ever becomes a master. Either way, replicas should never attempt to write to the database(s) directly.

Note

The Replication Manager allows you to only use one environment handle per process.

When you are ready to shut down your application:

  1. Close your databases

  2. Close your environment. This causes replication to stop as well.

Note

Before you can use the Replication Manager, you may have to enable it in your DB library. This is not a requirement for Microsoft Windows systems, or Unix systems that use pthread mutexes by default. Other systems, notably BSD and BSD-derived systems (such as Mac OS X), must enable the Replication Manager when you configure the DB build.

You do this by not disabling replication and by configuring the library with POSIX threads support. In other words, replication must be turned on in the build (it is by default), and POSIX thread support must be enabled if it is not already by default. To do this, use the --enable-pthread_api switch on the configure script.

For example:

../dist/configure --enable-pthread-api

Starting and Stopping Replication

As described above, you introduce replication to an application by starting with a transactional application, performing some basic replication configuration, and then starting replication using DbEnv::repmgr_start().

You stop replication by closing your environment cleanly in the same way you would for any DB application.

For example, the following code fragment initializes, then stops and starts replication. Note that other replication activities are omitted for brevity.

#include <db_cxx.h>

/* Use a 10mb cache */
#define CACHESIZE   (10 * 1024 * 1024)

...

    DbEnv *dbenv;            /* Environment handle. */
    const char *progname;     /* Program name. */
    const char *envHome;      /* Environment home directory. */
    const char *listen_host;  /* A TCP/IP hostname. */
    const char *other_host;   /* A TCP/IP hostname. */
    u_int16 listen_port;      /* A TCP/IP port. */
    u_int16 other_port;       /* A TCP/IP port. */

    /* Initialize variables */
    dbenv = NULL;
    progname = "example_replication";
    envHome = "ENVIRONMENT_HOME";
    listen_host = "mymachine.sleepycat.com";
    listen_port = 5001;
    other_host = "anothermachine.sleepycat.com";
    other_port = 4555;

    try {
        /* Create the environment handle */
        dbenv = new DbEnv(0);

        /*
         * Configure the environment handle. Here we configure 
         * asynchronous transactional commits for performance reasons. 
         */
        dbenv->set_errfile(stderr);
        dbenv->set_errpfx(progname);
        (void)dbenv->set_cachesize(0, CACHESIZE, 0);
        (void)dbenv->set_flags(DB_TXN_NOSYNC, 1);

        /*
         * Configure the local address. This is the local hostname and 
         * port that this replication environment will use to receive 
         * incoming replication messages. Note that this can be 
         * performed only once for the replication environment. 
         * It is required.
         */
       dbenv->repmgr_set_local_site(listen_host, listen_port, 0);

       /*
        * Set this replication environment's priority. This is used 
        * for elections.
        *
        * Set this number to a positive integer, or 0 if you do not want
        * this site to be able to become a master.
        */
        dbenv->rep_set_priority(100);

        /*
         * Add a site to the list of replication environments known to 
         * this application. 
         */
         dbenv->repmgr_add_remote_site(dbenv, other_host, other_port, 
                                       NULL, 0);

        /*
         * Identify the number of sites in the replication group. This is
         * necessary so that elections and permanent message handling 
         * can be performed correctly.
         */
         dbenv->rep_set_nsites(2);

        /* Open the environment handle. Note that we add DB_THREAD and
         * DB_INIT_REP to the list of flags. These are required.
         */
         dbenv->open(home, DB_CREATE | DB_RECOVER |
                           DB_INIT_LOCK | DB_INIT_LOG |
                           DB_INIT_MPOOL | DB_INIT_TXN  |
                           DB_THREAD | DB_INIT_REP,
                           0);

       /* 
        * Start the replication manager such that it uses 3 
        * threads. 
        */
         dbenv->repmgr_start(3, DB_REP_ELECTION);

       /* Sleep to give ourselves time to find a master */
        sleep(5);

       /*
        **********************************************************
        *** All other application code goes here, including  *****
        *** database opens                                   *****
        **********************************************************
        */


    } catch (DbException &de) {
        /* Error handling goes here */
    }

    /* Close out the application here.
    try {
        /* 
         * Make sure all your database handles are closed 
         *  (omitted from this example). 
         */

        /* Close the environment */
        if (dbenv != NULL)
            (void)dbenv->close(dbenv, 0);

    } catch (DbException &de) {
        /* Error handling goes here */
    }

    /* All done */ 

Managing Election Policies

Before continuing, it is worth taking a look at the startup election flags accepted by DbEnv::repgmr_start(). These flags control how your replication application will behave when it first starts up.

In the previous example, we specified DB_REP_ELECTION when we started replication. This causes the application to try to find a master upon startup. If it cannot, it calls for an election. In the event an election is held, the environment receiving the most number of votes will become the master.

There's some important points to make here:

  • This flag only requires that other environments in the replication group participate in the vote. There is no requirement that all such environments participate. In other words, if an environment starts up, it can call for an election, and select a master, even if all other environment have not yet joined the replication group.

  • It only requires a simple majority of participating environments to elect a master. The number of environments used to calculate the simple majority is based on the value set for DbEnv::rep_set_nsites(). This is always true of elections held using the Replication Manager.

  • As always, the environment participating in the election with the most up-to-date log files is selected as master. If an environment with more recent log files has not yet joined the replication group, it may not become the master.

Any one of these points may be enough to cause a less-than-optimum environment to be selected as master. Therefore, to give you a better degree of control over which environment becomes a master at application startup, the Replication Manager offers the following start-up flags:

Flag Description
DB_REP_MASTER

The application starts up and declares the environment to be a master without calling for an election. It is an error for more than one environment to start up using this flag, or for an environment to use this flag when a master already exists.

Note that no replication group should ever operate with more than one master.

In the event that a environment attempts to become a master when a master already exists, the replication code will resolve the problem by holding an election. Note, however, that there is always a possibility of data loss in the face of duplicate masters, because once a master is selected, the environment that loses the election will have to roll back any transactions committed until it is in sync with the "real" master.

DB_REP_CLIENT

The application starts up and declares the environment to be a replica without calling for an election. Note that the environment can still become a master if a subsequent application starts up, calls for an election, and this environment is elected master.

DB_REP_ELECTION

As described above, the application starts up, looks for a master, and if one is not found calls for an election.

Selecting the Number of Threads

Under the hood, the Replication Manager is threaded and you can control the number of threads used to process messages received from other replicas. The threads that the Replication Manager uses are:

  • Incoming message thread. This thread receives messages from the site's socket and passes those messages to message processing threads (see below) for handling.

  • Outgoing message thread. Outgoing messages are sent from whatever thread performed a write to the database(s). That is, the thread that called, for example, Db::put() is the thread that writes replication messages about that fact to the socket.

    Note that if this write activity would cause the thread to be blocked due to some condition on the socket, the Replication Manager will hand the outgoing message to the incoming message thread, and it will then write the message to the socket. This prevents your database write threads from blocking due to abnormal network I/O conditions.

  • Message processing threads are responsible for parsing and then responding to incoming replication messages. Typically, a response will include write activity to your database(s), so these threads can be busy performing disk I/O.

Of these threads, the only ones that you have any configuration control over are the message processing threads. In this case, you can determine how many of these threads you want to run.

It is always a bit of an art to decide on a thread count, but the short answer is you probably do not need more than three threads here, and it is likely that one will suffice. That said, the best thing to do is set your thread count to a fairly low number and then increase it if it appears that your application will benefit from the additional threads.