public void TestConsumeWithTxn()
        {
            testName = "TestConsumeWithTxn";
            SetUpTest(true);
            string queueDBFileName = testHome + "/" + testName + ".db";
            string queueDBName     = Path.GetFileName(queueDBFileName);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create   = true;
            envConfig.UseTxns  = true;
            envConfig.UseMPool = true;

            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);
            Transaction txn = env.BeginTransaction();

            QueueDatabaseConfig queueConfig =
                new QueueDatabaseConfig();

            queueConfig.Creation = CreatePolicy.ALWAYS;
            queueConfig.Env      = env;
            queueConfig.Length   = 1000;
            QueueDatabase queueDB = QueueDatabase.Open(
                queueDBName, queueConfig, txn);

            int           i = 1;
            DatabaseEntry data;
            DatabaseEntry getData = new DatabaseEntry();

            while (i <= 10)
            {
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes(i.ToString()));
                queueDB.Append(data, txn);
                if (i == 5)
                {
                    getData = data;
                }
                i++;
            }

            KeyValuePair <uint, DatabaseEntry> pair = queueDB.Consume(false, txn);

            queueDB.Close();
            txn.Commit();
            env.Close();

            Database db = Database.Open(queueDBFileName,
                                        new QueueDatabaseConfig());

            try
            {
                DatabaseEntry key =
                    new DatabaseEntry(BitConverter.GetBytes(pair.Key));
                db.Get(key);
            }
            catch (NotFoundException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                db.Close();
            }
        }
 private RecnoDatabase(DatabaseEnvironment env, uint flags)
     : base(env, flags)
 {
 }
Exemple #3
0
        private void Open()
        {
            Console.WriteLine("Opening environment and database");

            // Set up the environment.
            DatabaseEnvironmentConfig envCfg = new DatabaseEnvironmentConfig();

            envCfg.Create     = true;
            envCfg.UseMPool   = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseTxns    = true;

            // Allow multiple threads visit to the environment handle.
            envCfg.FreeThreaded = true;

            if (inMem)
            {
                envCfg.Private = true;
            }
            else
            {
                envCfg.RunRecovery = true;
            }

            /*
             * Indicate that we want db to internally perform
             * deadlock detection, aborting the transaction that
             * has performed the least amount of WriteData activity
             * in the event of a deadlock.
             */
            envCfg.LockSystemCfg = new LockingConfig();
            envCfg.LockSystemCfg.DeadlockResolution =
                DeadlockPolicy.MIN_WRITE;

            if (inMem)
            {
                // Specify in-memory logging.
                envCfg.LogSystemCfg          = new LogConfig();
                envCfg.LogSystemCfg.InMemory = true;

                /*
                 * Specify the size of the in-memory log buffer
                 * Must be large enough to handle the log data
                 * created by the largest transaction.
                 */
                envCfg.LogSystemCfg.BufferSize = 10 * 1024 * 1024;

                /*
                 * Specify the size of the in-memory cache,
                 * large enough to avoid paging to disk.
                 */
                envCfg.MPoolSystemCfg           = new MPoolConfig();
                envCfg.MPoolSystemCfg.CacheSize =
                    new CacheInfo(0, 10 * 1024 * 1024, 1);
            }

            // Set up the database.
            BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig();

            dbCfg.AutoCommit      = true;
            dbCfg.Creation        = CreatePolicy.IF_NEEDED;
            dbCfg.Duplicates      = DuplicatesPolicy.SORTED;
            dbCfg.FreeThreaded    = true;
            dbCfg.ReadUncommitted = true;

            /*
             * Open the environment. Any errors will be caught
             * by the caller.
             */
            env = DatabaseEnvironment.Open(home, envCfg);

            /*
             * Open the database. Do not provide a txn handle. This
             * Open is autocommitted because BTreeDatabaseConfig.AutoCommit
             * is true.
             */
            dbCfg.Env = env;
            db        = BTreeDatabase.Open(dbName, dbCfg);
        }
        public void OpenSecHashDBWithinTxn(string className,
                                           string funName, string home, string dbFileName,
                                           string dbSecFileName, bool ifDbName)
        {
            XmlElement xmlElem = Configuration.TestSetUp(
                className, funName);

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create     = true;
            envConfig.UseTxns    = true;
            envConfig.UseMPool   = true;
            envConfig.UseLogging = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            // Open a primary hash database.
            Transaction        openDBTxn = env.BeginTransaction();
            HashDatabaseConfig dbConfig  =
                new HashDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            HashDatabase db = HashDatabase.Open(
                dbFileName, dbConfig, openDBTxn);

            openDBTxn.Commit();

            // Open a secondary hash database.
            Transaction openSecTxn = env.BeginTransaction();
            SecondaryHashDatabaseConfig secDBConfig =
                new SecondaryHashDatabaseConfig(db,
                                                new SecondaryKeyGenDelegate(SecondaryKeyGen));

            SecondaryHashDatabaseConfigTest.Config(xmlElem,
                                                   ref secDBConfig, false);
            secDBConfig.HashFunction = null;
            secDBConfig.Env          = env;
            SecondaryHashDatabase secDB;

            if (ifDbName == false)
            {
                secDB = SecondaryHashDatabase.Open(
                    dbSecFileName, secDBConfig, openSecTxn);
            }
            else
            {
                secDB = SecondaryHashDatabase.Open(
                    dbSecFileName, "secondary", secDBConfig,
                    openSecTxn);
            }
            openSecTxn.Commit();

            // Confirm its flags configured in secDBConfig.
            Confirm(xmlElem, secDB, true);
            secDB.Close();

            // Open the existing secondary database.
            Transaction             secTxn    = env.BeginTransaction();
            SecondaryDatabaseConfig secConfig =
                new SecondaryDatabaseConfig(db,
                                            new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secConfig.Env = env;

            SecondaryDatabase secExDB;

            if (ifDbName == false)
            {
                secExDB = SecondaryHashDatabase.Open(
                    dbSecFileName, secConfig, secTxn);
            }
            else
            {
                secExDB = SecondaryHashDatabase.Open(
                    dbSecFileName, "secondary", secConfig,
                    secTxn);
            }
            secExDB.Close();
            secTxn.Commit();

            db.Close();
            env.Close();
        }
Exemple #5
0
        public void UnstableMaster()
        {
            string home = testHome + "/UnstableMaster";

            Configuration.ClearDir(home);

            // Open environment with replication configuration.
            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.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority        = 200;
            cfg.RepSystemCfg.ElectionRetry   = 10;
            cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.ALL;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            env.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Start as master site.
            env.RepMgrStartMaster(3);

            Console.WriteLine("Master: Finish initialization");

            // Notify clients to join.
            client1StartSignal.Set();
            client2StartSignal.Set();
            client3StartSignal.Set();

            // Wait for initialization of all clients.
            client1ReadySignal.WaitOne();
            client2ReadySignal.WaitOne();
            client3ReadySignal.WaitOne();

            foreach (RepMgrSite site in env.RepMgrRemoteSites)
            {
                Assert.AreEqual("127.0.0.1", site.Address.Host);
                Assert.IsTrue(ports.Contains(site.Address.Port));
            }

            // After all of them are ready, close the current master.
            Console.WriteLine("Master: Unexpected leave.");
            env.LogFlush();
            env.Close();
            masterLeaveSignal.Set();
        }
Exemple #6
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");
        }
        public void OpenSecDBWithinTxn(string home,
                                       string dbFileName, string dbSecFileName, bool ifDbName)
        {
            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create     = true;
            envConfig.UseTxns    = true;
            envConfig.UseMPool   = true;
            envConfig.UseLogging = true;
            envConfig.UseLocking = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            // Open a primary btree database.
            Transaction         openDBTxn = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig  =
                new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            BTreeDatabase db = BTreeDatabase.Open(
                dbFileName, dbConfig, openDBTxn);

            // Open a secondary btree database.
            SecondaryBTreeDatabaseConfig secDBConfig =
                new SecondaryBTreeDatabaseConfig(db,
                                                 new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secDBConfig.Env      = env;
            secDBConfig.Creation = CreatePolicy.IF_NEEDED;

            SecondaryBTreeDatabase secDB;

            if (ifDbName == false)
            {
                secDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, secDBConfig, openDBTxn);
            }
            else
            {
                secDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, "secondary", secDBConfig,
                    openDBTxn);
            }
            openDBTxn.Commit();
            secDB.Close();

            // Open the existing secondary database.
            Transaction             secTxn    = env.BeginTransaction();
            SecondaryDatabaseConfig secConfig =
                new SecondaryDatabaseConfig(db,
                                            new SecondaryKeyGenDelegate(SecondaryKeyGen));

            secConfig.Env = env;

            SecondaryDatabase secExDB;

            if (ifDbName == false)
            {
                secExDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, secConfig, secTxn);
            }
            else
            {
                secExDB = SecondaryBTreeDatabase.Open(
                    dbSecFileName, "secondary", secConfig,
                    secTxn);
            }
            secExDB.Close();
            secTxn.Commit();

            db.Close();
            env.Close();
        }
