From 259609ae49cea8d8e189bfcefd9e3d87d8c5ee1b Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Fri, 06 Sep 2013 07:02:28 +0000
Subject: [PATCH] CR-2260 Implement Rebuild all indexes Upgrade task - removed start task action (unused) - removed verifyTaskType function (unused) - used LDIF.search to retrieve backends (rebuild index works only on 'enabled' backend). - added rebuild all index task.  - In case of an upgrade where a rebuild all task is present :     . All the other single rebuild indexes are bypass.     . Rebuild-all is launched only if the the user wants to.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTask.java         |   14 --
 opendj-sdk/opends/src/messages/messages/tools.properties                              |    9 +
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/AbstractUpgradeTask.java |   10 -
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java             |   58 ++-------
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java        |  194 ++++++++++++++-----------------
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java        |   21 ++-
 6 files changed, 119 insertions(+), 187 deletions(-)

diff --git a/opendj-sdk/opends/src/messages/messages/tools.properties b/opendj-sdk/opends/src/messages/messages/tools.properties
index 8e8c789..c34dfd3 100644
--- a/opendj-sdk/opends/src/messages/messages/tools.properties
+++ b/opendj-sdk/opends/src/messages/messages/tools.properties
@@ -2545,8 +2545,11 @@
 upgrade task. Process aborted. Please check log for further details
 INFO_UPGRADE_REBUILD_INDEX_DECLINED_1844 =You have to rebuild the '%s' index \
 manually to get a fully functional server
-SEVERE_ERR_UPGRADE_INVALID_LOG_FILE_1845=Invalid log file %s
-INFO_UPGRADE_REBUILD_INDEX_ARGUMENTS_1846=The rebuild index tool arguments are %s
+INFO_UPGRADE_ALL_REBUILD_INDEX_DECLINED_1845=You have to rebuild all indexes \
+manually to get a fully functional server
+SEVERE_ERR_UPGRADE_INVALID_LOG_FILE_1846=Invalid log file %s
+INFO_UPGRADE_REBUILD_INDEX_ARGUMENTS_1847=The rebuild index tool arguments are %s
+INFO_UPGRADE_REBUILD_ALL_1848=Rebuilding all indexes
 
 # Upgrade tasks
 INFO_UPGRADE_TASK_6869_SUMMARY_10000=Fixing de-DE collation matching rule OID
@@ -2572,4 +2575,4 @@
 INFO_UPGRADE_TASK_8985_2_SUMMARY_10018=Updating subject attribute to user attribute configuration
 INFO_UPGRADE_TASK_9013_DESCRIPTION_10019=OpenDJ 2.5.0-Xpress1 introduced a \
  regression in the ds-sync-hist ordering index. This index has to be rebuilt and this could take a long time \
- to proceed. Do you want to launch this process at automatically at the end of the upgrade ?
+ to proceed. Do you want to launch this process at automatically at the end of the upgrade?
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/AbstractUpgradeTask.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/AbstractUpgradeTask.java
index 048a72c..e968f9d 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/AbstractUpgradeTask.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/AbstractUpgradeTask.java
@@ -55,16 +55,6 @@
    * {@inheritDoc}
    */
   @Override
-  public void start(UpgradeContext context)
-      throws ClientException
-  {
-    // Nothing to do.
-  }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
   public void perform(UpgradeContext context) throws ClientException
   {
     // Must be implemented.
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
index c946268..8622fdc 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
@@ -301,6 +301,8 @@
             rebuildSingleIndex(INFO_UPGRADE_TASK_9013_DESCRIPTION.get(),
                 "ds-sync-hist")));
 
+
+
     /*
      * All upgrades will refresh the server configuration schema and generate
      * a new upgrade folder.
@@ -384,15 +386,21 @@
      * and the application is non-interactive then, the process
      * may abort immediately.
      */
-    verify(context, tasks);
+    for (final UpgradeTask task : tasks)
+    {
+      task.verify(context);
+    }
 
     /*
-     * Asking upgrade requirements if needed to user.
+     * Let tasks interact with the user in order to obtain user's selection.
      */
     context.notify(INFO_UPGRADE_REQUIREMENTS.get(), TITLE_CALLBACK);
-    interact(context, tasks);
+    for (final UpgradeTask task : tasks)
+    {
+      task.interact(context);
+    }
 
-    // Starts upgrade.
+    // Starts upgrade
     final int userResponse = context.confirmYN(
         INFO_UPGRADE_DISPLAY_CONFIRM_START.get(), ConfirmationCallback.YES);
     if (userResponse == ConfirmationCallback.NO)
@@ -408,18 +416,6 @@
        */
       context.notify(INFO_UPGRADE_PERFORMING_TASKS.get(),
           TITLE_CALLBACK);
