Repository navigation
Conversation
…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(...).
69acb1d to
5a41246
Compare
|
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.
anshumg
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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```
There was a problem hiding this 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 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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| || connectionMaxDurationMillis != null | ||
| || maxHttp2ConnectionsPerHost != null | ||
| || connectionIdleTimeoutMillis != null; |
There was a problem hiding this comment.
Considering the javadoc says 0 or less means unset, it's worth it to reflect that in the code else it'll create issues.
| || 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.
There was a problem hiding this comment.
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.
|
Thanks for the review @anshumg. Pushed 2ea27d5, which addresses the test race, the HTTP/2 idle timeout, the max-duration warning and the |
https://issues.apache.org/jira/browse/SOLR-18643
Description
HttpJettySolrClient(andHttp2SolrClienton 9.x) give callers no control over the lifecycle oftheir pooled connections:
withMaxConnectionsPerHostsays (that setting only applies to HTTP/1.1).HttpClient.setIdleTimeout(-1)is hard-coded, so the client never closes a connection for beingidle.
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
QTimestayed normal and the other client processes, calling the samenodes, were unaffected. Replacing the process cleared it every time.
Solution
Three opt-in
Builderoptions. Defaults are unchanged.withConnectionMaxDuration(long, TimeUnit)ConnectionPool.Factoryand callsAbstractConnectionPool.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.withMaxHttp2ConnectionsPerHost(int)withMaxConnectionsPerHost.DEFAULT_MAX_HTTP2_CONNECTIONS_PER_HOST)withConnectionIdleTimeout(long, TimeUnit)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-requestwithIdleTimeout. 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.maxConnectionsPerHostfor HTTP/2,avoids changing behaviour for clients that set
maxConnectionsPerHosthigh for HTTP/1.1 — theconcern discussed on SOLR-17240 (SOLR-17240: Fix Http2SolrClient maxConnectionsPerDestination usage #2410).
HttpClient, all three are rejected together withwithHttpClient(...)(IllegalArgumentException).withMaxConnectionsPerHostnow 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 adifferent connection once the duration passes; without it the same connection is reused.
testMaxHttp2ConnectionsPerHost,testMaxHttp2ConnectionsPerHostIgnoredForHttp1,testMaxHttp2ConnectionsPerHostValidation: the HTTP/2 limit reaches the pool (default 4), isignored for HTTP/1.1, and rejects values below 1.
testConnectionIdleTimeoutClosesIdleConnections,testNoConnectionIdleTimeoutByDefault: with theoption an idle connection is closed and the next request still succeeds; the default stays -1.
testConnectionMaxDurationWithHttpClient,testConnectionOptionsWithHttpClient: the options arerejected together with
withHttpClient(...).Each new behaviour was checked to fail with its code path disabled.
Backport
The same change applies to
Http2SolrClientonbranch_9x(Jetty 10 has the sameConnectionPool.Factory,AbstractConnectionPool.setMaxDurationandHttpClient.setIdleTimeoutAPIs); a 9.x branch is ready.
Checklist
mainbranch../gradlew tidyand./gradlew :solr:solrj-jetty:check :solr:solrj:check(full./gradlew checknot run); changelog entry passes./gradlew logchangeLint.AI disclosure: this change was drafted with the help of an AI coding assistant (Claude) and
reviewed by me.