Elasticsearch: replace native API in prod w/ REST
Remove jest API which depends on Elasticsearch native API (also removed), so no longer depend on lucene for this scope. Replace such removed dependencies with the Elasticsearch low-level API, which does not depend on Lucene. That now used API is a REST client with minimal dependencies, indeed. -As opposed to Elastic's current high-level API, which still depends on lucene and other Gerrit-constraining libraries. Do so in order to decouple the elasticsearch client from lucene in Gerrit. That REST client does not logically require Lucene anyway. Most importantly, such decoupling now enables upcoming support for more than just one Elasticsearch server version. This includes the new possibility of bumping the latter to multiple yet later versions, such as 5 and 6.x. The currently used version (2.4) should also still be kept, for a while. Bumping such versions will likely require some Elasticsearch client code adaptations, so that Gerrit can (dynamically?) switch between either Elasticsearch server version to eventually support (hopefully soon). Doing the same for the elasticsearch tests in Gerrit is to be done using another change or more changes. Add a partial and customized fork of [1], based on [1]'s commit [2], to preserve the ability of building proper json requests for Elasticsearch. Put that forked json-generating code under a new 'builders' sub-package. There should be a possibility in some near future to consider removing that fork, based on potential progress such as the one proposed in [3]. Meanwhile, this fork shall be maintained to usual Gerrit quality levels. [1] https://github.com/elastic/elasticsearch [2] tag: v2.4.4 [3] https://github.com/elastic/elasticsearch/issues/30791 Bug: Issue 6094 Change-Id: I720c9885c9eab2388acc328eecb9eaa6940ced0c
This commit is contained in:
committed by
David Pursehouse
parent
172cc35d2d
commit
f9758fd8bb
@@ -18,13 +18,12 @@ import com.google.common.base.MoreObjects;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
|
||||
@Singleton
|
||||
@@ -33,7 +32,7 @@ class ElasticConfiguration {
|
||||
private static final String DEFAULT_PORT = "9200";
|
||||
private static final String DEFAULT_PROTOCOL = "http";
|
||||
|
||||
final List<String> urls;
|
||||
final List<HttpHost> urls;
|
||||
final String username;
|
||||
final String password;
|
||||
final boolean requestCompression;
|
||||
@@ -59,14 +58,18 @@ class ElasticConfiguration {
|
||||
|
||||
Set<String> subsections = cfg.getSubsections("elasticsearch");
|
||||
if (subsections.isEmpty()) {
|
||||
this.urls = Arrays.asList(buildUrl(DEFAULT_PROTOCOL, DEFAULT_HOST, DEFAULT_PORT));
|
||||
HttpHost httpHost =
|
||||
new HttpHost(DEFAULT_HOST, Integer.valueOf(DEFAULT_PORT), DEFAULT_PROTOCOL);
|
||||
this.urls = Collections.singletonList(httpHost);
|
||||
} else {
|
||||
this.urls = new ArrayList<>(subsections.size());
|
||||
for (String subsection : subsections) {
|
||||
String port = getString(cfg, subsection, "port", DEFAULT_PORT);
|
||||
String host = getString(cfg, subsection, "hostname", DEFAULT_HOST);
|
||||
String protocol = getString(cfg, subsection, "protocol", DEFAULT_PROTOCOL);
|
||||
this.urls.add(buildUrl(protocol, host, port));
|
||||
|
||||
HttpHost httpHost = new HttpHost(host, Integer.valueOf(port), protocol);
|
||||
this.urls.add(httpHost);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,19 +77,4 @@ class ElasticConfiguration {
|
||||
private String getString(Config cfg, String subsection, String name, String defaultValue) {
|
||||
return MoreObjects.firstNonNull(cfg.getString("elasticsearch", subsection, name), defaultValue);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user