You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2012/08/31 03:37:20 UTC

svn commit: r1379249 - /db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java

Author: tfischer
Date: Fri Aug 31 01:37:19 2012
New Revision: 1379249

URL: http://svn.apache.org/viewvc?rev=1379249&view=rev
Log:
- Fix border case in cleverQuantity where ids where fetched so fast that measured time lapse was 0
- improve logging

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.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=1379249&r1=1379248&r2=1379249&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 Aug 31 01:37:19 2012
@@ -38,11 +38,6 @@ import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
 import org.apache.torque.util.Transaction;
 
-//!!
-// NOTE:
-// It would be nice to decouple this from
-// Torque. This is a great stand-alone utility.
-
 /**
  * This method of ID generation is used to ensure that code is
  * more database independent.  For example, MySQL has an auto-increment
@@ -152,7 +147,7 @@ public class IDBroker implements Runnabl
     /**
      * Amount of time for the thread to sleep
      */
-    private static final int SLEEP_PERIOD = 60000;
+    private static final long SLEEP_PERIOD = 60000;
 
     /**
      * The safety Margin
@@ -447,11 +442,13 @@ public class IDBroker implements Runnabl
         {
             if (availableIds == null)
             {
-                log.debug("Forced id retrieval - no available list");
+                log.debug("Forced id retrieval - no available list for table "
+                        + tableName);
             }
             else
             {
-                log.debug("Forced id retrieval - " + availableIds.size());
+                log.debug("Forced id retrieval - " + availableIds.size()
+                        + " ids still available for table " + tableName);
             }
             storeIDs(tableName, true, connection);
             availableIds = ids.get(tableName);
@@ -559,7 +556,8 @@ public class IDBroker implements Runnabl
                         storeIDs(tableName, false, null);
                         if (log.isDebugEnabled())
                         {
-                            log.debug("Retrieved more ids for table: " + tableName);
+                            log.debug("Retrieved more ids for table: "
+                                + tableName);
                         }
                     }
                     catch (Exception exc)
@@ -621,20 +619,35 @@ public class IDBroker implements Runnabl
         {
             long thenLong = lastTime.getTime();
             long nowLong = now.getTime();
-            int timeLapse = (int) (nowLong - thenLong);
-            if (timeLapse < SLEEP_PERIOD && timeLapse > 0)
+            long timeLapse = nowLong - thenLong;
+            log.debug("checkTiming(): sleep time was "
+                + timeLapse + " milliseconds for table " + tableName);
+            if (timeLapse < SLEEP_PERIOD)
             {
-                if (log.isDebugEnabled())
-                {
-                    log.debug("Unscheduled retrieval of more ids for table: "
-                            + tableName);
-                }
+                log.debug("checkTiming(): Unscheduled retrieval of ids "
+                            + "for table " + tableName);
                 // Increase quantity, so that hopefully this does not
                 // happen again.
                 BigDecimal quantity = getQuantity(tableName, null);
-                float rate = quantity.floatValue() / timeLapse;
-                double newQuantity
-                        = Math.ceil(SLEEP_PERIOD * rate * SAFETY_MARGIN);
+                double newQuantity;
+                if (timeLapse > 0)
+                {
+                    float rate = quantity.floatValue() / timeLapse;
+                    newQuantity
+                            = Math.ceil(SLEEP_PERIOD * rate * SAFETY_MARGIN);
+                    log.debug("checkTiming(): calculated new quantity "
+                            + newQuantity + " from rate " + rate);
+                }
+                else
+                {
+                    // time lapse is so small that it was not measurable
+                    // use factor 2
+                    newQuantity = quantity.floatValue() * 2;
+                    log.debug("checkTiming(): calculated new quantity "
+                            + newQuantity
+                            + " from double the old quantity (time lapse 0)");
+                }
+
                 Double maxQuantity = configuration.getDouble(
                         DB_IDBROKER_CLEVERQUANTITY_MAX,
                         CLEVERQUANTITY_MAX_DEFAULT);
@@ -651,6 +664,8 @@ public class IDBroker implements Runnabl
                     }
                 }
                 quantityStore.put(tableName, new BigDecimal(newQuantity));
+                log.debug("checkTiming(): new quantity " + newQuantity
+                        + " stored in quantity store (not in db)");
             }
         }
         lastQueryTime.put(tableName, now);
@@ -671,6 +686,7 @@ public class IDBroker implements Runnabl
                           Connection connection)
             throws TorqueException
     {
+        log.debug("storeIDs(): Start retrieving ids from database.");
         BigDecimal nextId = null;
         BigDecimal quantity = null;
 
@@ -691,6 +707,11 @@ public class IDBroker implements Runnabl
             if (useNewConnection)
             {
                 connection = Transaction.begin(databaseName);
+                if (log.isTraceEnabled())
+                {
+                    log.trace("storeIDs(): fetched connection, "
+                            + "started transaction.");
+                }
             }
 
             // Write the current value of quantity of keys to grab
@@ -713,6 +734,11 @@ public class IDBroker implements Runnabl
             if (useNewConnection)
             {
                 Transaction.commit(connection);
+                if (log.isTraceEnabled())
+                {
+                    log.trace("storeIDs(): Transaction committed, "
+                            + "connection returned");
+                }
             }
         }
         catch (TorqueException e)
@@ -771,6 +797,8 @@ public class IDBroker implements Runnabl
         }
         else
         {
+            log.debug("getQuantity() : start fetch quantity for table "
+                    + tableName + " from database");
             boolean useNewConnection = (connection == null) || (configuration
                     .getBoolean(DB_IDBROKER_USENEWCONNECTION, true));
             try
@@ -778,6 +806,11 @@ public class IDBroker implements Runnabl
                 if (useNewConnection)
                 {
                     connection = Transaction.begin(databaseName);
+                    if (log.isTraceEnabled())
+                    {
+                        log.trace("getQuantity(): connection fetched, "
+                                + "transaction started");
+                    }
                 }
 
                 // Read the row from the ID_TABLE.
@@ -786,10 +819,17 @@ public class IDBroker implements Runnabl
                 // QUANTITY column.
                 quantity = results[1];
                 quantityStore.put(tableName, quantity);
+                log.debug("getQuantity() : quantity fetched for table "
+                        + tableName + ", result is " + quantity);
                 if (useNewConnection)
                 {
                     Transaction.commit(connection);
                     connection = null;
+                    if (log.isTraceEnabled())
+                    {
+                        log.trace("getQuantity(): transaction committed, "
+                                + "connection returned");
+                    }
                 }
             }
             catch (Exception e)
@@ -964,6 +1004,8 @@ public class IDBroker implements Runnabl
                                 BigDecimal quantity)
             throws TorqueException
     {
+        log.debug("updateQuantity(): start for table " + tableName
+                + " and quantity " + quantity);
         StringBuilder stmt = new StringBuilder();
         stmt.append("UPDATE ")
             .append(ID_TABLE)
@@ -981,13 +1023,14 @@ public class IDBroker implements Runnabl
 
         if (log.isDebugEnabled())
         {
-            log.debug("updateQuantity: " + stmt.toString());
+            log.debug("updateQuantity(): " + stmt.toString());
         }
 
         try
         {
             statement = con.createStatement();
             statement.executeUpdate(stmt.toString());
+            log.debug("updateQuantity(): quantity written, end");
         }
         catch (SQLException e)
         {



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