From 94db054afffe383cd4c3524ba6d236a29df4f30c Mon Sep 17 00:00:00 2001 From: sixcooler Date: Wed, 27 Nov 2013 19:07:36 +0100 Subject: [PATCH] memory-leak-fix: the DocListSearcher fires an query in its constructor 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. --- .../solr/connector/EmbeddedSolrConnector.java | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java b/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java index 669378853..87bf4dbc1 100644 --- a/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java +++ b/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java @@ -109,6 +109,7 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo @SuppressWarnings("unchecked") SolrCache 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() { @@ -237,9 +238,14 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo @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; } @@ -276,20 +282,23 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo @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 solrFields = new HashSet(); - 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 solrFields = new HashSet(); + 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 @@ -299,27 +308,27 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo 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) {} }