Skip to content

Commit

Permalink
memory-leak-fix: the DocListSearcher fires an query in its constructor
Browse files Browse the repository at this point in the history
and it is highly recommend to close every SolrRequest.
Every Request, which is not closed leaves a Searcher with its Chaches an
can not be garbage-collectet.
  • Loading branch information
sixcooler committed Nov 27, 2013
1 parent ae55d69 commit 94db054
Showing 1 changed file with 35 additions and 26 deletions.
Expand Up @@ -109,6 +109,7 @@ public void clearCaches() {
@SuppressWarnings("unchecked")
SolrCache<Integer, Document> documentCache = solrConfig.documentCacheConfig == null ? null : solrConfig.documentCacheConfig.newInstance();
if (documentCache != null) documentCache.clear();
this.core.getInfoRegistry().clear(); // don't know what this is for - but this is getting huge!
}

public SolrInstance getInstance() {
Expand Down Expand Up @@ -237,9 +238,14 @@ public void close() {

@Override
public long getCountByQuery(String querystring) {
DocListSearcher docListSearcher = new DocListSearcher(querystring, 0, 0, CollectionSchema.id.getSolrFieldName());
int numFound = docListSearcher.response.matches();
docListSearcher.close();
int numFound = 0;
DocListSearcher docListSearcher = null;
try {
docListSearcher = new DocListSearcher(querystring, 0, 0, CollectionSchema.id.getSolrFieldName());
numFound = docListSearcher.response.matches();
} finally {
if (docListSearcher != null) docListSearcher.close();
}
return numFound;
}

Expand Down Expand Up @@ -276,20 +282,23 @@ public Set<String> existsByIds(Set<String> ids) {

@Override
public String getFieldById(final String id, final String field) throws IOException {
DocListSearcher docListSearcher = new DocListSearcher("{!raw f=" + CollectionSchema.id.getSolrFieldName() + "}" + id, 0, 1, CollectionSchema.id.getSolrFieldName());
int numFound = docListSearcher.response.matches();
if (numFound == 0) return null;
Set<String> solrFields = new HashSet<String>();
solrFields.add(field);
try {
Document doc = docListSearcher.request.getSearcher().doc(docListSearcher.response.iterator().nextDoc(), solrFields);
return doc.get(field);
String ret = null;
DocListSearcher docListSearcher = null;
try {
docListSearcher = new DocListSearcher("{!raw f=" + CollectionSchema.id.getSolrFieldName() + "}" + id, 0, 1, CollectionSchema.id.getSolrFieldName());
int numFound = docListSearcher.response.matches();
if (numFound > 0) {
Set<String> solrFields = new HashSet<String>();
solrFields.add(field);
Document doc = docListSearcher.request.getSearcher().doc(docListSearcher.response.iterator().nextDoc(), solrFields);
ret = doc.get(field);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
docListSearcher.close();
if (docListSearcher != null) docListSearcher.close();
}
return null;
return ret;
}

@Override
Expand All @@ -299,27 +308,27 @@ public BlockingQueue<String> concurrentIDsByQuery(final String querystring, fina
final Thread t = new Thread() {
@Override
public void run() {
int o = offset;
int o = offset, responseCount = 0;
DocListSearcher docListSearcher = null;
while (System.currentTimeMillis() < endtime) {
try {
DocListSearcher docListSearcher = new DocListSearcher(querystring, o, pagesize, CollectionSchema.id.getSolrFieldName());
int responseCount = docListSearcher.response.size();
responseCount = 0;
docListSearcher = new DocListSearcher(querystring, o, pagesize, CollectionSchema.id.getSolrFieldName());
responseCount = docListSearcher.response.size();
SolrIndexSearcher searcher = docListSearcher.request.getSearcher();
DocIterator iterator = docListSearcher.response.iterator();
try {
for (int i = 0; i < responseCount; i++) {
Document doc = searcher.doc(iterator.nextDoc(), SOLR_ID_FIELDS);
try {queue.put(doc.get(CollectionSchema.id.getSolrFieldName()));} catch (final InterruptedException e) {break;}
}
} catch (IOException e) {
} finally {
docListSearcher.close();
for (int i = 0; i < responseCount; i++) {
Document doc = searcher.doc(iterator.nextDoc(), SOLR_ID_FIELDS);
try {queue.put(doc.get(CollectionSchema.id.getSolrFieldName()));} catch (final InterruptedException e) {break;}
}
if (responseCount < pagesize) break;
o += pagesize;
} catch (final SolrException e) {
break;
} catch (IOException e) {
} finally {
if (docListSearcher != null) docListSearcher.close();
}
if (responseCount < pagesize) break;
o += pagesize;
}
try {queue.put(AbstractSolrConnector.POISON_ID);} catch (final InterruptedException e1) {}
}
Expand Down

0 comments on commit 94db054

Please sign in to comment.