[jbosscache-commits] JBoss Cache SVN: r4736 - in core/trunk/src: test/java/org/jboss/cache/marshall and 1 other directory.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Nov 7 21:00:52 EST 2007


Author: manik.surtani at jboss.com
Date: 2007-11-07 21:00:52 -0500 (Wed, 07 Nov 2007)
New Revision: 4736

Added:
   core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller210.java
   core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller210Test.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
   core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
   core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
   core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java
   core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
Log:
JBCACHE-1211 - preliminary fix.

Modified: core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2007-11-08 01:59:17 UTC (rev 4735)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller200.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -24,16 +24,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.TreeSet;
+import java.util.*;
 
 /**
  * An enhanced marshaller for RPC calls between CacheImpl instances.
@@ -274,7 +265,7 @@
       else if (refMap.containsKey(o))// see if this object has been marshalled before.
       {
          out.writeByte(MAGICNUMBER_REF);
-         out.writeShort(refMap.get(o));
+         writeReference(out, refMap.get(o));
       }
       else if (o instanceof MethodCall)
       {
@@ -293,16 +284,14 @@
       }
       else if (o instanceof Fqn)
       {
-         int refId = createReference(o, refMap);
          out.writeByte(MAGICNUMBER_FQN);
-         out.writeShort(refId);
+         writeReference(out, createReference(o, refMap));
          marshallFqn((Fqn) o, out, refMap);
       }
       else if (o instanceof GlobalTransaction)
       {
-         int refId = createReference(o, refMap);
          out.writeByte(MAGICNUMBER_GTX);
-         out.writeShort(refId);
+         writeReference(out, createReference(o, refMap));
          marshallGlobalTransaction((GlobalTransaction) o, out, refMap);
       }
       else if (o instanceof IpAddress)
@@ -372,9 +361,8 @@
       }
       else if (o instanceof String)
       {
-         int refId = createReference(o, refMap);
          out.writeByte(MAGICNUMBER_STRING);
-         out.writeShort(refId);
+         writeReference(out, createReference(o, refMap));
          marshallString((String) o, out);
       }
       else if (o instanceof NodeDataMarker)
@@ -399,13 +387,12 @@
       }
       else if (o instanceof Serializable)
       {
-         int refId = createReference(o, refMap);
          if (log.isTraceEnabled())
          {
             log.trace("Warning: using object serialization for " + o.getClass());
          }
          out.writeByte(MAGICNUMBER_SERIALIZABLE);
-         out.writeShort(refId);
+         writeReference(out, createReference(o, refMap));
          out.writeObject(o);
       }
       else
@@ -533,14 +520,14 @@
          case MAGICNUMBER_NULL:
             return null;
          case MAGICNUMBER_REF:
-            reference = (int) in.readShort();
+            reference = readReference(in);
             if (!refMap.containsKey(reference))
             {
                throw new IOException("Unable to locate object reference " + reference + " in byte stream!");
             }
             return refMap.get(reference);
          case MAGICNUMBER_SERIALIZABLE:
-            reference = (int) in.readShort();
+            reference = readReference(in);
             retVal = in.readObject();
             refMap.put(reference, retVal);
             return retVal;
@@ -548,12 +535,12 @@
             retVal = unmarshallMethodCall(in, refMap);
             return retVal;
          case MAGICNUMBER_FQN:
-            reference = (int) in.readShort();
+            reference = readReference(in);
             retVal = unmarshallFqn(in, refMap);
             refMap.put(reference, retVal);
             return retVal;
          case MAGICNUMBER_GTX:
-            reference = (int) in.readShort();
+            reference = readReference(in);
             retVal = unmarshallGlobalTransaction(in, refMap);
             refMap.put(reference, retVal);
             return retVal;
@@ -586,7 +573,7 @@
          case MAGICNUMBER_SHORT:
             return in.readShort();
          case MAGICNUMBER_STRING:
-            reference = (int) in.readShort();
+            reference = readReference(in);
             retVal = unmarshallString(in);
             refMap.put(reference, retVal);
             return retVal;
@@ -767,4 +754,26 @@
          listToPopulate.add(unmarshallObject(in, refMap));
       }
    }
+
+   /**
+    * Reads a reference from a given stream.
+    * @param in the stream to read from
+    * @return an int representing a reference in RefMap.
+    * @throws IOException propagated from the OIS
+    */
+   protected int readReference(ObjectInputStream in) throws IOException
+   {
+      return in.readShort();
+   }
+
+   /**
+    * Writes a reference to a given object output stream.
+    * @param out the stream to write to
+    * @param reference the reference to write
+    * @throws java.io.IOException propagated from the OOS
+    */
+   protected void writeReference(ObjectOutputStream out, int reference) throws IOException
+   {
+      out.writeShort(reference);
+   }
 }

