-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOLR-17419: Introduce ParallelHttpShardHandler (#2681)
The default ShardHandler implementation, HttpShardHandler, sends all shard-requests serially, only parallelizing the waiting and parsing of responses. This works great for collections with few shards, but as the number of shards increases the serialized sending of shard-requests adds a larger and larger overhead to the overall request (especially when auth and PKI are done at request-sending time). This commit fixes this by introducing an alternate ShardHandler implementation, geared towards collections with many shards. This ShardHandler uses an executor to parallelize both request sending and response waiting/parsing. This consumes more CPU, but reduces greatly reduces the latency/QTime observed by users querying many-shard collections.
- Loading branch information
1 parent
2d2b061
commit eeeb0e3
Showing
14 changed files
with
329 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
solr/core/src/java/org/apache/solr/handler/component/ParallelHttpShardHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.handler.component; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import net.jcip.annotations.NotThreadSafe; | ||
import org.apache.solr.client.solrj.impl.LBSolrClient; | ||
import org.apache.solr.common.SolrException; | ||
import org.apache.solr.common.params.ModifiableSolrParams; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A version of {@link HttpShardHandler} optimized for massively-sharded collections. | ||
* | ||
* <p>Uses a {@link HttpShardHandlerFactory#commExecutor} thread for all work related to outgoing | ||
* requests, allowing {@link #submit(ShardRequest, String, ModifiableSolrParams)} to return more | ||
* quickly. (See {@link HttpShardHandler} for comparison.) | ||
* | ||
* <p>The additional focus on parallelization makes this an ideal implementation for collections | ||
* with many shards. | ||
*/ | ||
@NotThreadSafe | ||
public class ParallelHttpShardHandler extends HttpShardHandler { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
private final ExecutorService commExecutor; | ||
|
||
public ParallelHttpShardHandler(ParallelHttpShardHandlerFactory httpShardHandlerFactory) { | ||
super(httpShardHandlerFactory); | ||
this.commExecutor = httpShardHandlerFactory.commExecutor; | ||
} | ||
|
||
@Override | ||
public void submit(ShardRequest sreq, String shard, ModifiableSolrParams params) { | ||
// do this outside of the callable for thread safety reasons | ||
final List<String> urls = getURLs(shard); | ||
final var lbReq = prepareLBRequest(sreq, shard, params, urls); | ||
final var srsp = prepareShardResponse(sreq, shard); | ||
final var ssr = new SimpleSolrResponse(); | ||
srsp.setSolrResponse(ssr); | ||
pending.incrementAndGet(); | ||
|
||
if (urls.isEmpty()) { | ||
recordNoUrlShardResponse(srsp, shard); | ||
return; | ||
} | ||
|
||
long startTime = System.nanoTime(); | ||
final Runnable executeRequestRunnable = | ||
() -> { | ||
CompletableFuture<LBSolrClient.Rsp> future = this.lbClient.requestAsync(lbReq); | ||
future.whenComplete( | ||
(rsp, throwable) -> { | ||
if (rsp != null) { | ||
ssr.nl = rsp.getResponse(); | ||
srsp.setShardAddress(rsp.getServer()); | ||
ssr.elapsedTime = | ||
TimeUnit.MILLISECONDS.convert( | ||
System.nanoTime() - startTime, TimeUnit.NANOSECONDS); | ||
responses.add(srsp); | ||
} else if (throwable != null) { | ||
ssr.elapsedTime = | ||
TimeUnit.MILLISECONDS.convert( | ||
System.nanoTime() - startTime, TimeUnit.NANOSECONDS); | ||
srsp.setException(throwable); | ||
if (throwable instanceof SolrException) { | ||
srsp.setResponseCode(((SolrException) throwable).code()); | ||
} | ||
responses.add(srsp); | ||
} | ||
}); | ||
responseFutureMap.put(srsp, future); | ||
}; | ||
|
||
CompletableFuture.runAsync(executeRequestRunnable, commExecutor); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
solr/core/src/java/org/apache/solr/handler/component/ParallelHttpShardHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.handler.component; | ||
|
||
/** Creates {@link ParallelHttpShardHandler} instances */ | ||
public class ParallelHttpShardHandlerFactory extends HttpShardHandlerFactory { | ||
|
||
@Override | ||
public ShardHandler getShardHandler() { | ||
return new ParallelHttpShardHandler(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.