[hornetq-commits] JBoss hornetq SVN: r9191 - in trunk: tests/src/org/hornetq/tests/unit/core/journal/impl and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Apr 30 14:39:44 EDT 2010


Author: clebert.suconic at jboss.com
Date: 2010-04-30 14:39:43 -0400 (Fri, 30 Apr 2010)
New Revision: 9191

Modified:
   trunk/src/main/org/hornetq/core/journal/impl/JournalImpl.java
   trunk/tests/src/org/hornetq/tests/unit/core/journal/impl/JournalImplTestUnit.java
Log:
https://jira.jboss.org/jira/browse/HORNETQ-318 - Longs on FileIDs

Modified: trunk/src/main/org/hornetq/core/journal/impl/JournalImpl.java
===================================================================
--- trunk/src/main/org/hornetq/core/journal/impl/JournalImpl.java	2010-04-30 14:56:42 UTC (rev 9190)
+++ trunk/src/main/org/hornetq/core/journal/impl/JournalImpl.java	2010-04-30 18:39:43 UTC (rev 9191)
@@ -43,6 +43,7 @@
 
 import org.hornetq.api.core.HornetQBuffer;
 import org.hornetq.api.core.HornetQBuffers;
+import org.hornetq.api.core.HornetQException;
 import org.hornetq.api.core.Pair;
 import org.hornetq.core.journal.EncodingSupport;
 import org.hornetq.core.journal.IOAsyncTask;
@@ -2763,26 +2764,31 @@
          SequentialFile file = fileFactory.createSequentialFile(fileName, maxAIO);
 
          file.open(1, false);
-
-         long fileID = readFileHeader(file);
-
-         if (nextFileID.get() < fileID)
+         
+         try
          {
-            nextFileID.set(fileID);
+            long fileID = readFileHeader(file);
+   
+            if (nextFileID.get() < fileID)
+            {
+               nextFileID.set(fileID);
+            }
+   
+            long fileNameID = getFileNameID(fileName);
+   
+            // The compactor could create a fileName but use a previously assigned ID.
+            // Because of that we need to take both parts into account
+            if (nextFileID.get() < fileNameID)
+            {
+               nextFileID.set(fileNameID);
+            }
+   
+            orderedFiles.add(new JournalFileImpl(file, fileID));
          }
-
-         int fileNameID = getFileNameID(fileName);
-
-         // The compactor could create a fileName but use a previously assigned ID.
-         // Because of that we need to take both parts into account
-         if (nextFileID.get() < fileNameID)
+         finally
          {
-            nextFileID.set(fileNameID);
+            file.close();
          }
-
-         orderedFiles.add(new JournalFileImpl(file, fileID));
-
-         file.close();
       }
 
       // Now order them by ordering id - we can't use the file name for ordering
@@ -2806,8 +2812,19 @@
 
       int journalVersion = bb.getInt();
       
-      int userVersion = bb.getInt();
+      if (journalVersion != FORMAT_VERSION)
+      {
+         throw new HornetQException(HornetQException.IO_ERROR, "Journal files version mismatch");
+      }
       
+      
+      int readUserVersion = bb.getInt();
+      
+      if (readUserVersion != userVersion)
+      {
+         throw new HornetQException(HornetQException.IO_ERROR, "Journal data belong to a different version");
+      }
+      
       long fileID = bb.getLong();
 
       fileFactory.releaseBuffer(bb);
@@ -2821,7 +2838,7 @@
     * @param sequentialFile
     * @throws Exception
     */
