Merge branch 'stable-2.15'

* stable-2.15:
  Elasticsearch: Allow to configure REST client's retry timeout
  ElasticConfiguration: Make hosts private
  config-gerrit: Fix formatting of elasticsearch.name.hostname

Change-Id: I76d072aab5506a3896a45adc92211020afc98c91
This commit is contained in:
David Pursehouse
2018-07-03 12:58:56 +09:00
3 changed files with 19 additions and 56 deletions

View File

@@ -3009,41 +3009,13 @@ Password used to connect to Elasticsearch.
+ +
Not set by default. Not set by default.
[[elasticsearch.requestCompression]]elasticsearch.requestCompression:: [[elasticsearch.maxRetryTimeout]]elasticsearch.maxRetryTimeout::
+ +
Enable request compression. Sets the maximum timeout to honor in case of multiple retries of the same request.
+
Defaults to `false`.
[[elasticsearch.connectionTimeout]]elasticsearch.connectionTimeout::
+
How long should Gerrit waits for connection.
+ +
The value is in the usual time-unit format like `1 m`, `5 m`. The value is in the usual time-unit format like `1 m`, `5 m`.
+ +
Defaults to `5 m` Defaults to `30000 ms`.
[[elasticsearch.maxConnectionIdleTime]]elasticsearch.maxConnectionIdleTime::
+
How long connection can stay in idle.
+
The value is in the usual time-unit format like `1 m`, `5 m`.
+
Defaults to `5 m`
[[elasticsearch.maxTotalConnection]]elasticsearch.maxTotalConnection::
+
How many connections can be spawned simultaneously.
+
Defaults to `1`
[[elasticsearch.maxReadTimeout]]elasticsearch.maxReadTimeout::
+
Timeout for read operations.
+
The value is in the usual time-unit format like `1 m`, `5 m`.
+
Defaults to `5 m`
==== Elasticsearch server(s) configuration ==== Elasticsearch server(s) configuration
@@ -3058,7 +3030,7 @@ Defaults to `http`.
[[elasticsearch.name.hostname]]elasticsearch.name.hostname:: [[elasticsearch.name.hostname]]elasticsearch.name.hostname::
+ +
Elasticsearch server hostname. Elasticsearch server hostname.
+
Defaults to `localhost`. Defaults to `localhost`.
[[elasticsearch.name.port]]elasticsearch.name.port:: [[elasticsearch.name.port]]elasticsearch.name.port::

View File

@@ -37,15 +37,11 @@ class ElasticConfiguration {
private static final String DEFAULT_PROTOCOL = "http"; private static final String DEFAULT_PROTOCOL = "http";
private final Config cfg; private final Config cfg;
private final List<HttpHost> hosts;
final List<HttpHost> hosts;
final String username; final String username;
final String password; final String password;
final boolean requestCompression; final int maxRetryTimeout;
final long connectionTimeout;
final long maxConnectionIdleTime;
final int maxTotalConnection;
final int readTimeout;
final String prefix; final String prefix;
@Inject @Inject
@@ -53,15 +49,9 @@ class ElasticConfiguration {
this.cfg = cfg; this.cfg = cfg;
this.username = cfg.getString("elasticsearch", null, "username"); this.username = cfg.getString("elasticsearch", null, "username");
this.password = cfg.getString("elasticsearch", null, "password"); this.password = cfg.getString("elasticsearch", null, "password");
this.requestCompression = cfg.getBoolean("elasticsearch", null, "requestCompression", false); this.maxRetryTimeout =
this.connectionTimeout = (int)
cfg.getTimeUnit("elasticsearch", null, "connectionTimeout", 3000, TimeUnit.MILLISECONDS); cfg.getTimeUnit("elasticsearch", null, "maxRetryTimeout", 30000, TimeUnit.MILLISECONDS);
this.maxConnectionIdleTime =
cfg.getTimeUnit(
"elasticsearch", null, "maxConnectionIdleTime", 3000, TimeUnit.MILLISECONDS);
this.maxTotalConnection = cfg.getInt("elasticsearch", null, "maxTotalConnection", 1);
this.readTimeout =
(int) cfg.getTimeUnit("elasticsearch", null, "readTimeout", 3000, TimeUnit.MICROSECONDS);
this.prefix = Strings.nullToEmpty(cfg.getString("elasticsearch", null, "prefix")); this.prefix = Strings.nullToEmpty(cfg.getString("elasticsearch", null, "prefix"));
Set<String> subsections = cfg.getSubsections("elasticsearch"); Set<String> subsections = cfg.getSubsections("elasticsearch");
@@ -88,6 +78,10 @@ class ElasticConfiguration {
return cfg; return cfg;
} }
HttpHost[] getHosts() {
return hosts.toArray(new HttpHost[hosts.size()]);
}
String getIndexName(String name, int schemaVersion) { String getIndexName(String name, int schemaVersion) {
return String.format("%s%s_%04d", prefix, name, schemaVersion); return String.format("%s%s_%04d", prefix, name, schemaVersion);
} }

View File

@@ -22,8 +22,6 @@ import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
@@ -39,18 +37,14 @@ import org.elasticsearch.client.RestClientBuilder;
class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListener { class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListener {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final List<HttpHost> hosts; private final ElasticConfiguration cfg;
private final String username;
private final String password;
private RestClient client; private RestClient client;
private ElasticQueryAdapter adapter; private ElasticQueryAdapter adapter;
@Inject @Inject
ElasticRestClientProvider(ElasticConfiguration cfg) { ElasticRestClientProvider(ElasticConfiguration cfg) {
hosts = cfg.hosts; this.cfg = cfg;
username = cfg.username;
password = cfg.password;
} }
public static LifecycleModule module() { public static LifecycleModule module() {
@@ -132,12 +126,15 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
} }
private RestClient build() { private RestClient build() {
RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()])); RestClientBuilder builder = RestClient.builder(cfg.getHosts());
builder.setMaxRetryTimeoutMillis(cfg.maxRetryTimeout);
setConfiguredCredentialsIfAny(builder); setConfiguredCredentialsIfAny(builder);
return builder.build(); return builder.build();
} }
private void setConfiguredCredentialsIfAny(RestClientBuilder builder) { private void setConfiguredCredentialsIfAny(RestClientBuilder builder) {
String username = cfg.username;
String password = cfg.password;
if (username != null && password != null) { if (username != null && password != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials( credentialsProvider.setCredentials(