Exemple #8
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.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].Host   = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Port   = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true;
            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.LessOrEqual(0, 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();
        }
Exemple #9
0
        /*
         * Test the external file database with or without environment.
         * 1. Config and open the environment;
         * 2. Verify the environment external file configs;
         * 3. Config and open the database;
         * 4. Verify the database external file configs;
         * 5. Insert and verify some external file data by database methods;
         * 6. Verify the stats;
         * 7. Close all handles.
         * If "blobdbt" is true, set the data DatabaseEntry.ExternalFile as
         * true, otherwise make the data DatabaseEntry reach the external file
         * threshold in size.
         */
        void TestBlobHeapDatabase(uint env_threshold, string env_blobdir,
                                  uint db_threshold, string db_blobdir, bool blobdbt)
        {
            if (env_threshold == 0 && db_threshold == 0)
            {
                return;
            }

            string heapDBName =
                testHome + "/" + testName + ".db";

            Configuration.ClearDir(testHome);
            HeapDatabaseConfig cfg = new HeapDatabaseConfig();

            cfg.Creation = CreatePolicy.ALWAYS;
            string blrootdir = "__db_bl";

            // Open the environment and verify the external file configs.
            if (env_threshold > 0)
            {
                DatabaseEnvironmentConfig envConfig =
                    new DatabaseEnvironmentConfig();
                envConfig.AutoCommit            = true;
                envConfig.Create                = true;
                envConfig.UseMPool              = true;
                envConfig.UseLogging            = true;
                envConfig.UseTxns               = true;
                envConfig.UseLocking            = true;
                envConfig.ExternalFileThreshold = env_threshold;
                if (env_blobdir != null)
                {
                    envConfig.ExternalFileDir = env_blobdir;
                    blrootdir = env_blobdir;
                }
                DatabaseEnvironment env = DatabaseEnvironment.Open(
                    testHome, envConfig);
                if (env_blobdir == null)
                {
                    Assert.IsNull(env.ExternalFileDir);
                }
                else
                {
                    Assert.AreEqual(0,
                                    env.ExternalFileDir.CompareTo(env_blobdir));
                }
                Assert.AreEqual(env_threshold, env.ExternalFileThreshold);
                cfg.Env    = env;
                heapDBName = testName + ".db";
            }

            // Open the database and verify the external file configs.
            if (db_threshold > 0)
            {
                cfg.ExternalFileThreshold = db_threshold;
            }
            if (db_blobdir != null)
            {
                cfg.ExternalFileDir = db_blobdir;

                /*
                 * The external file directory setting in the database
                 * is effective only when it is opened without
                 * an environment.
                 */
                if (cfg.Env == null)
                {
                    blrootdir = db_blobdir;
                }
            }

            HeapDatabase db = HeapDatabase.Open(heapDBName, cfg);

            Assert.AreEqual(
                db_threshold > 0 ? db_threshold : env_threshold,
                db.ExternalFileThreshold);
            if (db_blobdir == null && cfg.Env == null)
            {
                Assert.IsNull(db.ExternalFileDir);
            }
            else
            {
                Assert.AreEqual(0, db.ExternalFileDir.CompareTo(blrootdir));
            }

            // Insert and verify some external file data by database
            // methods.
            string[]      records = { "a", "b", "c", "d", "e", "f", "g", "h",
                                      "i",      "j", "k", "l", "m", "n", "o", "p","q","r", "s",
                                      "t",      "u", "v", "w", "x", "y", "z" };
            DatabaseEntry kdbt = new DatabaseEntry();
            DatabaseEntry ddbt = new DatabaseEntry();

            byte[] ddata;
            string str;
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            ddbt.ExternalFile = blobdbt;
            Assert.AreEqual(blobdbt, ddbt.ExternalFile);
            for (int i = 0; i < records.Length; i++)
            {
                str = records[i];
                if (!blobdbt)
                {
                    for (int j = 0; j < db_threshold; j++)
                    {
                        str = str + records[i];
                    }
                }
                ddata     = Encoding.ASCII.GetBytes(str);
                ddbt.Data = ddata;
                kdbt      = new DatabaseEntry((db.Append(ddbt)).toArray());
                try
                {
                    pair = db.Get(kdbt);
                }
                catch (DatabaseException)
                {
                    db.Close();
                    if (cfg.Env != null)
                    {
                        cfg.Env.Close();
                    }
                    throw new TestException();
                }
                Assert.AreEqual(ddata, pair.Value.Data);
            }

            /*
             * Verify the external file files are created in the expected
             * location.
             * This part of test is disabled since BTreeDatabase.BlobSubDir
             * is not exposed to users.
             */
            //if (cfg.Env != null)
            //	blrootdir = testHome + "/" + blrootdir;
            //string blobdir = blrootdir + "/" + db.BlobSubDir;
            //Assert.AreEqual(records.Length,
            //    Directory.GetFiles(blobdir, "__db.bl*").Length);
            //Assert.AreEqual(1,
            //    Directory.GetFiles(blobdir, "__db_blob_meta.db").Length);

            // Verify the stats.
            HeapStats st = db.Stats();

            Assert.AreEqual(records.Length, st.nExternalFiles);

            // Close all handles.
            db.Close();
            if (cfg.Env != null)
            {
                cfg.Env.Close();
            }

            /*
             * Remove the default external file directory
             * when it is not under the test home.
             */
            if (db_blobdir == null && cfg.Env == null)
            {
                Directory.Delete("__db_bl", true);
            }
        }
