Example #1
0
        public static void Main(string[] args)
        {
            RepConfig config = new RepConfig();
            bool isPeer;
            uint tmpPort = 0;

            /*
             * RepQuoteExample is meant to be run from build_windows\AnyCPU, in
             * either the Debug or Release directory. The required core
             * libraries, however, are in either build_windows\Win32 or
             * build_windows\x64, depending upon the platform.  That location
             * needs to be added to the PATH environment variable for the
             * P/Invoke calls to work.
             */
            try {
                String pwd = Environment.CurrentDirectory;
                pwd = Path.Combine(pwd, "..");
                pwd = Path.Combine(pwd, "..");
                if (IntPtr.Size == 4)
                    pwd = Path.Combine(pwd, "Win32");
                else
                    pwd = Path.Combine(pwd, "x64");
            #if DEBUG
                pwd = Path.Combine(pwd, "Debug");
            #else
                pwd = Path.Combine(pwd, "Release");
            #endif
                pwd += ";" + Environment.GetEnvironmentVariable("PATH");
                Environment.SetEnvironmentVariable("PATH", pwd);
            } catch (Exception e) {
                Console.WriteLine(
                    "Unable to set the PATH environment variable.");
                Console.WriteLine(e.Message);
                return;
            }

            /*  Extract the command line parameters. */
            for (int i = 0; i < args.Length; i++)
            {
                isPeer = false;
                string s = args[i];
                if (s[0] != '-')
                    continue;
                switch (s[1])
                {
                    case 'a':
                        if (i == args.Length - 1)
                            usage();
                        i++;
                        if (args[i].Equals("all"))
                            config.ackPolicy = AckPolicy.ALL;
                        else if (!args[i].Equals("quorum"))
                            usage();
                        break;
                    case 'b':
                        config.bulk = true;
                        break;
                    case 'C':
                        config.startPolicy = StartPolicy.CLIENT;
                        break;
                    case 'h':
                        if (i == args.Length - 1)
                            usage();
                        i++;
                        config.home = args[i];
                        break;
                    case 'l':
                        if (i == args.Length - 1)
                            usage();
                        i++;
                        string[] words = args[i].Split(':');
                        if (words.Length != 2)
                        {
                            Console.Error.WriteLine("Invalid host " +
                                "specification host:port needed.");
                            usage();
                        }
                        try
                        {
                            tmpPort = uint.Parse(words[1]);
                        } catch (InvalidCastException)
                        {
                            Console.Error.WriteLine("Invalid host " +
                                "specification, could not parse port number.");
                            usage();
                        }
                        config.host.Host = words[0];
                        config.host.Port = tmpPort;
                        break;
                    case 'M':
                        config.startPolicy = StartPolicy.MASTER;
                        break;
                    case 'n':
                        if (i == args.Length - 1)
                            usage();
                        i++;
                        try
                        {
                            config.totalSites = uint.Parse(args[i]);
                        } catch (InvalidCastException)
                        {
                            Console.Error.WriteLine(
                            "Unable to parse number of total sites.");
                            usage();
                        }
                        break;
                    case 'p':
                        if (i == args.Length - 1)
                            usage();
                        i++;
                        try
                        {
                            config.priority = uint.Parse(args[i]);
                        } catch (InvalidCastException)
                        {
                            Console.Error.WriteLine("Unable to parse priority.");
                            usage();
                        }
                        break;
                    case 'r':
                    case 'R':
                        if (i == args.Length - 1)
                            usage();
                        if (args[i].Equals("R"))
                            isPeer = true;
                        i++;
                        words = args[i].Split(':');
                        if (words.Length != 2)
                        {
                            Console.Error.WriteLine("Invalid host " +
                                "specification host:port needed.");
                            usage();
                        }
                        try
                        {
                            tmpPort = uint.Parse(words[1]);
                        } catch (InvalidCastException)
                        {
                            Console.Error.WriteLine("Invalid host " +
                                "specification, could not parse port number.");
                            usage();
                        }
                        config.remote.Add(
                            new RemoteSite(words[0], tmpPort, isPeer));
                        break;
                    case 'v':
                        config.verbose = true;
                        break;
                    default:
                        Console.Error.WriteLine(
                            "Unrecognized option: " + args[i]);
                        usage();
                        break;
                }
            }

            /* Error check command line. */
            if (config.host.Host == null || config.home.Length == 0)
                usage();

            RepQuoteExample runner = null;
            try
            {
                runner = new RepQuoteExample();
                runner.init(config);
                runner.doloop();
                runner.terminate();
                runner = null;
            } catch (DatabaseException dbErr)
            {
                Console.Error.WriteLine("Caught an exception during " +
                    "initialization or processing: " + dbErr);
                if (runner != null)
                    runner.terminate();
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            RepConfig config = new RepConfig();
            bool      isPeer;
            uint      tmpPort = 0;

            /*
             * RepQuoteExample is meant to be run from build_windows\AnyCPU, in
             * either the Debug or Release directory. The required core
             * libraries, however, are in either build_windows\Win32 or
             * build_windows\x64, depending upon the platform.  That location
             * needs to be added to the PATH environment variable for the
             * P/Invoke calls to work.
             */
            try {
                String pwd = Environment.CurrentDirectory;
                pwd = Path.Combine(pwd, "..");
                pwd = Path.Combine(pwd, "..");
                if (IntPtr.Size == 4)
                {
                    pwd = Path.Combine(pwd, "Win32");
                }
                else
                {
                    pwd = Path.Combine(pwd, "x64");
                }
#if DEBUG
                pwd = Path.Combine(pwd, "Debug");
#else
                pwd = Path.Combine(pwd, "Release");
#endif
                pwd += ";" + Environment.GetEnvironmentVariable("PATH");
                Environment.SetEnvironmentVariable("PATH", pwd);
            } catch (Exception e) {
                Console.WriteLine(
                    "Unable to set the PATH environment variable.");
                Console.WriteLine(e.Message);
                return;
            }

            /*  Extract the command line parameters. */
            for (int i = 0; i < args.Length; i++)
            {
                isPeer = false;
                string s = args[i];
                if (s[0] != '-')
                {
                    continue;
                }
                switch (s[1])
                {
                case 'a':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    i++;
                    if (args[i].Equals("all"))
                    {
                        config.ackPolicy = AckPolicy.ALL;
                    }
                    else if (!args[i].Equals("quorum"))
                    {
                        usage();
                    }
                    break;

                case 'b':
                    config.bulk = true;
                    break;

                case 'C':
                    config.startPolicy = StartPolicy.CLIENT;
                    break;

                case 'h':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    i++;
                    config.home = args[i];
                    break;

                case 'l':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    i++;
                    string[] words = args[i].Split(':');
                    if (words.Length != 2)
                    {
                        Console.Error.WriteLine("Invalid host " +
                                                "specification host:port needed.");
                        usage();
                    }
                    try
                    {
                        tmpPort = uint.Parse(words[1]);
                    } catch (InvalidCastException)
                    {
                        Console.Error.WriteLine("Invalid host " +
                                                "specification, could not parse port number.");
                        usage();
                    }
                    config.host.Host = words[0];
                    config.host.Port = tmpPort;
                    break;

                case 'M':
                    config.startPolicy = StartPolicy.MASTER;
                    break;

                case 'n':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    i++;
                    try
                    {
                        config.totalSites = uint.Parse(args[i]);
                    } catch (InvalidCastException)
                    {
                        Console.Error.WriteLine(
                            "Unable to parse number of total sites.");
                        usage();
                    }
                    break;

                case 'p':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    i++;
                    try
                    {
                        config.priority = uint.Parse(args[i]);
                    } catch (InvalidCastException)
                    {
                        Console.Error.WriteLine("Unable to parse priority.");
                        usage();
                    }
                    break;

                case 'r':
                case 'R':
                    if (i == args.Length - 1)
                    {
                        usage();
                    }
                    if (args[i].Equals("R"))
                    {
                        isPeer = true;
                    }
                    i++;
                    words = args[i].Split(':');
                    if (words.Length != 2)
                    {
                        Console.Error.WriteLine("Invalid host " +
                                                "specification host:port needed.");
                        usage();
                    }
                    try
                    {
                        tmpPort = uint.Parse(words[1]);
                    } catch (InvalidCastException)
                    {
                        Console.Error.WriteLine("Invalid host " +
                                                "specification, could not parse port number.");
                        usage();
                    }
                    config.remote.Add(
                        new RemoteSite(words[0], tmpPort, isPeer));
                    break;

                case 'v':
                    config.verbose = true;
                    break;

                default:
                    Console.Error.WriteLine(
                        "Unrecognized option: " + args[i]);
                    usage();
                    break;
                }
            }

            /* Error check command line. */
            if (config.host.Host == null || config.home.Length == 0)
            {
                usage();
            }

            RepQuoteExample runner = null;
            try
            {
                runner = new RepQuoteExample();
                runner.init(config);
                runner.doloop();
                runner.terminate();
                runner = null;
            } catch (DatabaseException dbErr)
            {
                Console.Error.WriteLine("Caught an exception during " +
                                        "initialization or processing: " + dbErr);
                if (runner != null)
                {
                    runner.terminate();
                }
            }
        }         /* End main. */
Example #3
0
        public int init(RepConfig config)
        {
            int ret = 0;

            DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig();
            envConfig.ErrorPrefix = RepConfig.progname;
            envConfig.RepSystemCfg = new ReplicationConfig();
            envConfig.RepSystemCfg.RepMgrLocalSite = config.host;
            for (int i = 0; i < config.remote.Count; i++)
                envConfig.RepSystemCfg.AddRemoteSite(config.remote[i].Host,
                    config.remote[i].IsPeer);

            if (config.totalSites > 0)
                envConfig.RepSystemCfg.NSites = config.totalSites;

            envConfig.RepSystemCfg.BulkTransfer = config.bulk;

            /*
             * Configure heartbeat timeouts so that repmgr monitors the
             * health of the TCP connection.  Master sites broadcast a heartbeat
             * at the frequency specified by the DB_REP_HEARTBEAT_SEND timeout.
             * Client sites wait for message activity the length of the
             * DB_REP_HEARTBEAT_MONITOR timeout before concluding that the
             * connection to the master is lost.  The DB_REP_HEARTBEAT_MONITOR
             * timeout should be longer than the DB_REP_HEARTBEAT_SEND timeout.
             */
            envConfig.RepSystemCfg.HeartbeatMonitor = 10000000;
            envConfig.RepSystemCfg.HeartbeatSend = 5000000;

            /*
             * Set replication group election priority for this environment.
             * An election first selects the site with the most recent log
             * records as the new master.  If multiple sites have the most
             * recent log records, the site with the highest priority value
             * is selected as master.
             */
            envConfig.RepSystemCfg.Priority = config.priority;
            envConfig.MPoolSystemCfg = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = RepConfig.CACHESIZE;
            envConfig.TxnNoSync = true;

            envConfig.EventNotify = new EventNotifyDelegate(RepQuoteEventHandler);

            /*
             * Set the policy that determines how master and client sites
             * handle acknowledgement of replication messages needed for
             * permanent records.  The default policy of "quorum" requires only
             * a quorum of electable peers sufficient to ensure a permanent
             * record remains durable if an election is held.  The "all" option
             * requires all clients to acknowledge a permanent replication
             * message instead.
             */
            envConfig.RepSystemCfg.RepMgrAckPolicy = config.ackPolicy;

            /*
             * Set the threshold for the minimum and maximum time the client
             * waits before requesting retransmission of a missing message.
             * Base these values on the performance and load characteristics
             * of the master and client host platforms as well as the round
             * trip message time.
             */
            envConfig.RepSystemCfg.RetransmissionRequest(20000, 500000);

            /*
             * Configure deadlock detection to ensure that any deadlocks
             * are broken by having one of the conflicting lock requests
             * rejected. DB_LOCK_DEFAULT uses the lock policy specified
             * at environment creation time or DB_LOCK_RANDOM if none was
             * specified.
             */
            envConfig.LockSystemCfg = new LockingConfig();
            envConfig.LockSystemCfg.DeadlockResolution = DeadlockPolicy.DEFAULT;

            envConfig.Create = true;
            envConfig.RunRecovery = true;
            envConfig.FreeThreaded = true;
            envConfig.UseReplication = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            envConfig.Verbosity = new VerboseMessages();
            envConfig.Verbosity.Replication = config.verbose;

            try
            {
                dbenv = RepQuoteEnvironment.Open(config.home, envConfig);
            } catch(DatabaseException e)
            {
                Console.WriteLine("Fail to open environment: " + e.Message);
                return 1;
            }

            /* The following base replication features may also be useful to your
             * application. See Berkeley DB documentation for more details.
             *   - Master leases: Provide stricter consistency for data reads
             *     on a master site.
             *   - Timeouts: Customize the amount of time Berkeley DB waits
             *     for such things as an election to be concluded or a master
             *     lease to be granted.
             *   - Delayed client synchronization: Manage the master site's
             *     resources by spreading out resource-intensive client
             *     synchronizations.
             *   - Blocked client operations: Return immediately with an error
             *     instead of waiting indefinitely if a client operation is
             *     blocked by an ongoing client synchronization.
             *
             * The following repmgr features may also be useful to your
             * application.  See Berkeley DB documentation for more details.
             *  - Two-site strict majority rule - In a two-site replication
             *    group, require both sites to be available to elect a new
             *    master.
             *  - Timeouts - Customize the amount of time repmgr waits
             *    for such things as waiting for acknowledgements or attempting
             *    to reconnect to other sites.
             *  - Site list - return a list of sites currently known to repmgr.
             */

            /* Start checkpoint and log archive support threads. */
            checkpointThread = new Thread(new ThreadStart(CheckPoint));
            checkpointThread.Start();
            logArchiveThread = new Thread(new ThreadStart(LogArchive));
            logArchiveThread.Start();

            /* Start replication manager. */
            if (config.startPolicy == StartPolicy.CLIENT)
                dbenv.env.RepMgrStartClient(3);
            else if (config.startPolicy == StartPolicy.ELECTION)
                dbenv.env.RepMgrStartClient(3, true);
            else if (config.startPolicy == StartPolicy.MASTER)
                dbenv.env.RepMgrStartMaster(3);

            return ret;
        }
Example #4
0
        public int init(RepConfig config)
        {
            int ret = 0;

            DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig();

            envConfig.ErrorPrefix  = RepConfig.progname;
            envConfig.RepSystemCfg = new ReplicationConfig();
            envConfig.RepSystemCfg.RepMgrLocalSite = config.host;
            for (int i = 0; i < config.remote.Count; i++)
            {
                envConfig.RepSystemCfg.AddRemoteSite(config.remote[i].Host,
                                                     config.remote[i].IsPeer);
            }

            if (config.totalSites > 0)
            {
                envConfig.RepSystemCfg.NSites = config.totalSites;
            }

            envConfig.RepSystemCfg.BulkTransfer = config.bulk;

            /*
             * Configure heartbeat timeouts so that repmgr monitors the
             * health of the TCP connection.  Master sites broadcast a heartbeat
             * at the frequency specified by the DB_REP_HEARTBEAT_SEND timeout.
             * Client sites wait for message activity the length of the
             * DB_REP_HEARTBEAT_MONITOR timeout before concluding that the
             * connection to the master is lost.  The DB_REP_HEARTBEAT_MONITOR
             * timeout should be longer than the DB_REP_HEARTBEAT_SEND timeout.
             */
            envConfig.RepSystemCfg.HeartbeatMonitor = 10000000;
            envConfig.RepSystemCfg.HeartbeatSend    = 5000000;

            /*
             * Set replication group election priority for this environment.
             * An election first selects the site with the most recent log
             * records as the new master.  If multiple sites have the most
             * recent log records, the site with the highest priority value
             * is selected as master.
             */
            envConfig.RepSystemCfg.Priority    = config.priority;
            envConfig.MPoolSystemCfg           = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = RepConfig.CACHESIZE;
            envConfig.TxnNoSync = true;

            envConfig.EventNotify = new EventNotifyDelegate(RepQuoteEventHandler);

            /*
             * Set the policy that determines how master and client sites
             * handle acknowledgement of replication messages needed for
             * permanent records.  The default policy of "quorum" requires only
             * a quorum of electable peers sufficient to ensure a permanent
             * record remains durable if an election is held.  The "all" option
             * requires all clients to acknowledge a permanent replication
             * message instead.
             */
            envConfig.RepSystemCfg.RepMgrAckPolicy = config.ackPolicy;

            /*
             * Set the threshold for the minimum and maximum time the client
             * waits before requesting retransmission of a missing message.
             * Base these values on the performance and load characteristics
             * of the master and client host platforms as well as the round
             * trip message time.
             */
            envConfig.RepSystemCfg.RetransmissionRequest(20000, 500000);

            /*
             * Configure deadlock detection to ensure that any deadlocks
             * are broken by having one of the conflicting lock requests
             * rejected. DB_LOCK_DEFAULT uses the lock policy specified
             * at environment creation time or DB_LOCK_RANDOM if none was
             * specified.
             */
            envConfig.LockSystemCfg = new LockingConfig();
            envConfig.LockSystemCfg.DeadlockResolution = DeadlockPolicy.DEFAULT;

            envConfig.Create                = true;
            envConfig.RunRecovery           = true;
            envConfig.FreeThreaded          = true;
            envConfig.UseReplication        = true;
            envConfig.UseLocking            = true;
            envConfig.UseLogging            = true;
            envConfig.UseMPool              = true;
            envConfig.UseTxns               = true;
            envConfig.Verbosity             = new VerboseMessages();
            envConfig.Verbosity.Replication = config.verbose;

            try
            {
                dbenv = RepQuoteEnvironment.Open(config.home, envConfig);
            } catch (DatabaseException e)
            {
                Console.WriteLine("Fail to open environment: " + e.Message);
                return(1);
            }


            /* The following base replication features may also be useful to your
             * application. See Berkeley DB documentation for more details.
             *   - Master leases: Provide stricter consistency for data reads
             *     on a master site.
             *   - Timeouts: Customize the amount of time Berkeley DB waits
             *     for such things as an election to be concluded or a master
             *     lease to be granted.
             *   - Delayed client synchronization: Manage the master site's
             *     resources by spreading out resource-intensive client
             *     synchronizations.
             *   - Blocked client operations: Return immediately with an error
             *     instead of waiting indefinitely if a client operation is
             *     blocked by an ongoing client synchronization.
             *
             * The following repmgr features may also be useful to your
             * application.  See Berkeley DB documentation for more details.
             *  - Two-site strict majority rule - In a two-site replication
             *    group, require both sites to be available to elect a new
             *    master.
             *  - Timeouts - Customize the amount of time repmgr waits
             *    for such things as waiting for acknowledgements or attempting
             *    to reconnect to other sites.
             *  - Site list - return a list of sites currently known to repmgr.
             */

            /* Start checkpoint and log archive support threads. */
            checkpointThread = new Thread(new ThreadStart(CheckPoint));
            checkpointThread.Start();
            logArchiveThread = new Thread(new ThreadStart(LogArchive));
            logArchiveThread.Start();

            /* Start replication manager. */
            if (config.startPolicy == StartPolicy.CLIENT)
            {
                dbenv.env.RepMgrStartClient(3);
            }
            else if (config.startPolicy == StartPolicy.ELECTION)
            {
                dbenv.env.RepMgrStartClient(3, true);
            }
            else if (config.startPolicy == StartPolicy.MASTER)
            {
                dbenv.env.RepMgrStartMaster(3);
            }

            return(ret);
        }