Example #1
0
        public void TestSnapshotIsolation()
        {
            BTreeDatabaseConfig dbConfig;
            DatabaseEntry key, data;
            DatabaseEnvironmentConfig envConfig;
            Thread readThread, updateThread;
            Transaction txn;

            updateTxn = null;
            readTxn = null;
            paramDB = null;
            paramEnv = null;
            testName = "TestSnapshotIsolation";
            testHome = testFixtureHome + "/" + testName;

            Configuration.ClearDir(testHome);

            /*
             * Open environment with DB_MULTIVERSION
             * which is required by DB_TXN_SNAPSHOT.
             */
            envConfig = new DatabaseEnvironmentConfig();
            envConfig.Create = true;
            envConfig.UseMVCC = true;
            envConfig.UseTxns = true;
            envConfig.UseMPool = true;
            envConfig.UseLocking = true;
            envConfig.TxnTimeout = 1000;
            paramEnv = DatabaseEnvironment.Open(
                testHome, envConfig);
            paramEnv.DetectDeadlocks(DeadlockPolicy.YOUNGEST);

            /*
             * Open a transactional database and put 1000 records
             * into it within transaction.
             */
            txn = paramEnv.BeginTransaction();
            dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.UseMVCC = true;
            dbConfig.Env = paramEnv;
            paramDB = BTreeDatabase.Open(
                testName + ".db", dbConfig, txn);
            for (int i = 0; i < 256; i++)
            {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                paramDB.Put(key, data, txn);
            }
            txn.Commit();

            /*
             * Begin two threads, read and update thread.
             * The update thread runs a update transaction
             * using full read/write locking. The read thread
             * set DB_TXN_SNAPSHOT on read-only cursor.
             */
            readThread = new Thread(new ThreadStart(ReadTxn));
            updateThread = new Thread(new ThreadStart(UpdateTxn));
            updateThread.Start();
            Thread.Sleep(1000);
            readThread.Start();
            readThread.Join();
            updateThread.Join();

            // Commit transacion in both two threads.
            if (updateTxn != null)
                updateTxn.Commit();
            if (readTxn != null)
                readTxn.Commit();

            /*
             * Confirm that the overwrite operation works.
             */
            ConfirmOverwrite();

            paramDB.Close();
            paramEnv.Close();
        }
Example #2
0
        public void MoveWithRMW(string home, string name)
        {
            paramEnv = null;
            paramDB = null;

            // Open the environment.
            DatabaseEnvironmentConfig envCfg =
                new DatabaseEnvironmentConfig();
            envCfg.Create = true;
            envCfg.FreeThreaded = true;
            envCfg.UseLocking = true;
            envCfg.UseLogging = true;
            envCfg.UseMPool = true;
            envCfg.UseTxns = true;
            paramEnv = DatabaseEnvironment.Open(home, envCfg);

            // Open database in transaction.
            Transaction openTxn = paramEnv.BeginTransaction();
            BTreeDatabaseConfig cfg = new BTreeDatabaseConfig();
            cfg.Creation = CreatePolicy.ALWAYS;
            cfg.Env = paramEnv;
            cfg.FreeThreaded = true;
            cfg.PageSize = 4096;
            cfg.Duplicates = DuplicatesPolicy.UNSORTED;
            paramDB = BTreeDatabase.Open(name + ".db", cfg, openTxn);
            openTxn.Commit();

            /*
             * Put 10 different, 2 duplicate and another different
             * records into database.
             */
            Transaction txn = paramEnv.BeginTransaction();
            for (int i = 0; i < 13; i++)
            {
                DatabaseEntry key, data;
                if (i == 10 || i == 11)
                {
                    key = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes("key"));
                    data = new DatabaseEntry(
                        ASCIIEncoding.ASCII.GetBytes("data"));
                }
                else
                {
                    key = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                    data = new DatabaseEntry(
                        BitConverter.GetBytes(i));
                }
                paramDB.Put(key, data, txn);
            }

            txn.Commit();

            // Get a event wait handle.
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);

            /*
             * Start RdMfWt() in two threads. RdMfWt() reads
             * and writes data into database.
             */
            Thread t1 = new Thread(new ThreadStart(RdMfWt));
            Thread t2 = new Thread(new ThreadStart(RdMfWt));
            t1.Start();
            t2.Start();

            /*
             * Give both threads time to read before signalling
             * them to write.
             */
            Thread.Sleep(1000);

            // Invoke the write operation in both threads.
            signal.Set();

            // Return the number of deadlocks.
            while (t1.IsAlive || t2.IsAlive)
            {
                /*
                 * Give both threads time to write before
                 * counting the number of deadlocks.
                 */
                Thread.Sleep(1000);
                uint deadlocks = paramEnv.DetectDeadlocks(
                   DeadlockPolicy.DEFAULT);

                // Confirm that there won't be any deadlock.
                Assert.AreEqual(0, deadlocks);
            }

            t1.Join();
            t2.Join();
            paramDB.Close();
            paramEnv.Close();
        }
