SOLR-13545: ContentStreamUpdateRequest to close file.

This commit is contained in:
Mikhail Khludnev 2019-06-21 23:37:44 +03:00
parent b3e759a658
commit 9137a0b0fe
3 changed files with 48 additions and 8 deletions

View File

@ -172,9 +172,11 @@ Bug Fixes
* SOLR-13333: unleashing terms.ttf from terms.list when distrib=false (Munendra S N via Mikhail Khludnev) * 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 * 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) for callers that don't care about liveNodes. (hossman)
* SOLR-13545: ContentStreamUpdateRequest refused to close file (Colvin Cowie, Mikhail Khludnev)
Other Changes Other Changes
---------------------- ----------------------

View File

@ -61,7 +61,9 @@ public class ContentStreamUpdateRequest extends AbstractUpdateRequest {
return new RequestWriter.ContentWriter() { return new RequestWriter.ContentWriter() {
@Override @Override
public void write(OutputStream os) throws IOException { public void write(OutputStream os) throws IOException {
IOUtils.copy(stream.getStream(), os); try(var inStream = stream.getStream()) {
IOUtils.copy(inStream, os);
}
} }
@Override @Override

View File

@ -17,11 +17,17 @@
package org.apache.solr.client.solrj; 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.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -31,8 +37,6 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import com.google.common.collect.Maps;
import junit.framework.Assert;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4.SuppressSSL; import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; 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.CommonParams;
import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.params.FacetParams;
import org.apache.solr.common.params.ModifiableSolrParams; 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.NamedList;
import org.apache.solr.common.util.Pair; import org.apache.solr.common.util.Pair;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.noggit.JSONParser; import org.noggit.JSONParser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.apache.solr.common.params.UpdateParams.ASSUME_CONTENT_TYPE; import com.google.common.collect.Maps;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.CoreMatchers.is;
/** /**
* This should include tests against the example solr config * 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()); Assert.assertEquals(0, rsp.getResults().getNumFound());
ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update"); 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); up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
NamedList<Object> result = client.request(up); NamedList<Object> result = client.request(up);
assertNotNull("Couldn't upload books.csv", result); 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( "*:*") ); rsp = client.query( new SolrQuery( "*:*") );
Assert.assertEquals( 10, rsp.getResults().getNumFound() ); Assert.assertEquals( 10, rsp.getResults().getNumFound() );
} }
@Test @Test
public void testStreamingRequest() throws Exception { public void testStreamingRequest() throws Exception {
SolrClient client = getSolrClient(); SolrClient client = getSolrClient();