Exemple #10
0
        public void TestExists()
        {
            testName = "TestExists";
            SetUpTest(true);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create     = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool   = true;
            envConfig.UseTxns    = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            HeapDatabase db;

            try {
                Transaction openTxn = env.BeginTransaction();
                try {
                    HeapDatabaseConfig heapConfig =
                        new HeapDatabaseConfig();
                    heapConfig.Creation = CreatePolicy.IF_NEEDED;
                    heapConfig.Env      = env;
                    db = HeapDatabase.Open(testName + ".db",
                                           heapConfig, openTxn);
                    openTxn.Commit();
                } catch (DatabaseException e) {
                    openTxn.Abort();
                    throw e;
                }

                Transaction  cursorTxn = env.BeginTransaction();
                Cursor       cursor;
                HeapRecordId rid;
                try {
                    /*
                     * Put a record into heap database with
                     * cursor and abort the operation.
                     */
                    rid = db.Append(new DatabaseEntry(
                                        ASCIIEncoding.ASCII.GetBytes("foo")), cursorTxn);
                    cursor = db.Cursor(cursorTxn);
                    KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
                    pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                        new DatabaseEntry(rid.toArray()),
                        new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("bar")));
                    cursor.Add(pair);
                    cursor.Close();
                    Assert.IsTrue(db.Exists(
                                      new DatabaseEntry(rid.toArray()), cursorTxn));
                    cursorTxn.Abort();
                } catch (DatabaseException e) {
                    cursorTxn.Abort();
                    db.Close();
                    throw e;
                }

                Transaction delTxn = env.BeginTransaction();
                try {
                    /*
                     * The put operation is aborted in the heap
                     * database so querying if the record still exists
                     * throws KeyEmptyException.
                     */
                    Assert.IsFalse(db.Exists(
                                       new DatabaseEntry(rid.toArray()), delTxn));
                    delTxn.Commit();
                } catch (DatabaseException e) {
                    delTxn.Abort();
                    throw e;
                } finally {
                    db.Close();
                }
            } finally {
                env.Close();
            }
        }
Exemple #11
0
 internal Mutex(DatabaseEnvironment owner, uint mutexValue)
 {
     env = owner;
     val = mutexValue;
 }
Exemple #12
0
        /* Initialize environment and database (s) */
        public void InitDbs()
        {
            /* Open transactional environment */
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                 = true;
            envConfig.UseMPool               = true;
            envConfig.UseLocking             = true;
            envConfig.UseLogging             = true;
            envConfig.UseTxns                = true;
            envConfig.LockSystemCfg          = new LockingConfig();
            envConfig.LockSystemCfg.MaxLocks =
                (this.dups == 0) ?
                (uint)this.num :
                (uint)(this.num * this.dups);
            envConfig.LockSystemCfg.MaxObjects =
                envConfig.LockSystemCfg.MaxLocks;
            if (this.cachesize != 0)
            {
                envConfig.MPoolSystemCfg           = new MPoolConfig();
                envConfig.MPoolSystemCfg.CacheSize =
                    new CacheInfo(0, this.cachesize, 1);
            }

            try {
                env = DatabaseEnvironment.Open(home, envConfig);
            } catch (Exception e) {
                Console.WriteLine(e.StackTrace);
                System.Environment.Exit(1);
            }

            Transaction txn = env.BeginTransaction();

            try {
                /* Open primary db in transaction */
                BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                dbConfig.Env      = env;
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                if (this.pagesize != 0)
                {
                    dbConfig.PageSize = this.pagesize;
                }
                if (this.dups != 0)
                {
                    dbConfig.Duplicates = DuplicatesPolicy.UNSORTED;
                }
                pdb = BTreeDatabase.Open(dbFileName, pDbName, dbConfig, txn);

                /* Open secondary db in transaction */
                if (this.secondary)
                {
                    SecondaryBTreeDatabaseConfig sdbConfig =
                        new SecondaryBTreeDatabaseConfig(
                            pdb, new SecondaryKeyGenDelegate(SecondaryKeyGen));
                    sdbConfig.Creation = CreatePolicy.IF_NEEDED;
                    if (this.pagesize != 0)
                    {
                        sdbConfig.PageSize = this.pagesize;
                    }
                    sdbConfig.Duplicates = DuplicatesPolicy.SORTED;
                    sdbConfig.Env        = env;
                    sdb = SecondaryBTreeDatabase.Open(
                        dbFileName, sDbName, sdbConfig, txn);
                }

                txn.Commit();
            } catch (DatabaseException e1) {
                txn.Abort();
                throw e1;
            } catch (FileNotFoundException e2) {
                txn.Abort();
                throw e2;
            }
        }