-
-      /*
-       * Notify each task that the upgrade is about to be started.
-       */
-      for (final UpgradeTask task : tasks)
-      {
-        task.start(context);
-      }
-
-      /*
-       * Perform each task.
-       */
       for (final UpgradeTask task : tasks)
       {
         task.perform(context);
@@ -516,36 +512,6 @@
     MANDATORY_TASKS.addAll(Arrays.asList(tasks));
   }
 
-  private static void interact(final UpgradeContext context,
-      final List<UpgradeTask> tasks)
-      throws ClientException
-  {
-    /*
-     * Let tasks interact with the user in order to obtain user's selection.
-     */
-    for (final UpgradeTask task : tasks)
-    {
-      task.interact(context);
-    }
-  }
-
-
-
-  private static void verify(final UpgradeContext context,
-      final List<UpgradeTask> tasks)
-      throws ClientException
-  {
-    /*
-     * Let tasks interact with CLI to check if command line is correct.
-     */
-    for (final UpgradeTask task : tasks)
-    {
-      task.verify(context);
-    }
-  }
-
-
-
   /**
    * The server must be offline during the upgrade.
    *
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTask.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTask.java
index 968cacd..f70bb74 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTask.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTask.java
@@ -74,20 +74,6 @@
       throws ClientException;
 
   /**
-   * Notifies this task that the upgrade is about to start. This method will be
-   * invoked before any upgrade tasks have been performed. Most task
-   * implementation will not need to do anything.
-   *
-   * @param context
-   *          Context through which tasks can interact with the server
-   *          installation.
-   * @throws ClientException
-   *           If an error occurred while starting the task.
-   */
-  void start(UpgradeContext context)
-      throws ClientException;
-
-  /**
    * Verifies that this upgrade task can be completed or not.
    *
    * @param context
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
index de87f2a..4c90136 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
@@ -28,8 +28,6 @@
 package org.opends.server.tools.upgrade;
 
 import static org.opends.messages.ToolMessages.*;
-import static org.opends.server.tools.ToolConstants.OPTION_LONG_FORCE_UPGRADE;
-import static org.opends.server.tools.ToolConstants.OPTION_LONG_NO_PROMPT;
 import static org.opends.server.tools.upgrade.FileManager.copy;
 import static org.opends.server.tools.upgrade.Installation
 .CURRENT_CONFIG_FILE_NAME;
@@ -53,7 +51,6 @@
 import org.opends.server.protocols.ldap.LDAPFilter;
 import org.opends.server.tools.ClientException;
 import org.opends.server.tools.RebuildIndex;
-import org.opends.server.tools.upgrade.UpgradeTask.TaskType;
 import org.opends.server.util.BuildVersion;
 import org.opends.server.util.ChangeOperationType;
 
@@ -79,9 +76,14 @@
   static Set<String> indexesListToRebuild = new HashSet<String>();
 
   /**
-   * A flag to avoid rebuild indexes if all already selected.
+   * A flag to avoid rebuild single indexes if 'rebuild all' is selected.
    */
