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.
[[elasticsearch.requestCompression]]elasticsearch.requestCompression::
[[elasticsearch.maxRetryTimeout]]elasticsearch.maxRetryTimeout::
+
Enable request compression.
+
Defaults to `false`.
[[elasticsearch.connectionTimeout]]elasticsearch.connectionTimeout::
+
How long should Gerrit waits for connection.
Sets the maximum timeout to honor in case of multiple retries of the same request.
+
The value is in the usual time-unit format like `1 m`, `5 m`.
+
Defaults to `5 m`
[[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`
Defaults to `30000 ms`.
==== Elasticsearch server(s) configuration
@@ -3058,7 +3030,7 @@ Defaults to `http`.
[[elasticsearch.name.hostname]]elasticsearch.name.hostname::
+
Elasticsearch server hostname.
+
Defaults to `localhost`.
[[elasticsearch.name.port]]elasticsearch.name.port::

View File

@@ -37,15 +37,11 @@ class ElasticConfiguration {
private static final String DEFAULT_PROTOCOL = "http";
private final Config cfg;
private final List<HttpHost> hosts;
final List<HttpHost> hosts;
final String username;
final String password;
final boolean requestCompression;
final long connectionTimeout;
final long maxConnectionIdleTime;
final int maxTotalConnection;
final int readTimeout;
final int maxRetryTimeout;
final String prefix;
@Inject
@@ -53,15 +49,9 @@ class ElasticConfiguration {
this.cfg = cfg;
this.username = cfg.getString("elasticsearch", null, "username");
this.password = cfg.getString("elasticsearch", null, "password");
this.requestCompression = cfg.getBoolean("elasticsearch", null, "requestCompression", false);
this.connectionTimeout =
cfg.getTimeUnit("elasticsearch", null, "connectionTimeout", 3000, 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.maxRetryTimeout =
(int)
cfg.getTimeUnit("elasticsearch", null, "maxRetryTimeout", 30000, TimeUnit.MILLISECONDS);
this.prefix = Strings.nullToEmpty(cfg.getString("elasticsearch", null, "prefix"));
Set<String> subsections = cfg.getSubsections("elasticsearch");
@@ -88,6 +78,10 @@ class ElasticConfiguration {
return cfg;
}
HttpHost[] getHosts() {
return hosts.toArray(new HttpHost[hosts.size()]);
}
String getIndexName(String name, int 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.Singleton;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
@@ -39,18 +37,14 @@ import org.elasticsearch.client.RestClientBuilder;
class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListener {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final List<HttpHost> hosts;
private final String username;
private final String password;
private final ElasticConfiguration cfg;
private RestClient client;
private ElasticQueryAdapter adapter;
@Inject
ElasticRestClientProvider(ElasticConfiguration cfg) {
hosts = cfg.hosts;
username = cfg.username;
password = cfg.password;
this.cfg = cfg;
}
public static LifecycleModule module() {
@@ -132,12 +126,15 @@ class ElasticRestClientProvider implements Provider<RestClient>, LifecycleListen
}
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);
return builder.build();
}
private void setConfiguredCredentialsIfAny(RestClientBuilder builder) {
String username = cfg.username;
String password = cfg.password;
if (username != null && password != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(