Exemple #13
0
        public void TestLockStats()
        {
            testName = "TestLockStats";
            SetUpTest(true);

            // Configure locking subsystem.
            LockingConfig lkConfig = new LockingConfig();

            lkConfig.MaxLockers         = 60;
            lkConfig.MaxLocks           = 50;
            lkConfig.MaxObjects         = 70;
            lkConfig.Partitions         = 20;
            lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT;

            // Configure and open environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create                   = true;
            envConfig.FreeThreaded             = true;
            envConfig.LockSystemCfg            = lkConfig;
            envConfig.LockTimeout              = 1000;
            envConfig.MPoolSystemCfg           = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1);
            envConfig.NoLocking                = false;
            envConfig.TxnTimeout               = 2000;
            envConfig.UseLocking               = true;
            envConfig.UseMPool                 = true;
            envConfig.UseTxns                  = true;
            DatabaseEnvironment env =
                DatabaseEnvironment.Open(testHome, envConfig);

            // Get and confirm locking subsystem statistics.
            LockStats stats = env.LockingSystemStats();

            env.PrintLockingSystemStats(true, true);
            Assert.AreEqual(0, stats.AllocatedLockers);
            Assert.AreNotEqual(0, stats.AllocatedLocks);
            Assert.AreNotEqual(0, stats.AllocatedObjects);
            Assert.AreEqual(0, stats.InitLockers);
            Assert.AreNotEqual(0, stats.InitLocks);
            Assert.AreNotEqual(0, stats.InitObjects);
            Assert.AreEqual(0, stats.LastAllocatedLockerID);
            Assert.AreEqual(0, stats.LockConflictsNoWait);
            Assert.AreEqual(0, stats.LockConflictsWait);
            Assert.AreEqual(0, stats.LockDeadlocks);
            Assert.AreEqual(0, stats.LockDowngrades);
            Assert.AreEqual(0, stats.LockerNoWait);
            Assert.AreEqual(0, stats.Lockers);
            Assert.AreEqual(0, stats.LockerWait);
            Assert.AreEqual(9, stats.LockModes);
            Assert.AreEqual(0, stats.LockPuts);
            Assert.AreEqual(0, stats.LockRequests);
            Assert.AreEqual(0, stats.Locks);
            Assert.AreEqual(0, stats.LockSteals);
            Assert.AreEqual(1000, stats.LockTimeoutLength);
            Assert.AreEqual(0, stats.LockTimeouts);
            Assert.AreEqual(0, stats.LockUpgrades);
            Assert.AreEqual(0, stats.MaxBucketLength);
            Assert.AreEqual(0, stats.MaxLockers);
            Assert.AreEqual(60, stats.MaxLockersInTable);
            Assert.AreEqual(0, stats.MaxLocks);
            Assert.AreEqual(0, stats.MaxLocksInBucket);
            Assert.AreEqual(50, stats.MaxLocksInTable);
            Assert.AreEqual(0, stats.MaxLockSteals);
            Assert.AreEqual(0, stats.MaxObjects);
            Assert.AreEqual(0, stats.MaxObjectsInBucket);
            Assert.AreEqual(70, stats.MaxObjectsInTable);
            Assert.AreEqual(0, stats.MaxObjectSteals);
            Assert.AreEqual(0, stats.MaxPartitionLockNoWait);
            Assert.AreEqual(0, stats.MaxPartitionLockWait);
            Assert.AreNotEqual(0, stats.MaxUnusedID);
            Assert.AreEqual(20, stats.nPartitions);
            Assert.AreEqual(0, stats.ObjectNoWait);
            Assert.AreEqual(0, stats.Objects);
            Assert.AreEqual(0, stats.ObjectSteals);
            Assert.AreEqual(0, stats.ObjectWait);
            Assert.LessOrEqual(0, stats.PartitionLockNoWait);
            Assert.AreEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.AreNotEqual(0, stats.RegionSize);
            Assert.AreEqual(0, stats.RegionWait);
            Assert.AreNotEqual(0, stats.TableSize);
            Assert.AreEqual(2000, stats.TxnTimeoutLength);
            Assert.AreEqual(0, stats.TxnTimeouts);

            env.PrintLockingSystemStats();

            Transaction         txn      = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation     = CreatePolicy.IF_NEEDED;
            dbConfig.Env          = env;
            dbConfig.FreeThreaded = true;
            BTreeDatabase db = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);

            txn.Commit();

            testLockStatsEnv = env;
            testLockStatsDb  = db;

            Thread thread1 = new Thread(new ThreadStart(Write));
            Thread thread2 = new Thread(new ThreadStart(Read));

            thread1.Start();
            Thread.Sleep(10);
            thread2.Start();
            thread1.Join();
            thread2.Join();

            env.PrintLockingSystemStats();
            stats = env.LockingSystemStats();
            Assert.Less(0, stats.LastAllocatedLockerID);
            Assert.Less(0, stats.LockConflictsNoWait);
            Assert.LessOrEqual(0, stats.LockConflictsWait);
            Assert.LessOrEqual(0, stats.LockDeadlocks);
            Assert.Less(0, stats.LockDowngrades);
            Assert.LessOrEqual(0, stats.LockerNoWait);
            Assert.Less(0, stats.Lockers);
            Assert.LessOrEqual(0, stats.LockerWait);
            Assert.Less(0, stats.LockPuts);
            Assert.Less(0, stats.LockRequests);
            Assert.Less(0, stats.Locks);
            Assert.LessOrEqual(0, stats.LockSteals);
            Assert.LessOrEqual(0, stats.LockTimeouts);
            Assert.LessOrEqual(0, stats.LockUpgrades);
            Assert.Less(0, stats.MaxBucketLength);
            Assert.Less(0, stats.MaxLockers);
            Assert.Less(0, stats.MaxLocks);
            Assert.Less(0, stats.MaxLocksInBucket);
            Assert.LessOrEqual(0, stats.MaxLockSteals);
            Assert.Less(0, stats.MaxObjects);
            Assert.Less(0, stats.MaxObjectsInBucket);
            Assert.LessOrEqual(0, stats.MaxObjectSteals);
            Assert.LessOrEqual(0, stats.MaxPartitionLockNoWait);
            Assert.LessOrEqual(0, stats.MaxPartitionLockWait);
            Assert.Less(0, stats.MaxUnusedID);
            Assert.Less(0, stats.ObjectNoWait);
            Assert.Less(0, stats.Objects);
            Assert.LessOrEqual(0, stats.ObjectSteals);
            Assert.LessOrEqual(0, stats.ObjectWait);
            Assert.Less(0, stats.PartitionLockNoWait);
            Assert.LessOrEqual(0, stats.PartitionLockWait);
            Assert.Less(0, stats.RegionNoWait);
            Assert.LessOrEqual(0, stats.RegionWait);
            Assert.Less(0, stats.TxnTimeouts);

            db.Close();
            env.Close();
        }
Exemple #14
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", ports[1]);
            cfg.RepSystemCfg.Priority = 10;
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", ports[0]), false);
            cfg.RepSystemCfg.AddRemoteSite(
                new ReplicationHostAddress("127.0.0.1", ports[3]), 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 == 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();

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

            env.LogFlush();
            env.Close();
            Console.WriteLine("Client1: Leaving the replication");
        }
