From 9137a0b0fe618ad0762fbda56eab6f6e5c7570e8 Mon Sep 17 00:00:00 2001 From: Mikhail Khludnev Date: Fri, 21 Jun 2019 23:37:44 +0300 Subject: [PATCH] SOLR-13545: ContentStreamUpdateRequest to close file. --- solr/CHANGES.txt | 4 +- .../request/ContentStreamUpdateRequest.java | 4 +- .../solr/client/solrj/SolrExampleTests.java | 48 ++++++++++++++++--- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index a04bf07590b..ff33bf8c28e 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 equivilents + CloudSolrClient to be triggered on liveNode changes. Also add Predicate 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 28d787426c6..6b387c03caa 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 404906408a8..4c1048ccfc2 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 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();