From 1c50a85548b529e2b397a2d3b19c473d9b89bb62 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 12 Nov 2015 09:27:22 +0000
Subject: [PATCH] OPENDJ-1742 Implement 2.x -> 3.0 changelog migration task

---
 opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java      |   34 ++++++++++------
 opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java |   69 ++++++++++++++++++++++++++++++++++
 opendj-server-legacy/src/messages/org/opends/messages/tool.properties                |   12 +++++-
 3 files changed, 100 insertions(+), 15 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
index 8788dbc..22fbcef 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
@@ -34,8 +34,6 @@
 import java.util.NavigableMap;
 import java.util.TreeMap;
 
-import javax.security.auth.callback.ConfirmationCallback;
-
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.opends.server.core.LockFileManager;
@@ -46,13 +44,15 @@
 import com.forgerock.opendj.cli.ReturnCode;
 
 import static com.forgerock.opendj.cli.Utils.*;
-import static javax.security.auth.callback.TextOutputCallback.*;
+import static javax.security.auth.callback.ConfirmationCallback.*;
+import static javax.security.auth.callback.TextOutputCallback.WARNING;
+
 import static org.opends.messages.ToolMessages.*;
 import static org.opends.server.tools.upgrade.FormattedNotificationCallback.*;
 import static org.opends.server.tools.upgrade.LicenseFile.*;
 import static org.opends.server.tools.upgrade.UpgradeTasks.*;
