db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r1341830 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/store/KeepDisposableStatsPropertyTest.java functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java junit/IndexStatsUtil.java
Date Wed, 23 May 2012 11:11:03 GMT
Author: kristwaa
Date: Wed May 23 11:11:03 2012
New Revision: 1341830

URL: http://svn.apache.org/viewvc?rev=1341830&view=rev
Log:
DERBY-3790: Investigate if request for update statistics can be skipped for certain kind of
indexes, one instance may be unique indexes based on one column.

Minor test improvements: added/improved comments, skipped use of reduced row
count, removed unnecessary query execution.
Added a new method to wait for the old/current statistics to be replaced, and
then fetch the new statistics for the table.

Patch file: derby-3790-2a-minor_test_improvements.diff

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/KeepDisposableStatsPropertyTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/IndexStatsUtil.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/KeepDisposableStatsPropertyTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/KeepDisposableStatsPropertyTest.java?rev=1341830&r1=1341829&r2=1341830&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/KeepDisposableStatsPropertyTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/KeepDisposableStatsPropertyTest.java
Wed May 23 11:11:03 2012
@@ -31,9 +31,7 @@ import junit.framework.TestSuite;
 
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.IndexStatsUtil;
-import org.apache.derbyTesting.junit.JDBC;
 import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
-import org.apache.derbyTesting.junit.Utilities;
 
 /**
  * Tests that the debug property used to revert to the previous behavior for
@@ -67,7 +65,8 @@ public class KeepDisposableStatsProperty
     /** Runs the real test case. */
     private void assertOnSCUI(boolean keepDisposable)
             throws SQLException {
-        IndexStatsUtil stats = new IndexStatsUtil(openDefaultConnection());
+        IndexStatsUtil stats = new IndexStatsUtil(
+                openDefaultConnection(), 20*1000); // 20 seconds timeout
         // Create table.
         String TAB = "STAT_SCUI";
         dropTable(TAB);
@@ -126,11 +125,10 @@ public class KeepDisposableStatsProperty
         }
         commit();
         setAutoCommit(true);
-        JDBC.assertDrainResultsHasData(
-                stmt.executeQuery("select count(*) from " + TAB));
+        // Trigger the scheduling logic to get the istat daemon going
         prepareStatement("select * from " + TAB + " where id = ?"); 
-        Utilities.sleep(200);
-        IndexStatsUtil.IdxStats[] newStats = stats.getStatsTable(TAB);
+        IndexStatsUtil.IdxStats[] newStats =
+                stats.getNewStatsTable(TAB, oldStats);
         assertEquals(oldStats.length, newStats.length);
         for (int i=0; i < oldStats.length; i++) {
             assertEquals(keepDisposable, newStats[i].after(oldStats[i]));

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java?rev=1341830&r1=1341829&r2=1341830&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/helpers/DisposableIndexStatistics.java
Wed May 23 11:11:03 2012
@@ -39,6 +39,13 @@ import org.apache.derbyTesting.junit.Ind
  */
 public class DisposableIndexStatistics {
 
+    /**
+     * A row count currently chosen at will.
+     * <p>
+     * Note that if being used for testing the automatic istat daemon, the
+     * number of rows must be sufficiently high to trigger statistics creation,
+     * and likewise for the deltas when adding more rows to trigger an update.
+     */
     private static final int ROW_COUNT = 2000;
 
     private final Connection con;
@@ -151,10 +158,10 @@ public class DisposableIndexStatistics {
         ps.close();
         con.commit();
 
+        // Populate primary key table (has a multi-column primary key)
         ps = con.prepareStatement(
                 "insert into " + pktbl + " values (DEFAULT, ?)");
-        int reducedRowNumber = ROW_COUNT / 3;
-        for (int row = 0; row < reducedRowNumber; row++) {
+        for (int row = 0; row < ROW_COUNT; row++) {
             ps.setInt(1, row);
             ps.executeUpdate();
         }
@@ -162,10 +169,12 @@ public class DisposableIndexStatistics {
         con.commit();
 
         // Populate the main table.
+        // The modulo operations are used to vary the number of unique values
+        // in the columns and have been chosen at will.
         ps = con.prepareStatement(
                 "insert into " + tbl + " values (DEFAULT,?,?,?,?)");
         for (int row = 0; row < ROW_COUNT; row++) {
-            ps.setInt(1, (row % reducedRowNumber) +1);
+            ps.setInt(1, (row % ROW_COUNT) +1);
             ps.setInt(2, (row % 2000) +1);
             ps.setInt(3, (row % 19) +1);
             ps.setInt(4, row % 10);

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/IndexStatsUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/IndexStatsUtil.java?rev=1341830&r1=1341829&r2=1341830&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/IndexStatsUtil.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/IndexStatsUtil.java Wed
May 23 11:11:03 2012
@@ -28,9 +28,12 @@ import java.sql.SQLException;
 import java.sql.Timestamp;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import junit.framework.Assert;
 
@@ -242,6 +245,25 @@ public class IndexStatsUtil {
     }
 
     /**
+     * Waits for the current statistics to disappear and expects to fetch the
+     * same number of new statistics for the table.
+     *
+     * @param table the table to get statistics for
+     * @param currentStats the current statistics
+     * @return The new statistics.
+     * @throws SQLException if obtaining statistics fails
+     */
+    public IdxStats[] getNewStatsTable(String table, IdxStats[] currentStats)
+            throws SQLException {
+        if (timeout == 0) {
+            throw new IllegalStateException(
+                    "no timeout specified in the constructor");
+        }
+        awaitChange(currentStats, timeout);
+        return getStatsTable(table, currentStats.length);
+    }
+
+    /**
      * Obtains statistics for the specified index.
      *
      * @param index index name
@@ -449,6 +471,41 @@ public class IndexStatsUtil {
     }
 
     /**
+     * Waits until all given statistics entries have been changed, or until
+     * the call times out.
+     * <p>
+     * <em>NOTE</em>: The method is built on the assumption that the UUIDs of
+     * statistics objects aren't reused. That is, when statistics are updated,
+     * the old row in SYS.SYSSTATISTICS will be dropped and a new row will be
+     * inserted.
+     *
+     * @param current the statistics that must change / be replaced
+     * @param timeout maximum number of milliseconds to wait before giving up
+     * @throws SQLException if obtaining statistics fails
+     */
+    private void awaitChange(IdxStats[] current, long timeout)
+            throws SQLException {
+        Set oldStats = new HashSet(Arrays.asList(current));
+        Set newStats = null;
+        long start = System.currentTimeMillis();
+        // Make sure we run at least once.
+        while (System.currentTimeMillis() - start < timeout ||
+                newStats == null) {
+            newStats = new HashSet(Arrays.asList(getStats()));
+            newStats.retainAll(oldStats);
+            if (newStats.isEmpty()) {
+                return;
+            }
+            Utilities.sleep(200);
+        }
+        IdxStats[] outstanding = new IdxStats[newStats.size()];
+        newStats.toArray(outstanding);
+        Assert.fail(outstanding.length + " missing statistics changes " +
+                "(timeout=" + timeout + "ms): " +
+                buildStatString(outstanding, "<unchanged statistics>"));
+    }
+
+    /**
      * Immutable class representing index statistics.
      */
     public static final class IdxStats {



Mime
View raw message