Exemple #15
0
        static void Main(string[] args)
        {
            BTreeDatabase             btreeDB;
            BTreeDatabaseConfig       btreeDBConfig;
            DatabaseEnvironment       env;
            DatabaseEnvironmentConfig envConfig;
            Sequence       seq;
            SequenceConfig seqConfig;
            string         buff;
            string         dbFileName;
            string         home;
            string         progName;

            /*
             * excs_sequence 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;
            }

            progName = "excs_sequence";
            try {
                home       = args[0];
                dbFileName = args[1];
            } catch {
                Usage();
                return;
            };

            /* Optiionally remove the existing database file. */
            if (File.Exists(dbFileName))
            {
                while (true)
                {
                    Console.Write
                        ("File already exists, delete or not (y/n)?");
                    buff = Console.ReadLine();
                    if (buff == "y" || buff == "n")
                    {
                        break;
                    }
                }

                if (buff == "y")
                {
                    File.Delete(dbFileName);
                    Console.WriteLine("The existing {0} is deleted",
                                      dbFileName);
                }
            }

            /* Configure and open environment. */
            envConfig            = new DatabaseEnvironmentConfig();
            envConfig.Create     = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool   = true;
            envConfig.UseTxns    = true;
            try {
                env = DatabaseEnvironment.Open(home, envConfig);
            } catch (Exception e) {
                Console.WriteLine("{0}:{1}\n{2}",
                                  e.Source, e.Message, e.StackTrace);
                return;
            }

            /* Configure and open sequence's database. */
            btreeDBConfig             = new BTreeDatabaseConfig();
            btreeDBConfig.AutoCommit  = true;
            btreeDBConfig.Creation    = CreatePolicy.IF_NEEDED;
            btreeDBConfig.ErrorPrefix = progName;
            btreeDBConfig.Env         = env;
            try {
                btreeDB = BTreeDatabase.Open(dbFileName,
                                             btreeDBConfig);
            } catch (Exception e) {
                Console.WriteLine("{0}:{1}\n{2}",
                                  e.Source, e.Message, e.StackTrace);
                return;
            }

            /* Configure and initialize sequence. */
            seqConfig = new SequenceConfig();
            seqConfig.BackingDatabase = btreeDB;
            seqConfig.Creation        = CreatePolicy.IF_NEEDED;
            seqConfig.Increment       = true;
            seqConfig.InitialValue    = Int64.MaxValue;
            seqConfig.key             = new DatabaseEntry();
            seqConfig.SetRange(Int64.MinValue, Int64.MaxValue);
            seqConfig.Wrap = true;
            DbtFromString(seqConfig.key, "excs_sequence");
            seq = new Sequence(seqConfig);

            /* Get from sequence. */
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("{0}", seq.Get(2));
                Console.ReadLine();
            }

            Console.WriteLine("Sequence Name: {0}",
                              seq.BackingDatabase.FileName);
            Console.WriteLine("Sequence Key: {0}",
                              StrFromDBT(seq.Key));
            Console.WriteLine("{0}->{1}", seq.Min, seq.Max);
            Console.WriteLine(seq.Wrap ? "wrap" : "not wrap");
            Console.WriteLine(seq.Increment ? "increase" :
                              "decrease");

            /* Close sequence, database and environment. */
            seq.Close();
            btreeDB.Close();
            env.Close();
        }
Exemple #16
0
        public void Master()
        {
            string home   = testHome + "/Master";
            string dbName = "rep.db";

            Configuration.ClearDir(home);

            /*
             * Configure and open environment with replication
             * application.
             */
            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.RepSystemCfg = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[0];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority        = 100;
            cfg.RepSystemCfg.BulkTransfer    = true;
            cfg.RepSystemCfg.AckTimeout      = 2000;
            cfg.RepSystemCfg.BulkTransfer    = true;
            cfg.RepSystemCfg.CheckpointDelay = 1500;
            cfg.RepSystemCfg.Clockskew(102, 100);
            cfg.RepSystemCfg.ConnectionRetry     = 10;
            cfg.RepSystemCfg.DelayClientSync     = false;
            cfg.RepSystemCfg.ElectionRetry       = 5;
            cfg.RepSystemCfg.ElectionTimeout     = 3000;
            cfg.RepSystemCfg.FullElectionTimeout = 5000;
            cfg.RepSystemCfg.HeartbeatMonitor    = 100;
            cfg.RepSystemCfg.HeartbeatSend       = 10;
            cfg.RepSystemCfg.LeaseTimeout        = 1300;
            cfg.RepSystemCfg.AutoInit            = true;
            cfg.RepSystemCfg.NoBlocking          = false;
            cfg.RepSystemCfg.RepMgrAckPolicy     =
                AckPolicy.ALL_PEERS;
            cfg.RepSystemCfg.RetransmissionRequest(10, 100);
            cfg.RepSystemCfg.Strict2Site     = true;
            cfg.RepSystemCfg.UseMasterLeases = false;
            cfg.EventNotify = new EventNotifyDelegate(stuffHappened);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, cfg);

            // Get initial replication stats.
            ReplicationStats repStats = env.ReplicationSystemStats();

            env.PrintReplicationSystemStats();
            Assert.AreEqual(100, repStats.EnvPriority);
            Assert.AreEqual(1,
                            repStats.CurrentElectionGenerationNumber);
            Assert.AreEqual(0, repStats.CurrentGenerationNumber);
            Assert.AreEqual(0, repStats.AppliedTransactions);
            Assert.AreEqual(0, repStats.ElectionDataGeneration);

            // Start a master site with replication manager.
            env.RepMgrStartMaster(3);

            // Open a btree database and write some data.
            Transaction         txn      = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            dbConfig.PageSize = 512;
            BTreeDatabase db = BTreeDatabase.Open(dbName,
                                                  dbConfig, txn);

            txn.Commit();
            txn = env.BeginTransaction();
            for (int i = 0; i < 5; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(BitConverter.GetBytes(i)), txn);
            }
            txn.Commit();

            Console.WriteLine(
                "Master: Finished initialization and data#1.");

            // Client site could enter now.
            clientStartSignal.Set();
            Console.WriteLine(
                "Master: Wait for Client to join and get #1.");

            Console.WriteLine("...");

            // Put some new data into master site.
            txn = env.BeginTransaction();
            for (int i = 10; i < 15; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(BitConverter.GetBytes(i)),
                       txn);
            }
            txn.Commit();
            Console.WriteLine(
                "Master: Write something new, data #2.");
            Console.WriteLine("Master: Wait for client to read #2...");

            // Get the stats.
            repStats = env.ReplicationSystemStats(true);
            env.PrintReplicationSystemStats();
            Assert.LessOrEqual(0, repStats.AppliedTransactions);
            Assert.LessOrEqual(0, repStats.AwaitedLSN.LogFileNumber);
            Assert.LessOrEqual(0, repStats.AwaitedLSN.Offset);
            Assert.LessOrEqual(0, repStats.AwaitedPage);
            Assert.LessOrEqual(0, repStats.BadGenerationMessages);
            Assert.LessOrEqual(0, repStats.BulkBufferFills);
            Assert.LessOrEqual(0, repStats.BulkBufferOverflows);
            Assert.LessOrEqual(0, repStats.BulkBufferTransfers);
            Assert.LessOrEqual(0, repStats.BulkRecordsStored);
            Assert.LessOrEqual(0, repStats.ClientServiceRequests);
            Assert.LessOrEqual(0, repStats.ClientServiceRequestsMissing);
            Assert.IsInstanceOf(typeof(bool), repStats.ClientStartupComplete);
            Assert.AreEqual(2, repStats.CurrentElectionGenerationNumber);
            Assert.AreEqual(1, repStats.CurrentGenerationNumber);
            Assert.LessOrEqual(0, repStats.CurrentQueuedLogRecords);
            Assert.LessOrEqual(0, repStats.CurrentWinner);
            Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.LogFileNumber);
            Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.Offset);
            Assert.LessOrEqual(0, repStats.DuplicateLogRecords);
            Assert.LessOrEqual(0, repStats.DuplicatePages);
            Assert.LessOrEqual(0, repStats.DupMasters);
            Assert.LessOrEqual(0, repStats.ElectionGenerationNumber);
            Assert.LessOrEqual(0, repStats.ElectionPriority);
            Assert.LessOrEqual(0, repStats.Elections);
            Assert.LessOrEqual(0, repStats.ElectionStatus);
            Assert.LessOrEqual(0, repStats.ElectionsWon);
            Assert.LessOrEqual(0, repStats.ElectionTiebreaker);
            Assert.LessOrEqual(0, repStats.ElectionTimeSec);
            Assert.LessOrEqual(0, repStats.ElectionTimeUSec);
            Assert.AreEqual(repStats.EnvID, repStats.MasterEnvID);
            Assert.LessOrEqual(0, repStats.EnvPriority);
            Assert.LessOrEqual(0, repStats.FailedMessageSends);
            Assert.LessOrEqual(0, repStats.ForcedRerequests);
            Assert.LessOrEqual(0, repStats.IgnoredMessages);
            Assert.LessOrEqual(0, repStats.MasterChanges);
            Assert.LessOrEqual(0, repStats.MasterEnvID);
            Assert.LessOrEqual(0, repStats.MaxLeaseSec);
            Assert.LessOrEqual(0, repStats.MaxLeaseUSec);
            Assert.LessOrEqual(0, repStats.MaxPermanentLSN.Offset);
            Assert.LessOrEqual(0, repStats.MaxQueuedLogRecords);
            Assert.LessOrEqual(0, repStats.MessagesSent);
            Assert.LessOrEqual(0, repStats.MissedLogRecords);
            Assert.LessOrEqual(0, repStats.MissedPages);
            Assert.LessOrEqual(0, repStats.NewSiteMessages);
            Assert.LessOrEqual(repStats.MaxPermanentLSN.LogFileNumber,
                               repStats.NextLSN.LogFileNumber);
            if (repStats.MaxPermanentLSN.LogFileNumber ==
                repStats.NextLSN.LogFileNumber)
            {
                Assert.Less(repStats.MaxPermanentLSN.Offset,
                            repStats.NextLSN.Offset);
            }
            Assert.LessOrEqual(0, repStats.NextPage);
            Assert.LessOrEqual(0, repStats.Outdated);
            Assert.LessOrEqual(0, repStats.QueuedLogRecords);
            Assert.LessOrEqual(0, repStats.ReceivedLogRecords);
            Assert.LessOrEqual(0, repStats.ReceivedMessages);
            Assert.LessOrEqual(0, repStats.ReceivedPages);
            Assert.LessOrEqual(0, repStats.RegisteredSites);
            Assert.LessOrEqual(0, repStats.RegisteredSitesNeeded);
            Assert.LessOrEqual(0, repStats.Sites);
            Assert.LessOrEqual(0, repStats.StartSyncMessagesDelayed);
            Assert.AreEqual(2, repStats.Status);
            Assert.LessOrEqual(0, repStats.Throttled);
            Assert.LessOrEqual(0, repStats.Votes);

            // Get replication manager statistics.
            RepMgrStats repMgrStats = env.RepMgrSystemStats(true);

            Assert.LessOrEqual(0, repMgrStats.DroppedConnections);
            Assert.LessOrEqual(0, repMgrStats.DroppedMessages);
            Assert.LessOrEqual(0, repMgrStats.FailedConnections);
            Assert.LessOrEqual(0, repMgrStats.FailedMessages);
            Assert.LessOrEqual(0, repMgrStats.QueuedMessages);

            // Print them out.
            env.PrintRepMgrSystemStats();

            // Wait until client has finished reading.
            masterCloseSignal.WaitOne();
            Console.WriteLine("Master: Leave as well.");

            // Close all.
            db.Close(false);
            env.LogFlush();
            env.Close();
        }
