Package org.apache.derbyTesting.functionTests.util

Examples of org.apache.derbyTesting.functionTests.util.Barrier


        // helper thread. The main thread uses it to tell the helper thread
        // that it has started the index scan. The helper thread uses it
        // to tell the main thread that it has locked row 40 and is ready to
        // insert more values. Both threads should wait until the other thread
        // has reached the barrier before continuing.
        final Barrier barrier = new Barrier(2);

        // Lock a row on the first page in a different thread to stop the
        // index scan. Then split the first leaf by inserting many values
        // less than zero.
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                s.executeUpdate("update t set x = x where x = 40");
                s.close();

                // Tell the main thread that we've locked the row and that
                // it can go ahead with the index scan. Wait here until the
                // main thread has started the scan.
                barrier.await();

                // The main thread has started the index scan. Give it a
                // second to get to the row we have locked.
                Thread.sleep(1000L);

                // Split the first leaf
                PreparedStatement ps = conn.prepareStatement(
                        "insert into t values ?");
                for (int i = -1; i > -300; i--) {
                    ps.setInt(1, i);
                    ps.executeUpdate();
                }
                ps.close();
                conn.commit();
            }
        });

        // Prepare the index scan.
        ResultSet rs = s.executeQuery(
                "select * from t --DERBY-PROPERTIES constraint=C");

        // Perform an index scan. Will be blocked for a while when fetching
        // the row where x=40, but should be able to resume the scan.
        for (int i = 0; i < 300; i++) {
            assertTrue(rs.next());
            assertEquals(i, rs.getInt(1));

            // Once we have fetched the first row, tell the helper thread we
            // have started the index scan, and wait until it has locked the
            // row that should block the scan (x=40).
            if (i == 0) {
                barrier.await();
            }
        }
        assertFalse(rs.next());
        rs.close();
    }
View Full Code Here


        commit();

        // Object used for synchronization between main thread and helper
        // thread. They should both wait for the other thread to reach the
        // barrier point before continuing.
        final Barrier barrier = new Barrier(2);

        // Hold a lock in a different thread to stop the index scan, then
        // split the first leaf (on which the scan is positioned) before the
        // lock is released.
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                s.executeUpdate("update t set x = x where x = 40");

                // Tell the main thread we have locked the row, and wait for
                // it to start the index scan.
                barrier.await();

                // Give the index scan time to get to the row we have locked.
                Thread.sleep(1000);

                // The index scan should be blocked now. Split the first leaf
                // by inserting more values just before the lowest key, so
                // that we can verify that the index scan is able to reposition
                // correctly after a page split.
                for (int i = 0; i < 300; i++) {
                    s.executeUpdate("insert into t values -1");
                }
                s.close();
                conn.commit();
            }
        });

        // Perform an index scan. Will be blocked for a while when fetching
        // the row where x=40, but should be able to resume the scan after
        // the helper thread commits and releases its locks.
        ResultSet rs = s.executeQuery(
                "select * from t --DERBY-PROPERTIES index=IDX");

        for (int i = 0; i < 300; i++) {
            assertTrue(rs.next());
            assertEquals(i, rs.getInt(1));

            // Once we have fetched the first row, tell the helper thread we
            // have started the index scan, and wait until it has locked the
            // row that should block the scan (x=40).
            if (i == 0) {
                barrier.await();
            }
        }
        assertFalse(rs.next());
        rs.close();
    }
View Full Code Here

        // helper thread. The main thread uses it to tell the helper thread
        // that it has started the scan. The helper thread uses it
        // to tell the main thread that it is ready.
        // Both threads should wait until the other thread
        // has reached the barrier before continuing.
        final Barrier barrier = new Barrier(2);
       
        // start the second thread and make it do the same update
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                // note: asserts in this inner class do not make the test fail
                // so, just executing it here
                s.executeUpdate("update account set b = b + 11");
                s.close();
               
                // Tell the main thread that we've locked the row
                barrier.await();
               
                // The main thread now can continue - give it a
                // second to do its stuff
                //Thread.sleep(1000L);
                // we check that the 'wait' state is gone at the main thread,
                // it would not cause the test to fail if we checked it here.
            }
        });
       
        // now select from syscs_diag.lock_table, don't wait more than minute.
        int totalWait = 0;
        boolean found=false;
        do {
            totalWait += 500;
            Thread.sleep(500);
            // we want to look for 'WAIT' state. There will also
            // be one of more 'GRANT' state locks, likely background threads,
            // but we're not interested in those here.
            found=getWaitState();
        } while (!found && totalWait < 6000);
        // defer the assert until we've alerted the async thread
        // commit will release the lock
        commit();
       
        // set the timeout back so things can timeout.
        s.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY" +
                "('derby.locks.waitTimeout','5')");
        commit();
       
        // Now that we've found the wait state, tell the helper thread we
        // are done
        barrier.await();
       
        // now that we've released the helper thread, we can safely let
        // the test fail if the results of the earlier check were bad
        assertTrue("expected to find a 'WAIT' state, but did not", found);
       