Example #3
0
        /*
         * ifSetLock is used to indicate which timeout function
         * is used, SetLockTimeout or SetTxnTimeout.
         */
        public void TestTimeOut(bool ifSetLock)
        {
            // Open environment and begin transaction.
            Transaction txn;
            deadLockEnv = null;
            SetUpEnvWithTxnAndLocking(testHome,
                out deadLockEnv, out txn, 0, 0, 0, 0);

            // Define deadlock detection and resolve policy.
            deadLockEnv.DeadlockResolution =
                DeadlockPolicy.YOUNGEST;
            if (ifSetLock == true)
                txn.SetLockTimeout(10);
            else
                txn.SetTxnTimeout(10);

            txn.Commit();
            deadLockEnv.Close();
        }
        public void TestDetectDeadlocks()
        {
            testName = "TestDetectDeadlocks";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.FreeThreaded = true;
            testDetectDeadlocksEnv = DatabaseEnvironment.Open(
                testHome, cfg);

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testDetectDeadlocksEnv;
            dbConfig.Duplicates = DuplicatesPolicy.NONE;
            dbConfig.FreeThreaded = true;
            testDetectDeadlocksDB = BTreeDatabase.Open(
                testName + ".db", dbConfig);

            // Put one record("key", "data") into database.
            testDetectDeadlocksDB.Put(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));

            // Begin two threads to read and write record.
            Thread thread1 = new Thread(new ThreadStart(ReadAndPutRecordThread));
            Thread thread2 = new Thread(new ThreadStart(ReadAndPutRecordThread));
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            thread1.Start();
            thread2.Start();

            // Give enough time for threads to read record.
            Thread.Sleep(1000);

            /*
             * Let the two threads apply for write lock
             * synchronously.
             */
            signal.Set();

            // Confirm that there is deadlock in the environment.
            Thread.Sleep(1000);
            uint deadlockNum = testDetectDeadlocksEnv.DetectDeadlocks(
                DeadlockPolicy.DEFAULT);
            Assert.Less(0, deadlockNum);

            thread1.Join();
            thread2.Join();

            // Close all.
            testDetectDeadlocksDB.Close(false);
            testDetectDeadlocksEnv.Close();
        }
        public void TestCheckpoint()
        {
            testName = "TestCheckpoint";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.NoLocking = false;
            cfg.FreeThreaded = true;
            testCheckpointEnv = DatabaseEnvironment.Open(testHome, cfg);

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testCheckpointEnv;
            dbConfig.FreeThreaded = true;
            testCheckpointDB = BTreeDatabase.Open(testName + ".db", dbConfig);

            // Run a thread to put records into database.
            Thread thread1 = new Thread(new ThreadStart(PutRecordsThread));

            /*
             * Run a thread to do checkpoint periodically and
             * finally do a checkpoint to flush all in memory pool
             * to log files.
             */
            Thread thread2 = new Thread(new ThreadStart(CheckpointThread));

            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            // Close all.
            testCheckpointDB.Close();
            testCheckpointEnv.Close();
        }
        public void TestBeginTransaction()
        {
            testName = "TestBeginTransaction";
            SetUpTest(true);

            // Open an environment.
            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.NoLocking = false;
            cfg.FreeThreaded = true;
            testBeginTransactionEnv = DatabaseEnvironment.Open(testHome, cfg);
            testBeginTransactionEnv.DeadlockResolution = DeadlockPolicy.OLDEST;

            // Open btree database.
            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.AutoCommit = true;
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = testBeginTransactionEnv;
            dbConfig.Duplicates = DuplicatesPolicy.NONE;
            dbConfig.FreeThreaded = true;
            testBeginTransactionDB = BTreeDatabase.Open(
                testName + ".db", dbConfig);

            testBeginTransactionDB.Put(
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")),
                new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")));

            // Begin two threads to run dead lock detection.
            Thread thread1 = new Thread(new ThreadStart(
                DeadLockThreadWithLockTimeOut));
            Thread thread2 = new Thread(new ThreadStart(
                DeadLockThreadWithTxnTimeout));
            signal = new EventWaitHandle(false, EventResetMode.ManualReset);
            thread1.Start();
            thread2.Start();
            Thread.Sleep(1000);
            signal.Set();
            thread1.Join();
            thread2.Join();

            // Close all.
            testBeginTransactionDB.Close();
            testBeginTransactionEnv.Close();
        }
        public void TestFailCheck()
        {
            testName = "TestFailCheck";
            SetUpTest(true);

            DatabaseEnvironmentConfig cfg =
                new DatabaseEnvironmentConfig();
            cfg.Create = true;
            cfg.UseTxns = true;
            cfg.UseMPool = true;
            cfg.UseLogging = true;
            cfg.UseLocking = true;
            cfg.FreeThreaded = true;
            cfg.ThreadIsAlive = new ThreadIsAliveDelegate(ThrdAlive);
            cfg.SetThreadID = new SetThreadIDDelegate(SetThrdID);
            cfg.ThreadCount = 10;
            testFailCheckEnv = DatabaseEnvironment.Open(testHome, cfg);

            Thread thread = new Thread(new ThreadStart(WriteThreadWithoutTxnCommit));
            thread.Start();
            thread.Join();
            testFailCheckEnv.FailCheck();
            testFailCheckEnv.Close();
        }