You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mk...@apache.org on 2019/06/21 20:39:10 UTC

[lucene-solr] branch master updated: SOLR-13545: ContentStreamUpdateRequest to close file.

This is an automated email from the ASF dual-hosted git repository.

mkhl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/master by this push:
     new 9137a0b  SOLR-13545: ContentStreamUpdateRequest to close file.
9137a0b is described below

commit 9137a0b0fe618ad0762fbda56eab6f6e5c7570e8
Author: Mikhail Khludnev <mk...@apache.org>
AuthorDate: Fri Jun 21 23:37:44 2019 +0300

    SOLR-13545: ContentStreamUpdateRequest to close file.
---
 solr/CHANGES.txt                                   |  4 +-
 .../solrj/request/ContentStreamUpdateRequest.java  |  4 +-
 .../apache/solr/client/solrj/SolrExampleTests.java | 48 +++++++++++++++++++---
 3 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a04bf07..ff33bf8 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -172,9 +172,11 @@ Bug Fixes
 * SOLR-13333: unleashing terms.ttf from terms.list when distrib=false (Munendra S N via Mikhail Khludnev)
 
 * SOLR-13490: Fix CollectionStateWatcher/CollectionStatePredicate based APIs in ZkStateReader and
-  CloudSolrClient to be triggered on liveNode changes.  Also add Predicate<DocCollection> equivilents
+  CloudSolrClient to be triggered on liveNode changes.  Also add Predicate<DocCollection> equivalents
   for callers that don't care about liveNodes. (hossman)
 
+* SOLR-13545: ContentStreamUpdateRequest refused to close file (Colvin Cowie, Mikhail Khludnev)
+
 Other Changes
 ----------------------
 
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
index 28d7874..6b387c0 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/ContentStreamUpdateRequest.java
@@ -61,7 +61,9 @@ public class ContentStreamUpdateRequest extends AbstractUpdateRequest {
     return new RequestWriter.ContentWriter() {
       @Override
       public void write(OutputStream os) throws IOException {
-        IOUtils.copy(stream.getStream(), os);
+        try(var inStream = stream.getStream()) {
+          IOUtils.copy(inStream, os);
+        }
       }
 
       @Override
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
index 4049064..4c1048c 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
@@ -17,11 +17,17 @@
 package org.apache.solr.client.solrj;
 
 
+import static org.apache.solr.common.params.UpdateParams.ASSUME_CONTENT_TYPE;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.core.StringContains.containsString;
+
+import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.invoke.MethodHandles;
 import java.nio.ByteBuffer;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -31,8 +37,6 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Random;
 
-import com.google.common.collect.Maps;
-import junit.framework.Assert;
 import org.apache.lucene.util.TestUtil;
 import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
 import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
@@ -68,17 +72,17 @@ import org.apache.solr.common.params.AnalysisParams;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.params.FacetParams;
 import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.ContentStreamBase;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.Pair;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.noggit.JSONParser;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.solr.common.params.UpdateParams.ASSUME_CONTENT_TYPE;
-import static org.hamcrest.core.StringContains.containsString;
-import static org.hamcrest.CoreMatchers.is;
+import com.google.common.collect.Maps;
 
 /**
  * This should include tests against the example solr config
@@ -710,13 +714,45 @@ abstract public class SolrExampleTests extends SolrExampleTestsBase
     Assert.assertEquals(0, rsp.getResults().getNumFound());
 
     ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
-    up.addFile(getFile("solrj/books.csv"), "application/csv");
+    var file = getFile("solrj/books.csv");
+    final int opened[] =  new int[] {0};
+    final int closed[] =  new int[] {0};
+
+    var assertClosed = random().nextBoolean();
+    if (assertClosed) {
+      var allBytes = Files.readAllBytes(file.toPath());
+      
+      var contentStreamMock = new ContentStreamBase.ByteArrayStream(allBytes, "solrj/books.csv", "application/csv") {
+        @Override
+        public InputStream getStream() throws IOException {
+          opened [0]++;
+          return new ByteArrayInputStream( allBytes ) {
+            @Override
+            public void close() throws IOException {
+              super.close();
+              closed[0]++;
+            }
+          };
+        }
+      };
+      up.addContentStream(contentStreamMock);
+    } else {
+      up.addFile(file, "application/csv");
+    }
+    
     up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
     NamedList<Object> result = client.request(up);
     assertNotNull("Couldn't upload books.csv", result);
+    
+    if (assertClosed) {
+      assertEquals("open only once",1, opened[0]);
+      assertEquals("close exactly once",1, closed[0]);
+    }
     rsp = client.query( new SolrQuery( "*:*") );
     Assert.assertEquals( 10, rsp.getResults().getNumFound() );
  }
+ 
+ 
   @Test
   public void testStreamingRequest() throws Exception {
     SolrClient client = getSolrClient();