public void TestConfig()
 {
     testName = "TestConfig";
     /*
      * Configure the fields/properties and see if
      * they are updated successfully.
      */
     TransactionConfig txnConfig = new TransactionConfig();
     XmlElement xmlElem = Configuration.TestSetUp(
         testFixtureName, testName);
     Config(xmlElem, ref txnConfig, true);
     Confirm(xmlElem, txnConfig, true);
 }
        public static void Confirm(XmlElement xmlElement, 
		    TransactionConfig txnConfig, bool compulsory)
        {
            Configuration.ConfirmIsolation(xmlElement,
                "IsolationDegree", txnConfig.IsolationDegree,
                compulsory);
            Configuration.ConfirmBool(xmlElement, "NoWait",
                txnConfig.NoWait, compulsory);
            Configuration.ConfirmBool(xmlElement, "Snapshot",
                txnConfig.Snapshot, compulsory);
            Configuration.ConfirmLogFlush(xmlElement, "SyncAction",
                txnConfig.SyncAction, compulsory);
        }
Exemple #3
0
        public void ReadTxn()
        {
            // Get a new transaction for reading the db.
            TransactionConfig txnConfig =
                new TransactionConfig();
            txnConfig.Snapshot = true;
            readTxn = paramEnv.BeginTransaction(
                txnConfig);

            // Get a new cursor for putting record into db.
            CursorConfig cursorConfig = new CursorConfig();
            cursorConfig.WriteCursor = false;
            BTreeCursor cursor = paramDB.Cursor(
                cursorConfig, readTxn);

            // Continually reading record from db.
            try
            {
                Assert.IsTrue(cursor.MoveFirst());
                int i = 0;
                do
                {
                    Assert.AreEqual(
                        BitConverter.ToInt32(
                        cursor.Current.Key.Data, 0),
                        BitConverter.ToInt32(
                        cursor.Current.Value.Data, 0));
                } while (i <= 1000 && cursor.MoveNext());
            }
            catch (DeadlockException)
            {
            }
            finally
            {
                cursor.Close();
            }
        }
