Ejemplo n.º 1
0
        public void StableClient1()
        {
            string home = testHome + "/StableClient1";

            Configuration.ClearDir(home);

            // Get notification from master and start the #1 client.
            client1StartSignal.WaitOne();
            Console.WriteLine("Client1: Join the replication");

            // Open the environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepMgrLocalSite =
                new ReplicationHostAddress("127.0.0.1", 7888);
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", 8888), false);
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", 5888), true);
            cfg.RepSystemCfg.NSites          = 4;
            cfg.RepSystemCfg.ElectionRetry   = 10;
            cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.NONE;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Start the client who won't raise any election.
            env.RepMgrStartClient(3, false);

            // Leave enough time to sync.
            Thread.Sleep(20000);

            // The current client site is fully initialized.
            client1ReadySignal.Set();

            foreach (RepMgrSite site in env.RepMgrRemoteSites)
            {
                if (site.Address.Port == 5888)
                {
                    Assert.IsTrue(site.isPeer);
                }
                else
                {
                    Assert.IsFalse(site.isPeer);
                }
            }

            // Wait for master's leave signal.
            masterLeaveSignal.WaitOne();

            /*
             * Set the master's leave signal so that other clients
             * could be informed.
             */
            masterLeaveSignal.Set();

            // Leave sometime for client to hold election.
            Thread.Sleep(10000);

            env.LogFlush();
            env.Close();
            Console.WriteLine("Client1: Leaving the replication");
        }
Ejemplo n.º 2
0
        public void StableClient2()
        {
            string home = testHome + "/StableClient2";

            Configuration.ClearDir(home);

            client2StartSignal.WaitOne();
            Console.WriteLine("Client2: Join the replication");

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepMgrLocalSite =
                new ReplicationHostAddress("127.0.0.1", 6888);
            cfg.RepSystemCfg.Priority = 20;
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", 8888), false);
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", 7888), true);
            cfg.RepSystemCfg.NSites          = 4;
            cfg.RepSystemCfg.ElectionRetry   = 10;
            cfg.RepSystemCfg.RepMgrAckPolicy =
                AckPolicy.ONE_PEER;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Start the client who will raise election if no master.
            env.RepMgrStartClient(3, true);

            // Leave enough time to sync.
            Thread.Sleep(20000);

            // The current client site is fully initialized.
            client2ReadySignal.Set();

            // Wait for master's leave signal.
            masterLeaveSignal.WaitOne();

            /*
             * Set the master's leave signal so that other clients
             * could be informed.
             */
            masterLeaveSignal.Set();

            // Leave sometime for client to hold election.
            Thread.Sleep(5000);

            env.LogFlush();
            env.Close();
            Console.WriteLine("Client2: Leaving the replication");
        }
        public void TestCommitSuccess()
        {
            testName = "TestCommitSuccess";
            SetUpTest(true);
            string[] keys = { "key 1", "key 2", "key 3", "key 4",
                              "key 5", "key 6", "key 7", "key 8","key 9", "key 10" };

            DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, masterPort, true);
            DatabaseEnvironment client = SetUpEnv(testHome + "/client", 0, clientPort, false);

            master.RepMgrStartMaster(2);
            Thread.Sleep(2000);
            client.RepMgrStartClient(2);

            BTreeDatabase db1 = Open(master, true);
            BTreeDatabase db2 = null;

            for (; ;)
            {
                if (db2 == null)
                {
                    try {
                        db2 = Open(client, false);
                        break;
                    } catch (DatabaseException) {
                        if (db2 != null)
                        {
                            db2.Close(true);
                            db2 = null;
                        }
                        System.Threading.Thread.Sleep(1000);
                        continue;
                    }
                }
            }

            try {
                for (int i = 0; i < 3; i++)
                {
                    Transaction txn = master.BeginTransaction();

                    // Get the key.
                    DatabaseEntry key, data;
                    key = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes(keys[i]));

                    data = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes(keys[i]));

                    db1.Put(key, data, txn);
                    txn.Commit();

                    byte[] token = txn.CommitToken;
                    Assert.AreEqual(master.IsTransactionApplied(token, 5000), TransactionAppliedStatus.APPLIED);
                    Assert.AreEqual(client.IsTransactionApplied(token, 200000), TransactionAppliedStatus.APPLIED);
                }
            }
            finally {
                db1.Close();
                db2.Close();
                master.Close();
                client.Close();
            }
        }
