Name Elasticsearch indexes the same way it's done for Lucene

Change index is now named 'changes_0001' instead of 'gerrit'. This will
enable to have other indexes like the accounts index and multiple
versions to support online reindexing.

The only difference is Elasticsearch allows to prefix the index name to
allow to have use more than one Gerrit server in the same Elasticsearch
server. Prefix 'gerrit1_' would result in a change index named
'gerrit1_changes_0001'.

Change-Id: I9d089b6a0ce0e708a59e1f322ea1c011f78ffd31
This commit is contained in:
Hugo Arès 2016-11-15 15:12:55 -08:00
parent 3b943ab0e8
commit 3ba6dfe693
4 changed files with 19 additions and 13 deletions

View File

@ -2628,12 +2628,13 @@ Elasticsearch server port.
+
Defauls to `9200`.
[[index.name]]index.name::
[[index.prefix]]index.prefix::
+
This setting can be used to index changes from multiple Gerrit
instances in a single Elasticsearch cluster.
This setting can be used to prefix index names to allow multiple Gerrit
instances in a single Elasticsearch cluster. Prefix 'gerrit1_' would result in a
change index named 'gerrit1_changes_0001'.
+
Defaults to 'gerrit'.
Not set by default.
[[ldap]]
=== Section ldap

View File

@ -14,7 +14,6 @@
package com.google.gerrit.elasticsearch;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkState;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -47,7 +46,6 @@ import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.IndicesExists;
abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
private static final String DEFAULT_INDEX_NAME = "gerrit";
private final Schema<V> schema;
private final FillArgs fillArgs;
@ -61,7 +59,8 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
AbstractElasticIndex(@GerritServerConfig Config cfg,
FillArgs fillArgs,
SitePaths sitePaths,
Schema<V> schema) {
Schema<V> schema,
String indexName) {
this.fillArgs = fillArgs;
this.sitePaths = sitePaths;
this.schema = schema;
@ -69,8 +68,10 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
String hostname = getRequiredConfigOption(cfg, "hostname");
String port = getRequiredConfigOption(cfg, "port");
this.indexName =
firstNonNull(cfg.getString("index", null, "name"), DEFAULT_INDEX_NAME);
this.indexName = String.format("%s%s%04d",
Strings.nullToEmpty(cfg.getString("index", null, "prefix")),
indexName,
schema.getVersion());
// By default Elasticsearch has a 1s delay before changes are available in
// the index. Setting refresh(true) on calls to the index makes the index

View File

@ -121,6 +121,7 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
}
}
static final String CHANGES_PREFIX = "changes_";
static final String OPEN_CHANGES = "open_changes";
static final String CLOSED_CHANGES = "closed_changes";
@ -138,7 +139,7 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
FillArgs fillArgs,
SitePaths sitePaths,
@Assisted Schema<ChangeData> schema) {
super(cfg, fillArgs, sitePaths, schema);
super(cfg, fillArgs, sitePaths, schema, CHANGES_PREFIX);
this.db = db;
this.changeDataFactory = changeDataFactory;
mapping = new ChangeMapping(schema);

View File

@ -16,6 +16,7 @@ package com.google.gerrit.elasticsearch;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CHANGES_PREFIX;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CLOSED_CHANGES;
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.OPEN_CHANGES;
@ -51,6 +52,9 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
private static final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
private static final String INDEX_NAME =
String.format("%s%04d", CHANGES_PREFIX,
ChangeSchemaDefinitions.INSTANCE.getLatest().getVersion());
private static Node node;
private static String port;
private static File elasticDir;
@ -112,7 +116,7 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
@After
public void cleanupIndex() {
node.client().admin().indices().prepareDelete("gerrit").execute();
node.client().admin().indices().prepareDelete(INDEX_NAME).execute();
createIndexes();
}
@ -135,7 +139,6 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
elasticsearchConfig.setString("index", null, "protocol", "http");
elasticsearchConfig.setString("index", null, "hostname", "localhost");
elasticsearchConfig.setString("index", null, "port", port);
elasticsearchConfig.setString("index", null, "name", "gerrit");
elasticsearchConfig.setBoolean("index", "elasticsearch", "test", true);
return Guice.createInjector(
new InMemoryModule(elasticsearchConfig, notesMigration));
@ -151,7 +154,7 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
node.client()
.admin()
.indices()
.prepareCreate("gerrit")
.prepareCreate(INDEX_NAME)
.addMapping(OPEN_CHANGES, gson.toJson(openChangesMapping))
.addMapping(CLOSED_CHANGES, gson.toJson(closedChangesMapping))
.execute()