-  static boolean isRebuildAllIndexes = false;
+  static boolean isRebuildAllIndexesIsPresent = false;
+
+  /**
+   * A flag for marking 'rebuild all' task accepted by user.
+   */
+  static boolean isRebuildAllIndexesTaskAccepted = false;
 
   /**
    * Returns a new upgrade task which applies an LDIF record to all
@@ -406,18 +408,6 @@
       }
 
       @Override
-      public void start(UpgradeContext context) throws ClientException
-      {
-        if (currentVersionEqualToOrMoreRecentThan(context, version))
-        {
-          for (UpgradeTask task : tasks)
-          {
-            task.start(context);
-          }
-        }
-      }
-
-      @Override
       public void perform(UpgradeContext context) throws ClientException
       {
         if (currentVersionEqualToOrMoreRecentThan(context, version))
@@ -477,22 +467,36 @@
   {
     return new AbstractUpgradeTask()
     {
+      private boolean isATaskToPerform = false;
+
       @Override
-      public void perform(final UpgradeContext context) throws ClientException
+      public void interact(UpgradeContext context) throws ClientException
       {
-        // NYI.
+        Upgrade.setHasPostUpgradeTask(true);
+        // Requires answer from the user.
+        final int answer = context.confirmYN(summary, ConfirmationCallback.NO);
+        isATaskToPerform = (answer == ConfirmationCallback.YES);
+        isRebuildAllIndexesIsPresent =  true;
+        isRebuildAllIndexesTaskAccepted =  isATaskToPerform;
+
       }
 
       @Override
-      public void start(final UpgradeContext context) throws ClientException
+      public void postUpgrade(final UpgradeContext context)
+          throws ClientException
       {
-        context.notify(summary);
+        if (!isATaskToPerform)
+        {
+          postponePostUpgrade(context);
+        }
       }
 
       @Override
-      public void verify(final UpgradeContext context) throws ClientException
+      public void postponePostUpgrade(UpgradeContext context)
+          throws ClientException
       {
-        verifyTaskType(TaskType.MANDATORY_USER_INTERACTION, context);
+        context.notify(INFO_UPGRADE_ALL_REBUILD_INDEX_DECLINED.get(),
+            TextOutputCallback.WARNING);
       }
     };
   }
@@ -549,8 +553,11 @@
       public void postponePostUpgrade(UpgradeContext context)
           throws ClientException
       {
-        context.notify(INFO_UPGRADE_REBUILD_INDEX_DECLINED.get(index),
-            TextOutputCallback.WARNING);
+        if (!isRebuildAllIndexesIsPresent)
+        {
+          context.notify(INFO_UPGRADE_REBUILD_INDEX_DECLINED.get(index),
+              TextOutputCallback.WARNING);
+        }
       }
     };
   }
@@ -570,25 +577,32 @@
       public void postUpgrade(final UpgradeContext context)
           throws ClientException
       {
-        if (isRebuildAllIndexes)
-        {
-          // TODO To implement
-        }
-        else if (!indexesListToRebuild.isEmpty())
-        {
-          final Message message = INFO_UPGRADE_REBUILD_INDEX_STARTS.get(Arrays
-              .toString(indexesListToRebuild.toArray()));
-          final ProgressNotificationCallback pnc =
-              new ProgressNotificationCallback(0, message, 25);
-          LOG.log(Level.INFO, message.toString());
-          context.notifyProgress(pnc);
+        // Sets the arguments like the rebuild index command line.
+        final List<String> args = new LinkedList<String>();
+        args.addAll(Arrays.asList(
+            "-f",
+            new File(configDirectory, CURRENT_CONFIG_FILE_NAME)
+              .getAbsolutePath()));
 
-          // Sets the arguments like the rebuild index command line.
-          final List<String> args = new LinkedList<String>();
-          args.addAll(Arrays.asList(
-              "-f",
-              new File(configDirectory, CURRENT_CONFIG_FILE_NAME)
-                .getAbsolutePath()));
+        // Index(es) could be contained in several backends.
+        for (final String be : UpgradeUtils.getLocalBackendsFromConfig())
+        {
+          args.add("-b");
+          args.add(be);
+        }
+
+        Message message = null;
+        if (isRebuildAllIndexesIsPresent && isRebuildAllIndexesTaskAccepted)
+        {
+          args.add("--rebuildAll");
+          message = INFO_UPGRADE_REBUILD_ALL.get();
+        }
+        else if (!indexesListToRebuild.isEmpty()
+            && !isRebuildAllIndexesTaskAccepted)
+        {
+          message =
+              INFO_UPGRADE_REBUILD_INDEX_STARTS.get(Arrays
+                  .toString(indexesListToRebuild.toArray()));
 
           // Adding all requested indexes.
           for (final String indexToRebuild : indexesListToRebuild)
@@ -596,39 +610,38 @@
             args.add("-i");
             args.add(indexToRebuild);
           }
+        } else {
+          return;
+        }
 
-          // Index(es) could be contained in several backends.
-          for (final String be : UpgradeUtils.getLocalBackendsFromConfig())
-          {
-            args.add("-b");
-            args.add(be);
-          }
+        ProgressNotificationCallback pnc =
+            new ProgressNotificationCallback(0, message, 25);
+        LOG.log(Level.INFO, message.toString());
+        context.notifyProgress(pnc);
 
-          final String[] commandLineArgs =
-              args.toArray(new String[args.size()]);
-          // Displays info about command line args for log only.
-          LOG.log(Level.INFO, INFO_UPGRADE_REBUILD_INDEX_ARGUMENTS.get(Arrays
-              .toString(commandLineArgs)).toString());
-          /*
-           * The rebuild-index process just display a status ok / fails. The
-           * logger stream contains all the log linked to this process. The
-           * complete process is not displayed in the upgrade console.
-           */
-          final int result =
-              new RebuildIndex().rebuildIndexesWithinMultipleBackends(true,
-                  UpgradeLog.getPrintStream(), commandLineArgs);
-          if (result == 0)
-          {
-            LOG.log(Level.INFO, INFO_UPGRADE_REBUILD_INDEX_ENDS.get()
-                .toString());
-            context.notifyProgress(pnc.setProgress(100));
-          }
-          else
-          {
-            LOG.log(Level.SEVERE, ERR_UPGRADE_PERFORMING_POST_TASKS_FAIL.get()
-                .toString());
-            context.notifyProgress(pnc.setProgress(-100));
-          }
+        final String[] commandLineArgs = args.toArray(new String[args.size()]);
+        // Displays info about command line args for log only.
+        LOG.log(Level.INFO, INFO_UPGRADE_REBUILD_INDEX_ARGUMENTS.get(
+            Arrays.toString(commandLineArgs)).toString());
+
+        /*
+         * The rebuild-index process just display a status ok / fails. The
+         * logger stream contains all the log linked to this process. The
+         * complete process is not displayed in the upgrade console.
+         */
+        final int result =
+            new RebuildIndex().rebuildIndexesWithinMultipleBackends(true,
+                UpgradeLog.getPrintStream(), commandLineArgs);
+        if (result == 0)
+        {
+          LOG.log(Level.INFO, INFO_UPGRADE_REBUILD_INDEX_ENDS.get().toString());
+          context.notifyProgress(pnc.setProgress(100));
+        }
+        else
+        {
+          LOG.log(Level.SEVERE, ERR_UPGRADE_PERFORMING_POST_TASKS_FAIL.get()
+              .toString());
+          context.notifyProgress(pnc.setProgress(-100));
         }
       }
     };