Ejemplo n.º 4
0
        public void Client()
        {
            string home = testHome + "/Client";

            Configuration.ClearDir(home);

            clientStartSignal.WaitOne();
            Console.WriteLine("Client: Join the replication");

            // Open a environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepMgrLocalSite =
                new ReplicationHostAddress("127.0.0.1", 6870);
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", 8870), false);
            cfg.RepSystemCfg.NSites = 2;
            cfg.EventNotify         = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            // Start a client site with replication manager.
            env.RepMgrStartClient(3, false);

            // Leave enough time to sync.
            Thread.Sleep(20000);

            // Open database.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.NEVER;
            dbConfig.AutoCommit = true;
            dbConfig.Env        = env;
            dbConfig.PageSize   = 512;
            BTreeDatabase db = BTreeDatabase.Open("rep.db",
                                                  dbConfig);

            // Write data into database.
            Console.WriteLine("Client: Start reading data #1.");
            for (int i = 0; i < 5; i++)
            {
                db.GetBoth(new DatabaseEntry(
                               BitConverter.GetBytes(i)), new DatabaseEntry(
                               BitConverter.GetBytes(i)));
            }

            // Leave sometime for client to read new data from master.
            Thread.Sleep(20000);

            /*
             * Read the data. All data exists in master site should
             * appear in the client site.
             */
            Console.WriteLine("Client: Start reading data #2.");
            for (int i = 10; i < 15; i++)
            {
                db.GetBoth(new DatabaseEntry(
                               BitConverter.GetBytes(i)), new DatabaseEntry(
                               BitConverter.GetBytes(i)));
            }

            // Get the latest replication subsystem statistics.
            ReplicationStats repStats = env.ReplicationSystemStats();

            Assert.IsTrue(repStats.ClientStartupComplete);
            Assert.AreEqual(1, repStats.DuplicateLogRecords);
            Assert.LessOrEqual(0, repStats.EnvID);
            Assert.LessOrEqual(0, repStats.NextPage);
            Assert.LessOrEqual(0, repStats.ReceivedPages);
            Assert.AreEqual(1, repStats.Status);

            // Close all.
            db.Close(false);
            env.LogFlush();
            env.Close();
            Console.WriteLine(
                "Client: All data is read. Leaving the replication");

            // The master is closed after client's close.
            masterCloseSignal.Set();
        }
