You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2013/07/19 23:36:15 UTC

svn commit: r1505029 - in /qpid/trunk/qpid/java: bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/ bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/ bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgr...

Author: rgodfrey
Date: Fri Jul 19 21:36:14 2013
New Revision: 1505029

URL: http://svn.apache.org/r1505029
Log:
QPID-5009 : Update broker store to revision 7

Added:
    qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgradeFrom6To7.java
    qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderFailOnNewerVersionTest.java
      - copied, changed from r1504922, qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderTest.java
    qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/
    qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/test-store/
    qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/test-store/00000000.jdb   (with props)
Modified:
    qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/AbstractBDBMessageStore.java
    qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/Upgrader.java
    qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/store/AbstractJDBCMessageStore.java

Modified: qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/AbstractBDBMessageStore.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/AbstractBDBMessageStore.java?rev=1505029&r1=1505028&r2=1505029&view=diff
==============================================================================
--- qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/AbstractBDBMessageStore.java (original)
+++ qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/AbstractBDBMessageStore.java Fri Jul 19 21:36:14 2013
@@ -66,7 +66,7 @@ public abstract class AbstractBDBMessage
 
     private static final int LOCK_RETRY_ATTEMPTS = 5;
 
-    public static final int VERSION = 6;
+    public static final int VERSION = 7;
 
     private static final Map<String, String> ENVCONFIG_DEFAULTS = Collections.unmodifiableMap(new HashMap<String, String>()
     {{

Added: qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgradeFrom6To7.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgradeFrom6To7.java?rev=1505029&view=auto
==============================================================================
--- qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgradeFrom6To7.java (added)
+++ qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgradeFrom6To7.java Fri Jul 19 21:36:14 2013
@@ -0,0 +1,66 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.server.store.berkeleydb.upgrade;
+
+import com.sleepycat.bind.tuple.ByteBinding;
+import com.sleepycat.bind.tuple.IntegerBinding;
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseConfig;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.OperationStatus;
+import org.apache.qpid.AMQStoreException;
+
+public class UpgradeFrom6To7 extends AbstractStoreUpgrade
+{
+
+    private static final int DEFAULT_CONFIG_VERSION = 0;
+
+    @Override
+    public void performUpgrade(Environment environment, UpgradeInteractionHandler handler, String virtualHostName)
+            throws DatabaseException, AMQStoreException
+    {
+        reportStarting(environment, 6);
+        DatabaseConfig dbConfig = new DatabaseConfig();
+        dbConfig.setTransactional(true);
+        dbConfig.setAllowCreate(true);
+
+        Database versionDb = environment.openDatabase(null, "CONFIG_VERSION", dbConfig);
+
+        if(versionDb.count() == 0L)
+        {
+            DatabaseEntry key = new DatabaseEntry();
+            DatabaseEntry value = new DatabaseEntry();
+            IntegerBinding.intToEntry(DEFAULT_CONFIG_VERSION, value);
+            ByteBinding.byteToEntry((byte) 0, key);
+            OperationStatus status = versionDb.put(null, key, value);
+            if (status != OperationStatus.SUCCESS)
+            {
+                throw new AMQStoreException("Error initialising config version: " + status);
+            }
+        }
+
+        versionDb.close();
+
+        reportFinished(environment, 7);
+    }
+}

Modified: qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/Upgrader.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/Upgrader.java?rev=1505029&r1=1505028&r2=1505029&view=diff
==============================================================================
--- qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/Upgrader.java (original)
+++ qpid/trunk/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/upgrade/Upgrader.java Fri Jul 19 21:36:14 2013
@@ -20,6 +20,7 @@
  */
 package org.apache.qpid.server.store.berkeleydb.upgrade;
 
+import com.sleepycat.je.Cursor;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 
@@ -73,7 +74,12 @@ public class Upgrader
             }
 
             int version = getSourceVersion(versionDb);
-
+            if(version > AbstractBDBMessageStore.VERSION)
+            {
+                throw new AMQStoreException("Database version " + version
+                                            + " is higher than the most recent known version: "
+                                            + AbstractBDBMessageStore.VERSION);
+            }
             performUpgradeFromVersion(version, versionDb);
         }
         finally
@@ -87,19 +93,34 @@ public class Upgrader
 
     int getSourceVersion(Database versionDb)
     {
-        int version = AbstractBDBMessageStore.VERSION + 1;
-        OperationStatus result;
+        int version = -1;
 
-        do
+        Cursor cursor = null;
+        try
         {
-            version--;
+            cursor = versionDb.openCursor(null, null);
+
             DatabaseEntry key = new DatabaseEntry();
-            IntegerBinding.intToEntry(version, key);
             DatabaseEntry value = new DatabaseEntry();
 
-            result = versionDb.get(null, key, value, LockMode.READ_COMMITTED);
+            while(cursor.getNext(key, value, null) == OperationStatus.SUCCESS)
+            {
+                int ver = IntegerBinding.entryToInt(key);
+                if(ver > version)
+                {
+                    version = ver;
+                }
+            }
+        }
+        finally
+        {
+            if(cursor != null)
+            {
+                cursor.close();
+            }
         }
-        while(result == OperationStatus.NOTFOUND);
+
+
         return version;
     }
 

Copied: qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderFailOnNewerVersionTest.java (from r1504922, qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderTest.java)
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderFailOnNewerVersionTest.java?p2=qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderFailOnNewerVersionTest.java&p1=qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderTest.java&r1=1504922&r2=1505029&rev=1505029&view=diff
==============================================================================
--- qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderTest.java (original)
+++ qpid/trunk/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/UpgraderFailOnNewerVersionTest.java Fri Jul 19 21:36:14 2013
@@ -20,30 +20,31 @@
  */
 package org.apache.qpid.server.store.berkeleydb.upgrade;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.qpid.server.store.berkeleydb.AbstractBDBMessageStore;
-import org.apache.qpid.server.store.berkeleydb.tuple.ContentBinding;
-
 import com.sleepycat.bind.tuple.IntegerBinding;
 import com.sleepycat.bind.tuple.LongBinding;
 import com.sleepycat.je.Cursor;
 import com.sleepycat.je.Database;
 import com.sleepycat.je.DatabaseConfig;
 import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
 import com.sleepycat.je.OperationStatus;
 import com.sleepycat.je.Transaction;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.qpid.AMQStoreException;
+import org.apache.qpid.server.store.berkeleydb.AbstractBDBMessageStore;
+import org.apache.qpid.server.store.berkeleydb.tuple.ContentBinding;
 
-public class UpgraderTest extends AbstractUpgradeTestCase
+public class UpgraderFailOnNewerVersionTest extends AbstractUpgradeTestCase
 {
     private Upgrader _upgrader;
 
     @Override
     protected String getStoreDirectoryName()
     {
-        return "bdbstore-v4";
+        return "bdbstore-v999";
     }
 
     @Override
@@ -92,47 +93,17 @@ public class UpgraderTest extends Abstra
 
     public void testUpgrade() throws Exception
     {
-        assertEquals("Unexpected store version", -1, getStoreVersion());
-        _upgrader.upgradeIfNecessary();
-        assertEquals("Unexpected store version", AbstractBDBMessageStore.VERSION, getStoreVersion());
-        assertContent();
-    }
-
-    public void testEmptyDatabaseUpgradeDoesNothing() throws Exception
-    {
-        File nonExistentStoreLocation = new File(TMP_FOLDER, getName());
-        deleteDirectoryIfExists(nonExistentStoreLocation);
-
-        nonExistentStoreLocation.mkdir();
-        _environment = createEnvironment(nonExistentStoreLocation);
-        _upgrader = new Upgrader(_environment, getVirtualHostName());
-        _upgrader.upgradeIfNecessary();
-
-        List<String> databaseNames = _environment.getDatabaseNames();
-        List<String> expectedDatabases = new ArrayList<String>();
-        expectedDatabases.add(Upgrader.VERSION_DB_NAME);
-        assertEquals("Expectedonly VERSION table in initially empty store after upgrade: ", expectedDatabases, databaseNames);
-        assertEquals("Unexpected store version", AbstractBDBMessageStore.VERSION, getStoreVersion());
-
-        nonExistentStoreLocation.delete();
-    }
-
-    private void assertContent()
-    {
-        final ContentBinding contentBinding = ContentBinding.getInstance();
-        CursorOperation contentCursorOperation = new CursorOperation()
+        assertEquals("Unexpected store version", 999, getStoreVersion());
+        try
         {
-
-            @Override
-            public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key,
-                    DatabaseEntry value)
-            {
-                long id = LongBinding.entryToLong(key);
-                assertTrue("Unexpected id", id > 0);
-                byte[] content = contentBinding.entryToObject(value);
-                assertNotNull("Unexpected content", content);
-            }
-        };
-        new DatabaseTemplate(_environment, "MESSAGE_CONTENT", null).run(contentCursorOperation);
+            _upgrader.upgradeIfNecessary();
+            fail("Store should not be able to be upgraded");
+        }
+        catch(AMQStoreException ex)
+        {
+            assertEquals("Incorrect exception thrown", "Database version 999 is higher than the most recent known version: "
+                                                        + AbstractBDBMessageStore.VERSION, ex.getMessage());
+        }
     }
+
 }

Added: qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/test-store/00000000.jdb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/test-store/00000000.jdb?rev=1505029&view=auto
==============================================================================
Binary file - no diff available.

Propchange: qpid/trunk/qpid/java/bdbstore/src/test/resources/upgrade/bdbstore-v999/test-store/00000000.jdb
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/store/AbstractJDBCMessageStore.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/store/AbstractJDBCMessageStore.java?rev=1505029&r1=1505028&r2=1505029&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/store/AbstractJDBCMessageStore.java (original)
+++ qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/store/AbstractJDBCMessageStore.java Fri Jul 19 21:36:14 2013
@@ -74,13 +74,16 @@ abstract public class AbstractJDBCMessag
     public static String[] ALL_TABLES = new String[] { DB_VERSION_TABLE_NAME, LINKS_TABLE_NAME, BRIDGES_TABLE_NAME, XID_ACTIONS_TABLE_NAME,
         XID_TABLE_NAME, QUEUE_ENTRY_TABLE_NAME, MESSAGE_CONTENT_TABLE_NAME, META_DATA_TABLE_NAME, CONFIGURED_OBJECTS_TABLE_NAME, CONFIGURATION_VERSION_TABLE_NAME };
 
-    private static final int DB_VERSION = 6;
+    private static final int DB_VERSION = 7;
 
     private final AtomicLong _messageId = new AtomicLong(0);
     private AtomicBoolean _closed = new AtomicBoolean(false);
 
     private static final String CREATE_DB_VERSION_TABLE = "CREATE TABLE "+ DB_VERSION_TABLE_NAME + " ( version int not null )";
     private static final String INSERT_INTO_DB_VERSION = "INSERT INTO "+ DB_VERSION_TABLE_NAME + " ( version ) VALUES ( ? )";
