db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1333939 - in /db/torque/torque4/trunk: torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java
Date Fri, 04 May 2012 13:15:31 GMT
Author: tfischer
Date: Fri May  4 13:15:31 2012
New Revision: 1333939

URL: http://svn.apache.org/viewvc?rev=1333939&view=rev
Log:
TORQUE-14: 
- make maximum cleverquantity configurable under key torque.idbroker.clever.quantity.max
- use 10000 as default value as maximum cleverquantity

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java?rev=1333939&r1=1333938&r2=1333939&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
Fri May  4 13:15:31 2012
@@ -108,6 +108,9 @@ public class IDBroker implements Runnabl
     /** The backup quantity which is used if an error occurs. */
     private static final double PREFETCH_BACKUP_QUANTITY = 10d;
 
+    /** The default maximum for the quantity determined by cleverquantity. */
+    private static final double CLEVERQUANTITY_MAX_DEFAULT = 10000d;
+
     /** the name of the database in which this IdBroker is running. */
     private String databaseName;
 
@@ -180,6 +183,10 @@ public class IDBroker implements Runnabl
         "idbroker.clever.quantity";
 
     /** property name */
+    private static final String DB_IDBROKER_CLEVERQUANTITY_MAX =
+        "idbroker.clever.quantity.max";
+
+    /** property name */
     private static final String DB_IDBROKER_PREFETCH =
         "idbroker.prefetch";
 
@@ -639,10 +646,26 @@ public class IDBroker implements Runnabl
                 }
                 // Increase quantity, so that hopefully this does not
                 // happen again.
-                float rate = getQuantity(tableName, null).floatValue()
-                    / timeLapse;
-                quantityStore.put(tableName, new BigDecimal(
-                    Math.ceil(SLEEP_PERIOD * rate * SAFETY_MARGIN)));
+                BigDecimal quantity = getQuantity(tableName, null);
+                float rate = quantity.floatValue() / timeLapse;
+                double newQuantity
+                        = Math.ceil(SLEEP_PERIOD * rate * SAFETY_MARGIN);
+                Double maxQuantity = configuration.getDouble(
+                        DB_IDBROKER_CLEVERQUANTITY_MAX,
+                        CLEVERQUANTITY_MAX_DEFAULT);
+                if (maxQuantity != null && newQuantity > maxQuantity)
+                {
+                    if (quantity.doubleValue() > maxQuantity)
+                    {
+                        // do not decrease quantity value;
+                        newQuantity = quantity.doubleValue();
+                    }
+                    else
+                    {
+                        newQuantity = maxQuantity;
+                    }
+                }
+                quantityStore.put(tableName, new BigDecimal(newQuantity));
             }
         }
         lastQueryTime.put(tableName, now);
@@ -914,7 +937,7 @@ public class IDBroker implements Runnabl
      * @param quantity An int with the value of the quantity.
      * @exception Exception Database error.
      */
-    private void updateQuantity(Connection con, String tableName,
+    protected void updateQuantity(Connection con, String tableName,
                                 BigDecimal quantity)
         throws Exception
     {
@@ -951,4 +974,16 @@ public class IDBroker implements Runnabl
             }
         }
     }
+
+    /**
+     * Returns the quantity value for a table.
+     *
+     * @param tableName the name of the table.
+     * @return the quantity value for the table, or null if the table is
+     *         (still) unknown.
+     */
+    protected BigDecimal getQuantity(String tableName)
+    {
+        return quantityStore.get(tableName);
+    }
 }

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java?rev=1333939&r1=1333938&r2=1333939&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java
(original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/oid/IDBrokerTest.java
Fri May  4 13:15:31 2012
@@ -19,8 +19,11 @@ package org.apache.torque.oid;
  * under the License.
  */
 
+import java.math.BigDecimal;
 import java.sql.Connection;
 
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.torque.BaseDatabaseTestCase;
 import org.apache.torque.Torque;
 import org.apache.torque.test.AuthorPeer;
@@ -39,6 +42,19 @@ public class IDBrokerTest extends BaseDa
     {
         super.setUp();
         idBroker = new IDBroker(Torque.getDatabase(Torque.getDefaultDB()));
+        Connection connection = Torque.getConnection();
+        idBroker.updateQuantity(
+                connection,
+                AuthorPeer.TABLE_NAME,
+                new BigDecimal("10"));
+        connection.close();
+    }
+
+    @Override
+    public void tearDown() throws Exception
+    {
+        super.setUp();
+        idBroker.stop();
     }
 
     /**
@@ -60,4 +76,61 @@ public class IDBrokerTest extends BaseDa
         // assuming quantity > 1
         assertTrue(nextId > id + 1);
     }
+
+    /**
+     * Tests that the quanitity value is initially set to 10.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testInitialQuantityValue() throws Exception
+    {
+        idBroker.start();
+        Connection connection = Torque.getConnection();
+        idBroker.getIdAsInt(connection, AuthorPeer.TABLE_NAME);
+        assertEquals(
+                new BigDecimal(10),
+                idBroker.getQuantity(AuthorPeer.TABLE_NAME));
+    }
+
+    /**
+     * Tests that  cleverQuantity increases the quantity value
+     * after an unscheduled retrieval.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testCleverQuantityValue() throws Exception
+    {
+        Configuration configuration = new PropertiesConfiguration();
+        configuration.setProperty("idbroker.clever.quantity", "true");
+        idBroker.start();
+        idBroker.setConfiguration(configuration);
+        Connection connection = Torque.getConnection();
+        for (int i = 1 ; i <= 11; ++i)
+        {
+            idBroker.getIdAsInt(connection, AuthorPeer.TABLE_NAME);
+        }
+        assertTrue(new BigDecimal("10").compareTo(
+                idBroker.getQuantity(AuthorPeer.TABLE_NAME)) < 0);
+    }
+
+    /**
+     * Tests that the quantity is not increased above cleverQuantityMax.
+     *
+     * @throws Exception if the test fails
+     */
+    public void testCleverQuantityValueMax() throws Exception
+    {
+        Configuration configuration = new PropertiesConfiguration();
+        configuration.setProperty("idbroker.clever.quantity", "true");
+        configuration.setProperty("idbroker.clever.quantity.max", "14");
+        idBroker.start();
+        idBroker.setConfiguration(configuration);
+        Connection connection = Torque.getConnection();
+        for (int i = 1 ; i <= 11; ++i)
+        {
+            idBroker.getIdAsInt(connection, AuthorPeer.TABLE_NAME);
+        }
+        assertTrue(new BigDecimal("14").compareTo(
+                idBroker.getQuantity(AuthorPeer.TABLE_NAME)) == 0);
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message