Ejemplo n.º 5
0
        public void StableClient3()
        {
            string home = testHome + "/StableClient3";

            Configuration.ClearDir(home);

            client3StartSignal.WaitOne();
            Console.WriteLine("Client3: Join the replication");

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[3];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority = 80;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Host = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Port = ports[1];
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Peer = true;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            cfg.RepSystemCfg.ElectionRetry   = 10;
            cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.QUORUM;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            env.RepMgrStartClient(3, false);

            // Leave enough time to sync with master.
            Thread.Sleep(20000);

            // The current client site is fully initialized.
            client3ReadySignal.Set();

            // The current client site is fully initialized.
            client1ReadySignal.Set();

            foreach (RepMgrSite site in env.RepMgrRemoteSites)
            {
                if (site.Address.Port == ports[3])
                {
                    Assert.IsTrue(site.isPeer);
                }
                else
                {
                    Assert.IsFalse(site.isPeer);
                }
            }

            // Wait for master's leave signal.
            masterLeaveSignal.WaitOne();

            /*
             * Set the master's leave signal so that other clients
             * could be informed.
             */
            masterLeaveSignal.Set();

            /*
             * Master will leave the replication after all clients'
             * initialization. Leave sometime for master to leave
             * and for clients elect.
             */
            Thread.Sleep(5000);

            ReplicationStats repStats = env.ReplicationSystemStats();

            Assert.LessOrEqual(0, repStats.Elections);
            Assert.LessOrEqual(0, repStats.ElectionTiebreaker);
            Assert.LessOrEqual(0,
                               repStats.ElectionTimeSec + repStats.ElectionTimeUSec);
            Assert.LessOrEqual(0, repStats.MasterChanges);
            Assert.LessOrEqual(0, repStats.NewSiteMessages);
            Assert.LessOrEqual(0, repStats.ReceivedLogRecords);
            Assert.LessOrEqual(0, repStats.ReceivedMessages);
            Assert.LessOrEqual(0, repStats.ReceivedPages);
            Assert.GreaterOrEqual(4, repStats.RegisteredSitesNeeded);
            Assert.LessOrEqual(0, repStats.Sites);

            /*
             * Client 3 will be the new master. The Elected master should wait
             * until all other clients leave.
             */
            Thread.Sleep(10000);

            env.LogFlush();
            env.Close();
            Console.WriteLine("Client3: Leaving the replication");
        }
Ejemplo n.º 6
0
        public void StableClient1()
        {
            string home = testHome + "/StableClient1";

            Configuration.ClearDir(home);

            // Get notification from master and start the #1 client.
            client1StartSignal.WaitOne();
            Console.WriteLine("Client1: Join the replication");

            // Open the environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.UseReplication           = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.UseLocking   = true;
            cfg.UseTxns      = true;
            cfg.UseMPool     = true;
            cfg.Create       = true;
            cfg.UseLogging   = true;
            cfg.RunRecovery  = true;
            cfg.TxnNoSync    = true;
            cfg.FreeThreaded = true;
            cfg.LockTimeout  = 50000;
            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[1];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1]        = new DbSiteConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[2]      = new DbSiteConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Host = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Port = ports[3];
            cfg.RepSystemCfg.RepmgrSitesConfig[2].Peer = true;
            cfg.RepSystemCfg.ElectionRetry             = 10;
            cfg.RepSystemCfg.RepMgrAckPolicy           = AckPolicy.NONE;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Start the client who won't raise any election.
            env.RepMgrStartClient(3, false);

            // Leave enough time to sync.
            Thread.Sleep(20000);

            // Wait for master's leave signal.
            masterLeaveSignal.WaitOne();

            /*
             * Set the master's leave signal so that other clients
             * could be informed.
             */
            masterLeaveSignal.Set();

            // Leave sometime for client to hold election.
            Thread.Sleep(10000);

            env.LogFlush();
            env.Close();
            Console.WriteLine("Client1: Leaving the replication");
        }