Exemple #17
0
        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();
            }
        }
Exemple #18
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);
        }
Exemple #19
0
        public void TestPrepare()
        {
            testName = "TestPrepare";
            testHome = testFixtureHome + "/" + testName;

            DatabaseEnvironment env;

            byte[] gid;

            Configuration.ClearDir(testHome);

            /*
             * Open an environment and begin a transaction
             * called "transaction". Within the transacion, open a
             * database, write a record and close it. Then prepare
             * the transaction and panic the environment.
             */
            PanicPreparedTxn(testHome, testName, out env, out gid);

            /*
             * Recover the environment.     Log and db files are not
             * destoyed so run normal recovery. Recovery should
             * use DB_CREATE and DB_INIT_TXN flags when
             * opening the environment.
             */
            DatabaseEnvironmentConfig envConfig
                = new DatabaseEnvironmentConfig();

            envConfig.RunRecovery = true;
            envConfig.Create      = true;
            envConfig.UseTxns     = true;
            envConfig.UseMPool    = true;
            env = DatabaseEnvironment.Open(testHome, envConfig);

            // Reopen the database.
            BTreeDatabase db;

            OpenBtreeDBInEnv(testName + ".db", env, out db,
                             false, null);

            /*
             * Confirm that record("key", "data") exists in the
             * database.
             */
            DatabaseEntry key, data;

            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            try
            {
                db.GetBoth(key, data);
            }
            catch (DatabaseException)
            {
                throw new TestException();
            }
            finally
            {
                db.Close();
                env.Close();
            }
        }
Exemple #20
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 = new ReplicationConfig();
            cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig());
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Host      = "127.0.0.1";
            cfg.RepSystemCfg.RepmgrSitesConfig[0].Port      = ports[2];
            cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true;
            cfg.RepSystemCfg.Priority = 20;
            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.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");
        }
Exemple #21
0
 private HeapDatabase(DatabaseEnvironment env, uint flags)
     : base(env, flags)
 {
 }
Exemple #22
0
 private BTreeDatabase(DatabaseEnvironment env, uint flags)
     : base(env, flags)
 {
 }
Exemple #23
0
 internal SecondaryRecnoDatabase(DatabaseEnvironment env, uint flags)
     : base(env, flags)
 {
 }
 /// <summary>
 /// Protected construtor
 /// </summary>
 /// <param name="env">The environment in which to open the DB</param>
 /// <param name="flags">Flags to pass to DB->create</param>
 protected SecondaryDatabase(DatabaseEnvironment env, uint flags)
     : base(env, flags)
 {
 }
Exemple #25
0
        public void TestLoggingSystemStats()
        {
            testName = "TestLoggingSystemStats";
            SetUpTest(true);
            string logDir = "./";

            Directory.CreateDirectory(testHome + "/" + logDir);

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create                   = true;
            cfg.UseTxns                  = true;
            cfg.AutoCommit               = true;
            cfg.UseLocking               = true;
            cfg.UseMPool                 = true;
            cfg.UseLogging               = true;
            cfg.MPoolSystemCfg           = new MPoolConfig();
            cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1);

            cfg.LogSystemCfg                        = new LogConfig();
            cfg.LogSystemCfg.AutoRemove             = false;
            cfg.LogSystemCfg.BufferSize             = 10240;
            cfg.LogSystemCfg.Dir                    = logDir;
            cfg.LogSystemCfg.FileMode               = 755;
            cfg.LogSystemCfg.ForceSync              = true;
            cfg.LogSystemCfg.InMemory               = false;
            cfg.LogSystemCfg.LogExternalFileContent = false;
            cfg.LogSystemCfg.MaxFileSize            = 1048576;
            cfg.LogSystemCfg.NoBuffer               = false;
            cfg.LogSystemCfg.NoSync                 = true;
            cfg.LogSystemCfg.RegionSize             = 204800;
            cfg.LogSystemCfg.ZeroOnCreate           = true;

            DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);

            LogStats stats = env.LoggingSystemStats();

            env.Msgfile = testHome + "/" + testName + ".log";
            env.PrintLoggingSystemStats();
            Assert.AreEqual(10240, stats.BufferSize);
            Assert.AreEqual(1, stats.CurrentFile);
            Assert.AreNotEqual(0, stats.CurrentOffset);
            Assert.AreEqual(0, stats.FileId);
            Assert.AreEqual(1048576, stats.FileSize);
            Assert.AreEqual(0, stats.InitFileId);
            Assert.AreNotEqual(0, stats.MagicNumber);
            Assert.AreEqual(0, stats.MaxFileId);
            Assert.AreNotEqual(0, stats.PermissionsMode);
            Assert.AreEqual(1, stats.Records);
            Assert.AreNotEqual(0, stats.RegionLockNoWait);
            Assert.LessOrEqual(204800, stats.RegionSize);
            Assert.AreNotEqual(0, stats.Version);

            Transaction         openTxn  = env.BeginTransaction();
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();

            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env      = env;
            BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);

            openTxn.Commit();

            Transaction writeTxn = env.BeginTransaction();

            byte[] byteArr = new byte[1024];
            for (int i = 0; i < 1000; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                       new DatabaseEntry(byteArr), writeTxn);
            }
            writeTxn.Commit();

            stats = env.LoggingSystemStats();
            Assert.AreNotEqual(0, stats.Bytes);
            Assert.AreNotEqual(0, stats.BytesSinceCheckpoint);
            Assert.AreNotEqual(0, stats.DiskFileNumber);
            Assert.AreNotEqual(0, stats.DiskOffset);
            Assert.AreNotEqual(0, stats.MaxCommitsPerFlush);
            Assert.AreNotEqual(0, stats.MBytes);
            Assert.AreNotEqual(0, stats.MBytesSinceCheckpoint);
            Assert.AreNotEqual(0, stats.MinCommitsPerFlush);
            Assert.AreNotEqual(0, stats.OverflowWrites);
            Assert.AreEqual(0, stats.Syncs);
            Assert.AreNotEqual(0, stats.Writes);
            Assert.AreEqual(0, stats.Reads);
            Assert.AreEqual(0, stats.RegionLockWait);

            stats = env.LoggingSystemStats(true);
            stats = env.LoggingSystemStats();
            Assert.AreEqual(0, stats.Bytes);
            Assert.AreEqual(0, stats.BytesSinceCheckpoint);
            Assert.AreEqual(0, stats.MaxCommitsPerFlush);
            Assert.AreEqual(0, stats.MBytes);
            Assert.AreEqual(0, stats.MBytesSinceCheckpoint);
            Assert.AreEqual(0, stats.MinCommitsPerFlush);
            Assert.AreEqual(0, stats.OverflowWrites);
            Assert.AreEqual(0, stats.Syncs);
            Assert.AreEqual(0, stats.Writes);
            Assert.AreEqual(0, stats.Reads);

            env.PrintLoggingSystemStats(true, true);

            db.Close();
            env.Close();
        }
Exemple #26
0
        public void StatsInTxn(string home, string name, bool ifIsolation)
        {
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            EnvConfigCase1(envConfig);
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                home, envConfig);

            Transaction         openTxn  = env.BeginTransaction();
            RecnoDatabaseConfig dbConfig =
                new RecnoDatabaseConfig();

            ConfigCase1(dbConfig);
            dbConfig.Env = env;
            RecnoDatabase db = RecnoDatabase.Open(name + ".db",
                                                  dbConfig, openTxn);

            openTxn.Commit();

            Transaction statsTxn = env.BeginTransaction();
            RecnoStats  stats;
            RecnoStats  fastStats;

            if (ifIsolation == false)
            {
                stats     = db.Stats(statsTxn);
                fastStats = db.FastStats(statsTxn);
            }
            else
            {
                stats     = db.Stats(statsTxn, Isolation.DEGREE_ONE);
                fastStats = db.FastStats(statsTxn,
                                         Isolation.DEGREE_ONE);
            }
            ConfirmStatsPart1Case1(stats);

            // Put 1000 records into the database.
            PutRecordCase1(db, statsTxn);

            if (ifIsolation == false)
            {
                stats     = db.Stats(statsTxn);
                fastStats = db.FastStats(statsTxn);
            }
            else
            {
                stats     = db.Stats(statsTxn, Isolation.DEGREE_TWO);
                fastStats = db.FastStats(statsTxn,
                                         Isolation.DEGREE_TWO);
            }
            ConfirmStatsPart2Case1(stats);

            // Delete 500 records.
            for (int i = 250; i <= 750; i++)
            {
                db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)),
                          statsTxn);
            }

            if (ifIsolation == false)
            {
                stats     = db.Stats(statsTxn);
                fastStats = db.FastStats(statsTxn);
            }
            else
            {
                stats     = db.Stats(statsTxn, Isolation.DEGREE_THREE);
                fastStats = db.FastStats(statsTxn,
                                         Isolation.DEGREE_THREE);
            }
            ConfirmStatsPart3Case1(stats);

            statsTxn.Commit();
            db.Close();
            env.Close();
        }
