sijuv commented on code in PR #5322:
URL: https://github.com/apache/solr/pull/5322#discussion_r4794841679
##########
solr/solrj-jetty/src/test/org/apache/solr/client/solrj/jetty/HttpJettySolrClientTest.java:
##########
@@ -936,5 +941,192 @@ private static void assertIsTimeout(Throwable t) {
assertThat(t.getMessage(), containsStringIgnoringCase("Timeout"));
}
+ private static final long CONNECTION_MAX_DURATION_MS = 50;
+
+ /** How long to keep sending requests before giving up on seeing a replaced
connection. */
+ private static final long CONNECTION_REPLACED_DEADLINE_MS = 100 *
CONNECTION_MAX_DURATION_MS;
+
+ @Test
+ public void testConnectionMaxDurationIsApplied() throws Exception {
+ for (boolean useHttp1_1 : new boolean[] {false, true}) {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(useHttp1_1)
+ .withConnectionMaxDuration(CONNECTION_MAX_DURATION_MS,
TimeUnit.MILLISECONDS)
+ .build()) {
+ queryDefaultCollection(client);
+ assertEquals(
+ "useHttp1_1=" + useHttp1_1,
+ CONNECTION_MAX_DURATION_MS,
+ connectionPool(client).getMaxDuration());
+ }
+ }
+ }
+
+ @Test
+ public void testConnectionReplacedAfterMaxDuration() throws Exception {
+ for (boolean useHttp1_1 : new boolean[] {false, true}) {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(useHttp1_1)
+ .withConnectionMaxDuration(CONNECTION_MAX_DURATION_MS,
TimeUnit.MILLISECONDS)
+ .build()) {
+ queryDefaultCollection(client);
+ Connection first = idleConnection(client);
+ new TimeOut(CONNECTION_REPLACED_DEADLINE_MS, TimeUnit.MILLISECONDS,
TimeSource.NANO_TIME)
+ .waitFor(
+ "connection was not replaced, useHttp1_1=" + useHttp1_1,
+ () -> {
+ queryDefaultCollection(client);
+ return idleConnection(client) != first;
+ });
+ }
+ }
+ }
+
+ @Test
+ public void testConnectionReusedWithoutMaxDuration() throws Exception {
+ for (boolean useHttp1_1 : new boolean[] {false, true}) {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(useHttp1_1)
+ .build()) {
+ queryDefaultCollection(client);
+ Connection first = idleConnection(client);
+ queryDefaultCollection(client);
+ assertSame("useHttp1_1=" + useHttp1_1, first, idleConnection(client));
+ assertEquals(0, connectionPool(client).getMaxDuration());
+ }
+ }
+ }
+
+ @Test
+ public void testConnectionMaxDurationWithHttpClient() {
+ try (var seed = new
HttpJettySolrClient.Builder(solrTestRule.getBaseUrl()).build()) {
+ expectThrows(
+ IllegalArgumentException.class,
+ () ->
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .withHttpClient(seed)
+ .withConnectionMaxDuration(CONNECTION_MAX_DURATION_MS,
TimeUnit.MILLISECONDS)
+ .build());
+ }
+ }
+
+ private static final int MAX_HTTP2_CONNECTIONS = 8;
+
+ @Test
+ public void testMaxHttp2ConnectionsPerHost() throws Exception {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(false)
+ .withMaxHttp2ConnectionsPerHost(MAX_HTTP2_CONNECTIONS)
+ .build()) {
+ queryDefaultCollection(client);
+ assertEquals(MAX_HTTP2_CONNECTIONS,
client.getHttpClient().getMaxConnectionsPerDestination());
+ assertEquals(MAX_HTTP2_CONNECTIONS,
connectionPool(client).getMaxConnectionCount());
+ }
+ try (var client =
+ new
HttpJettySolrClient.Builder(solrTestRule.getBaseUrl()).useHttp1_1(false).build())
{
+ queryDefaultCollection(client);
+ assertEquals(
+ HttpJettySolrClient.DEFAULT_MAX_HTTP2_CONNECTIONS_PER_HOST,
+ connectionPool(client).getMaxConnectionCount());
+ }
+ }
+
+ @Test
+ public void testMaxHttp2ConnectionsPerHostIgnoredForHttp1() {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(true)
+ .withMaxHttp2ConnectionsPerHost(MAX_HTTP2_CONNECTIONS)
+ .build()) {
+ assertEquals(
+ new HttpClient().getMaxConnectionsPerDestination(),
+ client.getHttpClient().getMaxConnectionsPerDestination());
+ }
+ }
+
+ @Test
+ public void testMaxHttp2ConnectionsPerHostValidation() {
+ var builder = new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl());
+ expectThrows(IllegalArgumentException.class, () ->
builder.withMaxHttp2ConnectionsPerHost(0));
+ }
+
+ private static final long CONNECTION_IDLE_TIMEOUT_MS = 200;
+
+ /** How long to wait for the client to close an idle connection. */
+ private static final long CONNECTION_CLOSED_DEADLINE_MS = 50 *
CONNECTION_IDLE_TIMEOUT_MS;
+
+ @Test
+ public void testConnectionIdleTimeoutClosesIdleConnections() throws
Exception {
+ for (boolean useHttp1_1 : new boolean[] {false, true}) {
+ try (var client =
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .useHttp1_1(useHttp1_1)
+ .withConnectionIdleTimeout(CONNECTION_IDLE_TIMEOUT_MS,
TimeUnit.MILLISECONDS)
+ .build()) {
+ assertEquals(CONNECTION_IDLE_TIMEOUT_MS,
client.getHttpClient().getIdleTimeout());
+ queryDefaultCollection(client);
+ new TimeOut(CONNECTION_CLOSED_DEADLINE_MS, TimeUnit.MILLISECONDS,
TimeSource.NANO_TIME)
+ .waitFor(
+ "idle connection was not closed, useHttp1_1=" + useHttp1_1,
+ () -> connectionPool(client).getConnectionCount() == 0);
+ queryDefaultCollection(client);
+ }
+ }
+ }
+
+ @Test
+ public void testNoConnectionIdleTimeoutByDefault() {
+ try (var client = new
HttpJettySolrClient.Builder(solrTestRule.getBaseUrl()).build()) {
+ assertEquals(-1, client.getHttpClient().getIdleTimeout());
+ }
+ }
+
+ @Test
+ public void testConnectionOptionsWithHttpClient() {
+ try (var seed = new
HttpJettySolrClient.Builder(solrTestRule.getBaseUrl()).build()) {
+ expectThrows(
+ IllegalArgumentException.class,
+ () ->
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .withHttpClient(seed)
+ .withMaxHttp2ConnectionsPerHost(MAX_HTTP2_CONNECTIONS)
+ .build());
+ expectThrows(
+ IllegalArgumentException.class,
+ () ->
+ new HttpJettySolrClient.Builder(solrTestRule.getBaseUrl())
+ .withHttpClient(seed)
+ .withConnectionIdleTimeout(CONNECTION_IDLE_TIMEOUT_MS,
TimeUnit.MILLISECONDS)
+ .build());
+ }
+ }
+
+ private static void queryDefaultCollection(HttpJettySolrClient client) {
+ try {
+ client.query(DEFAULT_COLLECTION, new SolrQuery("*:*"));
+ } catch (SolrServerException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static AbstractConnectionPool connectionPool(HttpJettySolrClient
client) {
+ List<Destination> destinations = client.getHttpClient().getDestinations();
+ assertEquals(1, destinations.size());
+ return (AbstractConnectionPool) destinations.get(0).getConnectionPool();
+ }
+
+ /** The pooled connection the next request would use, without opening a new
one. */
+ private static Connection idleConnection(HttpJettySolrClient client) {
+ AbstractConnectionPool pool = connectionPool(client);
+ Connection connection = pool.acquire(false);
+ assertNotNull("no idle connection in " + pool, connection);
Review Comment:
Thanks, good catch. I reproduced it: with a 60 ms sleep between the query
and the pool check, 10 of 10 runs failed with "no idle connection". Fixed in
2ea27d5a99 along the lines you suggested: the helper is now
`idleConnectionOrNull`, capturing `first` retries, and the condition is
`current != null && current != first`. Max duration is now 300 ms with a 20x
deadline.
Checked with `-Ptests.iters=50` (50/50 pass), with a random 0-400 ms delay
injected after each query (20/20 pass), and with max duration not applied
(fails with "connection was not replaced").
While in there I found a smaller race in
`testConnectionReusedWithoutMaxDuration`: with HTTP/1.1, `client.query()` can
return before Jetty releases the connection to the pool, so `acquire(false)`
can briefly return null. That test now waits for the release
(`awaitIdleConnection`) without sending another query.
##########
solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/HttpJettySolrClient.java:
##########
@@ -1131,6 +1162,62 @@ public HttpJettySolrClient.Builder
withKeyStoreReloadInterval(long interval, Tim
return this;
}
+ /**
+ * Set the maximum time a pooled connection may be used for. Once a
connection is older than
+ * this, it is closed instead of being reused, and a new connection is
opened when needed. This
+ * applies to both HTTP/1.1 and HTTP/2 connections. If unset, 0 or less,
connections are reused
+ * for as long as they stay open.
+ *
+ * @param duration Maximum connection lifetime
+ * @param unit The unit for the duration
+ * @return This builder
+ */
+ public HttpJettySolrClient.Builder withConnectionMaxDuration(long
duration, TimeUnit unit) {
+ this.connectionMaxDurationMillis = unit.toMillis(duration);
+ if (this.connectionMaxDurationMillis == 0 && duration > 0) {
+ this.connectionMaxDurationMillis = 1L;
+ }
+ return this;
+ }
+
+ /**
+ * Set the maximum number of HTTP/2 connections opened per destination.
Requests are multiplexed
+ * over these connections. Has no effect when using HTTP/1.1; see {@link
+ * #withMaxConnectionsPerHost(int)} for that. Defaults to {@value
+ * HttpJettySolrClient#DEFAULT_MAX_HTTP2_CONNECTIONS_PER_HOST}.
+ *
+ * @param max Maximum HTTP/2 connections per destination, at least 1
+ * @return This builder
+ */
+ public HttpJettySolrClient.Builder withMaxHttp2ConnectionsPerHost(int max)
{
+ if (max < 1) {
+ throw new IllegalArgumentException(
+ "maxHttp2ConnectionsPerHost must be at least 1, got " + max);
+ }
+ this.maxHttp2ConnectionsPerHost = max;
+ return this;
+ }
+
+ /**
+ * Set how long a connection may go without any traffic before it is
closed. This applies to
+ * both HTTP/1.1 and HTTP/2 connections. It is separate from {@link
#withIdleTimeout(long,
+ * TimeUnit)}, which limits how long a single request may wait for data.
Set it above the
+ * longest expected wait for a response: an HTTP/2 connection that sees no
traffic for this long
+ * is closed even while a request on it is waiting. If unset, 0 or less,
idle connections are
+ * not closed by the client.
+ *
+ * @param timeout Idle timeout for connections
+ * @param unit The unit for the timeout
+ * @return This builder
+ */
+ public HttpJettySolrClient.Builder withConnectionIdleTimeout(long timeout,
TimeUnit unit) {
Review Comment:
Agreed, the impact is wider than one request. In 2ea27d5a99 the builder now
logs a warning when HTTP/2 is used and the connection idle timeout is at or
below the request idle timeout (`withIdleTimeout`, 600 s by default). I went
with a warning rather than rejecting it, since a short value can be
intentional. The javadoc and ref guide now say that every request waiting on
the connection fails, and mention the warning. Added
`testHttp2ConnectionIdleTimeoutWarning`.
##########
solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/HttpJettySolrClient.java:
##########
@@ -290,7 +296,23 @@ private HttpClient createHttpClient(Builder builder) {
HTTP2Client http2client = new HTTP2Client(clientConnector);
transport = new HttpClientTransportOverHTTP2(http2client);
httpClient = new HttpClient(transport);
- httpClient.setMaxConnectionsPerDestination(4);
+ httpClient.setMaxConnectionsPerDestination(
+ builder.maxHttp2ConnectionsPerHost != null
+ ? builder.maxHttp2ConnectionsPerHost
+ : DEFAULT_MAX_HTTP2_CONNECTIONS_PER_HOST);
+ }
+
+ if (builder.connectionMaxDurationMillis != null &&
builder.connectionMaxDurationMillis > 0) {
+ long maxDurationMillis = builder.connectionMaxDurationMillis;
+ ConnectionPool.Factory poolFactory =
transport.getConnectionPoolFactory();
+ transport.setConnectionPoolFactory(
+ destination -> {
+ ConnectionPool pool = poolFactory.newConnectionPool(destination);
+ if (pool instanceof AbstractConnectionPool abstractPool) {
Review Comment:
Done in 2ea27d5a99, using your suggested wording.
##########
solr/solrj-jetty/src/java/org/apache/solr/client/solrj/jetty/HttpJettySolrClient.java:
##########
@@ -1203,7 +1290,10 @@ private boolean hasHttpClientBuildingOptions() {
|| proxyHost != null
|| sslConfig != null
|| cookieStore != null
- || keyStoreReloadIntervalSecs != null;
+ || keyStoreReloadIntervalSecs != null
+ || connectionMaxDurationMillis != null
+ || maxHttp2ConnectionsPerHost != null
+ || connectionIdleTimeoutMillis != null;
Review Comment:
Done in 2ea27d5a99. Added `testUnsetConnectionOptionsWithHttpClient`, which
builds with `withHttpClient(seed)` and both options at 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]