Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-13622: Fix file-handle leak

Jason Gerlowski2546e7a2ec57
diff --git a/solr/core/src/java/org/apache/solr/handler/CatStream.java b/solr/core/src/java/org/apache/solr/handler/CatStream.java
index 17747550582..6a4752e6673 100644
--- a/solr/core/src/java/org/apache/solr/handler/CatStream.java
+++ b/solr/core/src/java/org/apache/solr/handler/CatStream.java
@@ -125,13 +125,14 @@ public class CatStream extends TupleStream implements Expressible {
@Override
public Tuple read() throws IOException {
if (maxLines >= 0 && linesReturned >= maxLines) {
- if (currentFileLines != null) currentFileLines.close();
+ closeCurrentFileIfSet();
return createEofTuple();
} else if (currentFileHasMoreLinesToRead()) {
return fetchNextLineFromCurrentFile();
} else if (advanceToNextFileWithData()) {
return fetchNextLineFromCurrentFile();
} else { // No more data
+ closeCurrentFileIfSet();
return createEofTuple();
}
}
@@ -187,9 +188,7 @@ public class CatStream extends TupleStream implements Expressible {
private boolean advanceToNextFileWithData() throws IOException {
while (allFilesToCrawl.hasNext()) {
- if (currentFileLines != null) {
- currentFileLines.close();
- }
+ closeCurrentFileIfSet();
currentFilePath = allFilesToCrawl.next();
currentFileLines = FileUtils.lineIterator(new File(currentFilePath.absolutePath), "UTF-8");
if (currentFileLines.hasNext()) return true;
@@ -221,6 +220,14 @@ public class CatStream extends TupleStream implements Expressible {
return Paths.get(chroot, pathRelativeToChroot).toString();
}
+ private void closeCurrentFileIfSet() {
+ if (currentFilePath != null) {
+ currentFileLines.close();
+ currentFilePath = null;
+ currentFileLines = null;
+ }
+ }
+
private void findReadableFiles(CrawlFile seed, List<CrawlFile> foundFiles) {
final File entry = new File(seed.absolutePath);
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
index ee63a2cc5ed..9c6e345fc9c 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java
@@ -3085,6 +3085,46 @@ public class StreamExpressionTest extends SolrCloudTestCase {
}
}
+ @Test
+ public void testCatStreamEmptyFile() throws Exception {
+ final String catStream = "cat(\"topLevel-empty.txt\")";
+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
+ paramsLoc.set("expr", catStream);
+ paramsLoc.set("qt", "/stream");
+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+FILESTREAM_COLLECTION;
+
+ SolrStream solrStream = new SolrStream(url, paramsLoc);
+
+ StreamContext context = new StreamContext();
+ solrStream.setStreamContext(context);
+ List<Tuple> tuples = getTuples(solrStream);
+
+ assertEquals(0, tuples.size());
+ }
+
+ @Test
+ public void testCatStreamMultipleFilesOneEmpty() throws Exception {
+ final String catStream = "cat(\"topLevel1.txt,topLevel-empty.txt\")";
+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();
+ paramsLoc.set("expr", catStream);
+ paramsLoc.set("qt", "/stream");
+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+FILESTREAM_COLLECTION;
+
+ SolrStream solrStream = new SolrStream(url, paramsLoc);
+
+ StreamContext context = new StreamContext();
+ solrStream.setStreamContext(context);
+ List<Tuple> tuples = getTuples(solrStream);
+
+ assertEquals(4, tuples.size());
+
+ for (int i = 0; i < 4; i++) {
+ Tuple t = tuples.get(i);
+ assertEquals("topLevel1.txt line " + String.valueOf(i+1), t.get("line"));
+ assertEquals("topLevel1.txt", t.get("file"));
+ }
+ }
+
@Test
public void testCatStreamMaxLines() throws Exception {
final String catStream = "cat(\"topLevel1.txt\", maxLines=2)";
@@ -3199,6 +3239,7 @@ public class StreamExpressionTest extends SolrCloudTestCase {
* dataDir
* |- topLevel1.txt
* |- topLevel2.txt
+ * |- topLevel-empty.txt
* |- directory1
* |- secondLevel1.txt
* |- secondLevel2.txt
@@ -3213,10 +3254,12 @@ public class StreamExpressionTest extends SolrCloudTestCase {
final File topLevel1 = new File(Paths.get(dataDir, "topLevel1.txt").toString());
final File topLevel2 = new File(Paths.get(dataDir, "topLevel2.txt").toString());
+ final File topLevelEmpty = new File(Paths.get(dataDir, "topLevel-empty.txt").toString());
final File secondLevel1 = new File(Paths.get(dataDir, "directory1", "secondLevel1.txt").toString());
final File secondLevel2 = new File(Paths.get(dataDir, "directory1", "secondLevel2.txt").toString());
populateFileWithData(topLevel1);
populateFileWithData(topLevel2);
+ topLevelEmpty.createNewFile();
populateFileWithData(secondLevel1);
populateFileWithData(secondLevel2);
}