diff --git a/Documentation/cmd-index-activate.txt b/Documentation/cmd-index-activate.txt index 37783ef013..0135cfee3c 100644 --- a/Documentation/cmd-index-activate.txt +++ b/Documentation/cmd-index-activate.txt @@ -5,7 +5,7 @@ gerrit index activate - Activate the latest index version available == SYNOPSIS -- -'ssh' -p @SSH_PORT@ @SSH_HOST@ 'gerrit index activate ' +'ssh' -p @SSH_PORT@ @SSH_HOST@ 'gerrit index activate' -- == DESCRIPTION @@ -18,9 +18,6 @@ number of successfully/failed indexed changes. This command allows to activate the latest index even if there were some failures. -The argument controls which secondary index is activated. Currently, the -only supported value is "changes". - == ACCESS Caller must be a member of the privileged 'Administrators' group. diff --git a/Documentation/cmd-index-start.txt b/Documentation/cmd-index-start.txt index cee283e45d..4148b2405c 100644 --- a/Documentation/cmd-index-start.txt +++ b/Documentation/cmd-index-start.txt @@ -5,7 +5,7 @@ gerrit index start - Start the online indexer == SYNOPSIS -- -'ssh' -p @SSH_PORT@ @SSH_HOST@ 'gerrit index start ' +'ssh' -p @SSH_PORT@ @SSH_HOST@ 'gerrit index start' -- == DESCRIPTION @@ -19,9 +19,6 @@ This command allows restarting the online indexer without having to restart Gerrit. This command will not start the indexer if it is already running or if the active index is the latest. -The argument controls which secondary index is started. Currently, the -only supported value is "changes". - == ACCESS Caller must be a member of the privileged 'Administrators' group. 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 2c46fd3225..9dd439941f 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 @@ -20,14 +20,15 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.primitives.Ints; import com.google.gerrit.extensions.events.LifecycleListener; +import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.SitePaths; -import com.google.gerrit.server.index.Index; -import com.google.gerrit.server.index.IndexCollection; -import com.google.gerrit.server.index.IndexDefinition; -import com.google.gerrit.server.index.IndexDefinition.IndexFactory; import com.google.gerrit.server.index.OnlineReindexer; import com.google.gerrit.server.index.Schema; +import com.google.gerrit.server.index.change.ChangeIndex; +import com.google.gerrit.server.index.change.ChangeIndexCollection; +import com.google.gerrit.server.index.change.ChangeIndexDefinition; +import com.google.gerrit.server.query.change.ChangeData; import com.google.inject.Inject; import com.google.inject.ProvisionException; import com.google.inject.Singleton; @@ -45,7 +46,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.TreeMap; @Singleton @@ -55,13 +55,13 @@ public class LuceneVersionManager implements LifecycleListener { static final String CHANGES_PREFIX = "changes_"; - private static class Version { - private final Schema schema; + private static class Version { + private final Schema schema; private final int version; private final boolean exists; private final boolean ready; - private Version(Schema schema, int version, boolean exists, + private Version(Schema schema, int version, boolean exists, boolean ready) { checkArgument(schema == null || schema.getVersion() == version); this.schema = schema; @@ -94,32 +94,33 @@ public class LuceneVersionManager implements LifecycleListener { } private final SitePaths sitePaths; - private final Map> defs; - private final Map> reindexers; + private final LuceneChangeIndex.Factory indexFactory; + private final ChangeIndexCollection indexes; + private final ChangeIndexDefinition changeDef; private final boolean onlineUpgrade; - private final String runReindexMsg; + private OnlineReindexer reindexer; @Inject LuceneVersionManager( @GerritServerConfig Config cfg, SitePaths sitePaths, - Collection> defs) { + LuceneChangeIndex.Factory indexFactory, + ChangeIndexCollection indexes, + ChangeIndexDefinition changeDef) { this.sitePaths = sitePaths; - this.defs = Maps.newHashMapWithExpectedSize(defs.size()); - for (IndexDefinition def : defs) { - this.defs.put(def.getName(), def); - } - - reindexers = Maps.newHashMapWithExpectedSize(defs.size()); - onlineUpgrade = cfg.getBoolean("index", null, "onlineUpgrade", true); - runReindexMsg = - "No index versions ready; run java -jar " + - sitePaths.gerrit_war.toAbsolutePath() + - " reindex"; + this.indexFactory = indexFactory; + this.indexes = indexes; + this.changeDef = changeDef; + this.onlineUpgrade = cfg.getBoolean("index", null, "onlineUpgrade", true); } @Override public void start() { + String runReindex = + "No index versions ready; run java -jar " + + sitePaths.gerrit_war.toAbsolutePath() + + " reindex"; + FileBasedConfig cfg; try { cfg = loadGerritIndexConfig(sitePaths); @@ -128,25 +129,18 @@ public class LuceneVersionManager implements LifecycleListener { } if (!Files.exists(sitePaths.index_dir)) { - throw new ProvisionException(runReindexMsg); + throw new ProvisionException(runReindex); } else if (!Files.exists(sitePaths.index_dir)) { log.warn("Not a directory: %s", sitePaths.index_dir.toAbsolutePath()); - throw new ProvisionException(runReindexMsg); + throw new ProvisionException(runReindex); } - for (IndexDefinition def : defs.values()) { - initIndex(def, cfg); - } - } - - private > void initIndex( - IndexDefinition def, FileBasedConfig cfg) { - TreeMap> versions = scanVersions(def, cfg); + TreeMap versions = scanVersions(cfg); // Search from the most recent ready version. // Write to the most recent ready version and the most recent version. - Version search = null; - List> write = Lists.newArrayListWithCapacity(2); - for (Version v : versions.descendingMap().values()) { + Version search = null; + List write = Lists.newArrayListWithCapacity(2); + for (Version v : versions.descendingMap().values()) { if (v.schema == null) { continue; } @@ -162,35 +156,27 @@ public class LuceneVersionManager implements LifecycleListener { } } if (search == null) { - throw new ProvisionException(runReindexMsg); + throw new ProvisionException(runReindex); } - IndexFactory factory = def.getIndexFactory(); - I searchIndex = factory.create(search.schema); - IndexCollection indexes = def.getIndexCollection(); + markNotReady(cfg, versions.values(), write); + LuceneChangeIndex searchIndex = + (LuceneChangeIndex) indexFactory.create(search.schema); indexes.setSearchIndex(searchIndex); - for (Version v : write) { + for (Version v : write) { if (v.schema != null) { if (v.version != search.version) { - indexes.addWriteIndex(factory.create(v.schema)); + indexes.addWriteIndex(indexFactory.create(v.schema)); + } else { + indexes.addWriteIndex(searchIndex); } - } else { - indexes.addWriteIndex(searchIndex); } } - // TODO: include index name. - markNotReady(cfg, versions.values(), write); - int latest = write.get(0).version; 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(); - } - } + reindexer = new OnlineReindexer<>(changeDef, latest); + reindexer.start(); } } @@ -200,11 +186,10 @@ public class LuceneVersionManager implements LifecycleListener { * @return true if started, otherwise false. * @throws ReindexerAlreadyRunningException */ - public synchronized boolean startReindexer(String name) + public synchronized boolean startReindexer() throws ReindexerAlreadyRunningException { - OnlineReindexer reindexer = reindexers.get(name); - validateReindexerNotRunning(reindexer); - if (!isCurrentIndexVersionLatest(name, reindexer)) { + validateReindexerNotRunning(); + if (!isCurrentIndexVersionLatest()) { reindexer.start(); return true; } @@ -217,56 +202,49 @@ public class LuceneVersionManager implements LifecycleListener { * @return true if index was activate, otherwise false. * @throws ReindexerAlreadyRunningException */ - public synchronized boolean activateLatestIndex(String name) + public synchronized boolean activateLatestIndex() throws ReindexerAlreadyRunningException { - OnlineReindexer reindexer = reindexers.get(name); - validateReindexerNotRunning(reindexer); - if (!isCurrentIndexVersionLatest(name, reindexer)) { + validateReindexerNotRunning(); + if (!isCurrentIndexVersionLatest()) { reindexer.activateIndex(); return true; } return false; } - private boolean isCurrentIndexVersionLatest( - String name, OnlineReindexer reindexer) { - int readVersion = defs.get(name).getIndexCollection().getSearchIndex() - .getSchema().getVersion(); + private boolean isCurrentIndexVersionLatest() { return reindexer == null - || reindexer.getVersion() == readVersion; + || reindexer.getVersion() == indexes.getSearchIndex().getSchema() + .getVersion(); } - private static void validateReindexerNotRunning( - OnlineReindexer reindexer) + private void validateReindexerNotRunning() throws ReindexerAlreadyRunningException { if (reindexer != null && reindexer.isRunning()) { throw new ReindexerAlreadyRunningException(); } } - private > TreeMap> - scanVersions(IndexDefinition def, Config cfg) { - TreeMap> versions = Maps.newTreeMap(); - for (Schema schema : def.getSchemas().values()) { - // This part is Lucene-specific. - Path p = getDir(sitePaths, def.getName(), schema); + private TreeMap scanVersions(Config cfg) { + TreeMap versions = Maps.newTreeMap(); + for (Schema schema : changeDef.getSchemas().values()) { + Path p = getDir(sitePaths, CHANGES_PREFIX, schema); boolean isDir = Files.isDirectory(p); if (Files.exists(p) && !isDir) { log.warn("Not a directory: %s", p.toAbsolutePath()); } int v = schema.getVersion(); - versions.put(v, new Version<>(schema, v, isDir, getReady(cfg, v))); + versions.put(v, new Version(schema, v, isDir, getReady(cfg, v))); } - String prefix = def.getName() + "_"; try (DirectoryStream paths = Files.newDirectoryStream(sitePaths.index_dir)) { for (Path p : paths) { String n = p.getFileName().toString(); - if (!n.startsWith(prefix)) { + if (!n.startsWith(CHANGES_PREFIX)) { continue; } - String versionStr = n.substring(prefix.length()); + String versionStr = n.substring(CHANGES_PREFIX.length()); Integer v = Ints.tryParse(versionStr); if (v == null || versionStr.length() != 4) { log.warn("Unrecognized version in index directory: {}", @@ -274,7 +252,7 @@ public class LuceneVersionManager implements LifecycleListener { continue; } if (!versions.containsKey(v)) { - versions.put(v, new Version(null, v, true, getReady(cfg, v))); + versions.put(v, new Version(null, v, true, getReady(cfg, v))); } } } catch (IOException e) { @@ -283,10 +261,10 @@ public class LuceneVersionManager implements LifecycleListener { return versions; } - private void markNotReady(FileBasedConfig cfg, Iterable> versions, - Collection> inUse) { + private void markNotReady(FileBasedConfig cfg, Iterable versions, + Collection inUse) { boolean dirty = false; - for (Version v : versions) { + for (Version v : versions) { if (!inUse.contains(v) && v.exists) { setReady(cfg, v.version, false); dirty = true; diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexActivateCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexActivateCommand.java index c508b1de57..dc67ac3fa5 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexActivateCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/IndexActivateCommand.java @@ -24,25 +24,19 @@ import com.google.gerrit.sshd.CommandMetaData; import com.google.gerrit.sshd.SshCommand; import com.google.inject.Inject; -import org.kohsuke.args4j.Argument; - @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) @CommandMetaData(name = "activate", description = "Activate the latest index version available", runsAt = MASTER) public class IndexActivateCommand extends SshCommand { - @Argument(index = 0, required = true, metaVar = "INDEX", - usage = "index name to activate") - private String name; - @Inject private LuceneVersionManager luceneVersionManager; @Override protected void run() throws UnloggedFailure { try { - if (luceneVersionManager.activateLatestIndex(name)) { + if (luceneVersionManager.activateLatestIndex()) { stdout.println("Activated latest index version"); } else { stdout.println("Not activating index, already using latest version"); 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 1575ed9368..1b3b8190d8 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 @@ -24,24 +24,18 @@ import com.google.gerrit.sshd.CommandMetaData; import com.google.gerrit.sshd.SshCommand; import com.google.inject.Inject; -import org.kohsuke.args4j.Argument; - @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) @CommandMetaData(name = "start", description = "Start the online reindexer", runsAt = MASTER) public class IndexStartCommand extends SshCommand { - @Argument(index = 0, required = true, metaVar = "INDEX", - usage = "index name to activate") - private String name; - @Inject private LuceneVersionManager luceneVersionManager; @Override protected void run() throws UnloggedFailure { try { - if (luceneVersionManager.startReindexer(name)) { + if (luceneVersionManager.startReindexer()) { stdout.println("Reindexer started"); } else { stdout.println("Nothing to reindex, index is already the latest version");