Added: core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller210.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller210.java	                        (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/CacheMarshaller210.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -0,0 +1,47 @@
+package org.jboss.cache.marshall;
+
+import org.jboss.cache.RegionManager;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * An evolution of {@link org.jboss.cache.marshall.CacheMarshaller200}, created to fix <a href="http://jira.jboss.org/jira/browse/JBCACHE-1211">JBCACHE-1211</a>.
+ *
+ * @author Manik Surtani
+ * @since 2.1.0
+ */
+public class CacheMarshaller210 extends CacheMarshaller200
+{
+   public CacheMarshaller210(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
+   {
+      super(manager, defaultInactive, useRegionBasedMarshalling);
+   }
+
+   /**
+    * This version of writeReference is written to solve JBCACHE-1211, where references are encoded as ints rather than shorts.
+    * @param out stream to write to
+    * @param reference reference to write
+    * @throws IOException propagated from OOS
+    * @see <a href="http://jira.jboss.org/jira/browse/JBCACHE-1211">JBCACHE-1211</a>
+    */
+   @Override
+   protected void writeReference(ObjectOutputStream out, int reference) throws IOException
+   {
+      out.writeInt(reference);
+   }
+
+   /**
+    * This version of readReference is written to solve JBCACHE-1211, where references are encoded as ints rather than shorts.
+    * @param in stream to read from
+    * @return reference
+    * @throws IOException propagated from OUS
+    * @see <a href="http://jira.jboss.org/jira/browse/JBCACHE-1211">JBCACHE-1211</a>
+    */
+   @Override
+   protected int readReference(ObjectInputStream in) throws IOException
+   {
+      return in.readInt();
+   }
+}

Modified: core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java	2007-11-08 01:59:17 UTC (rev 4735)
+++ core/trunk/src/main/java/org/jboss/cache/marshall/VersionAwareMarshaller.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -34,10 +34,11 @@
    
    private static final Log log = LogFactory.getLog(VersionAwareMarshaller.class);
    private static final int VERSION_200 = 20;
+   private static final int VERSION_210 = 21;
    
    private RegionManager manager;
    private boolean defaultInactive, useRegionBasedMarshalling;
-   AbstractMarshaller defaultMarshaller;
+   Marshaller defaultMarshaller;
    Map<Integer, Marshaller> marshallers = new HashMap<Integer, Marshaller>();
    private int versionInt;
 
@@ -52,14 +53,8 @@
 
       versionInt = toMinorVersionInt(version);
 
-      switch (versionInt)
-      {
-         case VERSION_200:
-         default:
-            defaultMarshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
-            marshallers.put(VERSION_200, defaultMarshaller);
-            break;
-      }
+      // this will cause the correct marshaller to be created and put in the map of marshallers
+      defaultMarshaller = getMarshaller(versionInt);
 
       if (log.isDebugEnabled())
       {
@@ -192,17 +187,28 @@
    Marshaller getMarshaller(int versionId)
    {
       Marshaller marshaller;
+      boolean knownVersion = false;
       switch (versionId)
       {
          case VERSION_200:
-         default:
-            marshaller = marshallers.get(VERSION_200);
+            marshaller = marshallers.get(VERSION_210);
             if (marshaller == null)
             {
                marshaller = new CacheMarshaller200(manager, defaultInactive, useRegionBasedMarshalling);
-               marshallers.put(VERSION_200, marshaller);
+               marshallers.put(VERSION_210, marshaller);
             }
             break;
+         case VERSION_210:
+            knownVersion = true;
+         default:
+            if (!knownVersion) log.warn("Unknown replication version String.  Falling back to the default marshaller, which is Version 2.1.0.");
+            marshaller = marshallers.get(VERSION_210);
+            if (marshaller == null)
+            {
+               marshaller = new CacheMarshaller210(manager, defaultInactive, useRegionBasedMarshalling);
+               marshallers.put(VERSION_210, marshaller);
+            }
+            break;
       }
       return marshaller;
    }

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2007-11-08 01:59:17 UTC (rev 4735)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller200Test.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -9,7 +9,6 @@
 import org.jboss.cache.Fqn;
 import org.jboss.cache.Region;
 import org.jboss.cache.RegionManager;
-import static org.testng.AssertJUnit.assertEquals;
 import org.testng.annotations.Test;
 
 import java.io.ByteArrayInputStream;
@@ -33,18 +32,6 @@
       expectedMarshallerClass = CacheMarshaller200.class;
    }
 
-   public void testHandle140Stream() throws Exception
-   {
-      VersionAwareMarshaller marshallerOld = new VersionAwareMarshaller(new RegionManager(), false, false, "1.4.0.GA");
-
-      // create a '140' stream.
-
-      byte[] buf = marshallerOld.objectToByteBuffer("hello");
-      Object unmarshalled = marshaller.objectFromByteBuffer(buf);
-
-      assertEquals("hello", unmarshalled);
-   }
-
    public void testRegionalisedStream() throws Exception
    {
       // need to test what's going on with 

Added: core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller210Test.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller210Test.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshaller210Test.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -0,0 +1,43 @@
+package org.jboss.cache.marshall;
+
+import org.jboss.cache.Fqn;
+import org.testng.annotations.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+ at Test (groups = {"functional"})
+public class CacheMarshaller210Test extends CacheMarshaller200Test
+{
+   public CacheMarshaller210Test()
+   {
+      currentVersion = "2.1.0.GA";
+      currentVersionShort = 21;
+      expectedMarshallerClass = CacheMarshaller210.class;
+   }
+
+   protected void doMapTest(int size) throws Exception
+   {
+      Map map = createMap(size);
+      Fqn fqn = Fqn.fromString("/my/stuff");
+      String key = "key";
+      MethodCall putMethod = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, fqn, key, map);
+      MethodCall replicateMethod = MethodCallFactory.create(MethodDeclarations.replicateMethod, putMethod);
+
+      byte[] buf = marshaller.objectToByteBuffer(replicateMethod);
+
+      assertMethodCallsEquals(replicateMethod, (MethodCall) marshaller.objectFromByteBuffer(buf));
+   }
+
+   protected Map createMap(int size)
+   {
+      Map map = new HashMap(size);
+      for (int i=0; i<size; i++) map.put("key-" + i, "value-" + i);
+      return map;
+   }
+
+   public void testLargeNumberOfObjectReferences() throws Exception
+   {
+      doMapTest(500000);
+   }
+}

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java	2007-11-08 01:59:17 UTC (rev 4735)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/CacheMarshallerTestBase.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -6,18 +6,17 @@
  */
 package org.jboss.cache.marshall;
 
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertNull;
-import static org.testng.AssertJUnit.assertTrue;
-
-import java.util.ArrayList;
-import java.util.List;
-
 import org.jboss.cache.Fqn;
 import org.jboss.cache.RegionManager;
+import static org.testng.AssertJUnit.*;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
 
+import java.util.ArrayList;
+import java.util.List;
+
+ at Test(groups = {"functional"})
 public abstract class CacheMarshallerTestBase
 {
    protected String currentVersion;
@@ -89,10 +88,11 @@
       assertEquals("Only one marshaller should be in the map by this stage", 1, marshaller.marshallers.size());
 
       assertEquals(expectedMarshallerClass, marshaller.getMarshaller(currentVersionShort).getClass());
-      assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(15).getClass());
-      assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(1).getClass());
-      assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(-1).getClass());
-      assertEquals(CacheMarshaller200.class, marshaller.getMarshaller(0).getClass());
+      assertEquals(expectedMarshallerClass, marshaller.getMarshaller(15).getClass());
+      assertEquals(expectedMarshallerClass, marshaller.getMarshaller(1).getClass());
+      assertEquals(expectedMarshallerClass, marshaller.getMarshaller(-1).getClass());
+      assertEquals(expectedMarshallerClass, marshaller.getMarshaller(0).getClass());
+      assertEquals(expectedMarshallerClass, marshaller.getMarshaller(20).getClass());
 
       assertEquals("One marshaller should be in the map by this stage", 1, marshaller.marshallers.size());
    }