-import static org.opends.server.tools.upgrade.UpgradeUtils.batDirectory;
-import static org.opends.server.tools.upgrade.UpgradeUtils.binDirectory;
+import static org.opends.server.tools.upgrade.UpgradeUtils.*;
+import static org.opends.server.util.StaticUtils.*;
 
 /**
  * This class contains the table of upgrade tasks that need performing when
@@ -446,6 +446,14 @@
     register("3.0.0",
         rebuildAllIndexes(INFO_UPGRADE_TASK_11260_SUMMARY.get()));
 
+    /** See OPENDJ-1742 */
+    register("3.0.0",
+        clearReplicationDbDirectory(),
+        modifyConfigEntry(INFO_UPGRADE_TASK_ENABLED_FILE_BASED_CHANGELOG.get(),
+            "(objectClass=ds-cfg-replication-server)",
+            "replace: ds-cfg-replication-db-implementation",
+            "ds-cfg-replication-db-implementation: log"));
+
     /*
      * All upgrades will refresh the server configuration schema and generate
      * a new upgrade folder.
@@ -536,8 +544,8 @@
       }
 
       // Starts upgrade
-      final int userResponse = context.confirmYN(INFO_UPGRADE_DISPLAY_CONFIRM_START.get(), ConfirmationCallback.YES);
-      if (userResponse == ConfirmationCallback.NO)
+      final int userResponse = context.confirmYN(INFO_UPGRADE_DISPLAY_CONFIRM_START.get(), YES);
+      if (userResponse == NO)
       {
         final LocalizableMessage message = INFO_UPGRADE_ABORTED_BY_USER.get();
         context.notify(message, WARNING);
@@ -588,9 +596,9 @@
     }
     catch (final Exception e)
     {
-      final LocalizableMessage message = ERR_UPGRADE_TASKS_FAIL.get(e.getMessage());
+      final LocalizableMessage message = ERR_UPGRADE_TASKS_FAIL.get(stackTraceToSingleLineString(e));
       context.notify(message, ERROR_CALLBACK);
-      throw new ClientException(ReturnCode.ERROR_UNEXPECTED, message);
+      throw new ClientException(ReturnCode.ERROR_UNEXPECTED, message, e);
     }
     finally
     {
@@ -759,20 +767,20 @@
         // -an accept license mode to continue the process.
         if (context.isForceUpgradeMode())
         {
-          answer = ConfirmationCallback.NO;
+          answer = NO;
           context.notify(LocalizableMessage.raw(INFO_LICENSE_ACCEPT.get() + " "
               + INFO_PROMPT_NO_COMPLETE_ANSWER.get()));
         }
         else
         {
-          answer = context.confirmYN(INFO_LICENSE_ACCEPT.get(), ConfirmationCallback.NO);
+          answer = context.confirmYN(INFO_LICENSE_ACCEPT.get(), NO);
         }
 
-        if (answer == ConfirmationCallback.NO)
+        if (answer == NO)
         {
           System.exit(EXIT_CODE_SUCCESS);
         }
-        else if (answer == ConfirmationCallback.YES)
+        else if (answer == YES)
         {
           LicenseFile.setApproval(true);
         }
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
index c815b5d..9bcf691 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
@@ -1095,6 +1095,75 @@
     }
   }
 
+  static UpgradeTask clearReplicationDbDirectory()
+  {
+    return new AbstractUpgradeTask()
+    {
+      private File replicationDbDir;
+
+      @Override
+      public void prepare(final UpgradeContext context) throws ClientException
+      {
+        String replDbDir = readReplicationDbDirFromConfig();
+        if (replDbDir != null
+            && context.confirmYN(INFO_UPGRADE_TASK_MIGRATE_CHANGELOG_DESCRIPTION.get(), NO) == YES)
+        {
+          replicationDbDir = new File(getInstancePath(), replDbDir).getAbsoluteFile();
+        }
+        // if replDbDir == null, then this is not an RS, there is no changelog DB to clear
+      }
+
+      private String readReplicationDbDirFromConfig() throws ClientException
+      {
+        final SearchRequest sr = Requests.newSearchRequest(
+            DN.valueOf("cn=replication server,cn=Multimaster Synchronization,cn=Synchronization Providers,cn=config"),
+            SearchScope.BASE_OBJECT, Filter.alwaysTrue());
+        try (final EntryReader entryReader = searchConfigFile(sr))
+        {
+          if (entryReader.hasNext())
+          {
+            final Entry replServerCfg = entryReader.readEntry();
+            return replServerCfg.parseAttribute("ds-cfg-replication-db-directory").asString();
+          }
+          return null;
+        }
+        catch (IOException e)
+        {
+          LocalizableMessage msg = INFO_UPGRADE_TASK_MIGRATE_CONFIG_READ_FAIL.get();
+          throw new ClientException(ReturnCode.APPLICATION_ERROR, msg, e);
+        }
+      }
+
+      @Override
+      public void perform(final UpgradeContext context) throws ClientException
+      {
+        if (replicationDbDir == null)
+        {
+          // there is no changelog DB to clear
+          return;
+        }
+
+        LocalizableMessage msg = INFO_UPGRADE_TASK_DELETE_CHANGELOG_SUMMARY.get(replicationDbDir);
+        ProgressNotificationCallback pnc =
+           new ProgressNotificationCallback(0, msg, 0);
+        context.notifyProgress(pnc);
+        try
+        {
+          FileManager.deleteRecursively(replicationDbDir);
+          context.notifyProgress(pnc.setProgress(100));
+        }
+        catch (ClientException e)
+        {
+          manageTaskException(context, e.getMessageObject(), pnc);
+        }
+        catch (Exception e)
+        {
+          manageTaskException(context, LocalizableMessage.raw(e.getLocalizedMessage()), pnc);
+        }
+      }
+    };
+  }
+
   /** Prevent instantiation. */
   private UpgradeTasks()
   {
diff --git a/opendj-server-legacy/src/messages/org/opends/messages/tool.properties b/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
index 8963d64..9862056 100644
--- a/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
+++ b/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
@@ -2666,7 +2666,7 @@
 INFO_UPGRADE_TASK_MIGRATE_JE_UGLY_DN_10041=The upgrade will not be performed because the JE backend '%s' contains a \
   base DN '%s' that cannot be migrated. The upgrade should be restarted once the JE backend has been exported and \
   disabled
-INFO_UPGRADE_TASK_MIGRATE_JE_CONFIG_READ_FAIL_10042=The upgrade will not be performed because the configuration file \
+INFO_UPGRADE_TASK_MIGRATE_CONFIG_READ_FAIL_10042=The upgrade will not be performed because the configuration file \
   config.ldif could not be parsed
 INFO_UPGRADE_TASK_MIGRATE_JE_ENV_UNREADABLE_10043=The upgrade cannot continue because the JE backend with \
   environment directory '%s' could not be accessed for migration
@@ -2681,7 +2681,15 @@
   backed up and that you have read the upgrade documentation before proceeding. Do you want to proceed with the \
   upgrade?
 INFO_UPGRADE_TASK_MIGRATE_JE_CANCELLED_10050=The upgrade will not be performed because some JE backends need to be \
-  migrated
+  migrated.
+INFO_UPGRADE_TASK_MIGRATE_CHANGELOG_DESCRIPTION_10051=OpenDJ 3.0.0 improved the replication changelog storage format. \
+  As a consequence, the old changelog content of the current replication server will be erased by the upgrade. \
+  The new changelog content will be automatically reconstructed from the changelog of \
+  other replication servers in the topology. After the upgrade, dsreplication reset-change-number can be used \
+  to reset the changelog change-number of the current replication server to match another replication server. \
+  Do you want to proceed with the upgrade?
+INFO_UPGRADE_TASK_DELETE_CHANGELOG_SUMMARY_10052=Removing content of changelog in '%s' directory.
+INFO_UPGRADE_TASK_ENABLED_FILE_BASED_CHANGELOG_10053=Enable log file based replication changelog storage.
 
 # Strings for generated reference documentation.
 REF_SHORT_DESC_BACKUP_15000=back up OpenDJ directory data

--
Gitblit v1.10.0