From torque-dev-return-11043-apmail-db-torque-dev-archive=db.apache.org@db.apache.org Fri May 4 13:15:56 2012 Return-Path: X-Original-To: apmail-db-torque-dev-archive@www.apache.org Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CDDB09A94 for ; Fri, 4 May 2012 13:15:56 +0000 (UTC) Received: (qmail 44864 invoked by uid 500); 4 May 2012 13:15:56 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 44787 invoked by uid 500); 4 May 2012 13:15:56 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 44778 invoked by uid 500); 4 May 2012 13:15:56 -0000 Received: (qmail 44770 invoked by uid 99); 4 May 2012 13:15:56 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 May 2012 13:15:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 May 2012 13:15:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E13002388865; Fri, 4 May 2012 13:15:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120504131531.E13002388865@eris.apache.org> 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