@@ -897,39 +910,6 @@
     };
   }
 
-  @SuppressWarnings("fallthrough")
-  private static void verifyTaskType(final TaskType type,
-      final UpgradeContext context) throws ClientException
-  {
-    /*
-     * Checks CLI/GUI options via context. The process will stop
-     * if user has selected conflicting options.
-     */
-    switch (type)
-    {
-    case NEED_USER_INTERACTION:
-    {
-      // Nothing to do.
-      break;
-    }
-    case MANDATORY_USER_INTERACTION:
-    case TAKE_LONG_TIME_TO_COMPLETE:
-    case CANNOT_BE_REVERTED:
-      // The option is not present ? Stops the process.
-      if (!context.isInteractiveMode() && !context.isForceUpgradeMode())
-      {
-        context
-            .notify(Message.raw(" "), FormattedNotificationCallback.BREAKLINE);
-        context.notify(ERR_UPGRADE_USER_INTERACTION_REQUIRED.get(
-            OPTION_LONG_NO_PROMPT, OPTION_LONG_FORCE_UPGRADE),
-            FormattedNotificationCallback.NOTICE_CALLBACK);
-        throw new ClientException(EXIT_CODE_MANUAL_INTERVENTION,
-            ERR_UPGRADE_INVALID_USER_OPTIONS_SELECTED.get());
-      }
-    default:
-      break;
-    }
-  }
 
   // Prevent instantiation.
   private UpgradeTasks()
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
index c73e57c..9d77810 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
@@ -36,10 +36,13 @@
 import org.forgerock.opendj.ldap.requests.AddRequest;
 import org.forgerock.opendj.ldap.requests.ModifyRequest;
 import org.forgerock.opendj.ldap.requests.Requests;
+import org.forgerock.opendj.ldap.requests.SearchRequest;
 import org.forgerock.opendj.ldap.schema.Schema;
 import org.forgerock.opendj.ldap.schema.SchemaBuilder;
 import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
 import org.forgerock.opendj.ldap.schema.UnknownSchemaElementException;
+import org.forgerock.opendj.ldif.EntryReader;
+import org.forgerock.opendj.ldif.LDIF;
 import org.forgerock.opendj.ldif.LDIFEntryReader;
 import org.forgerock.opendj.ldif.LDIFEntryWriter;
 import org.opends.server.core.DirectoryServer;
@@ -386,7 +389,9 @@
   }
 
   /**
-   * Retrieves the backends from the current configuration file.
+   * Retrieves the backends from the current configuration file. The backends
+   * must be enabled to be listed. No operations should be done within a
+   * disabled backend.
    *
    * @return A backend list.
    */
@@ -400,14 +405,16 @@
           new LDIFEntryReader(new FileInputStream(new File(configDirectory,
               CURRENT_CONFIG_FILE_NAME)));
 
-      final Filter filter =
-          Filter.equality("objectclass", "ds-cfg-local-db-backend");
-      final Matcher includeFilter = filter.matcher();
-      entryReader.setIncludeFilter(includeFilter);
+      final SearchRequest sr =
+          Requests.newSearchRequest("", SearchScope.WHOLE_SUBTREE,
+              "(&(objectclass=ds-cfg-local-db-backend)(ds-cfg-enabled=true))",
+              "ds-cfg-base-dn");
 
-      while (entryReader.hasNext())
+      final EntryReader resultReader = LDIF.search(entryReader, sr);
+
+      while (resultReader.hasNext())
       {
-        final Entry entry = entryReader.readEntry();
+        final Entry entry = resultReader.readEntry();
         listBackends.add(entry.getAttribute("ds-cfg-base-dn")
             .firstValueAsString());
       }

--
Gitblit v1.10.0