Reindex: Allow to specify index name for reindex operation

Check the indices passed with --index option and issue error message,
when invalid index names were passed, e.g.:

  $ reindex --site-path gerrit --index foo --index bar --index baz
  fatal: invalid index name(s): [bar, baz, foo] available indices are:
  [accounts, changes]

Change-Id: Ifb60e632bd1ff3cbb364e389f045e9a0de5a23c8
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
David Ostrovsky 2016-03-20 12:03:36 +01:00 committed by Edwin Kempin
parent bba1994755
commit ebbf81470a
2 changed files with 40 additions and 1 deletions

View File

@ -27,6 +27,10 @@ Rebuilds the secondary index.
--list::
List available index names.
--index::
Reindex only index with given name. This option can be supplied
more than once to reindex multiple indices.
== CONTEXT
The secondary index must be enabled. See
link:config-gerrit.html#index.type[index.type].

View File

@ -17,6 +17,10 @@ package com.google.gerrit.pgm;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gerrit.server.schema.DataSourceProvider.Context.MULTI_USER;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.gerrit.common.Die;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.lifecycle.LifecycleManager;
@ -47,6 +51,8 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
public class Reindex extends SiteProgram {
@ -63,6 +69,9 @@ public class Reindex extends SiteProgram {
@Option(name = "--list", usage = "List supported indices and exit")
private boolean list;
@Option(name = "--index", usage = "Only reindex specified indices")
private List<String> indices = new ArrayList<>();
private Injector dbInjector;
private Injector sysInjector;
private Config globalConfig;
@ -89,6 +98,7 @@ public class Reindex extends SiteProgram {
sysManager.add(sysInjector);
sysManager.start();
sysInjector.injectMembers(this);
checkIndicesOption();
try {
boolean ok = list ? list() : reindex();
@ -111,11 +121,36 @@ public class Reindex extends SiteProgram {
private boolean reindex() throws IOException {
boolean ok = true;
for (IndexDefinition<?, ?, ?> def : indexDefs) {
ok &= reindex(def);
if (indices.isEmpty() || indices.contains(def.getName())) {
ok &= reindex(def);
}
}
return ok;
}
private void checkIndicesOption() throws Die {
if (indices.isEmpty()) {
return;
}
checkNotNull(indexDefs, "Called this method before injectMembers?");
Set<String> valid = FluentIterable.from(indexDefs).transform(
new Function<IndexDefinition<?, ?, ?>, String>() {
@Override
public String apply(IndexDefinition<?, ?, ?> input) {
return input.getName();
}
}).toSortedSet(Ordering.natural());
Set<String> invalid = Sets.difference(Sets.newHashSet(indices), valid);
if (invalid.isEmpty()) {
return;
}
throw die("invalid index name(s): " + new TreeSet<>(invalid)
+ " available indices are: " + valid);
}
private void checkNotSlaveMode() throws Die {
if (globalConfig.getBoolean("container", "slave", false)) {
throw die("Cannot run reindex in slave mode");