Skip to content

SOLR-18643: Configurable HTTP/2 connection count, idle timeout and max lifetime in the Jetty SolrJ client - #5322

Open
sijuv wants to merge 3 commits into
apache:mainfrom
sijuv:conn-max-duration
Open

sijuv wants to merge 3 commits into
apache:mainfrom
sijuv:conn-max-duration

Conversation

@sijuv

@sijuv sijuv commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/SOLR-18643

Description

HttpJettySolrClient (and Http2SolrClient on 9.x) give callers no control over the lifecycle of
their pooled connections:

  • With HTTP/2 the number of connections per destination is hard-coded to 4, whatever
    withMaxConnectionsPerHost says (that setting only applies to HTTP/1.1).
  • HttpClient.setIdleTimeout(-1) is hard-coded, so the client never closes a connection for being
    idle.
  • A connection is reused for as long as it stays open; nothing ever retires a busy one.

If one of the few long-lived HTTP/2 connections degrades (for example it stops returning
responses for some streams), a large share of the client's requests to that node keep stalling
until their request timeout, and nothing short of restarting the client replaces the connection.

We saw this in production with a SolrJ HTTP/2 client calling SolrCloud: one client process at a
time, after hours of uptime, started timing out ~5–13% of its requests in roughly one-minute
bursts, while Solr's own QTime stayed normal and the other client processes, calling the same
nodes, were unaffected. Replacing the process cleared it every time.

Solution

Three opt-in Builder options. Defaults are unchanged.