View Full Code Here

        // helper thread. The main thread uses it to tell the helper thread
        // that it has started the index scan. The helper thread uses it
        // to tell the main thread that it has locked row 40 and is ready to
        // insert more values. Both threads should wait until the other thread
        // has reached the barrier before continuing.
        final Barrier barrier = new Barrier(2);

        // Lock a row on the first page in a different thread to stop the
        // index scan. Then split the first leaf by inserting many values
        // less than zero.
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                s.executeUpdate("update t set x = x where x = 40");
                s.close();

                // Tell the main thread that we've locked the row and that
                // it can go ahead with the index scan. Wait here until the
                // main thread has started the scan.
                barrier.await();

                // The main thread has started the index scan. Give it a
                // second to get to the row we have locked.
                Thread.sleep(1000L);

                // Split the first leaf
                PreparedStatement ps = conn.prepareStatement(
                        "insert into t values ?");
                for (int i = -1; i > -300; i--) {
                    ps.setInt(1, i);
                    ps.executeUpdate();
                }
                ps.close();
                conn.commit();
            }
        });

        // Prepare the index scan.
        ResultSet rs = s.executeQuery(
                "select * from t --DERBY-PROPERTIES constraint=C");

        // Perform an index scan. Will be blocked for a while when fetching
        // the row where x=40, but should be able to resume the scan.
        for (int i = 0; i < 300; i++) {
            assertTrue(rs.next());
            assertEquals(i, rs.getInt(1));

            // Once we have fetched the first row, tell the helper thread we
            // have started the index scan, and wait until it has locked the
            // row that should block the scan (x=40).
            if (i == 0) {
                barrier.await();
            }
        }
        assertFalse(rs.next());
        rs.close();
    }
View Full Code Here

        commit();

        // Object used for synchronization between main thread and helper
        // thread. They should both wait for the other thread to reach the
        // barrier point before continuing.
        final Barrier barrier = new Barrier(2);

        // Hold a lock in a different thread to stop the index scan, then
        // split the first leaf (on which the scan is positioned) before the
        // lock is released.
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                s.executeUpdate("update t set x = x where x = 40");

                // Tell the main thread we have locked the row, and wait for
                // it to start the index scan.
                barrier.await();

                // Give the index scan time to get to the row we have locked.
                Thread.sleep(1000);

                // The index scan should be blocked now. Split the first leaf
                // by inserting more values just before the lowest key, so
                // that we can verify that the index scan is able to reposition
                // correctly after a page split.
                for (int i = 0; i < 300; i++) {
                    s.executeUpdate("insert into t values -1");
                }
                s.close();
                conn.commit();
            }
        });

        // Perform an index scan. Will be blocked for a while when fetching
        // the row where x=40, but should be able to resume the scan after
        // the helper thread commits and releases its locks.
        ResultSet rs = s.executeQuery(
                "select * from t --DERBY-PROPERTIES index=IDX");

        for (int i = 0; i < 300; i++) {
            assertTrue(rs.next());
            assertEquals(i, rs.getInt(1));

            // Once we have fetched the first row, tell the helper thread we
            // have started the index scan, and wait until it has locked the
            // row that should block the scan (x=40).
            if (i == 0) {
                barrier.await();
            }
        }
        assertFalse(rs.next());
        rs.close();
    }
View Full Code Here

        // This barrier lets the two threads wait for each other so that both
        // can obtain a read lock before going on trying to obtain the write
        // lock. If one thread goes ahead and obtains the write lock before the
        // other thread has obtained the read lock, we won't see a deadlock.
        final Barrier readLockBarrier = new Barrier(threads.length);

        // Exceptions seen by the threads.
        final List<Exception> exceptions =
                Collections.synchronizedList(new ArrayList<Exception>());

        // Start the two threads. Both should first obtain a read lock, and
        // when both have the read lock, they should try to lock the same row
        // exclusively. They'll be blocking each other, and we have a deadlock.
        for (int i = 0; i < threads.length; i++) {
            final Connection c = openDefaultConnection();
            c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            c.setAutoCommit(false);

            final PreparedStatement select = c.prepareStatement(
                    "select * from derby3980 where i = 456");
            final PreparedStatement update = c.prepareStatement(
                    "update derby3980 set i = 456 where i = 456");

            threads[i] = new Thread() {
                public void run() {
                    try {
                        JDBC.assertSingleValueResultSet(
                                select.executeQuery(), "456");

                        // Now we've got the read lock. Wait until all threads
                        // have it before attempting to get the write lock.
                        readLockBarrier.await();

                        // All threads have the read lock. Now all should try
                        // to update the row and thereby create a deadlock.
                        assertUpdateCount(update, 1);

View Full Code Here

        PreparedStatement ps1 = prepareStatement("select * from t where id=2");
        final PreparedStatement ps2 =
                c2.prepareStatement("select * from t where id=1");

        // Create a barrier for the two threads to synchronize.
        final Barrier barrier = new Barrier(2);

        final SQLException[] holder = new SQLException[2];
        final Throwable[] unexpected = new Throwable[1];
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        // Let the main thread know the helper thread has
                        // started. The race for the locks can start.
                        barrier.await();

                        // This statement will be blocked because T1 holds
                        // an exclusive lock on the row we want.
                        JDBC.assertDrainResults(ps2.executeQuery());
                    } catch (SQLException e) {
                        holder[0] = e;
                    } catch (Throwable t) {
                        unexpected[0] = t;
                    }
                }
            });
        t.start();

        // Wait until the helper thread has started. Once the call returns,
        // both threads are ready, and the race for the locks can start.
        barrier.await();

        // This statement will be blocked because T2 holds an exclusive lock
        // on the row we want. So now we have T1 waiting for T2, and T2 waiting
        // for T1, and one of the transactions should be terminated because of
        // the deadlock.
View Full Code Here

     * @throws java.lang.Exception
     */
    public void concurrentCompilationTest() throws Exception {
        // Create a barrier that can be used to synchronize the two threads
        // so they perform the meta-data compilation at the same time.
        final Barrier barrier = new Barrier(2);

        // Create a thread thread that attempts to compile meta-data queries.
        final DatabaseMetaData dmd = getDMD();
        final Exception[] exception = new Exception[1];
        Thread th = new Thread() {
View Full Code Here

        // This barrier lets the two threads wait for each other so that both
        // can obtain a read lock before going on trying to obtain the write
        // lock. If one thread goes ahead and obtains the write lock before the
        // other thread has obtained the read lock, we won't see a deadlock.
        final Barrier readLockBarrier = new Barrier(threads.length);

        // Exceptions seen by the threads.
        final List exceptions = Collections.synchronizedList(new ArrayList());

        // Start the two threads. Both should first obtain a read lock, and
        // when both have the read lock, they should try to lock the same row
        // exclusively. They'll be blocking each other, and we have a deadlock.
        for (int i = 0; i < threads.length; i++) {
            final Connection c = openDefaultConnection();
            c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            c.setAutoCommit(false);

            final PreparedStatement select = c.prepareStatement(
                    "select * from derby3980 where i = 456");
            final PreparedStatement update = c.prepareStatement(
                    "update derby3980 set i = 456 where i = 456");

            threads[i] = new Thread() {
                public void run() {
                    try {
                        JDBC.assertSingleValueResultSet(
                                select.executeQuery(), "456");

                        // Now we've got the read lock. Wait until all threads
                        // have it before attempting to get the write lock.
                        readLockBarrier.await();

                        // All threads have the read lock. Now all should try
                        // to update the row and thereby create a deadlock.
                        assertUpdateCount(update, 1);

View Full Code Here

     * meta-data calls are not already compiled.
     */
    public void concurrentCompilationTest() throws Exception {
        // Create a barrier that can be used to synchronize the two threads
        // so they perform the meta-data compilation at the same time.
        final Barrier barrier = new Barrier(2);

        // Create a thread thread that attempts to compile meta-data queries.
        final DatabaseMetaData dmd = getDMD();
        final Exception[] exception = new Exception[1];
        Thread th = new Thread() {
View Full Code Here

TOP

Related Classes of org.apache.derbyTesting.functionTests.util.Barrier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.