-   static int initFileHeader(final SequentialFileFactory fileFactory, final SequentialFile sequentialFile, final int userVersion, final long fileID) throws Exception
+   public static int initFileHeader(final SequentialFileFactory fileFactory, final SequentialFile sequentialFile, final int userVersion, final long fileID) throws Exception
    {
       // We don't need to release buffers while writing.
       ByteBuffer bb = fileFactory.newBuffer(JournalImpl.SIZE_HEADER);
@@ -2846,7 +2863,7 @@
     * @param userVersion
     * @param fileID
     */
-   static void writeHeader(HornetQBuffer buffer, final int userVersion, final long fileID)
+   public static void writeHeader(HornetQBuffer buffer, final int userVersion, final long fileID)
    {
       buffer.writeInt(FORMAT_VERSION);
       
@@ -2953,11 +2970,11 @@
    }
 
    /** Get the ID part of the name */
-   private int getFileNameID(final String fileName)
+   private long getFileNameID(final String fileName)
    {
       try
       {
-         return Integer.parseInt(fileName.substring(filePrefix.length() + 1, fileName.indexOf('.')));
+         return Long.parseLong(fileName.substring(filePrefix.length() + 1, fileName.indexOf('.')));
       }
       catch (Throwable e)
       {

Modified: trunk/tests/src/org/hornetq/tests/unit/core/journal/impl/JournalImplTestUnit.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/unit/core/journal/impl/JournalImplTestUnit.java	2010-04-30 14:56:42 UTC (rev 9190)
+++ trunk/tests/src/org/hornetq/tests/unit/core/journal/impl/JournalImplTestUnit.java	2010-04-30 18:39:43 UTC (rev 9191)
@@ -13,12 +13,15 @@
 
 package org.hornetq.tests.unit.core.journal.impl;
 
+import java.nio.ByteBuffer;
 import java.util.List;
 
 import junit.framework.Assert;
 
+import org.hornetq.api.core.HornetQException;
 import org.hornetq.core.journal.EncodingSupport;
 import org.hornetq.core.journal.RecordInfo;
+import org.hornetq.core.journal.SequentialFile;
 import org.hornetq.core.journal.impl.JournalImpl;
 import org.hornetq.core.logging.Logger;
 import org.hornetq.tests.unit.core.journal.impl.fakes.SimpleEncoding;
@@ -186,7 +189,114 @@
       }
 
    }
+   
+   
+   public void testVersionCheck() throws Exception
+   {
+      setup(10, 10 * 1024, true);
+      createJournal();
+      startJournal();
+      load();
+      
+      stopJournal();
+      
+      fileFactory.start();
 
+      List<String> files = fileFactory.listFiles(fileExtension);
+      
+      for (String fileStr : files)
+      {
+         
+         SequentialFile file = fileFactory.createSequentialFile(fileStr, 1);
+         
+         ByteBuffer buffer = fileFactory.newBuffer(JournalImpl.SIZE_HEADER);
+         
+         for (int i = 0 ; i < JournalImpl.SIZE_HEADER; i++)
+         {
+            buffer.put(Byte.MAX_VALUE);
+         }
+         
+         buffer.rewind();
+         
+         file.open();
+         
+         file.position(0);
+         
+         file.writeDirect(buffer, sync);
+         
+         file.close();
+      }
+      
+      fileFactory.stop();
+
+      startJournal();
+      
+      boolean exceptionHappened = false;
+      try
+      {
+         load();
+      }
+      catch (HornetQException e)
+      {
+         exceptionHappened = true;
+         assertEquals(HornetQException.IO_ERROR, e.getCode());
+      }
+      
+      assertTrue("Exception was expected", exceptionHappened);
+      stopJournal();
+      
+      
+   }
+
+   // Validates the if the journal will work when the IDs are over MaxInt
+   public void testMaxInt() throws Exception
+   {
+      setup(10, 10 * 1024, true);
+      createJournal();
+      startJournal();
+      load();
+      
+      stopJournal();
+      
+      fileFactory.start();
+
+      List<String> files = fileFactory.listFiles(fileExtension);
+      
+      long fileID = Integer.MAX_VALUE;
+      for (String fileStr : files)
+      {
+         SequentialFile file = fileFactory.createSequentialFile(fileStr, 1);
+         
+         file.open();
+         
+         JournalImpl.initFileHeader(fileFactory, file, journal.getUserVersion(), fileID++);
+
+         file.close();
+      }
+      
+      fileFactory.stop();
+
+      startJournal();
+      
+      load();
+      
+      for (long i = 0 ; i < 100; i++)
+      {
+         add(i);
+
+         stopJournal();
+         
+         startJournal();
+
+         loadAndCheck();
+      }
+      
+      stopJournal();
+      
+   }
+
+
+
    public void testFilesImmediatelyAfterload() throws Exception
    {
       try



More information about the hornetq-commits mailing list