Extract gerrit_index.config into its own class

Change-Id: I5cdc77239dd0c8d6a2732220738c35ef88d33016
This commit is contained in:
Dave Borowitz
2016-03-18 12:38:18 +01:00
committed by David Ostrovsky
parent a209d9a4c4
commit 854cc5419d
5 changed files with 101 additions and 45 deletions

View File

@@ -48,7 +48,6 @@ import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -70,13 +69,11 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
return f.getName() + "_SORT"; return f.getName() + "_SORT";
} }
public static void setReady(SitePaths sitePaths, int version, boolean ready) public static void setReady(SitePaths sitePaths, String name, int version,
throws IOException { boolean ready) throws IOException {
try { try {
// TODO(dborowitz): Totally broken for non-change indexes. GerritIndexStatus cfg = new GerritIndexStatus(sitePaths);
FileBasedConfig cfg = cfg.setReady(name, version, ready);
LuceneVersionManager.loadGerritIndexConfig(sitePaths);
LuceneVersionManager.setReady(cfg, version, ready);
cfg.save(); cfg.save();
} catch (ConfigInvalidException e) { } catch (ConfigInvalidException e) {
throw new IOException(e); throw new IOException(e);
@@ -86,6 +83,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
private final Schema<V> schema; private final Schema<V> schema;
private final SitePaths sitePaths; private final SitePaths sitePaths;
private final Directory dir; private final Directory dir;
private final String name;
private final TrackingIndexWriter writer; private final TrackingIndexWriter writer;
private final ReferenceManager<IndexSearcher> searcherManager; private final ReferenceManager<IndexSearcher> searcherManager;
private final ControlledRealTimeReopenThread<IndexSearcher> reopenThread; private final ControlledRealTimeReopenThread<IndexSearcher> reopenThread;
@@ -102,6 +100,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
this.schema = schema; this.schema = schema;
this.sitePaths = sitePaths; this.sitePaths = sitePaths;
this.dir = dir; this.dir = dir;
this.name = name;
final String index = Joiner.on('_').skipNulls().join(name, subIndex); final String index = Joiner.on('_').skipNulls().join(name, subIndex);
IndexWriter delegateWriter; IndexWriter delegateWriter;
long commitPeriod = writerConfig.getCommitWithinMs(); long commitPeriod = writerConfig.getCommitWithinMs();
@@ -184,7 +183,7 @@ public abstract class AbstractLuceneIndex<K, V> implements Index<K, V> {
@Override @Override
public void markReady(boolean ready) throws IOException { public void markReady(boolean ready) throws IOException {
setReady(sitePaths, schema.getVersion(), ready); setReady(sitePaths, name, schema.getVersion(), ready);
} }
@Override @Override

View File

@@ -0,0 +1,78 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.lucene;
import com.google.common.primitives.Ints;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.change.ChangeSchemaDefinitions;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import java.io.IOException;
class GerritIndexStatus {
private static final String SECTION = "index";
private static final String KEY_READY = "ready";
private final FileBasedConfig cfg;
GerritIndexStatus(SitePaths sitePaths)
throws ConfigInvalidException, IOException {
cfg = new FileBasedConfig(
sitePaths.index_dir.resolve("gerrit_index.config").toFile(),
FS.detect());
cfg.load();
convertLegacyConfig();
}
void setReady(String indexName, int version, boolean ready) {
cfg.setBoolean(SECTION, indexDirName(indexName, version), KEY_READY, ready);
}
boolean getReady(String indexName, int version) {
return cfg.getBoolean(SECTION, indexDirName(indexName, version), KEY_READY,
false);
}
void save() throws IOException {
cfg.save();
}
private void convertLegacyConfig() throws IOException {
boolean dirty = false;
// Convert legacy [index "25"] to modern [index "changes_0025"].
for (String subsection : cfg.getSubsections(SECTION)) {
Integer v = Ints.tryParse(subsection);
if (v != null) {
String ready = cfg.getString(SECTION, subsection, KEY_READY);
if (ready != null) {
dirty = false;
cfg.unset(SECTION, subsection, KEY_READY);
cfg.setString(SECTION,
indexDirName(ChangeSchemaDefinitions.NAME, v), KEY_READY, ready);
}
}
}
if (dirty) {
cfg.save();
}
}
private static String indexDirName(String indexName, int version) {
return String.format("%s_%04d", indexName, version);
}
}

View File

@@ -126,7 +126,6 @@ public class LuceneChangeIndex implements ChangeIndex {
return QueryBuilder.intTerm(LEGACY_ID.getName(), id.get()); return QueryBuilder.intTerm(LEGACY_ID.getName(), id.get());
} }
private final SitePaths sitePaths;
private final FillArgs fillArgs; private final FillArgs fillArgs;
private final ListeningExecutorService executor; private final ListeningExecutorService executor;
private final Provider<ReviewDb> db; private final Provider<ReviewDb> db;
@@ -145,7 +144,6 @@ public class LuceneChangeIndex implements ChangeIndex {
ChangeData.Factory changeDataFactory, ChangeData.Factory changeDataFactory,
FillArgs fillArgs, FillArgs fillArgs,
@Assisted Schema<ChangeData> schema) throws IOException { @Assisted Schema<ChangeData> schema) throws IOException {
this.sitePaths = sitePaths;
this.fillArgs = fillArgs; this.fillArgs = fillArgs;
this.executor = executor; this.executor = executor;
this.db = db; this.db = db;
@@ -252,9 +250,9 @@ public class LuceneChangeIndex implements ChangeIndex {
@Override @Override
public void markReady(boolean ready) throws IOException { public void markReady(boolean ready) throws IOException {
// Do not delegate to ChangeSubIndex#markReady, since changes have an // Arbitrary done on open index, as ready bit is set
// additional level of directory nesting. // per index and not sub index
AbstractLuceneIndex.setReady(sitePaths, schema.getVersion(), ready); openIndex.markReady(ready);
} }
private Sort getSort() { private Sort getSort() {

View File

@@ -34,8 +34,6 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -76,23 +74,6 @@ public class LuceneVersionManager implements LifecycleListener {
prefix, schema.getVersion())); prefix, schema.getVersion()));
} }
static FileBasedConfig loadGerritIndexConfig(SitePaths sitePaths)
throws ConfigInvalidException, IOException {
FileBasedConfig cfg = new FileBasedConfig(
sitePaths.index_dir.resolve("gerrit_index.config").toFile(),
FS.detect());
cfg.load();
return cfg;
}
static void setReady(Config cfg, int version, boolean ready) {
cfg.setBoolean("index", Integer.toString(version), "ready", ready);
}
private static boolean getReady(Config cfg, int version) {
return cfg.getBoolean("index", Integer.toString(version), "ready", false);
}
private final SitePaths sitePaths; private final SitePaths sitePaths;
private final Map<String, IndexDefinition<?, ?, ?>> defs; private final Map<String, IndexDefinition<?, ?, ?>> defs;
private final Map<String, OnlineReindexer<?, ?, ?>> reindexers; private final Map<String, OnlineReindexer<?, ?, ?>> reindexers;
@@ -120,9 +101,9 @@ public class LuceneVersionManager implements LifecycleListener {
@Override @Override
public void start() { public void start() {
FileBasedConfig cfg; GerritIndexStatus cfg;
try { try {
cfg = loadGerritIndexConfig(sitePaths); cfg = new GerritIndexStatus(sitePaths);
} catch (ConfigInvalidException | IOException e) { } catch (ConfigInvalidException | IOException e) {
throw fail(e); throw fail(e);
} }
@@ -140,7 +121,7 @@ public class LuceneVersionManager implements LifecycleListener {
} }
private <K, V, I extends Index<K, V>> void initIndex( private <K, V, I extends Index<K, V>> void initIndex(
IndexDefinition<K, V, I> def, FileBasedConfig cfg) { IndexDefinition<K, V, I> def, GerritIndexStatus cfg) {
TreeMap<Integer, Version<V>> versions = scanVersions(def, cfg); TreeMap<Integer, Version<V>> versions = scanVersions(def, cfg);
// Search from the most recent ready version. // Search from the most recent ready version.
// Write to the most recent ready version and the most recent version. // Write to the most recent ready version and the most recent version.
@@ -179,8 +160,7 @@ public class LuceneVersionManager implements LifecycleListener {
} }
} }
// TODO: include index name. markNotReady(cfg, def.getName(), versions.values(), write);
markNotReady(cfg, versions.values(), write);
int latest = write.get(0).version; int latest = write.get(0).version;
if (onlineUpgrade && latest != search.version) { if (onlineUpgrade && latest != search.version) {
@@ -245,7 +225,7 @@ public class LuceneVersionManager implements LifecycleListener {
} }
private <K, V, I extends Index<K, V>> TreeMap<Integer, Version<V>> private <K, V, I extends Index<K, V>> TreeMap<Integer, Version<V>>
scanVersions(IndexDefinition<K, V, I> def, Config cfg) { scanVersions(IndexDefinition<K, V, I> def, GerritIndexStatus cfg) {
TreeMap<Integer, Version<V>> versions = Maps.newTreeMap(); TreeMap<Integer, Version<V>> versions = Maps.newTreeMap();
for (Schema<V> schema : def.getSchemas().values()) { for (Schema<V> schema : def.getSchemas().values()) {
// This part is Lucene-specific. // This part is Lucene-specific.
@@ -255,7 +235,8 @@ public class LuceneVersionManager implements LifecycleListener {
log.warn("Not a directory: %s", p.toAbsolutePath()); log.warn("Not a directory: %s", p.toAbsolutePath());
} }
int v = schema.getVersion(); int v = schema.getVersion();
versions.put(v, new Version<>(schema, v, isDir, getReady(cfg, v))); versions.put(v, new Version<>(
schema, v, isDir, cfg.getReady(def.getName(), v)));
} }
String prefix = def.getName() + "_"; String prefix = def.getName() + "_";
@@ -274,7 +255,8 @@ public class LuceneVersionManager implements LifecycleListener {
continue; continue;
} }
if (!versions.containsKey(v)) { if (!versions.containsKey(v)) {
versions.put(v, new Version<V>(null, v, true, getReady(cfg, v))); versions.put(v, new Version<V>(
null, v, true, cfg.getReady(def.getName(), v)));
} }
} }
} catch (IOException e) { } catch (IOException e) {
@@ -283,12 +265,12 @@ public class LuceneVersionManager implements LifecycleListener {
return versions; return versions;
} }
private <V> void markNotReady(FileBasedConfig cfg, Iterable<Version<V>> versions, private <V> void markNotReady(GerritIndexStatus cfg, String name,
Collection<Version<V>> inUse) { Iterable<Version<V>> versions, Collection<Version<V>> inUse) {
boolean dirty = false; boolean dirty = false;
for (Version<V> v : versions) { for (Version<V> v : versions) {
if (!inUse.contains(v) && v.exists) { if (!inUse.contains(v) && v.exists) {
setReady(cfg, v.version, false); cfg.setReady(name, v.version, false);
dirty = true; dirty = true;
} }
} }

View File

@@ -59,9 +59,8 @@ class InitIndex implements InitStep {
IndexType type = index.select("Type", "type", IndexType.LUCENE); IndexType type = index.select("Type", "type", IndexType.LUCENE);
for (SchemaDefinitions<?> def : IndexModule.ALL_SCHEMA_DEFS) { for (SchemaDefinitions<?> def : IndexModule.ALL_SCHEMA_DEFS) {
// TODO(dborowitz): Totally broken for non-change indexes.
AbstractLuceneIndex.setReady( AbstractLuceneIndex.setReady(
site, def.getLatest().getVersion(), true); site, def.getName(), def.getLatest().getVersion(), true);
} }
if ((site.isNew || isEmptySite()) && type == IndexType.LUCENE) { if ((site.isNew || isEmptySite()) && type == IndexType.LUCENE) {
// Do nothing // Do nothing