From 0cea62907aa1c9179e359bacccf0b2692ca01d46 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 01 Oct 2013 10:44:56 +0000
Subject: [PATCH] OPENDJ-1116 Introduce abstraction for the changelog DB

---
 opends/src/server/org/opends/server/replication/server/ReplicationServer.java                                          |   20 +++---
 opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java                                 |   71 ++++++++++++++++-------
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java                       |   20 ++++--
 opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java                                  |   10 ++
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/AssuredReplicationServerTest.java       |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ExternalChangeLogTest.java              |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/InitOnLineTest.java                            |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/FractionalReplicationTest.java          |    2 
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/ReplicationServerLoadBalancingTest.java |    2 
 9 files changed, 86 insertions(+), 45 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
index 462d4c2..d576bb7 100644
--- a/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
+++ b/opends/src/server/org/opends/server/replication/server/ReplicationServer.java
@@ -760,7 +760,17 @@
 
     shutdownECL();
 
-    this.changelogDB.shutdownDB();
+    try
+    {
+      this.changelogDB.shutdownDB();
+    }
+    catch (ChangelogException ignored)
+    {
+      if (debugEnabled())
+      {
+        TRACER.debugCaught(DebugLogLevel.WARNING, ignored);
+      }
+    }
 
     // Remove this instance from the global instance list
     allInstances.remove(this);
@@ -1106,14 +1116,6 @@
   }
 
   /**
-   * Removes the changelog database directory.
-   */
-  public void removeDb()
-  {
-    this.changelogDB.removeDB();
-  }
-
-  /**
    * {@inheritDoc}
    */
   @Override
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java b/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
index a775ebc..57489c9 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/api/ChangelogDB.java
@@ -80,13 +80,19 @@
 
   /**
    * Shutdown the replication database.
+   *
+   * @throws ChangelogException
+   *           If a database problem happened
    */
-  void shutdownDB();
+  void shutdownDB() throws ChangelogException;
 
   /**
    * Removes the changelog database directory.
+   *
+   * @throws ChangelogException
+   *           If a database problem happened
    */
-  void removeDB();
+  void removeDB() throws ChangelogException;
 
   /**
    * Returns the {@link ChangeNumberIndexDB} object.
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
index d13cd7a..c1be57d 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
@@ -264,45 +264,60 @@
     }
   }
 
-  private void shutdownCNIndexDB()
+  private void shutdownCNIndexDB() throws ChangelogException
   {
     synchronized (cnIndexDBLock)
     {
       if (cnIndexDB != null)
       {
-        try
-        {
-          cnIndexDB.shutdown();
-        }
-        catch (ChangelogException ignored)
-        {
-          if (debugEnabled())
-          {
-            TRACER.debugCaught(DebugLogLevel.WARNING, ignored);
-          }
-        }
+        cnIndexDB.shutdown();
       }
     }
   }
 
   /** {@inheritDoc} */
   @Override
-  public void shutdownDB()
+  public void shutdownDB() throws ChangelogException
   {
-    shutdownCNIndexDB();
+    // Remember the first exception because :
+    // - we want to try to remove everything we want to remove
+    // - then throw the first encountered exception
+    ChangelogException firstException = null;
+
+    try
+    {
+      shutdownCNIndexDB();
+    }
+    catch (ChangelogException e)
+    {
+      firstException = e;
+    }
 
     if (dbEnv != null)
     {
       dbEnv.shutdown();
     }
+
+    if (firstException != null)
+    {
+      throw firstException;
+    }
   }
 
   /**
    * Clears all content from the changelog database, but leaves its directory on
    * the filesystem.
+   *
+   * @throws ChangelogException
+   *           If a database problem happened
    */
-  public void clearDB()
+  public void clearDB() throws ChangelogException
   {
+    // Remember the first exception because :
+    // - we want to try to remove everything we want to remove
+    // - then throw the first encountered exception
+    ChangelogException firstException = null;
+
     for (DN baseDN : this.sourceDbHandlers.keySet())
     {
       removeDomain(baseDN);
@@ -316,26 +331,40 @@
         {
           cnIndexDB.clear();
         }
-        catch (Exception ignored)
+        catch (ChangelogException e)
         {
-          if (debugEnabled())
+          firstException = e;
+        }
+
+        try
+        {
+          shutdownCNIndexDB();
+        }
+        catch (ChangelogException e)
+        {
+          if (firstException == null)
           {
-            TRACER.debugCaught(DebugLogLevel.WARNING, ignored);
+            firstException = e;
           }
         }
 
-        shutdownCNIndexDB();
-
         cnIndexDB = null;
       }
     }
