ES: Extract Jest client builder into separate class

Extract jest client builder from AbstractElasticIndex into separate
class. This  way we would be able to create other elastic client for
online reindexing purposes like eg. listing current index versios.

Change-Id: I3ab4e94e9f8e24cf0f3085370d3e32cf18eb570b
This commit is contained in:
Dariusz Luksza
2017-04-24 11:51:33 +02:00
parent 831c40cce9
commit ca1c967a68
5 changed files with 95 additions and 51 deletions

View File

@@ -19,7 +19,6 @@ import static java.util.stream.Collectors.toList;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
@@ -36,9 +35,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gwtorm.protobuf.ProtobufCodec;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.http.JestHttpClient;
import io.searchbox.core.Bulk;
import io.searchbox.core.Delete;
@@ -46,10 +43,7 @@ import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.IndicesExists;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -80,45 +74,21 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
FillArgs fillArgs,
SitePaths sitePaths,
Schema<V> schema,
JestClientBuilder clientBuilder,
String indexName) {
this.fillArgs = fillArgs;
this.sitePaths = sitePaths;
this.schema = schema;
this.gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
this.queryBuilder = new ElasticQueryBuilder();
String protocol = MoreObjects.firstNonNull(cfg.getString("index", null, "protocol"), "http");
String hostname =
MoreObjects.firstNonNull(cfg.getString("index", null, "hostname"), "localhost");
String port = String.valueOf(cfg.getInt("index", null, "port", 9200));
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
// refresh immediately.
//
// Discovery should be disabled during test mode to prevent spurious
// connection failures caused by the client starting up and being ready
// before the test node.
//
// This setting should only be set to true during testing, and is not
// documented.
this.refresh = cfg.getBoolean("index", "elasticsearch", "test", false);
String url = buildUrl(protocol, hostname, port);
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(
new HttpClientConfig.Builder(url)
.multiThreaded(true)
.discoveryEnabled(!refresh)
.discoveryFrequency(1L, TimeUnit.MINUTES)
.build());
client = (JestHttpClient) factory.getObject();
this.refresh = clientBuilder.refresh;
this.client = clientBuilder.build();
}
@Override
@@ -207,19 +177,4 @@ abstract class AbstractElasticIndex<K, V> implements Index<K, V> {
}
return builder.endObject().string();
}
private String buildUrl(String protocol, String hostname, String port) {
try {
return new URL(protocol, hostname, Integer.parseInt(port), "").toString();
} catch (MalformedURLException | NumberFormatException e) {
throw new RuntimeException(
"Cannot build url to Elasticsearch from values: protocol="
+ protocol
+ " hostname="
+ hostname
+ " port="
+ port,
e);
}
}
}

View File

@@ -81,9 +81,10 @@ public class ElasticAccountIndex extends AbstractElasticIndex<Account.Id, Accoun
@GerritServerConfig Config cfg,
SitePaths sitePaths,
Provider<AccountCache> accountCache,
JestClientBuilder clientBuilder,
@Assisted Schema<AccountState> schema) {
// No parts of FillArgs are currently required, just use null.
super(cfg, null, sitePaths, schema, ACCOUNTS_PREFIX);
super(cfg, null, sitePaths, schema, clientBuilder, ACCOUNTS_PREFIX);
this.accountCache = accountCache;
this.mapping = new AccountMapping(schema);
}

View File

@@ -107,8 +107,9 @@ class ElasticChangeIndex extends AbstractElasticIndex<Change.Id, ChangeData>
ChangeData.Factory changeDataFactory,
FillArgs fillArgs,
SitePaths sitePaths,
JestClientBuilder clientBuilder,
@Assisted Schema<ChangeData> schema) {
super(cfg, fillArgs, sitePaths, schema, CHANGES_PREFIX);
super(cfg, fillArgs, sitePaths, schema, clientBuilder, CHANGES_PREFIX);
this.db = db;
this.changeDataFactory = changeDataFactory;
mapping = new ChangeMapping(schema);

View File

@@ -78,9 +78,10 @@ public class ElasticGroupIndex extends AbstractElasticIndex<AccountGroup.UUID, A
@GerritServerConfig Config cfg,
SitePaths sitePaths,
Provider<GroupCache> groupCache,
JestClientBuilder clientBuilder,
@Assisted Schema<AccountGroup> schema) {
// No parts of FillArgs are currently required, just use null.
super(cfg, null, sitePaths, schema, GROUPS_PREFIX);
super(cfg, null, sitePaths, schema, clientBuilder, GROUPS_PREFIX);
this.groupCache = groupCache;
this.mapping = new GroupMapping(schema);
}

View File

@@ -0,0 +1,86 @@
// Copyright (C) 2017 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.elasticsearch;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import com.google.common.base.MoreObjects;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.http.JestHttpClient;
@Singleton
class JestClientBuilder {
private final String url;
final boolean refresh;
@Inject
JestClientBuilder(@GerritServerConfig Config cfg) {
String protocol = MoreObjects.firstNonNull(cfg.getString("index", null, "protocol"), "http");
String hostname =
MoreObjects.firstNonNull(cfg.getString("index", null, "hostname"), "localhost");
String port = String.valueOf(cfg.getInt("index", null, "port", 9200));
// 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
// refresh immediately.
//
// Discovery should be disabled during test mode to prevent spurious
// connection failures caused by the client starting up and being ready
// before the test node.
//
// This setting should only be set to true during testing, and is not
// documented.
this.refresh = cfg.getBoolean("index", "elasticsearch", "test", false);
this.url = buildUrl(protocol, hostname, port);
}
JestHttpClient build() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(
new HttpClientConfig.Builder(url)
.multiThreaded(true)
.discoveryEnabled(!refresh)
.discoveryFrequency(1L, TimeUnit.MINUTES)
.build());
return (JestHttpClient) factory.getObject();
}
private String buildUrl(String protocol, String hostname, String port) {
try {
return new URL(protocol, hostname, Integer.parseInt(port), "").toString();
} catch (MalformedURLException | NumberFormatException e) {
throw new RuntimeException(
"Cannot build url to Elasticsearch from values: protocol="
+ protocol
+ " hostname="
+ hostname
+ " port="
+ port,
e);
}
}
}