From b9a5d00e15b76739978b7e321f12ab15a3b55d9e Mon Sep 17 00:00:00 2001 From: Khai Do Date: Sat, 21 May 2016 08:59:44 -0700 Subject: [PATCH] Add an optional flag to force an online reindex As discussed on Gerrit ML[1] and in referenced issue, there are situations where you may want to run an online reindex besides the upgrade gerrit use case. This change allows users to force an online reindex. [1] https://groups.google.com/d/msg/repo-discuss/pUn8fHVh58w/icsSFef2CAAJ feature: issue 3987 Change-Id: Ie88b7effda08996f7c0f325543be703bcf09c794 --- Documentation/cmd-index-start.txt | 7 +++++-- .../com/google/gerrit/lucene/LuceneVersionManager.java | 9 +++++---- .../google/gerrit/sshd/commands/IndexStartCommand.java | 6 +++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Documentation/cmd-index-start.txt b/Documentation/cmd-index-start.txt index 8f74c8f8e8..fbe4f3fed0 100644 --- a/Documentation/cmd-index-start.txt +++ b/Documentation/cmd-index-start.txt @@ -6,7 +6,7 @@ gerrit index start - Start the online indexer == SYNOPSIS [verse] -- -_ssh_ -p _gerrit index start_ +_ssh_ -p _gerrit index start_ [--force] -- == DESCRIPTION @@ -33,8 +33,11 @@ This command is intended to be used in scripts. * changes * accounts +--force:: + Force an online re-index. + == EXAMPLES -Start the online indexer for the changes index: +Start the online indexer for the 'changes' index: ---- $ ssh -p 29418 review.example.com gerrit index start changes diff --git a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneVersionManager.java b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneVersionManager.java index 94408c6c84..0a6acaa9e8 100644 --- a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneVersionManager.java +++ b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneVersionManager.java @@ -163,11 +163,11 @@ public class LuceneVersionManager implements LifecycleListener { markNotReady(cfg, def.getName(), versions.values(), write); int latest = write.get(0).version; + OnlineReindexer reindexer = new OnlineReindexer<>(def, latest); + reindexers.put(def.getName(), reindexer); if (onlineUpgrade && latest != search.version) { - OnlineReindexer reindexer = new OnlineReindexer<>(def, latest); synchronized (this) { if (!reindexers.containsKey(def.getName())) { - reindexers.put(def.getName(), reindexer); reindexer.start(); } } @@ -177,14 +177,15 @@ public class LuceneVersionManager implements LifecycleListener { /** * Start the online reindexer if the current index is not already the latest. * + * @param force start re-index * @return true if started, otherwise false. * @throws ReindexerAlreadyRunningException */ - public synchronized boolean startReindexer(String name) + public synchronized boolean startReindexer(String name, boolean force) throws ReindexerAlreadyRunningException { OnlineReindexer reindexer = reindexers.get(name); validateReindexerNotRunning(reindexer); - if (!isCurrentIndexVersionLatest(name, reindexer)) { + if (force || !isCurrentIndexVersionLatest(name, reindexer)) { reindexer.start(); return true; } diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexStartCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexStartCommand.java index 73e9f3364f..6629e3c74d 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexStartCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexStartCommand.java @@ -23,11 +23,15 @@ import com.google.gerrit.sshd.SshCommand; import com.google.inject.Inject; import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) @CommandMetaData(name = "start", description = "Start the online reindexer") public class IndexStartCommand extends SshCommand { + @Option(name = "--force", usage = "force a re-index") + private boolean force; + @Argument(index = 0, required = true, metaVar = "INDEX", usage = "index name to start") private String name; @@ -38,7 +42,7 @@ public class IndexStartCommand extends SshCommand { @Override protected void run() throws UnloggedFailure { try { - if (luceneVersionManager.startReindexer(name)) { + if (luceneVersionManager.startReindexer(name, force)) { stdout.println("Reindexer started"); } else { stdout.println("Nothing to reindex, index is already the latest version");