Merge changes I9d089b6a,Ida524865

* changes:
  Name Elasticsearch indexes the same way it's done for Lucene
  Remove injection annotations from constructor in AbstractElasticIndex
This commit is contained in:
David Pursehouse 2016-11-16 02:51:51 +00:00 committed by Gerrit Code Review
commit 5442fe17e5
4 changed files with 19 additions and 16 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;
@ -27,8 +26,6 @@ import com.google.gerrit.server.index.Index;
import com.google.gerrit.server.index.IndexUtils;
import com.google.gerrit.server.index.Schema;
import com.google.gerrit.server.index.Schema.Values;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -49,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;
@ -60,11 +56,11 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
protected final JestHttpClient client;
@Inject
AbstractElasticIndex(@GerritServerConfig Config cfg,
FillArgs fillArgs,
SitePaths sitePaths,
@Assisted Schema<V> schema) {
Schema<V> schema,
String indexName) {
this.fillArgs = fillArgs;
this.sitePaths = sitePaths;
this.schema = schema;
@ -72,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()