Exemple #27
0
        public void TestLogStatPrint()
        {
            testName = "TestLogStatPrint";
            SetUpTest(true);

            string[] messageInfo = new string[]
            {
                "Log magic number",
                "Log version number",
                "Log record cache size",
                "Log file mode",
                "Current log file size",
                "Initial fileid allocation",
                "Current fileids in use",
                "Maximum fileids used",
                "Records entered into the log",
                "Log bytes written",
                "Log bytes written since last checkpoint",
                "Total log file I/O writes",
                "Total log file I/O writes due to overflow",
                "Total log file flushes",
                "Total log file I/O reads",
                "Current log file number",
                "Current log file offset",
                "On-disk log file number",
                "On-disk log file offset",
                "Maximum commits in a log flush",
                "Minimum commits in a log flush",
                "Region size",
                "The number of region locks that required waiting (0%)"
            };

            string logDir = "./";

            Directory.CreateDirectory(testHome + "/" + logDir);

            // Configure and open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create  = true;
            envConfig.UseTxns = true;

            envConfig.LogSystemCfg                        = new LogConfig();
            envConfig.LogSystemCfg.AutoRemove             = false;
            envConfig.LogSystemCfg.BufferSize             = 10240;
            envConfig.LogSystemCfg.Dir                    = logDir;
            envConfig.LogSystemCfg.FileMode               = 755;
            envConfig.LogSystemCfg.ForceSync              = true;
            envConfig.LogSystemCfg.InMemory               = false;
            envConfig.LogSystemCfg.LogExternalFileContent = false;
            envConfig.LogSystemCfg.MaxFileSize            = 1048576;
            envConfig.LogSystemCfg.NoBuffer               = false;
            envConfig.LogSystemCfg.NoSync                 = true;
            envConfig.LogSystemCfg.RegionSize             = 204800;
            envConfig.LogSystemCfg.ZeroOnCreate           = true;

            DatabaseEnvironment env =
                DatabaseEnvironment.Open(testHome, envConfig);

            // Confirm message file does not exist.
            string messageFile = testHome + "/" + "msgfile";

            Assert.AreEqual(false, File.Exists(messageFile));

            // Call set_msgfile() of env.
            env.Msgfile = messageFile;

            // Print env statistic to message file.
            env.PrintLoggingSystemStats();

            // Confirm message file exists now.
            Assert.AreEqual(true, File.Exists(messageFile));

            env.Msgfile = "";
            int    counter = 0;
            string line;

            line = null;

            // Read the message file line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(@"" + messageFile);
            while ((line = file.ReadLine()) != null)
            {
                string[] tempStr = line.Split('\t');
                // Confirm the content of the message file.
                Assert.AreEqual(tempStr[1], messageInfo[counter]);
                counter++;
            }
            Assert.AreNotEqual(counter, 0);

            file.Close();
            env.Close();
        }
        public void TestKeyEmptyException()
        {
            testName = "TestKeyEmptyException";
            SetUpTest(true);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();

            envConfig.Create     = true;
            envConfig.UseLocking = true;
            envConfig.UseLogging = true;
            envConfig.UseMPool   = true;
            envConfig.UseTxns    = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            QueueDatabase db;

            try
            {
                Transaction openTxn = env.BeginTransaction();
                try
                {
                    QueueDatabaseConfig queueConfig =
                        new QueueDatabaseConfig();
                    queueConfig.Creation = CreatePolicy.IF_NEEDED;
                    queueConfig.Length   = 10;
                    queueConfig.Env      = env;
                    db = QueueDatabase.Open(testName + ".db",
                                            queueConfig, openTxn);
                    openTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    openTxn.Abort();
                    throw e;
                }

                Transaction cursorTxn = env.BeginTransaction();
                Cursor      cursor;
                try
                {
                    /*
                     * Put a record into queue database with
                     * cursor and abort the operation.
                     */
                    cursor = db.Cursor(cursorTxn);
                    KeyValuePair <DatabaseEntry, DatabaseEntry> pair;
                    pair = new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                        new DatabaseEntry(BitConverter.GetBytes((int)10)),
                        new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
                    cursor.Add(pair);
                    cursor.Close();
                    cursorTxn.Abort();
                }
                catch (DatabaseException e)
                {
                    cursorTxn.Abort();
                    db.Close();
                    throw e;
                }

                Transaction delTxn = env.BeginTransaction();
                try
                {
                    /*
                     * The put operation is aborted in the queue
                     * database so querying if the record still exists
                     * throws KeyEmptyException.
                     */
                    db.Exists(new DatabaseEntry(
                                  BitConverter.GetBytes((int)10)), delTxn);
                    delTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    delTxn.Abort();
                    throw e;
                }
                finally
                {
                    db.Close();
                }
            }
            catch (KeyEmptyException)
            {
                throw new ExpectedTestException();
            }
            finally
            {
                env.Close();
            }
        }
Exemple #29
0
        public void TestFullLogBufferException()
        {
            testName = "TestFullLogBufferException";
            SetUpTest(true);

            // Open an environment and configured log subsystem.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();

            cfg.Create                              = true;
            cfg.TxnNoSync                           = true;
            cfg.UseTxns                             = true;
            cfg.UseLocking                          = true;
            cfg.UseMPool                            = true;
            cfg.UseLogging                          = true;
            cfg.LogSystemCfg                        = new LogConfig();
            cfg.LogSystemCfg.AutoRemove             = false;
            cfg.LogSystemCfg.BufferSize             = 409600;
            cfg.LogSystemCfg.MaxFileSize            = 10480;
            cfg.LogSystemCfg.NoBuffer               = false;
            cfg.LogSystemCfg.NoSync                 = true;
            cfg.LogSystemCfg.ZeroOnCreate           = true;
            cfg.LogSystemCfg.InMemory               = true;
            cfg.LogSystemCfg.LogExternalFileContent = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);

            BTreeDatabase db;

            try
            {
                Transaction openTxn = env.BeginTransaction();
                try
                {
                    BTreeDatabaseConfig dbConfig =
                        new BTreeDatabaseConfig();
                    dbConfig.Creation = CreatePolicy.IF_NEEDED;
                    dbConfig.Env      = env;
                    db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
                    openTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    openTxn.Abort();
                    throw e;
                }

                Transaction writeTxn = env.BeginTransaction();
                try
                {
                    /*
                     * Writing 10 large records into in-memory logging
                     * database should throw FullLogBufferException since
                     * the amount of put data is larger than buffer size.
                     */
                    byte[] byteArr = new byte[204800];
                    for (int i = 0; i < 10; i++)
                    {
                        db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                               new DatabaseEntry(byteArr), writeTxn);
                    }
                    writeTxn.Commit();
                }
                catch (Exception e)
                {
                    writeTxn.Abort();
                    throw e;
                }
                finally
                {
                    db.Close(true);
                }
            }
            catch (FullLogBufferException e)
            {
                Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode);
                throw new ExpectedTestException();
            }
            finally
            {
                env.Close();
            }
        }
Exemple #30
0
 public static extern void DB_ENV_api2_internal_set(HandleRef jarg1, DatabaseEnvironment jarg2);
Exemple #31
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);
            }
        }