+
+    if (firstException != null)
+    {
+      throw firstException;
+    }
   }
 
   /** {@inheritDoc} */
   @Override
-  public void removeDB()
+  public void removeDB() throws ChangelogException
   {
     StaticUtils.recursiveDelete(dbDirectory);
+    // there is no point in keeping the DB open after it has been removed
+    shutdownDB();
   }
 
   /** {@inheritDoc} */
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/InitOnLineTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/InitOnLineTest.java
index d4512bc..bb523a7 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/InitOnLineTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/InitOnLineTest.java
@@ -1443,7 +1443,7 @@
    * Disconnect broker and remove entries from the local DB
    * @param testCase The name of the test case.
    */
-  private void afterTest(String testCase)
+  private void afterTest(String testCase) throws Exception
   {
     // Check that the domain has completed the import/export task.
     if (replDomain != null)
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
index 793773b..4304f01 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
@@ -464,7 +464,7 @@
     assertEquals(DirectoryServer.getBackend("replicationChanges"), null, "Replication changes backend object has been left");
   }
 
-  protected void clearChangelogDB(ReplicationServer rs)
+  protected void clearChangelogDB(ReplicationServer rs) throws Exception
   {
     ((JEChangelogDB) rs.getChangelogDB()).clearDB();
   }
@@ -473,8 +473,10 @@
    * Cleanup databases of the currently instantiated replication servers in the
    * VM
    */
-  protected void cleanUpReplicationServersDB() {
-    for (ReplicationServer rs : ReplicationServer.getAllInstances()) {
+  protected void cleanUpReplicationServersDB() throws Exception
+  {
+    for (ReplicationServer rs : ReplicationServer.getAllInstances())
+    {
       clearChangelogDB(rs);
     }
   }
@@ -483,21 +485,23 @@
    * Remove trailing directories and databases of the currently instantiated
    * replication servers.
    */
-  protected void removeReplicationServerDB() {
-    for (ReplicationServer rs : ReplicationServer.getAllInstances()) {
+  protected void removeReplicationServerDB() throws Exception
+  {
+    for (ReplicationServer rs : ReplicationServer.getAllInstances())
+    {
       clearChangelogDB(rs);
-      rs.removeDb();
+      rs.getChangelogDB().removeDB();
     }
   }
 
-  protected void remove(ReplicationServer... replicationServers)
+  protected void remove(ReplicationServer... replicationServers) throws Exception
   {
     for (ReplicationServer rs : replicationServers)
     {
       if (rs != null)
       {
         rs.remove();
-        rs.removeDb();
+        rs.getChangelogDB().removeDB();
       }
     }
   }
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/FractionalReplicationTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/FractionalReplicationTest.java
index c32a385..988d8d9 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/FractionalReplicationTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/FractionalReplicationTest.java
@@ -401,7 +401,7 @@
     gen = new CSNGenerator(DS2_ID, 0L);
   }
 
-  private void endTest()
+  private void endTest() throws Exception
   {
     if (replicationDomain != null)
     {
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/ReplicationServerLoadBalancingTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/ReplicationServerLoadBalancingTest.java
index d4a8434..dea446c 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/ReplicationServerLoadBalancingTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/ReplicationServerLoadBalancingTest.java
@@ -766,7 +766,7 @@
     }
   }
 
-  private void stopRs(int... rsIndexes)
+  private void stopRs(int... rsIndexes) throws Exception
   {
     for (int rsIndex : rsIndexes)
     {
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/AssuredReplicationServerTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/AssuredReplicationServerTest.java
index 8305b7b..6ed4021 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/AssuredReplicationServerTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/AssuredReplicationServerTest.java
@@ -201,7 +201,7 @@
     rs1 = rs2 = rs3 = rs4 = null;
   }
 
-  private void endTest()
+  private void endTest() throws Exception
   {
     disableService(fakeRDs);
     Arrays.fill(fakeRDs, null);
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ExternalChangeLogTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ExternalChangeLogTest.java
index e12dc75..12e3917 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ExternalChangeLogTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ExternalChangeLogTest.java
@@ -2069,7 +2069,7 @@
   }
 
   @AfterMethod
-  public void clearReplicationDb()
+  public void clearReplicationDb() throws Exception
   {
     clearChangelogDB(replicationServer);
   }

--
Gitblit v1.10.0