Option Effect Default
withConnectionMaxDuration(long, TimeUnit) Wraps the transport's ConnectionPool.Factory and calls AbstractConnectionPool.setMaxDuration(...) on each destination's pool: a connection older than the duration is closed instead of reused, and a new one is opened when needed. HTTP/1.1 and HTTP/2. no limit
withMaxHttp2ConnectionsPerHost(int) Number of HTTP/2 connections per destination (must be ≥ 1). No effect on HTTP/1.1, which keeps using withMaxConnectionsPerHost. 4 (DEFAULT_MAX_HTTP2_CONNECTIONS_PER_HOST)
withConnectionIdleTimeout(long, TimeUnit) Sets HttpClient.setIdleTimeout(...), which Jetty also applies to HTTP/2 sessions: a connection with no traffic for that long is closed. Separate from the existing per-request withIdleTimeout. The javadoc warns that an HTTP/2 connection with no traffic is closed even while a request on it is still waiting, so the value must exceed the longest expected response time. no idle timeout (-1)
  • A separate HTTP/2 connection option, rather than honouring maxConnectionsPerHost for HTTP/2,
    avoids changing behaviour for clients that set maxConnectionsPerHost high for HTTP/1.1 — the
    concern discussed on SOLR-17240 (SOLR-17240: Fix Http2SolrClient maxConnectionsPerDestination usage #2410).
  • Like other options that build the Jetty HttpClient, all three are rejected together with
    withHttpClient(...) (IllegalArgumentException).
  • The javadoc of withMaxConnectionsPerHost now points to the new HTTP/2 option.

Tests

HttpJettySolrClientTest, for HTTP/2 and HTTP/1.1 where both apply:

  • testConnectionMaxDurationIsApplied, testConnectionReplacedAfterMaxDuration,
    testConnectionReusedWithoutMaxDuration: max duration reaches the pool; a later request uses a
    different connection once the duration passes; without it the same connection is reused.
  • testMaxHttp2ConnectionsPerHost, testMaxHttp2ConnectionsPerHostIgnoredForHttp1,
    testMaxHttp2ConnectionsPerHostValidation: the HTTP/2 limit reaches the pool (default 4), is
    ignored for HTTP/1.1, and rejects values below 1.
  • testConnectionIdleTimeoutClosesIdleConnections, testNoConnectionIdleTimeoutByDefault: with the
    option an idle connection is closed and the next request still succeeds; the default stays -1.
  • testConnectionMaxDurationWithHttpClient, testConnectionOptionsWithHttpClient: the options are
    rejected together with withHttpClient(...).

Each new behaviour was checked to fail with its code path disabled.

Backport

The same change applies to Http2SolrClient on branch_9x (Jetty 10 has the same
ConnectionPool.Factory, AbstractConnectionPool.setMaxDuration and HttpClient.setIdleTimeout
APIs); a 9.x branch is ready.

Checklist

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch.
  • I have developed this patch against the main branch.
  • I have run ./gradlew tidy and ./gradlew :solr:solrj-jetty:check :solr:solrj:check (full ./gradlew check not run); changelog entry passes ./gradlew logchangeLint.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide

AI disclosure: this change was drafted with the help of an AI coding assistant (Claude) and
reviewed by me.

@sijuv sijuv changed the title Add withConnectionMaxDuration to the Jetty-based SolrJ client builder SOLR-18643 - Add withConnectionMaxDuration to the Jetty-based SolrJ client builder Nov 12, 2026
@sijuv sijuv changed the title SOLR-18643 - Add withConnectionMaxDuration to the Jetty-based SolrJ client builder SOLR-18643: Configurable HTTP/2 connection count, idle timeout and max lifetime in the Jetty SolrJ client" Nov 12, 2026
…x lifetime in the Jetty SolrJ client

Pooled connections were reused for as long as they stayed open: HTTP/2 was
limited to 4 connections per destination and the client-level connection idle
timeout was hard-coded to -1, so a degraded connection was never replaced
short of restarting the client.

Add three opt-in builder options, defaults unchanged:
- withConnectionMaxDuration: sets AbstractConnectionPool.setMaxDuration on each
  destination's pool, so connections older than the duration are replaced
  (HTTP/1.1 and HTTP/2).
- withMaxHttp2ConnectionsPerHost: number of HTTP/2 connections per destination
  (default 4). A separate option so clients tuning maxConnectionsPerHost for
  HTTP/1.1 keep their HTTP/2 behaviour.
- withConnectionIdleTimeout: HttpClient idle timeout, which Jetty also applies
  to HTTP/2 sessions.

All three are rejected together with withHttpClient(...).
@sijuv
sijuv force-pushed the conn-max-duration branch from 69acb1d to 5a41246 Compare November 12, 2026 02:06
@sijuv
sijuv marked this pull request as ready for review November 14, 2026 22:46
@github-actions

Copy link
Copy Markdown

Automated PR readiness judgements (LLM-generated; may be wrong; not updated on later pushes).

…the ref guide

Add a "Connection Reuse in HttpJettySolrClient" section to the SolrJ page
covering withConnectionMaxDuration, withMaxHttp2ConnectionsPerHost and
withConnectionIdleTimeout.
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 22, 2026
@anshumg anshumg changed the title SOLR-18643: Configurable HTTP/2 connection count, idle timeout and max lifetime in the Jetty SolrJ client" SOLR-18643: Configurable HTTP/2 connection count, idle timeout and max lifetime in the Jetty SolrJ client Dec 2, 2026

@anshumg anshumg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @sijuv , the motivation is clear and the code changes look good.

I've left a few comments, mainly around a flaky test due to timing issues. The idle-timeout one is worth a look too. The rest are nits.

No need to address in this directly in this PR but connections to a host are usually opened together, so with a max duration they'll also expire together, causing a burst of reconnects every period. Probably fine, but maybe worth a line in the docs.

private static Connection idleConnection(HttpJettySolrClient client) {
AbstractConnectionPool pool = connectionPool(client);
Connection connection = pool.acquire(false);
assertNotNull("no idle connection in " + pool, connection);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think testConnectionReplacedAfterMaxDuration can fail intermittently on busy (slow :/ ?) CI machines, even when max duration works correctly.

idleConnection() checks the pool with pool.acquire(false), but that call isn't read-only.
In Jetty 12.1, AbstractConnectionPool.activate() checks the connection's age on every acquire. If the connection has passed maxDuration, Jetty removes and closes it on the spot. With no other connection in the pool, and create=false, acquire returns null, and the assertNotNull fails.

Each poll acquires twice: once in queryDefaultCollection(client) and once in idleConnection(client). So if the connection's 50 ms expiry falls between the query's acquire and the helper's acquire, the helper retires it and the test fails with "no idle connection in ....".

The same can happen at the initial first capture if query #1 takes longer than 50 ms. TimeOut.waitFor doesn't catch exceptions from the condition, so this fails the test immediately instead of retrying.

I thought a longer maxDuration might help here but sadly it doesn't fully remove this. The connection still expires at some point during the polling, and the race happens whenever that point falls inside the query window.
A reliable way to see it: add a Thread.sleep(60) between queryDefaultCollection(client) and idleConnection(client). It's a simulation confirming that this is indeed an issue, but the probability of this getting triggered might be lower than my math suggests.

Since a null from acquire(false) is a normal result when max duration is set, one option is to let the check return null and poll again:

private static Connection idleConnectionOrNull(HttpJettySolrClient client) {
  AbstractConnectionPool pool = connectionPool(client);
  Connection connection = pool.acquire(false);
  if (connection != null) {
    pool.release(connection);
  }
  return connection;
}

Then the condition becomes
current != null && current != first
and capturing first retries until it gets a connection. The test still fails if max duration is broken, because first is reused until the deadline. Separately, a max duration of a few hundred ms, with the deadline scaled to match, would make it more tolerant of slow machines. Today the deadline is 100 * the max duration.

It's worth running this test repeatedly (e.g. -Ptests.iters=50) to see how often it fails as written. I'll try this out too, but something for you to also check.

You could use this

./gradlew -p solr/solrj-jetty test --tests "HttpJettySolrClientTest.testConnectionReplacedAfterMaxDuration" -Ptests.iters=50```

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 2ea27d5 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.

- name: Siju Varghese
nick: sijuv
links:
- name: SOLR-18643

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's something off here and I've left a note on the JIRA as there's a seemingly unrelated commit associated with the same JIRA in the changelog + commit history. I'm pretty sure this PR belongs to the JIRA, but just something to fix.
cc @gerlowskija

* @param unit The unit for the timeout
* @return This builder
*/
public HttpJettySolrClient.Builder withConnectionIdleTimeout(long timeout, TimeUnit unit) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc warning is accurate, but the impact is wider than one request. With HTTP/2, once the connection has had no traffic for this long, Jetty closes the whole connection. Waiting for a response counts as no traffic, so every request still waiting on that connection will fail instead of just the slow one.

The per-request idle timeout defaults to 600s. So setting, say, withConnectionIdleTimeout(60, TimeUnit.SECONDS) alone would quietly cap HTTP/2 requests at about 60s.

Could we warn (or reject) at build time when, with HTTP/2, this is at or below the per-request idle timeout? At the least, the docs could mention that other in-flight requests fail too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the impact is wider than one request. In 2ea27d5 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.

Comment on lines +1294 to +1296
|| connectionMaxDurationMillis != null
|| maxHttp2ConnectionsPerHost != null
|| connectionIdleTimeoutMillis != null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the javadoc says 0 or less means unset, it's worth it to reflect that in the code else it'll create issues.

Suggested change
|| connectionMaxDurationMillis != null
|| maxHttp2ConnectionsPerHost != null
|| connectionIdleTimeoutMillis != null;
|| (connectionMaxDurationMillis != null && connectionMaxDurationMillis > 0)
|| maxHttp2ConnectionsPerHost != null
|| (connectionIdleTimeoutMillis != null && connectionIdleTimeoutMillis > 0);

The formatting might be a bit off, so please check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2ea27d5. Added testUnsetConnectionOptionsWithHttpClient, which builds with withHttpClient(seed) and both options at 0.

… options

- Fix races in the connection reuse tests. Acquiring from the pool closes a
  connection past its max duration, and with HTTP/1.1 a request can return
  before its connection is released, so the tests now poll instead of
  asserting on the first acquire. Max duration in the test is now 300 ms.
- Warn at build time when, with HTTP/2, the connection idle timeout is at or
  below the request idle timeout, since it then fails every request waiting
  on the connection. Document this in the javadoc and ref guide.
- Warn when the connection max duration cannot be applied to a pool.
- Treat a max duration or idle timeout of 0 or less as unset when checking
  for conflicts with withHttpClient.
- Document that connections opened together also expire together.
@sijuv

sijuv commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @anshumg. Pushed 2ea27d5, which addresses the test race, the HTTP/2 idle timeout, the max-duration warning and the > 0 checks. I also added a sentence to the withConnectionMaxDuration javadoc and the ref guide noting that connections opened together also expire together, causing a burst of reconnects about once per period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

client:solrj documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants