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
This commit is contained in:
Khai Do 2016-05-21 08:59:44 -07:00
parent 9d8447b470
commit b9a5d00e15
3 changed files with 15 additions and 7 deletions

View File

@ -6,7 +6,7 @@ gerrit index start - Start the online indexer
== SYNOPSIS == SYNOPSIS
[verse] [verse]
-- --
_ssh_ -p <port> <host> _gerrit index start_ <INDEX> _ssh_ -p <port> <host> _gerrit index start_ <INDEX> [--force]
-- --
== DESCRIPTION == DESCRIPTION
@ -33,8 +33,11 @@ This command is intended to be used in scripts.
* changes * changes
* accounts * accounts
--force::
Force an online re-index.
== EXAMPLES == 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 $ ssh -p 29418 review.example.com gerrit index start changes

View File

@ -163,11 +163,11 @@ public class LuceneVersionManager implements LifecycleListener {
markNotReady(cfg, def.getName(), versions.values(), write); markNotReady(cfg, def.getName(), versions.values(), write);
int latest = write.get(0).version; int latest = write.get(0).version;
if (onlineUpgrade && latest != search.version) {
OnlineReindexer<K, V, I> reindexer = new OnlineReindexer<>(def, latest); OnlineReindexer<K, V, I> reindexer = new OnlineReindexer<>(def, latest);
reindexers.put(def.getName(), reindexer);
if (onlineUpgrade && latest != search.version) {
synchronized (this) { synchronized (this) {
if (!reindexers.containsKey(def.getName())) { if (!reindexers.containsKey(def.getName())) {
reindexers.put(def.getName(), reindexer);
reindexer.start(); reindexer.start();
} }
} }
@ -177,14 +177,15 @@ public class LuceneVersionManager implements LifecycleListener {
/** /**
* Start the online reindexer if the current index is not already the latest. * Start the online reindexer if the current index is not already the latest.
* *
* @param force start re-index
* @return true if started, otherwise false. * @return true if started, otherwise false.
* @throws ReindexerAlreadyRunningException * @throws ReindexerAlreadyRunningException
*/ */
public synchronized boolean startReindexer(String name) public synchronized boolean startReindexer(String name, boolean force)
throws ReindexerAlreadyRunningException { throws ReindexerAlreadyRunningException {
OnlineReindexer<?, ?, ?> reindexer = reindexers.get(name); OnlineReindexer<?, ?, ?> reindexer = reindexers.get(name);
validateReindexerNotRunning(reindexer); validateReindexerNotRunning(reindexer);
if (!isCurrentIndexVersionLatest(name, reindexer)) { if (force || !isCurrentIndexVersionLatest(name, reindexer)) {
reindexer.start(); reindexer.start();
return true; return true;
} }

View File

@ -23,11 +23,15 @@ import com.google.gerrit.sshd.SshCommand;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
@CommandMetaData(name = "start", description = "Start the online reindexer") @CommandMetaData(name = "start", description = "Start the online reindexer")
public class IndexStartCommand extends SshCommand { public class IndexStartCommand extends SshCommand {
@Option(name = "--force", usage = "force a re-index")
private boolean force;
@Argument(index = 0, required = true, metaVar = "INDEX", @Argument(index = 0, required = true, metaVar = "INDEX",
usage = "index name to start") usage = "index name to start")
private String name; private String name;
@ -38,7 +42,7 @@ public class IndexStartCommand extends SshCommand {
@Override @Override
protected void run() throws UnloggedFailure { protected void run() throws UnloggedFailure {
try { try {
if (luceneVersionManager.startReindexer(name)) { if (luceneVersionManager.startReindexer(name, force)) {
stdout.println("Reindexer started"); stdout.println("Reindexer started");
} else { } else {
stdout.println("Nothing to reindex, index is already the latest version"); stdout.println("Nothing to reindex, index is already the latest version");