Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/2880527 to your computer and use it in GitHub Desktop.
Save nolanlawson/2880527 to your computer and use it in GitHub Desktop.
Slight hack of the QuerySenderListener that fixes a bug in Solr 3.5.0 where spellcheck components that use collation/onlyMorePopular with query components get stuck on Thread.wait(). Plus, it's faster.
/**
* 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.core;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.search.DocList;
import org.apache.solr.search.DocIterator;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
/**
* Same as org.apache.solr.core, but splits up the LocalSolrQueryRequests into separate threads, for two
* purposes:
*
* 1) It's way faster when it's parallelized.
*
* 2) There's a bug in the original QuerySenderListener (as of 3.5.0) that causes spellcheck components
* that use collation (with an attached query component) to hang forever, because the thread gets stuck
* in SolrCore at searcherLock.wait().
*
* modified by Nolan Lawson
*
* @version $Id: QuerySenderListener.java 1137045 2011-06-17 21:49:38Z erick $
*/
class MultithreadedQuerySenderListener extends AbstractSolrEventListener {
private static final int NUM_THREADS = 8;
public MultithreadedQuerySenderListener(SolrCore core) {
super(core);
}
@Override
public void newSearcher(final SolrIndexSearcher newSearcher, final SolrIndexSearcher currentSearcher) {
final SolrIndexSearcher searcher = newSearcher;
// use a fixed thread pool to avoid overhead of thread creation
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
log.info("QuerySenderListener sending requests to " + newSearcher);
List<NamedList> allLists = (List<NamedList>)args.get("queries");
if (allLists == null) return;
for (final NamedList nlst : allLists) {
Runnable runnable = new Runnable() {
public void run() {
try {
// bind the request to a particular searcher (the newSearcher)
NamedList params = addEventParms(currentSearcher, nlst);
LocalSolrQueryRequest req = new LocalSolrQueryRequest(core,params) {
@Override public SolrIndexSearcher getSearcher() { return searcher; }
@Override public void close() { }
};
SolrQueryResponse rsp = new SolrQueryResponse();
core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
// Retrieve the Document instances (not just the ids) to warm
// the OS disk cache, and any Solr document cache. Only the top
// level values in the NamedList are checked for DocLists.
NamedList values = rsp.getValues();
for (int i=0; i<values.size(); i++) {
Object o = values.getVal(i);
if (o instanceof DocList) {
DocList docs = (DocList)o;
for (DocIterator iter = docs.iterator(); iter.hasNext();) {
newSearcher.doc(iter.nextDoc());
}
}
}
req.close();
} catch (Exception e) {
// do nothing... we want to continue with the other requests.
// the failure should have already been logged.
e.printStackTrace();
}
}
};
executorService.submit(runnable);
}
executorService.shutdown();
log.info("QuerySenderListener done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment