|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.io.UncheckedIOException; |
21 | 21 | import java.lang.invoke.MethodHandles; |
| 22 | +import java.net.ServerSocket; |
22 | 23 | import java.nio.file.Files; |
23 | 24 | import java.nio.file.Path; |
24 | 25 | import java.util.ArrayList; |
|
32 | 33 | import org.apache.solr.client.solrj.SolrClient; |
33 | 34 | import org.apache.solr.client.solrj.SolrQuery; |
34 | 35 | import org.apache.solr.client.solrj.SolrServerException; |
| 36 | +import org.apache.solr.client.solrj.request.QueryRequest; |
35 | 37 | import org.apache.solr.client.solrj.response.QueryResponse; |
36 | 38 | import org.apache.solr.client.solrj.response.SolrResponseBase; |
37 | 39 | import org.apache.solr.common.SolrInputDocument; |
@@ -206,6 +208,29 @@ public void testTwoServers() throws Exception { |
206 | 208 | } |
207 | 209 | } |
208 | 210 |
|
| 211 | + public void testTimeoutExceptionMarksServerAsZombie() throws Exception { |
| 212 | + try (ZombieTestContext ctx = new ZombieTestContext()) { |
| 213 | + LBSolrClient.Req lbReq = ctx.createQueryRequest(); |
| 214 | + |
| 215 | + try { |
| 216 | + ctx.lbClient.request(lbReq); |
| 217 | + } catch (Exception e) { |
| 218 | + } |
| 219 | + |
| 220 | + ctx.assertZombieState(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + public void testTimeoutExceptionMarksServerAsZombieAsyncRequest() throws Exception { |
| 225 | + try (ZombieTestContext ctx = new ZombieTestContext()) { |
| 226 | + LBSolrClient.Req lbReq = ctx.createQueryRequest(); |
| 227 | + |
| 228 | + ctx.lbClient.requestAsync(lbReq).exceptionally(e -> null).get(); |
| 229 | + |
| 230 | + ctx.assertZombieState(); |
| 231 | + } |
| 232 | + } |
| 233 | + |
209 | 234 | private LBSolrClient.Endpoint[] bootstrapBaseSolrEndpoints(int max) { |
210 | 235 | LBSolrClient.Endpoint[] solrUrls = new LBSolrClient.Endpoint[max]; |
211 | 236 | for (int i = 0; i < max; i++) { |
@@ -334,4 +359,60 @@ public void close() { |
334 | 359 | } |
335 | 360 | } |
336 | 361 | } |
| 362 | + |
| 363 | + private class ZombieTestContext implements AutoCloseable { |
| 364 | + final ServerSocket blackhole; |
| 365 | + final LBSolrClient.Endpoint nonRoutableEndpoint; |
| 366 | + final Http2SolrClient delegateClient; |
| 367 | + final LBHttp2SolrClient<?> lbClient; |
| 368 | + |
| 369 | + ZombieTestContext() throws Exception { |
| 370 | + //create a socket that allows a client to connect but causes them to hang until idleTimeout is triggered |
| 371 | + blackhole = new ServerSocket(0); |
| 372 | + int blackholePort = blackhole.getLocalPort(); |
| 373 | + nonRoutableEndpoint = |
| 374 | + new LBSolrClient.Endpoint("http://localhost:" + blackholePort + "/solr"); |
| 375 | + |
| 376 | + delegateClient = |
| 377 | + new Http2SolrClient.Builder() |
| 378 | + .withConnectionTimeout(1000, TimeUnit.MILLISECONDS) |
| 379 | + .withIdleTimeout(100, TimeUnit.MILLISECONDS) |
| 380 | + .build(); |
| 381 | + |
| 382 | + lbClient = |
| 383 | + new LBHttp2SolrClient.Builder<>(delegateClient) |
| 384 | + .setAliveCheckInterval(500, TimeUnit.MILLISECONDS) |
| 385 | + .build(); |
| 386 | + } |
| 387 | + |
| 388 | + LBSolrClient.Req createQueryRequest() { |
| 389 | + SolrQuery solrQuery = new SolrQuery("*:*"); |
| 390 | + QueryRequest queryRequest = new QueryRequest(solrQuery); |
| 391 | + |
| 392 | + List<LBSolrClient.Endpoint> endpoints = |
| 393 | + List.of( |
| 394 | + new LBSolrClient.Endpoint( |
| 395 | + nonRoutableEndpoint.getBaseUrl(), solr[0].getDefaultCollection()) |
| 396 | + ); |
| 397 | + return new LBSolrClient.Req(queryRequest, endpoints); |
| 398 | + } |
| 399 | + |
| 400 | + void assertZombieState() { |
| 401 | + assertTrue( |
| 402 | + "Non-routable endpoint should be marked as zombie due to timeout", |
| 403 | + lbClient.zombieServers.containsKey( |
| 404 | + nonRoutableEndpoint.getBaseUrl() + "/" + solr[0].getDefaultCollection())); |
| 405 | + } |
| 406 | + |
| 407 | + @Override |
| 408 | + public void close() { |
| 409 | + lbClient.close(); |
| 410 | + delegateClient.close(); |
| 411 | + try { |
| 412 | + blackhole.close(); |
| 413 | + } catch (IOException ioe) { |
| 414 | + |
| 415 | + } |
| 416 | + } |
| 417 | + } |
337 | 418 | } |
0 commit comments