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