[jboss-cvs] JBoss Messaging SVN: r5039 - in trunk: tests/src/org/jboss/messaging/tests/unit/util and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Sep 29 10:51:00 EDT 2008


Author: clebert.suconic at jboss.com
Date: 2008-09-29 10:51:00 -0400 (Mon, 29 Sep 2008)
New Revision: 5039

Modified:
   trunk/src/main/org/jboss/messaging/util/TimeAndCounterIDGenerator.java
   trunk/tests/src/org/jboss/messaging/tests/unit/util/TimeAndCounterIDGeneratorTest.java
Log:
Tweak on IDGenerator

Modified: trunk/src/main/org/jboss/messaging/util/TimeAndCounterIDGenerator.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/TimeAndCounterIDGenerator.java	2008-09-29 13:52:51 UTC (rev 5038)
+++ trunk/src/main/org/jboss/messaging/util/TimeAndCounterIDGenerator.java	2008-09-29 14:51:00 UTC (rev 5039)
@@ -27,7 +27,7 @@
 /**
  * A TimeAndCounterIDGenerator
  * <p>
- * Note: This sequence generator is valid as long as you generate less than 268435455 (fffffff) IDs per millisecond
+ * Note: This sequence generator is valid as long as you generate less than 268435455 (fffffff) IDs per 250 millisecond
  * </p>
  * <p>
  * (what is impossible at this point, This class alone will probably take a few seconds to generate this many IDs)
@@ -43,14 +43,9 @@
    /**
     * Bits to move the date accordingly to MASK_TIME
     */
-   private static final int BITS_TO_MOVE = 28;
+   private static final int BITS_TO_MOVE = 20;
 
-   // We take one bit out, as we don't want negative numbers
-   // With 4 bytes + 4 bits, we would minimize the possibility of duplicate IDs.
-   // The date portion would be repeated on every 397 days
-   //
-   // 4 bytes + 4 bits without the signal bit
-   public static final long MASK_TIME = 0x7ffffffffl;
+    public static final long MASK_TIME = 0xFFFFFFFFF00l;
 
    // Attributes ----------------------------------------------------
 
@@ -73,22 +68,7 @@
 
    public long generateID()
    {
-
-      long retValue = counter.incrementAndGet();
-
-      // The probability of a negative is very low.
-      // The server has to be started at the exact millisecond (or very close) to when
-      // System.currentTimeMillis() == **7ffffffffl or **fffffffffl(what would
-      // happen every 397 days and few hours), for instance:
-      // (117FFFFFFFF = Sat Feb 09 15:00:42 GMT-06:00 2008).
-      // But I still wanted to verify this for correctness.
-      while (retValue < 0)
-      {
-         refresh();
-         retValue = counter.incrementAndGet();
-      }
-
-      return retValue;
+      return counter.incrementAndGet();
    }
 
    public long getCurrentID()
@@ -111,22 +91,11 @@
 
    public synchronized void refresh()
    {
-      long oldTm = counter.get() >> BITS_TO_MOVE;
+      long oldTm = tmMark;
       long newTm = newTM();
 
-      // To avoid quick restarts on testcases.
-      // In a real scenario this will never happen, as refresh is called only on constructor or when the first bit on
-      // the counter explodes
-      // And that would happen only at one specific millisecond every 368 days, and that would never hit this case
       while (newTm == oldTm)
       {
-         try
-         {
-            Thread.sleep(20);
-         }
-         catch (InterruptedException e)
-         {
-         }
          newTm = newTM();
       }
       tmMark = newTm;

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/util/TimeAndCounterIDGeneratorTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/util/TimeAndCounterIDGeneratorTest.java	2008-09-29 13:52:51 UTC (rev 5038)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/util/TimeAndCounterIDGeneratorTest.java	2008-09-29 14:51:00 UTC (rev 5039)
@@ -22,6 +22,7 @@
 
 package org.jboss.messaging.tests.unit.util;
 
+import java.util.Date;
 import java.util.concurrent.CountDownLatch;
 
 import org.jboss.messaging.tests.util.UnitTestCase;
@@ -49,17 +50,12 @@
    public void testCalculation()
    {
       TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
-      long max = 100000;
+      long max = 11000;
 
       long lastNr = 0;
 
       for (long i = 0; i < max; i++)
       {
-         if (i % 1 == 1000)
-         {
-            seq.refresh();
-         }
-
          long seqNr = seq.generateID();
 
          assertTrue("The sequence generator should aways generate crescent numbers", seqNr > lastNr);
@@ -69,20 +65,33 @@
 
    }
 
-   public void testCalculationOnMultiThread() throws Throwable
+   public void testCalculationRefresh()
    {
+      TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
+      
+      long id1 = seq.generateID();
+      assertEquals(1, id1 & 0xffff);
+      assertEquals(2, seq.generateID() & 0xffff);
+      
+      seq.refresh();
+      
+      long id2 = seq.generateID();
+      
+      assertTrue(id2 > id1);
+      
+      assertEquals(1, id2 & 0xffff);
+      
+      
 
-      for (int i = 0; i < 10; i++)
-      {
-         internaltestCalculationOnMultiThread();
-      }
    }
 
-   public void internaltestCalculationOnMultiThread() throws Throwable
+   public void testCalculationOnMultiThread() throws Throwable
    {
       final ConcurrentHashSet<Long> hashSet = new ConcurrentHashSet<Long>();
 
       final TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
+      
+      System.out.println("Time = " + hex(System.currentTimeMillis()) + ", " + seq);
 
       seq.setInternalID(0xfffffffl - 1);
 
@@ -152,26 +161,6 @@
 
    }
 
-   public void testEdgeCaseOnDate()
-   {
-      long date = 0x117FFFFFFFFl;
-
-      final TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
-
-      seq.setInternalDate(date);
-
-      seq.setInternalID(0xfffffffl);
-
-      long newID = seq.generateID();
-
-      assertTrue("should be a positive number", newID > 0);
-
-      assertEquals("Counter part on ID should be 0x0000001, but it was " + (hex(newID & 0xfffffffl)),
-                   1,
-                   newID & 0xfffffffl);
-
-   }
-
    private static String hex(final long value)
    {
       return String.format("%1$X", value);




More information about the jboss-cvs-commits mailing list