+    private static final String SELECT_FROM_DB_VERSION = "SELECT version FROM " + DB_VERSION_TABLE_NAME;
+    private static final String UPDATE_DB_VERSION = "UPDATE " + DB_VERSION_TABLE_NAME + " SET version = ?";
+
 
     private static final String CREATE_CONFIG_VERSION_TABLE = "CREATE TABLE "+ CONFIGURATION_VERSION_TABLE_NAME + " ( version int not null )";
     private static final String INSERT_INTO_CONFIG_VERSION = "INSERT INTO "+ CONFIGURATION_VERSION_TABLE_NAME + " ( version ) VALUES ( ? )";
@@ -208,11 +211,83 @@ abstract public class AbstractJDBCMessag
     }
 
     private void commonConfiguration(String name, VirtualHost virtualHost)
-            throws ClassNotFoundException, SQLException
+            throws ClassNotFoundException, SQLException, AMQStoreException
     {
         implementationSpecificConfiguration(name, virtualHost);
         createOrOpenDatabase();
+        upgradeIfNecessary();
+    }
+
+    protected void upgradeIfNecessary() throws SQLException, AMQStoreException
+    {
+        Connection conn = newAutoCommitConnection();
+        try
+        {
 
+            PreparedStatement statement = conn.prepareStatement(SELECT_FROM_DB_VERSION);
+            try
+            {
+                ResultSet rs = statement.executeQuery();
+                try
+                {
+                    if(!rs.next())
+                    {
+                        throw new AMQStoreException(DB_VERSION_TABLE_NAME + " does not contain the database version");
+                    }
+                    int version = rs.getInt(1);
+                    switch (version)
+                    {
+                        case 6:
+                            upgradeFromV6();
+                        case DB_VERSION:
+                            return;
+                        default:
+                            throw new AMQStoreException("Unknown database version: " + version);
+                    }
+                }
+                finally
+                {
+                    rs.close();
+                }
+            }
+            finally
+            {
+                statement.close();
+            }
+        }
+        finally
+        {
+            conn.close();
+        }
+
+    }
+
+    private void upgradeFromV6() throws SQLException
+    {
+        updateDbVersion(7);
+    }
+
+    private void updateDbVersion(int newVersion) throws SQLException
+    {
+        Connection conn = newAutoCommitConnection();
+        try
+        {
+
+            PreparedStatement statement = conn.prepareStatement(UPDATE_DB_VERSION);
+            try
+            {
+                statement.setInt(1,newVersion);
+                statement.execute();
+            }
+            finally
+            {
+                statement.close();
+            }
+        }
+        finally
+        {
+            conn.close();
+        }
     }
 
     protected abstract void implementationSpecificConfiguration(String name,



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org