Ejemplo n.º 7
0
        public void TestRepMgrSite()
        {
            testName = "TestRepMgrSite";
            SetUpTest(true);

            string masterHome = testHome + "\\Master";

            Configuration.ClearDir(masterHome);

            string clientHome = testHome + "\\Client";

            Configuration.ClearDir(clientHome);

            ports.Clear();
            AvailablePorts portGen = new AvailablePorts();
            uint           mPort   = portGen.Current;

            portGen.MoveNext();
            uint cPort = portGen.Current;

            // Open environment with replication configuration.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create         = true;
            cfg.RunRecovery    = true;
            cfg.UseLocking     = true;
            cfg.UseLogging     = true;
            cfg.UseMPool       = true;
            cfg.UseReplication = true;
            cfg.FreeThreaded   = true;
            cfg.UseTxns        = true;
            cfg.EventNotify    = new EventNotifyDelegate(stuffHappened);

            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host         = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port         = mPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite    = true;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = true;
            cfg.RepSystemCfg.Priority = 100;

            DatabaseEnvironment mEnv = DatabaseEnvironment.Open(
                masterHome, cfg);

            mEnv.DeadlockResolution = DeadlockPolicy.DEFAULT;
            mEnv.RepMgrStartMaster(2);

            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port         = cPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false;
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = mPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            DatabaseEnvironment cEnv = DatabaseEnvironment.Open(
                clientHome, cfg);

            cEnv.RepMgrStartClient(2, false);

            /* Wait for client to start up */
            Thread.Sleep(2000);

            /*
             * Verify the client info could be achived by master's
             * remote site.
             */
            Assert.AreEqual(1, mEnv.RepMgrRemoteSites.Length);
            RepMgrSite rsite = mEnv.RepMgrRemoteSites[0];

            Assert.AreEqual("127.0.0.1", rsite.Address.Host);
            Assert.AreEqual(cPort, rsite.Address.Port);
            Assert.AreEqual(1, rsite.EId);
            Assert.AreEqual(true, rsite.isConnected);
            Assert.AreEqual(false, rsite.isPeer);

            DbSite site = mEnv.RepMgrSite("127.0.0.1", mPort);

            Assert.AreEqual("127.0.0.1", site.Address.Host);
            Assert.AreEqual(mPort, site.Address.Port);
            Assert.AreEqual(0, site.EId);
            Assert.AreEqual(true, site.GroupCreator);
            Assert.AreEqual(true, site.LocalSite);
            Assert.AreEqual(false, site.Helper);
            Assert.AreEqual(false, site.Legacy);
            Assert.AreEqual(false, site.Peer);
            site.Close();

            site = mEnv.RepMgrSite("127.0.0.1", cPort);
            Assert.AreEqual("127.0.0.1", site.Address.Host);
            Assert.AreEqual(cPort, site.Address.Port);
            Assert.AreEqual(1, site.EId);
            Assert.AreEqual(false, site.GroupCreator);
            Assert.AreEqual(false, site.LocalSite);
            Assert.AreEqual(false, site.Helper);
            Assert.AreEqual(false, site.Legacy);
            Assert.AreEqual(false, site.Peer);
            site.Remove();

            cEnv.Close();
            mEnv.Close();

            /*
             * Update the repmgr site, and verify it is updated in
             * unmanged memory.
             */
            rsite.Address = new ReplicationHostAddress(
                "192.168.1.1", 1000);
            rsite.EId         = 1024;
            rsite.isConnected = false;
            rsite.isPeer      = true;
            Assert.AreEqual("192.168.1.1", rsite.Address.Host);
            Assert.AreEqual(1000, rsite.Address.Port);
            Assert.AreEqual(1024, rsite.EId);
            Assert.AreEqual(false, rsite.isConnected);
            Assert.AreEqual(true, rsite.isPeer);
        }