Modified: core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java	2007-11-08 01:59:17 UTC (rev 4735)
+++ core/trunk/src/test/java/org/jboss/cache/marshall/VersionAwareMarshallerTest.java	2007-11-08 02:00:52 UTC (rev 4736)
@@ -6,13 +6,12 @@
  */
 package org.jboss.cache.marshall;
 
+import org.jboss.cache.RegionManager;
+import org.jboss.cache.Version;
 import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.Test;
 
 import java.io.ObjectInputStream;
-
-import org.jboss.cache.RegionManager;
-import org.jboss.cache.Version;
-import org.testng.annotations.Test;
 /**
  * Tests the enhanced treecache marshaller
  *
@@ -23,29 +22,32 @@
 {
    public void testMarshallerSelection()
    {
-      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "2.0.0.GA");
+      VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "2.1.0.GA");
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
+
+      marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "2.0.0.GA");
       assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.4.0.GA");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.5.0.GA");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.0.GA");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.0.SP2");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.3.1.GA");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.2.4.SP2");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
 
       marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "1.2.3");
-      assertEquals(CacheMarshaller200.class, marshaller.defaultMarshaller.getClass());
+      assertEquals(CacheMarshaller210.class, marshaller.defaultMarshaller.getClass());
    }
 
    public void testVersionHeaderDefaultCurrent() throws Exception




More information about the jbosscache-commits mailing list