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.
This commit is contained in:
sixcooler 2013-11-27 19:07:36 +01:00
parent ae55d69ef6
commit 94db054aff

View File

@ -109,6 +109,7 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
@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() {
@ -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<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
@ -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) {}
}