Ejemplo n.º 8
0
        public void StableClient(string home, int clientIdx,
                                 uint localPort, uint priority,
                                 uint helperPort, uint peerPort,
                                 EventWaitHandle notifyHandle)
        {
            int timeout = 0;

            Configuration.ClearDir(home);

            Console.WriteLine(
                "Client{0}: Join the replication", clientIdx);

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create                   = true;
            cfg.FreeThreaded             = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize =
                new CacheInfo(0, 20485760, 1);
            cfg.RunRecovery    = true;
            cfg.TxnNoSync      = true;
            cfg.UseLocking     = true;
            cfg.UseMPool       = true;
            cfg.UseTxns        = true;
            cfg.UseReplication = true;
            cfg.UseLogging     = true;
            cfg.RepSystemCfg   = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(
                new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = localPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority = priority;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(
                new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = helperPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            cfg.RepSystemCfg.ElectionRetry   = 100;
            cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.NONE;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            try {
                // Start the client.
                Assert.AreEqual(clientIdx - 1, startUpDone);
                env.RepMgrStartClient(3, false);
                while (startUpDone < clientIdx)
                {
                    if (timeout > 10)
                    {
                        throw new TestException();
                    }
                    timeout++;
                    Thread.Sleep(1000);
                }

                ReplicationStats repStats =
                    env.ReplicationSystemStats();
                Assert.LessOrEqual(0, repStats.Elections);
                Assert.LessOrEqual(0,
                                   repStats.ElectionTiebreaker);
                Assert.LessOrEqual(0, repStats.ElectionTimeSec +
                                   repStats.ElectionTimeUSec);
                Assert.LessOrEqual(0, repStats.MasterChanges);
                Assert.LessOrEqual(0, repStats.NewSiteMessages);
                Assert.LessOrEqual(0,
                                   repStats.ReceivedLogRecords);
                Assert.LessOrEqual(0, repStats.ReceivedMessages);
                Assert.LessOrEqual(0, repStats.ReceivedPages);
                Assert.GreaterOrEqual(clientIdx + 1,
                                      repStats.RegisteredSitesNeeded);
                Assert.LessOrEqual(0, repStats.Sites);

                // Notify the next event.
                notifyHandle.Set();

                // Wait for master's leave.
                if (!clientsElectionSignal.WaitOne(50000))
                {
                    throw new TestException();
                }

                timeout = 0;
                while (!electionDone)
                {
                    if (timeout > 10)
                    {
                        throw new TestException();
                    }
                    timeout++;
                    Thread.Sleep(1000);
                }

                env.LogFlush();

                timeout = 0;
                // Start up done event happens on client.
                while (startUpDone < 2)
                {
                    if (timeout > 10)
                    {
                        throw new TestException();
                    }
                    timeout++;
                    Thread.Sleep(1000);
                }
                Thread.Sleep((int)priority * 100);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            } finally {
                env.Close();
                Console.WriteLine(
                    "Client{0}: Leaving the replication",
                    clientIdx);
            }
        }
Ejemplo n.º 9
0
        public void TestReplicationView()
        {
            testName = "TestReplicationView";
            SetUpTest(true);

            string masterHome = testHome + "\\Master";

            Configuration.ClearDir(masterHome);

            string clientHome1 = testHome + "\\Client1";

            Configuration.ClearDir(clientHome1);

            string clientHome2 = testHome + "\\Client2";

            Configuration.ClearDir(clientHome2);

            ports.Clear();
            AvailablePorts portGen = new AvailablePorts();
            uint           mPort   = portGen.Current;

            portGen.MoveNext();
            uint cPort1 = portGen.Current;

            portGen.MoveNext();
            uint cPort2 = portGen.Current;

            /* Open environment with replication configuration. */
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create         = true;
            cfg.RunRecovery    = true;
            cfg.UseLocking     = true;
            cfg.UseLogging     = true;
            cfg.UseMPool       = true;
            cfg.UseReplication = true;
            cfg.FreeThreaded   = true;
            cfg.UseTxns        = true;
            cfg.EventNotify    = new EventNotifyDelegate(stuffHappened);

            cfg.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host         = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port         = mPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite    = true;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = true;
            cfg.RepSystemCfg.Priority = 100;

            /* Start up the master site. */
            DatabaseEnvironment mEnv = DatabaseEnvironment.Open(
                masterHome, cfg);

            mEnv.DeadlockResolution = DeadlockPolicy.DEFAULT;
            mEnv.RepMgrStartMaster(2);

            /* Open the environment of the client 1 site. */
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port         = cPort1;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false;
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = mPort;
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            /* Set the site as a partial view. */
            cfg.RepSystemCfg.ReplicationView = repView;
            DatabaseEnvironment cEnv1 = DatabaseEnvironment.Open(
                clientHome1, cfg);

            /* Open the environment of the client 2 site. */
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port         = cPort2;
            cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false;
            cfg.RepSystemCfg.Priority = 10;
            /* Set the site as a full view. */
            cfg.RepSystemCfg.ReplicationView = null;
            DatabaseEnvironment cEnv2 = DatabaseEnvironment.Open(
                clientHome2, cfg);

            /*
             * Create two database files db1.db and db2.db
             * on the master.
             */
            BTreeDatabaseConfig btreeDBConfig =
                new BTreeDatabaseConfig();

            btreeDBConfig.Env        = mEnv;
            btreeDBConfig.Creation   = CreatePolicy.ALWAYS;
            btreeDBConfig.AutoCommit = true;
            BTreeDatabase db1 =
                BTreeDatabase.Open("db1.db", btreeDBConfig);
            BTreeDatabase db2 =
                BTreeDatabase.Open("db2.db", btreeDBConfig);

            db1.Close();
            db2.Close();

            /* Start up the client sites. */
            cEnv1.RepMgrStartClient(2, false);
            cEnv2.RepMgrStartClient(2, false);

            /* Wait for clients to start up */
            int i = 0;

            while (!cEnv1.ReplicationSystemStats().ClientStartupComplete)
            {
                if (i < 20)
                {
                    Thread.Sleep(1000);
                    i++;
                }
                else
                {
                    throw new TestException();
                }
            }
            i = 0;
            while (!cEnv2.ReplicationSystemStats().ClientStartupComplete)
            {
                if (i < 20)
                {
                    Thread.Sleep(1000);
                    i++;
                }
                else
                {
                    throw new TestException();
                }
            }

            /*
             * Verify that the file db2.db is replicated to the
             * client 2 (full view), but not to the client 1
             * (partial view), and the file db1.db is
             * replicated to both sites.
             */
            btreeDBConfig.Env      = cEnv1;
            btreeDBConfig.Creation = CreatePolicy.NEVER;
            db1 = BTreeDatabase.Open("db1.db", btreeDBConfig);
            try
            {
                db2 = BTreeDatabase.Open("db2.db", btreeDBConfig);
                throw new TestException();
            }
            catch (DatabaseException e) {
                Assert.AreEqual(0, String.Compare(
                                    "No such file or directory", e.Message));
            }
            db1.Close();
            btreeDBConfig.Env = cEnv2;
            db1 = BTreeDatabase.Open("db1.db", btreeDBConfig);
            db2 = BTreeDatabase.Open("db2.db", btreeDBConfig);
            db1.Close();
            db2.Close();

            /* Get the replication manager statistic. */
            RepMgrStats repMgrStats = mEnv.RepMgrSystemStats();

            Assert.AreEqual(1, repMgrStats.ParticipantSites);
            Assert.AreEqual(3, repMgrStats.TotalSites);
            Assert.AreEqual(2, repMgrStats.ViewSites);

            /*
             * Verify the master is not a view locally
             * or from remote site.
             */
            ReplicationStats repstats =
                mEnv.ReplicationSystemStats();

            Assert.AreEqual(false, repstats.View);
            RepMgrSite[] rsite = cEnv1.RepMgrRemoteSites;
            Assert.AreEqual(2, rsite.Length);
            for (i = 0; i < rsite.Length; i++)
            {
                if (rsite[i].Address.Port == mPort)
                {
                    break;
                }
            }
            Assert.Greater(rsite.Length, i);
            Assert.AreEqual(false, rsite[i].isView);

            /*
             * Verify the clients are views locally
             * and from remote site.
             */
            rsite = mEnv.RepMgrRemoteSites;
            Assert.AreEqual(2, rsite.Length);
            Assert.AreEqual(true, rsite[0].isView);
            Assert.AreEqual(true, rsite[1].isView);
            repstats = cEnv1.ReplicationSystemStats();
            Assert.AreEqual(true, repstats.View);
            repstats = cEnv2.ReplicationSystemStats();
            Assert.AreEqual(true, repstats.View);

            cEnv2.Close();
            cEnv1.Close();
            mEnv.Close();
        }