Exemple #4
0
        public void UpdateTxn()
        {
            int int32Value;
            DatabaseEntry data;

            // Get a new transaction for updating the db.
            TransactionConfig txnConfig =
                new TransactionConfig();
            txnConfig.IsolationDegree =
                Isolation.DEGREE_THREE;

            updateTxn =
                paramEnv.BeginTransaction(txnConfig);

            // Continually putting record to db.

            BTreeCursor cursor =
                paramDB.Cursor(updateTxn);

            // Move the cursor to the first record.
            Assert.IsTrue(cursor.MoveFirst());
            int i = 0;
            try
            {
                do
                {

                    int32Value = BitConverter.ToInt32(
                         cursor.Current.Value.Data, 0);
                    data = new DatabaseEntry(
                         BitConverter.GetBytes(int32Value - 1));
                    cursor.Overwrite(data);
                } while (i <= 1000 && cursor.MoveNext());
            }
            catch (DeadlockException)
            {
            }
            finally
            {
                cursor.Close();
            }
        }
        public void PutRecordWithTxn(out DatabaseEnvironment env,
		    string home, string dbName, out Transaction txn)
        {
            BTreeDatabase db;

            // Open a new environment and begin a transaction.
            SetUpTransactionalEnv(home, out env);
            TransactionConfig txnConfig = new TransactionConfig();
            txnConfig.Name = "Transaction";
            txn = env.BeginTransaction(txnConfig);
            Assert.AreEqual("Transaction", txn.Name);

            // Open a new database within the transaction.
            OpenBtreeDBInEnv(dbName + ".db", env, out db, true, txn);

            // Write to the database within the transaction.
            WriteOneIntoBtreeDBWithTxn(db, txn);

            // Close the database.
            db.Close();
        }
        public void TestCloseWithoutSync()
        {
            testName = "TestCloseWithoutSync";
            testHome = testFixtureHome + "/" + testName;
            string btreeDBName = testName + ".db";

            Configuration.ClearDir(testHome);

            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.ForceFlush = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            envConfig.LogSystemCfg = new LogConfig();
            envConfig.LogSystemCfg.ForceSync = false;
            envConfig.LogSystemCfg.AutoRemove = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            TransactionConfig txnConfig = new TransactionConfig();
            txnConfig.SyncAction =
                TransactionConfig.LogFlush.WRITE_NOSYNC;
            Transaction txn = env.BeginTransaction(txnConfig);

            BTreeDatabaseConfig btreeConfig =
                new BTreeDatabaseConfig();
            btreeConfig.Creation = CreatePolicy.ALWAYS;
            btreeConfig.Env = env;

            BTreeDatabase btreeDB = BTreeDatabase.Open(
                btreeDBName, btreeConfig, txn);

            DatabaseEntry key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("key"));
            DatabaseEntry data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("data"));
            Assert.IsFalse(btreeDB.Exists(key, txn));
            btreeDB.Put(key, data, txn);
            btreeDB.Close(false);
            txn.Commit();
            env.Close();

            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.NEVER;
            using (BTreeDatabase db = BTreeDatabase.Open(
                testHome + "/" + btreeDBName, dbConfig))
            {
                Assert.IsFalse(db.Exists(key));
            }
        }
 public void DeadLockThreadWithTxnTimeout()
 {
     // Configure and begin a transaction.
     TransactionConfig txnConfig = new TransactionConfig();
     txnConfig.TxnTimeout = 5000;
     txnConfig.Name = "DeadLockThreadWithTxnTimeout";
     Transaction txn =
         testBeginTransactionEnv.BeginTransaction(txnConfig, null);
     try
     {
         testBeginTransactionDB.Put(
             new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
             new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));
         signal.WaitOne();
         testBeginTransactionDB.Put(
             new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newkey")),
             new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newdata")),
             txn);
         txn.Commit();
     }
     catch (DeadlockException)
     {
         try
         {
             txn.Abort();
         }
         catch (DatabaseException)
         {
             throw new TestException();
         }
     }
     catch (DatabaseException)
     {
         try
         {
             txn.Abort();
         }
         catch (DatabaseException)
         {
             throw new TestException();
         }
     }
 }
        public void TestTransactionSystemStats()
        {
            testName = "TestTransactionSystemStats";
            SetUpTest(true);

            TransactionStats stats;
            BTreeDatabase db;
            Transaction openTxn = null;

            // Open an environment.
            long dateTime;
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.MaxTransactions = 50;
            envConfig.UseLogging = true;
            envConfig.UseLocking = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            envConfig.TxnNoSync = false;
            envConfig.TxnNoWait = true;
            envConfig.TxnSnapshot = true;
            envConfig.TxnTimestamp = DateTime.Now;
            envConfig.TxnWriteNoSync = false;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            try
            {
                try
                {
                    // Confirm initial transaction subsystem statistics.
                    stats = env.TransactionSystemStats();
                    env.PrintTransactionSystemStats(true, true);
                    Assert.AreEqual(0, stats.Aborted);
                    Assert.AreEqual(0, stats.Active);
                    Assert.AreEqual(0, stats.Begun);
                    Assert.AreEqual(0, stats.Committed);
                    Assert.AreEqual(0, stats.LastCheckpoint.LogFileNumber);
                    Assert.AreEqual(0, stats.LastCheckpoint.Offset);
                    Assert.AreEqual(50, stats.MaxTransactions);
                    Assert.AreNotEqual(0, stats.RegionSize);
                    Assert.AreEqual(0, stats.Transactions.Count);
                }
                catch (AssertionException e)
                {
                    throw e;
                }

                try
                {
                    //Begin a transaction called openTxn and open a database.
                    TransactionConfig openTxnCfg = new TransactionConfig();
                    openTxnCfg.Name = "openTxn";
                    openTxn = env.BeginTransaction(openTxnCfg);
                    openTxn.Priority = 50;
                    BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
                    dbConfig.Creation = CreatePolicy.IF_NEEDED;
                    dbConfig.Env = env;
                    db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);
                }
                catch (DatabaseException e)
                {
                    if (openTxn != null)
                        openTxn.Abort();
                    throw e;
                }

                try
                {
                    // At least there is one transaction that is alive.
                    env.Checkpoint();
                    stats = env.TransactionSystemStats();
                    env.PrintTransactionSystemStats();
                    Assert.AreNotEqual(0, stats.Active);
                    Assert.AreNotEqual(0, stats.Transactions.Count);
                    Assert.AreNotEqual(0, stats.Transactions.Capacity);
                    Assert.AreNotEqual(0, stats.RegionLockNoWait);
                    dateTime = stats.LastCheckpointTime;

                    // Begin an embedded transaction called putTxn.
                    TransactionConfig putTxnCfg =
                        new TransactionConfig();
                    putTxnCfg.Name = "putTxn";
                    putTxnCfg.NoWait = false;
                    Transaction putTxn = env.BeginTransaction(
                        putTxnCfg, openTxn);
                    putTxn.Priority = 50;

                    try
                    {
                        // Put some records into database within putTxn.
                        for (int i = 0; i < 50; i++)
                            db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                                new DatabaseEntry(BitConverter.GetBytes(i)), putTxn);
                            stats = env.TransactionSystemStats();
                            Assert.AreNotEqual(0, stats.MaxActive);
                            Assert.AreNotEqual(0, stats.MaxTransactions);
                            Assert.AreEqual(0, stats.MaxSnapshot);
                            Assert.AreEqual(0, stats.Snapshot);
                            Assert.AreEqual(stats.Begun,
                                stats.Aborted + stats.Active + stats.Committed);
                            Assert.AreEqual(2, stats.Transactions.Count);

                        /*
                         * Both of LogFileNumber and Offset in active transaction
                         * couldn't be 0.
                         */
                        uint logFileNumbers = 0;
                        uint offSets = 0;
                        for (int i = 0; i < stats.Transactions.Count;i++)
                        {
                            logFileNumbers += stats.Transactions[i].Begun.LogFileNumber;
                            offSets += stats.Transactions[i].Begun.Offset;
                        }
                        Assert.AreNotEqual(0, logFileNumbers);
                        Assert.AreNotEqual(0, offSets);

                        // All active transactions are run by the same process and thread.

                        Assert.AreEqual(stats.Transactions[0].ThreadID,
                            stats.Transactions[1].ThreadID);
                        Assert.AreEqual(stats.Transactions[0].ProcessID,
                            stats.Transactions[1].ProcessID);

                        // All transactions are alive.
                        Assert.AreEqual(ActiveTransaction.TransactionStatus.RUNNING,
                            stats.Transactions[0].Status);
                        Assert.AreEqual(ActiveTransaction.TransactionStatus.RUNNING,
                            stats.Transactions[1].Status);

                        Assert.AreEqual(50, stats.Transactions[0].Priority);
                        Assert.AreEqual(50, stats.Transactions[1].Priority);

                        /*
                         * Find the openTxn in active transactions, which is the
                         * parent transaction of putTxn.
                         */
                        int parentPos = 0;
                        if (stats.Transactions[0].Name == "putTxn")
                            parentPos = 1;

                        // putTxn's parent id should be the openTxn.
                        Assert.AreEqual(stats.Transactions[parentPos].ID,
                            stats.Transactions[1 - parentPos].ParentID);

                        // Other stats should be an positive integer.
                        for (int i = 0; i < stats.Transactions.Count - 1; i++)
                        {
                            Assert.LessOrEqual(0,
                                stats.Transactions[i].BufferCopiesInCache);
                            Assert.LessOrEqual(0,
                                stats.Transactions[i].SnapshotReads.LogFileNumber);
                            Assert.LessOrEqual(0,
                                stats.Transactions[i].SnapshotReads.Offset);
                            Assert.IsNotNull(stats.Transactions[i].GlobalID);
                        }

                        // Commit putTxn.
                        putTxn.Commit();
                    }
                    catch (DatabaseException e)
                    {
                        putTxn.Abort();
                        throw e;
                    }

                    stats = env.TransactionSystemStats();
                    Assert.AreNotEqual(0, stats.LastCheckpoint.LogFileNumber);
                    Assert.AreNotEqual(0, stats.LastCheckpoint.Offset);
                    Assert.AreEqual(dateTime, stats.LastCheckpointTime);

                    openTxn.Commit();
                }
                catch (DatabaseException e)
                {
                    openTxn.Abort();
                    throw e;
                }
                finally
                {
                    db.Close();
                }
            }
            finally
            {
                env.Close();
            }
        }
        public void TestLogFile()
        {
            testName = "TestLogFile";
            SetUpTest(true);

            // Open environment and configure logging subsystem.
            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 = "./";
            cfg.LogSystemCfg.FileMode = 755;
            cfg.LogSystemCfg.ForceSync = true;
            cfg.LogSystemCfg.InMemory = false;
            cfg.LogSystemCfg.MaxFileSize = 1048576;
            cfg.LogSystemCfg.NoBuffer = false;
            cfg.LogSystemCfg.RegionSize = 204800;
            cfg.LogSystemCfg.ZeroOnCreate = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg);

            // Open database.
            Transaction allTxn = env.BeginTransaction();
            TransactionConfig txnConfig = new TransactionConfig();
            txnConfig.Name = "OpenTransaction";
            Transaction openTxn = env.BeginTransaction(txnConfig, allTxn);
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db = BTreeDatabase.Open(
                testName + ".db", dbConfig, openTxn);

            List<ActiveTransaction> activeTxns =
                env.TransactionSystemStats().Transactions;
            for (int i = 0; i < activeTxns.Count; i++)
                if (activeTxns[i].Name == "OpenTransaction")
                {
                    LSN lsn = new LSN(
                        activeTxns[i].Begun.LogFileNumber,
                        activeTxns[i].Begun.Offset);
                    env.LogFlush(lsn);
                    string fileName = env.LogFile(lsn);
                }

            openTxn.Commit();

            // Write "##" to log before putting data into database.
            env.WriteToLog("##");

            // Write 1000 records into database.
            TransactionConfig writeTxnConfig = new TransactionConfig();
            writeTxnConfig.Name = "WriteTxn";
            Transaction writeTxn = env.BeginTransaction(writeTxnConfig, allTxn);
            byte[] byteArr = new byte[1024];
            for (int i = 0; i < 1000; i++)
            {
                db.Put(new DatabaseEntry(BitConverter.GetBytes(i)),
                    new DatabaseEntry(byteArr), writeTxn);
                env.LogFlush();
                env.WriteToLog("#" + i.ToString(), writeTxn);
            }

            activeTxns = env.TransactionSystemStats().Transactions;
            for (int i = 0; i < activeTxns.Count; i++)
                if (activeTxns[i].Name == "WriteTxn")
                {
                    LSN lsn = new LSN(
                        activeTxns[i].Begun.LogFileNumber,
                        activeTxns[i].Begun.Offset);
                    env.LogFlush(lsn);
                    string fileName = env.LogFile(lsn);
                }

            writeTxn.Commit();
            db.Close();

            // Write "##" after data has been put.
            env.WriteToLog("##");

            List<string> logFiles = env.LogFiles(true);

            env.LogWrite(new DatabaseEntry(), true);

            env.RemoveUnusedLogFiles();

            allTxn.Commit();
            env.Close();
        }
        /// <summary>
        /// Create a new transaction in the environment.
        /// </summary>
        /// <remarks>
        /// In the presence of distributed transactions and two-phase commit,
        /// only the parental transaction, that is a transaction without a
        /// parent specified, should be passed as an parameter to 
        /// <see cref="Transaction.Prepare"/>.
        /// </remarks>
        /// <param name="cfg">
        /// The configuration properties for the transaction
        /// </param>
        /// <param name="parent">
        /// If non-null, the new transaction is a nested transaction,
        /// with <paramref name="parent"/> as the new transaction's parent.
        /// Transactions may be nested to any level.
        /// </param>
        /// <returns>A new transaction object</returns>
        public Transaction BeginTransaction(
            TransactionConfig cfg, Transaction parent)
        {
            DB_TXN dbtxn = dbenv.txn_begin(
                Transaction.getDB_TXN(parent), cfg.flags);
            Transaction txn = new Transaction(dbtxn);
            if (cfg.lockTimeoutIsSet)
                txn.SetLockTimeout(cfg.LockTimeout);
            if (cfg.nameIsSet)
                txn.Name = cfg.Name;
            if (cfg.txnTimeoutIsSet)
                txn.SetTxnTimeout(cfg.TxnTimeout);

            return txn;
        }
 /// <summary>
 /// Create a new transaction in the environment.
 /// </summary>
 /// <param name="cfg">
 /// The configuration properties for the transaction
 /// </param>
 /// <returns>A new transaction object</returns>
 public Transaction BeginTransaction(TransactionConfig cfg)
 {
     return BeginTransaction(cfg, null);
 }
        public void TestNestedTXN()
        {
            testName = "TestNestedTXN";
            SetUpTest(true);
            DatabaseEntry key, data;
            DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true);
            BTreeDatabase db = Open(master, true);
            TransactionConfig txnconfig = new TransactionConfig();
            Transaction txn = master.BeginTransaction();
            Transaction txn1 = master.BeginTransaction(txnconfig, txn);

            key = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Key"));
            data = new DatabaseEntry(
                ASCIIEncoding.ASCII.GetBytes("Data"));
            db.Put(key, data, txn1);
            txn1.Commit();
            txn.Commit();

            byte[] token;
            Assert.Throws<ArgumentException>(delegate { token = txn1.CommitToken; });
            master.Close();
        }
Exemple #13
0
        public static void ConfirmLogFlush(XmlElement xmlElem,
		    string name, TransactionConfig.LogFlush value,
		    bool compulsory)
        {
            XmlNode xmlNode;
            string logFlush;

            xmlNode = XMLReader.GetNode(xmlElem, name);
            if (xmlNode == null && compulsory == true)
                throw new ConfigNotFoundException(name);
            else if (xmlNode != null)
            {
                logFlush = xmlNode.InnerText;
                if (logFlush == "DEFAULT")
                    Assert.AreEqual(TransactionConfig.LogFlush.DEFAULT, value);
                else if (logFlush == "NOSYNC")
                    Assert.AreEqual(TransactionConfig.LogFlush.NOSYNC, value);
                else if (logFlush == "WRITE_NOSYNC")
                    Assert.AreEqual(TransactionConfig.LogFlush.WRITE_NOSYNC, value);
                else if (logFlush == "SYNC")
                    Assert.AreEqual(TransactionConfig.LogFlush.SYNC, value);
                else
                    throw new InvalidConfigException(name);
            }
        }
Exemple #14
0
        public static bool ConfigLogFlush(XmlElement xmlElem,
		    string name, ref TransactionConfig.LogFlush value,
		    bool compulsory)
        {
            XmlNode xmlNode;
            string logFlush;

            xmlNode = XMLReader.GetNode(xmlElem, name);
            if (xmlNode == null && compulsory == false)
                return false;
            else if (xmlNode == null && compulsory == true)
                throw new ConfigNotFoundException(name);

            logFlush = xmlNode.InnerText;
            if (logFlush == "DEFAULT")
                value = TransactionConfig.LogFlush.DEFAULT;
            else if (logFlush == "NOSYNC")
                value = TransactionConfig.LogFlush.NOSYNC;
            else if (logFlush == "WRITE_NOSYNC")
                value = TransactionConfig.LogFlush.WRITE_NOSYNC;
            else if (logFlush == "SYNC")
                value = TransactionConfig.LogFlush.SYNC;
            else
                throw new InvalidConfigException(name);

            return true;
        }
        public void TestTransactionStatPrint()
        {
            testName = "TestTransactionStatPrint";
            SetUpTest(true);

            string[] messageInfo = new string[]
            {
              "No checkpoint LSN",
              "Checkpoint timestamp",
              "Last transaction ID allocated",
              "Maximum number of active transactions configured",
              "Initial number of transactions configured",
              "Active transactions",
              "Maximum active transactions",
              "Number of transactions begun",
              "Number of transactions aborted",
              "Number of transactions committed",
              "Snapshot transactions",
              "Maximum snapshot transactions",
              "Number of transactions restored",
              "Region size",
              "The number of region locks that required waiting (0%)",
              ""
            };

            // Configure and open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.FreeThreaded = true;
            envConfig.LockTimeout = 1000;
            envConfig.MPoolSystemCfg = new MPoolConfig();
            envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1);
            envConfig.UseLocking = true;
            envConfig.UseMPool = true;
            envConfig.UseTxns = true;
            envConfig.MaxTransactions = 50;

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

            //Begin a transaction called openTxn and open a database.
            Transaction openTxn = null;
            TransactionConfig openTxnCfg = new TransactionConfig();
            openTxnCfg.Name = "openTxn";
            openTxn = env.BeginTransaction(openTxnCfg);
            openTxn.Priority = 50;
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db;
            db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn);

            // 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.PrintTransactionSystemStats();

            // 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.
                if (tempStr[0] == "Active transactions:")
                    break;
                Assert.AreEqual(messageInfo[counter], tempStr[1]);
                counter++;
            }
            Assert.AreNotEqual(0, counter);

            openTxn.Commit();
            file.Close();
            env.Close();
        }