Adding the Replication Manager to SimpleTxn
Prev | Chapter 3. The DB Replication Manager | Next

## Adding the Replication Manager to SimpleTxn

We now use the methods described above to add partial support to the SimpleTxn example that we presented in Transactional Application. That is, in this section we will:

- Enhance our command line options to accept information of interest to a replicated application.

- Configure our environment handle to use replication and the Replication Manager.

- Minimally configure the Replication Manager.

- Start replication.

Note that when we are done with this section, we will be only partially ready to run the application. Some critical pieces will be missing; specifically, we will not yet be handling the differences between a master and a replica. (We do that in the next chapter).

Also, note that in the following code fragments, additions and changes to the code are marked in `bold`.

To begin, we make some significant changes to our `RepConfig` class because we will be using it to maintain a lot more information that we needed for our simple transactional example.

We begin by importing a few new classes. `java.util.Vector` is used to organize a list of "other host" definitions (that is, the host and port information for the other replication participants known to this application). We also need a couple of classes used to manage individual host and port information, as well as replication startup policy information.

    package db.repquote_gsg;

    import java.util.Vector;

    import com.sleepycat.db.ReplicationHostAddress;
    import com.sleepycat.db.ReplicationManagerStartPolicy;

    public class RepConfig
    {

Next we add considerably to the constants and data members used by this class. All of this is used to manage information necessary for replication purposes. We also at this point change the program's name, since we will be doing that to the main class in our application a little later in this description.

        // Constant values used in the RepQuote application.
        public static final String progname = "RepQuoteExampleGSG";
        public static final int CACHESIZE = 10 * 1024 * 1024;
        public static final int SLEEPTIME = 5000;

        // member variables containing configuration information
        // String specifying the home directory for rep files.
        public String home;
        // stores an optional set of "other" hosts.
        public Vector<ReplicationHostAddress> otherHosts;
        // priority within the replication group.
        public int priority;
        public ReplicationManagerStartPolicy startPolicy;
        // The host address to listen to.
        public ReplicationHostAddress thisHost;
        // Optional parameter specifying the # of sites in the
        // replication group.
        public int totalSites;

        // member variables used internally.
        private int currOtherHost;
        private boolean gotListenAddress;

Now we update our class constructor to initialize all of these new variables:

        public RepConfig()
        {
            startPolicy = ReplicationManagerStartPolicy.REP_ELECTION;
            home = "TESTDIR";
            gotListenAddress = false;
            totalSites = 0;
            priority = 100;
            currOtherHost = 0;
            thisHost = new ReplicationHostAddress();
            otherHosts = new Vector()<ReplicationHostAddress>;
        }

Finally, we finish updating this class by providing a series of new getter and setter methods. These are used primarily for setting a retrieving host information of interest to our replicated application:

        public java.io.File getHome()
        {
            return new java.io.File(home);
        }

        public void setThisHost(String host, int port)
        {
            gotListenAddress = true;
            thisHost.port = port;
            thisHost.host = host;
        }

        public ReplicationHostAddress getThisHost()
        {
            if (!gotListenAddress) {
                System.err.println("Warning: no host specified.");
                System.err.println("Returning default.");
            }
            return thisHost;
        }

        public boolean gotListenAddress() {
            return gotListenAddress;
        }

        public void addOtherHost(String host, int port, boolean peer)
        {
            ReplicationHostAddress newInfo =
                new ReplicationHostAddress(host, port, peer);
            otherHosts.add(newInfo);
        }

        public ReplicationHostAddress getFirstOtherHost()
        {
            currOtherHost = 0;
            if (otherHosts.size() == 0)
                return null;
            return (ReplicationHostAddress)otherHosts.get(currOtherHost);
        }

        public ReplicationHostAddress getNextOtherHost()
        {
            currOtherHost++;
            if (currOtherHost >= otherHosts.size())
                return null;
            return (ReplicationHostAddress)otherHosts.get(currOtherHost);
        }

        public ReplicationHostAddress getOtherHost(int i)
        {
            if (i >= otherHosts.size())
                return null;
            return (ReplicationHostAddress)otherHosts.get(i);
        }
    }

Having completed our update to the `RepConfig` class, we can now start making changes to the main portion of our program. We begin by changing the program's name. (This, of course, means that we copy our `SimpleTxn` code to a file named `RepQuoteExampleGSG.java`.)

    package db.repquote_gsg;

    import java.io.FileNotFoundException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.lang.Thread;
    import java.lang.InterruptedException;

    import com.sleepycat.db.Cursor;
    import com.sleepycat.db.Database;
    import com.sleepycat.db.DatabaseConfig;
    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.DatabaseException;
    import com.sleepycat.db.DatabaseType;
    import com.sleepycat.db.Environment;
    import com.sleepycat.db.EnvironmentConfig;
    import com.sleepycat.db.LockMode;
    import com.sleepycat.db.OperationStatus;
    import db.repquote.RepConfig;

    public class RepQuoteExampleGSG
    {
        private RepConfig repConfig;
        private Environment dbenv;

Next we update our usage function. The application will continue to accept the `-h` parameter so that we can identify the environment home directory used by this application. However, we also add the

- `-l` parameter which allows us to identify the host and port used by this application to listen for replication messages.

- `-r` parameter which allows us to specify other replicas.

- `-n` parameter which allows us to identify the number of sites in this replication group.

- `-p` option, which is used to identify this replica's priority (recall that the priority is used as a tie breaker for elections)

        public RepQuoteExampleGSG()
            throws DatabaseException
        {
            repConfig = null;
            dbenv = null;
        }

        public static void usage()
        {
            System.err.println("usage: " + repConfig.progname);
            System.err.println("-h home[-r host:port][-l host:port]" +
                "[-n nsites][-p priority]");

            System.err.println("\t -l host:port +
                 "(required; l stands for local)\n" +
                 "\t -r host:port (optional; r stands for replica; any " +
                 "number of these may be specified)\n" +
                 "\t -h home directory\n" +
                 "\t -n nsites (optional; number of sites in replication " +
                 "group; defaults to 0\n" +
                 "\t    in which case we try to dynamically compute the " +
                 "number of sites in\n" +
                 "\t    the replication group)\n" +
                 "\t -p priority (optional: defaults to 100)\n");

            System.exit(1);
        }

Now we can begin working on our `main()` function. We begin by adding a couple of variables that we will use to collect TCP/IP host and port information.

        public static void main(String[] argv)
            throws Exception
        {
            RepConfig config = new RepConfig();
            String tmpHost;
            int tmpPort = 0;

Now we collect our command line arguments. As we do so, we will configure host and port information as required, and we will configure the application's election priority if necessary.

            // Extract the command line parameters
            for (int i = 0; i < argv.length; i++)
            {
                if (argv[i].compareTo("-h") == 0) {
                    // home - a string arg.
                    i++;
                    config.home = argv[i];
               } else if (argv[i].compareTo("-l") == 0) {
                    // "me" should be host:port
                    i++;
                    String[] words = argv[i].split(":");
                    if (words.length != 2) {
                        System.err.println(
                            "Invalid host specification host:port needed.");
                        usage();
                    }
                    try {
                        tmpPort = Integer.parseInt(words[1]);
                    } catch (NumberFormatException nfe) {
                        System.err.println("Invalid host specification, " +
                            "could not parse port number.");
                        usage();
                    }
                    config.setThisHost(words[0], tmpPort);
                } else if (argv[i].compareTo("-n") == 0) {
                    i++;
                    config.totalSites = Integer.parseInt(argv[i]);
               } else if (argv[i].compareTo("-p") == 0) {
                    i++;
                    config.priority = Integer.parseInt(argv[i]);
                } else if (argv[i].compareTo("-r") == 0) {
                    i++;
                    String[] words = argv[i].split(":");
                    if (words.length != 2) {
                        System.err.println(
                            "Invalid host specification host:port needed.");
                        usage();
                    }
                    try {
                        tmpPort = Integer.parseInt(words[1]);
                    } catch (NumberFormatException nfe) {
                        System.err.println("Invalid host specification, " +
                            "could not parse port number.");
                        usage();
                    }
                    config.addOtherHost(words[0], tmpPort, isPeer);
                } else {
                    System.err.println("Unrecognized option: " + argv[i]);
                    usage();
                }
            }

            // Error check command line.
            if ((!config.gotListenAddress()) || config.home.length() == 0)
                usage();

Having done that, the remainder of our `main()` function is left unchanged, with the exception of a few name changes required by the new class name:

            RepQuoteExampleGSG runner = null;
            try {
                runner = new RepQuoteExampleGSG();
                runner.init(config);

                runner.doloop();
                runner.terminate();
            } catch (DatabaseException dbe) {
                System.err.println("Caught an exception during " +
                    "initialization or processing: " + dbe.toString());
                if (runner != null)
                    runner.terminate();
            }
                System.exit(0);
        } // end main

Now we need to update our `RepQuoteExampleGSG.init()` method. Our updates are at first related to configuring replication. First, we need to update the method so that we can identify the local site to the environment handle (that is, the site identified by the `-l` command line option):

        public int init(RepConfig config)
            throws DatabaseException
        {
            int ret = 0;
            appConfig = config;
            EnvironmentConfig envConfig = new EnvironmentConfig();
            envConfig.setErrorStream(System.err);
            envConfig.setErrorPrefix(RepConfig.progname);

            envConfig.setReplicationManagerLocalSite(appConfig.getThisHost());

And we also add code to allow us to identify "other" sites to the environment handle (that is, the sites that we identify using the `-o` command line option). To do this, we iterate over each of the "other" sites provided to us using the `-o` command line option, and we add each one individually in turn:

            for (ReplicationHostAddress host = appConfig.getFirstOtherHost();
                host != null; host = appConfig.getNextOtherHost())
            {
                envConfig.replicationManagerAddRemoteSite(host);
            }

And then we need code to allow us to identify the total number of sites in this replication group, and to set the environment's priority.

            if (appConfig.totalSites > 0)
                envConfig.setReplicationNumSites(appConfig.totalSites);
            envConfig.setReplicationPriority(appConfig.priority);

We can now open our environment. Note that the options we use to open the environment are slightly different for a replicated application than they are for a non-replicated application. Namely, replication requires the `EnvironmentConfig.setInitializeReplication()` option.

Also, because we are using the Replication Manager, we must prepare our environment for threaded usage. For this reason, we also need the `DB_THREAD` flag.

            envConfig.setCacheSize(RepConfig.CACHESIZE);
            envConfig.setTxnNoSync(true);

            envConfig.setAllowCreate(true);
            envConfig.setRunRecovery(true);
            envConfig.setInitializeReplication(true);
            envConfig.setInitializeLocking(true);
            envConfig.setInitializeLogging(true);
            envConfig.setInitializeCache(true);
            envConfig.setTransactional(true);
            try {
                dbenv = new Environment(appConfig.getHome(), envConfig);
            } catch(FileNotFoundException e) {
                System.err.println("FileNotFound exception: " + e.toString());
                System.err.println(
                    "Ensure that the environment directory is pre-created.");
                ret = 1;
            }

Finally, we start replication before we exit this method. Immediately after exiting this method, our application will go into the `RepQuoteExampleGSG.doloop()` method, which is where the bulk of our application's work is performed. We update that method in the next chapter.

            // start Replication Manager
            dbenv.replicationManagerStart(3, appConfig.startPolicy);
            return ret;
        }

This completes our replication updates for the moment. We are not as yet ready to actually run this program; there remains a few critical pieces left to add to it. However, the work that we performed in this section represents a solid foundation for the remainder of our replication work.

Prev | Up | Next
Chapter 3. The DB Replication Manager | Home | Permanent Message Handling